I looked into it a bit, the Ice40.reset(MCNTRL))
is screwing things up so I removed that.
I used
void Flash::Enable(void)
{
release_flash();
free_flash();
flash_SPI_Enable();
uint8_t uCommand = 0xAB;
gpio_low (ICE40_SPI_CS);
write(&uCommand, 1);
gpio_high(ICE40_SPI_CS);
}
void Flash::WriteEnable(void)
{
Enable();
uint8_t uCommand = 0x06;
gpio_low (ICE40_SPI_CS);
write(&uCommand, 1);
gpio_high(ICE40_SPI_CS);
}
and changed:
uint8_t Flash::erase_write(uint8_t *data, uint8_t len, uint16_t esize){
uint32_t tail = addr + len;
uint8_t *page;
uint16_t wsize;
uint8_t wen = WEN;
uint8_t rs, sts = STATUS;
uint8_t pre[4] = {(esize == 32) ? ERASE32 : ERASE64, addr >> 16, addr >> 8, addr};
page = data;
bool bOk = CheckId();
while(addr < tail){
wsize = (tail - addr) > 255 ? 256 : tail - addr;
if((addr + wsize) >= block) { // too many times!
WriteEnable();
gpio_low(ICE40_SPI_CS);
erase(ERASE64);
gpio_high(ICE40_SPI_CS);
//HAL_Delay(2200);
// wait ready
gpio_low(ICE40_SPI_CS);
write(&sts,1);
do {
read(&rs,1);
} while (rs & 0x01);// READY bit?
gpio_high(ICE40_SPI_CS);
mode_led_toggle();
rs = 0;
block += 0x10000;
}
WriteEnable();
if(wsize == 256){
gpio_low(ICE40_SPI_CS);
write_page(page);
gpio_high(ICE40_SPI_CS);
addr += wsize;
page += wsize;
} else { // remainder less than page size
for(uint8_t i = 0; i < wsize; i++){
gpio_low(ICE40_SPI_CS);
write_byte(page++);
gpio_high(ICE40_SPI_CS);
addr++;
}
}
// wait ready
gpio_low(ICE40_SPI_CS);
write(&sts,1);
do {
read(&rs,1);
} while (rs & 0x01);// READY bit?
gpio_high(ICE40_SPI_CS);
}
return 0;
}
The erase and ready bits are now working, I think there may be some other issues though!
I'm also wondering if you have to WriteEnable between bytes?
My code is here: https://github.com/AndrewCapon/IceCore/blob/USB-CDC-issue-3/firmware/myStorm/CADFlashHandler.cpp
It has a streaming interface as well but uses a BuffereredWrite to store up 256 bytes and then write pages, It does work in a slightly different way to your stream though as it has an Init() and Stream() section.
I could create a new Flash class that fits in to your streaming model though?