Skip to content

Commit 0d5bcca

Browse files
committed
stm32/storage: Provide support for a second block device.
1 parent bb3359f commit 0d5bcca

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

ports/stm32/storage.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@
3636

3737
#define FLASH_PART1_START_BLOCK (0x100)
3838

39+
#if defined(MICROPY_HW_BDEV2_IOCTL)
40+
#define FLASH_PART2_START_BLOCK (FLASH_PART1_START_BLOCK + MICROPY_HW_BDEV2_IOCTL(BDEV_IOCTL_NUM_BLOCKS, 0))
41+
#endif
42+
3943
static bool storage_is_initialised = false;
4044

4145
void storage_init(void) {
@@ -44,6 +48,10 @@ void storage_init(void) {
4448

4549
MICROPY_HW_BDEV_IOCTL(BDEV_IOCTL_INIT, 0);
4650

51+
#if defined(MICROPY_HW_BDEV2_IOCTL)
52+
MICROPY_HW_BDEV2_IOCTL(BDEV_IOCTL_INIT, 0);
53+
#endif
54+
4755
// Enable the flash IRQ, which is used to also call our storage IRQ handler
4856
// It needs to go at a higher priority than all those components that rely on
4957
// the flash storage (eg higher than USB MSC).
@@ -57,15 +65,25 @@ uint32_t storage_get_block_size(void) {
5765
}
5866

5967
uint32_t storage_get_block_count(void) {
68+
#if defined(MICROPY_HW_BDEV2_IOCTL)
69+
return FLASH_PART2_START_BLOCK + MICROPY_HW_BDEV2_IOCTL(BDEV_IOCTL_NUM_BLOCKS, 0);
70+
#else
6071
return FLASH_PART1_START_BLOCK + MICROPY_HW_BDEV_IOCTL(BDEV_IOCTL_NUM_BLOCKS, 0);
72+
#endif
6173
}
6274

6375
void storage_irq_handler(void) {
6476
MICROPY_HW_BDEV_IOCTL(BDEV_IOCTL_IRQ_HANDLER, 0);
77+
#if defined(MICROPY_HW_BDEV2_IOCTL)
78+
MICROPY_HW_BDEV2_IOCTL(BDEV_IOCTL_IRQ_HANDLER, 0);
79+
#endif
6580
}
6681

6782
void storage_flush(void) {
6883
MICROPY_HW_BDEV_IOCTL(BDEV_IOCTL_SYNC, 0);
84+
#if defined(MICROPY_HW_BDEV2_IOCTL)
85+
MICROPY_HW_BDEV2_IOCTL(BDEV_IOCTL_SYNC, 0);
86+
#endif
6987
}
7088

7189
static void build_partition(uint8_t *buf, int boot, int type, uint32_t start_block, uint32_t num_blocks) {
@@ -114,7 +132,11 @@ bool storage_read_block(uint8_t *dest, uint32_t block) {
114132
}
115133

116134
build_partition(dest + 446, 0, 0x01 /* FAT12 */, FLASH_PART1_START_BLOCK, MICROPY_HW_BDEV_IOCTL(BDEV_IOCTL_NUM_BLOCKS, 0));
135+
#if defined(MICROPY_HW_BDEV2_IOCTL)
136+
build_partition(dest + 462, 0, 0x01 /* FAT12 */, FLASH_PART2_START_BLOCK, MICROPY_HW_BDEV2_IOCTL(BDEV_IOCTL_NUM_BLOCKS, 0));
137+
#else
117138
build_partition(dest + 462, 0, 0, 0, 0);
139+
#endif
118140
build_partition(dest + 478, 0, 0, 0, 0);
119141
build_partition(dest + 494, 0, 0, 0, 0);
120142

@@ -153,6 +175,12 @@ mp_uint_t storage_read_blocks(uint8_t *dest, uint32_t block_num, uint32_t num_bl
153175
}
154176
#endif
155177

178+
#if defined(MICROPY_HW_BDEV2_READBLOCKS)
179+
if (FLASH_PART2_START_BLOCK <= block_num && block_num + num_blocks <= FLASH_PART2_START_BLOCK + MICROPY_HW_BDEV2_IOCTL(BDEV_IOCTL_NUM_BLOCKS, 0)) {
180+
return MICROPY_HW_BDEV2_READBLOCKS(dest, block_num - FLASH_PART2_START_BLOCK, num_blocks);
181+
}
182+
#endif
183+
156184
for (size_t i = 0; i < num_blocks; i++) {
157185
if (!storage_read_block(dest + i * FLASH_BLOCK_SIZE, block_num + i)) {
158186
return 1; // error
@@ -168,6 +196,12 @@ mp_uint_t storage_write_blocks(const uint8_t *src, uint32_t block_num, uint32_t
168196
}
169197
#endif
170198

199+
#if defined(MICROPY_HW_BDEV2_WRITEBLOCKS)
200+
if (FLASH_PART2_START_BLOCK <= block_num && block_num + num_blocks <= FLASH_PART2_START_BLOCK + MICROPY_HW_BDEV2_IOCTL(BDEV_IOCTL_NUM_BLOCKS, 0)) {
201+
return MICROPY_HW_BDEV2_WRITEBLOCKS(src, block_num - FLASH_PART2_START_BLOCK, num_blocks);
202+
}
203+
#endif
204+
171205
for (size_t i = 0; i < num_blocks; i++) {
172206
if (!storage_write_block(src + i * FLASH_BLOCK_SIZE, block_num + i)) {
173207
return 1; // error

0 commit comments

Comments
 (0)