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

Children
No Data
Related