diskio_spitf.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  1. /*-----------------------------------------------------------------------*/
  2. /* Low level disk I/O module skeleton for FatFs (C)ChaN, 2016 */
  3. /*-----------------------------------------------------------------------*/
  4. /* If a working storage control module is available, it should be */
  5. /* attached to the FatFs via a glue function rather than modifying it. */
  6. /* This is an example of glue functions to attach various exsisting */
  7. /* storage control modules to the FatFs module with a defined API. */
  8. /*-----------------------------------------------------------------------*/
  9. #include "luat_base.h"
  10. #include "luat_spi.h"
  11. #include "luat_timer.h"
  12. #include "luat_gpio.h"
  13. #include "lauxlib.h"
  14. #include "ff.h" /* Obtains integer types */
  15. #include "diskio.h" /* Declarations of disk functions */
  16. #define LUAT_LOG_TAG "luat.fatfs"
  17. #include "luat_log.h"
  18. /*--------------------------------------------------------------------------
  19. Module Private Functions
  20. ---------------------------------------------------------------------------*/
  21. /* MMC/SD command (SPI mode) */
  22. #define CMD0 (0) /* GO_IDLE_STATE */
  23. #define CMD1 (1) /* SEND_OP_COND */
  24. #define ACMD41 (0x80+41) /* SEND_OP_COND (SDC) */
  25. #define CMD8 (8) /* SEND_IF_COND */
  26. #define CMD9 (9) /* SEND_CSD */
  27. #define CMD10 (10) /* SEND_CID */
  28. #define CMD12 (12) /* STOP_TRANSMISSION */
  29. #define CMD13 (13) /* SEND_STATUS */
  30. #define ACMD13 (0x80+13) /* SD_STATUS (SDC) */
  31. #define CMD16 (16) /* SET_BLOCKLEN */
  32. #define CMD17 (17) /* READ_SINGLE_BLOCK */
  33. #define CMD18 (18) /* READ_MULTIPLE_BLOCK */
  34. #define CMD23 (23) /* SET_BLOCK_COUNT */
  35. #define ACMD23 (0x80+23) /* SET_WR_BLK_ERASE_COUNT (SDC) */
  36. #define CMD24 (24) /* WRITE_BLOCK */
  37. #define CMD25 (25) /* WRITE_MULTIPLE_BLOCK */
  38. #define CMD32 (32) /* ERASE_ER_BLK_START */
  39. #define CMD33 (33) /* ERASE_ER_BLK_END */
  40. #define CMD38 (38) /* ERASE */
  41. #define CMD55 (55) /* APP_CMD */
  42. #define CMD58 (58) /* READ_OCR */
  43. static
  44. DSTATUS Stat = STA_NOINIT; /* Disk status */
  45. static
  46. BYTE CardType; /* b0:MMC, b1:SDv1, b2:SDv2, b3:Block addressing */
  47. extern BYTE FATFS_DEBUG; // debug log, 0 -- disable , 1 -- enable
  48. extern BYTE FATFS_SPI_ID; // 0 -- SPI_1, 1 -- SPI_2
  49. extern BYTE FATFS_SPI_CS; // GPIO 3
  50. static void dly_us(BYTE us) {
  51. if (us < 1) {
  52. return;
  53. }
  54. us += 999;
  55. luat_timer_mdelay(us/1000);
  56. }
  57. /*-----------------------------------------------------------------------*/
  58. /* Transmit bytes to the card (bitbanging) */
  59. /*-----------------------------------------------------------------------*/
  60. static
  61. void xmit_mmc (
  62. const BYTE* buff, /* Data to be sent */
  63. UINT bc /* Number of bytes to send */
  64. )
  65. {
  66. #if 0
  67. BYTE d;
  68. do {
  69. d = *buff++; /* Get a byte to be sent */
  70. if (d & 0x80) DI_H(); else DI_L(); /* bit7 */
  71. CK_H(); CK_L();
  72. if (d & 0x40) DI_H(); else DI_L(); /* bit6 */
  73. CK_H(); CK_L();
  74. if (d & 0x20) DI_H(); else DI_L(); /* bit5 */
  75. CK_H(); CK_L();
  76. if (d & 0x10) DI_H(); else DI_L(); /* bit4 */
  77. CK_H(); CK_L();
  78. if (d & 0x08) DI_H(); else DI_L(); /* bit3 */
  79. CK_H(); CK_L();
  80. if (d & 0x04) DI_H(); else DI_L(); /* bit2 */
  81. CK_H(); CK_L();
  82. if (d & 0x02) DI_H(); else DI_L(); /* bit1 */
  83. CK_H(); CK_L();
  84. if (d & 0x01) DI_H(); else DI_L(); /* bit0 */
  85. CK_H(); CK_L();
  86. } while (--bc);
  87. #endif
  88. if (FATFS_DEBUG)
  89. LLOGD("[FatFS]xmit_mmc bc=%d\r\n", bc);
  90. luat_spi_send(FATFS_SPI_ID, buff, bc);
  91. }
  92. /*-----------------------------------------------------------------------*/
  93. /* Receive bytes from the card (bitbanging) */
  94. /*-----------------------------------------------------------------------*/
  95. static
  96. void rcvr_mmc (
  97. BYTE *buff, /* Pointer to read buffer */
  98. UINT bc /* Number of bytes to receive */
  99. )
  100. {
  101. #if 0
  102. BYTE r;
  103. DI_H(); /* Send 0xFF */
  104. do {
  105. r = 0; if (DO) r++; /* bit7 */
  106. CK_H(); CK_L();
  107. r <<= 1; if (DO) r++; /* bit6 */
  108. CK_H(); CK_L();
  109. r <<= 1; if (DO) r++; /* bit5 */
  110. CK_H(); CK_L();
  111. r <<= 1; if (DO) r++; /* bit4 */
  112. CK_H(); CK_L();
  113. r <<= 1; if (DO) r++; /* bit3 */
  114. CK_H(); CK_L();
  115. r <<= 1; if (DO) r++; /* bit2 */
  116. CK_H(); CK_L();
  117. r <<= 1; if (DO) r++; /* bit1 */
  118. CK_H(); CK_L();
  119. r <<= 1; if (DO) r++; /* bit0 */
  120. CK_H(); CK_L();
  121. *buff++ = r; /* Store a received byte */
  122. } while (--bc);
  123. #endif
  124. //u8* buf2 = 0x00;
  125. //u8** buf = &buf2;
  126. BYTE tmp[bc];
  127. for(size_t i = 0; i < bc; i++)
  128. {
  129. tmp[i] = 0xFF;
  130. }
  131. DWORD t = luat_spi_transfer(FATFS_SPI_ID, tmp, buff, bc);
  132. //s32 t = platform_spi_recv(0, buf, bc);
  133. //memcpy(buff, buf2, bc);
  134. //if (FATFS_DEBUG)
  135. // LLOGD("[FatFS]rcvr_mmc first resp byte=%02X, t=%d\r\n", buf2[0], t);
  136. //free(buf2);
  137. }
  138. /*-----------------------------------------------------------------------*/
  139. /* Wait for card ready */
  140. /*-----------------------------------------------------------------------*/
  141. static
  142. int wait_ready (void) /* 1:OK, 0:Timeout */
  143. {
  144. BYTE d;
  145. UINT tmr;
  146. for (tmr = 5000; tmr; tmr--) { /* Wait for ready in timeout of 500ms */
  147. rcvr_mmc(&d, 1);
  148. if (d == 0xFF) break;
  149. dly_us(100);
  150. }
  151. return tmr ? 1 : 0;
  152. }
  153. /*-----------------------------------------------------------------------*/
  154. /* Deselect the card and release SPI bus */
  155. /*-----------------------------------------------------------------------*/
  156. static
  157. void spi_cs_deselect (void)
  158. {
  159. BYTE d;
  160. //CS_H(); /* Set CS# high */
  161. //platform_pio_op(0, 1 << FATFS_SPI_CS, 0);
  162. luat_gpio_set(FATFS_SPI_CS, 1);
  163. rcvr_mmc(&d, 1); /* Dummy clock (force DO hi-z for multiple slave SPI) */
  164. }
  165. /*-----------------------------------------------------------------------*/
  166. /* Select the card and wait for ready */
  167. /*-----------------------------------------------------------------------*/
  168. static
  169. int spi_cs_select (void) /* 1:OK, 0:Timeout */
  170. {
  171. BYTE d;
  172. //CS_L(); /* Set CS# low */
  173. //platform_pio_op(0, 1 << FATFS_SPI_CS, 1);
  174. luat_gpio_set(FATFS_SPI_CS, 0);
  175. rcvr_mmc(&d, 1); /* Dummy clock (force DO enabled) */
  176. if (wait_ready()) return 1; /* Wait for card ready */
  177. spi_cs_deselect();
  178. return 0; /* Failed */
  179. }
  180. /*-----------------------------------------------------------------------*/
  181. /* Receive a data packet from the card */
  182. /*-----------------------------------------------------------------------*/
  183. static
  184. int rcvr_datablock ( /* 1:OK, 0:Failed */
  185. BYTE *buff, /* Data buffer to store received data */
  186. UINT btr /* Byte count */
  187. )
  188. {
  189. BYTE d[2];
  190. UINT tmr;
  191. for (tmr = 1000; tmr; tmr--) { /* Wait for data packet in timeout of 100ms */
  192. rcvr_mmc(d, 1);
  193. if (d[0] != 0xFF) break;
  194. dly_us(100);
  195. }
  196. if (d[0] != 0xFE) return 0; /* If not valid data token, return with error */
  197. rcvr_mmc(buff, btr); /* Receive the data block into buffer */
  198. rcvr_mmc(d, 2); /* Discard CRC */
  199. return 1; /* Return with success */
  200. }
  201. /*-----------------------------------------------------------------------*/
  202. /* Send a data packet to the card */
  203. /*-----------------------------------------------------------------------*/
  204. static
  205. int xmit_datablock ( /* 1:OK, 0:Failed */
  206. const BYTE *buff, /* 512 byte data block to be transmitted */
  207. BYTE token /* Data/Stop token */
  208. )
  209. {
  210. BYTE d[2];
  211. if (!wait_ready()) return 0;
  212. d[0] = token;
  213. xmit_mmc(d, 1); /* Xmit a token */
  214. if (token != 0xFD) { /* Is it data token? */
  215. xmit_mmc(buff, 512); /* Xmit the 512 byte data block to MMC */
  216. rcvr_mmc(d, 2); /* Xmit dummy CRC (0xFF,0xFF) */
  217. rcvr_mmc(d, 1); /* Receive data response */
  218. if ((d[0] & 0x1F) != 0x05) /* If not accepted, return with error */
  219. return 0;
  220. }
  221. return 1;
  222. }
  223. /*-----------------------------------------------------------------------*/
  224. /* Send a command packet to the card */
  225. /*-----------------------------------------------------------------------*/
  226. static
  227. BYTE send_cmd ( /* Returns command response (bit7==1:Send failed)*/
  228. BYTE cmd, /* Command byte */
  229. DWORD arg /* Argument */
  230. )
  231. {
  232. BYTE n, d, buf[6];
  233. if (cmd & 0x80) { /* ACMD<n> is the command sequense of CMD55-CMD<n> */
  234. cmd &= 0x7F;
  235. n = send_cmd(CMD55, 0);
  236. if (n > 1) return n;
  237. }
  238. /* Select the card and wait for ready except to stop multiple block read */
  239. if (cmd != CMD12) {
  240. spi_cs_deselect();
  241. if (!spi_cs_select()) return 0xFF;
  242. }
  243. /* Send a command packet */
  244. buf[0] = 0x40 | cmd; /* Start + Command index */
  245. buf[1] = (BYTE)(arg >> 24); /* Argument[31..24] */
  246. buf[2] = (BYTE)(arg >> 16); /* Argument[23..16] */
  247. buf[3] = (BYTE)(arg >> 8); /* Argument[15..8] */
  248. buf[4] = (BYTE)arg; /* Argument[7..0] */
  249. n = 0x01; /* Dummy CRC + Stop */
  250. if (cmd == CMD0) n = 0x95; /* (valid CRC for CMD0(0)) */
  251. if (cmd == CMD8) n = 0x87; /* (valid CRC for CMD8(0x1AA)) */
  252. buf[5] = n;
  253. xmit_mmc(buf, 6);
  254. /* Receive command response */
  255. if (cmd == CMD12) rcvr_mmc(&d, 1); /* Skip a stuff byte when stop reading */
  256. n = 10; /* Wait for a valid response in timeout of 10 attempts */
  257. do
  258. rcvr_mmc(&d, 1);
  259. while ((d & 0x80) && --n);
  260. return d; /* Return with the response value */
  261. }
  262. /*--------------------------------------------------------------------------
  263. Public Functions
  264. ---------------------------------------------------------------------------*/
  265. /*-----------------------------------------------------------------------*/
  266. /* Get Disk Status */
  267. /*-----------------------------------------------------------------------*/
  268. DSTATUS spitf_status (
  269. void* userdata
  270. )
  271. {
  272. //if (drv) return STA_NOINIT;
  273. return Stat;
  274. }
  275. /*-----------------------------------------------------------------------*/
  276. /* Initialize Disk Drive */
  277. /*-----------------------------------------------------------------------*/
  278. DSTATUS spitf_initialize (
  279. void* userdata
  280. )
  281. {
  282. BYTE n, ty, cmd, buf[4];
  283. UINT tmr;
  284. DSTATUS s;
  285. //if (drv) return RES_NOTRDY;
  286. //dly_us(10000); /* 10ms */
  287. //CS_INIT(); CS_H(); /* Initialize port pin tied to CS */
  288. //CK_INIT(); CK_L(); /* Initialize port pin tied to SCLK */
  289. //DI_INIT(); /* Initialize port pin tied to DI */
  290. //DO_INIT(); /* Initialize port pin tied to DO */
  291. //luat_spi_close(FATFS_SPI_ID);
  292. //luat_spi_setup(FATFS_SPI_ID, 400*1000/*400khz*/, 0, 0, 8, 1, 1);
  293. for (n = 10; n; n--) rcvr_mmc(buf, 1); /* Apply 80 dummy clocks and the card gets ready to receive command */
  294. ty = 0;
  295. if (send_cmd(CMD0, 0) == 1) { /* Enter Idle state */
  296. if (send_cmd(CMD8, 0x1AA) == 1) { /* SDv2? */
  297. rcvr_mmc(buf, 4); /* Get trailing return value of R7 resp */
  298. if (buf[2] == 0x01 && buf[3] == 0xAA) { /* The card can work at vdd range of 2.7-3.6V */
  299. for (tmr = 1000; tmr; tmr--) { /* Wait for leaving idle state (ACMD41 with HCS bit) */
  300. if (send_cmd(ACMD41, 1UL << 30) == 0) break;
  301. dly_us(1000);
  302. }
  303. if (tmr && send_cmd(CMD58, 0) == 0) { /* Check CCS bit in the OCR */
  304. rcvr_mmc(buf, 4);
  305. ty = (buf[0] & 0x40) ? CT_SD2 | CT_BLOCK : CT_SD2; /* SDv2 */
  306. }
  307. }
  308. } else { /* SDv1 or MMCv3 */
  309. if (send_cmd(ACMD41, 0) <= 1) {
  310. ty = CT_SD1; cmd = ACMD41; /* SDv1 */
  311. } else {
  312. ty = CT_MMC; cmd = CMD1; /* MMCv3 */
  313. }
  314. for (tmr = 1000; tmr; tmr--) { /* Wait for leaving idle state */
  315. if (send_cmd(cmd, 0) == 0) break;
  316. dly_us(1000);
  317. }
  318. if (!tmr || send_cmd(CMD16, 512) != 0) /* Set R/W block length to 512 */
  319. ty = 0;
  320. }
  321. }
  322. CardType = ty;
  323. s = ty ? 0 : STA_NOINIT;
  324. Stat = s;
  325. spi_cs_deselect();
  326. //luat_spi_close(FATFS_SPI_ID);
  327. //spi.setup(id,0,0,8,400*1000,1)
  328. //luat_spi_setup(FATFS_SPI_ID, 1000*1000/*1Mhz*/, 0, 0, 8, 1, 1);
  329. return s;
  330. }
  331. /*-----------------------------------------------------------------------*/
  332. /* Read Sector(s) */
  333. /*-----------------------------------------------------------------------*/
  334. DRESULT spitf_read (
  335. void* userdata,
  336. BYTE *buff, /* Pointer to the data buffer to store read data */
  337. DWORD sector, /* Start sector number (LBA) */
  338. UINT count /* Sector count (1..128) */
  339. )
  340. {
  341. BYTE cmd;
  342. if (spitf_status(userdata) & STA_NOINIT) return RES_NOTRDY;
  343. if (!(CardType & CT_BLOCK)) sector *= 512; /* Convert LBA to byte address if needed */
  344. cmd = count > 1 ? CMD18 : CMD17; /* READ_MULTIPLE_BLOCK : READ_SINGLE_BLOCK */
  345. if (send_cmd(cmd, sector) == 0) {
  346. do {
  347. if (!rcvr_datablock(buff, 512)) break;
  348. buff += 512;
  349. } while (--count);
  350. if (cmd == CMD18) send_cmd(CMD12, 0); /* STOP_TRANSMISSION */
  351. }
  352. spi_cs_deselect();
  353. return count ? RES_ERROR : RES_OK;
  354. }
  355. /*-----------------------------------------------------------------------*/
  356. /* Write Sector(s) */
  357. /*-----------------------------------------------------------------------*/
  358. DRESULT spitf_write (
  359. void* userdata,
  360. const BYTE *buff, /* Pointer to the data to be written */
  361. DWORD sector, /* Start sector number (LBA) */
  362. UINT count /* Sector count (1..128) */
  363. )
  364. {
  365. if (spitf_status(userdata) & STA_NOINIT) return RES_NOTRDY;
  366. if (!(CardType & CT_BLOCK)) sector *= 512; /* Convert LBA to byte address if needed */
  367. if (count == 1) { /* Single block write */
  368. if ((send_cmd(CMD24, sector) == 0) /* WRITE_BLOCK */
  369. && xmit_datablock(buff, 0xFE))
  370. count = 0;
  371. }
  372. else { /* Multiple block write */
  373. if (CardType & CT_SDC) send_cmd(ACMD23, count);
  374. if (send_cmd(CMD25, sector) == 0) { /* WRITE_MULTIPLE_BLOCK */
  375. do {
  376. if (!xmit_datablock(buff, 0xFC)) break;
  377. buff += 512;
  378. } while (--count);
  379. if (!xmit_datablock(0, 0xFD)) /* STOP_TRAN token */
  380. count = 1;
  381. }
  382. }
  383. spi_cs_deselect();
  384. return count ? RES_ERROR : RES_OK;
  385. }
  386. /*-----------------------------------------------------------------------*/
  387. /* Miscellaneous Functions */
  388. /*-----------------------------------------------------------------------*/
  389. DRESULT spitf_ioctl (
  390. void* userdata,
  391. BYTE ctrl, /* Control code */
  392. void *buff /* Buffer to send/receive control data */
  393. )
  394. {
  395. DRESULT res;
  396. BYTE n, csd[16];
  397. DWORD cs;
  398. if (spitf_status(userdata) & STA_NOINIT) return RES_NOTRDY; /* Check if card is in the socket */
  399. res = RES_ERROR;
  400. switch (ctrl) {
  401. case CTRL_SYNC : /* Make sure that no pending write process */
  402. if (spi_cs_select()) res = RES_OK;
  403. break;
  404. case GET_SECTOR_COUNT : /* Get number of sectors on the disk (DWORD) */
  405. if ((send_cmd(CMD9, 0) == 0) && rcvr_datablock(csd, 16)) {
  406. if ((csd[0] >> 6) == 1) { /* SDC ver 2.00 */
  407. cs = csd[9] + ((WORD)csd[8] << 8) + ((DWORD)(csd[7] & 63) << 16) + 1;
  408. *(DWORD*)buff = cs << 10;
  409. } else { /* SDC ver 1.XX or MMC */
  410. n = (csd[5] & 15) + ((csd[10] & 128) >> 7) + ((csd[9] & 3) << 1) + 2;
  411. cs = (csd[8] >> 6) + ((WORD)csd[7] << 2) + ((WORD)(csd[6] & 3) << 10) + 1;
  412. *(DWORD*)buff = cs << (n - 9);
  413. }
  414. res = RES_OK;
  415. }
  416. break;
  417. case GET_BLOCK_SIZE : /* Get erase block size in unit of sector (DWORD) */
  418. *(DWORD*)buff = 128;
  419. res = RES_OK;
  420. break;
  421. default:
  422. res = RES_PARERR;
  423. }
  424. spi_cs_deselect();
  425. return res;
  426. }
  427. // DSTATUS spitf_initialize (void* userdata);
  428. // DSTATUS ramdisk_status (void* userdata);
  429. // DRESULT ramdisk_read (void* userdata, BYTE* buff, LBA_t sector, UINT count);
  430. // DRESULT ramdisk_write (void* userdata, const BYTE* buff, LBA_t sector, UINT count);
  431. // DRESULT ramdisk_ioctl (void* userdata, BYTE cmd, void* buff);
  432. const block_disk_opts_t spitf_disk_opts = {
  433. .initialize = spitf_initialize,
  434. .status = spitf_status,
  435. .read = spitf_read,
  436. .write = spitf_write,
  437. .ioctl = spitf_ioctl,
  438. };
  439. DRESULT diskio_open_spitf(BYTE pdrv, BYTE id, BYTE cs) {
  440. block_disk_t disk = {
  441. .opts = &spitf_disk_opts,
  442. .userdata = "spitf",
  443. };
  444. FATFS_SPI_ID = id;
  445. FATFS_SPI_CS = cs;
  446. return diskio_open(pdrv, &disk);
  447. }
  448. //static DWORD get_fattime() {
  449. // how to get?
  450. //}
  451. //--------------------------------------------------------------------------------------