Skip to content

Commit 9e5ea4d

Browse files
committed
stmhal: Add flash write support and flash storage driver.
1 parent 8a9a31e commit 9e5ea4d

File tree

6 files changed

+282
-5
lines changed

6 files changed

+282
-5
lines changed

stmhal/Makefile

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,12 @@ SRC_C = \
7878
exti.c \
7979
usrsw.c \
8080
rtc.c \
81+
flash.c \
82+
storage.c \
8183
sdcard.c \
8284

8385
# lcd.c \
8486
# servo.c \
85-
# flash.c \
86-
# storage.c \
8787
# accel.c \
8888
# timer.c \
8989
# audio.c \
@@ -100,6 +100,8 @@ SRC_HAL = $(addprefix $(HAL_DIR)/src/,\
100100
stm32f4xx_hal.c \
101101
stm32f4xx_hal_cortex.c \
102102
stm32f4xx_hal_dma.c \
103+
stm32f4xx_hal_flash.c \
104+
stm32f4xx_hal_flash_ex.c \
103105
stm32f4xx_hal_gpio.c \
104106
stm32f4xx_hal_pcd.c \
105107
stm32f4xx_hal_rcc.c \

stmhal/flash.c

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#include <stm32f4xx_hal.h>
2+
3+
#include "flash.h"
4+
5+
/* Base address of the Flash sectors */
6+
#define ADDR_FLASH_SECTOR_0 ((uint32_t)0x08000000) /* Base @ of Sector 0, 16 Kbytes */
7+
#define ADDR_FLASH_SECTOR_1 ((uint32_t)0x08004000) /* Base @ of Sector 1, 16 Kbytes */
8+
#define ADDR_FLASH_SECTOR_2 ((uint32_t)0x08008000) /* Base @ of Sector 2, 16 Kbytes */
9+
#define ADDR_FLASH_SECTOR_3 ((uint32_t)0x0800C000) /* Base @ of Sector 3, 16 Kbytes */
10+
#define ADDR_FLASH_SECTOR_4 ((uint32_t)0x08010000) /* Base @ of Sector 4, 64 Kbytes */
11+
#define ADDR_FLASH_SECTOR_5 ((uint32_t)0x08020000) /* Base @ of Sector 5, 128 Kbytes */
12+
#define ADDR_FLASH_SECTOR_6 ((uint32_t)0x08040000) /* Base @ of Sector 6, 128 Kbytes */
13+
#define ADDR_FLASH_SECTOR_7 ((uint32_t)0x08060000) /* Base @ of Sector 7, 128 Kbytes */
14+
#define ADDR_FLASH_SECTOR_8 ((uint32_t)0x08080000) /* Base @ of Sector 8, 128 Kbytes */
15+
#define ADDR_FLASH_SECTOR_9 ((uint32_t)0x080A0000) /* Base @ of Sector 9, 128 Kbytes */
16+
#define ADDR_FLASH_SECTOR_10 ((uint32_t)0x080C0000) /* Base @ of Sector 10, 128 Kbytes */
17+
#define ADDR_FLASH_SECTOR_11 ((uint32_t)0x080E0000) /* Base @ of Sector 11, 128 Kbytes */
18+
19+
static const uint32_t flash_info_table[26] = {
20+
ADDR_FLASH_SECTOR_0, FLASH_SECTOR_0,
21+
ADDR_FLASH_SECTOR_1, FLASH_SECTOR_1,
22+
ADDR_FLASH_SECTOR_2, FLASH_SECTOR_2,
23+
ADDR_FLASH_SECTOR_3, FLASH_SECTOR_3,
24+
ADDR_FLASH_SECTOR_4, FLASH_SECTOR_4,
25+
ADDR_FLASH_SECTOR_5, FLASH_SECTOR_5,
26+
ADDR_FLASH_SECTOR_6, FLASH_SECTOR_6,
27+
ADDR_FLASH_SECTOR_7, FLASH_SECTOR_7,
28+
ADDR_FLASH_SECTOR_8, FLASH_SECTOR_8,
29+
ADDR_FLASH_SECTOR_9, FLASH_SECTOR_9,
30+
ADDR_FLASH_SECTOR_10, FLASH_SECTOR_10,
31+
ADDR_FLASH_SECTOR_11, FLASH_SECTOR_11,
32+
ADDR_FLASH_SECTOR_11 + 0x20000, 0,
33+
};
34+
35+
uint32_t flash_get_sector_info(uint32_t addr, uint32_t *start_addr, uint32_t *size) {
36+
if (addr >= flash_info_table[0]) {
37+
for (int i = 0; i < 24; i += 2) {
38+
if (addr < flash_info_table[i + 2]) {
39+
if (start_addr != NULL) {
40+
*start_addr = flash_info_table[i];
41+
}
42+
if (size != NULL) {
43+
*size = flash_info_table[i + 2] - flash_info_table[i];
44+
}
45+
return flash_info_table[i + 1];
46+
}
47+
}
48+
}
49+
return 0;
50+
}
51+
52+
void flash_write(uint32_t flash_dest, const uint32_t *src, uint32_t num_word32) {
53+
// check there is something to write
54+
if (num_word32 == 0) {
55+
return;
56+
}
57+
58+
// unlock
59+
HAL_FLASH_Unlock();
60+
61+
// Clear pending flags (if any)
62+
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR |
63+
FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR|FLASH_FLAG_PGSERR);
64+
65+
// erase the sector(s)
66+
FLASH_EraseInitTypeDef EraseInitStruct;
67+
EraseInitStruct.TypeErase = TYPEERASE_SECTORS;
68+
EraseInitStruct.VoltageRange = VOLTAGE_RANGE_3; // voltage range needs to be 2.7V to 3.6V
69+
EraseInitStruct.Sector = flash_get_sector_info(flash_dest, NULL, NULL);
70+
EraseInitStruct.NbSectors = flash_get_sector_info(flash_dest + 4 * num_word32 - 1, NULL, NULL) - EraseInitStruct.Sector + 1;
71+
uint32_t SectorError = 0;
72+
if (HAL_FLASHEx_Erase(&EraseInitStruct, &SectorError) != HAL_OK) {
73+
// error occurred during sector erase
74+
return;
75+
}
76+
77+
// program the flash word by word
78+
for (int i = 0; i < num_word32; i++) {
79+
if (HAL_FLASH_Program(TYPEPROGRAM_WORD, flash_dest, *src) != HAL_OK) {
80+
// error occurred during flash write
81+
return;
82+
}
83+
flash_dest += 4;
84+
src += 1;
85+
}
86+
87+
// lock the flash
88+
HAL_FLASH_Lock();
89+
}

