sdcard.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749
  1. /* sd2iec - SD/MMC to Commodore serial bus interface/controller
  2. Copyright (C) 2007-2009 Ingo Korb <ingo@akana.de>
  3. Inspiration and low-level SD/MMC access based on code from MMC2IEC
  4. by Lars Pontoppidan et al., see sdcard.c|h and config.h.
  5. FAT filesystem access based on code from ChaN and Jim Brain, see ff.c|h.
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; version 2 of the License only.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. sdcard.c: SD/MMC access routines
  17. Extended, optimized and cleaned version of code from MMC2IEC,
  18. original copyright header follows:
  19. //
  20. // Title : SD/MMC Card driver
  21. // Author : Lars Pontoppidan, Aske Olsson, Pascal Dufour,
  22. // Date : Jan. 2006
  23. // Version : 0.42
  24. // Target MCU : Atmel AVR Series
  25. //
  26. // CREDITS:
  27. // This module is developed as part of a project at the technical univerisity of
  28. // Denmark, DTU.
  29. //
  30. // DESCRIPTION:
  31. // This SD card driver implements the fundamental communication with a SD card.
  32. // The driver is confirmed working on 8 MHz and 14.7456 MHz AtMega32 and has
  33. // been tested successfully with a large number of different SD and MMC cards.
  34. //
  35. // DISCLAIMER:
  36. // The author is in no way responsible for any problems or damage caused by
  37. // using this code. Use at your own risk.
  38. //
  39. // LICENSE:
  40. // This code is distributed under the GNU Public License
  41. // which can be found at http://www.gnu.org/licenses/gpl.txt
  42. //
  43. The exported functions in this file are weak-aliased to their corresponding
  44. versions defined in diskio.h so when this file is the only diskio provider
  45. compiled in they will be automatically used by the linker.
  46. */
  47. #include <avr/io.h>
  48. #include <avr/interrupt.h>
  49. #include <avr/pgmspace.h>
  50. #include <util/crc16.h>
  51. #include "config.h"
  52. #include "avrcompat.h"
  53. #include "crc7.h"
  54. #include "diskio.h"
  55. #include "spi.h"
  56. #include "uart.h"
  57. #include "sdcard.h"
  58. #ifndef TRUE
  59. #define TRUE -1
  60. #endif
  61. #ifndef FALSE
  62. #define FALSE 0
  63. #endif
  64. #ifdef CONFIG_TWINSD
  65. # define MAX_CARDS 2
  66. #else
  67. # define MAX_CARDS 1
  68. #endif
  69. // SD/MMC commands
  70. #define GO_IDLE_STATE 0
  71. #define SEND_OP_COND 1
  72. #define SWITCH_FUNC 6
  73. #define SEND_IF_COND 8
  74. #define SEND_CSD 9
  75. #define SEND_CID 10
  76. #define STOP_TRANSMISSION 12
  77. #define SEND_STATUS 13
  78. #define SET_BLOCKLEN 16
  79. #define READ_SINGLE_BLOCK 17
  80. #define READ_MULTIPLE_BLOCK 18
  81. #define WRITE_BLOCK 24
  82. #define WRITE_MULTIPLE_BLOCK 25
  83. #define PROGRAM_CSD 27
  84. #define SET_WRITE_PROT 28
  85. #define CLR_WRITE_PROT 29
  86. #define SEND_WRITE_PROT 30
  87. #define ERASE_WR_BLK_STAR_ADDR 32
  88. #define ERASE_WR_BLK_END_ADDR 33
  89. #define ERASE 38
  90. #define LOCK_UNLOCK 42
  91. #define APP_CMD 55
  92. #define GEN_CMD 56
  93. #define READ_OCR 58
  94. #define CRC_ON_OFF 59
  95. // SD ACMDs
  96. #define SD_STATUS 13
  97. #define SD_SEND_NUM_WR_BLOCKS 22
  98. #define SD_SET_WR_BLK_ERASE_COUNT 23
  99. #define SD_SEND_OP_COND 41
  100. #define SD_SET_CLR_CARD_DETECT 42
  101. #define SD_SEND_SCR 51
  102. // R1 status bits
  103. #define STATUS_IN_IDLE 1
  104. #define STATUS_ERASE_RESET 2
  105. #define STATUS_ILLEGAL_COMMAND 4
  106. #define STATUS_CRC_ERROR 8
  107. #define STATUS_ERASE_SEQ_ERROR 16
  108. #define STATUS_ADDRESS_ERROR 32
  109. #define STATUS_PARAMETER_ERROR 64
  110. /* Card types - cardtype == 0 is MMC */
  111. #define CARD_SD (1<<0)
  112. #define CARD_SDHC (1<<1)
  113. static uint8_t cardtype[MAX_CARDS];
  114. /**
  115. * getbits - read value from bit buffer
  116. * @buffer: pointer to the data buffer
  117. * @start : index of the first bit in the value
  118. * @bits : number of bits in the value
  119. *
  120. * This function returns a value from the memory region passed as
  121. * buffer, starting with bit "start" and "bits" bit long. The buffer
  122. * is assumed to be MSB first, passing 0 for start will read starting
  123. * from the highest-value bit of the first byte of the buffer.
  124. */
  125. static uint32_t getbits(void *buffer, uint16_t start, int8_t bits) {
  126. uint8_t *buf = buffer;
  127. uint32_t result = 0;
  128. if ((start % 8) != 0) {
  129. /* Unaligned start */
  130. result += buf[start / 8] & (0xff >> (start % 8));
  131. bits -= 8 - (start % 8);
  132. start += 8 - (start % 8);
  133. }
  134. while (bits >= 8) {
  135. result = (result << 8) + buf[start / 8];
  136. start += 8;
  137. bits -= 8;
  138. }
  139. if (bits > 0) {
  140. result = result << bits;
  141. result = result + (buf[start / 8] >> (8-bits));
  142. } else if (bits < 0) {
  143. /* Fraction of a single byte */
  144. result = result >> -bits;
  145. }
  146. return result;
  147. }
  148. static uint8_t sdResponse(uint8_t expected)
  149. {
  150. unsigned short count = 0x0FFF;
  151. while ((spiTransferByte(0xFF) != expected) && count )
  152. count--;
  153. // If count didn't run out, return success
  154. return (count != 0);
  155. }
  156. static uint8_t sdWaitWriteFinish(void)
  157. {
  158. unsigned short count = 0xFFFF; // wait for quite some time
  159. while ((spiTransferByte(0xFF) == 0) && count )
  160. count--;
  161. // If count didn't run out, return success
  162. return (count != 0);
  163. }
  164. static void deselectCard(uint8_t card) {
  165. // Send 8 clock cycles
  166. SPI_SS_HIGH(card);
  167. spiTransferByte(0xff);
  168. }
  169. /**
  170. * sendCommand - send a command to the SD card
  171. * @card : card number to be accessed
  172. * @command : command to be sent
  173. * @parameter: parameter to be sent
  174. * @deselect : Flags if the card should be deselected afterwards
  175. *
  176. * This function calculates the correct CRC7 for the command and
  177. * parameter and transmits all of it to the SD card. If requested
  178. * the card will be deselected afterwards.
  179. */
  180. static int sendCommand(const uint8_t card,
  181. const uint8_t command,
  182. const uint32_t parameter,
  183. const uint8_t deselect) {
  184. union {
  185. uint32_t l;
  186. uint8_t c[4];
  187. } long2char;
  188. uint8_t i,crc=0,errorcount;
  189. uint16_t counter;
  190. long2char.l = parameter;
  191. crc = crc7update(0 , 0x40+command);
  192. crc = crc7update(crc, long2char.c[3]);
  193. crc = crc7update(crc, long2char.c[2]);
  194. crc = crc7update(crc, long2char.c[1]);
  195. crc = crc7update(crc, long2char.c[0]);
  196. crc = (crc << 1) | 1;
  197. errorcount = 0;
  198. while (errorcount < CONFIG_SD_AUTO_RETRIES) {
  199. // Select card
  200. SPI_SS_LOW(card);
  201. #ifdef CONFIG_TWINSD
  202. if (card == 0 && command == GO_IDLE_STATE)
  203. /* Force both cards to SPI mode simultaneously */
  204. SPI_SS_LOW(1);
  205. #endif
  206. // Transfer command
  207. spiTransferByte(0x40+command);
  208. spiTransferLong(parameter);
  209. spiTransferByte(crc);
  210. // Wait for a valid response
  211. counter = 0;
  212. do {
  213. i = spiTransferByte(0xff);
  214. counter++;
  215. } while (i & 0x80 && counter < 0x1000);
  216. #ifdef CONFIG_TWINSD
  217. if (card == 0 && command == GO_IDLE_STATE)
  218. SPI_SS_HIGH(1);
  219. #endif
  220. // Check for CRC error
  221. // can't reliably retry unless deselect is allowed
  222. if (deselect && (i & STATUS_CRC_ERROR)) {
  223. uart_putc('x');
  224. deselectCard(card);
  225. errorcount++;
  226. continue;
  227. }
  228. if (deselect) deselectCard(card);
  229. break;
  230. }
  231. return i;
  232. }
  233. // Extended init sequence for SDHC support
  234. static uint8_t extendedInit(const uint8_t card) {
  235. uint8_t i;
  236. uint32_t answer;
  237. // Send CMD8: SEND_IF_COND
  238. // 0b000110101010 == 2.7-3.6V supply, check pattern 0xAA
  239. i = sendCommand(card, SEND_IF_COND, 0b000110101010, 0);
  240. if (i > 1) {
  241. // Card returned an error, ok (MMC oder SD1.x) but not SDHC
  242. deselectCard(card);
  243. return TRUE;
  244. }
  245. // No error, continue SDHC initialization
  246. answer = spiTransferLong(0);
  247. deselectCard(card);
  248. if (((answer >> 8) & 0x0f) != 0b0001) {
  249. // Card didn't accept our voltage specification
  250. return FALSE;
  251. }
  252. // Verify echo-back of check pattern
  253. if ((answer & 0xff) != 0b10101010) {
  254. // Check pattern mismatch, working but not SD2.0 compliant
  255. // The specs say we should not use the card, but let's try anyway.
  256. return TRUE;
  257. }
  258. return TRUE;
  259. }
  260. // SD common initialisation
  261. static void sdInit(const uint8_t card) {
  262. uint8_t i;
  263. uint16_t counter;
  264. counter = 0xffff;
  265. do {
  266. // Prepare for ACMD, send CMD55: APP_CMD
  267. i = sendCommand(card, APP_CMD, 0, 1);
  268. if (i > 1) {
  269. // Command not accepted, could be MMC
  270. return;
  271. }
  272. // Send ACMD41: SD_SEND_OP_COND
  273. // 1L<<30 == Host has High Capacity Support
  274. i = sendCommand(card, SD_SEND_OP_COND, 1L<<30, 1);
  275. // Repeat while card card accepts command but isn't ready
  276. } while (i == 1 && --counter > 0);
  277. // Ignore failures, there is at least one Sandisk MMC card
  278. // that accepts CMD55, but not ACMD41.
  279. if (i == 0)
  280. /* We know that a card is SD if ACMD41 was accepted. */
  281. cardtype[card] |= CARD_SD;
  282. }
  283. /* Detect changes of SD card 0 */
  284. #ifdef SD_CHANGE_VECT
  285. ISR(SD_CHANGE_VECT) {
  286. if (SDCARD_DETECT)
  287. disk_state = DISK_CHANGED;
  288. else
  289. disk_state = DISK_REMOVED;
  290. }
  291. #endif
  292. #ifdef CONFIG_TWINSD
  293. /* Detect changes of SD card 1 */
  294. ISR(SD2_CHANGE_VECT) {
  295. if (SD2_DETECT)
  296. disk_state = DISK_CHANGED;
  297. else
  298. disk_state = DISK_REMOVED;
  299. }
  300. #endif
  301. //
  302. // Public functions
  303. //
  304. void sd_init(void) {
  305. SDCARD_DETECT_SETUP();
  306. SDCARD_WP_SETUP();
  307. SD_CHANGE_SETUP();
  308. #ifdef CONFIG_TWINSD
  309. /* Initialize the control lines for card 2 */
  310. SD2_SETUP();
  311. SD2_CHANGE_SETUP();
  312. #endif
  313. }
  314. void disk_init(void) __attribute__ ((weak, alias("sd_init")));
  315. DSTATUS sd_status(BYTE drv) {
  316. #ifdef CONFIG_TWINSD
  317. if (drv != 0) {
  318. if (SD2_DETECT) {
  319. if (SD2_PIN & SD2_WP) {
  320. return STA_PROTECT;
  321. } else {
  322. return RES_OK;
  323. }
  324. } else {
  325. return STA_NOINIT|STA_NODISK;
  326. }
  327. } else
  328. #endif
  329. if (SDCARD_DETECT) {
  330. // uart_putc('0');
  331. if (SDCARD_WP) {
  332. // uart_putc('1');
  333. return STA_PROTECT;
  334. } else {
  335. // uart_putc('2');
  336. return RES_OK;
  337. }
  338. } else {
  339. // uart_putc('3');
  340. return STA_NOINIT|STA_NODISK;
  341. }
  342. }
  343. DSTATUS disk_status(BYTE drv) __attribute__ ((weak, alias("sd_status")));
  344. /**
  345. * sd_initialize - initialize SD card
  346. * @drv : drive
  347. *
  348. * This function tries to initialize the selected SD card.
  349. */
  350. DSTATUS sd_initialize(BYTE drv) {
  351. uint8_t i;
  352. uint16_t counter;
  353. uint32_t answer;
  354. if (drv >= MAX_CARDS)
  355. return STA_NOINIT|STA_NODISK;
  356. /* Don't bother initializing a card that isn't there */
  357. // uart_putc('#');
  358. if (sd_status(drv) & STA_NODISK)
  359. return sd_status(drv);
  360. /* JLB: Should be in sd_init, but some uIEC versions have
  361. * IEC lines tied to SPI, so I moved it here to resolve the
  362. * conflict.
  363. */
  364. spiInit();
  365. disk_state = DISK_ERROR;
  366. cardtype[drv] = 0;
  367. SPI_SS_HIGH(drv);
  368. // Send 80 clks
  369. for (i=0; i<10; i++) {
  370. spiTransferByte(0xFF);
  371. }
  372. // Reset card
  373. i = sendCommand(drv, GO_IDLE_STATE, 0, 1);
  374. if (i != 1) {
  375. return STA_NOINIT | STA_NODISK;
  376. }
  377. if (!extendedInit(drv))
  378. return STA_NOINIT | STA_NODISK;
  379. sdInit(drv);
  380. counter = 0xffff;
  381. // According to the spec READ_OCR should work at this point
  382. // without retries. One of my Sandisk-cards thinks otherwise.
  383. do {
  384. // Send CMD58: READ_OCR
  385. i = sendCommand(drv, READ_OCR, 0, 0);
  386. if (i > 1)
  387. deselectCard(drv);
  388. } while (i > 1 && counter-- > 0);
  389. if (counter > 0) {
  390. answer = spiTransferLong(0);
  391. // See if the card likes our supply voltage
  392. if (!(answer & SD_SUPPLY_VOLTAGE)) {
  393. // The code isn't set up to completely ignore the card,
  394. // but at least report it as nonworking
  395. deselectCard(drv);
  396. return STA_NOINIT | STA_NODISK;
  397. }
  398. // See what card we've got
  399. if (answer & 0x40000000) {
  400. cardtype[drv] |= CARD_SDHC;
  401. }
  402. }
  403. // Keep sending CMD1 (SEND_OP_COND) command until zero response
  404. counter = 0xffff;
  405. do {
  406. i = sendCommand(drv, SEND_OP_COND, 1L<<30, 1);
  407. counter--;
  408. } while (i != 0 && counter > 0);
  409. if (counter==0) {
  410. return STA_NOINIT | STA_NODISK;
  411. }
  412. #ifdef CONFIG_SD_DATACRC
  413. // Enable CRC checking
  414. // The SD spec says that the host "should" send CRC_ON_OFF before ACMD_SEND_OP_COND.
  415. // The MMC manual I have says that CRC_ON_OFF isn't allowed before SEND_OP_COND.
  416. // Let's just hope that all SD cards work with this order. =(
  417. i = sendCommand(drv, CRC_ON_OFF, 1, 1);
  418. if (i > 1) {
  419. return STA_NOINIT | STA_NODISK;
  420. }
  421. #endif
  422. // Send MMC CMD16(SET_BLOCKLEN) to 512 bytes
  423. i = sendCommand(drv, SET_BLOCKLEN, 512, 1);
  424. if (i != 0) {
  425. return STA_NOINIT | STA_NODISK;
  426. }
  427. // Thats it!
  428. disk_state = DISK_OK;
  429. return sd_status(drv);
  430. }
  431. DSTATUS disk_initialize(BYTE drv) __attribute__ ((weak, alias("sd_initialize")));
  432. /**
  433. * sd_read - reads sectors from the SD card to buffer
  434. * @drv : drive
  435. * @buffer: pointer to the buffer
  436. * @sector: first sector to be read
  437. * @count : number of sectors to be read
  438. *
  439. * This function reads count sectors from the SD card starting
  440. * at sector to buffer. Returns RES_ERROR if an error occured or
  441. * RES_OK if successful. Up to SD_AUTO_RETRIES will be made if
  442. * the calculated data CRC does not match the one sent by the
  443. * card. If there were errors during the command transmission
  444. * disk_state will be set to DISK_ERROR and no retries are made.
  445. */
  446. DRESULT sd_read(BYTE drv, BYTE *buffer, DWORD sector, BYTE count) {
  447. uint8_t sec,res,tmp,errorcount;
  448. uint16_t crc,recvcrc;
  449. if (drv >= MAX_CARDS)
  450. return RES_PARERR;
  451. for (sec=0;sec<count;sec++) {
  452. errorcount = 0;
  453. while (errorcount < CONFIG_SD_AUTO_RETRIES) {
  454. if (cardtype[drv] & CARD_SDHC)
  455. res = sendCommand(drv, READ_SINGLE_BLOCK, sector+sec, 0);
  456. else
  457. res = sendCommand(drv, READ_SINGLE_BLOCK, (sector+sec) << 9, 0);
  458. if (res != 0) {
  459. SPI_SS_HIGH(drv);
  460. disk_state = DISK_ERROR;
  461. return RES_ERROR;
  462. }
  463. // Wait for data token
  464. if (!sdResponse(0xFE)) {
  465. SPI_SS_HIGH(drv);
  466. disk_state = DISK_ERROR;
  467. return RES_ERROR;
  468. }
  469. uint16_t i;
  470. #ifdef CONFIG_SD_DATACRC
  471. BYTE *oldbuffer = buffer;
  472. #endif
  473. // Get data
  474. crc = 0;
  475. // Initiate data exchange over SPI
  476. SPDR = 0xff;
  477. for (i=0; i<512; i++) {
  478. // Wait until data has been received
  479. loop_until_bit_is_set(SPSR, SPIF);
  480. tmp = SPDR;
  481. // Transmit the next byte while we store the current one
  482. SPDR = 0xff;
  483. *(buffer++) = tmp;
  484. #ifdef CONFIG_SD_DATACRC
  485. crc = _crc_xmodem_update(crc, tmp);
  486. #endif
  487. }
  488. // Wait until the first CRC byte is received
  489. loop_until_bit_is_set(SPSR, SPIF);
  490. // Check CRC
  491. recvcrc = (SPDR << 8) + spiTransferByte(0xff);
  492. #ifdef CONFIG_SD_DATACRC
  493. if (recvcrc != crc) {
  494. uart_putc('X');
  495. deselectCard(drv);
  496. errorcount++;
  497. buffer = oldbuffer;
  498. continue;
  499. }
  500. #endif
  501. break;
  502. }
  503. deselectCard(drv);
  504. if (errorcount >= CONFIG_SD_AUTO_RETRIES) return RES_ERROR;
  505. }
  506. return RES_OK;
  507. }
  508. DRESULT disk_read(BYTE drv, BYTE *buffer, DWORD sector, BYTE count) __attribute__ ((weak, alias("sd_read")));
  509. /**
  510. * sd_write - writes sectors from buffer to the SD card
  511. * @drv : drive
  512. * @buffer: pointer to the buffer
  513. * @sector: first sector to be written
  514. * @count : number of sectors to be written
  515. *
  516. * This function writes count sectors from buffer to the SD card
  517. * starting at sector. Returns RES_ERROR if an error occured,
  518. * RES_WPRT if the card is currently write-protected or RES_OK
  519. * if successful. Up to SD_AUTO_RETRIES will be made if the card
  520. * signals a CRC error. If there were errors during the command
  521. * transmission disk_state will be set to DISK_ERROR and no retries
  522. * are made.
  523. */
  524. DRESULT sd_write(BYTE drv, const BYTE *buffer, DWORD sector, BYTE count) {
  525. uint8_t res,sec,errorcount,status;
  526. uint16_t crc;
  527. if (drv >= MAX_CARDS)
  528. return RES_PARERR;
  529. #ifdef CONFIG_TWINSD
  530. if (drv != 0) {
  531. if (SD2_PIN & SD2_WP)
  532. return RES_WRPRT;
  533. } else
  534. #endif
  535. if (SDCARD_WP) return RES_WRPRT;
  536. for (sec=0;sec<count;sec++) {
  537. errorcount = 0;
  538. while (errorcount < CONFIG_SD_AUTO_RETRIES) {
  539. if (cardtype[drv] & CARD_SDHC)
  540. res = sendCommand(drv, WRITE_BLOCK, sector+sec, 0);
  541. else
  542. res = sendCommand(drv, WRITE_BLOCK, (sector+sec)<<9, 0);
  543. if (res != 0) {
  544. SPI_SS_HIGH(drv);
  545. disk_state = DISK_ERROR;
  546. return RES_ERROR;
  547. }
  548. // Send data token
  549. spiTransferByte(0xFE);
  550. uint16_t i;
  551. const BYTE *oldbuffer = buffer;
  552. // Send data
  553. crc = 0;
  554. for (i=0; i<512; i++) {
  555. #ifdef CONFIG_SD_DATACRC
  556. crc = _crc_xmodem_update(crc, *buffer);
  557. #endif
  558. spiTransferByte(*(buffer++));
  559. }
  560. // Send CRC
  561. spiTransferByte(crc >> 8);
  562. spiTransferByte(crc & 0xff);
  563. // Get and check status feedback
  564. status = spiTransferByte(0xFF);
  565. // Retry if neccessary
  566. if ((status & 0x0F) != 0x05) {
  567. uart_putc('X');
  568. deselectCard(drv);
  569. errorcount++;
  570. buffer = oldbuffer;
  571. continue;
  572. }
  573. // Wait for write finish
  574. if (!sdWaitWriteFinish()) {
  575. SPI_SS_HIGH(drv);
  576. disk_state = DISK_ERROR;
  577. return RES_ERROR;
  578. }
  579. break;
  580. }
  581. deselectCard(drv);
  582. if (errorcount >= CONFIG_SD_AUTO_RETRIES) {
  583. if (!(status & STATUS_CRC_ERROR))
  584. disk_state = DISK_ERROR;
  585. return RES_ERROR;
  586. }
  587. }
  588. return RES_OK;
  589. }
  590. DRESULT disk_write(BYTE drv, const BYTE *buffer, DWORD sector, BYTE count) __attribute__ ((weak, alias("sd_write")));
  591. DRESULT sd_getinfo(BYTE drv, BYTE page, void *buffer) {
  592. uint8_t i;
  593. uint8_t buf[18];
  594. uint32_t capacity;
  595. if (drv >= MAX_CARDS)
  596. return RES_NOTRDY;
  597. if (sd_status(drv) & STA_NODISK)
  598. return RES_NOTRDY;
  599. if (page != 0)
  600. return RES_ERROR;
  601. /* Try to calculate the total number of sectors on the card */
  602. /* FIXME: Write a generic data read function and merge with sd_read */
  603. if (sendCommand(drv, SEND_CSD, 0, 0) != 0) {
  604. deselectCard(drv);
  605. return RES_ERROR;
  606. }
  607. /* Wait for data token */
  608. if (!sdResponse(0xfe)) {
  609. deselectCard(drv);
  610. return RES_ERROR;
  611. }
  612. for (i=0;i<18;i++) {
  613. buf[i] = spiTransferByte(0xff);
  614. }
  615. deselectCard(drv);
  616. if (cardtype[drv] & CARD_SDHC) {
  617. /* Special CSD for SDHC cards */
  618. capacity = (1 + getbits(buf,127-69,22)) * 1024;
  619. } else {
  620. /* Assume that MMC-CSD 1.0/1.1/1.2 and SD-CSD 1.1 are the same... */
  621. uint8_t exponent = 2 + getbits(buf, 127-49, 3);
  622. capacity = 1 + getbits(buf, 127-73, 12);
  623. exponent += getbits(buf, 127-83,4) - 9;
  624. while (exponent--) capacity *= 2;
  625. }
  626. diskinfo0_t *di = buffer;
  627. di->validbytes = sizeof(diskinfo0_t);
  628. di->disktype = DISK_TYPE_SD;
  629. di->sectorsize = 2;
  630. di->sectorcount = capacity;
  631. return RES_OK;
  632. }
  633. DRESULT disk_getinfo(BYTE drv, BYTE page, void *buffer) __attribute__ ((weak, alias("sd_getinfo")));