STM32F103 SDIO Sdcard驱动以及例程的问题

最近在看STM32F103相关的例程及驱动。在网上下载了一些官方的驱动例子来学习。

发现有个问题,卡的容量总是识别不对。具体的表现是:

8G的卡读出来的数值只有3.4G左右,按理来说读出来应该是7.5G以上的。

后来查询了一些资料发现,

typedef struct
{
  SD_CSD SD_csd;
  SD_CID SD_cid;
  uint32_t CardCapacity;  /*!< Card Capacity */
  uint32_t CardBlockSize; /*!< Card Block Size */
  uint16_t RCA;
  uint8_t CardType;
} SD_CardInfo;

上面的定义其实出错了,CardCapacity只能显示到最大4G。
那么把uint32_t 改成 uint64_t 后呢?实测下来也不行…… 还是显示只有3.4G左右。

后来深入研究Cardcapacity的计算。发现其实仅仅是一个数位转换的问题。

  {
    /*!< Byte 7 */
    tmp = (uint8_t)(CSD_Tab[1] & 0x000000FF);
    cardinfo->SD_csd.DeviceSize = (tmp & 0x3F) << 16;

    /*!< Byte 8 */
    tmp = (uint8_t)((CSD_Tab[2] & 0xFF000000) >> 24);

    cardinfo->SD_csd.DeviceSize |= (tmp << 8);

    /*!< Byte 9 */
    tmp =
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是基于STM32F103SDIO通信例程: 首先,需要在STM32CubeMX中配置SDIO和GPIO引脚。SDIO引脚需要配置为复用功能,GPIO引脚需要配置为推挽输出,具体配置如下图所示: ![SDIO配置示意图](https://i.loli.net/2021/01/27/Pc7fWqg5rL4GZ6M.png) 然后生成代码并导入到Keil或者其他开发环境中。在代码中需要引入以下头文件: ```c #include "stm32f1xx_hal.h" #include "stm32f1xx_hal_gpio.h" #include "stm32f1xx_hal_sdio.h" ``` 在main函数中初始化SDIO: ```c /* SDIO init function */ MX_SDIO_SD_Init(); ``` 其中,MX_SDIO_SD_Init()是由STM32CubeMX自动生成的SDIO初始化函数。 接下来,就可以进行SDIO通信了。以下是一个简单的示例程序,实现了在SD卡上创建一个文件,并写入一些数据: ```c #include "main.h" #include "stm32f1xx_hal.h" #include "stm32f1xx_hal_gpio.h" #include "stm32f1xx_hal_sdio.h" /* SDIO handle */ SD_HandleTypeDef hsd; /* Buffer for data */ uint8_t buffer[512]; int main(void) { /* MCU initialization */ HAL_Init(); /* System clock initialization */ SystemClock_Config(); /* SDIO init function */ MX_SDIO_SD_Init(); /* SD card initialization */ if (HAL_SD_Init(&hsd) != HAL_OK) { /* Initialization error */ Error_Handler(); } /* Enable wide bus operation */ if (HAL_SD_ConfigWideBusOperation(&hsd, SDIO_BUS_WIDE_4B) != HAL_OK) { /* Configuration error */ Error_Handler(); } /* Create a file on the SD card */ FIL file; FRESULT res = f_open(&file, "test.txt", FA_CREATE_ALWAYS | FA_WRITE); if (res != FR_OK) { /* File creation error */ Error_Handler(); } /* Write some data to the file */ for (int i = 0; i < 512; i++) { buffer[i] = i; } UINT bytes_written; res = f_write(&file, buffer, sizeof(buffer), &bytes_written); if (res != FR_OK || bytes_written != sizeof(buffer)) { /* Write error */ Error_Handler(); } /* Close the file */ f_close(&file); while (1) { /* Infinite loop */ } } /* SDIO init function */ void MX_SDIO_SD_Init(void) { /* SDIO GPIO Configuration */ GPIO_InitTypeDef GPIO_InitStruct = {0}; /* GPIO Ports Clock Enable */ __HAL_RCC_GPIOC_CLK_ENABLE(); __HAL_RCC_GPIOD_CLK_ENABLE(); __HAL_RCC_GPIOB_CLK_ENABLE(); /* Configure SDIO D0~D3, CLK and CMD pins */ GPIO_InitStruct.Pin = GPIO_PIN_8 | GPIO_PIN_9 | GPIO_PIN_10 | GPIO_PIN_11 | GPIO_PIN_12; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); GPIO_InitStruct.Pin = GPIO_PIN_2; HAL_GPIO_Init(GPIOD, &GPIO_InitStruct); GPIO_InitStruct.Pin = GPIO_PIN_15; HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); /* SDIO peripheral clock enable */ __HAL_RCC_SDIO_CLK_ENABLE(); /* SDIO interrupt Init */ HAL_NVIC_SetPriority(SDIO_IRQn, 0, 0); HAL_NVIC_EnableIRQ(SDIO_IRQn); /* SDIO DMA Init */ /* DMA controller clock enable */ __HAL_RCC_DMA2_CLK_ENABLE(); /* DMA interrupt init */ /* DMA2_Channel4_5_IRQn interrupt configuration */ HAL_NVIC_SetPriority(DMA2_Channel4_5_IRQn, 0, 0); HAL_NVIC_EnableIRQ(DMA2_Channel4_5_IRQn); } ``` 需要注意的是,SDIO通信需要使用FATFS文件系统库。如果还没有使用过FATFS,需要先进行FATFS的初始化和配置。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值