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

unable to read the data from file using f_read in fatfs example of nrf52

Hi,

I am interfacing micro SD card with nrf52 using fatfs example in sdk 12.0. As I am beginner, first I wanted to perform operations like read, write, delete and some basic functions with files. I am able to write the data into the file,which was directly given in sample example. But while reading the contents from file, I am not getting the data.

Has anyone tried reading and printing the contents on terminal?

  • If you try to read what you just written you need to adjust the read/write pointer. As the documentation for f_read(..) states, it starts to read from the current position of the read/write pointer. After you written data to the file, the read/write pointer will be at the end of the data.

    You can set the read/write pointer with the f_lseek() function.

  • Hi Ole,

    First of all thanks for your comment and sorry that I am replying with delay. It helped me to understand the concept of writing and reading the file in one cycle. I was using the following code for write and read operation:

        NRF_LOG_INFO("Writing to file " FILE_NAME1 "\r\n");
    ff_result = f_open(&file, FILE_NAME1, FA_READ | FA_WRITE | FA_OPEN_APPEND);
    if (ff_result != FR_OK)
    {
        NRF_LOG_INFO("Unable to open or create file: " FILE_NAME1 ".\r\n");
        return;
    }
    uint8_t d[10] = {255,255,255,255,255,255,255,255,255,255};
    
    char buffer [50]={0};
    int n;
    n=sprintf (buffer,"%d %d %d %d %d %d %d %d %d %d\r",d[0],d[1],d[2],d[3],d[4],d[5],d[6],d[7],d[8],d[9]);
    NRF_LOG_INFO("string %d chars long\r\n",n);
    
    for(uint8_t i=0;i<1;i++)
    {
        ff_result = f_write(&file, buffer, n, (UINT *) &bytes_written);
        if (ff_result != FR_OK)
        {
            NRF_LOG_INFO("Write failed\r\n.");
        }
        else
        {
            NRF_LOG_INFO("%d bytes written.\r\n", bytes_written);
        }
    }
    (void) f_close(&file);
    
    //read content of the file
    char data[50]={0}; /* Line buffer */
    unsigned int ByteRead = 0;
    
    ff_result = f_open(&file, FILE_NAME1, FA_READ | FA_WRITE | FA_OPEN_APPEND);
    if (ff_result != FR_OK)
    {
        NRF_LOG_INFO("Unable to open or create file: " FILE_NAME1 ".\r\n");
        return;
    }
    
    uint16_t size = f_size(&file) ;
    NRF_LOG_INFO("size of the file in bytes = %d\r\n",size);
    
    ff_result = f_read(&file,data,50, &ByteRead);
    
    if (ff_result != FR_OK)
    {
        NRF_LOG_INFO("Unable to read or create file: " FILE_NAME1 ".\r\n");
        return;
    }
    else if(ff_result == FR_OK)
    {
      NRF_LOG_INFO("%d bytes read\r\n",ByteRead);
    }
    else
    {
      NRF_LOG_INFO("Error operation\r\n");
    }
    

    In above code I just changed the f_open instruction as

    ff_result = f_open(&file, FILE_NAME1, FA_READ );
    

    that is I removed FA_WRITE | FA_OPEN_APPEND . So it was reading the buffer. Is it write approach?

  • Hi Ole,

    I have another issue which is written over following link. Here

    Can you help me to resolve it?

  • .

    ff_result = f_open(&file, FILE_NAME1, FA_READ );
    

    is the same as

    ff_result = f_open(&file, FILE_NAME1, FA_READ | FA_OPEN_EXISTING);
    

    So you will only open existing files for read permission.

    It should work with

    ff_result = f_open(&file, FILE_NAME1, FA_READ | FA_WRITE | FA_OPEN_APPEND);
    

    though.

    I will take a look at the other question.

Related