mmc.c
Upload User: zcw0111
Upload Date: 2021-05-17
Package Size: 48k
Code Size: 13k
Category:

3G develop

Development Platform:

C/C++

  1. /*-----------------------------------------------------------------------*/
  2. /* MMC/SDC (in SPI mode) control module  (C)ChaN, 2006                   */
  3. /*-----------------------------------------------------------------------*/
  4. /* Only rcvr_spi(), xmit_spi(), disk_timerproc(), disk_initialize () and */
  5. /* some macros are platform dependent.                                   */
  6. /*-----------------------------------------------------------------------*/
  7. #include <avr/io.h>
  8. #include "diskio.h"
  9. /* Definitions for MMC/SDC command */
  10. #define CMD0 (0x40+0) /* GO_IDLE_STATE */
  11. #define CMD1 (0x40+1) /* SEND_OP_COND */
  12. #define CMD8 (0x40+8) /* SEND_IF_COND */
  13. #define CMD9 (0x40+9) /* SEND_CSD */
  14. #define CMD10 (0x40+10) /* SEND_CID */
  15. #define CMD12 (0x40+12) /* STOP_TRANSMISSION */
  16. #define CMD16 (0x40+16) /* SET_BLOCKLEN */
  17. #define CMD17 (0x40+17) /* READ_SINGLE_BLOCK */
  18. #define CMD18 (0x40+18) /* READ_MULTIPLE_BLOCK */
  19. #define CMD23 (0x40+23) /* SET_BLOCK_COUNT */
  20. #define CMD24 (0x40+24) /* WRITE_BLOCK */
  21. #define CMD25 (0x40+25) /* WRITE_MULTIPLE_BLOCK */
  22. #define CMD41 (0x40+41) /* SEND_OP_COND (ACMD) */
  23. #define CMD55 (0x40+55) /* APP_CMD */
  24. #define CMD58 (0x40+58) /* READ_OCR */
  25. /* Control signals (Platform dependent) */
  26. #define SELECT() PORTB &= ~_BV(2) /* MMC CS = L */
  27. #define DESELECT() PORTB |= _BV(2) /* MMC CS = H */
  28. extern volatile
  29. BYTE MmcTmr[2]; /* 100Hz decrement timers */
  30. /*--------------------------------------------------------------------------
  31.    Module Private Functions
  32. ---------------------------------------------------------------------------*/
  33. static volatile
  34. DSTATUS Stat; /* Disk status */
  35. static
  36. BYTE CardType; /* b0:MMC, b1:SDC, b2:Block addressing */
  37. /*-----------------------------------------------------------------------*/
  38. /* Transmit a byte to MMC via SPI  (Platform dependent)                  */
  39. /*-----------------------------------------------------------------------*/
  40. #define xmit_spi(dat)  SPDR=(dat); loop_until_bit_is_set(SPSR,SPIF)
  41. /*-----------------------------------------------------------------------*/
  42. /* Receive a byte from MMC via SPI  (Platform dependent)                 */
  43. /*-----------------------------------------------------------------------*/
  44. static
  45. BYTE rcvr_spi (void)
  46. {
  47. SPDR = 0xFF;
  48. loop_until_bit_is_set(SPSR, SPIF);
  49. return SPDR;
  50. }
  51. /* Alternative macro to receive data fast */
  52. #define rcvr_spi_m(dst) SPDR=0xFF; loop_until_bit_is_set(SPSR,SPIF); *(dst)=SPDR
  53. /*-----------------------------------------------------------------------*/
  54. /* Wait for card ready                                                   */
  55. /*-----------------------------------------------------------------------*/
  56. static
  57. BYTE wait_ready (void)
  58. {
  59. BYTE res;
  60. MmcTmr[1] = 50; /* Wait for ready in timeout of 500ms */
  61. rcvr_spi();
  62. do
  63. res = rcvr_spi();
  64. while ((res != 0xFF) && MmcTmr[1]);
  65. return res;
  66. }
  67. /*-----------------------------------------------------------------------*/
  68. /* Receive a data packet from MMC                                        */
  69. /*-----------------------------------------------------------------------*/
  70. static
  71. BOOL rcvr_datablock (
  72. BYTE *buff, /* Data buffer to store received data */
  73. UINT btr /* Byte count (must be even number) */
  74. )
  75. {
  76. BYTE token;
  77. MmcTmr[0] = 10;
  78. do { /* Wait for data packet in timeout of 100ms */
  79. token = rcvr_spi();
  80. } while ((token == 0xFF) && MmcTmr[0]);
  81. if(token != 0xFE) return FALSE; /* If not valid data token, retutn with error */
  82. do { /* Receive the data block into buffer */
  83. rcvr_spi_m(buff++);
  84. rcvr_spi_m(buff++);
  85. } while (btr -= 2);
  86. rcvr_spi(); /* Discard CRC */
  87. rcvr_spi();
  88. return TRUE; /* Return with success */
  89. }
  90. /*-----------------------------------------------------------------------*/
  91. /* Send a data packet to MMC                                             */
  92. /*-----------------------------------------------------------------------*/
  93. #if _READONLY == 0
  94. static
  95. BOOL xmit_datablock (
  96. const BYTE *buff, /* 512 byte data block to be transmitted */
  97. BYTE token /* Data/Stop token */
  98. )
  99. {
  100. BYTE resp, wc;
  101. if (wait_ready() != 0xFF) return FALSE;
  102. xmit_spi(token); /* Xmit data token */
  103. if (token != 0xFD) { /* Is data token */
  104. wc = 0;
  105. do { /* Xmit the 512 byte data block to MMC */
  106. xmit_spi(*buff++);
  107. xmit_spi(*buff++);
  108. } while (--wc);
  109. xmit_spi(0xFF); /* CRC (Dummy) */
  110. xmit_spi(0xFF);
  111. resp = rcvr_spi(); /* Reveive data response */
  112. if ((resp & 0x1F) != 0x05) /* If not accepted, return with error */
  113. return FALSE;
  114. }
  115. return TRUE;
  116. }
  117. #endif /* _READONLY */
  118. /*-----------------------------------------------------------------------*/
  119. /* Send a command packet to MMC                                          */
  120. /*-----------------------------------------------------------------------*/
  121. static
  122. BYTE send_cmd (
  123. BYTE cmd, /* Command byte */
  124. DWORD arg /* Argument */
  125. )
  126. {
  127. BYTE n, res;
  128. if (wait_ready() != 0xFF) return 0xFF;
  129. /* Send command packet */
  130. xmit_spi(cmd); /* Command */
  131. xmit_spi((BYTE)(arg >> 24)); /* Argument[31..24] */
  132. xmit_spi((BYTE)(arg >> 16)); /* Argument[23..16] */
  133. xmit_spi((BYTE)(arg >> 8)); /* Argument[15..8] */
  134. xmit_spi((BYTE)arg); /* Argument[7..0] */
  135. n = 0;
  136. if (cmd == CMD0) n = 0x95; /* CRC for CMD0(0) */
  137. if (cmd == CMD8) n = 0x87; /* CRC for CMD8(0x1AA) */
  138. xmit_spi(n);
  139. /* Receive command response */
  140. if (cmd == CMD12) rcvr_spi(); /* Skip a stuff byte when stop reading */
  141. n = 10; /* Wait for a valid response in timeout of 10 attempts */
  142. do
  143. res = rcvr_spi();
  144. while ((res & 0x80) && --n);
  145. return res; /* Return with the response value */
  146. }
  147. /*--------------------------------------------------------------------------
  148.    Public Functions
  149. ---------------------------------------------------------------------------*/
  150. /*-----------------------------------------------------------------------*/
  151. /* Initialize Disk Drive                                                 */
  152. /*-----------------------------------------------------------------------*/
  153. DSTATUS disk_initialize (
  154. BYTE drv /* Physical drive nmuber (0) */
  155. )
  156. {
  157. BYTE n, ty, ocr[4];
  158. if (drv) return STA_NOINIT; /* Supports only single drive */
  159. for (n = 10; n; n--) rcvr_spi(); /* 80 dummy clocks */
  160. SELECT(); /* CS = L */
  161. ty = 0;
  162. if (send_cmd(CMD0, 0) == 1) { /* Enter Idle state */
  163. MmcTmr[0] = 100; /* Initialization timeout of 1000 msec */
  164. if (send_cmd(CMD8, 0x1AA) == 1) { /* SDC Ver2+ */
  165. for (n = 0; n < 4; n++) ocr[n] = rcvr_spi();
  166. if (ocr[2] == 0x01 && ocr[3] == 0xAA) { /* The card can work at vdd range of 2.7-3.6V */
  167. do {
  168. if (send_cmd(CMD55, 0) <= 1 && send_cmd(CMD41, 1UL << 30) == 0) break; /* ACMD41 with HCS bit */
  169. } while (MmcTmr[0]);
  170. if (MmcTmr[0] && send_cmd(CMD58, 0) == 0) { /* Check CCS bit */
  171. for (n = 0; n < 4; n++) ocr[n] = rcvr_spi();
  172. ty = (ocr[0] & 0x40) ? 6 : 2;
  173. }
  174. }
  175. } else { /* SDC Ver1 or MMC */
  176. ty = (send_cmd(CMD55, 0) <= 1 && send_cmd(CMD41, 0) <= 1) ? 2 : 1; /* SDC : MMC */
  177. do {
  178. if (ty == 2) {
  179. if (send_cmd(CMD55, 0) <= 1 && send_cmd(CMD41, 0) == 0) break; /* ACMD41 */
  180. } else {
  181. if (send_cmd(CMD1, 0) == 0) break; /* CMD1 */
  182. }
  183. } while (MmcTmr[0]);
  184. if (!MmcTmr[0] || send_cmd(CMD16, 512) != 0) /* Select R/W block length */
  185. ty = 0;
  186. }
  187. }
  188. CardType = ty;
  189. DESELECT(); /* CS = H */
  190. rcvr_spi(); /* Idle (Release DO) */
  191. if (ty) { /* Initialization succeded */
  192. Stat &= ~STA_NOINIT; /* Clear STA_NOINIT */
  193. } else { /* Initialization failed */
  194. Stat |=  STA_NOINIT; /* Set STA_NOINIT */
  195. }
  196. return Stat;
  197. }
  198. /*-----------------------------------------------------------------------*/
  199. /* Get Disk Status                                                       */
  200. /*-----------------------------------------------------------------------*/
  201. DSTATUS disk_status (
  202. BYTE drv /* Physical drive nmuber (0) */
  203. )
  204. {
  205. if (drv) return STA_NOINIT; /* Supports only single drive */
  206. return Stat;
  207. }
  208. /*-----------------------------------------------------------------------*/
  209. /* Read Sector(s)                                                        */
  210. /*-----------------------------------------------------------------------*/
  211. DRESULT disk_read (
  212. BYTE drv, /* Physical drive nmuber (0) */
  213. BYTE *buff, /* Pointer to the data buffer to store read data */
  214. DWORD sector, /* Start sector number (LBA) */
  215. BYTE count /* Sector count (1..255) */
  216. )
  217. {
  218. if (drv || !count) return RES_PARERR;
  219. if (Stat & STA_NOINIT) return RES_NOTRDY;
  220. if (!(CardType & 4)) sector *= 512; /* Convert to byte address if needed */
  221. SELECT(); /* CS = L */
  222. if (count == 1) { /* Single block read */
  223. if ((send_cmd(CMD17, sector) == 0) /* READ_SINGLE_BLOCK */
  224. && rcvr_datablock(buff, 512))
  225. count = 0;
  226. }
  227. else { /* Multiple block read */
  228. if (send_cmd(CMD18, sector) == 0) { /* READ_MULTIPLE_BLOCK */
  229. do {
  230. if (!rcvr_datablock(buff, 512)) break;
  231. buff += 512;
  232. } while (--count);
  233. send_cmd(CMD12, 0); /* STOP_TRANSMISSION */
  234. }
  235. }
  236. DESELECT(); /* CS = H */
  237. rcvr_spi(); /* Idle (Release DO) */
  238. return count ? RES_ERROR : RES_OK;
  239. }
  240. /*-----------------------------------------------------------------------*/
  241. /* Write Sector(s)                                                       */
  242. /*-----------------------------------------------------------------------*/
  243. #if _READONLY == 0
  244. DRESULT disk_write (
  245. BYTE drv, /* Physical drive nmuber (0) */
  246. const BYTE *buff, /* Pointer to the data to be written */
  247. DWORD sector, /* Start sector number (LBA) */
  248. BYTE count /* Sector count (1..255) */
  249. )
  250. {
  251. if (drv || !count) return RES_PARERR;
  252. if (Stat & STA_NOINIT) return RES_NOTRDY;
  253. if (Stat & STA_PROTECT) return RES_WRPRT;
  254. if (!(CardType & 4)) sector *= 512; /* Convert to byte address if needed */
  255. SELECT(); /* CS = L */
  256. if (count == 1) { /* Single block write */
  257. if ((send_cmd(CMD24, sector) == 0) /* WRITE_BLOCK */
  258. && xmit_datablock(buff, 0xFE))
  259. count = 0;
  260. }
  261. else { /* Multiple block write */
  262. if (CardType & 2) {
  263. send_cmd(CMD55, 0); send_cmd(CMD23, count); /* ACMD23 */
  264. }
  265. if (send_cmd(CMD25, sector) == 0) { /* WRITE_MULTIPLE_BLOCK */
  266. do {
  267. if (!xmit_datablock(buff, 0xFC)) break;
  268. buff += 512;
  269. } while (--count);
  270. if (!xmit_datablock(0, 0xFD)) /* STOP_TRAN token */
  271. count = 1;
  272. }
  273. }
  274. DESELECT(); /* CS = H */
  275. rcvr_spi(); /* Idle (Release DO) */
  276. return count ? RES_ERROR : RES_OK;
  277. }
  278. #endif /* _READONLY */
  279. /*-----------------------------------------------------------------------*/
  280. /* Miscellaneous Functions                                               */
  281. /*-----------------------------------------------------------------------*/
  282. DRESULT disk_ioctl (
  283. BYTE drv, /* Physical drive nmuber (0) */
  284. BYTE ctrl, /* Control code */
  285. void *buff /* Buffer to send/receive data block */
  286. )
  287. {
  288. DRESULT res;
  289. BYTE n, csd[16], *ptr = buff;
  290. WORD csize;
  291. if (drv) return RES_PARERR;
  292. SELECT(); /* CS = L */
  293. res = RES_ERROR;
  294. switch (ctrl) {
  295. case GET_SECTOR_COUNT : /* Get number of sectors on the disk (DWORD) */
  296. if ((send_cmd(CMD9, 0) == 0) && rcvr_datablock(csd, 16)) {
  297. if ((csd[0] >> 6) == 1) { /* SDC ver 2.00 */
  298. csize = csd[9] + ((WORD)csd[8] << 8) + 1;
  299. *(DWORD*)buff = (DWORD)csize << 10;
  300. } else { /* MMC or SDC ver 1.XX */
  301. n = (csd[5] & 15) + ((csd[10] & 128) >> 7) + ((csd[9] & 3) << 1) + 2;
  302. csize = (csd[8] >> 6) + ((WORD)csd[7] << 2) + ((WORD)(csd[6] & 3) << 10) + 1;
  303. *(DWORD*)buff = (DWORD)csize << (n - 9);
  304. }
  305. res = RES_OK;
  306. }
  307. break;
  308. case GET_SECTOR_SIZE : /* Get sectors on the disk (WORD) */
  309. *(WORD*)buff = 512;
  310. res = RES_OK;
  311. break;
  312. case CTRL_SYNC : /* Make sure that data has been written */
  313. if (wait_ready() == 0xFF)
  314. res = RES_OK;
  315. break;
  316. case MMC_GET_CSD : /* Receive CSD as a data block (16 bytes) */
  317. if (Stat & STA_NOINIT) return RES_NOTRDY;
  318. if ((send_cmd(CMD9, 0) == 0) /* READ_CSD */
  319. && rcvr_datablock(ptr, 16))
  320. res = RES_OK;
  321. break;
  322. case MMC_GET_CID : /* Receive CID as a data block (16 bytes) */
  323. if (Stat & STA_NOINIT) return RES_NOTRDY;
  324. if ((send_cmd(CMD10, 0) == 0) /* READ_CID */
  325. && rcvr_datablock(ptr, 16))
  326. res = RES_OK;
  327. break;
  328. case MMC_GET_OCR : /* Receive OCR as an R3 resp (4 bytes) */
  329. if (Stat & STA_NOINIT) return RES_NOTRDY;
  330. if (send_cmd(CMD58, 0) == 0) { /* READ_OCR */
  331. for (n = 0; n < 4; n++)
  332. *ptr++ = rcvr_spi();
  333. res = RES_OK;
  334. }
  335. break;
  336. default:
  337. res = RES_PARERR;
  338. }
  339. DESELECT(); /* CS = H */
  340. rcvr_spi(); /* Idle (Release DO) */
  341. return res;
  342. }
  343. /*---------------------------------------*/
  344. /* Device timer interrupt procedure      */
  345. /* This must be called in period of 10ms */
  346. /* (Platform dependent)                  */
  347. void disk_timerproc (void)
  348. {
  349. BYTE n;
  350. n = MmcTmr[0]; /* 100Hz decrement timer */
  351. if (n) MmcTmr[0] = --n;
  352. n = MmcTmr[1];
  353. if (n) MmcTmr[1] = --n;
  354. }