Skip to content

Commit ec64313

Browse files
committed
stm: add timer to storage cache so it can be flushed.
1 parent 318aec6 commit ec64313

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

stm/storage.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
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;
1718
static uint32_t cache_flash_sector_start;
1819
static uint32_t cache_flash_sector_size;
1920
static bool cache_dirty;
21+
static uint32_t sys_tick_counter_last_write;
2022

2123
static 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+
6472
void 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 {

stm/storage.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
void storage_init(void);
22
uint32_t storage_get_block_size(void);
33
uint32_t storage_get_block_count(void);
4+
bool storage_needs_flush(void);
45
void storage_flush(void);
56
bool storage_read_block(uint8_t *dest, uint32_t block);
67
bool storage_write_block(const uint8_t *src, uint32_t block);

0 commit comments

Comments
 (0)