1. 查詢對應 API
首先可以在 SPI Flash API 裏查看對應的 flash API。發現使用如下 API:
//spi_flash_erase_range(start_addr, size);
spi_flash_erase_range(0x8000,0x8000);
其中第一個參數 start_addr
代表偏移量,第二個參數 size
代表需要擦除的區域大小。
2.查詢分區表偏移量與大小
在 esp-idf 裏運行 make flash
後,可以看到提示 esptool.py --chip esp32 -p /dev/ttyUSB0 -b 460800 --before=default_reset --after=hard_reset write_flash --flash_mode dio --flash_freq 40m --flash_size 4MB 0x8000 partition_table/partition-table.bin 0x1000 bootloader/bootloader.bin 0x10000 hello-world.bin
。可以看到其中分區表的偏移量 0x8000 partition_table/partition-table.bin
。然後程序從 0x10000
開始,所以分區表大小爲 0x10000 - 0x8000 = 0x8000
。故在使用函數 spi_flash_erase_range(0x8000,0x8000)
即可。
3. 注意事項
在使用 spi_flash_erase_range(0x8000,0x8000)
後,有時芯片會一直復位並報錯,這是因爲並沒有開啓修改危險 flash 區域的權限。
- 解決方法:在終端裏輸入
make menuconfig
->SPI flash driver
->writing to dangerous flash regions
->allowed
,然後重新編譯燒錄即可。