SD卡的初始化和读写程序.doc
文本预览下载声明
/***********************************************************************************
SPI模式 读写SD卡
SD卡初始化过程:
1. 初始化STM32的SPI接口 使用低速模式
2. 延时至少74clock
3. 发送CMD0,需要返回0x01,进入Idle状态
4. 循环发送CMD55+ACMD41,直到返回0x00,进入Ready状态
5. 设置读写block大小为512byte
5. 把STM32的SPI设置为高速模式
读一个block块的过程
1. 发送CMD17(单块)或CMD18(多块)读命令,返回0x00
2. 接收数据开始令牌0xfe + 正式数据512Bytes + CRC 校验2Bytes
写一个block块的过程
1. 发送CMD24(单块)或CMD25(多块)写命令,返回0x00
2. 发送数据开始令牌0xfe + 正式数据512Bytes + CRC校验2Bytes
-----------------------------------------------------------------------------
* ----------------------------------------------
* | STM32F10x | MSD Pin |
* ----------------------------------------------
* | PE.3 | ChipSelect 1 |
* | PA.7 / MOSI | DataIn 2 |
* | | GND 3 (0 V) |
* | | VDD 4 (3.3 V) |
* | PA.5 / SCLK | Clock 5 |
* | | GND 6 (0 V) |
* | PA.6 / MISO | DataOut 7 |
* -----------------------------------------------
***********************************************************************************/
/* Includes ------------------------------------------------------------------*/
//#include stm32f10x_lib.h
#include stm32f10x.h
#include hardwareinit.h
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
#define sd_cs_1 GPIO_SetBits(GPIOB,GPIO_Pin_12)
#define sd_cs_0 GPIO_ResetBits(GPIOB,GPIO_Pin_12)
#define sd_check GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_15)
/* Select MSD Card: ChipSelect pin low */
//#define MSD_CS_LOW() GPIO_ResetBits(GPIOE, GPIO_Pin_3)
/* Deselect MSD Card: ChipSelect pin high */
//#define MSD_CS_HIGH() GPIO_SetBits(GPIOE, GPIO_Pin_3)
显示全部