Skip to content

Commit

Permalink
Merge pull request #493 from brianesquilona/dev_features_fixes
Browse files Browse the repository at this point in the history
HID LED patch to stop flashing on UART poll
  • Loading branch information
brianesquilona committed Sep 4, 2018
2 parents 15445a0 + 29a6db0 commit ba041e9
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 42 deletions.
12 changes: 11 additions & 1 deletion source/board/microbit.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
33 changes: 23 additions & 10 deletions source/daplink/bootloader/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;

Expand All @@ -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
Expand Down Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion source/daplink/bootloader/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
16 changes: 14 additions & 2 deletions source/daplink/cmsis-dap/usbd_user_hid.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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;

Expand Down
69 changes: 42 additions & 27 deletions source/daplink/interface/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -346,50 +346,65 @@ __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
gpio_set_hid_led(hid_led_value);
}

// 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
gpio_set_msc_led(msc_led_value);
}

// 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
Expand Down
2 changes: 1 addition & 1 deletion source/daplink/interface/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit ba041e9

Please sign in to comment.