stm320518_eval_spi_sd.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913
  1. /**
  2. ******************************************************************************
  3. * @file stm320518_eval_spi_sd.c
  4. * @author MCD Application Team
  5. * @version V1.0.0
  6. * @date 20-April-2012
  7. * @brief This file provides a set of functions needed to manage the SPI SD
  8. * Card memory mounted on STM320518-EVAL board.
  9. * It implements a high level communication layer for read and write
  10. * from/to this memory. The needed STM32F0xx hardware resources (SPI and
  11. * GPIO) are defined in stm320518_eval.h file, and the initialization is
  12. * performed in SD_LowLevel_Init() function declared in stm320518_eval.c
  13. * file.
  14. * You can easily tailor this driver to any other development board,
  15. * by just adapting the defines for hardware resources and
  16. * SD_LowLevel_Init() function.
  17. *
  18. * ===================================================================
  19. * Notes:
  20. * - This driver is intended for STM32F0xx families devices only.
  21. * - This driver doesn't support SD High Capacity cards.
  22. * ===================================================================
  23. *
  24. * +-------------------------------------------------------+
  25. * | Pin assignment |
  26. * +-------------------------+---------------+-------------+
  27. * | STM32F0xx SPI Pins | SD | Pin |
  28. * +-------------------------+---------------+-------------+
  29. * | SD_SPI_CS_PIN | ChipSelect | 1 |
  30. * | SD_SPI_MOSI_PIN / MOSI | DataIn | 2 |
  31. * | | GND | 3 (0 V) |
  32. * | | VDD | 4 (3.3 V)|
  33. * | SD_SPI_SCK_PIN / SCLK | Clock | 5 |
  34. * | | GND | 6 (0 V) |
  35. * | SD_SPI_MISO_PIN / MISO | DataOut | 7 |
  36. * +-------------------------+---------------+-------------+
  37. ******************************************************************************
  38. * @attention
  39. *
  40. * <h2><center>&copy; COPYRIGHT 2012 STMicroelectronics</center></h2>
  41. *
  42. * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  43. * You may not use this file except in compliance with the License.
  44. * You may obtain a copy of the License at:
  45. *
  46. * http://www.st.com/software_license_agreement_liberty_v2
  47. *
  48. * Unless required by applicable law or agreed to in writing, software
  49. * distributed under the License is distributed on an "AS IS" BASIS,
  50. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  51. * See the License for the specific language governing permissions and
  52. * limitations under the License.
  53. *
  54. ******************************************************************************
  55. */
  56. /* Includes ------------------------------------------------------------------*/
  57. #include "stm320518_eval_spi_sd.h"
  58. /** @addtogroup Utilities
  59. * @{
  60. */
  61. /** @addtogroup STM32_EVAL
  62. * @{
  63. */
  64. /** @addtogroup STM320518_EVAL
  65. * @{
  66. */
  67. /** @addtogroup STM320518_EVAL_SPI_SD
  68. * @brief This file includes the SD card driver of STM320518-EVAL boards.
  69. * @{
  70. */
  71. /** @defgroup STM320518_EVAL_SPI_SD_Private_Types
  72. * @{
  73. */
  74. /**
  75. * @}
  76. */
  77. /** @defgroup STM320518_EVAL_SPI_SD_Private_Defines
  78. * @{
  79. */
  80. /**
  81. * @}
  82. */
  83. /** @defgroup STM320518_EVAL_SPI_SD_Private_Macros
  84. * @{
  85. */
  86. /**
  87. * @}
  88. */
  89. /** @defgroup STM320518_EVAL_SPI_SD_Private_Variables
  90. * @{
  91. */
  92. /**
  93. * @}
  94. */
  95. /** @defgroup STM320518_EVAL_SPI_SD_Private_Function_Prototypes
  96. * @{
  97. */
  98. /**
  99. * @}
  100. */
  101. /** @defgroup STM320518_EVAL_SPI_SD_Private_Functions
  102. * @{
  103. */
  104. /**
  105. * @brief DeInitializes the SD/SD communication.
  106. * @param None
  107. * @retval None
  108. */
  109. void SD_DeInit(void)
  110. {
  111. SD_LowLevel_DeInit();
  112. }
  113. /**
  114. * @brief Initializes the SD/SD communication.
  115. * @param None
  116. * @retval The SD Response:
  117. * - SD_RESPONSE_FAILURE: Sequence failed
  118. * - SD_RESPONSE_NO_ERROR: Sequence succeed
  119. */
  120. SD_Error SD_Init(void)
  121. {
  122. uint32_t i = 0;
  123. /*!< Initialize SD_SPI */
  124. SD_LowLevel_Init();
  125. /*!< SD chip select high */
  126. SD_CS_HIGH();
  127. /*!< Send dummy byte 0xFF, 10 times with CS high */
  128. /*!< Rise CS and MOSI for 80 clocks cycles */
  129. for (i = 0; i <= 9; i++)
  130. {
  131. /*!< Send dummy byte 0xFF */
  132. SD_WriteByte(SD_DUMMY_BYTE);
  133. }
  134. /*------------Put SD in SPI mode--------------*/
  135. /*!< SD initialized and set to SPI mode properly */
  136. return (SD_GoIdleState());
  137. }
  138. /**
  139. * @brief Detect if SD card is correctly plugged in the memory slot.
  140. * @param None
  141. * @retval Return if SD is detected or not
  142. */
  143. uint8_t SD_Detect(void)
  144. {
  145. __IO uint8_t status = SD_PRESENT;
  146. /*!< Check GPIO to detect SD */
  147. if (GPIO_ReadInputData(SD_DETECT_GPIO_PORT) & SD_DETECT_PIN)
  148. {
  149. status = SD_NOT_PRESENT;
  150. }
  151. return status;
  152. }
  153. /**
  154. * @brief Returns information about specific card.
  155. * @param cardinfo: pointer to a SD_CardInfo structure that contains all SD
  156. * card information.
  157. * @retval The SD Response:
  158. * - SD_RESPONSE_FAILURE: Sequence failed
  159. * - SD_RESPONSE_NO_ERROR: Sequence succeed
  160. */
  161. SD_Error SD_GetCardInfo(SD_CardInfo *cardinfo)
  162. {
  163. SD_Error status = SD_RESPONSE_FAILURE;
  164. SD_GetCSDRegister(&(cardinfo->SD_csd));
  165. status = SD_GetCIDRegister(&(cardinfo->SD_cid));
  166. cardinfo->CardCapacity = (cardinfo->SD_csd.DeviceSize + 1) ;
  167. cardinfo->CardCapacity *= (1 << (cardinfo->SD_csd.DeviceSizeMul + 2));
  168. cardinfo->CardBlockSize = 1 << (cardinfo->SD_csd.RdBlockLen);
  169. cardinfo->CardCapacity *= cardinfo->CardBlockSize;
  170. /*!< Returns the reponse */
  171. return status;
  172. }
  173. /**
  174. * @brief Reads a block of data from the SD.
  175. * @param pBuffer: pointer to the buffer that receives the data read from the SD card.
  176. * @param ReadAddr: SD's internal address to read from.
  177. * @param BlockSize: the SD card Data block size.
  178. * @retval The SD Response:
  179. * - SD_RESPONSE_FAILURE: Sequence failed
  180. * - SD_RESPONSE_NO_ERROR: Sequence succeed
  181. */
  182. SD_Error SD_ReadBlock(uint8_t* pBuffer, uint32_t ReadAddr, uint16_t BlockSize)
  183. {
  184. uint32_t i = 0;
  185. SD_Error rvalue = SD_RESPONSE_FAILURE;
  186. /*!< SD chip select low */
  187. SD_CS_LOW();
  188. /*!< Send CMD17 (SD_CMD_READ_SINGLE_BLOCK) to read one block */
  189. SD_SendCmd(SD_CMD_READ_SINGLE_BLOCK, ReadAddr, 0xFF);
  190. /*!< Check if the SD acknowledged the read block command: R1 response (0x00: no errors) */
  191. if (!SD_GetResponse(SD_RESPONSE_NO_ERROR))
  192. {
  193. /*!< Now look for the data token to signify the start of the data */
  194. if (!SD_GetResponse(SD_START_DATA_SINGLE_BLOCK_READ))
  195. {
  196. /*!< Read the SD block data : read NumByteToRead data */
  197. for (i = 0; i < BlockSize; i++)
  198. {
  199. /*!< Save the received data */
  200. *pBuffer = SD_ReadByte();
  201. /*!< Point to the next location where the byte read will be saved */
  202. pBuffer++;
  203. }
  204. /*!< Get CRC bytes (not really needed by us, but required by SD) */
  205. SD_ReadByte();
  206. SD_ReadByte();
  207. /*!< Set response value to success */
  208. rvalue = SD_RESPONSE_NO_ERROR;
  209. }
  210. }
  211. /*!< SD chip select high */
  212. SD_CS_HIGH();
  213. /*!< Send dummy byte: 8 Clock pulses of delay */
  214. SD_WriteByte(SD_DUMMY_BYTE);
  215. /*!< Returns the reponse */
  216. return rvalue;
  217. }
  218. /**
  219. * @brief Reads multiple block of data from the SD.
  220. * @param pBuffer: pointer to the buffer that receives the data read from the SD card.
  221. * @param ReadAddr: SD's internal address to read from.
  222. * @param BlockSize: the SD card Data block size.
  223. * @param NumberOfBlocks: number of blocks to be read.
  224. * @retval The SD Response:
  225. * - SD_RESPONSE_FAILURE: Sequence failed
  226. * - SD_RESPONSE_NO_ERROR: Sequence succeed
  227. */
  228. SD_Error SD_ReadMultiBlocks(uint8_t* pBuffer, uint32_t ReadAddr, uint16_t BlockSize, uint32_t NumberOfBlocks)
  229. {
  230. uint32_t i = 0, Offset = 0;
  231. SD_Error rvalue = SD_RESPONSE_FAILURE;
  232. /*!< SD chip select low */
  233. SD_CS_LOW();
  234. /*!< Data transfer */
  235. while (NumberOfBlocks--)
  236. {
  237. /*!< Send CMD17 (SD_CMD_READ_SINGLE_BLOCK) to read one block */
  238. SD_SendCmd (SD_CMD_READ_SINGLE_BLOCK, ReadAddr + Offset, 0xFF);
  239. /*!< Check if the SD acknowledged the read block command: R1 response (0x00: no errors) */
  240. if (SD_GetResponse(SD_RESPONSE_NO_ERROR))
  241. {
  242. return SD_RESPONSE_FAILURE;
  243. }
  244. /*!< Now look for the data token to signify the start of the data */
  245. if (!SD_GetResponse(SD_START_DATA_SINGLE_BLOCK_READ))
  246. {
  247. /*!< Read the SD block data : read NumByteToRead data */
  248. for (i = 0; i < BlockSize; i++)
  249. {
  250. /*!< Read the pointed data */
  251. *pBuffer = SD_ReadByte();
  252. /*!< Point to the next location where the byte read will be saved */
  253. pBuffer++;
  254. }
  255. /*!< Set next read address*/
  256. Offset += 512;
  257. /*!< get CRC bytes (not really needed by us, but required by SD) */
  258. SD_ReadByte();
  259. SD_ReadByte();
  260. /*!< Set response value to success */
  261. rvalue = SD_RESPONSE_NO_ERROR;
  262. }
  263. else
  264. {
  265. /*!< Set response value to failure */
  266. rvalue = SD_RESPONSE_FAILURE;
  267. }
  268. }
  269. /*!< SD chip select high */
  270. SD_CS_HIGH();
  271. /*!< Send dummy byte: 8 Clock pulses of delay */
  272. SD_WriteByte(SD_DUMMY_BYTE);
  273. /*!< Returns the reponse */
  274. return rvalue;
  275. }
  276. /**
  277. * @brief Writes a block on the SD
  278. * @param pBuffer: pointer to the buffer containing the data to be written on
  279. * the SD card.
  280. * @param WriteAddr: address to write on.
  281. * @param BlockSize: the SD card Data block size.
  282. * @retval The SD Response:
  283. * - SD_RESPONSE_FAILURE: Sequence failed
  284. * - SD_RESPONSE_NO_ERROR: Sequence succeed
  285. */
  286. SD_Error SD_WriteBlock(uint8_t* pBuffer, uint32_t WriteAddr, uint16_t BlockSize)
  287. {
  288. uint32_t i = 0;
  289. SD_Error rvalue = SD_RESPONSE_FAILURE;
  290. /*!< SD chip select low */
  291. SD_CS_LOW();
  292. /*!< Send CMD24 (SD_CMD_WRITE_SINGLE_BLOCK) to write multiple block */
  293. SD_SendCmd(SD_CMD_WRITE_SINGLE_BLOCK, WriteAddr, 0xFF);
  294. /*!< Check if the SD acknowledged the write block command: R1 response (0x00: no errors) */
  295. if (!SD_GetResponse(SD_RESPONSE_NO_ERROR))
  296. {
  297. /*!< Send a dummy byte */
  298. SD_WriteByte(SD_DUMMY_BYTE);
  299. /*!< Send the data token to signify the start of the data */
  300. SD_WriteByte(0xFE);
  301. /*!< Write the block data to SD : write count data by block */
  302. for (i = 0; i < BlockSize; i++)
  303. {
  304. /*!< Send the pointed byte */
  305. SD_WriteByte(*pBuffer);
  306. /*!< Point to the next location where the byte read will be saved */
  307. pBuffer++;
  308. }
  309. /*!< Put CRC bytes (not really needed by us, but required by SD) */
  310. SD_ReadByte();
  311. SD_ReadByte();
  312. /*!< Read data response */
  313. if (SD_GetDataResponse() == SD_DATA_OK)
  314. {
  315. rvalue = SD_RESPONSE_NO_ERROR;
  316. }
  317. }
  318. /*!< SD chip select high */
  319. SD_CS_HIGH();
  320. /*!< Send dummy byte: 8 Clock pulses of delay */
  321. SD_WriteByte(SD_DUMMY_BYTE);
  322. /*!< Returns the reponse */
  323. return rvalue;
  324. }
  325. /**
  326. * @brief Writes many blocks on the SD
  327. * @param pBuffer: pointer to the buffer containing the data to be written on
  328. * the SD card.
  329. * @param WriteAddr: address to write on.
  330. * @param BlockSize: the SD card Data block size.
  331. * @param NumberOfBlocks: number of blocks to be written.
  332. * @retval The SD Response:
  333. * - SD_RESPONSE_FAILURE: Sequence failed
  334. * - SD_RESPONSE_NO_ERROR: Sequence succeed
  335. */
  336. SD_Error SD_WriteMultiBlocks(uint8_t* pBuffer, uint32_t WriteAddr, uint16_t BlockSize, uint32_t NumberOfBlocks)
  337. {
  338. uint32_t i = 0, Offset = 0;
  339. SD_Error rvalue = SD_RESPONSE_FAILURE;
  340. /*!< SD chip select low */
  341. SD_CS_LOW();
  342. /*!< Data transfer */
  343. while (NumberOfBlocks--)
  344. {
  345. /*!< Send CMD24 (SD_CMD_WRITE_SINGLE_BLOCK) to write blocks */
  346. SD_SendCmd(SD_CMD_WRITE_SINGLE_BLOCK, WriteAddr + Offset, 0xFF);
  347. /*!< Check if the SD acknowledged the write block command: R1 response (0x00: no errors) */
  348. if (SD_GetResponse(SD_RESPONSE_NO_ERROR))
  349. {
  350. return SD_RESPONSE_FAILURE;
  351. }
  352. /*!< Send dummy byte */
  353. SD_WriteByte(SD_DUMMY_BYTE);
  354. /*!< Send the data token to signify the start of the data */
  355. SD_WriteByte(SD_START_DATA_SINGLE_BLOCK_WRITE);
  356. /*!< Write the block data to SD : write count data by block */
  357. for (i = 0; i < BlockSize; i++)
  358. {
  359. /*!< Send the pointed byte */
  360. SD_WriteByte(*pBuffer);
  361. /*!< Point to the next location where the byte read will be saved */
  362. pBuffer++;
  363. }
  364. /*!< Set next write address */
  365. Offset += 512;
  366. /*!< Put CRC bytes (not really needed by us, but required by SD) */
  367. SD_ReadByte();
  368. SD_ReadByte();
  369. /*!< Read data response */
  370. if (SD_GetDataResponse() == SD_DATA_OK)
  371. {
  372. /*!< Set response value to success */
  373. rvalue = SD_RESPONSE_NO_ERROR;
  374. }
  375. else
  376. {
  377. /*!< Set response value to failure */
  378. rvalue = SD_RESPONSE_FAILURE;
  379. }
  380. }
  381. /*!< SD chip select high */
  382. SD_CS_HIGH();
  383. /*!< Send dummy byte: 8 Clock pulses of delay */
  384. SD_WriteByte(SD_DUMMY_BYTE);
  385. /*!< Returns the reponse */
  386. return rvalue;
  387. }
  388. /**
  389. * @brief Read the CSD card register.
  390. * @note Reading the contents of the CSD register in SPI mode is a simple
  391. * read-block transaction.
  392. * @param SD_csd: pointer on an SCD register structure
  393. * @retval The SD Response:
  394. * - SD_RESPONSE_FAILURE: Sequence failed
  395. * - SD_RESPONSE_NO_ERROR: Sequence succeed
  396. */
  397. SD_Error SD_GetCSDRegister(SD_CSD* SD_csd)
  398. {
  399. uint32_t i = 0;
  400. SD_Error rvalue = SD_RESPONSE_FAILURE;
  401. uint8_t CSD_Tab[16];
  402. /*!< SD chip select low */
  403. SD_CS_LOW();
  404. /*!< Send CMD9 (CSD register) or CMD10(CSD register) */
  405. SD_SendCmd(SD_CMD_SEND_CSD, 0, 0xFF);
  406. /*!< Wait for response in the R1 format (0x00 is no errors) */
  407. if (!SD_GetResponse(SD_RESPONSE_NO_ERROR))
  408. {
  409. if (!SD_GetResponse(SD_START_DATA_SINGLE_BLOCK_READ))
  410. {
  411. for (i = 0; i < 16; i++)
  412. {
  413. /*!< Store CSD register value on CSD_Tab */
  414. CSD_Tab[i] = SD_ReadByte();
  415. }
  416. }
  417. /*!< Get CRC bytes (not really needed by us, but required by SD) */
  418. SD_WriteByte(SD_DUMMY_BYTE);
  419. SD_WriteByte(SD_DUMMY_BYTE);
  420. /*!< Set response value to success */
  421. rvalue = SD_RESPONSE_NO_ERROR;
  422. }
  423. /*!< SD chip select high */
  424. SD_CS_HIGH();
  425. /*!< Send dummy byte: 8 Clock pulses of delay */
  426. SD_WriteByte(SD_DUMMY_BYTE);
  427. /*!< Byte 0 */
  428. SD_csd->CSDStruct = (CSD_Tab[0] & 0xC0) >> 6;
  429. SD_csd->SysSpecVersion = (CSD_Tab[0] & 0x3C) >> 2;
  430. SD_csd->Reserved1 = CSD_Tab[0] & 0x03;
  431. /*!< Byte 1 */
  432. SD_csd->TAAC = CSD_Tab[1];
  433. /*!< Byte 2 */
  434. SD_csd->NSAC = CSD_Tab[2];
  435. /*!< Byte 3 */
  436. SD_csd->MaxBusClkFrec = CSD_Tab[3];
  437. /*!< Byte 4 */
  438. SD_csd->CardComdClasses = CSD_Tab[4] << 4;
  439. /*!< Byte 5 */
  440. SD_csd->CardComdClasses |= (CSD_Tab[5] & 0xF0) >> 4;
  441. SD_csd->RdBlockLen = CSD_Tab[5] & 0x0F;
  442. /*!< Byte 6 */
  443. SD_csd->PartBlockRead = (CSD_Tab[6] & 0x80) >> 7;
  444. SD_csd->WrBlockMisalign = (CSD_Tab[6] & 0x40) >> 6;
  445. SD_csd->RdBlockMisalign = (CSD_Tab[6] & 0x20) >> 5;
  446. SD_csd->DSRImpl = (CSD_Tab[6] & 0x10) >> 4;
  447. SD_csd->Reserved2 = 0; /*!< Reserved */
  448. SD_csd->DeviceSize = (CSD_Tab[6] & 0x03) << 10;
  449. /*!< Byte 7 */
  450. SD_csd->DeviceSize |= (CSD_Tab[7]) << 2;
  451. /*!< Byte 8 */
  452. SD_csd->DeviceSize |= (CSD_Tab[8] & 0xC0) >> 6;
  453. SD_csd->MaxRdCurrentVDDMin = (CSD_Tab[8] & 0x38) >> 3;
  454. SD_csd->MaxRdCurrentVDDMax = (CSD_Tab[8] & 0x07);
  455. /*!< Byte 9 */
  456. SD_csd->MaxWrCurrentVDDMin = (CSD_Tab[9] & 0xE0) >> 5;
  457. SD_csd->MaxWrCurrentVDDMax = (CSD_Tab[9] & 0x1C) >> 2;
  458. SD_csd->DeviceSizeMul = (CSD_Tab[9] & 0x03) << 1;
  459. /*!< Byte 10 */
  460. SD_csd->DeviceSizeMul |= (CSD_Tab[10] & 0x80) >> 7;
  461. SD_csd->EraseGrSize = (CSD_Tab[10] & 0x40) >> 6;
  462. SD_csd->EraseGrMul = (CSD_Tab[10] & 0x3F) << 1;
  463. /*!< Byte 11 */
  464. SD_csd->EraseGrMul |= (CSD_Tab[11] & 0x80) >> 7;
  465. SD_csd->WrProtectGrSize = (CSD_Tab[11] & 0x7F);
  466. /*!< Byte 12 */
  467. SD_csd->WrProtectGrEnable = (CSD_Tab[12] & 0x80) >> 7;
  468. SD_csd->ManDeflECC = (CSD_Tab[12] & 0x60) >> 5;
  469. SD_csd->WrSpeedFact = (CSD_Tab[12] & 0x1C) >> 2;
  470. SD_csd->MaxWrBlockLen = (CSD_Tab[12] & 0x03) << 2;
  471. /*!< Byte 13 */
  472. SD_csd->MaxWrBlockLen |= (CSD_Tab[13] & 0xC0) >> 6;
  473. SD_csd->WriteBlockPaPartial = (CSD_Tab[13] & 0x20) >> 5;
  474. SD_csd->Reserved3 = 0;
  475. SD_csd->ContentProtectAppli = (CSD_Tab[13] & 0x01);
  476. /*!< Byte 14 */
  477. SD_csd->FileFormatGrouop = (CSD_Tab[14] & 0x80) >> 7;
  478. SD_csd->CopyFlag = (CSD_Tab[14] & 0x40) >> 6;
  479. SD_csd->PermWrProtect = (CSD_Tab[14] & 0x20) >> 5;
  480. SD_csd->TempWrProtect = (CSD_Tab[14] & 0x10) >> 4;
  481. SD_csd->FileFormat = (CSD_Tab[14] & 0x0C) >> 2;
  482. SD_csd->ECC = (CSD_Tab[14] & 0x03);
  483. /*!< Byte 15 */
  484. SD_csd->CSD_CRC = (CSD_Tab[15] & 0xFE) >> 1;
  485. SD_csd->Reserved4 = 1;
  486. /*!< Return the reponse */
  487. return rvalue;
  488. }
  489. /**
  490. * @brief Read the CID card register.
  491. * @note Reading the contents of the CID register in SPI mode is a simple
  492. * read-block transaction.
  493. * @param SD_cid: pointer on an CID register structure
  494. * @retval The SD Response:
  495. * - SD_RESPONSE_FAILURE: Sequence failed
  496. * - SD_RESPONSE_NO_ERROR: Sequence succeed
  497. */
  498. SD_Error SD_GetCIDRegister(SD_CID* SD_cid)
  499. {
  500. uint32_t i = 0;
  501. SD_Error rvalue = SD_RESPONSE_FAILURE;
  502. uint8_t CID_Tab[16];
  503. /*!< SD chip select low */
  504. SD_CS_LOW();
  505. /*!< Send CMD10 (CID register) */
  506. SD_SendCmd(SD_CMD_SEND_CID, 0, 0xFF);
  507. /*!< Wait for response in the R1 format (0x00 is no errors) */
  508. if (!SD_GetResponse(SD_RESPONSE_NO_ERROR))
  509. {
  510. if (!SD_GetResponse(SD_START_DATA_SINGLE_BLOCK_READ))
  511. {
  512. /*!< Store CID register value on CID_Tab */
  513. for (i = 0; i < 16; i++)
  514. {
  515. CID_Tab[i] = SD_ReadByte();
  516. }
  517. }
  518. /*!< Get CRC bytes (not really needed by us, but required by SD) */
  519. SD_WriteByte(SD_DUMMY_BYTE);
  520. SD_WriteByte(SD_DUMMY_BYTE);
  521. /*!< Set response value to success */
  522. rvalue = SD_RESPONSE_NO_ERROR;
  523. }
  524. /*!< SD chip select high */
  525. SD_CS_HIGH();
  526. /*!< Send dummy byte: 8 Clock pulses of delay */
  527. SD_WriteByte(SD_DUMMY_BYTE);
  528. /*!< Byte 0 */
  529. SD_cid->ManufacturerID = CID_Tab[0];
  530. /*!< Byte 1 */
  531. SD_cid->OEM_AppliID = CID_Tab[1] << 8;
  532. /*!< Byte 2 */
  533. SD_cid->OEM_AppliID |= CID_Tab[2];
  534. /*!< Byte 3 */
  535. SD_cid->ProdName1 = CID_Tab[3] << 24;
  536. /*!< Byte 4 */
  537. SD_cid->ProdName1 |= CID_Tab[4] << 16;
  538. /*!< Byte 5 */
  539. SD_cid->ProdName1 |= CID_Tab[5] << 8;
  540. /*!< Byte 6 */
  541. SD_cid->ProdName1 |= CID_Tab[6];
  542. /*!< Byte 7 */
  543. SD_cid->ProdName2 = CID_Tab[7];
  544. /*!< Byte 8 */
  545. SD_cid->ProdRev = CID_Tab[8];
  546. /*!< Byte 9 */
  547. SD_cid->ProdSN = CID_Tab[9] << 24;
  548. /*!< Byte 10 */
  549. SD_cid->ProdSN |= CID_Tab[10] << 16;
  550. /*!< Byte 11 */
  551. SD_cid->ProdSN |= CID_Tab[11] << 8;
  552. /*!< Byte 12 */
  553. SD_cid->ProdSN |= CID_Tab[12];
  554. /*!< Byte 13 */
  555. SD_cid->Reserved1 |= (CID_Tab[13] & 0xF0) >> 4;
  556. SD_cid->ManufactDate = (CID_Tab[13] & 0x0F) << 8;
  557. /*!< Byte 14 */
  558. SD_cid->ManufactDate |= CID_Tab[14];
  559. /*!< Byte 15 */
  560. SD_cid->CID_CRC = (CID_Tab[15] & 0xFE) >> 1;
  561. SD_cid->Reserved2 = 1;
  562. /*!< Return the reponse */
  563. return rvalue;
  564. }
  565. /**
  566. * @brief Send 5 bytes command to the SD card.
  567. * @param Cmd: The user expected command to send to SD card.
  568. * @param Arg: The command argument.
  569. * @param Crc: The CRC.
  570. * @retval None
  571. */
  572. void SD_SendCmd(uint8_t Cmd, uint32_t Arg, uint8_t Crc)
  573. {
  574. uint32_t i = 0x00;
  575. uint8_t Frame[6];
  576. Frame[0] = (Cmd | 0x40); /*!< Construct byte 1 */
  577. Frame[1] = (uint8_t)(Arg >> 24); /*!< Construct byte 2 */
  578. Frame[2] = (uint8_t)(Arg >> 16); /*!< Construct byte 3 */
  579. Frame[3] = (uint8_t)(Arg >> 8); /*!< Construct byte 4 */
  580. Frame[4] = (uint8_t)(Arg); /*!< Construct byte 5 */
  581. Frame[5] = (Crc); /*!< Construct CRC: byte 6 */
  582. for (i = 0; i < 6; i++)
  583. {
  584. SD_WriteByte(Frame[i]); /*!< Send the Cmd bytes */
  585. }
  586. }
  587. /**
  588. * @brief Get SD card data response.
  589. * @param None
  590. * @retval The SD status: Read data response xxx0<status>1
  591. * - status 010: Data accecpted
  592. * - status 101: Data rejected due to a crc error
  593. * - status 110: Data rejected due to a Write error.
  594. * - status 111: Data rejected due to other error.
  595. */
  596. uint8_t SD_GetDataResponse(void)
  597. {
  598. uint32_t i = 0;
  599. uint8_t response, rvalue;
  600. while (i <= 64)
  601. {
  602. /*!< Read resonse */
  603. response = SD_ReadByte();
  604. /*!< Mask unused bits */
  605. response &= 0x1F;
  606. switch (response)
  607. {
  608. case SD_DATA_OK:
  609. {
  610. rvalue = SD_DATA_OK;
  611. break;
  612. }
  613. case SD_DATA_CRC_ERROR:
  614. return SD_DATA_CRC_ERROR;
  615. case SD_DATA_WRITE_ERROR:
  616. return SD_DATA_WRITE_ERROR;
  617. default:
  618. {
  619. rvalue = SD_DATA_OTHER_ERROR;
  620. break;
  621. }
  622. }
  623. /*!< Exit loop in case of data ok */
  624. if (rvalue == SD_DATA_OK)
  625. break;
  626. /*!< Increment loop counter */
  627. i++;
  628. }
  629. /*!< Wait null data */
  630. while (SD_ReadByte() == 0);
  631. /*!< Return response */
  632. return response;
  633. }
  634. /**
  635. * @brief Returns the SD response.
  636. * @param None
  637. * @retval The SD Response:
  638. * - SD_RESPONSE_FAILURE: Sequence failed
  639. * - SD_RESPONSE_NO_ERROR: Sequence succeed
  640. */
  641. SD_Error SD_GetResponse(uint8_t Response)
  642. {
  643. uint32_t Count = 0xFFF;
  644. /* Check if response is got or a timeout is happen */
  645. while ((SD_ReadByte() != Response) && Count)
  646. {
  647. Count--;
  648. }
  649. if (Count == 0)
  650. {
  651. /* After time out */
  652. return SD_RESPONSE_FAILURE;
  653. }
  654. else
  655. {
  656. /* Right response got */
  657. return SD_RESPONSE_NO_ERROR;
  658. }
  659. }
  660. /**
  661. * @brief Returns the SD status.
  662. * @param None
  663. * @retval The SD status.
  664. */
  665. uint16_t SD_GetStatus(void)
  666. {
  667. uint16_t Status = 0;
  668. /*!< SD chip select low */
  669. SD_CS_LOW();
  670. /*!< Send CMD13 (SD_SEND_STATUS) to get SD status */
  671. SD_SendCmd(SD_CMD_SEND_STATUS, 0, 0xFF);
  672. Status = SD_ReadByte();
  673. Status |= (uint16_t)(SD_ReadByte() << 8);
  674. /*!< SD chip select high */
  675. SD_CS_HIGH();
  676. /*!< Send dummy byte 0xFF */
  677. SD_WriteByte(SD_DUMMY_BYTE);
  678. return Status;
  679. }
  680. /**
  681. * @brief Put SD in Idle state.
  682. * @param None
  683. * @retval The SD Response:
  684. * - SD_RESPONSE_FAILURE: Sequence failed
  685. * - SD_RESPONSE_NO_ERROR: Sequence succeed
  686. */
  687. SD_Error SD_GoIdleState(void)
  688. {
  689. /*!< SD chip select low */
  690. SD_CS_LOW();
  691. /*!< Send CMD0 (SD_CMD_GO_IDLE_STATE) to put SD in SPI mode */
  692. SD_SendCmd(SD_CMD_GO_IDLE_STATE, 0, 0x95);
  693. /*!< Wait for In Idle State Response (R1 Format) equal to 0x01 */
  694. if (SD_GetResponse(SD_IN_IDLE_STATE))
  695. {
  696. /*!< No Idle State Response: return response failue */
  697. return SD_RESPONSE_FAILURE;
  698. }
  699. /*----------Activates the card initialization process-----------*/
  700. do
  701. {
  702. /*!< SD chip select high */
  703. SD_CS_HIGH();
  704. /*!< Send Dummy byte 0xFF */
  705. SD_WriteByte(SD_DUMMY_BYTE);
  706. /*!< SD chip select low */
  707. SD_CS_LOW();
  708. /*!< Send CMD1 (Activates the card process) until response equal to 0x0 */
  709. SD_SendCmd(SD_CMD_SEND_OP_COND, 0, 0xFF);
  710. /*!< Wait for no error Response (R1 Format) equal to 0x00 */
  711. }
  712. while (SD_GetResponse(SD_RESPONSE_NO_ERROR));
  713. /*!< SD chip select high */
  714. SD_CS_HIGH();
  715. /*!< Send dummy byte 0xFF */
  716. SD_WriteByte(SD_DUMMY_BYTE);
  717. return SD_RESPONSE_NO_ERROR;
  718. }
  719. /**
  720. * @brief Write a byte on the SD.
  721. * @param Data: byte to send.
  722. * @retval None
  723. */
  724. uint8_t SD_WriteByte(uint8_t Data)
  725. {
  726. /*!< Wait until the transmit buffer is empty */
  727. while(SPI_I2S_GetFlagStatus(SD_SPI, SPI_I2S_FLAG_TXE) == RESET)
  728. {
  729. }
  730. /*!< Send the byte */
  731. SPI_SendData8(SD_SPI, Data);
  732. /*!< Wait to receive a byte*/
  733. while(SPI_I2S_GetFlagStatus(SD_SPI, SPI_I2S_FLAG_RXNE) == RESET)
  734. {
  735. }
  736. /*!< Return the byte read from the SPI bus */
  737. return SPI_ReceiveData8(SD_SPI);
  738. }
  739. /**
  740. * @brief Read a byte from the SD.
  741. * @param None
  742. * @retval The received byte.
  743. */
  744. uint8_t SD_ReadByte(void)
  745. {
  746. uint8_t Data = 0;
  747. /*!< Wait until the transmit buffer is empty */
  748. while (SPI_I2S_GetFlagStatus(SD_SPI, SPI_I2S_FLAG_TXE) == RESET)
  749. {
  750. }
  751. /*!< Send the byte */
  752. SPI_SendData8(SD_SPI, SD_DUMMY_BYTE);
  753. /*!< Wait until a data is received */
  754. while (SPI_I2S_GetFlagStatus(SD_SPI, SPI_I2S_FLAG_RXNE) == RESET)
  755. {
  756. }
  757. /*!< Get the received data */
  758. Data = SPI_ReceiveData8(SD_SPI);
  759. /*!< Return the shifted data */
  760. return Data;
  761. }
  762. /**
  763. * @}
  764. */
  765. /**
  766. * @}
  767. */
  768. /**
  769. * @}
  770. */
  771. /**
  772. * @}
  773. */
  774. /**
  775. * @}
  776. */
  777. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/