stm32_adafruit_sd.c 35 KB

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