2021-01-31 03:43:09 +00:00
|
|
|
/*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2019 Ha Thach (tinyusb.org)
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2021-06-12 19:35:46 +00:00
|
|
|
#include "tusb_config.h"
|
|
|
|
|
2021-06-06 03:10:36 +00:00
|
|
|
#include "bsp/board.h" /* a tinyusb header */
|
2021-01-31 03:43:09 +00:00
|
|
|
#include "tusb.h"
|
|
|
|
|
|
|
|
#include "DAP_config.h" /* ARM code *assumes* this is included prior to DAP.h */
|
|
|
|
#include "DAP.h"
|
|
|
|
|
2021-06-06 03:10:36 +00:00
|
|
|
#include "protocfg.h"
|
|
|
|
#include "protos.h"
|
|
|
|
|
2021-06-07 23:22:55 +00:00
|
|
|
#include "libco.h"
|
|
|
|
#include "thread.h"
|
|
|
|
|
2021-06-06 03:10:36 +00:00
|
|
|
#ifdef PICO_BOARD
|
|
|
|
#include <pico/binary_info.h>
|
|
|
|
#endif
|
2021-02-25 23:45:13 +00:00
|
|
|
|
2021-06-07 23:22:55 +00:00
|
|
|
static cothread_t mainthread
|
|
|
|
#ifdef DBOARD_HAS_UART
|
|
|
|
, uartthread
|
|
|
|
#endif
|
|
|
|
#ifdef DBOARD_HAS_SERPROG
|
|
|
|
, serprogthread
|
|
|
|
#endif
|
|
|
|
;
|
|
|
|
|
|
|
|
void thread_yield(void) {
|
|
|
|
co_switch(mainthread);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DBOARD_HAS_UART
|
|
|
|
static void uart_thread_fn(void) {
|
|
|
|
cdc_uart_init();
|
|
|
|
thread_yield();
|
|
|
|
while (1) {
|
|
|
|
cdc_uart_task();
|
|
|
|
thread_yield();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef DBOARD_HAS_SERPROG
|
|
|
|
static void serprog_thread_fn(void) {
|
|
|
|
cdc_serprog_init();
|
|
|
|
thread_yield();
|
|
|
|
while (1) {
|
|
|
|
cdc_serprog_task();
|
|
|
|
thread_yield();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef DBOARD_HAS_UART
|
|
|
|
static uint8_t uartstack[4096];
|
|
|
|
#endif
|
|
|
|
#ifdef DBOARD_HAS_UART
|
|
|
|
static uint8_t serprogstack[4096];
|
|
|
|
#endif
|
|
|
|
|
|
|
|
extern uint32_t co_active_buffer[64];
|
|
|
|
uint32_t co_active_buffer[64];
|
|
|
|
extern cothread_t co_active_handle;
|
|
|
|
cothread_t co_active_handle;
|
|
|
|
|
2021-01-31 03:43:09 +00:00
|
|
|
int main(void)
|
|
|
|
{
|
2021-06-07 23:22:55 +00:00
|
|
|
mainthread = co_active();
|
|
|
|
|
2021-06-06 03:10:36 +00:00
|
|
|
// TODO: split this out in a bsp-specific file
|
2021-06-12 19:35:46 +00:00
|
|
|
#if defined(PICO_BOARD) && !defined(USE_USBCDC_FOR_STDIO)
|
2021-06-06 03:10:36 +00:00
|
|
|
// use hardcoded values from TinyUSB board.h
|
|
|
|
bi_decl(bi_2pins_with_func(0, 1, GPIO_FUNC_UART));
|
|
|
|
#endif
|
2021-01-31 03:43:09 +00:00
|
|
|
board_init();
|
2021-06-06 03:10:36 +00:00
|
|
|
|
|
|
|
#ifdef DBOARD_HAS_UART
|
2021-06-07 23:22:55 +00:00
|
|
|
//cdc_uart_init();
|
|
|
|
uartthread = co_derive(uartstack, sizeof uartstack, uart_thread_fn);
|
|
|
|
co_switch(uartthread); // will call cdc_uart_init() on correct thread
|
2021-06-06 03:10:36 +00:00
|
|
|
#endif
|
|
|
|
#ifdef DBOARD_HAS_SERPROG
|
2021-06-07 23:22:55 +00:00
|
|
|
//cdc_serprog_init();
|
|
|
|
serprogthread = co_derive(serprogstack, sizeof serprogstack, serprog_thread_fn);
|
|
|
|
co_switch(serprogthread); // will call cdc_serprog_init() on correct thread
|
2021-06-06 03:10:36 +00:00
|
|
|
#endif
|
|
|
|
#ifdef DBOARD_HAS_CMSISDAP
|
2021-01-31 03:43:09 +00:00
|
|
|
DAP_Setup();
|
2021-06-06 03:10:36 +00:00
|
|
|
#endif
|
2021-01-31 03:43:09 +00:00
|
|
|
|
|
|
|
tusb_init();
|
|
|
|
|
2021-06-12 19:35:46 +00:00
|
|
|
#ifdef USE_USBCDC_FOR_STDIO
|
|
|
|
stdio_usb_init();
|
|
|
|
#endif
|
|
|
|
|
2021-01-31 03:43:09 +00:00
|
|
|
while (1)
|
|
|
|
{
|
2021-06-08 00:23:02 +00:00
|
|
|
//printf("hi\n");
|
|
|
|
|
2021-01-31 03:43:09 +00:00
|
|
|
tud_task(); // tinyusb device task
|
2021-06-06 03:10:36 +00:00
|
|
|
#ifdef DBOARD_HAS_UART
|
2021-06-07 23:22:55 +00:00
|
|
|
//cdc_uart_task();
|
|
|
|
co_switch(uartthread);
|
2021-06-06 03:10:36 +00:00
|
|
|
#endif
|
2021-06-07 23:22:55 +00:00
|
|
|
|
|
|
|
tud_task(); // tinyusb device task
|
2021-06-06 03:10:36 +00:00
|
|
|
#ifdef DBOARD_HAS_SERPROG
|
2021-06-07 23:22:55 +00:00
|
|
|
//cdc_serprog_task();
|
|
|
|
co_switch(serprogthread);
|
2021-06-06 03:10:36 +00:00
|
|
|
#endif
|
2021-06-07 23:22:55 +00:00
|
|
|
|
|
|
|
//printf("hi\n");
|
2021-01-31 03:43:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------+
|
|
|
|
// USB HID
|
|
|
|
//--------------------------------------------------------------------+
|
|
|
|
|
|
|
|
// Invoked when received GET_REPORT control request
|
|
|
|
// Application must fill buffer report's content and return its length.
|
|
|
|
// Return zero will cause the stack to STALL request
|
2021-06-13 18:07:54 +00:00
|
|
|
uint16_t tud_hid_get_report_cb(uint8_t instance, uint8_t report_id, hid_report_type_t report_type, uint8_t* buffer, uint16_t reqlen)
|
2021-01-31 03:43:09 +00:00
|
|
|
{
|
|
|
|
// TODO not Implemented
|
2021-06-13 18:07:54 +00:00
|
|
|
(void) instance;
|
2021-01-31 03:43:09 +00:00
|
|
|
(void) report_id;
|
|
|
|
(void) report_type;
|
|
|
|
(void) buffer;
|
|
|
|
(void) reqlen;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-06-13 18:07:54 +00:00
|
|
|
void tud_hid_set_report_cb(uint8_t instance, uint8_t report_id, hid_report_type_t report_type, uint8_t const* RxDataBuffer, uint16_t bufsize)
|
2021-01-31 03:43:09 +00:00
|
|
|
{
|
2021-02-01 00:18:22 +00:00
|
|
|
static uint8_t TxDataBuffer[CFG_TUD_HID_EP_BUFSIZE];
|
2021-01-31 03:43:09 +00:00
|
|
|
uint32_t response_size = TU_MIN(CFG_TUD_HID_EP_BUFSIZE, bufsize);
|
|
|
|
|
|
|
|
// This doesn't use multiple report and report ID
|
2021-06-13 18:07:54 +00:00
|
|
|
(void) instance;
|
2021-01-31 03:43:09 +00:00
|
|
|
(void) report_id;
|
|
|
|
(void) report_type;
|
|
|
|
|
2021-06-06 03:10:36 +00:00
|
|
|
#ifdef DBOARD_HAS_CMSISDAP
|
2021-01-31 03:43:09 +00:00
|
|
|
DAP_ProcessCommand(RxDataBuffer, TxDataBuffer);
|
2021-06-06 03:10:36 +00:00
|
|
|
#endif
|
2021-01-31 03:43:09 +00:00
|
|
|
|
|
|
|
tud_hid_report(0, TxDataBuffer, response_size);
|
|
|
|
}
|