From 94a17d720ce0ebfed5ef2e166af789f22fbcc324 Mon Sep 17 00:00:00 2001 From: hathach Date: Sat, 11 Aug 2018 00:07:06 +0700 Subject: [PATCH] move flash_write and flush to seperate files for other usage --- Makefile | 1 + src/flash.c | 86 ++++++++++++++++++++++++++++++++++++++++++ src/flash.h | 58 ++++++++++++++++++++++++++++ src/usb/uf2/ghostfat.c | 44 +-------------------- 4 files changed, 147 insertions(+), 42 deletions(-) create mode 100644 src/flash.c create mode 100644 src/flash.h diff --git a/Makefile b/Makefile index ce46c88..2885dbc 100644 --- a/Makefile +++ b/Makefile @@ -120,6 +120,7 @@ endif # src C_SOURCE_FILES += $(SRC_PATH)/main.c +C_SOURCE_FILES += $(SRC_PATH)/flash.c C_SOURCE_FILES += $(SRC_PATH)/dfu_ble_svc.c C_SOURCE_FILES += $(SRC_PATH)/dfu_init.c diff --git a/src/flash.c b/src/flash.c new file mode 100644 index 0000000..426ef6c --- /dev/null +++ b/src/flash.c @@ -0,0 +1,86 @@ +/**************************************************************************/ +/*! + @file flash.c + @author hathach (tinyusb.org) + + @section LICENSE + + Software License Agreement (BSD License) + + Copyright (c) 2018, Adafruit Industries (adafruit.com) + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. Neither the name of the copyright holders nor the + names of its contributors may be used to endorse or promote products + derived from this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY + EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ +/**************************************************************************/ + +#include +#include "flash.h" +#include "boards.h" + +#include "nrf_sdm.h" + +#define FLASH_PAGE_SIZE 4096 + +#define NO_CACHE 0xffffffff + +static uint32_t _fl_addr = NO_CACHE; +static uint8_t _fl_buf[FLASH_PAGE_SIZE] __attribute__((aligned(4))); +static bool _first_flush = true; + + +void flash_flush(void) +{ + if ( _fl_addr == NO_CACHE ) return; + + if ( _first_flush ) + { + _first_flush = false; + + // disable softdevice + sd_softdevice_disable(); + + led_blink_fast(true); + } + + if ( memcmp(_fl_buf, (void *) _fl_addr, FLASH_PAGE_SIZE) != 0 ) + { + nrf_nvmc_page_erase(_fl_addr); + nrf_nvmc_write_words(_fl_addr, (uint32_t *) _fl_buf, FLASH_PAGE_SIZE / sizeof(uint32_t)); + } + + _fl_addr = NO_CACHE; +} + +void flash_write (uint32_t dst, const uint8_t *src, int len) +{ + uint32_t newAddr = dst & ~(FLASH_PAGE_SIZE - 1); + + if ( newAddr != _fl_addr ) + { + flash_flush(); + _fl_addr = newAddr; + memcpy(_fl_buf, (void *) newAddr, FLASH_PAGE_SIZE); + } + memcpy(_fl_buf + (dst & (FLASH_PAGE_SIZE - 1)), src, len); +} diff --git a/src/flash.h b/src/flash.h new file mode 100644 index 0000000..a42772b --- /dev/null +++ b/src/flash.h @@ -0,0 +1,58 @@ +/**************************************************************************/ +/*! + @file flash.h + @author hathach (tinyusb.org) + + @section LICENSE + + Software License Agreement (BSD License) + + Copyright (c) 2018, Adafruit Industries (adafruit.com) + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. Neither the name of the copyright holders nor the + names of its contributors may be used to endorse or promote products + derived from this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY + EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ +/**************************************************************************/ + +#ifndef FLASH_H_ +#define FLASH_H_ + +#include +#include + +#include "nrf_nvmc.h" + +#ifdef __cplusplus + extern "C" { +#endif + +//void flash_erase(uint32_t page_addr); +void flash_write(uint32_t dst, const uint8_t *src, int len); +void flash_flush(void); + + +#ifdef __cplusplus + } +#endif + +#endif /* FLASH_H_ */ diff --git a/src/usb/uf2/ghostfat.c b/src/usb/uf2/ghostfat.c index ce8c297..60e10eb 100644 --- a/src/usb/uf2/ghostfat.c +++ b/src/usb/uf2/ghostfat.c @@ -1,6 +1,6 @@ #include "uf2.h" -#include "nrf_nvmc.h" +#include "flash.h" #include #include "boards.h" @@ -115,15 +115,9 @@ static const FAT_BootBlock BootBlock = { .FilesystemIdentifier = "FAT16 ", }; -#define NO_CACHE 0xffffffff - #define NRF_LOG_DEBUG(...) #define NRF_LOG_WARNING(...) -uint32_t flashAddr = NO_CACHE; -uint8_t flashBuf[FLASH_PAGE_SIZE] __attribute__((aligned(4))); -bool firstFlush = true; - static WriteState _wr_state = { 0 }; static uint32_t get_flash_size(void) { @@ -246,40 +240,6 @@ static void uf2_write_complete(uint32_t numBlocks) bootloader_dfu_update_process(update_status); } -void flushFlash() { - if (flashAddr == NO_CACHE) - return; - - if (firstFlush) { - firstFlush = false; - - // disable softdevice - sd_softdevice_disable(); - - led_blink_fast(true); - } - - NRF_LOG_DEBUG("Flush at %x", flashAddr); - if (memcmp(flashBuf, (void *)flashAddr, FLASH_PAGE_SIZE) != 0) { - NRF_LOG_DEBUG("Write flush at %x", flashAddr); - nrf_nvmc_page_erase(flashAddr); - nrf_nvmc_write_words(flashAddr, (uint32_t *)flashBuf, FLASH_PAGE_SIZE / sizeof(uint32_t)); - } - - flashAddr = NO_CACHE; -} - -void flash_write(uint32_t dst, const uint8_t *src, int len) { - uint32_t newAddr = dst & ~(FLASH_PAGE_SIZE - 1); - - if (newAddr != flashAddr) { - flushFlash(); - flashAddr = newAddr; - memcpy(flashBuf, (void *)newAddr, FLASH_PAGE_SIZE); - } - memcpy(flashBuf + (dst & (FLASH_PAGE_SIZE - 1)), src, len); -} - /** Write an block * * @return number of bytes processed, only 3 following values @@ -340,7 +300,7 @@ int write_block(uint32_t block_no, uint8_t *data, bool quiet/*, WriteState *stat } // flush last blocks if needed - flushFlash(); + flash_flush(); uf2_write_complete(state->numBlocks); }