And here’s the code, only real modification is the addition of the spi code to trigger the full chip erase.
Code
#include “mbed.h”
#include “SPIFBlockDevice.h”
FileHandle *mbed::mbed_override_console(int)
{
static BufferedSerial serial(PTC15, PTC14); //Use for CFG 1 varients
return &serial;
}
// Create flash device on SPI bus with PTE5 as chip select
SPIFBlockDevice spif(PTD2, PTD3, PTD1, PTD0, 20000000);
SPI spi(PTD2, PTD3, PTD1, PTD0);
unsigned char block_protection_10[18];
void WriteEnable(void) {
spi.select();
spi.write(0x06);
spi.deselect();
}
void ClearBlockProtect(void) {
unsigned char i = 0;
block_protection_10[0] = 0x00;
block_protection_10[1] = 0x00;
block_protection_10[2] = 0x00;
block_protection_10[3] = 0x00;
block_protection_10[4] = 0x00;
block_protection_10[5] = 0x00;
block_protection_10[6] = 0x00;
block_protection_10[7] = 0x00;
block_protection_10[8] = 0x00;
block_protection_10[9] = 0x00;
block_protection_10[10] = 0x00;
block_protection_10[11] = 0x00;
block_protection_10[12] = 0x00;
block_protection_10[13] = 0x00;
block_protection_10[14] = 0x00;
block_protection_10[15] = 0x00;
block_protection_10[16] = 0x00;
block_protection_10[17] = 0x00;
WriteEnable();
spi.select();
spi.write(0x42);
for (i = 18; i > 0; i--) {spi.write(block_protection_10[i - 1]);}
spi.deselect();
}
void ChipErase(void) {
ClearBlockProtect();
WriteEnable();
spi.select();
spi.write(0xC7);
spi.deselect();
}
int main()
{
//DigitalOut wp(PTB19);
printf("spif test\n");
// Initialize the SPI flash device, and print the memory layout
spif.init();
printf("spif size: %llu\n", spif.size());
printf("spif read size: %llu\n", spif.get_read_size());
printf("spif program size: %llu\n", spif.get_program_size());
printf("spif erase size: %llu\n", spif.get_erase_size());
ThisThread::sleep_for(1);
// Write "Hello World!" to the first block
char *buffer = (char *)malloc(spif.get_erase_size());
sprintf(buffer, "Hello World!\n");
int rc = spif.erase(0, spif.get_erase_size());
if(rc != 0){
printf("Erase Failed: %d\n", rc);
}
rc = spif.program(buffer, 0, spif.get_erase_size());
if(rc != 0){
printf("Program Failed: %d\n", rc);
}
sprintf(buffer, "NOPE\n");
// Read back what was stored
rc = spif.read(buffer, 0, spif.get_erase_size());
if(rc != 0){
printf("Read Failed: %d\n", rc);
}
printf("Contents: %s\n", buffer);
// Deinitialize the device
spif.deinit();
}