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.
Previous article:Analysis of several delay methods in stm32CubeMX HAL library
Next article:stm32f405 HAL library serial port receiving error
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- CGD and Qorvo to jointly revolutionize motor control solutions
- CGD and Qorvo to jointly revolutionize motor control solutions
- Keysight Technologies FieldFox handheld analyzer with VDI spread spectrum module to achieve millimeter wave analysis function
- Infineon's PASCO2V15 XENSIV PAS CO2 5V Sensor Now Available at Mouser for Accurate CO2 Level Measurement
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- A new chapter in Great Wall Motors R&D: solid-state battery technology leads the future
- Naxin Micro provides full-scenario GaN driver IC solutions
- Interpreting Huawei’s new solid-state battery patent, will it challenge CATL in 2030?
- Are pure electric/plug-in hybrid vehicles going crazy? A Chinese company has launched the world's first -40℃ dischargeable hybrid battery that is not afraid of cold
- Work Summary
- How does a phase shifter shift phase?
- Ferrite beads and inductors
- CircuitBrains Deluxe Development Board
- Silicon Labs Development Kit Review – First Look
- SIMterix-Simplis~7~
- Is it because there is no system initialization function that causes the register version of the program PROTUES cannot be simulated?
- [GD32E231 DIY Contest] 2. Light up a digit of the digital tube
- [National Technology N32G457 Review] 2. Basic Engineering Evaluation and Template Engineering Construction
- pyb-L series low power mpy development board