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
  • Hello!

    Thank you for answering.

    I call it as shown in the image:

    I also added it in case f_open returns an error in the main while loop. But it returns FR_NOT_READY in the second call (also as shown on the image above).

    FYI, it enters the main while loop with the status FR_OK, after the call of fatfs_init(). That is, everything seems fine until the program enters the while loop.

  • Hello,

    I just found that if I comment the line shown on the image the code runs without a problem, except that I only get empty spaces stored in the TXT file because the data stored in the SD card is an empty array. The function causing the problem is the one that reads data from the queue. If I uncomment that line, the data is correctly retrieved from the queue, but there is an SD card error. If I comment on that line, the SD card works but I don't read the data from the queue.

    Would you happen to know what could be causing this trouble?

  • I tried copying the ADC data to a global array and converting it to uint8_t (in the ADC callback function). In the while loop, I get the data and save it in the file. It worked this way. Somehow the byte_queue_read function is interfering with the SD card functionality.

  • 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.

Related