stm32_adafruit_sd.c 35 KB

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