@@ -58,6 +58,9 @@ static supervisor_allocation *supervisor_cache = NULL;
58
58
59
59
// Wait until both the write enable and write in progress bits have cleared.
60
60
static bool wait_for_flash_ready (void ) {
61
+ if (flash_device == NULL ) {
62
+ return false;
63
+ }
61
64
bool ok = true;
62
65
// Both the write enable and write in progress bits should be low.
63
66
if (flash_device -> no_ready_bit ) {
@@ -192,6 +195,9 @@ static bool copy_block(uint32_t src_address, uint32_t dest_address) {
192
195
return true;
193
196
}
194
197
198
+ #define READ_JEDEC_ID_RETRY_COUNT (100)
199
+
200
+ // If this fails, flash_device will remain NULL.
195
201
void supervisor_flash_init (void ) {
196
202
if (flash_device != NULL ) {
197
203
return ;
@@ -220,7 +226,11 @@ void supervisor_flash_init(void) {
220
226
#else
221
227
// The response will be 0xff if the flash needs more time to start up.
222
228
uint8_t jedec_id_response [3 ] = {0xff , 0xff , 0xff };
223
- while (jedec_id_response [0 ] == 0xff ) {
229
+ // Response can also be 0x00 if reading before ready. When compiled with `-O2`, typically
230
+ // takes three tries to read on Grand Central M4.
231
+
232
+ size_t count = READ_JEDEC_ID_RETRY_COUNT ;
233
+ while ((count -- > 0 ) && (jedec_id_response [0 ] == 0xff || jedec_id_response [2 ] == 0x00 )) {
224
234
spi_flash_read_command (CMD_READ_JEDEC_ID , jedec_id_response , 3 );
225
235
}
226
236
for (uint8_t i = 0 ; i < EXTERNAL_FLASH_DEVICE_COUNT ; i ++ ) {
@@ -234,6 +244,7 @@ void supervisor_flash_init(void) {
234
244
}
235
245
#endif
236
246
if (flash_device == NULL ) {
247
+ // Flash did not respond. Give up.
237
248
return ;
238
249
}
239
250
@@ -293,6 +304,9 @@ uint32_t supervisor_flash_get_block_size(void) {
293
304
294
305
// The total number of available blocks.
295
306
uint32_t supervisor_flash_get_block_count (void ) {
307
+ if (flash_device == NULL ) {
308
+ return 0 ;
309
+ }
296
310
// We subtract one erase sector size because we may use it as a staging area
297
311
// for writes.
298
312
return (flash_device -> total_size - SPI_FLASH_ERASE_SIZE ) / FILESYSTEM_BLOCK_SIZE ;
0 commit comments