msd.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938
  1. /******************** (C) COPYRIGHT 2008 STMicroelectronics ********************
  2. * File Name : msd.c
  3. * Author : MCD Application Team
  4. * Version : V2.1
  5. * Date : 05/30/2008
  6. * Description : MSD card driver source file.
  7. * Pin assignment:
  8. * ----------------------------------------------
  9. * | STM32F10x | MSD Pin |
  10. * ----------------------------------------------
  11. * | P0.4 | ChipSelect 1 |
  12. * | P0.1 / MOSI | DataIn 2 |
  13. * | | GND 3 (0 V) |
  14. * | | VDD 4 (3.3 V) |
  15. * | P0.2 / SCLK | Clock 5 |
  16. * | | GND 6 (0 V) |
  17. * | P0.0 / MISO | DataOut 7 |
  18. * -----------------------------------------------
  19. ********************************************************************************
  20. * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  21. * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
  22. * AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
  23. * INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
  24. * CONTENT OF SUCH SOFTWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
  25. * INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  26. * FOR MORE INFORMATION PLEASE CAREFULLY READ THE LICENSE AGREEMENT FILE LOCATED
  27. * IN THE ROOT DIRECTORY OF THIS FIRMWARE PACKAGE.
  28. *******************************************************************************/
  29. /* Includes ------------------------------------------------------------------*/
  30. #include "msd.h"
  31. #include <stm32f10x_spi.h>
  32. /* Private typedef -----------------------------------------------------------*/
  33. /* Private define ------------------------------------------------------------*/
  34. /* Private macro -------------------------------------------------------------*/
  35. /* Select MSD Card: ChipSelect pin low */
  36. #define MSD_CS_LOW() GPIO_ResetBits(GPIOD, GPIO_Pin_9)
  37. /* Deselect MSD Card: ChipSelect pin high */
  38. #define MSD_CS_HIGH() GPIO_SetBits(GPIOD, GPIO_Pin_9)
  39. /* Private function prototypes -----------------------------------------------*/
  40. static void SPI_Config(void);
  41. /* Private functions ---------------------------------------------------------*/
  42. /*******************************************************************************
  43. * Function Name : MSD_Init
  44. * Description : Initializes the MSD/SD communication.
  45. * Input : None
  46. * Output : None
  47. * Return : The MSD Response: - MSD_RESPONSE_FAILURE: Sequence failed
  48. * - MSD_RESPONSE_NO_ERROR: Sequence succeed
  49. *******************************************************************************/
  50. u8 MSD_Init(void)
  51. {
  52. u32 i = 0;
  53. /* Initialize SPI1 */
  54. SPI_Config();
  55. /* MSD chip select high */
  56. MSD_CS_HIGH();
  57. /* Send dummy byte 0xFF, 10 times with CS high*/
  58. /* rise CS and MOSI for 80 clocks cycles */
  59. for (i = 0; i <= 9; i++)
  60. {
  61. /* Send dummy byte 0xFF */
  62. MSD_WriteByte(DUMMY);
  63. }
  64. /*------------Put MSD in SPI mode--------------*/
  65. /* MSD initialized and set to SPI mode properly */
  66. return (MSD_GoIdleState());
  67. }
  68. /*******************************************************************************
  69. * Function Name : MSD_WriteBlock
  70. * Description : Writes a block on the MSD
  71. * Input : - pBuffer : pointer to the buffer containing the data to be
  72. * written on the MSD.
  73. * - WriteAddr : address to write on.
  74. * - NumByteToWrite: number of data to write
  75. * Output : None
  76. * Return : The MSD Response: - MSD_RESPONSE_FAILURE: Sequence failed
  77. * - MSD_RESPONSE_NO_ERROR: Sequence succeed
  78. *******************************************************************************/
  79. u8 MSD_WriteBlock(u8* pBuffer, u32 WriteAddr, u16 NumByteToWrite)
  80. {
  81. u32 i = 0;
  82. u8 rvalue = MSD_RESPONSE_FAILURE;
  83. /* MSD chip select low */
  84. MSD_CS_LOW();
  85. /* Send CMD24 (MSD_WRITE_BLOCK) to write multiple block */
  86. MSD_SendCmd(MSD_WRITE_BLOCK, WriteAddr, 0xFF);
  87. /* Check if the MSD acknowledged the write block command: R1 response (0x00: no errors) */
  88. if (!MSD_GetResponse(MSD_RESPONSE_NO_ERROR))
  89. {
  90. /* Send a dummy byte */
  91. MSD_WriteByte(DUMMY);
  92. /* Send the data token to signify the start of the data */
  93. MSD_WriteByte(0xFE);
  94. /* Write the block data to MSD : write count data by block */
  95. for (i = 0; i < NumByteToWrite; i++)
  96. {
  97. /* Send the pointed byte */
  98. MSD_WriteByte(*pBuffer);
  99. /* Point to the next location where the byte read will be saved */
  100. pBuffer++;
  101. }
  102. /* Put CRC bytes (not really needed by us, but required by MSD) */
  103. MSD_ReadByte();
  104. MSD_ReadByte();
  105. /* Read data response */
  106. if (MSD_GetDataResponse() == MSD_DATA_OK)
  107. {
  108. rvalue = MSD_RESPONSE_NO_ERROR;
  109. }
  110. }
  111. /* MSD chip select high */
  112. MSD_CS_HIGH();
  113. /* Send dummy byte: 8 Clock pulses of delay */
  114. MSD_WriteByte(DUMMY);
  115. /* Returns the reponse */
  116. return rvalue;
  117. }
  118. /*******************************************************************************
  119. * Function Name : MSD_ReadBlock
  120. * Description : Reads a block of data from the MSD.
  121. * Input : - pBuffer : pointer to the buffer that receives the data read
  122. * from the MSD.
  123. * - ReadAddr : MSD's internal address to read from.
  124. * - NumByteToRead : number of bytes to read from the MSD.
  125. * Output : None
  126. * Return : The MSD Response: - MSD_RESPONSE_FAILURE: Sequence failed
  127. * - MSD_RESPONSE_NO_ERROR: Sequence succeed
  128. *******************************************************************************/
  129. u8 MSD_ReadBlock(u8* pBuffer, u32 ReadAddr, u16 NumByteToRead)
  130. {
  131. u32 i = 0;
  132. u8 rvalue = MSD_RESPONSE_FAILURE;
  133. /* MSD chip select low */
  134. MSD_CS_LOW();
  135. /* Send CMD17 (MSD_READ_SINGLE_BLOCK) to read one block */
  136. MSD_SendCmd(MSD_READ_SINGLE_BLOCK, ReadAddr, 0xFF);
  137. /* Check if the MSD acknowledged the read block command: R1 response (0x00: no errors) */
  138. if (!MSD_GetResponse(MSD_RESPONSE_NO_ERROR))
  139. {
  140. /* Now look for the data token to signify the start of the data */
  141. if (!MSD_GetResponse(MSD_START_DATA_SINGLE_BLOCK_READ))
  142. {
  143. /* Read the MSD block data : read NumByteToRead data */
  144. for (i = 0; i < NumByteToRead; i++)
  145. {
  146. /* Save the received data */
  147. *pBuffer = MSD_ReadByte();
  148. /* Point to the next location where the byte read will be saved */
  149. pBuffer++;
  150. }
  151. /* Get CRC bytes (not really needed by us, but required by MSD) */
  152. MSD_ReadByte();
  153. MSD_ReadByte();
  154. /* Set response value to success */
  155. rvalue = MSD_RESPONSE_NO_ERROR;
  156. }
  157. }
  158. /* MSD chip select high */
  159. MSD_CS_HIGH();
  160. /* Send dummy byte: 8 Clock pulses of delay */
  161. MSD_WriteByte(DUMMY);
  162. /* Returns the reponse */
  163. return rvalue;
  164. }
  165. /*******************************************************************************
  166. * Function Name : MSD_WriteBuffer
  167. * Description : Writes many blocks on the MSD
  168. * Input : - pBuffer : pointer to the buffer containing the data to be
  169. * written on the MSD.
  170. * - WriteAddr : address to write on.
  171. * - NumByteToWrite: number of data to write
  172. * Output : None
  173. * Return : The MSD Response: - MSD_RESPONSE_FAILURE: Sequence failed
  174. * - MSD_RESPONSE_NO_ERROR: Sequence succeed
  175. *******************************************************************************/
  176. u8 MSD_WriteBuffer(u8* pBuffer, u32 WriteAddr, u32 NumByteToWrite)
  177. {
  178. u32 i = 0, NbrOfBlock = 0, Offset = 0;
  179. u8 rvalue = MSD_RESPONSE_FAILURE;
  180. /* Calculate number of blocks to write */
  181. NbrOfBlock = NumByteToWrite / BLOCK_SIZE;
  182. /* MSD chip select low */
  183. MSD_CS_LOW();
  184. /* Data transfer */
  185. while (NbrOfBlock --)
  186. {
  187. /* Send CMD24 (MSD_WRITE_BLOCK) to write blocks */
  188. MSD_SendCmd(MSD_WRITE_BLOCK, WriteAddr + Offset, 0xFF);
  189. /* Check if the MSD acknowledged the write block command: R1 response (0x00: no errors) */
  190. if (MSD_GetResponse(MSD_RESPONSE_NO_ERROR))
  191. {
  192. return MSD_RESPONSE_FAILURE;
  193. }
  194. /* Send dummy byte */
  195. MSD_WriteByte(DUMMY);
  196. /* Send the data token to signify the start of the data */
  197. MSD_WriteByte(MSD_START_DATA_SINGLE_BLOCK_WRITE);
  198. /* Write the block data to MSD : write count data by block */
  199. for (i = 0; i < BLOCK_SIZE; i++)
  200. {
  201. /* Send the pointed byte */
  202. MSD_WriteByte(*pBuffer);
  203. /* Point to the next location where the byte read will be saved */
  204. pBuffer++;
  205. }
  206. /* Set next write address */
  207. Offset += 512;
  208. /* Put CRC bytes (not really needed by us, but required by MSD) */
  209. MSD_ReadByte();
  210. MSD_ReadByte();
  211. /* Read data response */
  212. if (MSD_GetDataResponse() == MSD_DATA_OK)
  213. {
  214. /* Set response value to success */
  215. rvalue = MSD_RESPONSE_NO_ERROR;
  216. }
  217. else
  218. {
  219. /* Set response value to failure */
  220. rvalue = MSD_RESPONSE_FAILURE;
  221. }
  222. }
  223. /* MSD chip select high */
  224. MSD_CS_HIGH();
  225. /* Send dummy byte: 8 Clock pulses of delay */
  226. MSD_WriteByte(DUMMY);
  227. /* Returns the reponse */
  228. return rvalue;
  229. }
  230. /*******************************************************************************
  231. * Function Name : MSD_ReadBuffer
  232. * Description : Reads multiple block of data from the MSD.
  233. * Input : - pBuffer : pointer to the buffer that receives the data read
  234. * from the MSD.
  235. * - ReadAddr : MSD's internal address to read from.
  236. * - NumByteToRead : number of bytes to read from the MSD.
  237. * Output : None
  238. * Return : The MSD Response: - MSD_RESPONSE_FAILURE: Sequence failed
  239. * - MSD_RESPONSE_NO_ERROR: Sequence succeed
  240. *******************************************************************************/
  241. u8 MSD_ReadBuffer(u8* pBuffer, u32 ReadAddr, u32 NumByteToRead)
  242. {
  243. u32 i = 0, NbrOfBlock = 0, Offset = 0;
  244. u8 rvalue = MSD_RESPONSE_FAILURE;
  245. /* Calculate number of blocks to read */
  246. NbrOfBlock = NumByteToRead / BLOCK_SIZE;
  247. /* MSD chip select low */
  248. MSD_CS_LOW();
  249. /* Data transfer */
  250. while (NbrOfBlock --)
  251. {
  252. /* Send CMD17 (MSD_READ_SINGLE_BLOCK) to read one block */
  253. MSD_SendCmd (MSD_READ_SINGLE_BLOCK, ReadAddr + Offset, 0xFF);
  254. /* Check if the MSD acknowledged the read block command: R1 response (0x00: no errors) */
  255. if (MSD_GetResponse(MSD_RESPONSE_NO_ERROR))
  256. {
  257. return MSD_RESPONSE_FAILURE;
  258. }
  259. /* Now look for the data token to signify the start of the data */
  260. if (!MSD_GetResponse(MSD_START_DATA_SINGLE_BLOCK_READ))
  261. {
  262. /* Read the MSD block data : read NumByteToRead data */
  263. for (i = 0; i < BLOCK_SIZE; i++)
  264. {
  265. /* Read the pointed data */
  266. *pBuffer = MSD_ReadByte();
  267. /* Point to the next location where the byte read will be saved */
  268. pBuffer++;
  269. }
  270. /* Set next read address*/
  271. Offset += 512;
  272. /* get CRC bytes (not really needed by us, but required by MSD) */
  273. MSD_ReadByte();
  274. MSD_ReadByte();
  275. /* Set response value to success */
  276. rvalue = MSD_RESPONSE_NO_ERROR;
  277. }
  278. else
  279. {
  280. /* Set response value to failure */
  281. rvalue = MSD_RESPONSE_FAILURE;
  282. }
  283. }
  284. /* MSD chip select high */
  285. MSD_CS_HIGH();
  286. /* Send dummy byte: 8 Clock pulses of delay */
  287. MSD_WriteByte(DUMMY);
  288. /* Returns the reponse */
  289. return rvalue;
  290. }
  291. /*******************************************************************************
  292. * Function Name : MSD_GetCSDRegister
  293. * Description : Read the CSD card register.
  294. * Reading the contents of the CSD register in SPI mode
  295. * is a simple read-block transaction.
  296. * Input : - MSD_csd: pointer on an SCD register structure
  297. * Output : None
  298. * Return : The MSD Response: - MSD_RESPONSE_FAILURE: Sequence failed
  299. * - MSD_RESPONSE_NO_ERROR: Sequence succeed
  300. *******************************************************************************/
  301. u8 MSD_GetCSDRegister(sMSD_CSD* MSD_csd)
  302. {
  303. u32 i = 0;
  304. u8 rvalue = MSD_RESPONSE_FAILURE;
  305. u8 CSD_Tab[16];
  306. /* MSD chip select low */
  307. MSD_CS_LOW();
  308. /* Send CMD9 (CSD register) or CMD10(CSD register) */
  309. MSD_SendCmd(MSD_SEND_CSD, 0, 0xFF);
  310. /* Wait for response in the R1 format (0x00 is no errors) */
  311. if (!MSD_GetResponse(MSD_RESPONSE_NO_ERROR))
  312. {
  313. if (!MSD_GetResponse(MSD_START_DATA_SINGLE_BLOCK_READ))
  314. {
  315. for (i = 0; i < 16; i++)
  316. {
  317. /* Store CSD register value on CSD_Tab */
  318. CSD_Tab[i] = MSD_ReadByte();
  319. }
  320. }
  321. /* Get CRC bytes (not really needed by us, but required by MSD) */
  322. MSD_WriteByte(DUMMY);
  323. MSD_WriteByte(DUMMY);
  324. /* Set response value to success */
  325. rvalue = MSD_RESPONSE_NO_ERROR;
  326. }
  327. /* MSD chip select high */
  328. MSD_CS_HIGH();
  329. /* Send dummy byte: 8 Clock pulses of delay */
  330. MSD_WriteByte(DUMMY);
  331. /* Byte 0 */
  332. MSD_csd->CSDStruct = (CSD_Tab[0] & 0xC0) >> 6;
  333. MSD_csd->SysSpecVersion = (CSD_Tab[0] & 0x3C) >> 2;
  334. MSD_csd->Reserved1 = CSD_Tab[0] & 0x03;
  335. /* Byte 1 */
  336. MSD_csd->TAAC = CSD_Tab[1] ;
  337. /* Byte 2 */
  338. MSD_csd->NSAC = CSD_Tab[2];
  339. /* Byte 3 */
  340. MSD_csd->MaxBusClkFrec = CSD_Tab[3];
  341. /* Byte 4 */
  342. MSD_csd->CardComdClasses = CSD_Tab[4] << 4;
  343. /* Byte 5 */
  344. MSD_csd->CardComdClasses |= (CSD_Tab[5] & 0xF0) >> 4;
  345. MSD_csd->RdBlockLen = CSD_Tab[5] & 0x0F;
  346. /* Byte 6 */
  347. MSD_csd->PartBlockRead = (CSD_Tab[6] & 0x80) >> 7;
  348. MSD_csd->WrBlockMisalign = (CSD_Tab[6] & 0x40) >> 6;
  349. MSD_csd->RdBlockMisalign = (CSD_Tab[6] & 0x20) >> 5;
  350. MSD_csd->DSRImpl = (CSD_Tab[6] & 0x10) >> 4;
  351. MSD_csd->Reserved2 = 0; /* Reserved */
  352. MSD_csd->DeviceSize = (CSD_Tab[6] & 0x03) << 10;
  353. /* Byte 7 */
  354. MSD_csd->DeviceSize |= (CSD_Tab[7]) << 2;
  355. /* Byte 8 */
  356. MSD_csd->DeviceSize |= (CSD_Tab[8] & 0xC0) >> 6;
  357. MSD_csd->MaxRdCurrentVDDMin = (CSD_Tab[8] & 0x38) >> 3;
  358. MSD_csd->MaxRdCurrentVDDMax = (CSD_Tab[8] & 0x07);
  359. /* Byte 9 */
  360. MSD_csd->MaxWrCurrentVDDMin = (CSD_Tab[9] & 0xE0) >> 5;
  361. MSD_csd->MaxWrCurrentVDDMax = (CSD_Tab[9] & 0x1C) >> 2;
  362. MSD_csd->DeviceSizeMul = (CSD_Tab[9] & 0x03) << 1;
  363. /* Byte 10 */
  364. MSD_csd->DeviceSizeMul |= (CSD_Tab[10] & 0x80) >> 7;
  365. MSD_csd->EraseGrSize = (CSD_Tab[10] & 0x7C) >> 2;
  366. MSD_csd->EraseGrMul = (CSD_Tab[10] & 0x03) << 3;
  367. /* Byte 11 */
  368. MSD_csd->EraseGrMul |= (CSD_Tab[11] & 0xE0) >> 5;
  369. MSD_csd->WrProtectGrSize = (CSD_Tab[11] & 0x1F);
  370. /* Byte 12 */
  371. MSD_csd->WrProtectGrEnable = (CSD_Tab[12] & 0x80) >> 7;
  372. MSD_csd->ManDeflECC = (CSD_Tab[12] & 0x60) >> 5;
  373. MSD_csd->WrSpeedFact = (CSD_Tab[12] & 0x1C) >> 2;
  374. MSD_csd->MaxWrBlockLen = (CSD_Tab[12] & 0x03) << 2;
  375. /* Byte 13 */
  376. MSD_csd->MaxWrBlockLen |= (CSD_Tab[13] & 0xc0) >> 6;
  377. MSD_csd->WriteBlockPaPartial = (CSD_Tab[13] & 0x20) >> 5;
  378. MSD_csd->Reserved3 = 0;
  379. MSD_csd->ContentProtectAppli = (CSD_Tab[13] & 0x01);
  380. /* Byte 14 */
  381. MSD_csd->FileFormatGrouop = (CSD_Tab[14] & 0x80) >> 7;
  382. MSD_csd->CopyFlag = (CSD_Tab[14] & 0x40) >> 6;
  383. MSD_csd->PermWrProtect = (CSD_Tab[14] & 0x20) >> 5;
  384. MSD_csd->TempWrProtect = (CSD_Tab[14] & 0x10) >> 4;
  385. MSD_csd->FileFormat = (CSD_Tab[14] & 0x0C) >> 2;
  386. MSD_csd->ECC = (CSD_Tab[14] & 0x03);
  387. /* Byte 15 */
  388. MSD_csd->msd_CRC = (CSD_Tab[15] & 0xFE) >> 1;
  389. MSD_csd->Reserved4 = 1;
  390. /* Return the reponse */
  391. return rvalue;
  392. }
  393. /*******************************************************************************
  394. * Function Name : MSD_GetCIDRegister
  395. * Description : Read the CID card register.
  396. * Reading the contents of the CID register in SPI mode
  397. * is a simple read-block transaction.
  398. * Input : - MSD_cid: pointer on an CID register structure
  399. * Output : None
  400. * Return : The MSD Response: - MSD_RESPONSE_FAILURE: Sequence failed
  401. * - MSD_RESPONSE_NO_ERROR: Sequence succeed
  402. *******************************************************************************/
  403. u8 MSD_GetCIDRegister(sMSD_CID* MSD_cid)
  404. {
  405. u32 i = 0;
  406. u8 rvalue = MSD_RESPONSE_FAILURE;
  407. u8 CID_Tab[16];
  408. /* MSD chip select low */
  409. MSD_CS_LOW();
  410. /* Send CMD10 (CID register) */
  411. MSD_SendCmd(MSD_SEND_CID, 0, 0xFF);
  412. /* Wait for response in the R1 format (0x00 is no errors) */
  413. if (!MSD_GetResponse(MSD_RESPONSE_NO_ERROR))
  414. {
  415. if (!MSD_GetResponse(MSD_START_DATA_SINGLE_BLOCK_READ))
  416. {
  417. /* Store CID register value on CID_Tab */
  418. for (i = 0; i < 16; i++)
  419. {
  420. CID_Tab[i] = MSD_ReadByte();
  421. }
  422. }
  423. /* Get CRC bytes (not really needed by us, but required by MSD) */
  424. MSD_WriteByte(DUMMY);
  425. MSD_WriteByte(DUMMY);
  426. /* Set response value to success */
  427. rvalue = MSD_RESPONSE_NO_ERROR;
  428. }
  429. /* MSD chip select high */
  430. MSD_CS_HIGH();
  431. /* Send dummy byte: 8 Clock pulses of delay */
  432. MSD_WriteByte(DUMMY);
  433. /* Byte 0 */
  434. MSD_cid->ManufacturerID = CID_Tab[0];
  435. /* Byte 1 */
  436. MSD_cid->OEM_AppliID = CID_Tab[1] << 8;
  437. /* Byte 2 */
  438. MSD_cid->OEM_AppliID |= CID_Tab[2];
  439. /* Byte 3 */
  440. MSD_cid->ProdName1 = CID_Tab[3] << 24;
  441. /* Byte 4 */
  442. MSD_cid->ProdName1 |= CID_Tab[4] << 16;
  443. /* Byte 5 */
  444. MSD_cid->ProdName1 |= CID_Tab[5] << 8;
  445. /* Byte 6 */
  446. MSD_cid->ProdName1 |= CID_Tab[6];
  447. /* Byte 7 */
  448. MSD_cid->ProdName2 = CID_Tab[7];
  449. /* Byte 8 */
  450. MSD_cid->ProdRev = CID_Tab[8];
  451. /* Byte 9 */
  452. MSD_cid->ProdSN = CID_Tab[9] << 24;
  453. /* Byte 10 */
  454. MSD_cid->ProdSN |= CID_Tab[10] << 16;
  455. /* Byte 11 */
  456. MSD_cid->ProdSN |= CID_Tab[11] << 8;
  457. /* Byte 12 */
  458. MSD_cid->ProdSN |= CID_Tab[12];
  459. /* Byte 13 */
  460. MSD_cid->Reserved1 |= (CID_Tab[13] & 0xF0) >> 4;
  461. /* Byte 14 */
  462. MSD_cid->ManufactDate = (CID_Tab[13] & 0x0F) << 8;
  463. /* Byte 15 */
  464. MSD_cid->ManufactDate |= CID_Tab[14];
  465. /* Byte 16 */
  466. MSD_cid->msd_CRC = (CID_Tab[15] & 0xFE) >> 1;
  467. MSD_cid->Reserved2 = 1;
  468. /* Return the reponse */
  469. return rvalue;
  470. }
  471. /*******************************************************************************
  472. * Function Name : MSD_SendCmd
  473. * Description : Send 5 bytes command to the MSD card.
  474. * Input : - Cmd: the user expected command to send to MSD card
  475. * - Arg: the command argument
  476. * - Crc: the CRC
  477. * Output : None
  478. * Return : None
  479. *******************************************************************************/
  480. void MSD_SendCmd(u8 Cmd, u32 Arg, u8 Crc)
  481. {
  482. u32 i = 0x00;
  483. u8 Frame[6];
  484. /* Construct byte1 */
  485. Frame[0] = (Cmd | 0x40);
  486. /* Construct byte2 */
  487. Frame[1] = (u8)(Arg >> 24);
  488. /* Construct byte3 */
  489. Frame[2] = (u8)(Arg >> 16);
  490. /* Construct byte4 */
  491. Frame[3] = (u8)(Arg >> 8);
  492. /* Construct byte5 */
  493. Frame[4] = (u8)(Arg);
  494. /* Construct CRC: byte6 */
  495. Frame[5] = (Crc);
  496. /* Send the Cmd bytes */
  497. for (i = 0; i < 6; i++)
  498. {
  499. MSD_WriteByte(Frame[i]);
  500. }
  501. }
  502. /*******************************************************************************
  503. * Function Name : MSD_GetDataResponse
  504. * Description : Get MSD card data response.
  505. * Input : None
  506. * Output : None
  507. * Return : The MSD status: Read data response xxx0<status>1
  508. * - status 010: Data accecpted
  509. * - status 101: Data rejected due to a crc error
  510. * - status 110: Data rejected due to a Write error.
  511. * - status 111: Data rejected due to other error.
  512. *******************************************************************************/
  513. u8 MSD_GetDataResponse(void)
  514. {
  515. u32 i = 0;
  516. u8 response, rvalue;
  517. while (i <= 64)
  518. {
  519. /* Read resonse */
  520. response = MSD_ReadByte();
  521. /* Mask unused bits */
  522. response &= 0x1F;
  523. switch (response)
  524. {
  525. case MSD_DATA_OK:
  526. {
  527. rvalue = MSD_DATA_OK;
  528. break;
  529. }
  530. case MSD_DATA_CRC_ERROR:
  531. return MSD_DATA_CRC_ERROR;
  532. case MSD_DATA_WRITE_ERROR:
  533. return MSD_DATA_WRITE_ERROR;
  534. default:
  535. {
  536. rvalue = MSD_DATA_OTHER_ERROR;
  537. break;
  538. }
  539. }
  540. /* Exit loop in case of data ok */
  541. if (rvalue == MSD_DATA_OK)
  542. break;
  543. /* Increment loop counter */
  544. i++;
  545. }
  546. /* Wait null data */
  547. while (MSD_ReadByte() == 0);
  548. /* Return response */
  549. return response;
  550. }
  551. /*******************************************************************************
  552. * Function Name : MSD_GetResponse
  553. * Description : Returns the MSD response.
  554. * Input : None
  555. * Output : None
  556. * Return : The MSD Response: - MSD_RESPONSE_FAILURE: Sequence failed
  557. * - MSD_RESPONSE_NO_ERROR: Sequence succeed
  558. *******************************************************************************/
  559. u8 MSD_GetResponse(u8 Response)
  560. {
  561. u32 Count = 0xFFF;
  562. /* Check if response is got or a timeout is happen */
  563. while ((MSD_ReadByte() != Response) && Count)
  564. {
  565. Count--;
  566. }
  567. if (Count == 0)
  568. {
  569. /* After time out */
  570. return MSD_RESPONSE_FAILURE;
  571. }
  572. else
  573. {
  574. /* Right response got */
  575. return MSD_RESPONSE_NO_ERROR;
  576. }
  577. }
  578. /*******************************************************************************
  579. * Function Name : MSD_GetStatus
  580. * Description : Returns the MSD status.
  581. * Input : None
  582. * Output : None
  583. * Return : The MSD status.
  584. *******************************************************************************/
  585. u16 MSD_GetStatus(void)
  586. {
  587. u16 Status = 0;
  588. /* MSD chip select low */
  589. MSD_CS_LOW();
  590. /* Send CMD13 (MSD_SEND_STATUS) to get MSD status */
  591. MSD_SendCmd(MSD_SEND_STATUS, 0, 0xFF);
  592. Status = MSD_ReadByte();
  593. Status |= (u16)(MSD_ReadByte() << 8);
  594. /* MSD chip select high */
  595. MSD_CS_HIGH();
  596. /* Send dummy byte 0xFF */
  597. MSD_WriteByte(DUMMY);
  598. return Status;
  599. }
  600. /*******************************************************************************
  601. * Function Name : MSD_GoIdleState
  602. * Description : Put MSD in Idle state.
  603. * Input : None
  604. * Output : None
  605. * Return : The MSD Response: - MSD_RESPONSE_FAILURE: Sequence failed
  606. * - MSD_RESPONSE_NO_ERROR: Sequence succeed
  607. *******************************************************************************/
  608. u8 MSD_GoIdleState(void)
  609. {
  610. /* MSD chip select low */
  611. MSD_CS_LOW();
  612. /* Send CMD0 (GO_IDLE_STATE) to put MSD in SPI mode */
  613. MSD_SendCmd(MSD_GO_IDLE_STATE, 0, 0x95);
  614. /* Wait for In Idle State Response (R1 Format) equal to 0x01 */
  615. if (MSD_GetResponse(MSD_IN_IDLE_STATE))
  616. {
  617. /* No Idle State Response: return response failue */
  618. return MSD_RESPONSE_FAILURE;
  619. }
  620. /*----------Activates the card initialization process-----------*/
  621. do
  622. {
  623. /* MSD chip select high */
  624. MSD_CS_HIGH();
  625. /* Send Dummy byte 0xFF */
  626. MSD_WriteByte(DUMMY);
  627. /* MSD chip select low */
  628. MSD_CS_LOW();
  629. /* Send CMD1 (Activates the card process) until response equal to 0x0 */
  630. MSD_SendCmd(MSD_SEND_OP_COND, 0, 0xFF);
  631. /* Wait for no error Response (R1 Format) equal to 0x00 */
  632. }
  633. while (MSD_GetResponse(MSD_RESPONSE_NO_ERROR));
  634. /* MSD chip select high */
  635. MSD_CS_HIGH();
  636. /* Send dummy byte 0xFF */
  637. MSD_WriteByte(DUMMY);
  638. return MSD_RESPONSE_NO_ERROR;
  639. }
  640. /*******************************************************************************
  641. * Function Name : MSD_WriteByte
  642. * Description : Write a byte on the MSD.
  643. * Input : Data: byte to send.
  644. * Output : None
  645. * Return : None.
  646. *******************************************************************************/
  647. void MSD_WriteByte(u8 Data)
  648. {
  649. /* Wait until the transmit buffer is empty */
  650. while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
  651. /* Send the byte */
  652. SPI_I2S_SendData(SPI1, Data);
  653. }
  654. /*******************************************************************************
  655. * Function Name : MSD_ReadByte
  656. * Description : Read a byte from the MSD.
  657. * Input : None.
  658. * Output : None
  659. * Return : The received byte.
  660. *******************************************************************************/
  661. u8 MSD_ReadByte(void)
  662. {
  663. u8 Data = 0;
  664. /* Wait until the transmit buffer is empty */
  665. while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
  666. /* Send the byte */
  667. SPI_I2S_SendData(SPI1, DUMMY);
  668. /* Wait until a data is received */
  669. while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);
  670. /* Get the received data */
  671. Data = SPI_I2S_ReceiveData(SPI1);
  672. /* Return the shifted data */
  673. return Data;
  674. }
  675. /*******************************************************************************
  676. * Function Name : SPI_Config
  677. * Description : Initializes the SPI1 and CS pins.
  678. * Input : None
  679. * Output : None
  680. * Return : None
  681. *******************************************************************************/
  682. void SPI_Config(void)
  683. {
  684. u16 i, j;
  685. GPIO_InitTypeDef GPIO_InitStructure;
  686. SPI_InitTypeDef SPI_InitStructure;
  687. /* GPIOA and GPIOC Periph clock enable */
  688. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOD, ENABLE);
  689. /* SPI1 Periph clock enable */
  690. RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
  691. /* Configure SPI1 pins: SCK, MISO and MOSI */
  692. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
  693. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  694. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  695. GPIO_Init(GPIOA, &GPIO_InitStructure);
  696. /* Configure PD9 pin: CS pin ,PD10 : SD Power */
  697. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9|GPIO_Pin_10;
  698. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  699. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  700. GPIO_Init(GPIOD, &GPIO_InitStructure);
  701. /* SPI1 Config */
  702. SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
  703. SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
  704. SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
  705. SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;
  706. SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
  707. SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
  708. SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4;
  709. SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
  710. SPI_InitStructure.SPI_CRCPolynomial = 7;
  711. SPI_Init(SPI1, &SPI_InitStructure);
  712. /* SPI1 enable */
  713. SPI_Cmd(SPI1, ENABLE);
  714. /* active SD card */
  715. GPIO_ResetBits(GPIOD, GPIO_Pin_10);
  716. for(i=0;i<65530;i++)
  717. {
  718. for(j=0;j<5000;j++)
  719. ;
  720. }
  721. }
  722. /******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/
  723. /*
  724. * RT-Thread SD Card Driver
  725. * 20090417 Bernard
  726. */
  727. #include <rtthread.h>
  728. #include <dfs_fs.h>
  729. static struct rt_device sdcard_device;
  730. static struct dfs_partition part;
  731. #define SECTOR_SIZE 512
  732. /* RT-Thread Device Driver Interface */
  733. static rt_err_t rt_msd_init(rt_device_t dev)
  734. {
  735. sMSD_CSD MSD_csd;
  736. MSD_GetCSDRegister(&MSD_csd);
  737. return RT_EOK;
  738. }
  739. static rt_err_t rt_msd_open(rt_device_t dev, rt_uint16_t oflag)
  740. {
  741. return RT_EOK;
  742. }
  743. static rt_err_t rt_msd_close(rt_device_t dev)
  744. {
  745. return RT_EOK;
  746. }
  747. static rt_size_t rt_msd_read(rt_device_t dev, rt_off_t pos, void* buffer, rt_size_t size)
  748. {
  749. rt_uint8_t status;
  750. rt_uint32_t i;
  751. status = MSD_RESPONSE_NO_ERROR;
  752. rt_kprintf("read: 0x%x, size %d\n", pos, size);
  753. /* read all sectors */
  754. for (i = 0; i < size / SECTOR_SIZE; i ++)
  755. {
  756. status = MSD_ReadBlock((rt_uint8_t*)((rt_uint8_t*)buffer + i * SECTOR_SIZE),
  757. (part.offset + i)* SECTOR_SIZE + pos,
  758. SECTOR_SIZE);
  759. if (status != MSD_RESPONSE_NO_ERROR)
  760. {
  761. rt_kprintf("sd card read failed\n");
  762. return 0;
  763. }
  764. }
  765. if (status == MSD_RESPONSE_NO_ERROR) return size;
  766. rt_kprintf("read failed: %d\n", status);
  767. return 0;
  768. }
  769. static rt_size_t rt_msd_write (rt_device_t dev, rt_off_t pos, const void* buffer, rt_size_t size)
  770. {
  771. rt_uint8_t status;
  772. rt_uint32_t i;
  773. status = MSD_RESPONSE_NO_ERROR;
  774. rt_kprintf("write: 0x%x, size %d\n", pos, size);
  775. /* read all sectors */
  776. for (i = 0; i < size / SECTOR_SIZE; i ++)
  777. {
  778. status = MSD_WriteBuffer((rt_uint8_t*)((rt_uint8_t*)buffer + i * SECTOR_SIZE),
  779. (part.offset + i)* SECTOR_SIZE + pos,
  780. SECTOR_SIZE);
  781. if (status != MSD_RESPONSE_NO_ERROR)
  782. {
  783. rt_kprintf("sd card write failed\n");
  784. return 0;
  785. }
  786. }
  787. if (status == MSD_RESPONSE_NO_ERROR) return size;
  788. rt_kprintf("write failed: %d\n", status);
  789. return 0;
  790. }
  791. static rt_err_t rt_msd_control(rt_device_t dev, rt_uint8_t cmd, void *args)
  792. {
  793. return RT_EOK;
  794. }
  795. void rt_hw_msd_init()
  796. {
  797. if (MSD_Init() == MSD_RESPONSE_NO_ERROR)
  798. {
  799. rt_uint8_t status;
  800. rt_uint8_t *sector;
  801. /* register sdcard device */
  802. sdcard_device.init = rt_msd_init;
  803. sdcard_device.open = rt_msd_open;
  804. sdcard_device.close = rt_msd_close;
  805. sdcard_device.read = rt_msd_read;
  806. sdcard_device.write = rt_msd_write;
  807. sdcard_device.control = rt_msd_control;
  808. /* no private */
  809. sdcard_device.private = RT_NULL;
  810. /* get the first sector to read partition table */
  811. sector = (rt_uint8_t*) rt_malloc (512);
  812. if (sector == RT_NULL)
  813. {
  814. rt_kprintf("allocate partition sector buffer failed\n");
  815. return;
  816. }
  817. status = MSD_ReadBlock(sector, 0, 512);
  818. if (status == MSD_RESPONSE_NO_ERROR)
  819. {
  820. /* get the first partition */
  821. status = dfs_filesystem_get_partition(&part, sector, 0);
  822. if (status != RT_EOK)
  823. {
  824. /* there is no partition table */
  825. part.offset = 0;
  826. part.size = 0;
  827. }
  828. }
  829. else
  830. {
  831. /* there is no partition table */
  832. part.offset = 0;
  833. part.size = 0;
  834. }
  835. /* release sector buffer */
  836. rt_free(sector);
  837. rt_device_register(&sdcard_device, "sd0",
  838. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_REMOVABLE | RT_DEVICE_FLAG_STANDALONE);
  839. }
  840. else
  841. {
  842. rt_kprintf("sdcard init failed\n");
  843. }
  844. }