| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199 |
- #include "hard_sdio_sd.h"
- #include "board.h"
- #include "test.h"
- // 使用官方的SDMMC例程包 SD1 挂载在
- // PD27 D2 PD26 D3 PD21 CMD PD22 CLK PD18 D0 PD17 D1 PD28 CD
- // 修改时要修改初始化io 和时钟 在port文件内
- // 这里负责操作初始化IO 和 时钟的接口 注意在port文件填写对应的回调函数
- // SD_TEST 20260322 pass
- #ifdef SD_TEST
- #include "hpm_sdmmc_sd.h"
- #include "ff.h"
- #include "diskio.h"
- FATFS s_sd_disk;
- FIL s_file;
- DIR s_dir;
- FRESULT fatfs_result;
- BYTE work[FF_MAX_SS];
- const TCHAR driver_num_buf[4] = { DEV_SD + '0', ':', '/', '\0' };
- #define TEST_DIR_NAME "hpmicro_sd_test_dir0"
- static FRESULT sd_write_file(void)
- {
- FRESULT fresult = f_open(&s_file, "readme.txt", FA_WRITE | FA_CREATE_ALWAYS);
- if (fresult != FR_OK) {
- printf("Create new file failed, cause: %d\n", show_error_string(fresult));
- } else {
- printf("Create new file successfully, status=%d\n", fresult);
- }
- char hello_str[] = "Hello, this is SD card FATFS demo\n";
- UINT byte_written;
- fresult = f_write(&s_file, hello_str, sizeof(hello_str), &byte_written);
- if (fresult != FR_OK) {
- printf("Write file failed, cause: %s\n", show_error_string(fresult));
- } else {
- printf("Write file operation is successfully\n");
- }
- f_close(&s_file);
- return fresult;
- }
- static FRESULT sd_read_file(void)
- {
- FRESULT fresult = f_open(&s_file, "readme.txt", FA_READ);
- if (fresult != FR_OK) {
- printf("Open file failed, cause: %s\n", show_error_string(fresult));
- } else {
- printf("Open file successfully\n");
- }
- if (fresult != FR_OK) {
- return fresult;
- }
- TCHAR str[100];
- f_gets(str, sizeof(str), &s_file);
- printf("%s\n", str);
- f_close(&s_file);
- return fresult;
- }
- const char *show_error_string(FRESULT fresult)
- {
- const char *result_str;
- switch (fresult) {
- case FR_OK:
- result_str = "succeeded";
- break;
- case FR_DISK_ERR:
- result_str = "A hard error occurred in the low level disk I/O level";
- break;
- case FR_INT_ERR:
- result_str = "Assertion failed";
- break;
- case FR_NOT_READY:
- result_str = "The physical drive cannot work";
- break;
- case FR_NO_FILE:
- result_str = "Could not find the file";
- break;
- case FR_NO_PATH:
- result_str = "Could not find the path";
- break;
- case FR_INVALID_NAME:
- result_str = "Tha path name format is invalid";
- break;
- case FR_DENIED:
- result_str = "Access denied due to prohibited access or directory full";
- break;
- case FR_EXIST:
- result_str = "Access denied due to prohibited access";
- break;
- case FR_INVALID_OBJECT:
- result_str = "The file/directory object is invalid";
- break;
- case FR_WRITE_PROTECTED:
- result_str = "The physical drive is write protected";
- break;
- case FR_INVALID_DRIVE:
- result_str = "The logical driver number is invalid";
- break;
- case FR_NOT_ENABLED:
- result_str = "The volume has no work area";
- break;
- case FR_NO_FILESYSTEM:
- result_str = "There is no valid FAT volume";
- break;
- case FR_MKFS_ABORTED:
- result_str = "THe f_mkfs() aborted due to any problem";
- break;
- case FR_TIMEOUT:
- result_str = "Could not get a grant to access the volume within defined period";
- break;
- case FR_LOCKED:
- result_str = "The operation is rejected according to the file sharing policy";
- break;
- case FR_NOT_ENOUGH_CORE:
- result_str = "LFN working buffer could not be allocated";
- break;
- case FR_TOO_MANY_OPEN_FILES:
- result_str = "Number of open files > FF_FS_LOCK";
- break;
- case FR_INVALID_PARAMETER:
- result_str = "Given parameter is invalid";
- break;
- default:
- result_str = "Unknown error";
- break;
- }
- return result_str;
- }
- static FRESULT sd_mount_fs(void)
- {
- FRESULT fresult = f_mount(&s_sd_disk, driver_num_buf, 1);
- if (fresult == FR_OK) {
- printf("SD card has been mounted successfully\n");
- } else {
- printf("Failed to mount SD card, cause: %s\n", show_error_string(fresult));
- }
- fresult = f_chdrive(driver_num_buf);
- return fresult;
- }
- static FRESULT sd_mkfs(void)
- {
- printf("Formatting the SD card, depending on the SD card capacity, the formatting process may take a long time\n");
- FRESULT fresult = f_mkfs(driver_num_buf, NULL, work, sizeof(work));
- if (fresult != FR_OK) {
- printf("Making File system failed, cause: %s\n", show_error_string(fresult));
- } else {
- printf("Making file system is successful\n");
- }
- return fresult;
- }
- void SD1_test(void)
- {
- bool need_init_filesystem = true;
- board_init();
-
- /* Before doing FATFS operation, ensure the SD card is present */
- DSTATUS dstatus = disk_status(DEV_SD);
- if (dstatus == STA_NODISK) {
- printf("No disk in the SD slot, please insert an SD card...\n");
- do {
- dstatus = disk_status(DEV_SD);
- } while (dstatus == STA_NODISK);
- board_delay_ms(100);
- printf("Detected SD card, re-initialize the filesystem...\n");
- need_init_filesystem = true;
- }
- dstatus = disk_initialize(DEV_SD);
- if (dstatus != RES_OK) {
- printf("Failed to initialize SD disk\n");
- }
- if (need_init_filesystem) {
- fatfs_result = sd_mount_fs();
- if (fatfs_result == FR_NO_FILESYSTEM) {
- printf("There is no File system available, making file system...\n");
- fatfs_result = sd_mkfs();
- if (fatfs_result != FR_OK) {
- printf("Failed to make filesystem, cause:%s\n", show_error_string(fatfs_result));
- } else {
- need_init_filesystem = false;
- }
- }
- }
- }
-
- #endif
|