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
  • Hi,

    If you have already initialized UART, you will get INVALID_STATE error if you try to init it again. You either need to uninit the old initialization before re-init, or use the old initialized UART interface.

    Best regards,
    Jørgen

  • Hi Jorgen,

    thank you for your answer, I figured out that I could not init the UART twice, same with BLE stack & other init functions, how to uninit those ?

    I also tried something else, instead of trying to re-init, I change the device mode & write the new mode in flash, reset the device & then init with the new mode but I have some issues trying to write in flash. Indeed, when the device receive the UART command to change its mode, a flag is set to 1 & in the main function, when this flag is set, I try to write in flash the new mode & then reset the device but writing to flash fails, the program waits for NRF_EVT_FLASH_OPERATION_SUCCESS which never happens.

    I do not understand pretty well this behaviour, I guess the sys_evt_handler callback is never called to trigger this event but I am not sure and i do not know why either.

    Regards,

    Nicolas Hell.

Reply
  • Hi Jorgen,

    thank you for your answer, I figured out that I could not init the UART twice, same with BLE stack & other init functions, how to uninit those ?

    I also tried something else, instead of trying to re-init, I change the device mode & write the new mode in flash, reset the device & then init with the new mode but I have some issues trying to write in flash. Indeed, when the device receive the UART command to change its mode, a flag is set to 1 & in the main function, when this flag is set, I try to write in flash the new mode & then reset the device but writing to flash fails, the program waits for NRF_EVT_FLASH_OPERATION_SUCCESS which never happens.

    I do not understand pretty well this behaviour, I guess the sys_evt_handler callback is never called to trigger this event but I am not sure and i do not know why either.

    Regards,

    Nicolas Hell.

Children
No Data
Related