diff --git a/source/daplink/drag-n-drop/flash_intf.h b/source/daplink/drag-n-drop/flash_intf.h index bdb16307b..3c92f5fc6 100644 --- a/source/daplink/drag-n-drop/flash_intf.h +++ b/source/daplink/drag-n-drop/flash_intf.h @@ -37,6 +37,7 @@ typedef error_t (*flash_intf_erase_sector_cb_t)(uint32_t sector); typedef error_t (*flash_intf_erase_chip_cb_t)(void); typedef uint32_t (*flash_program_page_min_size_cb_t)(uint32_t addr); typedef uint32_t (*flash_erase_sector_size_cb_t)(uint32_t addr); +typedef uint8_t (*flash_busy_cb_t)(void); typedef struct { flash_intf_init_cb_t init; @@ -46,6 +47,7 @@ typedef struct { flash_intf_erase_chip_cb_t erase_chip; flash_program_page_min_size_cb_t program_page_min_size; flash_erase_sector_size_cb_t erase_sector_size; + flash_busy_cb_t flash_busy; } flash_intf_t; // All flash interfaces. Unsupported interfaces are NULL. diff --git a/source/daplink/drag-n-drop/flash_manager.c b/source/daplink/drag-n-drop/flash_manager.c index 31a4e0e4a..a453bd73d 100755 --- a/source/daplink/drag-n-drop/flash_manager.c +++ b/source/daplink/drag-n-drop/flash_manager.c @@ -276,6 +276,10 @@ static bool flash_intf_valid(const flash_intf_t *flash_intf) if (0 == flash_intf->erase_sector_size) { return false; } + + if (0 == flash_intf->flash_busy) { + return false; + } return true; } diff --git a/source/daplink/drag-n-drop/iap_flash_intf.c b/source/daplink/drag-n-drop/iap_flash_intf.c index ea56644e4..22612ce29 100644 --- a/source/daplink/drag-n-drop/iap_flash_intf.c +++ b/source/daplink/drag-n-drop/iap_flash_intf.c @@ -63,6 +63,7 @@ static bool sector_erase_allowed(uint32_t addr); static error_t intercept_page_write(uint32_t addr, const uint8_t *buf, uint32_t size); static error_t intercept_sector_erase(uint32_t addr); static error_t critical_erase_and_program(uint32_t addr, const uint8_t *data, uint32_t size); +static uint8_t target_flash_busy(void); static const flash_intf_t flash_intf = { init, @@ -72,6 +73,7 @@ static const flash_intf_t flash_intf = { erase_chip, program_page_min_size, erase_sector_size, + target_flash_busy, }; const flash_intf_t *const flash_intf_iap_protected = &flash_intf; @@ -497,3 +499,7 @@ static error_t critical_erase_and_program(uint32_t addr, const uint8_t *data, ui return ERROR_SUCCESS; } + +static uint8_t target_flash_busy(void){ + return (state == STATE_OPEN); +} diff --git a/source/daplink/interface/main.c b/source/daplink/interface/main.c index 569897039..94cf8f385 100644 --- a/source/daplink/interface/main.c +++ b/source/daplink/interface/main.c @@ -41,6 +41,7 @@ #include "bootloader.h" #include "cortex_m.h" #include "sdk.h" +#include "flash_intf.h" // Event flags for main task // Timers events @@ -328,7 +329,7 @@ __task void main_task(void) if (flags & FLAGS_MAIN_30MS) { // handle reset button without eventing - if (!reset_pressed && gpio_get_reset_btn_fwrd()) { + if (!reset_pressed && gpio_get_reset_btn_fwrd() && !flash_intf_target->flash_busy()) { //added checking if flashing on target is in progress // Reset button pressed target_set_state(RESET_HOLD); reset_pressed = 1; diff --git a/source/daplink/interface/target_flash.c b/source/daplink/interface/target_flash.c index 71b4a93fc..28d878642 100644 --- a/source/daplink/interface/target_flash.c +++ b/source/daplink/interface/target_flash.c @@ -32,6 +32,12 @@ #include "util.h" #include "settings.h" +typedef enum { + STATE_CLOSED, + STATE_OPEN, + STATE_ERROR +} state_t; + static error_t target_flash_init(void); static error_t target_flash_uninit(void); static error_t target_flash_program_page(uint32_t adr, const uint8_t *buf, uint32_t size); @@ -39,6 +45,7 @@ static error_t target_flash_erase_sector(uint32_t addr); static error_t target_flash_erase_chip(void); static uint32_t target_flash_program_page_min_size(uint32_t addr); static uint32_t target_flash_erase_sector_size(uint32_t addr); +static uint8_t target_flash_busy(void); static const flash_intf_t flash_intf = { target_flash_init, @@ -48,8 +55,11 @@ static const flash_intf_t flash_intf = { target_flash_erase_chip, target_flash_program_page_min_size, target_flash_erase_sector_size, + target_flash_busy, }; +static state_t state = STATE_CLOSED; + const flash_intf_t *const flash_intf_target = &flash_intf; static error_t target_flash_init() @@ -68,7 +78,7 @@ static error_t target_flash_init() if (0 == swd_flash_syscall_exec(&flash->sys_call_s, flash->init, target_device.flash_start, 0, 0, 0)) { return ERROR_INIT; } - + state = STATE_OPEN; return ERROR_SUCCESS; } @@ -84,6 +94,7 @@ static error_t target_flash_uninit(void) // Check to see if anything needs to be done after programming. // This is usually a no-op for most targets. target_set_state(POST_FLASH_RESET); + state = STATE_CLOSED; swd_off(); return ERROR_SUCCESS; } @@ -207,3 +218,7 @@ static uint32_t target_flash_erase_sector_size(uint32_t addr) } return target_device.sector_size; } + +static uint8_t target_flash_busy(void){ + return (state == STATE_OPEN); +} diff --git a/source/daplink/usb2uart/usbd_user_cdc_acm.c b/source/daplink/usb2uart/usbd_user_cdc_acm.c index fd6f5443f..b5580b02a 100644 --- a/source/daplink/usb2uart/usbd_user_cdc_acm.c +++ b/source/daplink/usb2uart/usbd_user_cdc_acm.c @@ -24,6 +24,7 @@ #include "main.h" #include "target_reset.h" #include "uart.h" +#include "flash_intf.h" UART_Configuration UART_Config; @@ -111,22 +112,22 @@ static U32 start_break_time = 0; int32_t USBD_CDC_ACM_SendBreak(uint16_t dur) { uint32_t end_break_time; - - // reset and send the unique id over CDC - if (dur != 0) { - start_break_time = os_time_get(); - target_set_state(RESET_HOLD); - } else { - end_break_time = os_time_get(); - - // long reset -> send uID over serial (300 -> break > 3s) - if ((end_break_time - start_break_time) >= (300)) { - main_reset_target(1); + if (!flash_intf_target->flash_busy()) { //added checking if flashing on target is in progress + // reset and send the unique id over CDC + if (dur != 0) { + start_break_time = os_time_get(); + target_set_state(RESET_HOLD); } else { - main_reset_target(0); + end_break_time = os_time_get(); + + // long reset -> send uID over serial (300 -> break > 3s) + if ((end_break_time - start_break_time) >= (300)) { + main_reset_target(1); + } else { + main_reset_target(0); + } } } - return (1); }