stm32_adafruit_sd.c 37 KB

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