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

How to store BLE nus uart data in Flash Memory

I am working on a project where I have to store the incoming UART data over BLE on the NRF52840. I am using fstorage library to store the data in flash but I am not able to store the data in flash memory (NRF Breakpoint after trying to write the data to flash)

I have attached my ble nus data handler function and the main function for reference.

bool ble_data_receive=false;
char data[100];


static void nus_data_handler(ble_nus_evt_t * p_evt)
{
    app_uart_flush();
    if (p_evt->type == BLE_NUS_EVT_RX_DATA && p_evt->params.rx_data.length>0)
    {
        uint32_t err_code;

        NRF_LOG_DEBUG("Received data from BLE NUS. Writing data on UART.");
        NRF_LOG_HEXDUMP_DEBUG(p_evt->params.rx_data.p_data, p_evt->params.rx_data.length);
        for (uint32_t i = 0; i < p_evt->params.rx_data.length; i++)
        {
            do
            {
                data[i]=p_evt->params.rx_data.p_data[i];
                err_code = app_uart_put(p_evt->params.rx_data.p_data[i]);
                if ((err_code != NRF_SUCCESS) && (err_code != NRF_ERROR_BUSY))
                {
                    NRF_LOG_ERROR("Failed receiving NUS message. Error 0x%x. ", err_code);
                    APP_ERROR_CHECK(err_code);
                }
            } while (err_code == NRF_ERROR_BUSY);
        }
        if (p_evt->params.rx_data.p_data[p_evt->params.rx_data.length - 1] == '\r')
        {
            while (app_uart_put('\n') == NRF_ERROR_BUSY);
        }
    }
    printf("The length of data is: %d\n\r",p_evt->params.rx_data.length);
    ble_data_receive=true;
    printf("BLE Data received and bool changed to true\n\r");
}


int main(void)
{
////////////////////////////// F STORAGE CODE//////////////////////////

    ret_code_t err_code;
    nrf_fstorage_api_t * p_fs_api;
    p_fs_api = &nrf_fstorage_sd;
    err_code = nrf_fstorage_init(&fstorage, p_fs_api, NULL);
    APP_ERROR_CHECK(err_code);
 
//////////////////////////////// BLE UART CODE/////////////////////////    
    bool erase_bonds;
    uart_init();
    log_init();
    //cli_init();
    timer_init();
    power_management_init();
    ble_stack_init();
    gap_params_init();
    gatt_init();
    services_init();
    advertising_init();                         
    conn_params_init();
    advertising_start();
    //cli_start();

    for (;;)
    {
        idle_state_handle();
        if(ble_data_receive==true){
            printf("Commencing write operation\n\r");
            err_code = nrf_fstorage_write(&fstorage, 0x60000, &data, 100, NULL);
            APP_ERROR_CHECK(err_code);
            if(err_code==NRF_SUCCESS){
              printf("Write operation done!\n\r");
            }
            printf("Printing data here\n\r");
            printf(&data);
            ble_data_receive=false;
            printf("BLE Data received and bool changed to false\n\r");
        }
        //cli_process();
    }
}

Related