My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

onboard_sd.cpp 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. /**
  2. * STM32F1: MMCv3/SDv1/SDv2 (SPI mode) control module
  3. *
  4. * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  5. * Copyright (c) 2019 BigTreeTech [https://github.com/bigtreetech]
  6. * Copyright (C) 2015, ChaN, all right reserved.
  7. *
  8. * This software is a free software and there is NO WARRANTY.
  9. * No restriction on use. You can use, modify and redistribute it for
  10. * personal, non-profit or commercial products UNDER YOUR RESPONSIBILITY.
  11. * Redistributions of source code must retain the above copyright notice.
  12. *
  13. */
  14. #include "../../inc/MarlinConfig.h"
  15. #ifdef HAS_ONBOARD_SD
  16. #include "onboard_sd.h"
  17. #include "SPI.h"
  18. #include "fastio.h"
  19. #ifdef SHARED_SD_CARD
  20. #ifndef ON_BOARD_SPI_DEVICE
  21. #define ON_BOARD_SPI_DEVICE SPI_DEVICE
  22. #endif
  23. #define ONBOARD_SD_SPI SPI
  24. #else
  25. SPIClass OnBoardSPI(ON_BOARD_SPI_DEVICE);
  26. #define ONBOARD_SD_SPI OnBoardSPI
  27. #endif
  28. #if ON_BOARD_SPI_DEVICE == 1
  29. #define SPI_CLOCK_MAX SPI_BAUD_PCLK_DIV_4
  30. #else
  31. #define SPI_CLOCK_MAX SPI_BAUD_PCLK_DIV_2
  32. #endif
  33. #define CS_LOW() WRITE(ONBOARD_SD_CS_PIN, LOW) /* Set OnBoardSPI cs low */
  34. #define CS_HIGH() WRITE(ONBOARD_SD_CS_PIN, HIGH) /* Set OnBoardSPI cs high */
  35. #define FCLK_FAST() ONBOARD_SD_SPI.setClockDivider(SPI_CLOCK_MAX)
  36. #define FCLK_SLOW() ONBOARD_SD_SPI.setClockDivider(SPI_BAUD_PCLK_DIV_256)
  37. /*--------------------------------------------------------------------------
  38. Module Private Functions
  39. ---------------------------------------------------------------------------*/
  40. /* MMC/SD command */
  41. #define CMD0 (0) /* GO_IDLE_STATE */
  42. #define CMD1 (1) /* SEND_OP_COND (MMC) */
  43. #define ACMD41 (0x80+41) /* SEND_OP_COND (SDC) */
  44. #define CMD8 (8) /* SEND_IF_COND */
  45. #define CMD9 (9) /* SEND_CSD */
  46. #define CMD10 (10) /* SEND_CID */
  47. #define CMD12 (12) /* STOP_TRANSMISSION */
  48. #define ACMD13 (0x80+13) /* SD_STATUS (SDC) */
  49. #define CMD16 (16) /* SET_BLOCKLEN */
  50. #define CMD17 (17) /* READ_SINGLE_BLOCK */
  51. #define CMD18 (18) /* READ_MULTIPLE_BLOCK */
  52. #define CMD23 (23) /* SET_BLOCK_COUNT (MMC) */
  53. #define ACMD23 (0x80+23) /* SET_WR_BLK_ERASE_COUNT (SDC) */
  54. #define CMD24 (24) /* WRITE_BLOCK */
  55. #define CMD25 (25) /* WRITE_MULTIPLE_BLOCK */
  56. #define CMD32 (32) /* ERASE_ER_BLK_START */
  57. #define CMD33 (33) /* ERASE_ER_BLK_END */
  58. #define CMD38 (38) /* ERASE */
  59. #define CMD48 (48) /* READ_EXTR_SINGLE */
  60. #define CMD49 (49) /* WRITE_EXTR_SINGLE */
  61. #define CMD55 (55) /* APP_CMD */
  62. #define CMD58 (58) /* READ_OCR */
  63. static volatile DSTATUS Stat = STA_NOINIT; /* Physical drive status */
  64. static volatile UINT timeout;
  65. static BYTE CardType; /* Card type flags */
  66. /*-----------------------------------------------------------------------*/
  67. /* Send/Receive data to the MMC (Platform dependent) */
  68. /*-----------------------------------------------------------------------*/
  69. /* Exchange a byte */
  70. static BYTE xchg_spi (
  71. BYTE dat /* Data to send */
  72. ) {
  73. BYTE returnByte = ONBOARD_SD_SPI.transfer(dat);
  74. return returnByte;
  75. }
  76. /* Receive multiple byte */
  77. static void rcvr_spi_multi (
  78. BYTE *buff, /* Pointer to data buffer */
  79. UINT btr /* Number of bytes to receive (16, 64 or 512) */
  80. ) {
  81. ONBOARD_SD_SPI.dmaTransfer(0, const_cast<uint8_t*>(buff), btr);
  82. }
  83. #if _DISKIO_WRITE
  84. /* Send multiple bytes */
  85. static void xmit_spi_multi (
  86. const BYTE *buff, /* Pointer to the data */
  87. UINT btx /* Number of bytes to send (multiple of 16) */
  88. ) {
  89. ONBOARD_SD_SPI.dmaSend(const_cast<uint8_t*>(buff), btx);
  90. }
  91. #endif // _DISKIO_WRITE
  92. /*-----------------------------------------------------------------------*/
  93. /* Wait for card ready */
  94. /*-----------------------------------------------------------------------*/
  95. static int wait_ready ( /* 1:Ready, 0:Timeout */
  96. UINT wt /* Timeout [ms] */
  97. ) {
  98. BYTE d;
  99. timeout = millis() + wt;
  100. do {
  101. d = xchg_spi(0xFF);
  102. /* This loop takes a while. Insert rot_rdq() here for multitask environment. */
  103. } while (d != 0xFF && (timeout > millis())); /* Wait for card goes ready or timeout */
  104. return (d == 0xFF) ? 1 : 0;
  105. }
  106. /*-----------------------------------------------------------------------*/
  107. /* Deselect card and release SPI */
  108. /*-----------------------------------------------------------------------*/
  109. static void deselect() {
  110. CS_HIGH(); /* CS = H */
  111. xchg_spi(0xFF); /* Dummy clock (force DO hi-z for multiple slave SPI) */
  112. }
  113. /*-----------------------------------------------------------------------*/
  114. /* Select card and wait for ready */
  115. /*-----------------------------------------------------------------------*/
  116. static int select() { /* 1:OK, 0:Timeout */
  117. CS_LOW(); /* CS = L */
  118. xchg_spi(0xFF); /* Dummy clock (force DO enabled) */
  119. if (wait_ready(500)) return 1; /* Leading busy check: Wait for card ready */
  120. deselect(); /* Timeout */
  121. return 0;
  122. }
  123. /*-----------------------------------------------------------------------*/
  124. /* Control SPI module (Platform dependent) */
  125. /*-----------------------------------------------------------------------*/
  126. static void power_on() { /* Enable SSP module and attach it to I/O pads */
  127. ONBOARD_SD_SPI.setModule(ON_BOARD_SPI_DEVICE);
  128. ONBOARD_SD_SPI.begin();
  129. ONBOARD_SD_SPI.setBitOrder(MSBFIRST);
  130. ONBOARD_SD_SPI.setDataMode(SPI_MODE0);
  131. OUT_WRITE(ONBOARD_SD_CS_PIN, HIGH); /* Set CS# high */
  132. }
  133. static void power_off() { /* Disable SPI function */
  134. select(); /* Wait for card ready */
  135. deselect();
  136. }
  137. /*-----------------------------------------------------------------------*/
  138. /* Receive a data packet from the MMC */
  139. /*-----------------------------------------------------------------------*/
  140. static int rcvr_datablock ( /* 1:OK, 0:Error */
  141. BYTE *buff, /* Data buffer */
  142. UINT btr /* Data block length (byte) */
  143. ) {
  144. BYTE token;
  145. timeout = millis() + 200;
  146. do { /* Wait for DataStart token in timeout of 200ms */
  147. token = xchg_spi(0xFF);
  148. /* This loop will take a while. Insert rot_rdq() here for multitask environment. */
  149. } while ((token == 0xFF) && (timeout > millis()));
  150. if (token != 0xFE) return 0; /* Function fails if invalid DataStart token or timeout */
  151. rcvr_spi_multi(buff, btr); /* Store trailing data to the buffer */
  152. xchg_spi(0xFF); xchg_spi(0xFF); /* Discard CRC */
  153. return 1; /* Function succeeded */
  154. }
  155. /*-----------------------------------------------------------------------*/
  156. /* Send a data packet to the MMC */
  157. /*-----------------------------------------------------------------------*/
  158. #if _DISKIO_WRITE
  159. static int xmit_datablock ( /* 1:OK, 0:Failed */
  160. const BYTE *buff, /* Ponter to 512 byte data to be sent */
  161. BYTE token /* Token */
  162. ) {
  163. BYTE resp;
  164. if (!wait_ready(500)) return 0; /* Leading busy check: Wait for card ready to accept data block */
  165. xchg_spi(token); /* Send token */
  166. if (token == 0xFD) return 1; /* Do not send data if token is StopTran */
  167. xmit_spi_multi(buff, 512); /* Data */
  168. xchg_spi(0xFF); xchg_spi(0xFF); /* Dummy CRC */
  169. resp = xchg_spi(0xFF); /* Receive data resp */
  170. return (resp & 0x1F) == 0x05 ? 1 : 0; /* Data was accepted or not */
  171. /* Busy check is done at next transmission */
  172. }
  173. #endif // _DISKIO_WRITE
  174. /*-----------------------------------------------------------------------*/
  175. /* Send a command packet to the MMC */
  176. /*-----------------------------------------------------------------------*/
  177. static BYTE send_cmd ( /* Return value: R1 resp (bit7==1:Failed to send) */
  178. BYTE cmd, /* Command index */
  179. DWORD arg /* Argument */
  180. ) {
  181. BYTE n, res;
  182. if (cmd & 0x80) { /* Send a CMD55 prior to ACMD<n> */
  183. cmd &= 0x7F;
  184. res = send_cmd(CMD55, 0);
  185. if (res > 1) return res;
  186. }
  187. /* Select the card and wait for ready except to stop multiple block read */
  188. if (cmd != CMD12) {
  189. deselect();
  190. if (!select()) return 0xFF;
  191. }
  192. /* Send command packet */
  193. xchg_spi(0x40 | cmd); /* Start + command index */
  194. xchg_spi((BYTE)(arg >> 24)); /* Argument[31..24] */
  195. xchg_spi((BYTE)(arg >> 16)); /* Argument[23..16] */
  196. xchg_spi((BYTE)(arg >> 8)); /* Argument[15..8] */
  197. xchg_spi((BYTE)arg); /* Argument[7..0] */
  198. n = 0x01; /* Dummy CRC + Stop */
  199. if (cmd == CMD0) n = 0x95; /* Valid CRC for CMD0(0) */
  200. if (cmd == CMD8) n = 0x87; /* Valid CRC for CMD8(0x1AA) */
  201. xchg_spi(n);
  202. /* Receive command resp */
  203. if (cmd == CMD12) xchg_spi(0xFF); /* Diacard following one byte when CMD12 */
  204. n = 10; /* Wait for response (10 bytes max) */
  205. do
  206. res = xchg_spi(0xFF);
  207. while ((res & 0x80) && --n);
  208. return res; /* Return received response */
  209. }
  210. /*--------------------------------------------------------------------------
  211. Public Functions
  212. ---------------------------------------------------------------------------*/
  213. /*-----------------------------------------------------------------------*/
  214. /* Initialize disk drive */
  215. /*-----------------------------------------------------------------------*/
  216. DSTATUS disk_initialize (
  217. BYTE drv /* Physical drive number (0) */
  218. ) {
  219. BYTE n, cmd, ty, ocr[4];
  220. if (drv) return STA_NOINIT; /* Supports only drive 0 */
  221. power_on(); /* Initialize SPI */
  222. if (Stat & STA_NODISK) return Stat; /* Is a card existing in the soket? */
  223. FCLK_SLOW();
  224. for (n = 10; n; n--) xchg_spi(0xFF); /* Send 80 dummy clocks */
  225. ty = 0;
  226. if (send_cmd(CMD0, 0) == 1) { /* Put the card SPI state */
  227. timeout = millis() + 1000; /* Initialization timeout = 1 sec */
  228. if (send_cmd(CMD8, 0x1AA) == 1) { /* Is the catd SDv2? */
  229. for (n = 0; n < 4; n++) ocr[n] = xchg_spi(0xFF); /* Get 32 bit return value of R7 resp */
  230. if (ocr[2] == 0x01 && ocr[3] == 0xAA) { /* Does the card support 2.7-3.6V? */
  231. while ((timeout > millis()) && send_cmd(ACMD41, 1UL << 30)) ; /* Wait for end of initialization with ACMD41(HCS) */
  232. if ((timeout > millis()) && send_cmd(CMD58, 0) == 0) { /* Check CCS bit in the OCR */
  233. for (n = 0; n < 4; n++) ocr[n] = xchg_spi(0xFF);
  234. ty = (ocr[0] & 0x40) ? CT_SD2 | CT_BLOCK : CT_SD2; /* Check if the card is SDv2 */
  235. }
  236. }
  237. } else { /* Not an SDv2 card */
  238. if (send_cmd(ACMD41, 0) <= 1) { /* SDv1 or MMCv3? */
  239. ty = CT_SD1; cmd = ACMD41; /* SDv1 (ACMD41(0)) */
  240. } else {
  241. ty = CT_MMC; cmd = CMD1; /* MMCv3 (CMD1(0)) */
  242. }
  243. while ((timeout > millis()) && send_cmd(cmd, 0)) ; /* Wait for the card leaves idle state */
  244. if (!(timeout > millis()) || send_cmd(CMD16, 512) != 0) /* Set block length: 512 */
  245. ty = 0;
  246. }
  247. }
  248. CardType = ty; /* Card type */
  249. deselect();
  250. if (ty) { /* OK */
  251. FCLK_FAST(); /* Set fast clock */
  252. Stat &= ~STA_NOINIT; /* Clear STA_NOINIT flag */
  253. } else { /* Failed */
  254. power_off();
  255. Stat = STA_NOINIT;
  256. }
  257. return Stat;
  258. }
  259. /*-----------------------------------------------------------------------*/
  260. /* Get disk status */
  261. /*-----------------------------------------------------------------------*/
  262. DSTATUS disk_status (
  263. BYTE drv /* Physical drive number (0) */
  264. ) {
  265. if (drv) return STA_NOINIT; /* Supports only drive 0 */
  266. return Stat; /* Return disk status */
  267. }
  268. /*-----------------------------------------------------------------------*/
  269. /* Read sector(s) */
  270. /*-----------------------------------------------------------------------*/
  271. DRESULT disk_read (
  272. BYTE drv, /* Physical drive number (0) */
  273. BYTE *buff, /* Pointer to the data buffer to store read data */
  274. DWORD sector, /* Start sector number (LBA) */
  275. UINT count /* Number of sectors to read (1..128) */
  276. ) {
  277. BYTE cmd;
  278. if (drv || !count) return RES_PARERR; /* Check parameter */
  279. if (Stat & STA_NOINIT) return RES_NOTRDY; /* Check if drive is ready */
  280. if (!(CardType & CT_BLOCK)) sector *= 512; /* LBA ot BA conversion (byte addressing cards) */
  281. FCLK_FAST();
  282. cmd = count > 1 ? CMD18 : CMD17; /* READ_MULTIPLE_BLOCK : READ_SINGLE_BLOCK */
  283. if (send_cmd(cmd, sector) == 0) {
  284. do {
  285. if (!rcvr_datablock(buff, 512)) break;
  286. buff += 512;
  287. } while (--count);
  288. if (cmd == CMD18) send_cmd(CMD12, 0); /* STOP_TRANSMISSION */
  289. }
  290. deselect();
  291. return count ? RES_ERROR : RES_OK; /* Return result */
  292. }
  293. /*-----------------------------------------------------------------------*/
  294. /* Write sector(s) */
  295. /*-----------------------------------------------------------------------*/
  296. #if _DISKIO_WRITE
  297. DRESULT disk_write(
  298. BYTE drv, /* Physical drive number (0) */
  299. const BYTE *buff, /* Ponter to the data to write */
  300. DWORD sector, /* Start sector number (LBA) */
  301. UINT count /* Number of sectors to write (1..128) */
  302. ) {
  303. if (drv || !count) return RES_PARERR; /* Check parameter */
  304. if (Stat & STA_NOINIT) return RES_NOTRDY; /* Check drive status */
  305. if (Stat & STA_PROTECT) return RES_WRPRT; /* Check write protect */
  306. FCLK_FAST();
  307. if (!(CardType & CT_BLOCK)) sector *= 512; /* LBA ==> BA conversion (byte addressing cards) */
  308. if (count == 1) { /* Single sector write */
  309. if ((send_cmd(CMD24, sector) == 0) /* WRITE_BLOCK */
  310. && xmit_datablock(buff, 0xFE)) {
  311. count = 0;
  312. }
  313. }
  314. else { /* Multiple sector write */
  315. if (CardType & CT_SDC) send_cmd(ACMD23, count); /* Predefine number of sectors */
  316. if (send_cmd(CMD25, sector) == 0) { /* WRITE_MULTIPLE_BLOCK */
  317. do {
  318. if (!xmit_datablock(buff, 0xFC)) break;
  319. buff += 512;
  320. } while (--count);
  321. if (!xmit_datablock(0, 0xFD)) count = 1; /* STOP_TRAN token */
  322. }
  323. }
  324. deselect();
  325. return count ? RES_ERROR : RES_OK; /* Return result */
  326. }
  327. #endif // _DISKIO_WRITE
  328. /*-----------------------------------------------------------------------*/
  329. /* Miscellaneous drive controls other than data read/write */
  330. /*-----------------------------------------------------------------------*/
  331. #if _DISKIO_IOCTL
  332. DRESULT disk_ioctl (
  333. BYTE drv, /* Physical drive number (0) */
  334. BYTE cmd, /* Control command code */
  335. void *buff /* Pointer to the conrtol data */
  336. ) {
  337. DRESULT res;
  338. BYTE n, csd[16], *ptr = (BYTE *)buff;
  339. DWORD *dp, st, ed, csize;
  340. #if _DISKIO_ISDIO
  341. SDIO_CMD *sdio = buff;
  342. BYTE rc, *buf;
  343. UINT dc;
  344. #endif
  345. if (drv) return RES_PARERR; /* Check parameter */
  346. if (Stat & STA_NOINIT) return RES_NOTRDY; /* Check if drive is ready */
  347. res = RES_ERROR;
  348. FCLK_FAST();
  349. switch (cmd) {
  350. case CTRL_SYNC: /* Wait for end of internal write process of the drive */
  351. if (select()) res = RES_OK;
  352. break;
  353. case GET_SECTOR_COUNT: /* Get drive capacity in unit of sector (DWORD) */
  354. if ((send_cmd(CMD9, 0) == 0) && rcvr_datablock(csd, 16)) {
  355. if ((csd[0] >> 6) == 1) { /* SDC ver 2.00 */
  356. csize = csd[9] + ((WORD)csd[8] << 8) + ((DWORD)(csd[7] & 63) << 16) + 1;
  357. *(DWORD*)buff = csize << 10;
  358. } else { /* SDC ver 1.XX or MMC ver 3 */
  359. n = (csd[5] & 15) + ((csd[10] & 128) >> 7) + ((csd[9] & 3) << 1) + 2;
  360. csize = (csd[8] >> 6) + ((WORD)csd[7] << 2) + ((WORD)(csd[6] & 3) << 10) + 1;
  361. *(DWORD*)buff = csize << (n - 9);
  362. }
  363. res = RES_OK;
  364. }
  365. break;
  366. case GET_BLOCK_SIZE: /* Get erase block size in unit of sector (DWORD) */
  367. if (CardType & CT_SD2) { /* SDC ver 2.00 */
  368. if (send_cmd(ACMD13, 0) == 0) { /* Read SD status */
  369. xchg_spi(0xFF);
  370. if (rcvr_datablock(csd, 16)) { /* Read partial block */
  371. for (n = 64 - 16; n; n--) xchg_spi(0xFF); /* Purge trailing data */
  372. *(DWORD*)buff = 16UL << (csd[10] >> 4);
  373. res = RES_OK;
  374. }
  375. }
  376. } else { /* SDC ver 1.XX or MMC */
  377. if ((send_cmd(CMD9, 0) == 0) && rcvr_datablock(csd, 16)) { /* Read CSD */
  378. if (CardType & CT_SD1) { /* SDC ver 1.XX */
  379. *(DWORD*)buff = (((csd[10] & 63) << 1) + ((WORD)(csd[11] & 128) >> 7) + 1) << ((csd[13] >> 6) - 1);
  380. } else { /* MMC */
  381. *(DWORD*)buff = ((WORD)((csd[10] & 124) >> 2) + 1) * (((csd[11] & 3) << 3) + ((csd[11] & 224) >> 5) + 1);
  382. }
  383. res = RES_OK;
  384. }
  385. }
  386. break;
  387. case CTRL_TRIM: /* Erase a block of sectors (used when _USE_TRIM in ffconf.h is 1) */
  388. if (!(CardType & CT_SDC)) break; /* Check if the card is SDC */
  389. if (disk_ioctl(drv, MMC_GET_CSD, csd)) break; /* Get CSD */
  390. if (!(csd[0] >> 6) && !(csd[10] & 0x40)) break; /* Check if sector erase can be applied to the card */
  391. dp = (DWORD *)buff; st = dp[0]; ed = dp[1]; /* Load sector block */
  392. if (!(CardType & CT_BLOCK)) {
  393. st *= 512; ed *= 512;
  394. }
  395. if (send_cmd(CMD32, st) == 0 && send_cmd(CMD33, ed) == 0 && send_cmd(CMD38, 0) == 0 && wait_ready(30000)) { /* Erase sector block */
  396. res = RES_OK; /* FatFs does not check result of this command */
  397. }
  398. break;
  399. /* Following commands are never used by FatFs module */
  400. case MMC_GET_TYPE: /* Get MMC/SDC type (BYTE) */
  401. *ptr = CardType;
  402. res = RES_OK;
  403. break;
  404. case MMC_GET_CSD: /* Read CSD (16 bytes) */
  405. if (send_cmd(CMD9, 0) == 0 && rcvr_datablock(ptr, 16)) { /* READ_CSD */
  406. res = RES_OK;
  407. }
  408. break;
  409. case MMC_GET_CID: /* Read CID (16 bytes) */
  410. if (send_cmd(CMD10, 0) == 0 && rcvr_datablock(ptr, 16)) { /* READ_CID */
  411. res = RES_OK;
  412. }
  413. break;
  414. case MMC_GET_OCR: /* Read OCR (4 bytes) */
  415. if (send_cmd(CMD58, 0) == 0) { /* READ_OCR */
  416. for (n = 4; n; n--) *ptr++ = xchg_spi(0xFF);
  417. res = RES_OK;
  418. }
  419. break;
  420. case MMC_GET_SDSTAT: /* Read SD status (64 bytes) */
  421. if (send_cmd(ACMD13, 0) == 0) { /* SD_STATUS */
  422. xchg_spi(0xFF);
  423. if (rcvr_datablock(ptr, 64)) res = RES_OK;
  424. }
  425. break;
  426. #if _DISKIO_ISDIO
  427. case ISDIO_READ:
  428. sdio = buff;
  429. if (send_cmd(CMD48, 0x80000000 | sdio->func << 28 | sdio->addr << 9 | ((sdio->ndata - 1) & 0x1FF)) == 0) {
  430. for (Timer1 = 1000; (rc = xchg_spi(0xFF)) == 0xFF && Timer1; ) ;
  431. if (rc == 0xFE) {
  432. for (buf = sdio->data, dc = sdio->ndata; dc; dc--) *buf++ = xchg_spi(0xFF);
  433. for (dc = 514 - sdio->ndata; dc; dc--) xchg_spi(0xFF);
  434. res = RES_OK;
  435. }
  436. }
  437. break;
  438. case ISDIO_WRITE:
  439. sdio = buff;
  440. if (send_cmd(CMD49, 0x80000000 | sdio->func << 28 | sdio->addr << 9 | ((sdio->ndata - 1) & 0x1FF)) == 0) {
  441. xchg_spi(0xFF); xchg_spi(0xFE);
  442. for (buf = sdio->data, dc = sdio->ndata; dc; dc--) xchg_spi(*buf++);
  443. for (dc = 514 - sdio->ndata; dc; dc--) xchg_spi(0xFF);
  444. if ((xchg_spi(0xFF) & 0x1F) == 0x05) res = RES_OK;
  445. }
  446. break;
  447. case ISDIO_MRITE:
  448. sdio = buff;
  449. if (send_cmd(CMD49, 0x84000000 | sdio->func << 28 | sdio->addr << 9 | sdio->ndata >> 8) == 0) {
  450. xchg_spi(0xFF); xchg_spi(0xFE);
  451. xchg_spi(sdio->ndata);
  452. for (dc = 513; dc; dc--) xchg_spi(0xFF);
  453. if ((xchg_spi(0xFF) & 0x1F) == 0x05) res = RES_OK;
  454. }
  455. break;
  456. #endif // _DISKIO_ISDIO
  457. default: res = RES_PARERR;
  458. }
  459. deselect();
  460. return res;
  461. }
  462. #endif // _DISKIO_IOCTL
  463. #endif // HAS_ONBOARD_SD