callback to erase flash sector */
| 413 | callback to erase flash sector |
| 414 | */ |
| 415 | bool Storage::_flash_erase_sector(uint8_t sector) |
| 416 | { |
| 417 | #ifdef STORAGE_FLASH_PAGE |
| 418 | #if AP_FLASH_STORAGE_DOUBLE_PAGE |
| 419 | sector *= 2; |
| 420 | #endif |
| 421 | // erasing a page can take long enough that USB may not initialise properly if it happens |
| 422 | // while the host is connecting. Only do a flash erase if we have been up for more than 4s |
| 423 | for (uint8_t i=0; i<STORAGE_FLASH_RETRIES; i++) { |
| 424 | /* |
| 425 | a sector erase stops the whole MCU. We need to setup a long |
| 426 | expected delay, and not only when running in the main |
| 427 | thread. We can't use EXPECT_DELAY_MS() as it checks we are |
| 428 | in the main thread |
| 429 | */ |
| 430 | EXPECT_DELAY_MS(1000); |
| 431 | #if AP_FLASH_STORAGE_DOUBLE_PAGE |
| 432 | if (hal.flash->erasepage(_flash_page+sector) && hal.flash->erasepage(_flash_page+sector+1)) { |
| 433 | return true; |
| 434 | } |
| 435 | #else |
| 436 | if (hal.flash->erasepage(_flash_page+sector)) { |
| 437 | return true; |
| 438 | } |
| 439 | #endif |
| 440 | hal.scheduler->delay(1); |
| 441 | } |
| 442 | return false; |
| 443 | #else |
| 444 | return false; |
| 445 | #endif |
| 446 | } |
| 447 | |
| 448 | /* |
| 449 | callback to check if erase is allowed |