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

Writing to flash in the nus example

Hi

I am using the nrf53832 dk.

In the nus example once the data is received from the phone(central) I want to write this data to the flash. I referred to the fstorage example for flash write.

 I made the following changes to the nus_data_handler function.

I am able to run the code, but when i send data from my phone(central) to the dk no data is sent and the device  gets disconnected and starts advertising again.

static void nus_data_handler(ble_nus_evt_t * p_evt)
{
    char wagon[20];//string i added 
    if (p_evt->type == BLE_NUS_EVT_RX_DATA)
    {
        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);
        strcpy(wagon,p_evt->params.rx_data.p_data);//string copy
        for (uint32_t i = 0; i < p_evt->params.rx_data.length; i++)
        {
            do
            {
                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);
        }
    }
    //changes i made 
    ret_code_t rc;
    nrf_fstorage_api_t * p_fs_api;
    p_fs_api = &nrf_fstorage_sd;
    rc = nrf_fstorage_init(&fstorage, p_fs_api, NULL);
    APP_ERROR_CHECK(rc);
    rc = nrf_fstorage_write(&fstorage, 0x3e000, wagon, strlen(wagon), NULL);
    APP_ERROR_CHECK(rc);

}

Is my methodology of writing to flash correct?

If not what changes should i make?

Thanks in advance

Related