SD卡读写函数(.c)

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

* Description : * Input : * Output : * Return : xxx0<status>1 * - status 010: Data accecpted * - status 101: Data rejected due to a crc error * - status 110: Data rejected due to a Write error. * - status 111: Data rejected due to other error. *************************************************************** ****************/ u8 SD_SPI_Wait_Data_Response(void) { u32 i = 0; u8 response, rvalue; while (i <= 80) { /* Read resonse */ response = SD_SPI_ReadByte(); /* Mask unused bits */ response &= 0x1F; USART_Sent_res(response); switch (response) { case SD_SPI_DATA_OK: { rvalue = SD_SPI_DATA_OK; break; } case SD_SPI_DATA_CRC_ERROR: return SD_SPI_DATA_CRC_ERROR; case SD_SPI_DATA_WRITE_ERROR: return SD_SPI_DATA_WRITE_ERROR; default: { rvalue = SD_SPI_DATA_OTHER_ERROR; break; } } /* Exit loop in case of data ok */ if (rvalue == SD_SPI_DATA_OK) break; /* Increment loop counter */ 第 4 页
SD卡读写函数.c /* Includes -----------------------------------------------------------------*/ #include "SD_SPI.h" #include "stm32f10x_lib.h" #include "usart1.h" /* Private typedef -----------------------------------------------------------*/ /* Private define ------------------------------------------------------------*/ /* Private macro -------------------------------------------------------------*/ u8 Card_Type = 0x00; //***************************于所移植的硬件相关 ******************************** /* Select SD_SPI Card: ChipSelect pin low */ #define SD_SPI_CS_LOW() GPIO_ResetBits(GPIOD, GPIO_Pin_9) //于所移植的硬件相关 /* Deselect SD_SPI Card: ChipSelect pin high */ #define SD_SPI_CS_HIGH() GPIO_SetBits(GPIOD, GPIO_Pin_9) //于所移植的硬件相关 //电源控制 #define SD_SPI_POWERON() GPIO_ResetBits(GPIOD, GPIO_Pin_10) //于所移植的硬件相关 #define SD_SPI_POWEROFF() GPIO_SetBits(GPIOD, GPIO_Pin_10) //于所移植的硬件相关 //***************************于所移植的硬件相关 ******************************** /* Private function prototypes -----------------------------------------------*/ void SPI_Config(void); /* Global Variable ------------------------------------------------------------*/ /* Private functions ---------------------------------------------------------*/ /************************************************************** ***************** * Function Name : SD_SPI_SendCmd 第 1 页
* Description * Input SD_SPI card * - Arg: the command argument * - Crc: the CRC * Output : None * Return : None *************************************************************** ****************/ u8 SD_SPI_SendCmd(u8 Cmd, u32 Arg, u8 Crc) { u8 res = 0xFF; u16 Retry = 0x00; /* Send dummy byte 0xFF */ SD_SPI_WriteByte(DUMMY); /* send byte1 */ SD_SPI_WriteByte(Cmd | 0x40); /* send byte2 */ SD_SPI_WriteByte((u8)(Arg >> 24)); /* send byte3 */ SD_SPI_WriteByte((u8)(Arg >> 16)); /* send byte4 */ SD_SPI_WriteByte((u8)(Arg >> 8)); /* send byte5 */ SD_SPI_WriteByte((u8)(Arg)); /* send CRC: byte6 */ SD_SPI_WriteByte(Crc); //lest read a stuff byte CMD12 数据手册有说明 if(Cmd == 12) SD_SPI_ReadByte(); /* Wait CMD response try max 8000 */ while (Retry < 8000) { res = SD_SPI_ReadByte(); /* 当回应为 0x00 0x01 退出 */ if(res <= 0x01) break; Retry++; } return res; } /************************************************************** ***************** * Function Name : SD_SPI_Wait_Token * Description : wait the Token. * Input : None 第 2 页
SD卡读写函数.c Get SD_SPI card data response. None None The SD_SPI status: Rea //等待 SD卡不忙 if(SD_SPI_Wait_Not_Busy()) /* Return response */ else } i++; return 0xFF; return response;
SD卡读写函数.c : Send 5 bytes command to the SD_SPI card. : - Cmd: the user expected command to send to
SD卡读写函数.c * Output : None * Return : Response: - SD_SPI_RESPONSE_FAILURE: Sequence failed * - SD_SPI_RESPONSE_NO_ERROR: Sequence succeed *************************************************************** ****************/ u8 SD_SPI_Wait_Token(u8 Response) { u32 Count = 0; /* Check if response is got or a timeout is happen */ while ((SD_SPI_ReadByte() != Response) && ((++Count) < 8000)); /* After time out */ if (Count >= 8000) return SD_SPI_RESPONSE_FAILURE; /* Right response got */ else return SD_SPI_RESPONSE_NO_ERROR; } /************************************************************** ***************** * Function Name : SD_SPI_Wait_Not_Busy * Description : wait the SD_SPI is not Busy. * Input : None * Output : None * Return : Response: - SD_SPI_RESPONSE_FAILURE: Sequence failed * - SD_SPI_RESPONSE_NO_ERROR: Sequence succeed *************************************************************** ****************/ u8 SD_SPI_Wait_Not_Busy(void) { u32 Count = 0; /* Check if response is 0xFF or a timeout is happen */ while ((SD_SPI_ReadByte() != 0xFF) && ((++Count) < 80000)); /* After time out */ if (Count >= 80000) return SD_SPI_RESPONSE_FAILURE; /* Right res 0xFF */ else return SD_SPI_RESPONSE_NO_ERROR; } /************************************************************** ***************** * Function Name : SD_SPI_Wait_Data_Response 第 3 页
/************************************************************** ***************** * Function Name : SD_SPI_GetStatus * Description : Returns the SD_SPI status. 针对于CMD13,回复 格式R2 * Input : None * Output : None * Return : The SD_SPI status. *************************************************************** ****************/ u16 SD_SPI_GetStatus(void) { u16 Status = 0; /* SD_SPI chip select low */ SD_SPI_CS_LOW(); /* Send CMD13 (SD_SPI_SEND_STATUS) to get SD_SPI status */ SD_SPI_SendCmd(SD_SPI_SEND_STATUS, 0, 0xFF); Status = SD_SPI_ReadByte(); Status |= (u16)(SD_SPI_ReadByte() << 8); /* SD_SPI chip select high */ SD_SPI_CS_HIGH(); /* Send dummy byte 0xFF */ SD_SPI_WriteByte(DUMMY); return Status; } /************************************************************** ***************** * Function Name : SD_SPI_Init * Description : Initializes the SD_SPI/SD communication. * Input : None 第 5 页
相关文档
最新文档