stm32_adafruit_sd.c 37 KB

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