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

Parents
  • I forgot to set the code snippet to C format 

    here is the snippet 

    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);
    
    }

  • As mentioned by  you can use FDS, but note that this does not provide any better integration with softdevice than what fstorage does. FDS is a higher layer library that builds on top of fstorage, they both support asynchronous write/erase, and both softdevice and NVMC backends. What FDS does provide, is simpler access and organization of stored data, in addition to wear leveling and content integration management.

    If you want to keep using fstorage, I would recommend that debug the application to check for error codes. It sounds like the chip resets, which is the default behavior if it ends up in the error handler.

  • when i run the above code in debug mode in ses 

    the app_error_weak.c file opens and execution stops at NRF_BREAKPOINT_COND;

    and the comment next to this statement states that "  On assert, the system can only recover with a reset."

    why is this happening?

  • Some function returned an error code. The error codes are normally passed to APP_ERROR_CHECK macro, which will call the error handler in case of non-zero error code (not NRF_SUCCESS). If you set the DEBUG flag, and have logging enabled, the error code and line number will be output to the log. You can also check the variables directly when debugging.

  • how do i setup the debug flag and enable logging on ses?

Reply Children
Related