stm32_adafruit_sd.c 36 KB

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