use all spi flash v0.1

* introduced a new communication command CMD_FLASHMEM_PAGES64K to get the number of 64k pages
* "the last page" is a special, holding the dicts and some other stuff, relocated to there
* raised timeout when wiping a mem page (W25Q16 looks a bit slower than W25X20BV)
* loop all pages in Flash_WipeMemory()
This commit is contained in:
ANTodorov 2024-11-17 18:32:11 +02:00
commit 48ec109a1e
No known key found for this signature in database
GPG key ID: 318CC11D7ED4016B
10 changed files with 193 additions and 36 deletions

View file

@ -43,6 +43,8 @@ static uint32_t FLASHMEM_SPIBAUDRATE = FLASH_BAUD;
#ifndef AS_BOOTROM
uint8_t spi_flash_p64k = 0;
void FlashmemSetSpiBaudrate(uint32_t baudrate) {
FLASHMEM_SPIBAUDRATE = baudrate;
Dbprintf("Spi Baudrate : %dMHz", FLASHMEM_SPIBAUDRATE / 1000000);
@ -144,14 +146,15 @@ uint16_t Flash_WriteData(uint32_t address, uint8_t *in, uint16_t len) {
return 0;
}
// out-of-range
if (((address >> 16) & 0xFF) > MAX_BLOCKS) {
Dbprintf("Flash_WriteData, block out-of-range");
if (!FlashInit()) {
if (g_dbglevel > 3) Dbprintf("Flash_WriteData init fail");
return 0;
}
if (!FlashInit()) {
if (g_dbglevel > 3) Dbprintf("Flash_WriteData init fail");
// out-of-range
if (((address >> 16) & 0xFF) > spi_flash_p64k) {
Dbprintf("Flash_WriteData, block out-of-range %02x > %02x", (address >> 16) & 0xFF, spi_flash_p64k);
FlashStop();
return 0;
}
@ -187,8 +190,8 @@ uint16_t Flash_WriteDataCont(uint32_t address, uint8_t *in, uint16_t len) {
return 0;
}
if (((address >> 16) & 0xFF) > MAX_BLOCKS) {
Dbprintf("Flash_WriteDataCont, block out-of-range");
if (((address >> 16) & 0xFF) > spi_flash_p64k) {
Dbprintf("Flash_WriteDataCont, block out-of-range %02x > %02x", (address >> 16) & 0xFF, spi_flash_p64k);
return 0;
}
@ -266,18 +269,11 @@ bool Flash_WipeMemory(void) {
// Each block is 64Kb. Four blocks
// one block erase takes 1s ( 1000ms )
Flash_WriteEnable();
Flash_Erase64k(0);
Flash_CheckBusy(BUSY_TIMEOUT);
Flash_WriteEnable();
Flash_Erase64k(1);
Flash_CheckBusy(BUSY_TIMEOUT);
Flash_WriteEnable();
Flash_Erase64k(2);
Flash_CheckBusy(BUSY_TIMEOUT);
Flash_WriteEnable();
Flash_Erase64k(3);
Flash_CheckBusy(BUSY_TIMEOUT);
for (uint8_t i=0; i < spi_flash_p64k; i++) {
Flash_WriteEnable();
Flash_Erase64k(i);
Flash_CheckBusy(BUSY_TIMEOUT);
}
FlashStop();
return true;
@ -293,7 +289,7 @@ void Flash_WriteEnable(void) {
// execution time: 0.8ms / 800us
bool Flash_Erase4k(uint8_t block, uint8_t sector) {
if (block > MAX_BLOCKS || sector > MAX_SECTORS) return false;
if (block > spi_flash_p64k || sector > MAX_SECTORS) return false;
FlashSendByte(SECTORERASE);
FlashSendByte(block);
@ -328,7 +324,7 @@ bool Flash_Erase32k(uint32_t address) {
// 0x03 00 00 -- 0x 03 FF FF == block 3
bool Flash_Erase64k(uint8_t block) {
if (block > MAX_BLOCKS) return false;
if (block > spi_flash_p64k) return false;
FlashSendByte(BLOCK64ERASE);
FlashSendByte(block);
@ -404,6 +400,7 @@ void Flashmem_print_status(void) {
);
}
}
Dbprintf(" Flash pages (64k)....... " _YELLOW_("0x%02x (%u)"), spi_flash_p64k, spi_flash_p64k);
uint8_t uid[8] = {0, 0, 0, 0, 0, 0, 0, 0};
Flash_UniqueID(uid);
@ -457,6 +454,38 @@ void Flashmem_print_info(void) {
FlashStop();
}
//read spi flash JEDEC ID and fill the global variable spi_flash_p64k
bool FlashDetect(bool flash_init) {
flash_device_type_t flash_device = {0};
if (flash_init) {
if (!FlashInit()) {
if (g_dbglevel > 3) Dbprintf("FlashDetect() FlashInit fail");
return 0;
}
}
if (!Flash_ReadID(&flash_device, true)) {
if (g_dbglevel > 3) Dbprintf("Flash_ReadID failed");
return false;
}
uint32_t identifier = (flash_device.manufacturer_id <<16) + (flash_device.device_id <<8) + flash_device.device_id2;
int i = 0;
for (; i < ARRAYLEN(SpiFlashTable); i++) {
if (SpiFlashTable[i].identifier == identifier) {
break;
}
}
spi_flash_p64k = SpiFlashTable[i].pages64;
if (flash_init) {
FlashStop();
}
return true;
}
#endif // #ifndef AS_BOOTROM
@ -471,6 +500,12 @@ bool FlashInit(void) {
return false;
}
#ifndef AS_BOOTROM
if (spi_flash_p64k == 0){
if (!FlashDetect(false)) return 0;
}
#endif // #ifndef AS_BOOTROM
return true;
}

View file

@ -145,6 +145,37 @@ uint16_t Flash_WriteDataCont(uint32_t address, uint8_t *in, uint16_t len);
void Flashmem_print_status(void);
void Flashmem_print_info(void);
typedef struct spi_flash_s {
const uint32_t identifier;
const uint8_t pages64;
const char *desc;
} spi_flash_t;
// spi_flash_t is expected to be NULL terminated
const static spi_flash_t SpiFlashTable[] = {
// Manufacturer: Puya
{ 0x856015, 32, "P25Q16H" },
// Manufacturer: Winbond
{ 0xEF3012, 4, "W25X20BV" },
{ 0xEF3013, 8, "W25X40BV" },
{ 0xEF4013, 8, "W25Q40BV" },
{ 0xEF4014, 16, "W25Q80BV" },
{ 0xEF4015, 32, "W25Q16BV" },
{ 0xEF4016, 64, "W25Q32BV" },
{ 0xEF7022, 4, "W25Q02JV" },
{ 0x000000, 4, "Unknown!" }
};
#ifndef ARRAYLEN
# define ARRAYLEN(x) (sizeof(x)/sizeof((x)[0]))
#endif
extern uint8_t spi_flash_p64k;
bool FlashDetect(bool);
#endif // #ifndef AS_BOOTROM