From 1e3eb6dcb4b45600b27e29193893326e30da49dd Mon Sep 17 00:00:00 2001 From: Brian Esquilona Date: Fri, 31 Aug 2018 13:53:15 -0500 Subject: [PATCH 1/2] Added LED state to turn off blinking or go back to MAIN_LED_DEF state. MAIN_LED_OFF is now MAIN_LED_DEF for default state. --- source/daplink/bootloader/main.c | 33 ++++++++++----- source/daplink/bootloader/main.h | 2 +- source/daplink/interface/main.c | 69 +++++++++++++++++++------------- source/daplink/interface/main.h | 2 +- 4 files changed, 67 insertions(+), 39 deletions(-) diff --git a/source/daplink/bootloader/main.c b/source/daplink/bootloader/main.c index f3256a44a..2fed2976a 100644 --- a/source/daplink/bootloader/main.c +++ b/source/daplink/bootloader/main.c @@ -31,6 +31,11 @@ #include "util.h" #include "cortex_m.h" +//default msc led settings +#ifndef MSC_LED_DEF +#define MSC_LED_DEF GPIO_LED_ON +#endif + __asm void modify_stack_pointer_and_start_app(uint32_t r0_sp, uint32_t r1_pc) { MOV SP, R0 @@ -117,7 +122,7 @@ __task void main_task(void) // State processing uint16_t flags; // LED - gpio_led_state_t msc_led_value = GPIO_LED_ON; + gpio_led_state_t msc_led_value = MSC_LED_DEF; // USB uint32_t usb_state_count; @@ -128,10 +133,10 @@ __task void main_task(void) // Get a reference to this task main_task_id = os_tsk_self(); - // Turn on LEDs + // Set LED defaults gpio_set_hid_led(GPIO_LED_OFF); gpio_set_cdc_led(GPIO_LED_OFF); - gpio_set_msc_led(GPIO_LED_OFF); + gpio_set_msc_led(msc_led_value); // Update version information file info_init(); // USB @@ -216,15 +221,23 @@ __task void main_task(void) // 30mS tick used for flashing LED when USB is busy if (flags & FLAGS_MAIN_30MS) { - if (msc_led_usb_activity && ((msc_led_state == MAIN_LED_FLASH) || (msc_led_state == MAIN_LED_FLASH_PERMANENT))) { - // Flash MSD LED ONCE - msc_led_value = (GPIO_LED_ON == msc_led_value) ? GPIO_LED_OFF : GPIO_LED_ON; - // If in flash mode stop after one cycle but in bootloader LED stays on - if ((GPIO_LED_ON == msc_led_value) && (MAIN_LED_FLASH == msc_led_state)) { + if (msc_led_usb_activity) { + + if((msc_led_state == MAIN_LED_FLASH) || (msc_led_state == MAIN_LED_FLASH_PERMANENT)){ + // Toggle LED value + msc_led_value = (GPIO_LED_ON == msc_led_value) ? GPIO_LED_OFF : GPIO_LED_ON; + // If in flash mode stop after one cycle but in bootloader LED stays on + if ((MSC_LED_DEF == msc_led_value) && (MAIN_LED_FLASH == msc_led_state)) { + msc_led_usb_activity = 0; + msc_led_state = MAIN_LED_DEF; + } + + }else{ + //LED next state is MAIN_LED_DEF + msc_led_value = MSC_LED_DEF; msc_led_usb_activity = 0; - //for now the only place to turn off a blinking state - msc_led_state = MAIN_LED_OFF; } + // Update hardware gpio_set_msc_led(msc_led_value); } diff --git a/source/daplink/bootloader/main.h b/source/daplink/bootloader/main.h index 82d0e16fa..8715861cf 100644 --- a/source/daplink/bootloader/main.h +++ b/source/daplink/bootloader/main.h @@ -54,7 +54,7 @@ typedef enum { * @brief Statest the USB connection can be in */ typedef enum { - MAIN_LED_OFF = 0, + MAIN_LED_DEF = 0, MAIN_LED_FLASH, MAIN_LED_FLASH_PERMANENT } main_led_state_t; diff --git a/source/daplink/interface/main.c b/source/daplink/interface/main.c index c7cfede31..4d42d68b3 100644 --- a/source/daplink/interface/main.c +++ b/source/daplink/interface/main.c @@ -198,9 +198,9 @@ __task void main_task(void) // State processing uint16_t flags = 0; // LED - gpio_led_state_t hid_led_value = GPIO_LED_OFF; - gpio_led_state_t cdc_led_value = GPIO_LED_OFF; - gpio_led_state_t msc_led_value = GPIO_LED_OFF; + gpio_led_state_t hid_led_value = HID_LED_DEF; + gpio_led_state_t cdc_led_value = CDC_LED_DEF; + gpio_led_state_t msc_led_value = MSC_LED_DEF; // USB uint32_t usb_state_count = USB_BUSY_TIME; uint32_t usb_no_config_count = USB_CONFIGURE_TIMEOUT; @@ -215,9 +215,9 @@ __task void main_task(void) // leds gpio_init(); // Turn to LED default settings - gpio_set_hid_led(HID_LED_DEF); - gpio_set_cdc_led(CDC_LED_DEF); - gpio_set_msc_led(MSC_LED_DEF); + gpio_set_hid_led(hid_led_value); + gpio_set_cdc_led(cdc_led_value); + gpio_set_msc_led(msc_led_value); // Initialize the DAP DAP_Setup(); // do some init with the target before USB and files are configured @@ -346,16 +346,21 @@ __task void main_task(void) } // DAP LED - if (hid_led_usb_activity && ((hid_led_state == MAIN_LED_FLASH) || (hid_led_state == MAIN_LED_FLASH_PERMANENT))) { + if (hid_led_usb_activity) { - // Toggle LED value - hid_led_value = GPIO_LED_ON == hid_led_value ? GPIO_LED_OFF : GPIO_LED_ON; + if((hid_led_state == MAIN_LED_FLASH) || (hid_led_state == MAIN_LED_FLASH_PERMANENT)){ + // Toggle LED value + hid_led_value = GPIO_LED_ON == hid_led_value ? GPIO_LED_OFF : GPIO_LED_ON; - // If in flash mode stop after one cycle - if ((HID_LED_DEF == hid_led_value) && (MAIN_LED_FLASH == hid_led_state)) { + // If in flash mode stop after one cycle + if ((HID_LED_DEF == hid_led_value) && (MAIN_LED_FLASH == hid_led_state)) { + hid_led_usb_activity = 0; + hid_led_state = MAIN_LED_DEF; + } + }else{ + //LED next state is MAIN_LED_DEF + hid_led_value = HID_LED_DEF; hid_led_usb_activity = 0; - //for now the only place to turn off a blinking state - hid_led_state = MAIN_LED_OFF; } // Update hardware @@ -363,16 +368,21 @@ __task void main_task(void) } // MSD LED - if (msc_led_usb_activity && ((msc_led_state == MAIN_LED_FLASH) || (msc_led_state == MAIN_LED_FLASH_PERMANENT))) { + if (msc_led_usb_activity) { - // Toggle LED value - msc_led_value = GPIO_LED_ON == msc_led_value ? GPIO_LED_OFF : GPIO_LED_ON; + if((msc_led_state == MAIN_LED_FLASH) || (msc_led_state == MAIN_LED_FLASH_PERMANENT)){ + // Toggle LED value + msc_led_value = GPIO_LED_ON == msc_led_value ? GPIO_LED_OFF : GPIO_LED_ON; - // If in flash mode stop after one cycle - if ((MSC_LED_DEF == msc_led_value) && (MAIN_LED_FLASH == msc_led_state)) { + // If in flash mode stop after one cycle + if ((MSC_LED_DEF == msc_led_value) && (MAIN_LED_FLASH == msc_led_state)) { + msc_led_usb_activity = 0; + msc_led_state = MAIN_LED_DEF; + } + }else{ + //LED next state is MAIN_LED_DEF + msc_led_value = MSC_LED_DEF; msc_led_usb_activity = 0; - //for now the only place to turn off a blinking state - msc_led_state = MAIN_LED_OFF; } // Update hardware @@ -380,16 +390,21 @@ __task void main_task(void) } // CDC LED - if (cdc_led_usb_activity && ((cdc_led_state == MAIN_LED_FLASH) || (cdc_led_state == MAIN_LED_FLASH_PERMANENT))) { + if (cdc_led_usb_activity) { - // Toggle LED value - cdc_led_value = GPIO_LED_ON == cdc_led_value ? GPIO_LED_OFF : GPIO_LED_ON; + if ((cdc_led_state == MAIN_LED_FLASH) || (cdc_led_state == MAIN_LED_FLASH_PERMANENT)){ + // Toggle LED value + cdc_led_value = GPIO_LED_ON == cdc_led_value ? GPIO_LED_OFF : GPIO_LED_ON; - // If in flash mode stop after one cycle - if ((CDC_LED_DEF == cdc_led_value) && (MAIN_LED_FLASH == cdc_led_state)) { + // If in flash mode stop after one cycle + if ((CDC_LED_DEF == cdc_led_value) && (MAIN_LED_FLASH == cdc_led_state)) { + cdc_led_usb_activity = 0; + cdc_led_state = MAIN_LED_DEF; + } + }else{ + //LED next state is MAIN_LED_DEF + cdc_led_value = CDC_LED_DEF; cdc_led_usb_activity = 0; - //for now the only place to turn off a blinking state - cdc_led_state = MAIN_LED_OFF; } // Update hardware diff --git a/source/daplink/interface/main.h b/source/daplink/interface/main.h index 663ef6503..aac2f7db3 100644 --- a/source/daplink/interface/main.h +++ b/source/daplink/interface/main.h @@ -31,7 +31,7 @@ extern "C" { // LED state typedef enum main_led_state { - MAIN_LED_OFF = 0, + MAIN_LED_DEF = 0, MAIN_LED_FLASH, MAIN_LED_FLASH_PERMANENT } main_led_state_t; From 29a6db04a0459760ee8bb172a571e03976f396fb Mon Sep 17 00:00:00 2001 From: Brian Esquilona Date: Fri, 31 Aug 2018 13:55:32 -0500 Subject: [PATCH 2/2] HID LED patch to stop flashing on UART poll This happens in WebUSB polling the UART for reads and the returned length 0 meaning no data --- source/board/microbit.c | 12 +++++++++++- source/daplink/cmsis-dap/usbd_user_hid.c | 16 ++++++++++++++-- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/source/board/microbit.c b/source/board/microbit.c index 7268e67c3..3d394b86a 100644 --- a/source/board/microbit.c +++ b/source/board/microbit.c @@ -20,7 +20,8 @@ */ #include "fsl_device_registers.h" #include "IO_Config.h" - +#include "DAP.h" +#include "RTL.h" // URL_NAME and DRIVE_NAME must be 11 characters excluding // the null terminated character // Note - 4 byte alignemnt required as workaround for ARMCC compiler bug with weak references @@ -76,3 +77,12 @@ void prerun_board_config(void) { mb_version_t board_version = (mb_version_t)read_board_type_pin(); set_board_id(board_version); } + +// USB HID override function return 1 if the activity is trivial or response is null +uint8_t usbd_hid_no_activity(U8 *buf) +{ + if(buf[0] == ID_DAP_Vendor3 && buf[1] == 0) + return 1; + else + return 0; +} diff --git a/source/daplink/cmsis-dap/usbd_user_hid.c b/source/daplink/cmsis-dap/usbd_user_hid.c index 1066342d9..a18095936 100644 --- a/source/daplink/cmsis-dap/usbd_user_hid.c +++ b/source/daplink/cmsis-dap/usbd_user_hid.c @@ -103,9 +103,17 @@ int usbd_hid_get_report(U8 rtype, U8 rid, U8 *buf, U8 req) return (0); } +// USB HID override function return 1 if the activity is trivial or response is null +__attribute__((weak)) +uint8_t usbd_hid_no_activity(U8 *buf) +{ + return 0; +} + // USB HID Callback: when data is received from the host void usbd_hid_set_report(U8 rtype, U8 rid, U8 *buf, int len, U8 req) { + main_led_state_t led_next_state = MAIN_LED_FLASH; switch (rtype) { case HID_REPORT_OUTPUT: if (len == 0) { @@ -123,6 +131,10 @@ void usbd_hid_set_report(U8 rtype, U8 rid, U8 *buf, int len, U8 req) free_count--; memcpy(USB_Request[recv_idx], buf, len); DAP_ExecuteCommand(buf, USB_Request[recv_idx]); + if(usbd_hid_no_activity(USB_Request[recv_idx]) == 1){ + //revert HID LED to default if the response is null + led_next_state = MAIN_LED_DEF; + } recv_idx = (recv_idx + 1) % DAP_PACKET_COUNT; send_count++; if (USB_ResponseIdle) { @@ -132,8 +144,8 @@ void usbd_hid_set_report(U8 rtype, U8 rid, U8 *buf, int len, U8 req) } else { util_assert(0); } - - main_blink_hid_led(MAIN_LED_FLASH); + + main_blink_hid_led(led_next_state); break;