25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

661 lines
18 KiB

  1. /*------------------------------------------------------------------------/
  2. / MMCv3/SDv1/SDv2+ (in SPI mode) control module
  3. /-------------------------------------------------------------------------/
  4. /
  5. / Copyright (C) 2014, ChaN, all right reserved.
  6. /
  7. / * This software is a free software and there is NO WARRANTY.
  8. / * No restriction on use. You can use, modify and redistribute it for
  9. / personal, non-profit or commercial products UNDER YOUR RESPONSIBILITY.
  10. / * Redistributions of source code must retain the above copyright notice.
  11. /
  12. /-------------------------------------------------------------------------*/
  13. //#include <p24FJ64GA002.h>
  14. //#include <plib.h>
  15. #include "DigitalIO.h"
  16. #include "diskio.h"
  17. static inline __attribute__((always_inline)) unsigned char SPICalculateBRG(unsigned int pb_clk, unsigned int spi_clk);
  18. static void set_fast_clk(void);
  19. static void set_slow_clk(void);
  20. /* Socket controls (Platform dependent) */
  21. #define CS_LOW() SD_SPI_CS_PIN = 0 /* MMC CS = L */
  22. #define CS_HIGH() SD_SPI_CS_PIN = 1 /* MMC CS = H */
  23. #define MMC_CD (true) /* Card detected (yes:true, no:false, default:true) */
  24. #define MMC_WP (false) /* Write protected (yes:true, no:false, default:false) */
  25. /* SPI bit rate controls */
  26. #define FCLK_SLOW() set_slow_clk() /* Set slow clock for card initialization (100k-400k) */
  27. #define FCLK_FAST() set_fast_clk() /* Set fast clock for generic read/write */
  28. /*--------------------------------------------------------------------------
  29. Module Private Functions
  30. ---------------------------------------------------------------------------*/
  31. /* Definitions for MMC/SDC command */
  32. #define CMD0 (0) /* GO_IDLE_STATE */
  33. #define CMD1 (1) /* SEND_OP_COND */
  34. #define ACMD41 (41|0x80) /* SEND_OP_COND (SDC) */
  35. #define CMD8 (8) /* SEND_IF_COND */
  36. #define CMD9 (9) /* SEND_CSD */
  37. #define CMD10 (10) /* SEND_CID */
  38. #define CMD12 (12) /* STOP_TRANSMISSION */
  39. #define ACMD13 (13|0x80) /* SD_STATUS (SDC) */
  40. #define CMD16 (16) /* SET_BLOCKLEN */
  41. #define CMD17 (17) /* READ_SINGLE_BLOCK */
  42. #define CMD18 (18) /* READ_MULTIPLE_BLOCK */
  43. #define CMD23 (23) /* SET_BLOCK_COUNT */
  44. #define ACMD23 (23|0x80) /* SET_WR_BLK_ERASE_COUNT (SDC) */
  45. #define CMD24 (24) /* WRITE_BLOCK */
  46. #define CMD25 (25) /* WRITE_MULTIPLE_BLOCK */
  47. #define CMD41 (41) /* SEND_OP_COND (ACMD) */
  48. #define CMD55 (55) /* APP_CMD */
  49. #define CMD58 (58) /* READ_OCR */
  50. static volatile
  51. DSTATUS Stat = STA_NOINIT; /* Disk status */
  52. static volatile
  53. UINT Timer1, Timer2; /* 1000Hz decrement timer */
  54. static
  55. UINT CardType;
  56. static inline __attribute__((always_inline)) unsigned char SPICalculateBRG(unsigned int pb_clk, unsigned int spi_clk)
  57. {
  58. unsigned int brg;
  59. brg = pb_clk / (2 * spi_clk);
  60. if(pb_clk % (2 * spi_clk))
  61. brg++;
  62. if(brg > 0x100)
  63. brg = 0x100;
  64. if(brg)
  65. brg--;
  66. return (unsigned char) brg;
  67. }
  68. /*-----------------------------------------------------------------------*/
  69. /* Interface Controls (Platform dependent) */
  70. /*-----------------------------------------------------------------------*/
  71. /* When the target system does not support socket power control, there */
  72. /* is nothing to do in these functions. */
  73. static void power_on (void)
  74. {
  75. SPI2CON = 0;
  76. SPI2CONbits.MSTEN = 1;
  77. SPI2CONbits.CKE = 0;
  78. SPI2CONbits.CKP = 1;
  79. FCLK_SLOW();
  80. SPI2CONbits.ON = 1;
  81. return;
  82. }
  83. static void power_off (void)
  84. {
  85. SPI2CONbits.ON = 0; /* Disable SPI2 */
  86. }
  87. static void set_slow_clk(void)
  88. {
  89. unsigned WasON = SPI2CONbits.ON;
  90. SPI2CONbits.ON = 0;
  91. SPI2BRG = SPICalculateBRG(80000000,40000);
  92. SPI2CONbits.ON = WasON;
  93. }
  94. static void set_fast_clk(void)
  95. {
  96. unsigned WasON = SPI2CONbits.ON;
  97. SPI2CONbits.ON = 0;
  98. SPI2BRG = SPICalculateBRG(80000000,20000000);
  99. SPI2CONbits.ON = WasON;
  100. }
  101. /*-----------------------------------------------------------------------*/
  102. /* SPI Transactions (Platform dependent) */
  103. /*-----------------------------------------------------------------------*/
  104. /* Single byte SPI transaction */
  105. static BYTE xchg_spi (BYTE dat)
  106. {
  107. #ifndef USE_PINGUINO
  108. while( SPI2STATbits.SPITBF == 1 )
  109. {
  110. }
  111. #endif
  112. // ----------------
  113. // sending data
  114. // ----------------
  115. SPI2BUF = dat;
  116. while(SPI2STATbits.SPIRBF == 0)
  117. {
  118. }
  119. return (BYTE)SPI2BUF; /* Get received byte */
  120. }
  121. /* Multi-byte SPI transaction (transmit) */
  122. static
  123. void xmit_spi_multi (
  124. const BYTE* buff, /* Data to be sent */
  125. UINT cnt /* Number of bytes to send */
  126. )
  127. {
  128. do {
  129. SPI2BUF = *buff++; /* Initiate an SPI transaction */
  130. while (SPI2STATbits.SPIRBF == 0) ; /* Wait for end of the SPI transaction */
  131. SPI2BUF; /* Discard received byte */
  132. SPI2BUF = *buff++;
  133. while (SPI2STATbits.SPIRBF == 0) ;
  134. SPI2BUF;
  135. } while (cnt -= 2);
  136. }
  137. /* Multi-byte SPI transaction (receive) */
  138. static
  139. void rcvr_spi_multi (
  140. BYTE* buff, /* Buffer to store received data */
  141. UINT cnt /* Number of bytes to receive */
  142. )
  143. {
  144. do {
  145. SPI2BUF = 0xFF; /* Initiate an SPI transaction */
  146. while (SPI2STATbits.SPIRBF == 0) ; /* Wait for end of the SPI transaction */
  147. *buff++ = SPI2BUF; /* Get received byte */
  148. SPI2BUF = 0xFF;
  149. while (SPI2STATbits.SPIRBF == 0) ;
  150. *buff++ = SPI2BUF;
  151. } while (cnt -= 2);
  152. }
  153. /*-----------------------------------------------------------------------*/
  154. /* Wait for card ready */
  155. /*-----------------------------------------------------------------------*/
  156. static
  157. int wait_ready (void)
  158. {
  159. BYTE d;
  160. Timer2 = 500; /* Wait for ready in timeout of 500ms */
  161. do {
  162. d = xchg_spi(0xFF);
  163. } while ((d != 0xFF) && Timer2);
  164. return (d == 0xFF) ? 1 : 0;
  165. }
  166. /*-----------------------------------------------------------------------*/
  167. /* Deselect the card and release SPI bus */
  168. /*-----------------------------------------------------------------------*/
  169. static
  170. void deselect (void)
  171. {
  172. CS_HIGH(); /* Set CS# high */
  173. xchg_spi(0xFF); /* Dummy clock (force DO hi-z for multiple slave SPI) */
  174. }
  175. /*-----------------------------------------------------------------------*/
  176. /* Select the card and wait ready */
  177. /*-----------------------------------------------------------------------*/
  178. static
  179. int select (void) /* 1:Successful, 0:Timeout */
  180. {
  181. CS_LOW(); /* Set CS# low */
  182. xchg_spi(0xFF); /* Dummy clock (force DO enabled) */
  183. if (wait_ready()) return 1; /* Wait for card ready */
  184. deselect();
  185. return 0; /* Timeout */
  186. }
  187. /*-----------------------------------------------------------------------*/
  188. /* Receive a data packet from MMC */
  189. /*-----------------------------------------------------------------------*/
  190. static
  191. int rcvr_datablock ( /* 1:OK, 0:Failed */
  192. BYTE *buff, /* Data buffer to store received data */
  193. UINT btr /* Byte count (must be multiple of 4) */
  194. )
  195. {
  196. BYTE token;
  197. Timer1 = 100;
  198. do { /* Wait for data packet in timeout of 100ms */
  199. token = xchg_spi(0xFF);
  200. } while ((token == 0xFF) && Timer1);
  201. if(token != 0xFE) return 0; /* If not valid data token, retutn with error */
  202. rcvr_spi_multi(buff, btr); /* Receive the data block into buffer */
  203. xchg_spi(0xFF); /* Discard CRC */
  204. xchg_spi(0xFF);
  205. return 1; /* Return with success */
  206. }
  207. /*-----------------------------------------------------------------------*/
  208. /* Send a data packet to MMC */
  209. /*-----------------------------------------------------------------------*/
  210. #if _USE_WRITE
  211. static
  212. int xmit_datablock ( /* 1:OK, 0:Failed */
  213. const BYTE *buff, /* 512 byte data block to be transmitted */
  214. BYTE token /* Data token */
  215. )
  216. {
  217. BYTE resp;
  218. if (!wait_ready()) return 0;
  219. xchg_spi(token); /* Xmit a token */
  220. if (token != 0xFD) { /* Not StopTran token */
  221. xmit_spi_multi(buff, 512); /* Xmit the data block to the MMC */
  222. xchg_spi(0xFF); /* CRC (Dummy) */
  223. xchg_spi(0xFF);
  224. resp = xchg_spi(0xFF); /* Receive a data response */
  225. if ((resp & 0x1F) != 0x05) return 0; /* If not accepted, return with error */
  226. }
  227. return 1;
  228. }
  229. #endif
  230. /*-----------------------------------------------------------------------*/
  231. /* Send a command packet to MMC */
  232. /*-----------------------------------------------------------------------*/
  233. static
  234. BYTE send_cmd (
  235. BYTE cmd, /* Command byte */
  236. DWORD arg /* Argument */
  237. )
  238. {
  239. BYTE n, res;
  240. if (cmd & 0x80) { /* ACMD<n> is the command sequense of CMD55-CMD<n> */
  241. cmd &= 0x7F;
  242. res = send_cmd(CMD55, 0);
  243. if (res > 1) return res;
  244. }
  245. /* Select the card and wait for ready except to stop multiple block read */
  246. if (cmd != CMD12) {
  247. deselect();
  248. if (!select()) return 0xFF;
  249. }
  250. /* Send command packet */
  251. xchg_spi(0x40 | cmd); /* Start + Command index */
  252. xchg_spi((BYTE)(arg >> 24)); /* Argument[31..24] */
  253. xchg_spi((BYTE)(arg >> 16)); /* Argument[23..16] */
  254. xchg_spi((BYTE)(arg >> 8)); /* Argument[15..8] */
  255. xchg_spi((BYTE)arg); /* Argument[7..0] */
  256. n = 0x01; /* Dummy CRC + Stop */
  257. if (cmd == CMD0) n = 0x95; /* Valid CRC for CMD0(0) + Stop */
  258. if (cmd == CMD8) n = 0x87; /* Valid CRC for CMD8(0x1AA) + Stop */
  259. xchg_spi(n);
  260. /* Receive command response */
  261. if (cmd == CMD12) xchg_spi(0xFF); /* Skip a stuff byte on stop to read */
  262. n = 10; /* Wait for a valid response in timeout of 10 attempts */
  263. do {
  264. res = xchg_spi(0xFF);
  265. } while ((res & 0x80) && --n);
  266. return res; /* Return with the response value */
  267. }
  268. /*--------------------------------------------------------------------------
  269. Public Functions
  270. ---------------------------------------------------------------------------*/
  271. /*-----------------------------------------------------------------------*/
  272. /* Get Disk Status */
  273. /*-----------------------------------------------------------------------*/
  274. DSTATUS disk_status (
  275. BYTE pdrv /* Physical drive nmuber (0) */
  276. )
  277. {
  278. if (pdrv != 0) return STA_NOINIT; /* Supports only single drive */
  279. return Stat;
  280. }
  281. /*-----------------------------------------------------------------------*/
  282. /* Initialize Disk Drive */
  283. /*-----------------------------------------------------------------------*/
  284. DSTATUS disk_initialize (
  285. BYTE pdrv /* Physical drive nmuber (0) */
  286. )
  287. {
  288. BYTE n, cmd, ty, ocr[4];
  289. if (pdrv != 0) return STA_NOINIT; /* Supports only single drive */
  290. if (Stat & STA_NODISK) return Stat; /* No card in the socket */
  291. power_on(); /* Initialize memory card interface */
  292. FCLK_SLOW();
  293. for (n = 10; n; n--) xchg_spi(0xFF); /* 80 dummy clocks */
  294. ty = 0;
  295. if (send_cmd(CMD0, 0) == 1) { /* Enter Idle state */
  296. Timer1 = 1000; /* Initialization timeout of 1000 msec */
  297. if (send_cmd(CMD8, 0x1AA) == 1) { /* SDv2? */
  298. for (n = 0; n < 4; n++) ocr[n] = xchg_spi(0xFF); /* Get trailing return value of R7 resp */
  299. if (ocr[2] == 0x01 && ocr[3] == 0xAA) { /* The card can work at vdd range of 2.7-3.6V */
  300. while (Timer1 && send_cmd(ACMD41, 0x40000000)); /* Wait for leaving idle state (ACMD41 with HCS bit) */
  301. if (Timer1 && send_cmd(CMD58, 0) == 0) { /* Check CCS bit in the OCR */
  302. for (n = 0; n < 4; n++) ocr[n] = xchg_spi(0xFF);
  303. ty = (ocr[0] & 0x40) ? CT_SD2|CT_BLOCK : CT_SD2; /* SDv2+ */
  304. }
  305. }
  306. } else { /* SDv1 or MMCv3 */
  307. if (send_cmd(ACMD41, 0) <= 1) {
  308. ty = CT_SD1; cmd = ACMD41; /* SDv1 */
  309. } else {
  310. ty = CT_MMC; cmd = CMD1; /* MMCv3 */
  311. }
  312. while (Timer1 && send_cmd(cmd, 0)); /* Wait for leaving idle state */
  313. if (!Timer1 || send_cmd(CMD16, 512) != 0) ty = 0; /* Set read/write block length to 512 */
  314. }
  315. }
  316. CardType = ty;
  317. deselect();
  318. if (ty) { /* Function succeded */
  319. Stat &= ~STA_NOINIT; /* Clear STA_NOINIT */
  320. FCLK_FAST();
  321. } else { /* Function failed */
  322. power_off(); /* Deinitialize interface */
  323. }
  324. return Stat;
  325. }
  326. /*-----------------------------------------------------------------------*/
  327. /* Read Sector(s) */
  328. /*-----------------------------------------------------------------------*/
  329. DRESULT disk_read (
  330. BYTE pdrv, /* Physical drive nmuber (0) */
  331. BYTE *buff, /* Pointer to the data buffer to store read data */
  332. DWORD sector, /* Start sector number (LBA) */
  333. UINT count /* Sector count (1..128) */
  334. )
  335. {
  336. if (pdrv || !count) return RES_PARERR;
  337. if (Stat & STA_NOINIT) return RES_NOTRDY;
  338. if (!(CardType & CT_BLOCK)) sector *= 512; /* Convert to byte address if needed */
  339. if (count == 1) { /* Single block read */
  340. if ((send_cmd(CMD17, sector) == 0) /* READ_SINGLE_BLOCK */
  341. && rcvr_datablock(buff, 512)) {
  342. count = 0;
  343. }
  344. }
  345. else { /* Multiple block read */
  346. if (send_cmd(CMD18, sector) == 0) { /* READ_MULTIPLE_BLOCK */
  347. do {
  348. if (!rcvr_datablock(buff, 512)) break;
  349. buff += 512;
  350. } while (--count);
  351. send_cmd(CMD12, 0); /* STOP_TRANSMISSION */
  352. }
  353. }
  354. deselect();
  355. return count ? RES_ERROR : RES_OK;
  356. }
  357. /*-----------------------------------------------------------------------*/
  358. /* Write Sector(s) */
  359. /*-----------------------------------------------------------------------*/
  360. #if _USE_WRITE
  361. DRESULT disk_write (
  362. BYTE pdrv, /* Physical drive nmuber (0) */
  363. const BYTE *buff, /* Pointer to the data to be written */
  364. DWORD sector, /* Start sector number (LBA) */
  365. UINT count /* Sector count (1..128) */
  366. )
  367. {
  368. if (pdrv || !count) return RES_PARERR;
  369. if (Stat & STA_NOINIT) return RES_NOTRDY;
  370. if (Stat & STA_PROTECT) return RES_WRPRT;
  371. if (!(CardType & CT_BLOCK)) sector *= 512; /* Convert to byte address if needed */
  372. if (count == 1) { /* Single block write */
  373. if ((send_cmd(CMD24, sector) == 0) /* WRITE_BLOCK */
  374. && xmit_datablock(buff, 0xFE)) {
  375. count = 0;
  376. }
  377. }
  378. else { /* Multiple block write */
  379. if (CardType & CT_SDC) send_cmd(ACMD23, count);
  380. if (send_cmd(CMD25, sector) == 0) { /* WRITE_MULTIPLE_BLOCK */
  381. do {
  382. if (!xmit_datablock(buff, 0xFC)) break;
  383. buff += 512;
  384. } while (--count);
  385. if (!xmit_datablock(0, 0xFD)) count = 1; /* STOP_TRAN token */
  386. }
  387. }
  388. deselect();
  389. return count ? RES_ERROR : RES_OK;
  390. }
  391. #endif
  392. /*-----------------------------------------------------------------------*/
  393. /* Miscellaneous Functions */
  394. /*-----------------------------------------------------------------------*/
  395. #if _USE_IOCTL
  396. DRESULT disk_ioctl (
  397. BYTE pdrv, /* Physical drive nmuber (0) */
  398. BYTE cmd, /* Control code */
  399. void *buff /* Buffer to send/receive data block */
  400. )
  401. {
  402. DRESULT res;
  403. BYTE n, csd[16], *ptr = buff;
  404. DWORD csz;
  405. if (pdrv) return RES_PARERR;
  406. if (Stat & STA_NOINIT) return RES_NOTRDY;
  407. res = RES_ERROR;
  408. switch (cmd) {
  409. case CTRL_SYNC : /* Flush write-back cache, Wait for end of internal process */
  410. if (select()) res = RES_OK;
  411. break;
  412. case GET_SECTOR_COUNT : /* Get number of sectors on the disk (WORD) */
  413. if ((send_cmd(CMD9, 0) == 0) && rcvr_datablock(csd, 16)) {
  414. if ((csd[0] >> 6) == 1) { /* SDv2? */
  415. csz = csd[9] + ((WORD)csd[8] << 8) + ((DWORD)(csd[7] & 63) << 16) + 1;
  416. *(DWORD*)buff = csz << 10;
  417. } else { /* SDv1 or MMCv3 */
  418. n = (csd[5] & 15) + ((csd[10] & 128) >> 7) + ((csd[9] & 3) << 1) + 2;
  419. csz = (csd[8] >> 6) + ((WORD)csd[7] << 2) + ((WORD)(csd[6] & 3) << 10) + 1;
  420. *(DWORD*)buff = csz << (n - 9);
  421. }
  422. res = RES_OK;
  423. }
  424. break;
  425. case GET_BLOCK_SIZE : /* Get erase block size in unit of sectors (DWORD) */
  426. if (CardType & CT_SD2) { /* SDv2+? */
  427. if (send_cmd(ACMD13, 0) == 0) { /* Read SD status */
  428. xchg_spi(0xFF);
  429. if (rcvr_datablock(csd, 16)) { /* Read partial block */
  430. for (n = 64 - 16; n; n--) xchg_spi(0xFF); /* Purge trailing data */
  431. *(DWORD*)buff = 16UL << (csd[10] >> 4);
  432. res = RES_OK;
  433. }
  434. }
  435. } else { /* SDv1 or MMCv3 */
  436. if ((send_cmd(CMD9, 0) == 0) && rcvr_datablock(csd, 16)) { /* Read CSD */
  437. if (CardType & CT_SD1) { /* SDv1 */
  438. *(DWORD*)buff = (((csd[10] & 63) << 1) + ((WORD)(csd[11] & 128) >> 7) + 1) << ((csd[13] >> 6) - 1);
  439. } else { /* MMCv3 */
  440. *(DWORD*)buff = ((WORD)((csd[10] & 124) >> 2) + 1) * (((csd[11] & 3) << 3) + ((csd[11] & 224) >> 5) + 1);
  441. }
  442. res = RES_OK;
  443. }
  444. }
  445. break;
  446. case MMC_GET_TYPE : /* Get card type flags (1 byte) */
  447. *ptr = CardType;
  448. res = RES_OK;
  449. break;
  450. case MMC_GET_CSD : /* Receive CSD as a data block (16 bytes) */
  451. if ((send_cmd(CMD9, 0) == 0) /* READ_CSD */
  452. && rcvr_datablock(buff, 16))
  453. res = RES_OK;
  454. break;
  455. case MMC_GET_CID : /* Receive CID as a data block (16 bytes) */
  456. if ((send_cmd(CMD10, 0) == 0) /* READ_CID */
  457. && rcvr_datablock(buff, 16))
  458. res = RES_OK;
  459. break;
  460. case MMC_GET_OCR : /* Receive OCR as an R3 resp (4 bytes) */
  461. if (send_cmd(CMD58, 0) == 0) { /* READ_OCR */
  462. for (n = 0; n < 4; n++)
  463. *((BYTE*)buff+n) = xchg_spi(0xFF);
  464. res = RES_OK;
  465. }
  466. break;
  467. case MMC_GET_SDSTAT : /* Receive SD statsu as a data block (64 bytes) */
  468. if ((CardType & CT_SD2) && send_cmd(ACMD13, 0) == 0) { /* SD_STATUS */
  469. xchg_spi(0xFF);
  470. if (rcvr_datablock(buff, 64)) res = RES_OK;
  471. }
  472. break;
  473. case CTRL_POWER_OFF : /* Power off */
  474. power_off();
  475. Stat |= STA_NOINIT;
  476. res = RES_OK;
  477. break;
  478. default:
  479. res = RES_PARERR;
  480. }
  481. deselect();
  482. return res;
  483. }
  484. #endif
  485. /*-----------------------------------------------------------------------*/
  486. /* Device Timer Driven Procedure */
  487. /*-----------------------------------------------------------------------*/
  488. /* This function must be called by timer interrupt in period of 1ms */
  489. void disk_timerproc (void)
  490. {
  491. BYTE s;
  492. UINT n;
  493. n = Timer1; /* 1000Hz decrement timer with zero stopped */
  494. if (n) Timer1 = --n;
  495. n = Timer2;
  496. if (n) Timer2 = --n;
  497. /* Update socket status */
  498. s = Stat;
  499. if (MMC_WP) {
  500. s |= STA_PROTECT;
  501. } else {
  502. s &= ~STA_PROTECT;
  503. }
  504. if (MMC_CD) {
  505. s &= ~STA_NODISK;
  506. } else {
  507. s |= (STA_NODISK | STA_NOINIT);
  508. }
  509. Stat = s;
  510. }