Beware that this post is related to an SDK in maintenance mode
More Info: Consider nRF Connect SDK for new designs
This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Saving saadc data into a micro SD card using fatfs example - FR_NOT_ENABLED

Hello.

I am trying to write code to read data using saadc and write the sampled data in a micro SD card using fatfs.

I started from the fatfs example and initialized and configured the saadc exactly as in saadc example except that I changed the timer to sample 120 samples at every 1 ms.

 I configured a queue to received the sampled data. I store the data in the queue at every call to the saadc callback:

void saadc_callback(nrf_drv_saadc_evt_t const * p_event)
{
    if (p_event->type == NRF_DRV_SAADC_EVT_DONE)
    {
        ret_code_t err_code;

        err_code = nrf_drv_saadc_buffer_convert(p_event->data.done.p_buffer, SAMPLES_IN_BUFFER);
        APP_ERROR_CHECK(err_code);  

        if(done < NUMBER_OF_PACKETS){
            err_code = byte_queue_write(p_event->data.done.p_buffer, SAMPLES_IN_BUFFER * 2);//If data cannot be written to queue                        
            done++;
        }
        else{
            NRF_LOG_INFO("DONE WRITING TO THE FILE");
        }
        
    }
}

In the while loop, if there is data in the queue, I try to get it and save it in the file on the SD card:

while (true)
    {
        //__WFE();      

        if(!(byte_queue_read(saadc_source, sizeof(saadc_source)))){
            unit16_2_uint8(saadc_source, saadc_destination);

            NRF_LOG_INFO("Writing to file " FILE_NAME "...");
            ff_result = f_open(&file, FILE_NAME2, FA_READ | FA_WRITE | FA_OPEN_APPEND);
            if (ff_result != FR_OK)
            {
                NRF_LOG_INFO("Unable to open or create file: " FILE_NAME ".");
                            
            }

            ff_result = f_write(&file, saadc_destination, sizeof(saadc_destination) - 1, (UINT *) &bytes_written);
            if (ff_result != FR_OK)
            {
                NRF_LOG_INFO("Write failed\r\n.");
            }
            else
            {
                NRF_LOG_INFO("%d bytes written.", bytes_written);
            }

            (void) f_close(&file);          

        }        
                            
    }

However, the f_open() function always returns FR_NOT_ENABLED

And the device ends up stuck here:

This error seems to happen in this location at the file ff.c:

However, If I call the function fatfs_example() I can save the example file NORDIC.TXT without any errors.

Would anyone happen to know what could be causing the error?

Thank you.

Parents Reply Children
  • Hi 

    Thanks a lot for your detailed report. 

    Can you zip and attach your project so I can have a look at the code more easily?

    I can make the case private, in case you don't want to share your code in a public case. 

    Best regards
    Torbjørn

  • Hello.

    Sharing the code is not a problem at all. Most of the work was done by Nordic anyway. However, I was preparing to share the code and I seem to have fixed the problem. To be honest, I am not sure how I fixed it, but I suspect it was a problem with the configuration of the queue. I was trying to store the uint16_t data type coming from the ADC in the queue, but then I decided to convert the data before storing it in the queue (Convert from uint16_t to uint8_t). So, I changed the queue configuration to store uint8_t data and it worked!

    Still, I am sharing the code in case someone needs it. This code configures the nrf52840 to sample 120 samples from pins p.0.04 and p.0.05, converts the data from unit16_t to uint8_t (120 samples become 240 elements), and rearranges the samples in an array with 240 samples, being that the first 120 elements correspond to channel 1 and the last 120 elements correspond to channel 2.

    OBS: The code was written in SDK 16.0

    fatfs_saadc.zip

    It may not have been written with the finest of the C programming skills, but it seems to be doing the job.

    Thank you for your help.

    Thiago.

  • Hi Thiago

    Great to hear you found the issue, and thanks a lot for sharing your code Slight smile

    I will consider the case resolved then. 

    Best regards
    Torbjørn

Related