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

Writting to multiple files in SD Card using FATFS

Hi,

I have multiple sensor data that needs to be recorded in separate files. This data is being collected in parallel. Using the FATFS xample in SDK 15, I am able to write one sensor's data to the SD Card. But I am not able to open a second file within the same directory. I use the code below to initialise and write. I tried to chnag ethe variable for FILINFO and FIL  and also defined a new FILE_NAME to make a new file in the same directory but that doesnt work. Any pointers how to execute this will be really helpful. Thanks a lot!

	

static FATFS fs;
static DIR dir;
static FILINFO fno;
static FIL file;

uint32_t bytes_written;
FRESULT ff_result;
DSTATUS disk_state = STA_NOINIT;


int sd_init(){		

	
	
				NRF_BLOCK_DEV_SDC_DEFINE(
        m_block_dev_sdc,
        NRF_BLOCK_DEV_SDC_CONFIG(
                SDC_SECTOR_SIZE,
                APP_SDCARD_CONFIG(12,13,14,11)
         ),
         NFR_BLOCK_DEV_INFO_CONFIG("Nordic", "SDC", "1.00")
				);
	


				#define FILE_NAME   "NORDIC.TXT"
				#define TEST_STRING "SD card example.\r\n"
	


	// Initialize FATFS disk I/O interface by providing the block device.
    static diskio_blkdev_t drives[] =
    {
            DISKIO_BLOCKDEV_CONFIG(NRF_BLOCKDEV_BASE_ADDR(m_block_dev_sdc, block_dev), NULL)
    };

    diskio_blockdev_register(drives, ARRAY_SIZE(drives));

    NRF_LOG_INFO("Initializing disk 0 (SDC)...\r\n");
    for (uint32_t retries = 3; retries && disk_state; --retries)
    {
        disk_state = disk_initialize(0);
    }
    if (disk_state)
    {
        NRF_LOG_INFO("Disk initialization failed.\r\n");
        return 0;
    }
    
    uint32_t blocks_per_mb = (1024uL * 1024uL) / m_block_dev_sdc.block_dev.p_ops->geometry(&m_block_dev_sdc.block_dev)->blk_size;
    uint32_t capacity = m_block_dev_sdc.block_dev.p_ops->geometry(&m_block_dev_sdc.block_dev)->blk_count / blocks_per_mb;
    NRF_LOG_INFO("Capacity: %d MB\r\n", capacity);

    NRF_LOG_INFO("Mounting volume...\r\n");
    ff_result = f_mount(&fs, "", 1);
    if (ff_result)
    {
        NRF_LOG_INFO("Mount failed.\r\n");
        return 0;
    }

    NRF_LOG_INFO("\r\n Listing directory: /\r\n");
    ff_result = f_opendir(&dir, "/");
    if (ff_result)
    {
        NRF_LOG_INFO("Directory listing failed!\r\n");
        return 0;
    }
    
    do
    {
        ff_result = f_readdir(&dir, &fno);
        if (ff_result != FR_OK)
        {
            NRF_LOG_INFO("Directory read failed.");
            return 0;
        }
        
        if (fno.fname[0])
        {
            if (fno.fattrib & AM_DIR)
            {
                NRF_LOG_RAW_INFO("   <DIR>   %s\r\n",(uint32_t)fno.fname);
            }
            else
            {
                NRF_LOG_RAW_INFO("%9lu  %s\r\n", fno.fsize, (uint32_t)fno.fname);
            }
        }
    }
    while (fno.fname[0]);
    NRF_LOG_RAW_INFO("\r\n");
    
    NRF_LOG_INFO("Writing to file " FILE_NAME "...\r\n");
    ff_result = f_open(&file, FILE_NAME, FA_READ | FA_WRITE | FA_OPEN_APPEND);
    if (ff_result != FR_OK)
    {
        NRF_LOG_INFO("Unable to open or create file: " FILE_NAME ".\r\n");
        return 0;
    }
		 return 0;
	} 



	int Write(){
 		
 	// writiing in sd card
 			ff_result = f_open(&file, FILE_NAME, FA_READ | FA_WRITE | FA_OPEN_APPEND);
			NRF_LOG_INFO("Writing to file " FILE_NAME "...\r\n");
			print_f(ff_result);
		
		for (uint8_t d=0; d<20;d++){			// SIZE OF BUFFER	
		char dat_temp1[22]={0};

								sprintf(dat_temp1,"%d,%d,%d,%d",BS[d],F[d], R[d], M[d]);
//								f_write(&file, time_stamp_imp[d], sizeof(time_stamp_imp[11]), (UINT *) &bytes_written);
								f_write(&file, dat_temp1, 22, (UINT *) &bytes_written);
								f_write(&file, newline, 2, (UINT *) &bytes_written);

								}			
				

NRF_LOG_INFO( "Data in SD Card \n");		

 		
 		f_close(&file);
	NRF_LOG_INFO( "file close\n");					
  		return 0;								
// NRF_LOG_INFO( "Data Written in SD Card \n");										
 							
 	
 	}

Related