stm32 spi读写tf卡
时间: 2023-07-20 12:03:28 浏览: 641
stm32+HAL+模拟SPI+TF+MSC模拟U盘
以下是使用STM32的SPI接口读写TF卡的基本步骤:
1. 配置SPI接口:设置SPI时钟、CPOL、CPHA、数据大小等参数,使能SPI接口。
2. 初始化TF卡:通过SPI接口向TF卡发送初始化命令,完成TF卡的初始化工作。
3. 发送读写命令:通过SPI接口向TF卡发送读写命令,指定读写地址和数据长度。
4. 数据传输:根据命令类型,通过SPI接口进行数据传输,读取或写入数据。
5. 关闭SPI接口:完成数据传输后,关闭SPI接口。
下面是一个简单的示例代码,使用SPI1和PB12引脚进行TF卡读写操作:
```
#include "stm32f10x.h"
#define SPIx SPI1
#define SPI_GPIO GPIOB
#define SPI_SCK_PIN GPIO_Pin_13
#define SPI_MISO_PIN GPIO_Pin_14
#define SPI_MOSI_PIN GPIO_Pin_15
#define CS_GPIO GPIOB
#define CS_PIN GPIO_Pin_12
void SPI_Configuration(void)
{
SPI_InitTypeDef SPI_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1 | RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Pin = SPI_SCK_PIN | SPI_MOSI_PIN;
GPIO_Init(SPI_GPIO, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Pin = SPI_MISO_PIN;
GPIO_Init(SPI_GPIO, &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_High;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_Init(SPIx, &SPI_InitStructure);
SPI_Cmd(SPIx, ENABLE);
}
void CS_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = CS_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(CS_GPIO, &GPIO_InitStructure);
GPIO_SetBits(CS_GPIO, CS_PIN);
}
void TF_Init(void)
{
uint8_t i;
GPIO_ResetBits(CS_GPIO, CS_PIN);
for (i = 0; i < 10; i++)
{
SPIx_ReadWriteByte(0xFF);
}
GPIO_SetBits(CS_GPIO, CS_PIN);
}
uint8_t SPIx_ReadWriteByte(uint8_t data)
{
while (SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_TXE) == RESET)
;
SPI_I2S_SendData(SPIx, data);
while (SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_RXNE) == RESET)
;
return SPI_I2S_ReceiveData(SPIx);
}
void TF_Read_Block(uint32_t blockAddr, uint8_t *buf)
{
uint16_t i;
GPIO_ResetBits(CS_GPIO, CS_PIN);
SPIx_ReadWriteByte(0xFF);
SPIx_ReadWriteByte(0xFF);
SPIx_ReadWriteByte(0x51);
SPIx_ReadWriteByte((blockAddr >> 24) & 0xFF);
SPIx_ReadWriteByte((blockAddr >> 16) & 0xFF);
SPIx_ReadWriteByte((blockAddr >> 8) & 0xFF);
SPIx_ReadWriteByte(blockAddr & 0xFF);
SPIx_ReadWriteByte(0xFF);
while (SPIx_ReadWriteByte(0xFF) != 0xFE);
for (i = 0; i < 512; i++)
{
buf[i] = SPIx_ReadWriteByte(0xFF);
}
SPIx_ReadWriteByte(0xFF);
SPIx_ReadWriteByte(0xFF);
GPIO_SetBits(CS_GPIO, CS_PIN);
}
void TF_Write_Block(uint32_t blockAddr, const uint8_t *buf)
{
uint16_t i;
GPIO_ResetBits(CS_GPIO, CS_PIN);
SPIx_ReadWriteByte(0xFF);
SPIx_ReadWriteByte(0xFF);
SPIx_ReadWriteByte(0x58);
SPIx_ReadWriteByte((blockAddr >> 24) & 0xFF);
SPIx_ReadWriteByte((blockAddr >> 16) & 0xFF);
SPIx_ReadWriteByte((blockAddr >> 8) & 0xFF);
SPIx_ReadWriteByte(blockAddr & 0xFF);
SPIx_ReadWriteByte(0xFF);
SPIx_ReadWriteByte(0xFE);
for (i = 0; i < 512; i++)
{
SPIx_ReadWriteByte(buf[i]);
}
SPIx_ReadWriteByte(0xFF);
SPIx_ReadWriteByte(0xFF);
GPIO_SetBits(CS_GPIO, CS_PIN);
}
int main(void)
{
uint8_t buf[512];
SystemInit();
SPI_Configuration();
CS_Configuration();
TF_Init();
TF_Read_Block(0, buf);
while (1)
;
}
```
在上面的代码中,使用了TF_Read_Block和TF_Write_Block分别读取和写入TF卡的一个512字节的数据块,可以根据需要修改代码进行扩展。
阅读全文