📄 sd_mmc.c
字号:
#include "stm32f10x_lib.h"
#include"hardware.h"
#include "SD_MMC.h"
void SPI_Config(void) ;
#define SPI_CS_Assert() GPIO_ResetBits(GPIOD, GPIO_Pin_9)
/* Deselect MSD Card: ChipSelect pin high */
#define SPI_CS_Deassert() GPIO_SetBits(GPIOD, GPIO_Pin_9)
unsigned char SPI_WriteByte(unsigned char data)
{
uint8 Data = 0;
/* Wait until the transmit buffer is empty */
while (SPI_GetFlagStatus(SPI1, SPI_FLAG_TXE) == RESET);
/* Send the byte */
SPI_SendData(SPI1, data);
/* Wait until a data is received */
while (SPI_GetFlagStatus(SPI1, SPI_FLAG_RXNE) == RESET);
/* Get the received data */
Data = SPI_ReceiveData(SPI1);
/* Return the shifted data */
return Data;
}
//sd卡写命令 //sd send command
uint8 MMC_SD_SendCommand(uint8 cmd, uint32 arg)
{
uint8 r1;
uint8 retry=0;
SPI_WriteByte(0xff);
SPI_CS_Assert();
SPI_WriteByte(cmd | 0x40);//分别写入命令 //send command
SPI_WriteByte(arg>>24);
SPI_WriteByte(arg>>16);
SPI_WriteByte(arg>>8);
SPI_WriteByte(arg);
SPI_WriteByte(0x95);
while((r1 = SPI_WriteByte(0xff)) == 0xff)//等待响应, //wait response
if(retry++ > 200) break;//超时退出 //time out error
SPI_CS_Deassert();
return r1;//返回状态值 //return state
}
//sd卡复位 //reset sd card (software)
uint8 MMC_SD_Reset(void)
{
uint8 i;
uint8 retry;
uint8 r1=0;
retry = 0;
SPI_Config();
GPIO_ResetBits(GPIOD, GPIO_Pin_10);
delay(1000);
/* MSD chip select high */
SPI_CS_Deassert();
do
{
for(i=0;i<10;i++) SPI_WriteByte(0xff);
r1 = MMC_SD_SendCommand(0, 0);//发idle命令 //send idle command
retry++;
if(retry>100) return 1;//超时退出 //time out
} while(r1 != 0x01);
retry = 0;
do
{
r1 = MMC_SD_SendCommand(1, 0);//发active命令 //send active command
retry++;
if(retry>200) return 1;//超时退出 //time out
} while(r1);
//SPI_High();
r1 = MMC_SD_SendCommand(59, 0);//关crc //disable CRC
r1 = MMC_SD_SendCommand(16, 512);//设扇区大小512 //set sector size to 512
return 0;//正常返回 //normal return
}
//读一个扇区 //read one sector
uint8 MMC_SD_ReadSingleBlock(uint32 sector, uint8* buffer)
{
uint8 r1;
uint16 i;
//uint8 retry=0;
r1 = MMC_SD_SendCommand(17, sector<<9);//读命令 //read command
if(r1 != 0x00)
return r1;
SPI_CS_Assert();
//等数据的开始 //wait to start recieve data
while(SPI_WriteByte(0xff) != 0xfe);//if(retry++ > 50){SPI_CS_Deassert();return 1;}
for(i=0; i<512; i++)//读512个数据 //read 512 bytes
{
*buffer++ = SPI_WriteByte(0xff);
}
SPI_WriteByte(0xff);//伪crc
SPI_WriteByte(0xff);
SPI_CS_Deassert();
return 0;
}
//写一个扇区 //wirite one sector //not used in this application
uint8 MMC_SD_WriteSingleBlock(uint32 sector, uint8* buffer)
{
uint8 r1;
uint16 i;
//uint8 retry=0;
r1 = MMC_SD_SendCommand(24, sector<<9);//写命令 //send command
if(r1 != 0x00)
return r1;
SPI_CS_Assert();
SPI_WriteByte(0xff);
SPI_WriteByte(0xff);
SPI_WriteByte(0xff);
SPI_WriteByte(0xfe);//发开始符 //send start byte
for(i=0; i<512; i++)//送512字节数据 //send 512 bytes data
{
SPI_WriteByte(*buffer++);
}
SPI_WriteByte(0xff);
SPI_WriteByte(0xff);
r1 = SPI_WriteByte(0xff);
if( (r1&0x1f) != 0x05)//等待是否成功 //judge if it successful
{
SPI_CS_Deassert();
return r1;
}
//等待操作完 //wait no busy
while(!SPI_WriteByte(0xff));//if(retry++ > 50){SPI_CS_Deassert();return 1;}
SPI_CS_Deassert();
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -