您现在的位置:主页 > 工程案例 >

stm32 SPI读写储存卡(MicroSD TF卡)

时期:2021-12-21 01:16 点击数:
本文摘要:简述花了较长的时间,来弄读写储存卡(大部门教程讲的比力全可是不是很容易懂),这里希望我的代码履历能够资助到你。

leyu乐鱼体育

乐鱼体育官网登录

简述花了较长的时间,来弄读写储存卡(大部门教程讲的比力全可是不是很容易懂),这里希望我的代码履历能够资助到你。操作分析及实现0.整个流程1、上电以后储存卡的初始化2、如何举行读写实现1.上电以后储存卡的初始化上电给MicroSD卡至少74个时钟信号发送CDM0 (x041)复位发送CMD1 让MicroSD卡进入SPI模式2.如何举行读写这里主要对1,3举行详细的讨论你需要知道的是spi通信是怎样举行的就可以完成以上连个任务了3.下面是详细的历程//首先看一下我们的函数char SD_Init(void); //初始化函数char WriteSectorToMicroSD(long addr,char *buff);//写一个扇区 512bitchar ReadSectorFromMicroSD(long sector,char *buffer);// 读一个扇区 下面是主函数里的的部门//界说读入缓冲区extern char MicroSDDataBuff[512];void MicroSDTest(){//这是在内存卡初始化以后的测试举行读写如果不乐成还是从读写序列号开始吧 //WriteSectorToMicroSD(2,"Hello I here did you find me!!!"); delay_ms(200); ReadSectorFromMicroSD(2,MicroSDDataBuff); //printf("MicroSD read is:%s!!!!nn",MicroSDDataBuff);}int main(){char arr[100]={0},key=-1;int x=0,y=0;SPI2_Init();uart_init(115200);delay_init();SD_Init();MicroSDTest();printf("Read:%s",MicroSDDataBuff);while(1);}//下面是MicroSD的初始化及读写函数#define MicroSD_CS_SET GPIO_SetBits(GPIOB,GPIO_Pin_0)#define MicroSD_CS_RESET GPIO_ResetBits(GPIOB,GPIO_Pin_0)char MicroSDDataBuff[512]={0};//一个扇区的巨细char SentCommandToMicroSD(char cmd,int data,char crc){char result=0,times=0;MicroSD_CS_SET;//克制SD卡片选 同步时钟SPI1_ReadWriteByte(0xff); MicroSD_CS_RESET;//开始传输SPI1_ReadWriteByte(cmd);for(times=0;times<4;times++) { SPI1_ReadWriteByte((data>>24)&0xff); data<<=8; } SPI1_ReadWriteByte(crc);SPI1_ReadWriteByte(0xff); //八个时钟 times=0;do{ //读取后8位 result = SPI1_ReadWriteByte(0xFF); times++;}while((result==0xff)&&(times<200));return result;}//初始化化不是很稳定也就是说明传输数据可能不是很稳定char SD_Init(void){char i,temp=0;//char CMD[] = {0x40,0x00,0x00,0x00,0x00,0x95};int retry=0;GPIO_InitTypeDef GPIO_InitStructure;RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE );//PORTB时钟使能 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //PB0GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;GPIO_Init(GPIOB, &GPIO_InitStructure);//初始化GPIOB0GPIO_SetBits(GPIOB,GPIO_Pin_0);//初始化SPI1SPI1_Init();MicroSD_CS_SET;//发送至少74个时钟信号,数据顺便填写for (i=0;i<0x2f;i++){SPI1_ReadWriteByte(0xff);} //为了能够乐成写入CMD0,在这里写200次do { temp=SentCommandToMicroSD(0x40,0,0x95); retry++; if(retry>800){ //凌驾200次 //CMD0 Error!return(INIT_CMD0_ERROR); printf("Init MicroSD CMD0 Error!!!Back:%dnn",temp);return 0;} }while(temp!=0x01); //回应01h,停止写入printf("Reset MicroSD successfully!!!times:%dnn",retry);//发送CMD1到SD卡 retry=0; do{//为了能乐成写入CMD1,写100次 temp=SentCommandToMicroSD(0x41,0,0xff);retry++; if(retry>800) { //凌驾100次printf("Init 1MicroSD CMD1 Error!!!Back:%dnn",temp);return 0;}}while(temp!=0x00);//回应00h停止写入 MicroSD_CS_SET; //片选无效printf("Init MicroSD sent CMD1 successfully!!!times:%dnn",retry);//更换更快的SPI速率SPI1_SetSpeed(SPI_BaudRatePrescaler_4);return 0;}char WriteSectorToMicroSD(long addr,char *buff) { int tmp,retry; unsigned int i; addr = addr << 9; //addr = addr * 512 //写下令24到SD卡中去 retry=0; do{ //为了可靠写入,写100次 tmp=SentCommandToMicroSD(0x58,addr,0xff); retry++; if(retry>800) { printf("Write CMD58 Error!!!nr"); return 1; //send commamd Error! } } while(tmp!=0);//在写之前先发生100个时钟信号 for (i=0;i<100;i++) { SPI1_ReadWriteByte(0xff); } //写入开始字节 SPI1_ReadWriteByte(0xfe); //现在可以写入512个字节 for (i=0;i<512;i++) { SPI1_ReadWriteByte(*buff); buff++; } //CRC-Byte spi模式只需要在前两个下令发送,后面的crc自动不校验 SPI1_ReadWriteByte(0xFF); //Dummy CRC SPI1_ReadWriteByte(0xFF); //CRC Code tmp=SPI1_ReadWriteByte(0xff); // read response if((tmp & 0x1F)!=0x05) // 写入的512个字节是未被接受 { MicroSD_CS_SET; printf("Write data didn't accept by MicroSDnr"); return 1; //Error! } //等到SD卡不忙为止 //因为数据被接受后,SD卡在向储存阵列中编程数据 while (SPI1_ReadWriteByte(0xff)!=0xff); //克制SD卡 写入乐成 MicroSD_CS_SET; return 0; }char ReadSectorFromMicroSD(long sector,char *buffer) {int retry; //下令16 int times=0;//unsigned char CMD[] = {0x51,0x00,0x00,0x00,0x00,0xFF}; unsigned char temp; //地址变换 由逻辑块地址转为字节地址 sector = sector << 9; //sector = sector * 512//将下令16写入SD卡 retry=0; do{ //为了保证写入下令 一共写100次 temp=SentCommandToMicroSD(0x51,sector,0xff); retry++; if(retry>800) { printf("Read sector from MicroSD is failed!!nr"); return 1; //block write Error! } } while(temp!=0); //Read Start Byte form MMC/SD-Card (FEh/Start Byte) //Now data is ready,you can read it out. while (SPI1_ReadWriteByte(0xff) != 0xfe); for(times=0;times<512;times++) { MicroSDDataBuff[times]=SPI1_ReadWriteByte(0xff); } //克制SD卡 读出完成 MicroSD_CS_SET; return 0;} 驱动MicroSD卡如果像我用的stm32f103c8t6的焦点板的话,可能还会好一点。

乐鱼体育官网登录

网上的资料由一点乱,如果想要详细的keil工程的话留下联系方式我会发给你的。另外呢后面的工程会用到这里的MicroSD的驱动,然后架设文件系统,所以这里还是关键的,另有一些坑需要深挖。


本文关键词:stm32,SPI,读写,储存,卡,MicroSD,简述,花了,较,乐鱼体育官网登录

本文来源:乐鱼体育官网登录-www.rtf3.com



Copyright © 2008-2021 www.rtf3.com. 乐鱼体育官网登录科技 版权所有 备案号:ICP备45192870号-9