帮我写一段stm32 sd卡写入的驱动 
时间: 2023-04-04 15:01:40 浏览: 41
好的,以下是一段基于STM32的SD卡写入驱动的代码:
#include "stm32f4xx.h"
#include "stm32f4xx_gpio.h"
#include "stm32f4xx_rcc.h"
#include "stm32f4xx_sdio.h"
#define SDIO_INIT_CLK_DIV x76 // SDIO初始化时的时钟分频系数
#define SDIO_TRANSFER_CLK_DIV x02 // SDIO传输时的时钟分频系数
void SDIO_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC | RCC_AHB1Periph_GPIOD | RCC_AHB1Periph_GPIOB, ENABLE);
// PC8-11: SDIO_D-D3
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource8, GPIO_AF_SDIO);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource9, GPIO_AF_SDIO);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource10, GPIO_AF_SDIO);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource11, GPIO_AF_SDIO);
// PD2: SDIO_CMD
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOD, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOD, GPIO_PinSource2, GPIO_AF_SDIO);
// PB15: SDIO_CK
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource15, GPIO_AF_SDIO);
}
void SDIO_Config(void)
{
SDIO_InitTypeDef SDIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SDIO, ENABLE);
SDIO_DeInit();
SDIO_InitStructure.SDIO_ClockDiv = SDIO_INIT_CLK_DIV;
SDIO_InitStructure.SDIO_ClockEdge = SDIO_ClockEdge_Rising;
SDIO_InitStructure.SDIO_ClockBypass = SDIO_ClockBypass_Disable;
SDIO_InitStructure.SDIO_ClockPowerSave = SDIO_ClockPowerSave_Disable;
SDIO_InitStructure.SDIO_BusWide = SDIO_BusWide_1b;
SDIO_InitStructure.SDIO_HardwareFlowControl = SDIO_HardwareFlowControl_Disable;
SDIO_Init(&SDIO_InitStructure);
SDIO_SetPowerState(SDIO_PowerState_ON);
SDIO_ClockCmd(ENABLE);
SDIO_InitStructure.SDIO_ClockDiv = SDIO_TRANSFER_CLK_DIV;
SDIO_Init(&SDIO_InitStructure);
}
void SDIO_WriteBlock(uint8_t *buf, uint32_t addr, uint16_t blocksize)
{
SDIO_DataInitTypeDef SDIO_DataInitStructure;
SDIO_DataInitStructure.SDIO_DataTimeOut = SD_DATATIMEOUT;
SDIO_DataInitStructure.SDIO_DataLength = blocksize;
SDIO_DataInitStructure.SDIO_DataBlockSize = SDIO_DataBlockSize_512b;
SDIO_DataInitStructure.SDIO_TransferDir = SDIO_TransferDir_ToCard;
SDIO_DataInitStructure.SDIO_TransferMode = SDIO_TransferMode_Block;
SDIO_DataInitStructure.SDIO_DPSM = SDIO_DPSM_Enable;
SDIO_ConfigData(&SDIO_DataInitStructure);
SDIO_SendCommand(SDIO_CMD_SET_BLOCKLEN, blocksize, SDIO_Response_Short);
SDIO_SendCommand(SDIO_CMD_WRITE_SINGLE_BLOCK, addr, SDIO_Response_Short);
while (SDIO_GetFlagStatus(SDIO_FLAG_TXFIFOHE) == RESET);
for (int i = ; i < blocksize; i += 4)
{
while (SDIO_GetFlagStatus(SDIO_FLAG_TXFIFOHE) == RESET);
SDIO_WriteData(*((uint32_t *)buf));
buf += 4;
}
while (SDIO_GetFlagStatus(SDIO_FLAG_TXC) == RESET);
}
int main(void)
{
uint8_t buf[512];
uint32_t addr = x00000000;
uint16_t blocksize = 512;
SDIO_GPIO_Config();
SDIO_Config();
// 将buf中的数据写入SD卡
SDIO_WriteBlock(buf, addr, blocksize);
while (1);
return ;
}
相关推荐


















