stm32F103RCT6 SD卡 SPI 通信 程序
时间: 2023-05-29 11:03:18 浏览: 321
以下是一个简单的 STM32F103RCT6 与 SD 卡之间进行 SPI 通信的程序示例:
```c
#include "stm32f10x.h"
#include "stm32f10x_spi.h"
#include "stm32f10x_gpio.h"
#define CS_LOW() GPIO_ResetBits(GPIOB, GPIO_Pin_12)
#define CS_HIGH() GPIO_SetBits(GPIOB, GPIO_Pin_12)
#define CMD0 0x40 // 0x40+0 = 0x40
#define CMD8 0x48 // 0x40+8 = 0x48
#define CMD17 0x51 // 0x40+17 = 0x51
#define CMD24 0x58 // 0x40+24 = 0x58
#define CMD55 0x77 // 0x40+55 = 0x77
#define ACMD41 0x69 // 0x40+41 = 0x69
void delay_us(uint32_t us)
{
uint32_t i, j;
for (i = 0; i < us; i++) {
for (j = 0; j < 8; j++) {
__NOP();
}
}
}
void spi_init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
SPI_InitTypeDef SPI_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
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(SPI1, &SPI_InitStructure);
SPI_Cmd(SPI1, ENABLE);
}
uint8_t spi_sendbyte(SPI_TypeDef* SPIx, uint8_t byte)
{
while (SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_TXE) == RESET);
SPI_I2S_SendData(SPIx, byte);
while (SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_RXNE) == RESET);
return SPI_I2S_ReceiveData(SPIx);
}
uint8_t send_command(uint8_t cmd, uint32_t arg, uint8_t crc)
{
uint8_t i, response;
CS_LOW();
spi_sendbyte(SPI1, cmd | 0x40);
spi_sendbyte(SPI1, arg >> 24);
spi_sendbyte(SPI1, arg >> 16);
spi_sendbyte(SPI1, arg >> 8);
spi_sendbyte(SPI1, arg);
spi_sendbyte(SPI1, crc);
for (i = 0; i < 10; i++) {
response = spi_sendbyte(SPI1, 0xFF);
if (!(response & 0x80)) {
break;
}
}
CS_HIGH();
return response;
}
uint8_t sd_init(void)
{
uint8_t i, response;
uint32_t retry;
spi_init();
CS_HIGH();
for (i = 0; i < 10; i++) {
spi_sendbyte(SPI1, 0xFF);
}
// CMD0: Reset
retry = 0;
do {
response = send_command(CMD0, 0, 0x95);
retry++;
} while ((response != 0x01) && (retry < 0x20));
if (retry == 0x20) {
return 1;
}
// CMD8: Send interface condition
response = send_command(CMD8, 0x1AA, 0x87);
if (response == 0x01) {
for (i = 0; i < 4; i++) {
response = spi_sendbyte(SPI1, 0xFF);
}
if ((response & 0x0F) != 0x01) {
return 1;
}
}
// ACMD41: Send operation condition
retry = 0;
do {
response = send_command(CMD55, 0, 0x65);
response = send_command(ACMD41, 0x40000000, 0xE5);
retry++;
} while ((response != 0x00) && (retry < 0x20));
if (retry == 0x20) {
return 1;
}
// CMD58: Read OCR
response = send_command(CMD58, 0, 0xFD);
for (i = 0; i < 4; i++) {
spi_sendbyte(SPI1, 0xFF);
}
CS_HIGH();
return 0;
}
int main(void)
{
sd_init();
while (1) {
// TODO: Do something with the SD card
}
}
```
上述示例程序中定义了一些常量和函数,其中:
- `CS_LOW()` 和 `CS_HIGH()` 函数用于使能和禁止 SD 卡的片选信号;
- `CMD0`、`CMD8`、`CMD17`、`CMD24`、`CMD55` 和 `ACMD41` 常量分别表示 SD 卡的若干命令;
- `delay_us()` 函数用于产生微秒级的延时;
- `spi_init()` 函数用于初始化 SPI 外设;
- `spi_sendbyte()` 函数用于向 SPI 外设发送一个字节,并返回接收到的字节;
- `send_command()` 函数用于向 SD 卡发送一个命令,并返回响应结果;
- `sd_init()` 函数用于初始化 SD 卡并检测其是否可用。
在 `main()` 函数中,首先调用 `sd_init()` 函数来初始化 SD 卡和 SPI 外设,然后进入一个死循环,可在其中执行 SD 卡读写操作等操作。
阅读全文