36
36
37
37
#define FLASH_PART1_START_BLOCK (0x100)
38
38
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
+
39
43
static bool storage_is_initialised = false;
40
44
41
45
void storage_init (void ) {
@@ -44,6 +48,10 @@ void storage_init(void) {
44
48
45
49
MICROPY_HW_BDEV_IOCTL (BDEV_IOCTL_INIT , 0 );
46
50
51
+ #if defined(MICROPY_HW_BDEV2_IOCTL )
52
+ MICROPY_HW_BDEV2_IOCTL (BDEV_IOCTL_INIT , 0 );
53
+ #endif
54
+
47
55
// Enable the flash IRQ, which is used to also call our storage IRQ handler
48
56
// It needs to go at a higher priority than all those components that rely on
49
57
// the flash storage (eg higher than USB MSC).
@@ -57,15 +65,25 @@ uint32_t storage_get_block_size(void) {
57
65
}
58
66
59
67
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
60
71
return FLASH_PART1_START_BLOCK + MICROPY_HW_BDEV_IOCTL (BDEV_IOCTL_NUM_BLOCKS , 0 );
72
+ #endif
61
73
}
62
74
63
75
void storage_irq_handler (void ) {
64
76
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
65
80
}
66
81
67
82
void storage_flush (void ) {
68
83
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
69
87
}
70
88
71
89
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) {
114
132
}
115
133
116
134
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
117
138
build_partition (dest + 462 , 0 , 0 , 0 , 0 );
139
+ #endif
118
140
build_partition (dest + 478 , 0 , 0 , 0 , 0 );
119
141
build_partition (dest + 494 , 0 , 0 , 0 , 0 );
120
142
@@ -153,6 +175,12 @@ mp_uint_t storage_read_blocks(uint8_t *dest, uint32_t block_num, uint32_t num_bl
153
175
}
154
176
#endif
155
177
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
+
156
184
for (size_t i = 0 ; i < num_blocks ; i ++ ) {
157
185
if (!storage_read_block (dest + i * FLASH_BLOCK_SIZE , block_num + i )) {
158
186
return 1 ; // error
@@ -168,6 +196,12 @@ mp_uint_t storage_write_blocks(const uint8_t *src, uint32_t block_num, uint32_t
168
196
}
169
197
#endif
170
198
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
+
171
205
for (size_t i = 0 ; i < num_blocks ; i ++ ) {
172
206
if (!storage_write_block (src + i * FLASH_BLOCK_SIZE , block_num + i )) {
173
207
return 1 ; // error
0 commit comments