stmhal/flash.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
uint32_t flash_get_sector_info(uint32_t addr, uint32_t *start_addr, uint32_t *size);
2+
void flash_write(uint32_t flash_dest, const uint32_t *src, uint32_t num_word32);

stmhal/main.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@
2929
#include "usrsw.h"
3030
#include "usb.h"
3131
#include "rtc.h"
32+
#include "storage.h"
3233
#include "sdcard.h"
3334
#if 0
3435
#include "ff.h"
3536
#include "lexerfatfs.h"
3637
#include "servo.h"
3738
#include "lcd.h"
38-
#include "storage.h"
3939
#include "accel.h"
4040
#include "timer.h"
4141
#include "pybwlan.h"
@@ -232,9 +232,7 @@ int main(void) {
232232
#if MICROPY_HW_HAS_SDCARD
233233
sdcard_init();
234234
#endif
235-
#if 0
236235
storage_init();
237-
#endif
238236

239237
int first_soft_reset = true;
240238

stmhal/storage.c

Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
#include <stdint.h>
2+
#include <string.h>
3+
#include <stm32f4xx_hal.h>
4+
5+
#include "misc.h"
6+
#include "systick.h"
7+
#include "mpconfig.h"
8+
#include "qstr.h"
9+
#include "obj.h"
10+
#include "led.h"
11+
#include "flash.h"
12+
#include "storage.h"
13+
14+
#define CACHE_MEM_START_ADDR (0x10000000) // CCM data RAM, 64k
15+
#define FLASH_PART1_START_BLOCK (0x100)
16+
#define FLASH_PART1_NUM_BLOCKS (224) // 16k+16k+16k+64k=112k
17+
#define FLASH_MEM_START_ADDR (0x08004000) // sector 1, 16k
18+
19+
static bool flash_is_initialised = false;
20+
static bool flash_cache_dirty;
21+
static uint32_t flash_cache_sector_id;
22+
static uint32_t flash_cache_sector_start;
23+
static uint32_t flash_cache_sector_size;
24+
static uint32_t flash_tick_counter_last_write;
25+
26+
static void flash_cache_flush(void) {
27+
if (flash_cache_dirty) {
28+
// sync the cache RAM buffer by writing it to the flash page
29+
flash_write(flash_cache_sector_start, (const uint32_t*)CACHE_MEM_START_ADDR, flash_cache_sector_size / 4);
30+
flash_cache_dirty = false;
31+
// indicate a clean cache with LED off
32+
led_state(PYB_LED_R1, 0);
33+
}
34+
}
35+
36+
static uint8_t *flash_cache_get_addr_for_write(uint32_t flash_addr) {
37+
uint32_t flash_sector_start;
38+
uint32_t flash_sector_size;
39+
uint32_t flash_sector_id = flash_get_sector_info(flash_addr, &flash_sector_start, &flash_sector_size);
40+
if (flash_cache_sector_id != flash_sector_id) {
41+
flash_cache_flush();
42+
memcpy((void*)CACHE_MEM_START_ADDR, (const void*)flash_sector_start, flash_sector_size);
43+
flash_cache_sector_id = flash_sector_id;
44+
flash_cache_sector_start = flash_sector_start;
45+
flash_cache_sector_size = flash_sector_size;
46+
}
47+
flash_cache_dirty = true;
48+
// indicate a dirty cache with LED on
49+
led_state(PYB_LED_R1, 1);
50+
return (uint8_t*)CACHE_MEM_START_ADDR + flash_addr - flash_sector_start;
51+
}
52+
53+
static uint8_t *flash_cache_get_addr_for_read(uint32_t flash_addr) {
54+
uint32_t flash_sector_start;
55+
uint32_t flash_sector_size;
56+
uint32_t flash_sector_id = flash_get_sector_info(flash_addr, &flash_sector_start, &flash_sector_size);
57+
if (flash_cache_sector_id == flash_sector_id) {
58+
// in cache, copy from there
59+
return (uint8_t*)CACHE_MEM_START_ADDR + flash_addr - flash_sector_start;
60+
}
61+
// not in cache, copy straight from flash
62+
return (uint8_t*)flash_addr;
63+
}
64+
65+
void storage_init(void) {
66+
if (!flash_is_initialised) {
67+
flash_cache_dirty = false;
68+
flash_cache_sector_id = 0;
69+
flash_is_initialised = true;
70+
flash_tick_counter_last_write = 0;
71+
}
72+
}
73+
74+
uint32_t storage_get_block_size(void) {
75+
return FLASH_BLOCK_SIZE;
76+
}
77+
78+
uint32_t storage_get_block_count(void) {
79+
return FLASH_PART1_START_BLOCK + FLASH_PART1_NUM_BLOCKS;
80+
}
81+
82+
bool storage_needs_flush(void) {
83+
// wait 2 seconds after last write to flush
84+
return flash_cache_dirty && sys_tick_has_passed(flash_tick_counter_last_write, 2000);
85+
}
86+
87+
void storage_flush(void) {
88+
flash_cache_flush();
89+
}
90+
91+
static void build_partition(uint8_t *buf, int boot, int type, uint32_t start_block, uint32_t num_blocks) {
92+
buf[0] = boot;
93+
94+
if (num_blocks == 0) {
95+
buf[1] = 0;
96+
buf[2] = 0;
97+
buf[3] = 0;
98+
} else {
99+
buf[1] = 0xff;
100+
buf[2] = 0xff;
101+
buf[3] = 0xff;
102+
}
103+
104+
buf[4] = type;
105+
106+
if (num_blocks == 0) {
107+
buf[5] = 0;
108+
buf[6] = 0;
109+
buf[7] = 0;
110+
} else {
111+
buf[5] = 0xff;
112+
buf[6] = 0xff;
113+
buf[7] = 0xff;
114+
}
115+
116+
buf[8] = start_block;
117+
buf[9] = start_block >> 8;
118+
buf[10] = start_block >> 16;
119+
buf[11] = start_block >> 24;
120+
121+
buf[12] = num_blocks;
122+
buf[13] = num_blocks >> 8;
123+
buf[14] = num_blocks >> 16;
124+
buf[15] = num_blocks >> 24;
125+
}
126+
127+
bool storage_read_block(uint8_t *dest, uint32_t block) {
128+
//printf("RD %u\n", block);
129+
if (block == 0) {
130+
// fake the MBR so we can decide on our own partition table
131+
132+
for (int i = 0; i < 446; i++) {
133+
dest[i] = 0;
134+
}
135+
136+
build_partition(dest + 446, 0, 0x01 /* FAT12 */, FLASH_PART1_START_BLOCK, FLASH_PART1_NUM_BLOCKS);
137+
build_partition(dest + 462, 0, 0, 0, 0);
138+
build_partition(dest + 478, 0, 0, 0, 0);
139+
build_partition(dest + 494, 0, 0, 0, 0);
140+
141+
dest[510] = 0x55;
142+
dest[511] = 0xaa;
143+
144+
return true;
145+
146+
} else if (FLASH_PART1_START_BLOCK <= block && block < FLASH_PART1_START_BLOCK + FLASH_PART1_NUM_BLOCKS) {
147+
// non-MBR block, get data from flash memory, possibly via cache
148+
uint32_t flash_addr = FLASH_MEM_START_ADDR + (block - FLASH_PART1_START_BLOCK) * FLASH_BLOCK_SIZE;
149+
uint8_t *src = flash_cache_get_addr_for_read(flash_addr);
150+
memcpy(dest, src, FLASH_BLOCK_SIZE);
151+
return true;
152+
153+
} else {
154+
// bad block number
155+
return false;
156+
}
157+
}
158+
159+
bool storage_write_block(const uint8_t *src, uint32_t block) {
160+
//printf("WR %u\n", block);
161+
if (block == 0) {
162+
// can't write MBR, but pretend we did
163+
return true;
164+
165+
} else if (FLASH_PART1_START_BLOCK <= block && block < FLASH_PART1_START_BLOCK + FLASH_PART1_NUM_BLOCKS) {
166+
// non-MBR block, copy to cache
167+
uint32_t flash_addr = FLASH_MEM_START_ADDR + (block - FLASH_PART1_START_BLOCK) * FLASH_BLOCK_SIZE;
168+
uint8_t *dest = flash_cache_get_addr_for_write(flash_addr);
169+
memcpy(dest, src, FLASH_BLOCK_SIZE);
170+
flash_tick_counter_last_write = HAL_GetTick();
171+
return true;
172+
173+
} else {
174+
// bad block number
175+
return false;
176+
}
177+
}

stmhal/storage.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#define FLASH_BLOCK_SIZE (512)
2+
3+
void storage_init(void);
4+
uint32_t storage_get_block_size(void);
5+
uint32_t storage_get_block_count(void);
6+
bool storage_needs_flush(void);
7+
void storage_flush(void);
8+
bool storage_read_block(uint8_t *dest, uint32_t block);
9+
bool storage_write_block(const uint8_t *src, uint32_t block);

0 commit comments

Comments
 (0)