m61_sd卡模块的使用

[复制链接]
查看1717 | 回复23 | 2024-2-5 22:37:17 | 显示全部楼层 |阅读模式
搜了下看 sdk里面有demo但是没文档。

稍微整理了下。
里面默认是一个 读写文件的demo
路径在aithinker_Ai-M6X_SDK\examples\fatfs\main.c
核心头文件

//fatfs
#include "fatfs_diskio_register.h"
#include "ff.h"


board_sdh_gpio_init();
直接抄就行,修改引脚的函数
引脚定义。
1.png
2.png


  1. #include "bflb_mtimer.h"
  2. #include "board.h"
  3. #include "fatfs_diskio_register.h"
  4. #include "ff.h"

  5. #define DBG_TAG "MAIN"
  6. #include "log.h"

  7. FATFS fs;
  8. __attribute((aligned(8))) static uint32_t workbuf[4096];

  9. MKFS_PARM fs_para = {
  10.     .fmt = FM_FAT32,     /* Format option (FM_FAT, FM_FAT32, FM_EXFAT and FM_SFD) */
  11.     .n_fat = 1,          /* Number of FATs */
  12.     .align = 0,          /* Data area alignment (sector) */
  13.     .n_root = 1,         /* Number of root directory entries */
  14.     .au_size = 512 * 32, /* Cluster size (byte) */
  15. };

  16. void filesystem_init(void)
  17. {
  18.     FRESULT ret;

  19.     board_sdh_gpio_init();

  20.     fatfs_sdh_driver_register();

  21.     ret = f_mount(&fs, "/sd", 1);
  22.     // ret = FR_NO_FILESYSTEM;

  23.     if (ret == FR_NO_FILESYSTEM) {
  24.         LOG_W("No filesystem yet, try to be formatted...\r\n");

  25.         ret = f_mkfs("/sd", &fs_para, workbuf, sizeof(workbuf));

  26.         if (ret != FR_OK) {
  27.             LOG_F("fail to make filesystem %d\r\n", ret);
  28.             _CALL_ERROR();
  29.         }

  30.         if (ret == FR_OK) {
  31.             LOG_I("done with formatting.\r\n");
  32.             LOG_I("first start to unmount.\r\n");
  33.             ret = f_mount(NULL, "/sd", 1);
  34.             LOG_I("then start to remount.\r\n");
  35.         }
  36.     } else if (ret != FR_OK) {
  37.         LOG_F("fail to mount filesystem,error= %d\r\n", ret);
  38.         LOG_F("SD card might fail to initialise.\r\n");
  39.         _CALL_ERROR();
  40.     } else {
  41.         LOG_D("Succeed to mount filesystem\r\n");
  42.     }

  43.     if (ret == FR_OK) {
  44.         LOG_I("FileSystem cluster size:%d-sectors (%d-Byte)\r\n", fs.csize, fs.csize * 512);
  45.     }
  46. }

  47. #define SDU_DATA_CHECK 1

  48. char test_data[] =
  49.     "I've been reading books of old \r\n\
  50.     The legends and the myths \r\n\
  51.     Achilles and his gold \r\n\
  52.     Hercules and his gifts \r\n\
  53.     Spiderman's control \r\n\
  54.     And Batman with his fists\r\n\
  55.     And clearly I don't see myself upon that list\r\n\
  56.     But she said, where'd you wanna go?\r\n\
  57.     How much you wanna risk?\r\n\
  58.     I'm not looking for somebody\r\n\
  59.     With some superhuman gifts\r\n\
  60.     Some superhero\r\n\
  61.     Some fairytale bliss\r\n\
  62.     Just something I can turn to\r\n\
  63.     Somebody I can kiss\r\n\
  64.     I want something just like this\r\n\r\n";

  65. BYTE RW_Buffer[32 * 1024] = { 0 };
  66. #if SDU_DATA_CHECK
  67. BYTE Check_Buffer[sizeof(RW_Buffer)] = { 0 };
  68. #endif

  69. void fatfs_write_read_test()
  70. {
  71.     FRESULT ret;
  72.     FIL fnew;
  73.     UINT fnum;

  74.     uint32_t time_node, i, j;

  75.     /* full test data to buff */
  76.     for (uint32_t size = 0; size < (sizeof(RW_Buffer) - sizeof(test_data)); size += sizeof(test_data)) {
  77.         memcpy(&RW_Buffer[size], test_data, sizeof(test_data));
  78. #if SDU_DATA_CHECK
  79.         memcpy(&Check_Buffer[size], test_data, sizeof(test_data));
  80. #endif
  81.     }

  82.     /* write test */
  83.     LOG_I("\r\n******************** be about to write test... **********************\r\n");
  84.     ret = f_open(&fnew, "/sd/test_file.txt", FA_CREATE_ALWAYS | FA_WRITE);
  85.     if (ret == FR_OK) {
  86.         time_node = (uint32_t)bflb_mtimer_get_time_ms();
  87.         /*write into file*/
  88.         ret = f_write(&fnew, RW_Buffer, 1024, &fnum);
  89.         for (i = 0; i < 1024; i++) {
  90.             ret = f_write(&fnew, RW_Buffer, sizeof(RW_Buffer), &fnum);
  91.             if (ret) {
  92.                 break;
  93.             }
  94.         }

  95.         /* close file */
  96.         ret |= f_close(&fnew);
  97.         /* get time */
  98.         time_node = (uint32_t)bflb_mtimer_get_time_ms() - time_node;

  99.         if (ret == FR_OK) {
  100.             LOG_I("Write Test Succeed! \r\n");
  101.             LOG_I("Single data size:%d Byte, Write the number:%d, Total size:%d KB\r\n", sizeof(RW_Buffer), i, sizeof(RW_Buffer) * i >> 10);
  102.             LOG_I("Time:%dms, Write Speed:%d KB/s \r\n", time_node, ((sizeof(RW_Buffer) * i) >> 10) * 1000 / time_node);
  103.         } else {
  104.             LOG_F("Fail to write files(%d) num:%d\n", ret, i);
  105.             return;
  106.         }
  107.     } else {
  108.         LOG_F("Fail to open or create files: %d.\r\n", ret);
  109.         return;
  110.     }

  111.     /* read test */
  112.     LOG_I("\r\n******************** be about to read test... **********************\r\n");
  113.     ret = f_open(&fnew, "/sd/test_file.txt", FA_OPEN_EXISTING | FA_READ);
  114.     if (ret == FR_OK) {
  115.         time_node = (uint32_t)bflb_mtimer_get_time_ms();

  116.         ret = f_read(&fnew, RW_Buffer, 1024, &fnum);
  117.         for (i = 0; i < 1024; i++) {
  118.             ret = f_read(&fnew, RW_Buffer, sizeof(RW_Buffer), &fnum);
  119.             if (ret) {
  120.                 break;
  121.             }
  122.         }
  123.         /* close file */
  124.         ret |= f_close(&fnew);
  125.         /* get time */
  126.         time_node = (uint32_t)bflb_mtimer_get_time_ms() - time_node;

  127.         if (ret == FR_OK) {
  128.             LOG_I("Read Test Succeed! \r\n");
  129.             LOG_I("Single data size:%dByte, Read the number:%d, Total size:%d KB\r\n", sizeof(RW_Buffer), i, sizeof(RW_Buffer) * i >> 10);
  130.             LOG_I("Time:%dms, Read Speed:%d KB/s \r\n", time_node, ((sizeof(RW_Buffer) * i) >> 10) * 1000 / time_node);
  131.         } else {
  132.             LOG_F("Fail to read file: (%d), num:%d\n", ret, i);
  133.             return;
  134.         }
  135.     } else {
  136.         LOG_F("Fail to open files.\r\n");
  137.         return;
  138.     }

  139.     /* check data */
  140. #if SDU_DATA_CHECK
  141.     LOG_I("\r\n******************** be about to check test... **********************\r\n");
  142.     ret = f_open(&fnew, "/sd/test_file.txt", FA_OPEN_EXISTING | FA_READ);
  143.     if (ret == FR_OK) {
  144.         ret = f_read(&fnew, RW_Buffer, 1024, &fnum);
  145.         for (i = 0; i < 1024; i++) {
  146.             ret = f_read(&fnew, RW_Buffer, sizeof(RW_Buffer), &fnum);
  147.             if (ret) {
  148.                 break;
  149.             }
  150.             for (j = 0; j < sizeof(RW_Buffer); j++) {
  151.                 if (RW_Buffer[j] != Check_Buffer[j]) {
  152.                     break;
  153.                 }
  154.             }
  155.             if (j < sizeof(RW_Buffer)) {
  156.                 break;
  157.             }
  158.         }
  159.         /* close file */
  160.         ret |= f_close(&fnew);

  161.         if (ret == FR_OK) {
  162.             if (i < 1024 || j < sizeof(RW_Buffer)) {
  163.                 LOG_I("Check Test Error! \r\n");
  164.                 LOG_I("Data Error!  Num:%d/1024, Byte:%d/%d", i, j, sizeof(RW_Buffer));
  165.             } else {
  166.                 LOG_I("Check Test Succeed! \r\n");
  167.                 LOG_I("All Data Is Good! \r\n");
  168.             }

  169.         } else {
  170.             LOG_F("Fail to read file: (%d), num:%d\n", ret, i);
  171.             return;
  172.         }
  173.     } else {
  174.         LOG_F("Fail to open files.\r\n");
  175.         return;
  176.     }
  177. #endif
  178. }

  179. int main(void)
  180. {
  181.     board_init();

  182.     filesystem_init();

  183.     fatfs_write_read_test();

  184. #if defined(CONFIG_NEWLIB) && CONFIG_NEWLIB && defined(CONFIG_NEWLIB_FATFS) && CONFIG_NEWLIB_FATFS
  185.     FILE *fp;
  186.     fp = fopen("/sd/hellotest.txt", "w+");
  187.     fprintf(fp, "hello world\r\n");
  188.     fclose(fp);
  189. #endif

  190.     while (1) {
  191.         bflb_mtimer_delay_ms(200);
  192.     }
  193. }
复制代码


回复

使用道具 举报

noonezero | 2024-2-6 08:02:22 | 显示全部楼层
回复

使用道具 举报

干簧管 | 2024-2-6 08:39:06 | 显示全部楼层
赞👍
回复

使用道具 举报

lazy | 2024-2-6 08:49:27 | 显示全部楼层
回复

使用道具 举报

WT_0213 | 2024-2-6 08:57:45 | 显示全部楼层
回复

使用道具 举报

wukong50 | 2024-2-6 08:59:26 | 显示全部楼层
回复

使用道具 举报

WT_0213 | 2024-2-6 09:46:42 | 显示全部楼层
更详细的操作可以参考小安派 cam 那个里面操作sd卡的逻辑比较多一些
回复 支持 反对

使用道具 举报

timo | 2024-2-6 11:26:13 | 显示全部楼层
回复

使用道具 举报

wukong50 | 2024-2-7 07:39:16 | 显示全部楼层
回复

使用道具 举报

bzhou830 | 2024-2-7 15:01:20 | 显示全部楼层
选择去发光,而不是被照亮
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则