From 25671db338d1e44b7d8990b16f8c4399b5493b3a Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 8 Aug 2018 18:01:09 +0700 Subject: [PATCH] solve #9, disable softdevice when upgrading with uf2 file --- src/main.c | 14 +++++--- src/usb/msc_uf2.c | 2 -- src/usb/uf2/ghostfat.c | 77 ++++++++---------------------------------- 3 files changed, 23 insertions(+), 70 deletions(-) diff --git a/src/main.c b/src/main.c index a97cb1b..8eb4b25 100644 --- a/src/main.c +++ b/src/main.c @@ -196,7 +196,7 @@ void board_init(void) // Init scheduler APP_SCHED_INIT(SCHED_MAX_EVENT_DATA_SIZE, SCHED_QUEUE_SIZE); - // Init timer (RTC1) + // Init app timer (use RTC1) app_timer_init(); // Configure Systick for led blinky @@ -230,7 +230,7 @@ void board_teardown(void) * be initialized if a chip reset has occured. Soft reset (jump ) from * application must not reinitialize the SoftDevice. */ -static uint32_t softdev_init(bool init_softdevice) +uint32_t softdev_init(bool init_softdevice) { if (init_softdevice) { @@ -292,18 +292,19 @@ static uint32_t softdev_init(bool init_softdevice) APP_ERROR_CHECK( sd_ble_cfg_set(BLE_CONN_CFG_GAP, &blecfg, ram_start) ); // Enable BLE stack. + // Note: Interrupt state (enabled, forwarding) is not work properly if not enable ble APP_ERROR_CHECK( sd_ble_enable(&ram_start) ); return NRF_SUCCESS; } -uint32_t softdev_teardown(void) +void softdev_teardown(void) { - APP_ERROR_CHECK ( sd_softdevice_disable() ); - return NRF_SUCCESS; + sd_softdevice_disable(); } + int main(void) { // SD is already Initialized in case of BOOTLOADER_DFU_OTA_MAGIC @@ -350,6 +351,9 @@ int main(void) if ( dfu_start || !bootloader_app_is_valid(DFU_BANK_0_REGION_START) ) { + // Enable BLE if in OTA +// if ( _ota_update ) softdev_ble_enable(); + // Initiate an update of the firmware. APP_ERROR_CHECK( bootloader_dfu_start(_ota_update, 0) ); } diff --git a/src/usb/msc_uf2.c b/src/usb/msc_uf2.c index c8a8f20..74228b3 100644 --- a/src/usb/msc_uf2.c +++ b/src/usb/msc_uf2.c @@ -38,8 +38,6 @@ #if CFG_TUD_MSC -#include "pstorage.h" - /*------------------------------------------------------------------*/ /* MACRO TYPEDEF CONSTANT ENUM *------------------------------------------------------------------*/ diff --git a/src/usb/uf2/ghostfat.c b/src/usb/uf2/ghostfat.c index 4081c16..de49f38 100644 --- a/src/usb/uf2/ghostfat.c +++ b/src/usb/uf2/ghostfat.c @@ -8,8 +8,6 @@ #include "bootloader_settings.h" #include "bootloader.h" -#include "pstorage.h" - typedef struct { uint8_t JumpInstruction[3]; @@ -125,13 +123,8 @@ static const FAT_BootBlock BootBlock = { uint32_t flashAddr = NO_CACHE; uint8_t flashBuf[FLASH_PAGE_SIZE] __attribute__((aligned(4))); bool firstFlush = true; -//bool hadWrite = false; static WriteState _wr_state = { 0 }; -static WriteState* state = &_wr_state; -volatile bool _is_flashing = false; -static pstorage_handle_t _fat_psh = { .module_id = 0, .block_id = USER_FLASH_END } ; - static uint32_t get_flash_size(void) { static uint32_t flash_sz = 0; @@ -234,8 +227,8 @@ void read_block(uint32_t block_no, uint8_t *data) { /* Write UF2 *------------------------------------------------------------------*/ -/** inform bootloader to update setting and reset */ -static void uf2_write_complete(void) +/** uf2 upgrade complete -> inform bootloader to update setting and reset */ +static void uf2_write_complete(uint32_t numBlocks) { led_blink_fast(false); @@ -244,37 +237,15 @@ static void uf2_write_complete(void) memset(&update_status, 0, sizeof(dfu_update_status_t )); update_status.status_code = DFU_UPDATE_APP_COMPLETE; update_status.app_crc = 0; // skip CRC checking with uf2 upgrade - update_status.app_size = state->numBlocks*256; + update_status.app_size = numBlocks*256; + + // re-enable softdevice + extern uint32_t softdev_init(bool init_softdevice); + softdev_init(false); bootloader_dfu_update_process(update_status); } -static void fat_pstorage_cb(pstorage_handle_t * p_handle, uint8_t op_code, uint32_t result, uint8_t * p_data, uint32_t data_len) -{ - if ( result != NRF_SUCCESS ) - { - TU_ASSERT(false, ); - } - - if ( PSTORAGE_CLEAR_OP_CODE == op_code) - { - // erase complete start writing - _fat_psh.block_id = p_handle->block_id; - TU_ASSERT( pstorage_store(&_fat_psh, flashBuf, FLASH_PAGE_SIZE, 0), ); - } - else if ( PSTORAGE_STORE_OP_CODE == op_code) - { - // write completes - _is_flashing = false; - - // whole uf2 file is written, complete the write op - if ( state->numWritten >= state->numBlocks ) - { - uf2_write_complete(); - } - } -} - void flushFlash() { if (flashAddr == NO_CACHE) return; @@ -282,9 +253,8 @@ void flushFlash() { if (firstFlush) { firstFlush = false; - // Init pstorage - pstorage_module_param_t fat_psp = { .cb = fat_pstorage_cb}; - pstorage_register(&fat_psp, &_fat_psh); + // disable softdevice + sd_softdevice_disable(); led_blink_fast(true); } @@ -292,11 +262,8 @@ void flushFlash() { NRF_LOG_DEBUG("Flush at %x", flashAddr); if (memcmp(flashBuf, (void *)flashAddr, FLASH_PAGE_SIZE) != 0) { NRF_LOG_DEBUG("Write flush at %x", flashAddr); - _is_flashing = true; - - // Writing to flash will be done in erase complete callback - _fat_psh.block_id = flashAddr; - TU_ASSERT ( pstorage_clear(&_fat_psh, FLASH_PAGE_SIZE), ); + nrf_nvmc_page_erase(flashAddr); + nrf_nvmc_write_words(flashAddr, (uint32_t *)flashBuf, FLASH_PAGE_SIZE / sizeof(uint32_t)); } flashAddr = NO_CACHE; @@ -305,15 +272,8 @@ void flushFlash() { void flash_write(uint32_t dst, const uint8_t *src, int len) { uint32_t newAddr = dst & ~(FLASH_PAGE_SIZE - 1); - // hadWrite = true; - if (newAddr != flashAddr) { flushFlash(); - - // writing previous cached data, skip current data until flashing is done - // tinyusb stack will invoke write_block() with the same parameters later on - if ( _is_flashing ) return; - flashAddr = newAddr; memcpy(flashBuf, (void *)newAddr, FLASH_PAGE_SIZE); } @@ -329,6 +289,7 @@ void flash_write(uint32_t dst, const uint8_t *src, int len) { */ int write_block(uint32_t block_no, uint8_t *data, bool quiet/*, WriteState *state*/) { UF2_Block *bl = (void *)data; + WriteState* state = &_wr_state; NRF_LOG_DEBUG("Write magic: %x", bl->magicStart0); @@ -336,9 +297,6 @@ int write_block(uint32_t block_no, uint8_t *data, bool quiet/*, WriteState *stat return -1; } - // still busy with flashing previous blocks - if ( _is_flashing ) return 0; - if ((bl->flags & UF2_FLAG_NOFLASH) || bl->payloadSize > 256 || (bl->targetAddr & 0xff) || bl->targetAddr < USER_FLASH_START || bl->targetAddr + bl->payloadSize > USER_FLASH_END) { #if USE_DBG_MSC @@ -354,9 +312,6 @@ int write_block(uint32_t block_no, uint8_t *data, bool quiet/*, WriteState *stat flash_write(bl->targetAddr, bl->data, bl->payloadSize); } - // flash_write cause a flush to write previous cached data, this write data is not consumed yet - if ( _is_flashing ) return 0; - if (state && bl->numBlocks) { if (state->numBlocks != bl->numBlocks) { if (bl->numBlocks >= MAX_BLOCKS || state->numBlocks) @@ -379,14 +334,10 @@ int write_block(uint32_t block_no, uint8_t *data, bool quiet/*, WriteState *stat } - // flush last blocks + // flush last blocks if needed flushFlash(); - // no flashing due to last blocks is the same to contents on the flash already - // complete the write - if (!_is_flashing) { - uf2_write_complete(); - } + uf2_write_complete(state->numBlocks); } } NRF_LOG_DEBUG("wr %d=%d (of %d)", state->numWritten, bl->blockNo, bl->numBlocks);