stm32_adafruit_sd.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037
  1. /**
  2. ******************************************************************************
  3. * @file stm32_adafruit_sd.c
  4. * @author MCD Application Team
  5. * @brief This file provides a set of functions needed to manage the SD card
  6. * mounted on the Adafruit 1.8" TFT LCD shield (reference ID 802),
  7. * that is used with the STM32 Nucleo board through SPI interface.
  8. * It implements a high level communication layer for read and write
  9. * from/to this memory. The needed STM32XXxx hardware resources (SPI and
  10. * GPIO) are defined in stm32XXxx_nucleo.h file, and the initialization is
  11. * performed in SD_IO_Init() function declared in stm32XXxx_nucleo.c
  12. * file.
  13. * You can easily tailor this driver to any other development board,
  14. * by just adapting the defines for hardware resources and
  15. * SD_IO_Init() function.
  16. *
  17. * +-------------------------------------------------------+
  18. * | Pin assignment |
  19. * +-------------------------+---------------+-------------+
  20. * | STM32XXxx SPI Pins | SD | Pin |
  21. * +-------------------------+---------------+-------------+
  22. * | SD_SPI_CS_PIN | ChipSelect | 1 |
  23. * | SD_SPI_MOSI_PIN / MOSI | DataIn | 2 |
  24. * | | GND | 3 (0 V) |
  25. * | | VDD | 4 (3.3 V)|
  26. * | SD_SPI_SCK_PIN / SCLK | Clock | 5 |
  27. * | | GND | 6 (0 V) |
  28. * | SD_SPI_MISO_PIN / MISO | DataOut | 7 |
  29. * +-------------------------+---------------+-------------+
  30. ******************************************************************************
  31. * @attention
  32. *
  33. * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
  34. *
  35. * Redistribution and use in source and binary forms, with or without modification,
  36. * are permitted provided that the following conditions are met:
  37. * 1. Redistributions of source code must retain the above copyright notice,
  38. * this list of conditions and the following disclaimer.
  39. * 2. Redistributions in binary form must reproduce the above copyright notice,
  40. * this list of conditions and the following disclaimer in the documentation
  41. * and/or other materials provided with the distribution.
  42. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  43. * may be used to endorse or promote products derived from this software
  44. * without specific prior written permission.
  45. *
  46. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  47. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  48. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  49. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  50. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  51. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  52. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  53. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  54. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  55. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  56. *
  57. ******************************************************************************
  58. */
  59. /* File Info : -----------------------------------------------------------------
  60. User NOTES
  61. 1. How to use this driver:
  62. --------------------------
  63. - This driver does not need a specific component driver for the micro SD device
  64. to be included with.
  65. 2. Driver description:
  66. ---------------------
  67. + Initialization steps:
  68. o Initialize the micro SD card using the BSP_SD_Init() function.
  69. o Checking the SD card presence is not managed because SD detection pin is
  70. not physically mapped on the Adafruit shield.
  71. o The function BSP_SD_GetCardInfo() is used to get the micro SD card information
  72. which is stored in the structure "SD_CardInfo".
  73. + Micro SD card operations
  74. o The micro SD card can be accessed with read/write block(s) operations once
  75. it is ready for access. The access can be performed in polling
  76. mode by calling the functions BSP_SD_ReadBlocks()/BSP_SD_WriteBlocks()
  77. o The SD erase block(s) is performed using the function BSP_SD_Erase() with
  78. specifying the number of blocks to erase.
  79. o The SD runtime status is returned when calling the function BSP_SD_GetStatus().
  80. ------------------------------------------------------------------------------*/
  81. /* Includes ------------------------------------------------------------------*/
  82. #include "stm32_adafruit_sd.h"
  83. #include "stdlib.h"
  84. #include "string.h"
  85. #include "stdio.h"
  86. /** @addtogroup BSP
  87. * @{
  88. */
  89. /** @addtogroup STM32_ADAFRUIT
  90. * @{
  91. */
  92. /** @defgroup STM32_ADAFRUIT_SD
  93. * @{
  94. */
  95. /* Private typedef -----------------------------------------------------------*/
  96. /** @defgroup STM32_ADAFRUIT_SD_Private_Types_Definitions
  97. * @{
  98. */
  99. typedef struct {
  100. uint8_t r1;
  101. uint8_t r2;
  102. uint8_t r3;
  103. uint8_t r4;
  104. uint8_t r5;
  105. } SD_CmdAnswer_typedef;
  106. /**
  107. * @}
  108. */
  109. /* Private define ------------------------------------------------------------*/
  110. /** @defgroup STM32_ADAFRUIT_SD_Private_Defines
  111. * @{
  112. */
  113. #define SD_DUMMY_BYTE 0xFF
  114. #define SD_MAX_FRAME_LENGTH 17 /* Lenght = 16 + 1 */
  115. #define SD_CMD_LENGTH 6
  116. #define SD_MAX_TRY 100 /* Number of try */
  117. #define SD_CSD_STRUCT_V1 0x2 /* CSD struct version V1 */
  118. #define SD_CSD_STRUCT_V2 0x1 /* CSD struct version V2 */
  119. /**
  120. * @brief SD ansewer format
  121. */
  122. typedef enum {
  123. SD_ANSWER_R1_EXPECTED,
  124. SD_ANSWER_R1B_EXPECTED,
  125. SD_ANSWER_R2_EXPECTED,
  126. SD_ANSWER_R3_EXPECTED,
  127. SD_ANSWER_R4R5_EXPECTED,
  128. SD_ANSWER_R7_EXPECTED,
  129. }SD_Answer_type;
  130. /**
  131. * @brief Start Data tokens:
  132. * Tokens (necessary because at nop/idle (and CS active) only 0xff is
  133. * on the data/command line)
  134. */
  135. #define SD_TOKEN_START_DATA_SINGLE_BLOCK_READ 0xFE /* Data token start byte, Start Single Block Read */
  136. #define SD_TOKEN_START_DATA_MULTIPLE_BLOCK_READ 0xFE /* Data token start byte, Start Multiple Block Read */
  137. #define SD_TOKEN_START_DATA_SINGLE_BLOCK_WRITE 0xFE /* Data token start byte, Start Single Block Write */
  138. #define SD_TOKEN_START_DATA_MULTIPLE_BLOCK_WRITE 0xFD /* Data token start byte, Start Multiple Block Write */
  139. #define SD_TOKEN_STOP_DATA_MULTIPLE_BLOCK_WRITE 0xFD /* Data toke stop byte, Stop Multiple Block Write */
  140. /**
  141. * @brief Commands: CMDxx = CMD-number | 0x40
  142. */
  143. #define SD_CMD_GO_IDLE_STATE 0 /* CMD0 = 0x40 */
  144. #define SD_CMD_SEND_OP_COND 1 /* CMD1 = 0x41 */
  145. #define SD_CMD_SEND_IF_COND 8 /* CMD8 = 0x48 */
  146. #define SD_CMD_SEND_CSD 9 /* CMD9 = 0x49 */
  147. #define SD_CMD_SEND_CID 10 /* CMD10 = 0x4A */
  148. #define SD_CMD_STOP_TRANSMISSION 12 /* CMD12 = 0x4C */
  149. #define SD_CMD_SEND_STATUS 13 /* CMD13 = 0x4D */
  150. #define SD_CMD_SET_BLOCKLEN 16 /* CMD16 = 0x50 */
  151. #define SD_CMD_READ_SINGLE_BLOCK 17 /* CMD17 = 0x51 */
  152. #define SD_CMD_READ_MULT_BLOCK 18 /* CMD18 = 0x52 */
  153. #define SD_CMD_SET_BLOCK_COUNT 23 /* CMD23 = 0x57 */
  154. #define SD_CMD_WRITE_SINGLE_BLOCK 24 /* CMD24 = 0x58 */
  155. #define SD_CMD_WRITE_MULT_BLOCK 25 /* CMD25 = 0x59 */
  156. #define SD_CMD_PROG_CSD 27 /* CMD27 = 0x5B */
  157. #define SD_CMD_SET_WRITE_PROT 28 /* CMD28 = 0x5C */
  158. #define SD_CMD_CLR_WRITE_PROT 29 /* CMD29 = 0x5D */
  159. #define SD_CMD_SEND_WRITE_PROT 30 /* CMD30 = 0x5E */
  160. #define SD_CMD_SD_ERASE_GRP_START 32 /* CMD32 = 0x60 */
  161. #define SD_CMD_SD_ERASE_GRP_END 33 /* CMD33 = 0x61 */
  162. #define SD_CMD_UNTAG_SECTOR 34 /* CMD34 = 0x62 */
  163. #define SD_CMD_ERASE_GRP_START 35 /* CMD35 = 0x63 */
  164. #define SD_CMD_ERASE_GRP_END 36 /* CMD36 = 0x64 */
  165. #define SD_CMD_UNTAG_ERASE_GROUP 37 /* CMD37 = 0x65 */
  166. #define SD_CMD_ERASE 38 /* CMD38 = 0x66 */
  167. #define SD_CMD_SD_APP_OP_COND 41 /* CMD41 = 0x69 */
  168. #define SD_CMD_APP_CMD 55 /* CMD55 = 0x77 */
  169. #define SD_CMD_READ_OCR 58 /* CMD55 = 0x79 */
  170. /**
  171. * @brief SD reponses and error flags
  172. */
  173. typedef enum
  174. {
  175. /* R1 answer value */
  176. SD_R1_NO_ERROR = (0x00),
  177. SD_R1_IN_IDLE_STATE = (0x01),
  178. SD_R1_ERASE_RESET = (0x02),
  179. SD_R1_ILLEGAL_COMMAND = (0x04),
  180. SD_R1_COM_CRC_ERROR = (0x08),
  181. SD_R1_ERASE_SEQUENCE_ERROR= (0x10),
  182. SD_R1_ADDRESS_ERROR = (0x20),
  183. SD_R1_PARAMETER_ERROR = (0x40),
  184. /* R2 answer value */
  185. SD_R2_NO_ERROR = 0x00,
  186. SD_R2_CARD_LOCKED = 0x01,
  187. SD_R2_LOCKUNLOCK_ERROR = 0x02,
  188. SD_R2_ERROR = 0x04,
  189. SD_R2_CC_ERROR = 0x08,
  190. SD_R2_CARD_ECC_FAILED = 0x10,
  191. SD_R2_WP_VIOLATION = 0x20,
  192. SD_R2_ERASE_PARAM = 0x40,
  193. SD_R2_OUTOFRANGE = 0x80,
  194. /**
  195. * @brief Data response error
  196. */
  197. SD_DATA_OK = (0x05),
  198. SD_DATA_CRC_ERROR = (0x0B),
  199. SD_DATA_WRITE_ERROR = (0x0D),
  200. SD_DATA_OTHER_ERROR = (0xFF)
  201. } SD_Error;
  202. /**
  203. * @}
  204. */
  205. /* Private macro -------------------------------------------------------------*/
  206. /** @defgroup STM32_ADAFRUIT_SD_Private_Macros
  207. * @{
  208. */
  209. /**
  210. * @}
  211. */
  212. /* Private variables ---------------------------------------------------------*/
  213. /** @defgroup STM32_ADAFRUIT_SD_Private_Variables
  214. * @{
  215. */
  216. __IO uint8_t SdStatus = SD_NOT_PRESENT;
  217. /* flag_SDHC :
  218. 0 : Standard capacity
  219. 1 : High capacity
  220. */
  221. uint16_t flag_SDHC = 0;
  222. /**
  223. * @}
  224. */
  225. /* Private function prototypes -----------------------------------------------*/
  226. static uint8_t SD_GetCIDRegister(SD_CID* Cid);
  227. static uint8_t SD_GetCSDRegister(SD_CSD* Csd);
  228. static uint8_t SD_GetDataResponse(void);
  229. static uint8_t SD_GoIdleState(void);
  230. static SD_CmdAnswer_typedef SD_SendCmd(uint8_t Cmd, uint32_t Arg, uint8_t Crc, uint8_t Answer);
  231. static uint8_t SD_WaitData(uint8_t data);
  232. static uint8_t SD_ReadData(void);
  233. /** @defgroup STM32_ADAFRUIT_SD_Private_Function_Prototypes
  234. * @{
  235. */
  236. /**
  237. * @}
  238. */
  239. /* Private functions ---------------------------------------------------------*/
  240. /** @defgroup STM32_ADAFRUIT_SD_Private_Functions
  241. * @{
  242. */
  243. /**
  244. * @brief Initializes the SD/SD communication.
  245. * @param None
  246. * @retval The SD Response:
  247. * - MSD_ERROR: Sequence failed
  248. * - MSD_OK: Sequence succeed
  249. */
  250. uint8_t BSP_SD_Init(void)
  251. {
  252. /* Configure IO functionalities for SD pin */
  253. SD_IO_Init();
  254. /* SD detection pin is not physically mapped on the Adafruit shield */
  255. SdStatus = SD_PRESENT;
  256. /* SD initialized and set to SPI mode properly */
  257. return SD_GoIdleState();
  258. }
  259. /**
  260. * @brief Returns information about specific card.
  261. * @param pCardInfo: Pointer to a SD_CardInfo structure that contains all SD
  262. * card information.
  263. * @retval The SD Response:
  264. * - MSD_ERROR: Sequence failed
  265. * - MSD_OK: Sequence succeed
  266. */
  267. uint8_t BSP_SD_GetCardInfo(SD_CardInfo *pCardInfo)
  268. {
  269. uint8_t status;
  270. status = SD_GetCSDRegister(&(pCardInfo->Csd));
  271. status|= SD_GetCIDRegister(&(pCardInfo->Cid));
  272. if(flag_SDHC == 1 )
  273. {
  274. pCardInfo->LogBlockSize = 512;
  275. pCardInfo->CardBlockSize = 512;
  276. pCardInfo->CardCapacity = (pCardInfo->Csd.version.v2.DeviceSize + 1) * 1024 * pCardInfo->LogBlockSize;
  277. pCardInfo->LogBlockNbr = (pCardInfo->CardCapacity) / (pCardInfo->LogBlockSize);
  278. }
  279. else
  280. {
  281. pCardInfo->CardCapacity = (pCardInfo->Csd.version.v1.DeviceSize + 1) ;
  282. pCardInfo->CardCapacity *= (1 << (pCardInfo->Csd.version.v1.DeviceSizeMul + 2));
  283. pCardInfo->LogBlockSize = 512;
  284. pCardInfo->CardBlockSize = 1 << (pCardInfo->Csd.RdBlockLen);
  285. pCardInfo->CardCapacity *= pCardInfo->CardBlockSize;
  286. pCardInfo->LogBlockNbr = (pCardInfo->CardCapacity) / (pCardInfo->LogBlockSize);
  287. }
  288. return status;
  289. }
  290. /**
  291. * @brief Reads block(s) from a specified address in the SD card, in polling mode.
  292. * @param pData: Pointer to the buffer that will contain the data to transmit
  293. * @param ReadAddr: Address from where data is to be read. The address is counted
  294. * in blocks of 512bytes
  295. * @param NumOfBlocks: Number of SD blocks to read
  296. * @param Timeout: This parameter is used for compatibility with BSP implementation
  297. * @retval SD status
  298. */
  299. uint8_t BSP_SD_ReadBlocks(uint32_t *pData, uint32_t ReadAddr, uint32_t NumOfBlocks, uint32_t Timeout)
  300. {
  301. uint32_t offset = 0;
  302. uint8_t retr = BSP_SD_ERROR;
  303. uint8_t *ptr = NULL;
  304. SD_CmdAnswer_typedef response;
  305. uint16_t BlockSize = 512;
  306. /* Send CMD16 (SD_CMD_SET_BLOCKLEN) to set the size of the block and
  307. Check if the SD acknowledged the set block length command: R1 response (0x00: no errors) */
  308. response = SD_SendCmd(SD_CMD_SET_BLOCKLEN, BlockSize, 0xFF, SD_ANSWER_R1_EXPECTED);
  309. SD_IO_CSState(1);
  310. SD_IO_WriteByte(SD_DUMMY_BYTE);
  311. if ( response.r1 != SD_R1_NO_ERROR)
  312. {
  313. goto error;
  314. }
  315. ptr = malloc(sizeof(uint8_t)*BlockSize);
  316. if( ptr == NULL )
  317. {
  318. goto error;
  319. }
  320. memset(ptr, SD_DUMMY_BYTE, sizeof(uint8_t)*BlockSize);
  321. /* Data transfer */
  322. while (NumOfBlocks--)
  323. {
  324. /* Send CMD17 (SD_CMD_READ_SINGLE_BLOCK) to read one block */
  325. /* Check if the SD acknowledged the read block command: R1 response (0x00: no errors) */
  326. response = SD_SendCmd(SD_CMD_READ_SINGLE_BLOCK, (ReadAddr + offset) * (flag_SDHC == 1 ? 1: BlockSize), 0xFF, SD_ANSWER_R1_EXPECTED);
  327. if ( response.r1 != SD_R1_NO_ERROR)
  328. {
  329. goto error;
  330. }
  331. /* Now look for the data token to signify the start of the data */
  332. if (SD_WaitData(SD_TOKEN_START_DATA_SINGLE_BLOCK_READ) == BSP_SD_OK)
  333. {
  334. /* Read the SD block data : read NumByteToRead data */
  335. SD_IO_WriteReadData(ptr, (uint8_t*)pData + offset, BlockSize);
  336. /* Set next read address*/
  337. offset += BlockSize;
  338. /* get CRC bytes (not really needed by us, but required by SD) */
  339. SD_IO_WriteByte(SD_DUMMY_BYTE);
  340. SD_IO_WriteByte(SD_DUMMY_BYTE);
  341. }
  342. else
  343. {
  344. goto error;
  345. }
  346. /* End the command data read cycle */
  347. SD_IO_CSState(1);
  348. SD_IO_WriteByte(SD_DUMMY_BYTE);
  349. }
  350. retr = BSP_SD_OK;
  351. error :
  352. /* Send dummy byte: 8 Clock pulses of delay */
  353. SD_IO_CSState(1);
  354. SD_IO_WriteByte(SD_DUMMY_BYTE);
  355. if(ptr != NULL) free(ptr);
  356. /* Return the reponse */
  357. return retr;
  358. }
  359. /**
  360. * @brief Writes block(s) to a specified address in the SD card, in polling mode.
  361. * @param pData: Pointer to the buffer that will contain the data to transmit
  362. * @param WriteAddr: Address from where data is to be written. The address is counted
  363. * in blocks of 512bytes
  364. * @param NumOfBlocks: Number of SD blocks to write
  365. * @param Timeout: This parameter is used for compatibility with BSP implementation
  366. * @retval SD status
  367. */
  368. uint8_t BSP_SD_WriteBlocks(uint32_t *pData, uint32_t WriteAddr, uint32_t NumOfBlocks, uint32_t Timeout)
  369. {
  370. uint32_t offset = 0;
  371. uint8_t retr = BSP_SD_ERROR;
  372. uint8_t *ptr = NULL;
  373. SD_CmdAnswer_typedef response;
  374. uint16_t BlockSize = 512;
  375. /* Send CMD16 (SD_CMD_SET_BLOCKLEN) to set the size of the block and
  376. Check if the SD acknowledged the set block length command: R1 response (0x00: no errors) */
  377. response = SD_SendCmd(SD_CMD_SET_BLOCKLEN, BlockSize, 0xFF, SD_ANSWER_R1_EXPECTED);
  378. SD_IO_CSState(1);
  379. SD_IO_WriteByte(SD_DUMMY_BYTE);
  380. if ( response.r1 != SD_R1_NO_ERROR)
  381. {
  382. goto error;
  383. }
  384. ptr = malloc(sizeof(uint8_t)*BlockSize);
  385. if (ptr == NULL)
  386. {
  387. goto error;
  388. }
  389. /* Data transfer */
  390. while (NumOfBlocks--)
  391. {
  392. /* Send CMD24 (SD_CMD_WRITE_SINGLE_BLOCK) to write blocks and
  393. Check if the SD acknowledged the write block command: R1 response (0x00: no errors) */
  394. response = SD_SendCmd(SD_CMD_WRITE_SINGLE_BLOCK, (WriteAddr + offset) * (flag_SDHC == 1 ? 1 : BlockSize), 0xFF, SD_ANSWER_R1_EXPECTED);
  395. if (response.r1 != SD_R1_NO_ERROR)
  396. {
  397. goto error;
  398. }
  399. /* Send dummy byte for NWR timing : one byte between CMDWRITE and TOKEN */
  400. SD_IO_WriteByte(SD_DUMMY_BYTE);
  401. SD_IO_WriteByte(SD_DUMMY_BYTE);
  402. /* Send the data token to signify the start of the data */
  403. SD_IO_WriteByte(SD_TOKEN_START_DATA_SINGLE_BLOCK_WRITE);
  404. /* Write the block data to SD */
  405. SD_IO_WriteReadData((uint8_t*)pData + offset, ptr, BlockSize);
  406. /* Set next write address */
  407. offset += BlockSize;
  408. /* Put CRC bytes (not really needed by us, but required by SD) */
  409. SD_IO_WriteByte(SD_DUMMY_BYTE);
  410. SD_IO_WriteByte(SD_DUMMY_BYTE);
  411. /* Read data response */
  412. if (SD_GetDataResponse() != SD_DATA_OK)
  413. {
  414. /* Set response value to failure */
  415. goto error;
  416. }
  417. SD_IO_CSState(1);
  418. SD_IO_WriteByte(SD_DUMMY_BYTE);
  419. }
  420. retr = BSP_SD_OK;
  421. error :
  422. if(ptr != NULL) free(ptr);
  423. /* Send dummy byte: 8 Clock pulses of delay */
  424. SD_IO_CSState(1);
  425. SD_IO_WriteByte(SD_DUMMY_BYTE);
  426. /* Return the reponse */
  427. return retr;
  428. }
  429. /**
  430. * @brief Erases the specified memory area of the given SD card.
  431. * @param StartAddr: Start address in Blocks (Size of a block is 512bytes)
  432. * @param EndAddr: End address in Blocks (Size of a block is 512bytes)
  433. * @retval SD status
  434. */
  435. uint8_t BSP_SD_Erase(uint32_t StartAddr, uint32_t EndAddr)
  436. {
  437. uint8_t retr = BSP_SD_ERROR;
  438. SD_CmdAnswer_typedef response;
  439. uint16_t BlockSize = 512;
  440. /* Send CMD32 (Erase group start) and check if the SD acknowledged the erase command: R1 response (0x00: no errors) */
  441. response = SD_SendCmd(SD_CMD_SD_ERASE_GRP_START, (StartAddr) * (flag_SDHC == 1 ? 1 : BlockSize), 0xFF, SD_ANSWER_R1_EXPECTED);
  442. SD_IO_CSState(1);
  443. SD_IO_WriteByte(SD_DUMMY_BYTE); if (response.r1 == SD_R1_NO_ERROR)
  444. {
  445. /* Send CMD33 (Erase group end) and Check if the SD acknowledged the erase command: R1 response (0x00: no errors) */
  446. response = SD_SendCmd(SD_CMD_SD_ERASE_GRP_END, (EndAddr*512) * (flag_SDHC == 1 ? 1 : BlockSize), 0xFF, SD_ANSWER_R1_EXPECTED);
  447. SD_IO_CSState(1);
  448. SD_IO_WriteByte(SD_DUMMY_BYTE);
  449. if (response.r1 == SD_R1_NO_ERROR)
  450. {
  451. /* Send CMD38 (Erase) and Check if the SD acknowledged the erase command: R1 response (0x00: no errors) */
  452. response = SD_SendCmd(SD_CMD_ERASE, 0, 0xFF, SD_ANSWER_R1B_EXPECTED);
  453. if (response.r1 == SD_R1_NO_ERROR)
  454. {
  455. retr = BSP_SD_OK;
  456. }
  457. SD_IO_CSState(1);
  458. SD_IO_WriteByte(SD_DUMMY_BYTE);
  459. }
  460. }
  461. /* Return the reponse */
  462. return retr;
  463. }
  464. /**
  465. * @brief Returns the SD status.
  466. * @param None
  467. * @retval The SD status.
  468. */
  469. uint8_t BSP_SD_GetCardState(void)
  470. {
  471. SD_CmdAnswer_typedef retr;
  472. /* Send CMD13 (SD_SEND_STATUS) to get SD status */
  473. retr = SD_SendCmd(SD_CMD_SEND_STATUS, 0, 0xFF, SD_ANSWER_R2_EXPECTED);
  474. SD_IO_CSState(1);
  475. SD_IO_WriteByte(SD_DUMMY_BYTE);
  476. /* Find SD status according to card state */
  477. if(( retr.r1 == SD_R1_NO_ERROR) && ( retr.r2 == SD_R2_NO_ERROR))
  478. {
  479. return BSP_SD_OK;
  480. }
  481. return BSP_SD_ERROR;
  482. }
  483. /**
  484. * @brief Reads the SD card SCD register.
  485. * Reading the contents of the CSD register in SPI mode is a simple
  486. * read-block transaction.
  487. * @param Csd: pointer on an SCD register structure
  488. * @retval SD status
  489. */
  490. uint8_t SD_GetCSDRegister(SD_CSD* Csd)
  491. {
  492. uint16_t counter = 0;
  493. uint8_t CSD_Tab[16];
  494. uint8_t retr = BSP_SD_ERROR;
  495. SD_CmdAnswer_typedef response;
  496. /* Send CMD9 (CSD register) or CMD10(CSD register) and Wait for response in the R1 format (0x00 is no errors) */
  497. response = SD_SendCmd(SD_CMD_SEND_CSD, 0, 0xFF, SD_ANSWER_R1_EXPECTED);
  498. if(response.r1 == SD_R1_NO_ERROR)
  499. {
  500. if (SD_WaitData(SD_TOKEN_START_DATA_SINGLE_BLOCK_READ) == BSP_SD_OK)
  501. {
  502. for (counter = 0; counter < 16; counter++)
  503. {
  504. /* Store CSD register value on CSD_Tab */
  505. CSD_Tab[counter] = SD_IO_WriteByte(SD_DUMMY_BYTE);
  506. }
  507. /* Get CRC bytes (not really needed by us, but required by SD) */
  508. SD_IO_WriteByte(SD_DUMMY_BYTE);
  509. SD_IO_WriteByte(SD_DUMMY_BYTE);
  510. /*************************************************************************
  511. CSD header decoding
  512. *************************************************************************/
  513. /* Byte 0 */
  514. Csd->CSDStruct = (CSD_Tab[0] & 0xC0) >> 6;
  515. Csd->Reserved1 = CSD_Tab[0] & 0x3F;
  516. /* Byte 1 */
  517. Csd->TAAC = CSD_Tab[1];
  518. /* Byte 2 */
  519. Csd->NSAC = CSD_Tab[2];
  520. /* Byte 3 */
  521. Csd->MaxBusClkFrec = CSD_Tab[3];
  522. /* Byte 4/5 */
  523. Csd->CardComdClasses = (CSD_Tab[4] << 4) | ((CSD_Tab[5] & 0xF0) >> 4);
  524. Csd->RdBlockLen = CSD_Tab[5] & 0x0F;
  525. /* Byte 6 */
  526. Csd->PartBlockRead = (CSD_Tab[6] & 0x80) >> 7;
  527. Csd->WrBlockMisalign = (CSD_Tab[6] & 0x40) >> 6;
  528. Csd->RdBlockMisalign = (CSD_Tab[6] & 0x20) >> 5;
  529. Csd->DSRImpl = (CSD_Tab[6] & 0x10) >> 4;
  530. /*************************************************************************
  531. CSD v1/v2 decoding
  532. *************************************************************************/
  533. if(flag_SDHC == 0)
  534. {
  535. Csd->version.v1.Reserved1 = ((CSD_Tab[6] & 0x0C) >> 2);
  536. Csd->version.v1.DeviceSize = ((CSD_Tab[6] & 0x03) << 10)
  537. | (CSD_Tab[7] << 2)
  538. | ((CSD_Tab[8] & 0xC0) >> 6);
  539. Csd->version.v1.MaxRdCurrentVDDMin = (CSD_Tab[8] & 0x38) >> 3;
  540. Csd->version.v1.MaxRdCurrentVDDMax = (CSD_Tab[8] & 0x07);
  541. Csd->version.v1.MaxWrCurrentVDDMin = (CSD_Tab[9] & 0xE0) >> 5;
  542. Csd->version.v1.MaxWrCurrentVDDMax = (CSD_Tab[9] & 0x1C) >> 2;
  543. Csd->version.v1.DeviceSizeMul = ((CSD_Tab[9] & 0x03) << 1)
  544. |((CSD_Tab[10] & 0x80) >> 7);
  545. }
  546. else
  547. {
  548. Csd->version.v2.Reserved1 = ((CSD_Tab[6] & 0x0F) << 2) | ((CSD_Tab[7] & 0xC0) >> 6);
  549. Csd->version.v2.DeviceSize= ((CSD_Tab[7] & 0x3F) << 16) | (CSD_Tab[8] << 8) | CSD_Tab[9];
  550. Csd->version.v2.Reserved2 = ((CSD_Tab[10] & 0x80) >> 8);
  551. }
  552. Csd->EraseSingleBlockEnable = (CSD_Tab[10] & 0x40) >> 6;
  553. Csd->EraseSectorSize = ((CSD_Tab[10] & 0x3F) << 1)
  554. |((CSD_Tab[11] & 0x80) >> 7);
  555. Csd->WrProtectGrSize = (CSD_Tab[11] & 0x7F);
  556. Csd->WrProtectGrEnable = (CSD_Tab[12] & 0x80) >> 7;
  557. Csd->Reserved2 = (CSD_Tab[12] & 0x60) >> 5;
  558. Csd->WrSpeedFact = (CSD_Tab[12] & 0x1C) >> 2;
  559. Csd->MaxWrBlockLen = ((CSD_Tab[12] & 0x03) << 2)
  560. |((CSD_Tab[13] & 0xC0) >> 6);
  561. Csd->WriteBlockPartial = (CSD_Tab[13] & 0x20) >> 5;
  562. Csd->Reserved3 = (CSD_Tab[13] & 0x1F);
  563. Csd->FileFormatGrouop = (CSD_Tab[14] & 0x80) >> 7;
  564. Csd->CopyFlag = (CSD_Tab[14] & 0x40) >> 6;
  565. Csd->PermWrProtect = (CSD_Tab[14] & 0x20) >> 5;
  566. Csd->TempWrProtect = (CSD_Tab[14] & 0x10) >> 4;
  567. Csd->FileFormat = (CSD_Tab[14] & 0x0C) >> 2;
  568. Csd->Reserved4 = (CSD_Tab[14] & 0x03);
  569. Csd->crc = (CSD_Tab[15] & 0xFE) >> 1;
  570. Csd->Reserved5 = (CSD_Tab[15] & 0x01);
  571. retr = BSP_SD_OK;
  572. }
  573. }
  574. /* Send dummy byte: 8 Clock pulses of delay */
  575. SD_IO_CSState(1);
  576. SD_IO_WriteByte(SD_DUMMY_BYTE);
  577. /* Return the reponse */
  578. return retr;
  579. }
  580. /**
  581. * @brief Reads the SD card CID register.
  582. * Reading the contents of the CID register in SPI mode is a simple
  583. * read-block transaction.
  584. * @param Cid: pointer on an CID register structure
  585. * @retval SD status
  586. */
  587. uint8_t SD_GetCIDRegister(SD_CID* Cid)
  588. {
  589. uint32_t counter = 0;
  590. uint8_t retr = BSP_SD_ERROR;
  591. uint8_t CID_Tab[16];
  592. SD_CmdAnswer_typedef response;
  593. /* Send CMD10 (CID register) and Wait for response in the R1 format (0x00 is no errors) */
  594. response = SD_SendCmd(SD_CMD_SEND_CID, 0, 0xFF, SD_ANSWER_R1_EXPECTED);
  595. if(response.r1 == SD_R1_NO_ERROR)
  596. {
  597. if(SD_WaitData(SD_TOKEN_START_DATA_SINGLE_BLOCK_READ) == BSP_SD_OK)
  598. {
  599. /* Store CID register value on CID_Tab */
  600. for (counter = 0; counter < 16; counter++)
  601. {
  602. CID_Tab[counter] = SD_IO_WriteByte(SD_DUMMY_BYTE);
  603. }
  604. /* Get CRC bytes (not really needed by us, but required by SD) */
  605. SD_IO_WriteByte(SD_DUMMY_BYTE);
  606. SD_IO_WriteByte(SD_DUMMY_BYTE);
  607. /* Byte 0 */
  608. Cid->ManufacturerID = CID_Tab[0];
  609. /* Byte 1 */
  610. Cid->OEM_AppliID = CID_Tab[1] << 8;
  611. /* Byte 2 */
  612. Cid->OEM_AppliID |= CID_Tab[2];
  613. /* Byte 3 */
  614. Cid->ProdName1 = CID_Tab[3] << 24;
  615. /* Byte 4 */
  616. Cid->ProdName1 |= CID_Tab[4] << 16;
  617. /* Byte 5 */
  618. Cid->ProdName1 |= CID_Tab[5] << 8;
  619. /* Byte 6 */
  620. Cid->ProdName1 |= CID_Tab[6];
  621. /* Byte 7 */
  622. Cid->ProdName2 = CID_Tab[7];
  623. /* Byte 8 */
  624. Cid->ProdRev = CID_Tab[8];
  625. /* Byte 9 */
  626. Cid->ProdSN = CID_Tab[9] << 24;
  627. /* Byte 10 */
  628. Cid->ProdSN |= CID_Tab[10] << 16;
  629. /* Byte 11 */
  630. Cid->ProdSN |= CID_Tab[11] << 8;
  631. /* Byte 12 */
  632. Cid->ProdSN |= CID_Tab[12];
  633. /* Byte 13 */
  634. Cid->Reserved1 |= (CID_Tab[13] & 0xF0) >> 4;
  635. Cid->ManufactDate = (CID_Tab[13] & 0x0F) << 8;
  636. /* Byte 14 */
  637. Cid->ManufactDate |= CID_Tab[14];
  638. /* Byte 15 */
  639. Cid->CID_CRC = (CID_Tab[15] & 0xFE) >> 1;
  640. Cid->Reserved2 = 1;
  641. retr = BSP_SD_OK;
  642. }
  643. }
  644. /* Send dummy byte: 8 Clock pulses of delay */
  645. SD_IO_CSState(1);
  646. SD_IO_WriteByte(SD_DUMMY_BYTE);
  647. /* Return the reponse */
  648. return retr;
  649. }
  650. /**
  651. * @brief Sends 5 bytes command to the SD card and get response
  652. * @param Cmd: The user expected command to send to SD card.
  653. * @param Arg: The command argument.
  654. * @param Crc: The CRC.
  655. * @param Answer: SD_ANSWER_NOT_EXPECTED or SD_ANSWER_EXPECTED
  656. * @retval SD status
  657. */
  658. SD_CmdAnswer_typedef SD_SendCmd(uint8_t Cmd, uint32_t Arg, uint8_t Crc, uint8_t Answer)
  659. {
  660. uint8_t frame[SD_CMD_LENGTH], frameout[SD_CMD_LENGTH];
  661. SD_CmdAnswer_typedef retr = {0xFF, 0xFF , 0xFF, 0xFF, 0xFF};
  662. /* R1 Lenght = NCS(0)+ 6 Bytes command + NCR(min1 max8) + 1 Bytes answer + NEC(0) = 15bytes */
  663. /* R1b identical to R1 + Busy information */
  664. /* R2 Lenght = NCS(0)+ 6 Bytes command + NCR(min1 max8) + 2 Bytes answer + NEC(0) = 16bytes */
  665. /* Prepare Frame to send */
  666. frame[0] = (Cmd | 0x40); /* Construct byte 1 */
  667. frame[1] = (uint8_t)(Arg >> 24); /* Construct byte 2 */
  668. frame[2] = (uint8_t)(Arg >> 16); /* Construct byte 3 */
  669. frame[3] = (uint8_t)(Arg >> 8); /* Construct byte 4 */
  670. frame[4] = (uint8_t)(Arg); /* Construct byte 5 */
  671. frame[5] = (Crc | 0x01); /* Construct byte 6 */
  672. /* Send the command */
  673. SD_IO_CSState(0);
  674. SD_IO_WriteReadData(frame, frameout, SD_CMD_LENGTH); /* Send the Cmd bytes */
  675. switch(Answer)
  676. {
  677. case SD_ANSWER_R1_EXPECTED :
  678. retr.r1 = SD_ReadData();
  679. break;
  680. case SD_ANSWER_R1B_EXPECTED :
  681. retr.r1 = SD_ReadData();
  682. retr.r2 = SD_IO_WriteByte(SD_DUMMY_BYTE);
  683. /* Set CS High */
  684. SD_IO_CSState(1);
  685. HAL_Delay(1);
  686. /* Set CS Low */
  687. SD_IO_CSState(0);
  688. /* Wait IO line return 0xFF */
  689. while (SD_IO_WriteByte(SD_DUMMY_BYTE) != 0xFF);
  690. break;
  691. case SD_ANSWER_R2_EXPECTED :
  692. retr.r1 = SD_ReadData();
  693. retr.r2 = SD_IO_WriteByte(SD_DUMMY_BYTE);
  694. break;
  695. case SD_ANSWER_R3_EXPECTED :
  696. case SD_ANSWER_R7_EXPECTED :
  697. retr.r1 = SD_ReadData();
  698. retr.r2 = SD_IO_WriteByte(SD_DUMMY_BYTE);
  699. retr.r3 = SD_IO_WriteByte(SD_DUMMY_BYTE);
  700. retr.r4 = SD_IO_WriteByte(SD_DUMMY_BYTE);
  701. retr.r5 = SD_IO_WriteByte(SD_DUMMY_BYTE);
  702. break;
  703. default :
  704. break;
  705. }
  706. return retr;
  707. }
  708. /**
  709. * @brief Gets the SD card data response and check the busy flag.
  710. * @param None
  711. * @retval The SD status: Read data response xxx0<status>1
  712. * - status 010: Data accecpted
  713. * - status 101: Data rejected due to a crc error
  714. * - status 110: Data rejected due to a Write error.
  715. * - status 111: Data rejected due to other error.
  716. */
  717. uint8_t SD_GetDataResponse(void)
  718. {
  719. uint8_t dataresponse;
  720. uint8_t rvalue = SD_DATA_OTHER_ERROR;
  721. dataresponse = SD_IO_WriteByte(SD_DUMMY_BYTE);
  722. SD_IO_WriteByte(SD_DUMMY_BYTE); /* read the busy response byte*/
  723. /* Mask unused bits */
  724. switch (dataresponse & 0x1F)
  725. {
  726. case SD_DATA_OK:
  727. rvalue = SD_DATA_OK;
  728. /* Set CS High */
  729. SD_IO_CSState(1);
  730. /* Set CS Low */
  731. SD_IO_CSState(0);
  732. /* Wait IO line return 0xFF */
  733. while (SD_IO_WriteByte(SD_DUMMY_BYTE) != 0xFF);
  734. break;
  735. case SD_DATA_CRC_ERROR:
  736. rvalue = SD_DATA_CRC_ERROR;
  737. break;
  738. case SD_DATA_WRITE_ERROR:
  739. rvalue = SD_DATA_WRITE_ERROR;
  740. break;
  741. default:
  742. break;
  743. }
  744. /* Return response */
  745. return rvalue;
  746. }
  747. /**
  748. * @brief Put the SD in Idle state.
  749. * @param None
  750. * @retval SD status
  751. */
  752. uint8_t SD_GoIdleState(void)
  753. {
  754. SD_CmdAnswer_typedef response;
  755. __IO uint8_t counter = 0;
  756. /* Send CMD0 (SD_CMD_GO_IDLE_STATE) to put SD in SPI mode and
  757. wait for In Idle State Response (R1 Format) equal to 0x01 */
  758. do{
  759. counter++;
  760. response = SD_SendCmd(SD_CMD_GO_IDLE_STATE, 0, 0x95, SD_ANSWER_R1_EXPECTED);
  761. SD_IO_CSState(1);
  762. SD_IO_WriteByte(SD_DUMMY_BYTE);
  763. if(counter >= SD_MAX_TRY)
  764. {
  765. return BSP_SD_ERROR;
  766. }
  767. }
  768. while(response.r1 != SD_R1_IN_IDLE_STATE);
  769. /* Send CMD8 (SD_CMD_SEND_IF_COND) to check the power supply status
  770. and wait until response (R7 Format) equal to 0xAA and */
  771. response = SD_SendCmd(SD_CMD_SEND_IF_COND, 0x1AA, 0x87, SD_ANSWER_R7_EXPECTED);
  772. SD_IO_CSState(1);
  773. SD_IO_WriteByte(SD_DUMMY_BYTE);
  774. if((response.r1 & SD_R1_ILLEGAL_COMMAND) == SD_R1_ILLEGAL_COMMAND)
  775. {
  776. /* initialise card V1 */
  777. do
  778. {
  779. /* initialise card V1 */
  780. /* Send CMD55 (SD_CMD_APP_CMD) before any ACMD command: R1 response (0x00: no errors) */
  781. response = SD_SendCmd(SD_CMD_APP_CMD, 0x00000000, 0xFF, SD_ANSWER_R1_EXPECTED);
  782. SD_IO_CSState(1);
  783. SD_IO_WriteByte(SD_DUMMY_BYTE);
  784. /* Send ACMD41 (SD_CMD_SD_APP_OP_COND) to initialize SDHC or SDXC cards: R1 response (0x00: no errors) */
  785. response = SD_SendCmd(SD_CMD_SD_APP_OP_COND, 0x00000000, 0xFF, SD_ANSWER_R1_EXPECTED);
  786. SD_IO_CSState(1);
  787. SD_IO_WriteByte(SD_DUMMY_BYTE);
  788. }
  789. while(response.r1 == SD_R1_IN_IDLE_STATE);
  790. flag_SDHC = 0;
  791. }
  792. else if(response.r1 == SD_R1_IN_IDLE_STATE)
  793. {
  794. /* initialise card V2 */
  795. do {
  796. /* Send CMD55 (SD_CMD_APP_CMD) before any ACMD command: R1 response (0x00: no errors) */
  797. response = SD_SendCmd(SD_CMD_APP_CMD, 0, 0xFF, SD_ANSWER_R1_EXPECTED);
  798. SD_IO_CSState(1);
  799. SD_IO_WriteByte(SD_DUMMY_BYTE);
  800. /* Send ACMD41 (SD_CMD_SD_APP_OP_COND) to initialize SDHC or SDXC cards: R1 response (0x00: no errors) */
  801. response = SD_SendCmd(SD_CMD_SD_APP_OP_COND, 0x40000000, 0xFF, SD_ANSWER_R1_EXPECTED);
  802. SD_IO_CSState(1);
  803. SD_IO_WriteByte(SD_DUMMY_BYTE);
  804. }
  805. while(response.r1 == SD_R1_IN_IDLE_STATE);
  806. if((response.r1 & SD_R1_ILLEGAL_COMMAND) == SD_R1_ILLEGAL_COMMAND)
  807. {
  808. do {
  809. /* Send CMD55 (SD_CMD_APP_CMD) before any ACMD command: R1 response (0x00: no errors) */
  810. response = SD_SendCmd(SD_CMD_APP_CMD, 0, 0xFF, SD_ANSWER_R1_EXPECTED);
  811. SD_IO_CSState(1);
  812. SD_IO_WriteByte(SD_DUMMY_BYTE);
  813. if(response.r1 != SD_R1_IN_IDLE_STATE)
  814. {
  815. return BSP_SD_ERROR;
  816. }
  817. /* Send ACMD41 (SD_CMD_SD_APP_OP_COND) to initialize SDHC or SDXC cards: R1 response (0x00: no errors) */
  818. response = SD_SendCmd(SD_CMD_SD_APP_OP_COND, 0x00000000, 0xFF, SD_ANSWER_R1_EXPECTED);
  819. SD_IO_CSState(1);
  820. SD_IO_WriteByte(SD_DUMMY_BYTE);
  821. }
  822. while(response.r1 == SD_R1_IN_IDLE_STATE);
  823. }
  824. /* Send CMD58 (SD_CMD_READ_OCR) to initialize SDHC or SDXC cards: R3 response (0x00: no errors) */
  825. response = SD_SendCmd(SD_CMD_READ_OCR, 0x00000000, 0xFF, SD_ANSWER_R3_EXPECTED);
  826. SD_IO_CSState(1);
  827. SD_IO_WriteByte(SD_DUMMY_BYTE);
  828. if(response.r1 != SD_R1_NO_ERROR)
  829. {
  830. return BSP_SD_ERROR;
  831. }
  832. flag_SDHC = (response.r2 & 0x40) >> 6;
  833. }
  834. else
  835. {
  836. return BSP_SD_ERROR;
  837. }
  838. return BSP_SD_OK;
  839. }
  840. /**
  841. * @brief Waits a data until a value different from SD_DUMMY_BITE
  842. * @param None
  843. * @retval the value read
  844. */
  845. uint8_t SD_ReadData(void)
  846. {
  847. uint8_t timeout = 0x08;
  848. uint8_t readvalue;
  849. /* Check if response is got or a timeout is happen */
  850. do {
  851. readvalue = SD_IO_WriteByte(SD_DUMMY_BYTE);
  852. timeout--;
  853. }while ((readvalue == SD_DUMMY_BYTE) && timeout);
  854. /* Right response got */
  855. return readvalue;
  856. }
  857. /**
  858. * @brief Waits a data from the SD card
  859. * @param data : Expected data from the SD card
  860. * @retval BSP_SD_OK or BSP_SD_TIMEOUT
  861. */
  862. uint8_t SD_WaitData(uint8_t data)
  863. {
  864. uint16_t timeout = 0xFFFF;
  865. uint8_t readvalue;
  866. /* Check if response is got or a timeout is happen */
  867. do {
  868. readvalue = SD_IO_WriteByte(SD_DUMMY_BYTE);
  869. timeout--;
  870. }while ((readvalue != data) && timeout);
  871. if (timeout == 0)
  872. {
  873. /* After time out */
  874. return BSP_SD_TIMEOUT;
  875. }
  876. /* Right response got */
  877. return BSP_SD_OK;
  878. }
  879. /**
  880. * @}
  881. */
  882. /**
  883. * @}
  884. */
  885. /**
  886. * @}
  887. */
  888. /**
  889. * @}
  890. */
  891. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/