某10

懒得写,咕了

STM32H750 SDMMC FATFs 记录

SDMMC

打开SDMMC2外设,Mode设置为SD 4 bits Wide bus, 将does the board use uSD transceiver禁用,时钟设置为32MHz。

FATFS

FATFS选项卡勾选SD Card选项。这里需要注意:如果你的CODE_PAGE选择了非拉丁语言如中文,它将会生成代码页文件,该文件体积很大,STM32H750VB无法存储下来。会导致编译最后的链接失败。
进入FATFS的Platform Setting选项卡,在这里设置SD插入检测的引脚,将对应引脚GPIO设定为 Input 内部上拉,然后在这个页面选择该引脚即可。

测试代码

// Detect the SD card
uint8_t SD_State = MSD_ERROR;
while(SD_State != MSD_OK){
    SD_State = BSP_SD_Init();
    if( SD_State == MSD_ERROR_SD_NOT_PRESENT){
        printf("Waiting to insert SD card...\r\n");
        HAL_Delay(1000);
    }
    if( SD_State == MSD_ERROR) {
        printf("SD card error!\r\n");
        Error_Handler();
        HAL_Delay(1000);
    }
}
printf("SD card OK!\r\n");
// Read SD Card Info
HAL_SD_CardInfoTypeDef SDCardInfo1;
HAL_SD_GetCardInfo(&hsd2, &SDCardInfo1);
// Print SD Card Size
uint64_t SDSize = (uint64_t)SDCardInfo1.BlockSize * (uint64_t)SDCardInfo1.BlockNbr / 1024;
printf("SD Size: ");
if(SDSize < 1024){
    printf("%dKB\r\n",(int)SDSize);
}else if(SDSize/1024 < 1024){
    printf("%dMB\r\n",(int)(SDSize/1024));
}else if(SDSize/1024/1024 < 1024){
    printf("%dGB\r\n",(int)(SDSize/1024/1024));
}
// Mount SD Card
if(f_mount(&SDFatFS, (TCHAR const*)SDPath, 0) != FR_OK)
{
    /* FatFs Initialization Error */
    printf("FatFs mount failed!\r\n");
    Error_Handler();
} else {
    printf("FatFs mount success!\r\n");
    /* Create and Open a new text file object with write access */
    if(f_open(&SDFile, "STM32.TXT", FA_CREATE_ALWAYS | FA_WRITE) != FR_OK)
    {
        /* 'STM32.TXT' file Open for write Error */
        printf("Open file failed!\r\n");
        Error_Handler();
    }
    else
    {
        printf("Open file success!\r\n");
    }
}

评论卡