From cd75a36c65229e44099f20cb4fb985ca0f2e7054 Mon Sep 17 00:00:00 2001 From: Alex Taradov Date: Sat, 30 Jul 2022 18:57:35 -0700 Subject: [PATCH] Implemented 2 DAP buffers (for SAM D11 for now) --- platform/samd11/dap_config.h | 2 +- platform/samd11/hal_config.h | 4 +- platform/samd11/main.c | 77 ++++++++++++++++++++++++++++++------ 3 files changed, 67 insertions(+), 16 deletions(-) diff --git a/platform/samd11/dap_config.h b/platform/samd11/dap_config.h index f17955a..cdff536 100644 --- a/platform/samd11/dap_config.h +++ b/platform/samd11/dap_config.h @@ -13,7 +13,7 @@ #define DAP_CONFIG_DEFAULT_CLOCK 1000000 // Hz #define DAP_CONFIG_PACKET_SIZE 64 -#define DAP_CONFIG_PACKET_COUNT 1 +#define DAP_CONFIG_PACKET_COUNT 2 #define DAP_CONFIG_JTAG_DEV_COUNT 8 diff --git a/platform/samd11/hal_config.h b/platform/samd11/hal_config.h index c3a5927..74e9e9a 100644 --- a/platform/samd11/hal_config.h +++ b/platform/samd11/hal_config.h @@ -9,10 +9,10 @@ #include "hal_gpio.h" /*- Definitions -------------------------------------------------------------*/ -//#define HAL_BOARD_STD +#define HAL_BOARD_STD //#define HAL_BOARD_VCP_V1 //#define HAL_BOARD_VCP_V3 -#define HAL_BOARD_OBD +//#define HAL_BOARD_OBD #if defined(HAL_BOARD_CUSTOM) // Externally supplied board configuration takes precedence diff --git a/platform/samd11/main.c b/platform/samd11/main.c index cec942b..cba1f87 100644 --- a/platform/samd11/main.c +++ b/platform/samd11/main.c @@ -22,9 +22,14 @@ /*- Variables ---------------------------------------------------------------*/ static alignas(4) uint8_t app_req_buf_hid[DAP_CONFIG_PACKET_SIZE]; -static alignas(4) uint8_t app_resp_buf_hid[DAP_CONFIG_PACKET_SIZE]; static alignas(4) uint8_t app_req_buf_bulk[DAP_CONFIG_PACKET_SIZE]; -static alignas(4) uint8_t app_resp_buf_bulk[DAP_CONFIG_PACKET_SIZE]; +static alignas(4) uint8_t app_req_buf[DAP_CONFIG_PACKET_SIZE]; +static alignas(4) uint8_t app_resp_buf[DAP_CONFIG_PACKET_SIZE]; +static int app_req_buf_hid_size = 0; +static int app_req_buf_bulk_size = 0; +static int app_resp_size = 0; +static bool app_resp_free = true; +static int app_resp_interface; static uint64_t app_system_time = 0; static uint64_t app_status_timeout = 0; static bool app_dap_event = false; @@ -253,37 +258,35 @@ void usb_cdc_recv_callback(int size) //----------------------------------------------------------------------------- void usb_hid_send_callback(void) { - usb_hid_recv(app_req_buf_hid, DAP_CONFIG_PACKET_SIZE); + app_resp_free = true; } //----------------------------------------------------------------------------- void usb_hid_recv_callback(int size) { - app_dap_event = true; - dap_process_request(app_req_buf_hid, sizeof(app_req_buf_hid), - app_resp_buf_hid, sizeof(app_resp_buf_hid)); - usb_hid_send(app_resp_buf_hid, sizeof(app_resp_buf_hid)); - (void)size; + app_req_buf_hid_size = size; } //----------------------------------------------------------------------------- static void usb_bulk_send_callback(void) { - usb_recv(USB_BULK_EP_RECV, app_req_buf_bulk, sizeof(app_req_buf_bulk)); + app_resp_free = true; } //----------------------------------------------------------------------------- static void usb_bulk_recv_callback(int size) { - app_dap_event = true; - size = dap_process_request(app_req_buf_bulk, size, - app_resp_buf_bulk, sizeof(app_resp_buf_bulk)); - usb_send(USB_BULK_EP_SEND, app_resp_buf_bulk, size); + app_req_buf_bulk_size = size; } //----------------------------------------------------------------------------- void usb_configuration_callback(int config) { + app_resp_size = 0; + app_resp_free = true; + app_req_buf_hid_size = 0; + app_req_buf_bulk_size = 0; + usb_set_send_callback(USB_BULK_EP_SEND, usb_bulk_send_callback); usb_set_recv_callback(USB_BULK_EP_RECV, usb_bulk_recv_callback); @@ -325,6 +328,52 @@ static void status_timer_task(void) #endif } +//----------------------------------------------------------------------------- +static void dap_task(void) +{ + int size; + + if (app_resp_size && app_resp_free) + { + if (USB_INTF_BULK == app_resp_interface) + usb_send(USB_BULK_EP_SEND, app_resp_buf, app_resp_size); + else + usb_hid_send(app_resp_buf, sizeof(app_resp_buf)); + + app_resp_free = false; + app_resp_size = 0; + } + + if (app_req_buf_hid_size) + { + size = app_req_buf_hid_size; + app_req_buf_hid_size = 0; + app_resp_interface = USB_INTF_HID; + + memcpy(app_req_buf, app_req_buf_hid, size); + + usb_hid_recv(app_req_buf_hid, sizeof(app_req_buf_hid)); + } + else if (app_req_buf_bulk_size) + { + size = app_req_buf_bulk_size; + app_req_buf_bulk_size = 0; + app_resp_interface = USB_INTF_BULK; + + memcpy(app_req_buf, app_req_buf_bulk, size); + + usb_recv(USB_BULK_EP_RECV, app_req_buf_bulk, sizeof(app_req_buf_bulk)); + } + else + { + return; + } + + app_dap_event = true; + + app_resp_size = dap_process_request(app_req_buf, size, app_resp_buf, sizeof(app_resp_buf)); +} + //----------------------------------------------------------------------------- int main(void) { @@ -356,6 +405,7 @@ int main(void) sys_time_task(); status_timer_task(); usb_task(); + dap_task(); #ifdef HAL_CONFIG_ENABLE_VCP tx_task(); @@ -370,3 +420,4 @@ int main(void) return 0; } +