Now during startup I read the first four bytes of address 0x00 of the flash.
If it reads 0xE9030060, this means the mode is QIO, and I write back 0xE9030260 to set it to DIO.
If it already has been set to DIO, I don't write anything.
This really seems to work well!
Just a small summary:
0xE9 03 02 60
Magic value ? SPI Mode Size(high nibble)& Speed(low nibble)
00: QIO 0x: 512kB (256x2) x0: 40 MHz
01: QOUT 1x: 256kB x1: 26,67 MHz
02: DIO 2x: 1MB (512x2) x2: 20 MHz
03: DOUT 3x: 2MB (512x2) xf: 80 MHz
4x: 4MB (512x2)
5x: 2MB (1024x2)
6x: 4MB (1024x2)
One more thing I can't seem to figure out:
I have to erase the whole first sector(4kB), before I can write to it.
This means I first have to read 4096 Bytes to a buffer, change the 3rd Byte and write the whole thing back.
Is there a way which would not consume this many RAM?
I know I can do a os_zalloc and then free the whole block, but I'd rather not.
spi_flash_read(0x0 * SPI_FLASH_SEC_SIZE, (uint32 *)buff, 4096);
os_printf("0x0 sec:%02x%02x%02x%02x\r\n", buff[0], buff[1], buff[2], buff[3]);
if((buff[2] != 0x02) || buff[3] != 0x60)
{
os_printf("Flash is not set to DIO or 4MB+40MHz. Correcting settings...\n\r");
buff[0] = 0xE9;
buff[1] = 0x03;
buff[2] = 0x02;
buff[3] = 0x60;
spi_flash_erase_sector(0x0);
result = spi_flash_write(0x0 * SPI_FLASH_SEC_SIZE, (uint32 *)&buff[0], 4096);
os_printf("Result: %d\n\r", result);
}
else
{
os_printf("Flash settings are quitte allright\n\r");
}