This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Write in flash + re-init problem

Hello,

I am currently running into an issue with nrf51822. I am trying to write in flash a flag which defines the device I use. Indeeed, the device on which the code will run can have 3 modes & a user can modify this mode with a command sent through UART. When the command has been run, the system should re-init to modify the UART, advertising or conn_params settings.

There is no particular problem with initialization but when the UART command is run, the system stops. Indeed, the write command is blocked in an infinite loop, waiting for the NRF_EVT_FLASH_OPERATION_SUCCESS event.

I join the code run at initialization & the function used to write in flash, the function run when the UART command has been received just calls the init function to re-init all settings with the new device mode.

void system_init(void)
{
    uint32_t err_code;
    
    softdevice_sys_evt_handler_set(sys_evt_handler);
    
    if (ble_device_type == DEFAULT)
    {
        ble_device_type = WBLE;
    }
    APP_TIMER_INIT(APP_TIMER_PRESCALER, APP_TIMER_OP_QUEUE_SIZE, NULL);
    
    if (ble_device_type == WBLE)
    {
        db_discovery_init();
    }
    uart_init();
    ble_stack_init();
    //Function used to write the device type flag in flash memory
    wat_write_flash_type((uint8_t )ble_device_type);
    
    else if (ble_device_type == UART_MODULE || ble_device_type == WBEACON)
    {
        gap_params_init();
        services_init();
        if (ble_device_type == UART_MODULE)
        {
            advertising_init();
            conn_params_init();
            err_code = ble_advertising_start(BLE_ADV_MODE_SLOW);
        }
        else if (ble_device_type == WBEACON)
        {
            err_code = ble_advertising_start(BLE_ADV_MODE_FAST);
            APP_ERROR_CHECK(err_code);
        }
        
    }
}

uint8_t wat_write_flash_type(uint8_t type)
{
    uint32_t status = NRF_ERROR_BUSY;
    uint8_t d_type[4];
    
    d_type[0] = type;
    d_type[1] = 0;
    d_type[2] = 0;
    d_type[3] = 0;
    
    do
    {
        status = sd_flash_write((uint32_t *)TYPE_LOCATION, (uint32_t*)d_type, 1);
    }
    while (status == NRF_ERROR_BUSY);
    
    flashBusy = true;
    
    while(flashBusy)
    {
        sd_app_evt_wait();
    }

    if(status != NRF_SUCCESS)
    {
        return 1; // Error
    }
    
    return 0;
}

I also join the callback used to trigger the event.

void sys_evt_handler(uint32_t evt)
{
    switch(evt)
    {
        case NRF_EVT_FLASH_OPERATION_SUCCESS:
            flashBusy = false;
        break;
        
        case NRF_EVT_FLASH_OPERATION_ERROR:
        break;
        
        default:
        break;
    }
}

It seems that when I try to re-init, the system is in INVALID_STATE (that is the error code returned by the function uart_init).

Do not hesitate to ask for further information if needed.

Parents Reply
  • I have already kinda done that with Keil & "Memory" section while in debug mode and the value did not change. I tried it with nrfjprog but got same results, when I erase & reflash all (including SoftDevice), if I read at this address, I have FF FF FF FF which is normal. Then I run the program once and set the default value at this memory location which is 00 00 00 00, that works.

    After that, I cannot change the value anymore, I try to erase it first then write the new value but that does not seems to work.

Children
Related