hal_fatfs.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #include "hal_fatfs.h"
  2. #include "ff.h"
  3. #include "diskio_blkdev.h"
  4. #include "nrf_block_dev_sdc.h"
  5. #define FILE_NAME "IMU.TXT"
  6. #define TEST_STRING "SD card example."
  7. #define SDC_SCK_PIN SPI_SCK_PIN ///< SDC serial clock (SCK) pin.
  8. #define SDC_MOSI_PIN SPI_MOSI_PIN ///< SDC serial data in (DI) pin.
  9. #define SDC_MISO_PIN SPI_MISO_PIN ///< SDC serial data out (DO) pin.
  10. #define SDC_CS_PIN SPI_SS_PIN ///< SDC chip select (CS) pin.
  11. static FATFS fs;
  12. static DIR dir;
  13. static FILINFO fno;
  14. static FIL file;
  15. /**
  16. * @brief SDC block device definition
  17. * */
  18. NRF_BLOCK_DEV_SDC_DEFINE(
  19. m_block_dev_sdc,
  20. NRF_BLOCK_DEV_SDC_CONFIG(
  21. SDC_SECTOR_SIZE,
  22. APP_SDCARD_CONFIG(SDC_MOSI_PIN, SDC_MISO_PIN, SDC_SCK_PIN, SDC_CS_PIN)
  23. ),
  24. NFR_BLOCK_DEV_INFO_CONFIG("Nordic", "SDC", "1.00")
  25. );
  26. uint32_t FATFS_WriteString(char * p)
  27. {
  28. uint32_t bytes_written;
  29. if (f_write(&file, (char *)p, strlen(p), (UINT *) &bytes_written) == FR_OK){f_sync(&file); return bytes_written;} //success
  30. DEBUG_LOG("FATFS_WriteString fail");
  31. return 0; //fail
  32. }
  33. uint32_t FATFS_WriteBuff_8b(uint8_t * p,uint32_t len)
  34. {
  35. uint32_t bytes_written;
  36. if (f_write(&file, (uint8_t *)p, len, (UINT *) &bytes_written) == FR_OK){ return bytes_written;} //success
  37. else return 0; //fail
  38. }
  39. void FATFS_CloseFile(void)
  40. {
  41. // FRESULT ff_result;
  42. // ff_result = f_mount(NULL, "", 1);
  43. // if (ff_result)
  44. // {
  45. // DEBUG_LOG("Umount failed.\n");
  46. // }
  47. f_close(&file);
  48. }
  49. void FATFS_OpenFile(char * filename)
  50. {
  51. FRESULT ff_result;
  52. DEBUG_LOG("\r\n Listing directory: /\n");
  53. ff_result = f_opendir(&dir, "/");
  54. if (ff_result)
  55. {
  56. DEBUG_LOG("Directory listing failed!\n");
  57. return;
  58. }
  59. do
  60. {
  61. ff_result = f_readdir(&dir, &fno);
  62. if (ff_result != FR_OK)
  63. {
  64. DEBUG_LOG("Directory read failed.\n");
  65. return;
  66. }
  67. if (fno.fname[0])
  68. {
  69. if (fno.fattrib & AM_DIR)
  70. {
  71. DEBUG_LOG(" <DIR> %s\n",(uint32_t)fno.fname);
  72. }
  73. else
  74. {
  75. DEBUG_LOG("%9lu %s\n", fno.fsize, (uint32_t)fno.fname);
  76. }
  77. }
  78. }
  79. while (fno.fname[0]);
  80. DEBUG_LOG("\n");
  81. DEBUG_LOG("Writing to file %s ...\n",filename);
  82. ff_result = f_open(&file, filename, FA_READ | FA_WRITE | FA_CREATE_ALWAYS);
  83. if (ff_result != FR_OK)
  84. {
  85. DEBUG_LOG("Unable to open or create file:%s.\n",filename);
  86. return;
  87. }
  88. }
  89. void FATFS_Init(void)
  90. {
  91. FRESULT ff_result;
  92. DSTATUS disk_state = STA_NOINIT;
  93. DEBUG_LOG("FATFS example started.\n");
  94. // Initialize FATFS disk I/O interface by providing the block device.
  95. static diskio_blkdev_t drives[] =
  96. {
  97. DISKIO_BLOCKDEV_CONFIG(NRF_BLOCKDEV_BASE_ADDR(m_block_dev_sdc, block_dev), NULL)
  98. };
  99. diskio_blockdev_register(drives, ARRAY_SIZE(drives));
  100. DEBUG_LOG("Initializing disk 0 (SDC)...\n");
  101. for (uint32_t retries = 3; retries && disk_state; --retries)
  102. {
  103. disk_state = disk_initialize(0);
  104. }
  105. if (disk_state)
  106. {
  107. DEBUG_LOG("Disk initialization failed.\n");
  108. return;
  109. }
  110. uint32_t blocks_per_mb = (1024uL * 1024uL) / m_block_dev_sdc.block_dev.p_ops->geometry(&m_block_dev_sdc.block_dev)->blk_size;
  111. uint32_t capacity = m_block_dev_sdc.block_dev.p_ops->geometry(&m_block_dev_sdc.block_dev)->blk_count / blocks_per_mb;
  112. DEBUG_LOG("Capacity: %d MB\n", capacity);
  113. DEBUG_LOG("Mounting volume...\n");
  114. ff_result = f_mount(&fs, "", 1);
  115. if (ff_result)
  116. {
  117. // DEBUG_LOG("Mount failed.\n");
  118. DEBUG_LOG("Umounting volume...\n");
  119. ff_result = f_mount(NULL, "", 1);
  120. if (ff_result) DEBUG_LOG("Umount failed.\n");
  121. ff_result = f_mount(&fs, "", 1);
  122. if (ff_result) DEBUG_LOG("Mount failed.\n");
  123. }
  124. }