I am experimenting with calling sd_flash_write with waiting, if it returns NRF_ERROR_BUSY, just try again, and then return either success fail
void flashWrite( uint8_t *addr, uint16_t len, uint8_t *buf )
{
uint32_t error_code; uint32_t word_len;
uint32_t * buffer_ptr = NULL;
// buf address could be not WORD-aligned need to create a buffer for it
if (len ==0){
Display(("write len = 0\r\n"));
return;
}
if ( (len & 0x3) == 0){
word_len = len >> 2; // len is times of 4
}else{
word_len = (len >> 2) + 1;
}
if (( buffer_ptr = (uint32_t*)malloc(word_len<<2) ) ) {
memcpy(buffer_ptr, buf, word_len<<2);
while (( error_code = sd_flash_write( (uint32_t*) addr, buffer_ptr, word_len)) == NRF_ERROR_BUSY);
if ( (error_code ) != NRF_SUCCESS){
Display(("sd_flash_write() error code 0x%x addr=0x%x len=0x%x wordlen=0x%x srcaddr=0x%x\r\n", error_code, addr, len, word_len, buf));
}
else
{
Display(("sd_flash_write() OK addr=0x%x len=0x%x wordlen=0x%x\r\n", addr, len, word_len));
}
free(buffer_ptr);
}else{
Display(("flashWrite malloc error\r\n"));
}
};
/**@brief Function for dispatching a system event to interested modules.
*
* @details This function is called from the System event interrupt handler after a system
* event has been received.
*
* @param[in] sys_evt System stack event.
*/
void sys_evt_dispatch(uint32_t sys_evt)
{
if (sys_evt == NRF_EVT_FLASH_OPERATION_SUCCESS){
Display(("Flash Op is Done\r\n"));
}else{
LOG_DEBUG(" sys_evt_dispatch code 0x%x", sys_evt);
}
}
main()
{
// init staff, erase flash ...
flashWrite(...);
flashWrite(...);
flashWrite(...);
}
If I called several sd_flash_write in such manner back to back, I can only receive one NRF_EVT_FLASH_OPERATION_SUCCESS callback ( "Flash Op is Done" is only printed once")
I checked flash memory content all write operation did succeed.
So it seems NRF_EVT_FLASH_OPERATION_SUCCESS more like "the last flash operation is successful and flash is idle now", it is not like every flash operation will trigger one NRF_EVT_FLASH_OPERATION_SUCCESS , but a series of flash operation trigger one NRF_EVT_FLASH_OPERATION_SUCCESS
Is SoftDevice designed to be so?