move flash_write and flush to seperate files for other usage
This commit is contained in:
		
							
								
								
									
										1
									
								
								Makefile
									
									
									
									
									
								
							
							
						
						
									
										1
									
								
								Makefile
									
									
									
									
									
								
							| @@ -120,6 +120,7 @@ endif | |||||||
|  |  | ||||||
| # src | # src | ||||||
| C_SOURCE_FILES += $(SRC_PATH)/main.c | 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_ble_svc.c | ||||||
| C_SOURCE_FILES += $(SRC_PATH)/dfu_init.c | C_SOURCE_FILES += $(SRC_PATH)/dfu_init.c | ||||||
|  |  | ||||||
|   | |||||||
							
								
								
									
										86
									
								
								src/flash.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										86
									
								
								src/flash.c
									
									
									
									
									
										Normal file
									
								
							| @@ -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 <string.h> | ||||||
|  | #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); | ||||||
|  | } | ||||||
							
								
								
									
										58
									
								
								src/flash.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										58
									
								
								src/flash.h
									
									
									
									
									
										Normal file
									
								
							| @@ -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 <stdint.h> | ||||||
|  | #include <stdbool.h> | ||||||
|  |  | ||||||
|  | #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_ */ | ||||||
| @@ -1,6 +1,6 @@ | |||||||
|  |  | ||||||
| #include "uf2.h" | #include "uf2.h" | ||||||
| #include "nrf_nvmc.h" | #include "flash.h" | ||||||
| #include <string.h> | #include <string.h> | ||||||
|  |  | ||||||
| #include "boards.h" | #include "boards.h" | ||||||
| @@ -115,15 +115,9 @@ static const FAT_BootBlock BootBlock = { | |||||||
|     .FilesystemIdentifier = "FAT16   ", |     .FilesystemIdentifier = "FAT16   ", | ||||||
| }; | }; | ||||||
|  |  | ||||||
| #define NO_CACHE 0xffffffff |  | ||||||
|  |  | ||||||
| #define NRF_LOG_DEBUG(...) | #define NRF_LOG_DEBUG(...) | ||||||
| #define NRF_LOG_WARNING(...) | #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 WriteState _wr_state = { 0 }; | ||||||
| static uint32_t get_flash_size(void) | 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); |   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 | /** Write an block | ||||||
|  * |  * | ||||||
|  * @return number of bytes processed, only 3 following values |  * @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 |                 // flush last blocks if needed | ||||||
|                 flushFlash(); |                 flash_flush(); | ||||||
|  |  | ||||||
|                 uf2_write_complete(state->numBlocks); |                 uf2_write_complete(state->numBlocks); | ||||||
|             } |             } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user