STM32F401 SDIO HAL driver initializes SD card

Publisher:幸福之星Latest update time:2018-07-19 Source: eefocusKeywords:STM32F401 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

The HAL part of STM32F4xx already provides the SD driver, and the IO PINs during initialization are placed in HAL_SD_MspInit.


void HAL_SD_MspInit(SD_HandleTypeDef *hsd)

{

GPIO_InitTypeDef  GPIO_InitStruct;

 

__HAL_RCC_GPIOC_CLK_ENABLE();

__HAL_RCC_GPIOD_CLK_ENABLE();

 

GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;

GPIO_InitStruct.Pull      = GPIO_PULLUP;

GPIO_InitStruct.Speed     = GPIO_SPEED_FAST;

GPIO_InitStruct.Alternate = GPIO_AF12_SDIO;

 

/*SDIO GPIO Configuration   

PC8     ------> SDIO_D0

PC9     ------> SDIO_D1

PC10     ------> SDIO_D2

PC11     ------> SDIO_D3

PC12     ------> SDIO_CK

PD2     ------> SDIO_CMD

*/

GPIO_InitStruct.Pin = GPIO_PIN_8 | GPIO_PIN_9 | GPIO_PIN_10 | GPIO_PIN_11 | GPIO_PIN_12;    

HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);

GPIO_InitStruct.Pin = GPIO_PIN_2;    

HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);

 

__HAL_RCC_SDIO_CLK_ENABLE(); 

__SDIO_CLK_ENABLE();

 

hsd->Instance = SDIO;

hsd->Init.ClockEdge           = SDIO_CLOCK_EDGE_RISING;

hsd->Init.ClockBypass         = SDIO_CLOCK_BYPASS_DISABLE;

hsd->Init.ClockPowerSave      = SDIO_CLOCK_POWER_SAVE_DISABLE;

hsd->Init.BusWide             = SDIO_BUS_WIDE_1B;

hsd->Init.HardwareFlowControl = SDIO_HARDWARE_FLOW_CONTROL_DISABLE;

hsd->Init.ClockDiv            = SDIO_TRANSFER_CLK_DIV;

}


Define two variables

SD_HandleTypeDef hsd;

HAL_SD_CardInfoTypedef SDCardInfo;


The relevant code for initializing the SD card in the main function:

__IO HAL_SD_ErrorTypedef errorstate = SD_OK;

errorstate = HAL_SD_Init(&hsd, &SDCardInfo);

if(SD_OK == errorstate)

{

errorstate = HAL_SD_WideBusOperation_Config(&hsd, SDIO_BUS_WIDE_4B);

if(SD_OK != errorstate)

{

while(1);

}

....

}

The HAL_SD_Init() function will first call HAL_SD_MspInit() to initialize PIN and clk, etc.


It should be noted that the bus width can only be set to 1 bit during initialization, and then


HAL_SD_WideBusOperation_Config(&hsd, SDIO_BUS_WIDE_4B) is used to switch to 4-bit mode. It is invalid to directly set it to 4 bits during initialization.


After completing the above work, you can use HAL_SD_ReadBlocks() and HAL_SD_WriteBlocks() to complete block read and write operations. If FATFS is needed, you can refer to other examples.


Keywords:STM32F401 Reference address:STM32F401 SDIO HAL driver initializes SD card

Previous article:Analysis of several delay methods in stm32CubeMX HAL library
Next article:stm32f405 HAL library serial port receiving error

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号