123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- #include "hal_fatfs.h"
- #include "ff.h"
- #include "diskio_blkdev.h"
- #include "nrf_block_dev_sdc.h"
- #define FILE_NAME "IMU.TXT"
- #define TEST_STRING "SD card example."
- #define SDC_SCK_PIN SPI_SCK_PIN
- #define SDC_MOSI_PIN SPI_MOSI_PIN
- #define SDC_MISO_PIN SPI_MISO_PIN
- #define SDC_CS_PIN SPI_SS_PIN
- static FATFS fs;
- static DIR dir;
- static FILINFO fno;
- static FIL file;
-
- NRF_BLOCK_DEV_SDC_DEFINE(
- m_block_dev_sdc,
- NRF_BLOCK_DEV_SDC_CONFIG(
- SDC_SECTOR_SIZE,
- APP_SDCARD_CONFIG(SDC_MOSI_PIN, SDC_MISO_PIN, SDC_SCK_PIN, SDC_CS_PIN)
- ),
- NFR_BLOCK_DEV_INFO_CONFIG("Nordic", "SDC", "1.00")
- );
- uint32_t FATFS_WriteString(char * p)
- {
- uint32_t bytes_written;
-
- if (f_write(&file, (char *)p, strlen(p), (UINT *) &bytes_written) == FR_OK){f_sync(&file); return bytes_written;}
- DEBUG_LOG("FATFS_WriteString fail");
- return 0;
- }
- uint32_t FATFS_WriteBuff_8b(uint8_t * p,uint32_t len)
- {
- uint32_t bytes_written;
-
- if (f_write(&file, (uint8_t *)p, len, (UINT *) &bytes_written) == FR_OK){ return bytes_written;}
- else return 0;
- }
- void FATFS_CloseFile(void)
- {
- f_close(&file);
- }
- void FATFS_OpenFile(char * filename)
- {
- FRESULT ff_result;
- DEBUG_LOG("\r\n Listing directory: /\n");
- ff_result = f_opendir(&dir, "/");
- if (ff_result)
- {
- DEBUG_LOG("Directory listing failed!\n");
- return;
- }
- do
- {
- ff_result = f_readdir(&dir, &fno);
- if (ff_result != FR_OK)
- {
- DEBUG_LOG("Directory read failed.\n");
- return;
- }
- if (fno.fname[0])
- {
- if (fno.fattrib & AM_DIR)
- {
- DEBUG_LOG(" <DIR> %s\n",(uint32_t)fno.fname);
- }
- else
- {
- DEBUG_LOG("%9lu %s\n", fno.fsize, (uint32_t)fno.fname);
- }
- }
- }
- while (fno.fname[0]);
- DEBUG_LOG("\n");
- DEBUG_LOG("Writing to file %s ...\n",filename);
- ff_result = f_open(&file, filename, FA_READ | FA_WRITE | FA_CREATE_ALWAYS);
- if (ff_result != FR_OK)
- {
- DEBUG_LOG("Unable to open or create file:%s.\n",filename);
- return;
- }
- }
- void FATFS_Init(void)
- {
- FRESULT ff_result;
-
- DSTATUS disk_state = STA_NOINIT;
- DEBUG_LOG("FATFS example started.\n");
-
- 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));
- DEBUG_LOG("Initializing disk 0 (SDC)...\n");
- for (uint32_t retries = 3; retries && disk_state; --retries)
- {
- disk_state = disk_initialize(0);
- }
- if (disk_state)
- {
- DEBUG_LOG("Disk initialization failed.\n");
- return;
- }
- 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;
- DEBUG_LOG("Capacity: %d MB\n", capacity);
- DEBUG_LOG("Mounting volume...\n");
- ff_result = f_mount(&fs, "", 1);
- if (ff_result)
- {
- DEBUG_LOG("Umounting volume...\n");
- ff_result = f_mount(NULL, "", 1);
- if (ff_result) DEBUG_LOG("Umount failed.\n");
- ff_result = f_mount(&fs, "", 1);
- if (ff_result) DEBUG_LOG("Mount failed.\n");
- }
- }
|