My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

onboard_sd.cpp 20KB

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