STM32 通过SPI模式实现SD卡 读卡器
时间: 2023-06-29 13:09:59 浏览: 96
实现SD卡读卡器的步骤如下:
1. 硬件连接:将SD卡读卡器的MISO、MOSI、SCK、CS(片选)引脚分别连接到STM32的SPI接口的MISO、MOSI、SCK、NSS(片选)引脚上。
2. 初始化SPI接口:初始化SPI接口,设置SPI模式、数据位数、时钟极性和相位等参数。
3. 初始化SD卡:通过SPI接口发送SD卡的初始化命令,等待SD卡响应,并设置SD卡的工作参数。
4. 读取数据:通过SPI接口发送读数据命令,读取数据。
具体代码实现如下:
```c
#include "stm32f4xx.h"
/* 定义SPI接口和引脚 */
#define SPIx_SPI SPI2
#define SPIx_CLK RCC_APB1Periph_SPI2
#define SPIx_IRQn SPI2_IRQn
#define SPIx_IRQHANDLER SPI2_IRQHandler
#define SPIx_SCK_PIN GPIO_Pin_13
#define SPIx_SCK_GPIO_PORT GPIOB
#define SPIx_SCK_GPIO_CLK RCC_AHB1Periph_GPIOB
#define SPIx_MISO_PIN GPIO_Pin_14
#define SPIx_MISO_GPIO_PORT GPIOB
#define SPIx_MISO_GPIO_CLK RCC_AHB1Periph_GPIOB
#define SPIx_MOSI_PIN GPIO_Pin_15
#define SPIx_MOSI_GPIO_PORT GPIOB
#define SPIx_MOSI_GPIO_CLK RCC_AHB1Periph_GPIOB
#define SPIx_CS_PIN GPIO_Pin_12
#define SPIx_CS_GPIO_PORT GPIOB
#define SPIx_CS_GPIO_CLK RCC_AHB1Periph_GPIOB
/* 定义SD卡命令 */
#define CMD0 0x40
#define CMD8 0x48
#define CMD9 0x49
#define CMD16 0x50
#define CMD17 0x51
#define CMD55 0x77
#define CMD58 0x7A
#define ACMD41 0x69
/* 定义SD卡响应 */
#define R1 0x01
#define R1_IDLE_STATE 0x01
#define R1_ILLEGAL_COMMAND 0x04
#define R1_CRC_ERROR 0x08
#define R1_ERASE_RESET 0x20
#define R1_IN_IDLE_STATE 0x80
/* SD卡初始化函数 */
uint8_t SD_Init(void)
{
uint8_t i, response, data[4];
/* 初始化SPI接口 */
SPI_InitTypeDef SPI_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(SPIx_SCK_GPIO_CLK | SPIx_MISO_GPIO_CLK | SPIx_MOSI_GPIO_CLK, ENABLE);
RCC_APB1PeriphClockCmd(SPIx_CLK, ENABLE);
GPIO_PinAFConfig(SPIx_SCK_GPIO_PORT, GPIO_PinSource13, GPIO_AF_SPI2);
GPIO_PinAFConfig(SPIx_MISO_GPIO_PORT, GPIO_PinSource14, GPIO_AF_SPI2);
GPIO_PinAFConfig(SPIx_MOSI_GPIO_PORT, GPIO_PinSource15, GPIO_AF_SPI2);
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_NOPULL;
GPIO_InitStructure.GPIO_Pin = SPIx_SCK_PIN;
GPIO_Init(SPIx_SCK_GPIO_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = SPIx_MISO_PIN;
GPIO_Init(SPIx_MISO_GPIO_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = SPIx_MOSI_PIN;
GPIO_Init(SPIx_MOSI_GPIO_PORT, &GPIO_InitStructure);
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(SPIx_SPI, &SPI_InitStructure);
SPI_Cmd(SPIx_SPI, ENABLE);
/* 等待SD卡上电 */
for(i = 0; i < 10; i++)
{
SPIx_WriteByte(0xFF);
}
/* 发送CMD0命令 */
response = SD_SendCommand(CMD0, 0x00000000, R1_IDLE_STATE);
if(response != R1_IDLE_STATE)
{
return 1;
}
/* 发送CMD8命令 */
response = SD_SendCommand(CMD8, 0x000001AA, R1_IDLE_STATE);
if(response == R1_IDLE_STATE)
{
for(i = 0; i < 4; i++)
{
data[i] = SPIx_WriteByte(0xFF);
}
if((data[2] != 0x01) || (data[3] != 0xAA))
{
return 1;
}
}
/* 发送ACMD41命令 */
while(1)
{
response = SD_SendCommand(CMD55, 0x00000000, R1_IDLE_STATE);
if(response != R1_IDLE_STATE)
{
return 1;
}
response = SD_SendCommand(ACMD41, 0x40000000, R1_IDLE_STATE);
if(response == R1_IDLE_STATE)
{
break;
}
}
/* 发送CMD58命令 */
response = SD_SendCommand(CMD58, 0x00000000, R1_IDLE_STATE);
if(response != R1_IDLE_STATE)
{
return 1;
}
for(i = 0; i < 4; i++)
{
data[i] = SPIx_WriteByte(0xFF);
}
return 0;
}
/* 发送SD卡命令 */
uint8_t SD_SendCommand(uint8_t cmd, uint32_t arg, uint8_t crc)
{
uint8_t response;
/* 发送命令 */
SPIx_WriteByte(cmd | 0x40);
SPIx_WriteByte((uint8_t)(arg >> 24));
SPIx_WriteByte((uint8_t)(arg >> 16));
SPIx_WriteByte((uint8_t)(arg >> 8));
SPIx_WriteByte((uint8_t)arg);
SPIx_WriteByte(crc);
/* 接收响应 */
response = SPIx_WriteByte(0xFF);
while(response == 0xFF)
{
response = SPIx_WriteByte(0xFF);
}
return response;
}
/* 读取SD卡数据 */
void SD_ReadData(uint32_t sector, uint8_t *buffer)
{
uint32_t i;
uint8_t response;
/* 发送CMD17命令 */
response = SD_SendCommand(CMD17, sector << 9, R1);
if(response != 0x00)
{
return;
}
/* 读取数据 */
for(i = 0; i < 512; i++)
{
buffer[i] = SPIx_WriteByte(0xFF);
}
/* 停止传输 */
SPIx_WriteByte(0xFF);
SPIx_WriteByte(0xFF);
}
/* SPI接口写入一个字节 */
uint8_t SPIx_WriteByte(uint8_t byte)
{
while(SPI_I2S_GetFlagStatus(SPIx_SPI, SPI_I2S_FLAG_TXE) == RESET);
SPI_I2S_SendData(SPIx_SPI, byte);
while(SPI_I2S_GetFlagStatus(SPIx_SPI, SPI_I2S_FLAG_RXNE) == RESET);
return SPI_I2S_ReceiveData(SPIx_SPI);
}
```
注:以上代码仅供参考,实际使用时需要根据具体情况进行调整。