Skip to content

Commit

Permalink
Merge pull request #528 from brianesquilona/dev_fixes
Browse files Browse the repository at this point in the history
Added checking to prevent target reset while flashing
  • Loading branch information
brianesquilona authored Nov 7, 2018
2 parents 128482c + 3223349 commit b571618
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 15 deletions.
2 changes: 2 additions & 0 deletions source/daplink/drag-n-drop/flash_intf.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand Down
4 changes: 4 additions & 0 deletions source/daplink/drag-n-drop/flash_manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
6 changes: 6 additions & 0 deletions source/daplink/drag-n-drop/iap_flash_intf.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}
3 changes: 2 additions & 1 deletion source/daplink/interface/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down
17 changes: 16 additions & 1 deletion source/daplink/interface/target_flash.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,20 @@
#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);
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,
Expand All @@ -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()
Expand All @@ -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;
}

Expand All @@ -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;
}
Expand Down Expand Up @@ -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);
}
27 changes: 14 additions & 13 deletions source/daplink/usb2uart/usbd_user_cdc_acm.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "main.h"
#include "target_reset.h"
#include "uart.h"
#include "flash_intf.h"

UART_Configuration UART_Config;

Expand Down Expand Up @@ -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);
}

Expand Down

0 comments on commit b571618

Please sign in to comment.