stm32_adafruit_sd.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020
  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. #include "spi.h"
  89. #include "api-hal-spi.h"
  90. /** @addtogroup BSP
  91. * @{
  92. */
  93. /** @addtogroup STM32_ADAFRUIT
  94. * @{
  95. */
  96. /** @defgroup STM32_ADAFRUIT_SD
  97. * @{
  98. */
  99. /* Private typedef -----------------------------------------------------------*/
  100. /** @defgroup STM32_ADAFRUIT_SD_Private_Types_Definitions
  101. * @{
  102. */
  103. typedef struct {
  104. uint8_t r1;
  105. uint8_t r2;
  106. uint8_t r3;
  107. uint8_t r4;
  108. uint8_t r5;
  109. } SD_CmdAnswer_typedef;
  110. /**
  111. * @}
  112. */
  113. /* Private define ------------------------------------------------------------*/
  114. /** @defgroup STM32_ADAFRUIT_SD_Private_Defines
  115. * @{
  116. */
  117. #define SD_DUMMY_BYTE 0xFF
  118. #define SD_MAX_FRAME_LENGTH 17 /* Lenght = 16 + 1 */
  119. #define SD_CMD_LENGTH 6
  120. #define SD_MAX_TRY 100 /* Number of try */
  121. #define SD_CSD_STRUCT_V1 0x2 /* CSD struct version V1 */
  122. #define SD_CSD_STRUCT_V2 0x1 /* CSD struct version V2 */
  123. /**
  124. * @brief SD ansewer format
  125. */
  126. typedef enum {
  127. SD_ANSWER_R1_EXPECTED,
  128. SD_ANSWER_R1B_EXPECTED,
  129. SD_ANSWER_R2_EXPECTED,
  130. SD_ANSWER_R3_EXPECTED,
  131. SD_ANSWER_R4R5_EXPECTED,
  132. SD_ANSWER_R7_EXPECTED,
  133. } SD_Answer_type;
  134. /**
  135. * @brief Start Data tokens:
  136. * Tokens (necessary because at nop/idle (and CS active) only 0xff is
  137. * on the data/command line)
  138. */
  139. #define SD_TOKEN_START_DATA_SINGLE_BLOCK_READ \
  140. 0xFE /* Data token start byte, Start Single Block Read */
  141. #define SD_TOKEN_START_DATA_MULTIPLE_BLOCK_READ \
  142. 0xFE /* Data token start byte, Start Multiple Block Read */
  143. #define SD_TOKEN_START_DATA_SINGLE_BLOCK_WRITE \
  144. 0xFE /* Data token start byte, Start Single Block Write */
  145. #define SD_TOKEN_START_DATA_MULTIPLE_BLOCK_WRITE \
  146. 0xFD /* Data token start byte, Start Multiple Block Write */
  147. #define SD_TOKEN_STOP_DATA_MULTIPLE_BLOCK_WRITE \
  148. 0xFD /* Data toke stop byte, Stop Multiple Block Write */
  149. /**
  150. * @brief Commands: CMDxx = CMD-number | 0x40
  151. */
  152. #define SD_CMD_GO_IDLE_STATE 0 /* CMD0 = 0x40 */
  153. #define SD_CMD_SEND_OP_COND 1 /* CMD1 = 0x41 */
  154. #define SD_CMD_SEND_IF_COND 8 /* CMD8 = 0x48 */
  155. #define SD_CMD_SEND_CSD 9 /* CMD9 = 0x49 */
  156. #define SD_CMD_SEND_CID 10 /* CMD10 = 0x4A */
  157. #define SD_CMD_STOP_TRANSMISSION 12 /* CMD12 = 0x4C */
  158. #define SD_CMD_SEND_STATUS 13 /* CMD13 = 0x4D */
  159. #define SD_CMD_SET_BLOCKLEN 16 /* CMD16 = 0x50 */
  160. #define SD_CMD_READ_SINGLE_BLOCK 17 /* CMD17 = 0x51 */
  161. #define SD_CMD_READ_MULT_BLOCK 18 /* CMD18 = 0x52 */
  162. #define SD_CMD_SET_BLOCK_COUNT 23 /* CMD23 = 0x57 */
  163. #define SD_CMD_WRITE_SINGLE_BLOCK 24 /* CMD24 = 0x58 */
  164. #define SD_CMD_WRITE_MULT_BLOCK 25 /* CMD25 = 0x59 */
  165. #define SD_CMD_PROG_CSD 27 /* CMD27 = 0x5B */
  166. #define SD_CMD_SET_WRITE_PROT 28 /* CMD28 = 0x5C */
  167. #define SD_CMD_CLR_WRITE_PROT 29 /* CMD29 = 0x5D */
  168. #define SD_CMD_SEND_WRITE_PROT 30 /* CMD30 = 0x5E */
  169. #define SD_CMD_SD_ERASE_GRP_START 32 /* CMD32 = 0x60 */
  170. #define SD_CMD_SD_ERASE_GRP_END 33 /* CMD33 = 0x61 */
  171. #define SD_CMD_UNTAG_SECTOR 34 /* CMD34 = 0x62 */
  172. #define SD_CMD_ERASE_GRP_START 35 /* CMD35 = 0x63 */
  173. #define SD_CMD_ERASE_GRP_END 36 /* CMD36 = 0x64 */
  174. #define SD_CMD_UNTAG_ERASE_GROUP 37 /* CMD37 = 0x65 */
  175. #define SD_CMD_ERASE 38 /* CMD38 = 0x66 */
  176. #define SD_CMD_SD_APP_OP_COND 41 /* CMD41 = 0x69 */
  177. #define SD_CMD_APP_CMD 55 /* CMD55 = 0x77 */
  178. #define SD_CMD_READ_OCR 58 /* CMD55 = 0x79 */
  179. /**
  180. * @brief SD reponses and error flags
  181. */
  182. typedef enum {
  183. /* R1 answer value */
  184. SD_R1_NO_ERROR = (0x00),
  185. SD_R1_IN_IDLE_STATE = (0x01),
  186. SD_R1_ERASE_RESET = (0x02),
  187. SD_R1_ILLEGAL_COMMAND = (0x04),
  188. SD_R1_COM_CRC_ERROR = (0x08),
  189. SD_R1_ERASE_SEQUENCE_ERROR = (0x10),
  190. SD_R1_ADDRESS_ERROR = (0x20),
  191. SD_R1_PARAMETER_ERROR = (0x40),
  192. /* R2 answer value */
  193. SD_R2_NO_ERROR = 0x00,
  194. SD_R2_CARD_LOCKED = 0x01,
  195. SD_R2_LOCKUNLOCK_ERROR = 0x02,
  196. SD_R2_ERROR = 0x04,
  197. SD_R2_CC_ERROR = 0x08,
  198. SD_R2_CARD_ECC_FAILED = 0x10,
  199. SD_R2_WP_VIOLATION = 0x20,
  200. SD_R2_ERASE_PARAM = 0x40,
  201. SD_R2_OUTOFRANGE = 0x80,
  202. /**
  203. * @brief Data response error
  204. */
  205. SD_DATA_OK = (0x05),
  206. SD_DATA_CRC_ERROR = (0x0B),
  207. SD_DATA_WRITE_ERROR = (0x0D),
  208. SD_DATA_OTHER_ERROR = (0xFF)
  209. } SD_Error;
  210. /**
  211. * @}
  212. */
  213. /* Private macro -------------------------------------------------------------*/
  214. /** @defgroup STM32_ADAFRUIT_SD_Private_Macros
  215. * @{
  216. */
  217. /**
  218. * @}
  219. */
  220. /* Private variables ---------------------------------------------------------*/
  221. /** @defgroup STM32_ADAFRUIT_SD_Private_Variables
  222. * @{
  223. */
  224. __IO uint8_t SdStatus = SD_NOT_PRESENT;
  225. /* flag_SDHC :
  226. 0 : Standard capacity
  227. 1 : High capacity
  228. */
  229. uint16_t flag_SDHC = 0;
  230. /**
  231. * @}
  232. */
  233. /* Private function prototypes -----------------------------------------------*/
  234. static uint8_t SD_GetCIDRegister(SD_CID* Cid);
  235. static uint8_t SD_GetCSDRegister(SD_CSD* Csd);
  236. static uint8_t SD_GetDataResponse(void);
  237. static uint8_t SD_GoIdleState(void);
  238. static SD_CmdAnswer_typedef SD_SendCmd(uint8_t Cmd, uint32_t Arg, uint8_t Crc, uint8_t Answer);
  239. static uint8_t SD_WaitData(uint8_t data);
  240. static uint8_t SD_ReadData(void);
  241. /** @defgroup STM32_ADAFRUIT_SD_Private_Function_Prototypes
  242. * @{
  243. */
  244. /**
  245. * @}
  246. */
  247. /* Private functions ---------------------------------------------------------*/
  248. /** @defgroup STM32_ADAFRUIT_SD_Private_Functions
  249. * @{
  250. */
  251. /**
  252. * @brief Initializes the SD/SD communication.
  253. * @param None
  254. * @retval The SD Response:
  255. * - MSD_ERROR: Sequence failed
  256. * - MSD_OK: Sequence succeed
  257. */
  258. uint8_t BSP_SD_Init(void) {
  259. // TODO: SPI manager
  260. api_hal_spi_lock(&SPI_SD_HANDLE);
  261. /* Init to maximum slow speed */
  262. SD_SPI_Reconfigure_Slow();
  263. /* Configure IO functionalities for SD pin */
  264. SD_IO_Init();
  265. /* SD detection pin is not physically mapped on the Adafruit shield */
  266. SdStatus = SD_PRESENT;
  267. uint8_t res = SD_GoIdleState();
  268. /* Init to maximum fastest speed */
  269. SD_SPI_Reconfigure_Fast();
  270. // TODO: SPI manager
  271. api_hal_spi_unlock(&SPI_SD_HANDLE);
  272. /* SD initialized and set to SPI mode properly */
  273. return res;
  274. }
  275. /**
  276. * @brief Returns information about specific card.
  277. * @param pCardInfo: Pointer to a SD_CardInfo structure that contains all SD
  278. * card information.
  279. * @retval The SD Response:
  280. * - MSD_ERROR: Sequence failed
  281. * - MSD_OK: Sequence succeed
  282. */
  283. uint8_t BSP_SD_GetCardInfo(SD_CardInfo* pCardInfo) {
  284. uint8_t status;
  285. status = SD_GetCSDRegister(&(pCardInfo->Csd));
  286. status |= SD_GetCIDRegister(&(pCardInfo->Cid));
  287. if(flag_SDHC == 1) {
  288. pCardInfo->LogBlockSize = 512;
  289. pCardInfo->CardBlockSize = 512;
  290. pCardInfo->CardCapacity = ((uint64_t)pCardInfo->Csd.version.v2.DeviceSize + 1UL) * 1024UL *
  291. (uint64_t)pCardInfo->LogBlockSize;
  292. pCardInfo->LogBlockNbr = (pCardInfo->CardCapacity) / (pCardInfo->LogBlockSize);
  293. } else {
  294. pCardInfo->CardCapacity = (pCardInfo->Csd.version.v1.DeviceSize + 1);
  295. pCardInfo->CardCapacity *= (1 << (pCardInfo->Csd.version.v1.DeviceSizeMul + 2));
  296. pCardInfo->LogBlockSize = 512;
  297. pCardInfo->CardBlockSize = 1 << (pCardInfo->Csd.RdBlockLen);
  298. pCardInfo->CardCapacity *= pCardInfo->CardBlockSize;
  299. pCardInfo->LogBlockNbr = (pCardInfo->CardCapacity) / (pCardInfo->LogBlockSize);
  300. }
  301. return status;
  302. }
  303. /**
  304. * @brief Reads block(s) from a specified address in the SD card, in polling mode.
  305. * @param pData: Pointer to the buffer that will contain the data to transmit
  306. * @param ReadAddr: Address from where data is to be read. The address is counted
  307. * in blocks of 512bytes
  308. * @param NumOfBlocks: Number of SD blocks to read
  309. * @param Timeout: This parameter is used for compatibility with BSP implementation
  310. * @retval SD status
  311. */
  312. uint8_t
  313. BSP_SD_ReadBlocks(uint32_t* pData, uint32_t ReadAddr, uint32_t NumOfBlocks, uint32_t Timeout) {
  314. uint32_t offset = 0;
  315. uint8_t retr = BSP_SD_ERROR;
  316. SD_CmdAnswer_typedef response;
  317. uint16_t BlockSize = 512;
  318. uint8_t* ptr = NULL;
  319. // uint8_t ptr[512];
  320. /* Send CMD16 (SD_CMD_SET_BLOCKLEN) to set the size of the block and
  321. Check if the SD acknowledged the set block length command: R1 response (0x00: no errors) */
  322. response = SD_SendCmd(SD_CMD_SET_BLOCKLEN, BlockSize, 0xFF, SD_ANSWER_R1_EXPECTED);
  323. SD_IO_CSState(1);
  324. SD_IO_WriteByte(SD_DUMMY_BYTE);
  325. if(response.r1 != SD_R1_NO_ERROR) {
  326. goto error;
  327. }
  328. ptr = malloc(sizeof(uint8_t) * BlockSize);
  329. if(ptr == NULL) {
  330. goto error;
  331. }
  332. memset(ptr, SD_DUMMY_BYTE, sizeof(uint8_t) * BlockSize);
  333. /* Data transfer */
  334. while(NumOfBlocks--) {
  335. /* Send CMD17 (SD_CMD_READ_SINGLE_BLOCK) to read one block */
  336. /* Check if the SD acknowledged the read block command: R1 response (0x00: no errors) */
  337. response = SD_SendCmd(
  338. SD_CMD_READ_SINGLE_BLOCK,
  339. (ReadAddr + offset) * (flag_SDHC == 1 ? 1 : BlockSize),
  340. 0xFF,
  341. SD_ANSWER_R1_EXPECTED);
  342. if(response.r1 != SD_R1_NO_ERROR) {
  343. goto error;
  344. }
  345. /* Now look for the data token to signify the start of the data */
  346. if(SD_WaitData(SD_TOKEN_START_DATA_SINGLE_BLOCK_READ) == BSP_SD_OK) {
  347. /* Read the SD block data : read NumByteToRead data */
  348. SD_IO_WriteReadData(ptr, (uint8_t*)pData + offset, BlockSize);
  349. /* Set next read address*/
  350. offset += BlockSize;
  351. /* get CRC bytes (not really needed by us, but required by SD) */
  352. SD_IO_WriteByte(SD_DUMMY_BYTE);
  353. SD_IO_WriteByte(SD_DUMMY_BYTE);
  354. } else {
  355. goto error;
  356. }
  357. /* End the command data read cycle */
  358. SD_IO_CSState(1);
  359. SD_IO_WriteByte(SD_DUMMY_BYTE);
  360. }
  361. retr = BSP_SD_OK;
  362. error:
  363. /* Send dummy byte: 8 Clock pulses of delay */
  364. SD_IO_CSState(1);
  365. SD_IO_WriteByte(SD_DUMMY_BYTE);
  366. if(ptr != NULL) free(ptr);
  367. /* Return the reponse */
  368. return retr;
  369. }
  370. /**
  371. * @brief Writes block(s) to a specified address in the SD card, in polling mode.
  372. * @param pData: Pointer to the buffer that will contain the data to transmit
  373. * @param WriteAddr: Address from where data is to be written. The address is counted
  374. * in blocks of 512bytes
  375. * @param NumOfBlocks: Number of SD blocks to write
  376. * @param Timeout: This parameter is used for compatibility with BSP implementation
  377. * @retval SD status
  378. */
  379. uint8_t
  380. BSP_SD_WriteBlocks(uint32_t* pData, uint32_t WriteAddr, uint32_t NumOfBlocks, uint32_t Timeout) {
  381. uint32_t offset = 0;
  382. uint8_t retr = BSP_SD_ERROR;
  383. uint8_t* ptr = NULL;
  384. SD_CmdAnswer_typedef response;
  385. uint16_t BlockSize = 512;
  386. /* Send CMD16 (SD_CMD_SET_BLOCKLEN) to set the size of the block and
  387. Check if the SD acknowledged the set block length command: R1 response (0x00: no errors) */
  388. response = SD_SendCmd(SD_CMD_SET_BLOCKLEN, BlockSize, 0xFF, SD_ANSWER_R1_EXPECTED);
  389. SD_IO_CSState(1);
  390. SD_IO_WriteByte(SD_DUMMY_BYTE);
  391. if(response.r1 != SD_R1_NO_ERROR) {
  392. goto error;
  393. }
  394. ptr = malloc(sizeof(uint8_t) * BlockSize);
  395. if(ptr == NULL) {
  396. goto error;
  397. }
  398. /* Data transfer */
  399. while(NumOfBlocks--) {
  400. /* Send CMD24 (SD_CMD_WRITE_SINGLE_BLOCK) to write blocks and
  401. Check if the SD acknowledged the write block command: R1 response (0x00: no errors) */
  402. response = SD_SendCmd(
  403. SD_CMD_WRITE_SINGLE_BLOCK,
  404. (WriteAddr + offset) * (flag_SDHC == 1 ? 1 : BlockSize),
  405. 0xFF,
  406. SD_ANSWER_R1_EXPECTED);
  407. if(response.r1 != SD_R1_NO_ERROR) {
  408. goto error;
  409. }
  410. /* Send dummy byte for NWR timing : one byte between CMDWRITE and TOKEN */
  411. SD_IO_WriteByte(SD_DUMMY_BYTE);
  412. SD_IO_WriteByte(SD_DUMMY_BYTE);
  413. /* Send the data token to signify the start of the data */
  414. SD_IO_WriteByte(SD_TOKEN_START_DATA_SINGLE_BLOCK_WRITE);
  415. /* Write the block data to SD */
  416. SD_IO_WriteReadData((uint8_t*)pData + offset, ptr, BlockSize);
  417. /* Set next write address */
  418. offset += BlockSize;
  419. /* Put CRC bytes (not really needed by us, but required by SD) */
  420. SD_IO_WriteByte(SD_DUMMY_BYTE);
  421. SD_IO_WriteByte(SD_DUMMY_BYTE);
  422. /* Read data response */
  423. if(SD_GetDataResponse() != SD_DATA_OK) {
  424. /* Set response value to failure */
  425. goto error;
  426. }
  427. SD_IO_CSState(1);
  428. SD_IO_WriteByte(SD_DUMMY_BYTE);
  429. }
  430. retr = BSP_SD_OK;
  431. error:
  432. if(ptr != NULL) free(ptr);
  433. /* Send dummy byte: 8 Clock pulses of delay */
  434. SD_IO_CSState(1);
  435. SD_IO_WriteByte(SD_DUMMY_BYTE);
  436. /* Return the reponse */
  437. return retr;
  438. }
  439. /**
  440. * @brief Erases the specified memory area of the given SD card.
  441. * @param StartAddr: Start address in Blocks (Size of a block is 512bytes)
  442. * @param EndAddr: End address in Blocks (Size of a block is 512bytes)
  443. * @retval SD status
  444. */
  445. uint8_t BSP_SD_Erase(uint32_t StartAddr, uint32_t EndAddr) {
  446. uint8_t retr = BSP_SD_ERROR;
  447. SD_CmdAnswer_typedef response;
  448. uint16_t BlockSize = 512;
  449. /* Send CMD32 (Erase group start) and check if the SD acknowledged the erase command: R1 response (0x00: no errors) */
  450. response = SD_SendCmd(
  451. SD_CMD_SD_ERASE_GRP_START,
  452. (StartAddr) * (flag_SDHC == 1 ? 1 : BlockSize),
  453. 0xFF,
  454. SD_ANSWER_R1_EXPECTED);
  455. SD_IO_CSState(1);
  456. SD_IO_WriteByte(SD_DUMMY_BYTE);
  457. if(response.r1 == SD_R1_NO_ERROR) {
  458. /* Send CMD33 (Erase group end) and Check if the SD acknowledged the erase command: R1 response (0x00: no errors) */
  459. response = SD_SendCmd(
  460. SD_CMD_SD_ERASE_GRP_END,
  461. (EndAddr * 512) * (flag_SDHC == 1 ? 1 : BlockSize),
  462. 0xFF,
  463. SD_ANSWER_R1_EXPECTED);
  464. SD_IO_CSState(1);
  465. SD_IO_WriteByte(SD_DUMMY_BYTE);
  466. if(response.r1 == SD_R1_NO_ERROR) {
  467. /* Send CMD38 (Erase) and Check if the SD acknowledged the erase command: R1 response (0x00: no errors) */
  468. response = SD_SendCmd(SD_CMD_ERASE, 0, 0xFF, SD_ANSWER_R1B_EXPECTED);
  469. if(response.r1 == SD_R1_NO_ERROR) {
  470. retr = BSP_SD_OK;
  471. }
  472. SD_IO_CSState(1);
  473. SD_IO_WriteByte(SD_DUMMY_BYTE);
  474. }
  475. }
  476. /* Return the reponse */
  477. return retr;
  478. }
  479. /**
  480. * @brief Returns the SD status.
  481. * @param None
  482. * @retval The SD status.
  483. */
  484. uint8_t BSP_SD_GetCardState(void) {
  485. SD_CmdAnswer_typedef retr;
  486. /* Send CMD13 (SD_SEND_STATUS) to get SD status */
  487. retr = SD_SendCmd(SD_CMD_SEND_STATUS, 0, 0xFF, SD_ANSWER_R2_EXPECTED);
  488. SD_IO_CSState(1);
  489. SD_IO_WriteByte(SD_DUMMY_BYTE);
  490. /* Find SD status according to card state */
  491. if((retr.r1 == SD_R1_NO_ERROR) && (retr.r2 == SD_R2_NO_ERROR)) {
  492. return BSP_SD_OK;
  493. }
  494. return BSP_SD_ERROR;
  495. }
  496. /**
  497. * @brief Reads the SD card SCD register.
  498. * Reading the contents of the CSD register in SPI mode is a simple
  499. * read-block transaction.
  500. * @param Csd: pointer on an SCD register structure
  501. * @retval SD status
  502. */
  503. uint8_t SD_GetCSDRegister(SD_CSD* Csd) {
  504. uint16_t counter = 0;
  505. uint8_t CSD_Tab[16];
  506. uint8_t retr = BSP_SD_ERROR;
  507. SD_CmdAnswer_typedef response;
  508. /* Send CMD9 (CSD register) or CMD10(CSD register) and Wait for response in the R1 format (0x00 is no errors) */
  509. response = SD_SendCmd(SD_CMD_SEND_CSD, 0, 0xFF, SD_ANSWER_R1_EXPECTED);
  510. if(response.r1 == SD_R1_NO_ERROR) {
  511. if(SD_WaitData(SD_TOKEN_START_DATA_SINGLE_BLOCK_READ) == BSP_SD_OK) {
  512. for(counter = 0; counter < 16; counter++) {
  513. /* Store CSD register value on CSD_Tab */
  514. CSD_Tab[counter] = SD_IO_WriteByte(SD_DUMMY_BYTE);
  515. }
  516. /* Get CRC bytes (not really needed by us, but required by SD) */
  517. SD_IO_WriteByte(SD_DUMMY_BYTE);
  518. SD_IO_WriteByte(SD_DUMMY_BYTE);
  519. /*************************************************************************
  520. CSD header decoding
  521. *************************************************************************/
  522. /* Byte 0 */
  523. Csd->CSDStruct = (CSD_Tab[0] & 0xC0) >> 6;
  524. Csd->Reserved1 = CSD_Tab[0] & 0x3F;
  525. /* Byte 1 */
  526. Csd->TAAC = CSD_Tab[1];
  527. /* Byte 2 */
  528. Csd->NSAC = CSD_Tab[2];
  529. /* Byte 3 */
  530. Csd->MaxBusClkFrec = CSD_Tab[3];
  531. /* Byte 4/5 */
  532. Csd->CardComdClasses = (CSD_Tab[4] << 4) | ((CSD_Tab[5] & 0xF0) >> 4);
  533. Csd->RdBlockLen = CSD_Tab[5] & 0x0F;
  534. /* Byte 6 */
  535. Csd->PartBlockRead = (CSD_Tab[6] & 0x80) >> 7;
  536. Csd->WrBlockMisalign = (CSD_Tab[6] & 0x40) >> 6;
  537. Csd->RdBlockMisalign = (CSD_Tab[6] & 0x20) >> 5;
  538. Csd->DSRImpl = (CSD_Tab[6] & 0x10) >> 4;
  539. /*************************************************************************
  540. CSD v1/v2 decoding
  541. *************************************************************************/
  542. if(flag_SDHC == 0) {
  543. Csd->version.v1.Reserved1 = ((CSD_Tab[6] & 0x0C) >> 2);
  544. Csd->version.v1.DeviceSize = ((CSD_Tab[6] & 0x03) << 10) | (CSD_Tab[7] << 2) |
  545. ((CSD_Tab[8] & 0xC0) >> 6);
  546. Csd->version.v1.MaxRdCurrentVDDMin = (CSD_Tab[8] & 0x38) >> 3;
  547. Csd->version.v1.MaxRdCurrentVDDMax = (CSD_Tab[8] & 0x07);
  548. Csd->version.v1.MaxWrCurrentVDDMin = (CSD_Tab[9] & 0xE0) >> 5;
  549. Csd->version.v1.MaxWrCurrentVDDMax = (CSD_Tab[9] & 0x1C) >> 2;
  550. Csd->version.v1.DeviceSizeMul = ((CSD_Tab[9] & 0x03) << 1) |
  551. ((CSD_Tab[10] & 0x80) >> 7);
  552. } else {
  553. Csd->version.v2.Reserved1 = ((CSD_Tab[6] & 0x0F) << 2) |
  554. ((CSD_Tab[7] & 0xC0) >> 6);
  555. Csd->version.v2.DeviceSize = ((CSD_Tab[7] & 0x3F) << 16) | (CSD_Tab[8] << 8) |
  556. CSD_Tab[9];
  557. Csd->version.v2.Reserved2 = ((CSD_Tab[10] & 0x80) >> 8);
  558. }
  559. Csd->EraseSingleBlockEnable = (CSD_Tab[10] & 0x40) >> 6;
  560. Csd->EraseSectorSize = ((CSD_Tab[10] & 0x3F) << 1) | ((CSD_Tab[11] & 0x80) >> 7);
  561. Csd->WrProtectGrSize = (CSD_Tab[11] & 0x7F);
  562. Csd->WrProtectGrEnable = (CSD_Tab[12] & 0x80) >> 7;
  563. Csd->Reserved2 = (CSD_Tab[12] & 0x60) >> 5;
  564. Csd->WrSpeedFact = (CSD_Tab[12] & 0x1C) >> 2;
  565. Csd->MaxWrBlockLen = ((CSD_Tab[12] & 0x03) << 2) | ((CSD_Tab[13] & 0xC0) >> 6);
  566. Csd->WriteBlockPartial = (CSD_Tab[13] & 0x20) >> 5;
  567. Csd->Reserved3 = (CSD_Tab[13] & 0x1F);
  568. Csd->FileFormatGrouop = (CSD_Tab[14] & 0x80) >> 7;
  569. Csd->CopyFlag = (CSD_Tab[14] & 0x40) >> 6;
  570. Csd->PermWrProtect = (CSD_Tab[14] & 0x20) >> 5;
  571. Csd->TempWrProtect = (CSD_Tab[14] & 0x10) >> 4;
  572. Csd->FileFormat = (CSD_Tab[14] & 0x0C) >> 2;
  573. Csd->Reserved4 = (CSD_Tab[14] & 0x03);
  574. Csd->crc = (CSD_Tab[15] & 0xFE) >> 1;
  575. Csd->Reserved5 = (CSD_Tab[15] & 0x01);
  576. retr = BSP_SD_OK;
  577. }
  578. }
  579. /* Send dummy byte: 8 Clock pulses of delay */
  580. SD_IO_CSState(1);
  581. SD_IO_WriteByte(SD_DUMMY_BYTE);
  582. /* Return the reponse */
  583. return retr;
  584. }
  585. /**
  586. * @brief Reads the SD card CID register.
  587. * Reading the contents of the CID register in SPI mode is a simple
  588. * read-block transaction.
  589. * @param Cid: pointer on an CID register structure
  590. * @retval SD status
  591. */
  592. uint8_t SD_GetCIDRegister(SD_CID* Cid) {
  593. uint32_t counter = 0;
  594. uint8_t retr = BSP_SD_ERROR;
  595. uint8_t CID_Tab[16];
  596. SD_CmdAnswer_typedef response;
  597. /* Send CMD10 (CID register) and Wait for response in the R1 format (0x00 is no errors) */
  598. response = SD_SendCmd(SD_CMD_SEND_CID, 0, 0xFF, SD_ANSWER_R1_EXPECTED);
  599. if(response.r1 == SD_R1_NO_ERROR) {
  600. if(SD_WaitData(SD_TOKEN_START_DATA_SINGLE_BLOCK_READ) == BSP_SD_OK) {
  601. /* Store CID register value on CID_Tab */
  602. for(counter = 0; counter < 16; counter++) {
  603. CID_Tab[counter] = SD_IO_WriteByte(SD_DUMMY_BYTE);
  604. }
  605. /* Get CRC bytes (not really needed by us, but required by SD) */
  606. SD_IO_WriteByte(SD_DUMMY_BYTE);
  607. SD_IO_WriteByte(SD_DUMMY_BYTE);
  608. /* Byte 0 */
  609. Cid->ManufacturerID = CID_Tab[0];
  610. /* Byte 1 */
  611. Cid->OEM_AppliID = CID_Tab[1] << 8;
  612. /* Byte 2 */
  613. Cid->OEM_AppliID |= CID_Tab[2];
  614. /* Byte 3 */
  615. Cid->ProdName1 = CID_Tab[3] << 24;
  616. /* Byte 4 */
  617. Cid->ProdName1 |= CID_Tab[4] << 16;
  618. /* Byte 5 */
  619. Cid->ProdName1 |= CID_Tab[5] << 8;
  620. /* Byte 6 */
  621. Cid->ProdName1 |= CID_Tab[6];
  622. /* Byte 7 */
  623. Cid->ProdName2 = CID_Tab[7];
  624. /* Byte 8 */
  625. Cid->ProdRev = CID_Tab[8];
  626. /* Byte 9 */
  627. Cid->ProdSN = CID_Tab[9] << 24;
  628. /* Byte 10 */
  629. Cid->ProdSN |= CID_Tab[10] << 16;
  630. /* Byte 11 */
  631. Cid->ProdSN |= CID_Tab[11] << 8;
  632. /* Byte 12 */
  633. Cid->ProdSN |= CID_Tab[12];
  634. /* Byte 13 */
  635. Cid->Reserved1 |= (CID_Tab[13] & 0xF0) >> 4;
  636. Cid->ManufactDate = (CID_Tab[13] & 0x0F) << 8;
  637. /* Byte 14 */
  638. Cid->ManufactDate |= CID_Tab[14];
  639. /* Byte 15 */
  640. Cid->CID_CRC = (CID_Tab[15] & 0xFE) >> 1;
  641. Cid->Reserved2 = 1;
  642. retr = BSP_SD_OK;
  643. }
  644. }
  645. /* Send dummy byte: 8 Clock pulses of delay */
  646. SD_IO_CSState(1);
  647. SD_IO_WriteByte(SD_DUMMY_BYTE);
  648. /* Return the reponse */
  649. return retr;
  650. }
  651. /**
  652. * @brief Sends 5 bytes command to the SD card and get response
  653. * @param Cmd: The user expected command to send to SD card.
  654. * @param Arg: The command argument.
  655. * @param Crc: The CRC.
  656. * @param Answer: SD_ANSWER_NOT_EXPECTED or SD_ANSWER_EXPECTED
  657. * @retval SD status
  658. */
  659. SD_CmdAnswer_typedef SD_SendCmd(uint8_t Cmd, uint32_t Arg, uint8_t Crc, uint8_t Answer) {
  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. case SD_ANSWER_R1_EXPECTED:
  677. retr.r1 = SD_ReadData();
  678. break;
  679. case SD_ANSWER_R1B_EXPECTED:
  680. retr.r1 = SD_ReadData();
  681. retr.r2 = SD_IO_WriteByte(SD_DUMMY_BYTE);
  682. /* Set CS High */
  683. SD_IO_CSState(1);
  684. HAL_Delay(1);
  685. /* Set CS Low */
  686. SD_IO_CSState(0);
  687. /* Wait IO line return 0xFF */
  688. while(SD_IO_WriteByte(SD_DUMMY_BYTE) != 0xFF)
  689. ;
  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. uint8_t dataresponse;
  719. uint8_t rvalue = SD_DATA_OTHER_ERROR;
  720. dataresponse = SD_IO_WriteByte(SD_DUMMY_BYTE);
  721. SD_IO_WriteByte(SD_DUMMY_BYTE); /* read the busy response byte*/
  722. /* Mask unused bits */
  723. switch(dataresponse & 0x1F) {
  724. case SD_DATA_OK:
  725. rvalue = SD_DATA_OK;
  726. /* Set CS High */
  727. SD_IO_CSState(1);
  728. /* Set CS Low */
  729. SD_IO_CSState(0);
  730. /* Wait IO line return 0xFF */
  731. while(SD_IO_WriteByte(SD_DUMMY_BYTE) != 0xFF)
  732. ;
  733. break;
  734. case SD_DATA_CRC_ERROR:
  735. rvalue = SD_DATA_CRC_ERROR;
  736. break;
  737. case SD_DATA_WRITE_ERROR:
  738. rvalue = SD_DATA_WRITE_ERROR;
  739. break;
  740. default:
  741. break;
  742. }
  743. /* Return response */
  744. return rvalue;
  745. }
  746. /**
  747. * @brief Put the SD in Idle state.
  748. * @param None
  749. * @retval SD status
  750. */
  751. uint8_t SD_GoIdleState(void) {
  752. SD_CmdAnswer_typedef response;
  753. __IO uint8_t counter = 0;
  754. /* Send CMD0 (SD_CMD_GO_IDLE_STATE) to put SD in SPI mode and
  755. wait for In Idle State Response (R1 Format) equal to 0x01 */
  756. do {
  757. counter++;
  758. response = SD_SendCmd(SD_CMD_GO_IDLE_STATE, 0, 0x95, SD_ANSWER_R1_EXPECTED);
  759. SD_IO_CSState(1);
  760. SD_IO_WriteByte(SD_DUMMY_BYTE);
  761. if(counter >= SD_MAX_TRY) {
  762. return BSP_SD_ERROR;
  763. }
  764. } while(response.r1 != SD_R1_IN_IDLE_STATE);
  765. /* Send CMD8 (SD_CMD_SEND_IF_COND) to check the power supply status
  766. and wait until response (R7 Format) equal to 0xAA and */
  767. response = SD_SendCmd(SD_CMD_SEND_IF_COND, 0x1AA, 0x87, SD_ANSWER_R7_EXPECTED);
  768. SD_IO_CSState(1);
  769. SD_IO_WriteByte(SD_DUMMY_BYTE);
  770. if((response.r1 & SD_R1_ILLEGAL_COMMAND) == SD_R1_ILLEGAL_COMMAND) {
  771. /* initialise card V1 */
  772. do {
  773. /* initialise card V1 */
  774. /* Send CMD55 (SD_CMD_APP_CMD) before any ACMD command: R1 response (0x00: no errors) */
  775. response = SD_SendCmd(SD_CMD_APP_CMD, 0x00000000, 0xFF, SD_ANSWER_R1_EXPECTED);
  776. SD_IO_CSState(1);
  777. SD_IO_WriteByte(SD_DUMMY_BYTE);
  778. /* Send ACMD41 (SD_CMD_SD_APP_OP_COND) to initialize SDHC or SDXC cards: R1 response (0x00: no errors) */
  779. response = SD_SendCmd(SD_CMD_SD_APP_OP_COND, 0x00000000, 0xFF, SD_ANSWER_R1_EXPECTED);
  780. SD_IO_CSState(1);
  781. SD_IO_WriteByte(SD_DUMMY_BYTE);
  782. } while(response.r1 == SD_R1_IN_IDLE_STATE);
  783. flag_SDHC = 0;
  784. } else if(response.r1 == SD_R1_IN_IDLE_STATE) {
  785. /* initialise card V2 */
  786. do {
  787. /* Send CMD55 (SD_CMD_APP_CMD) before any ACMD command: R1 response (0x00: no errors) */
  788. response = SD_SendCmd(SD_CMD_APP_CMD, 0, 0xFF, SD_ANSWER_R1_EXPECTED);
  789. SD_IO_CSState(1);
  790. SD_IO_WriteByte(SD_DUMMY_BYTE);
  791. /* Send ACMD41 (SD_CMD_SD_APP_OP_COND) to initialize SDHC or SDXC cards: R1 response (0x00: no errors) */
  792. response = SD_SendCmd(SD_CMD_SD_APP_OP_COND, 0x40000000, 0xFF, SD_ANSWER_R1_EXPECTED);
  793. SD_IO_CSState(1);
  794. SD_IO_WriteByte(SD_DUMMY_BYTE);
  795. } while(response.r1 == SD_R1_IN_IDLE_STATE);
  796. if((response.r1 & SD_R1_ILLEGAL_COMMAND) == SD_R1_ILLEGAL_COMMAND) {
  797. do {
  798. /* Send CMD55 (SD_CMD_APP_CMD) before any ACMD command: R1 response (0x00: no errors) */
  799. response = SD_SendCmd(SD_CMD_APP_CMD, 0, 0xFF, SD_ANSWER_R1_EXPECTED);
  800. SD_IO_CSState(1);
  801. SD_IO_WriteByte(SD_DUMMY_BYTE);
  802. if(response.r1 != SD_R1_IN_IDLE_STATE) {
  803. return BSP_SD_ERROR;
  804. }
  805. /* Send ACMD41 (SD_CMD_SD_APP_OP_COND) to initialize SDHC or SDXC cards: R1 response (0x00: no errors) */
  806. response =
  807. SD_SendCmd(SD_CMD_SD_APP_OP_COND, 0x00000000, 0xFF, SD_ANSWER_R1_EXPECTED);
  808. SD_IO_CSState(1);
  809. SD_IO_WriteByte(SD_DUMMY_BYTE);
  810. } while(response.r1 == SD_R1_IN_IDLE_STATE);
  811. }
  812. /* Send CMD58 (SD_CMD_READ_OCR) to initialize SDHC or SDXC cards: R3 response (0x00: no errors) */
  813. response = SD_SendCmd(SD_CMD_READ_OCR, 0x00000000, 0xFF, SD_ANSWER_R3_EXPECTED);
  814. SD_IO_CSState(1);
  815. SD_IO_WriteByte(SD_DUMMY_BYTE);
  816. if(response.r1 != SD_R1_NO_ERROR) {
  817. return BSP_SD_ERROR;
  818. }
  819. flag_SDHC = (response.r2 & 0x40) >> 6;
  820. } else {
  821. return BSP_SD_ERROR;
  822. }
  823. return BSP_SD_OK;
  824. }
  825. /**
  826. * @brief Waits a data until a value different from SD_DUMMY_BITE
  827. * @param None
  828. * @retval the value read
  829. */
  830. uint8_t SD_ReadData(void) {
  831. uint8_t timeout = 0x08;
  832. uint8_t readvalue;
  833. /* Check if response is got or a timeout is happen */
  834. do {
  835. readvalue = SD_IO_WriteByte(SD_DUMMY_BYTE);
  836. timeout--;
  837. } while((readvalue == SD_DUMMY_BYTE) && timeout);
  838. /* Right response got */
  839. return readvalue;
  840. }
  841. /**
  842. * @brief Waits a data from the SD card
  843. * @param data : Expected data from the SD card
  844. * @retval BSP_SD_OK or BSP_SD_TIMEOUT
  845. */
  846. uint8_t SD_WaitData(uint8_t data) {
  847. uint16_t timeout = 0xFFFF;
  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 != data) && timeout);
  854. if(timeout == 0) {
  855. /* After time out */
  856. return BSP_SD_TIMEOUT;
  857. }
  858. /* Right response got */
  859. return BSP_SD_OK;
  860. }
  861. /**
  862. * @}
  863. */
  864. /**
  865. * @}
  866. */
  867. /**
  868. * @}
  869. */
  870. /**
  871. * @}
  872. */
  873. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/