File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 22#include "std.h"
33
44#include "misc.h"
5+ #include "systick.h"
56#include "led.h"
67#include "flash.h"
78#include "storage.h"
@@ -17,6 +18,7 @@ static uint32_t cache_flash_sector_id;
1718static uint32_t cache_flash_sector_start ;
1819static uint32_t cache_flash_sector_size ;
1920static bool cache_dirty ;
21+ static uint32_t sys_tick_counter_last_write ;
2022
2123static void cache_flush (void ) {
2224 if (cache_dirty ) {
@@ -50,6 +52,7 @@ void storage_init(void) {
5052 cache_flash_sector_id = 0 ;
5153 cache_dirty = false;
5254 is_initialised = true;
55+ sys_tick_counter_last_write = 0 ;
5356 }
5457}
5558
@@ -61,6 +64,11 @@ uint32_t storage_get_block_count(void) {
6164 return FLASH_PART1_START_BLOCK + FLASH_PART1_NUM_BLOCKS ;
6265}
6366
67+ bool storage_needs_flush (void ) {
68+ // wait 2 seconds after last write to flush
69+ return cache_dirty && sys_tick_has_passed (sys_tick_counter_last_write , 2000 );
70+ }
71+
6472void storage_flush (void ) {
6573 cache_flush ();
6674}
@@ -143,6 +151,7 @@ bool storage_write_block(const uint8_t *src, uint32_t block) {
143151 uint32_t flash_addr = FLASH_MEM_START_ADDR + (block - FLASH_PART1_START_BLOCK ) * BLOCK_SIZE ;
144152 uint8_t * dest = cache_get_addr_for_write (flash_addr );
145153 memcpy (dest , src , BLOCK_SIZE );
154+ sys_tick_counter_last_write = sys_tick_counter ;
146155 return true;
147156
148157 } else {
Original file line number Diff line number Diff line change 11void storage_init (void );
22uint32_t storage_get_block_size (void );
33uint32_t storage_get_block_count (void );
4+ bool storage_needs_flush (void );
45void storage_flush (void );
56bool storage_read_block (uint8_t * dest , uint32_t block );
67bool storage_write_block (const uint8_t * src , uint32_t block );
You can’t perform that action at this time.
0 commit comments