diskio_spitf.c 16 KB

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