/firmware/src/Motherboard/lib_sd/sd_raw.c

http://github.com/makerbot/G3Firmware · C · 994 lines · 613 code · 113 blank · 268 comment · 123 complexity · 9e49e700d36a684db7e72d25c1e0f3c3 MD5 · raw file

  1. /*
  2. * Copyright (c) 2006-2010 by Roland Riegel <feedback@roland-riegel.de>
  3. *
  4. * This file is free software; you can redistribute it and/or modify
  5. * it under the terms of either the GNU General Public License version 2
  6. * or the GNU Lesser General Public License version 2.1, both as
  7. * published by the Free Software Foundation.
  8. */
  9. #include <string.h>
  10. #include <avr/io.h>
  11. #include "sd_raw.h"
  12. /**
  13. * \addtogroup sd_raw MMC/SD/SDHC card raw access
  14. *
  15. * This module implements read and write access to MMC, SD
  16. * and SDHC cards. It serves as a low-level driver for the
  17. * higher level modules such as partition and file system
  18. * access.
  19. *
  20. * @{
  21. */
  22. /**
  23. * \file
  24. * MMC/SD/SDHC raw access implementation (license: GPLv2 or LGPLv2.1)
  25. *
  26. * \author Roland Riegel
  27. */
  28. /**
  29. * \addtogroup sd_raw_config MMC/SD configuration
  30. * Preprocessor defines to configure the MMC/SD support.
  31. */
  32. /**
  33. * @}
  34. */
  35. /* commands available in SPI mode */
  36. /* CMD0: response R1 */
  37. #define CMD_GO_IDLE_STATE 0x00
  38. /* CMD1: response R1 */
  39. #define CMD_SEND_OP_COND 0x01
  40. /* CMD8: response R7 */
  41. #define CMD_SEND_IF_COND 0x08
  42. /* CMD9: response R1 */
  43. #define CMD_SEND_CSD 0x09
  44. /* CMD10: response R1 */
  45. #define CMD_SEND_CID 0x0a
  46. /* CMD12: response R1 */
  47. #define CMD_STOP_TRANSMISSION 0x0c
  48. /* CMD13: response R2 */
  49. #define CMD_SEND_STATUS 0x0d
  50. /* CMD16: arg0[31:0]: block length, response R1 */
  51. #define CMD_SET_BLOCKLEN 0x10
  52. /* CMD17: arg0[31:0]: data address, response R1 */
  53. #define CMD_READ_SINGLE_BLOCK 0x11
  54. /* CMD18: arg0[31:0]: data address, response R1 */
  55. #define CMD_READ_MULTIPLE_BLOCK 0x12
  56. /* CMD24: arg0[31:0]: data address, response R1 */
  57. #define CMD_WRITE_SINGLE_BLOCK 0x18
  58. /* CMD25: arg0[31:0]: data address, response R1 */
  59. #define CMD_WRITE_MULTIPLE_BLOCK 0x19
  60. /* CMD27: response R1 */
  61. #define CMD_PROGRAM_CSD 0x1b
  62. /* CMD28: arg0[31:0]: data address, response R1b */
  63. #define CMD_SET_WRITE_PROT 0x1c
  64. /* CMD29: arg0[31:0]: data address, response R1b */
  65. #define CMD_CLR_WRITE_PROT 0x1d
  66. /* CMD30: arg0[31:0]: write protect data address, response R1 */
  67. #define CMD_SEND_WRITE_PROT 0x1e
  68. /* CMD32: arg0[31:0]: data address, response R1 */
  69. #define CMD_TAG_SECTOR_START 0x20
  70. /* CMD33: arg0[31:0]: data address, response R1 */
  71. #define CMD_TAG_SECTOR_END 0x21
  72. /* CMD34: arg0[31:0]: data address, response R1 */
  73. #define CMD_UNTAG_SECTOR 0x22
  74. /* CMD35: arg0[31:0]: data address, response R1 */
  75. #define CMD_TAG_ERASE_GROUP_START 0x23
  76. /* CMD36: arg0[31:0]: data address, response R1 */
  77. #define CMD_TAG_ERASE_GROUP_END 0x24
  78. /* CMD37: arg0[31:0]: data address, response R1 */
  79. #define CMD_UNTAG_ERASE_GROUP 0x25
  80. /* CMD38: arg0[31:0]: stuff bits, response R1b */
  81. #define CMD_ERASE 0x26
  82. /* ACMD41: arg0[31:0]: OCR contents, response R1 */
  83. #define CMD_SD_SEND_OP_COND 0x29
  84. /* CMD42: arg0[31:0]: stuff bits, response R1b */
  85. #define CMD_LOCK_UNLOCK 0x2a
  86. /* CMD55: arg0[31:0]: stuff bits, response R1 */
  87. #define CMD_APP 0x37
  88. /* CMD58: arg0[31:0]: stuff bits, response R3 */
  89. #define CMD_READ_OCR 0x3a
  90. /* CMD59: arg0[31:1]: stuff bits, arg0[0:0]: crc option, response R1 */
  91. #define CMD_CRC_ON_OFF 0x3b
  92. /* command responses */
  93. /* R1: size 1 byte */
  94. #define R1_IDLE_STATE 0
  95. #define R1_ERASE_RESET 1
  96. #define R1_ILL_COMMAND 2
  97. #define R1_COM_CRC_ERR 3
  98. #define R1_ERASE_SEQ_ERR 4
  99. #define R1_ADDR_ERR 5
  100. #define R1_PARAM_ERR 6
  101. /* R1b: equals R1, additional busy bytes */
  102. /* R2: size 2 bytes */
  103. #define R2_CARD_LOCKED 0
  104. #define R2_WP_ERASE_SKIP 1
  105. #define R2_ERR 2
  106. #define R2_CARD_ERR 3
  107. #define R2_CARD_ECC_FAIL 4
  108. #define R2_WP_VIOLATION 5
  109. #define R2_INVAL_ERASE 6
  110. #define R2_OUT_OF_RANGE 7
  111. #define R2_CSD_OVERWRITE 7
  112. #define R2_IDLE_STATE (R1_IDLE_STATE + 8)
  113. #define R2_ERASE_RESET (R1_ERASE_RESET + 8)
  114. #define R2_ILL_COMMAND (R1_ILL_COMMAND + 8)
  115. #define R2_COM_CRC_ERR (R1_COM_CRC_ERR + 8)
  116. #define R2_ERASE_SEQ_ERR (R1_ERASE_SEQ_ERR + 8)
  117. #define R2_ADDR_ERR (R1_ADDR_ERR + 8)
  118. #define R2_PARAM_ERR (R1_PARAM_ERR + 8)
  119. /* R3: size 5 bytes */
  120. #define R3_OCR_MASK (0xffffffffUL)
  121. #define R3_IDLE_STATE (R1_IDLE_STATE + 32)
  122. #define R3_ERASE_RESET (R1_ERASE_RESET + 32)
  123. #define R3_ILL_COMMAND (R1_ILL_COMMAND + 32)
  124. #define R3_COM_CRC_ERR (R1_COM_CRC_ERR + 32)
  125. #define R3_ERASE_SEQ_ERR (R1_ERASE_SEQ_ERR + 32)
  126. #define R3_ADDR_ERR (R1_ADDR_ERR + 32)
  127. #define R3_PARAM_ERR (R1_PARAM_ERR + 32)
  128. /* Data Response: size 1 byte */
  129. #define DR_STATUS_MASK 0x0e
  130. #define DR_STATUS_ACCEPTED 0x05
  131. #define DR_STATUS_CRC_ERR 0x0a
  132. #define DR_STATUS_WRITE_ERR 0x0c
  133. /* status bits for card types */
  134. #define SD_RAW_SPEC_1 0
  135. #define SD_RAW_SPEC_2 1
  136. #define SD_RAW_SPEC_SDHC 2
  137. #if !SD_RAW_SAVE_RAM
  138. /* static data buffer for acceleration */
  139. static uint8_t raw_block[512];
  140. /* offset where the data within raw_block lies on the card */
  141. static offset_t raw_block_address;
  142. #if SD_RAW_WRITE_BUFFERING
  143. /* flag to remember if raw_block was written to the card */
  144. static uint8_t raw_block_written;
  145. #endif
  146. #endif
  147. /* card type state */
  148. static uint8_t sd_raw_card_type;
  149. /* private helper functions */
  150. static void sd_raw_send_byte(uint8_t b);
  151. static uint8_t sd_raw_rec_byte();
  152. static uint8_t sd_raw_send_command(uint8_t command, uint32_t arg);
  153. /**
  154. * \ingroup sd_raw
  155. * Initializes memory card communication.
  156. *
  157. * \returns 0 on failure, 1 on success.
  158. */
  159. uint8_t sd_raw_init()
  160. {
  161. /* enable inputs for reading card status */
  162. configure_pin_available();
  163. configure_pin_locked();
  164. /* enable outputs for MOSI, SCK, SS, input for MISO */
  165. configure_pin_mosi();
  166. configure_pin_sck();
  167. configure_pin_ss();
  168. configure_pin_miso();
  169. unselect_card();
  170. /* initialize SPI with lowest frequency; max. 400kHz during identification mode of card */
  171. SPCR = (0 << SPIE) | /* SPI Interrupt Enable */
  172. (1 << SPE) | /* SPI Enable */
  173. (0 << DORD) | /* Data Order: MSB first */
  174. (1 << MSTR) | /* Master mode */
  175. (0 << CPOL) | /* Clock Polarity: SCK low when idle */
  176. (0 << CPHA) | /* Clock Phase: sample on rising SCK edge */
  177. (1 << SPR1) | /* Clock Frequency: f_OSC / 128 */
  178. (1 << SPR0);
  179. SPSR &= ~(1 << SPI2X); /* No doubled clock frequency */
  180. /* initialization procedure */
  181. sd_raw_card_type = 0;
  182. if(!sd_raw_available())
  183. return 0;
  184. /* card needs 74 cycles minimum to start up */
  185. for(uint8_t i = 0; i < 10; ++i)
  186. {
  187. /* wait 8 clock cycles */
  188. sd_raw_rec_byte();
  189. }
  190. /* address card */
  191. select_card();
  192. /* reset card */
  193. uint8_t response;
  194. for(uint16_t i = 0; ; ++i)
  195. {
  196. response = sd_raw_send_command(CMD_GO_IDLE_STATE, 0);
  197. if(response == (1 << R1_IDLE_STATE))
  198. break;
  199. if(i == 0x1ff)
  200. {
  201. unselect_card();
  202. return 0;
  203. }
  204. }
  205. #if SD_RAW_SDHC
  206. /* check for version of SD card specification */
  207. response = sd_raw_send_command(CMD_SEND_IF_COND, 0x100 /* 2.7V - 3.6V */ | 0xaa /* test pattern */);
  208. if((response & (1 << R1_ILL_COMMAND)) == 0)
  209. {
  210. sd_raw_rec_byte();
  211. sd_raw_rec_byte();
  212. if((sd_raw_rec_byte() & 0x01) == 0)
  213. return 0; /* card operation voltage range doesn't match */
  214. if(sd_raw_rec_byte() != 0xaa)
  215. return 0; /* wrong test pattern */
  216. /* card conforms to SD 2 card specification */
  217. sd_raw_card_type |= (1 << SD_RAW_SPEC_2);
  218. }
  219. else
  220. #endif
  221. {
  222. /* determine SD/MMC card type */
  223. sd_raw_send_command(CMD_APP, 0);
  224. response = sd_raw_send_command(CMD_SD_SEND_OP_COND, 0);
  225. if((response & (1 << R1_ILL_COMMAND)) == 0)
  226. {
  227. /* card conforms to SD 1 card specification */
  228. sd_raw_card_type |= (1 << SD_RAW_SPEC_1);
  229. }
  230. else
  231. {
  232. /* MMC card */
  233. }
  234. }
  235. /* wait for card to get ready */
  236. for(uint16_t i = 0; ; ++i)
  237. {
  238. if(sd_raw_card_type & ((1 << SD_RAW_SPEC_1) | (1 << SD_RAW_SPEC_2)))
  239. {
  240. uint32_t arg = 0;
  241. #if SD_RAW_SDHC
  242. if(sd_raw_card_type & (1 << SD_RAW_SPEC_2))
  243. arg = 0x40000000;
  244. #endif
  245. sd_raw_send_command(CMD_APP, 0);
  246. response = sd_raw_send_command(CMD_SD_SEND_OP_COND, arg);
  247. }
  248. else
  249. {
  250. response = sd_raw_send_command(CMD_SEND_OP_COND, 0);
  251. }
  252. if((response & (1 << R1_IDLE_STATE)) == 0)
  253. break;
  254. if(i == 0x7fff)
  255. {
  256. unselect_card();
  257. return 0;
  258. }
  259. }
  260. #if SD_RAW_SDHC
  261. if(sd_raw_card_type & (1 << SD_RAW_SPEC_2))
  262. {
  263. if(sd_raw_send_command(CMD_READ_OCR, 0))
  264. {
  265. unselect_card();
  266. return 0;
  267. }
  268. if(sd_raw_rec_byte() & 0x40)
  269. sd_raw_card_type |= (1 << SD_RAW_SPEC_SDHC);
  270. sd_raw_rec_byte();
  271. sd_raw_rec_byte();
  272. sd_raw_rec_byte();
  273. }
  274. #endif
  275. /* set block size to 512 bytes */
  276. if(sd_raw_send_command(CMD_SET_BLOCKLEN, 512))
  277. {
  278. unselect_card();
  279. return 0;
  280. }
  281. /* deaddress card */
  282. unselect_card();
  283. /* switch to highest SPI frequency possible */
  284. SPCR &= ~((1 << SPR1) | (1 << SPR0)); /* Clock Frequency: f_OSC / 4 */
  285. SPSR |= (1 << SPI2X); /* Doubled Clock Frequency: f_OSC / 2 */
  286. #if !SD_RAW_SAVE_RAM
  287. /* the first block is likely to be accessed first, so precache it here */
  288. raw_block_address = (offset_t) -1;
  289. #if SD_RAW_WRITE_BUFFERING
  290. raw_block_written = 1;
  291. #endif
  292. if(!sd_raw_read(0, raw_block, sizeof(raw_block)))
  293. return 0;
  294. #endif
  295. return 1;
  296. }
  297. /**
  298. * \ingroup sd_raw
  299. * Checks wether a memory card is located in the slot.
  300. *
  301. * \returns 1 if the card is available, 0 if it is not.
  302. */
  303. uint8_t sd_raw_available()
  304. {
  305. return get_pin_available() == 0x00;
  306. }
  307. /**
  308. * \ingroup sd_raw
  309. * Checks wether the memory card is locked for write access.
  310. *
  311. * \returns 1 if the card is locked, 0 if it is not.
  312. */
  313. uint8_t sd_raw_locked()
  314. {
  315. return get_pin_locked() == 0x00;
  316. }
  317. /**
  318. * \ingroup sd_raw
  319. * Sends a raw byte to the memory card.
  320. *
  321. * \param[in] b The byte to sent.
  322. * \see sd_raw_rec_byte
  323. */
  324. void sd_raw_send_byte(uint8_t b)
  325. {
  326. SPDR = b;
  327. /* wait for byte to be shifted out */
  328. while(!(SPSR & (1 << SPIF)));
  329. SPSR &= ~(1 << SPIF);
  330. }
  331. /**
  332. * \ingroup sd_raw
  333. * Receives a raw byte from the memory card.
  334. *
  335. * \returns The byte which should be read.
  336. * \see sd_raw_send_byte
  337. */
  338. uint8_t sd_raw_rec_byte()
  339. {
  340. /* send dummy data for receiving some */
  341. SPDR = 0xff;
  342. while(!(SPSR & (1 << SPIF)));
  343. SPSR &= ~(1 << SPIF);
  344. return SPDR;
  345. }
  346. /**
  347. * \ingroup sd_raw
  348. * Send a command to the memory card which responses with a R1 response (and possibly others).
  349. *
  350. * \param[in] command The command to send.
  351. * \param[in] arg The argument for command.
  352. * \returns The command answer.
  353. */
  354. uint8_t sd_raw_send_command(uint8_t command, uint32_t arg)
  355. {
  356. uint8_t response;
  357. /* wait some clock cycles */
  358. sd_raw_rec_byte();
  359. /* send command via SPI */
  360. sd_raw_send_byte(0x40 | command);
  361. sd_raw_send_byte((arg >> 24) & 0xff);
  362. sd_raw_send_byte((arg >> 16) & 0xff);
  363. sd_raw_send_byte((arg >> 8) & 0xff);
  364. sd_raw_send_byte((arg >> 0) & 0xff);
  365. switch(command)
  366. {
  367. case CMD_GO_IDLE_STATE:
  368. sd_raw_send_byte(0x95);
  369. break;
  370. case CMD_SEND_IF_COND:
  371. sd_raw_send_byte(0x87);
  372. break;
  373. default:
  374. sd_raw_send_byte(0xff);
  375. break;
  376. }
  377. /* receive response */
  378. for(uint8_t i = 0; i < 10; ++i)
  379. {
  380. response = sd_raw_rec_byte();
  381. if(response != 0xff)
  382. break;
  383. }
  384. return response;
  385. }
  386. /**
  387. * \ingroup sd_raw
  388. * Reads raw data from the card.
  389. *
  390. * \param[in] offset The offset from which to read.
  391. * \param[out] buffer The buffer into which to write the data.
  392. * \param[in] length The number of bytes to read.
  393. * \returns 0 on failure, 1 on success.
  394. * \see sd_raw_read_interval, sd_raw_write, sd_raw_write_interval
  395. */
  396. uint8_t sd_raw_read(offset_t offset, uint8_t* buffer, uintptr_t length)
  397. {
  398. offset_t block_address;
  399. uint16_t block_offset;
  400. uint16_t read_length;
  401. while(length > 0)
  402. {
  403. /* determine byte count to read at once */
  404. block_offset = offset & 0x01ff;
  405. block_address = offset - block_offset;
  406. read_length = 512 - block_offset; /* read up to block border */
  407. if(read_length > length)
  408. read_length = length;
  409. #if !SD_RAW_SAVE_RAM
  410. /* check if the requested data is cached */
  411. if(block_address != raw_block_address)
  412. #endif
  413. {
  414. #if SD_RAW_WRITE_BUFFERING
  415. if(!sd_raw_sync())
  416. return 0;
  417. #endif
  418. /* address card */
  419. select_card();
  420. /* send single block request */
  421. #if SD_RAW_SDHC
  422. if(sd_raw_send_command(CMD_READ_SINGLE_BLOCK, (sd_raw_card_type & (1 << SD_RAW_SPEC_SDHC) ? block_address / 512 : block_address)))
  423. #else
  424. if(sd_raw_send_command(CMD_READ_SINGLE_BLOCK, block_address))
  425. #endif
  426. {
  427. unselect_card();
  428. return 0;
  429. }
  430. /* wait for data block (start byte 0xfe) */
  431. while(sd_raw_rec_byte() != 0xfe);
  432. #if SD_RAW_SAVE_RAM
  433. /* read byte block */
  434. uint16_t read_to = block_offset + read_length;
  435. for(uint16_t i = 0; i < 512; ++i)
  436. {
  437. uint8_t b = sd_raw_rec_byte();
  438. if(i >= block_offset && i < read_to)
  439. *buffer++ = b;
  440. }
  441. #else
  442. /* read byte block */
  443. uint8_t* cache = raw_block;
  444. for(uint16_t i = 0; i < 512; ++i)
  445. *cache++ = sd_raw_rec_byte();
  446. raw_block_address = block_address;
  447. memcpy(buffer, raw_block + block_offset, read_length);
  448. buffer += read_length;
  449. #endif
  450. /* read crc16 */
  451. sd_raw_rec_byte();
  452. sd_raw_rec_byte();
  453. /* deaddress card */
  454. unselect_card();
  455. /* let card some time to finish */
  456. sd_raw_rec_byte();
  457. }
  458. #if !SD_RAW_SAVE_RAM
  459. else
  460. {
  461. /* use cached data */
  462. memcpy(buffer, raw_block + block_offset, read_length);
  463. buffer += read_length;
  464. }
  465. #endif
  466. length -= read_length;
  467. offset += read_length;
  468. }
  469. return 1;
  470. }
  471. /**
  472. * \ingroup sd_raw
  473. * Continuously reads units of \c interval bytes and calls a callback function.
  474. *
  475. * This function starts reading at the specified offset. Every \c interval bytes,
  476. * it calls the callback function with the associated data buffer.
  477. *
  478. * By returning zero, the callback may stop reading.
  479. *
  480. * \note Within the callback function, you can not start another read or
  481. * write operation.
  482. * \note This function only works if the following conditions are met:
  483. * - (offset - (offset % 512)) % interval == 0
  484. * - length % interval == 0
  485. *
  486. * \param[in] offset Offset from which to start reading.
  487. * \param[in] buffer Pointer to a buffer which is at least interval bytes in size.
  488. * \param[in] interval Number of bytes to read before calling the callback function.
  489. * \param[in] length Number of bytes to read altogether.
  490. * \param[in] callback The function to call every interval bytes.
  491. * \param[in] p An opaque pointer directly passed to the callback function.
  492. * \returns 0 on failure, 1 on success
  493. * \see sd_raw_write_interval, sd_raw_read, sd_raw_write
  494. */
  495. uint8_t sd_raw_read_interval(offset_t offset, uint8_t* buffer, uintptr_t interval, uintptr_t length, sd_raw_read_interval_handler_t callback, void* p)
  496. {
  497. if(!buffer || interval == 0 || length < interval || !callback)
  498. return 0;
  499. #if !SD_RAW_SAVE_RAM
  500. while(length >= interval)
  501. {
  502. /* as reading is now buffered, we directly
  503. * hand over the request to sd_raw_read()
  504. */
  505. if(!sd_raw_read(offset, buffer, interval))
  506. return 0;
  507. if(!callback(buffer, offset, p))
  508. break;
  509. offset += interval;
  510. length -= interval;
  511. }
  512. return 1;
  513. #else
  514. /* address card */
  515. select_card();
  516. uint16_t block_offset;
  517. uint16_t read_length;
  518. uint8_t* buffer_cur;
  519. uint8_t finished = 0;
  520. do
  521. {
  522. /* determine byte count to read at once */
  523. block_offset = offset & 0x01ff;
  524. read_length = 512 - block_offset;
  525. /* send single block request */
  526. #if SD_RAW_SDHC
  527. if(sd_raw_send_command(CMD_READ_SINGLE_BLOCK, (sd_raw_card_type & (1 << SD_RAW_SPEC_SDHC) ? offset / 512 : offset - block_offset)))
  528. #else
  529. if(sd_raw_send_command(CMD_READ_SINGLE_BLOCK, offset - block_offset))
  530. #endif
  531. {
  532. unselect_card();
  533. return 0;
  534. }
  535. /* wait for data block (start byte 0xfe) */
  536. while(sd_raw_rec_byte() != 0xfe);
  537. /* read up to the data of interest */
  538. for(uint16_t i = 0; i < block_offset; ++i)
  539. sd_raw_rec_byte();
  540. /* read interval bytes of data and execute the callback */
  541. do
  542. {
  543. if(read_length < interval || length < interval)
  544. break;
  545. buffer_cur = buffer;
  546. for(uint16_t i = 0; i < interval; ++i)
  547. *buffer_cur++ = sd_raw_rec_byte();
  548. if(!callback(buffer, offset + (512 - read_length), p))
  549. {
  550. finished = 1;
  551. break;
  552. }
  553. read_length -= interval;
  554. length -= interval;
  555. } while(read_length > 0 && length > 0);
  556. /* read rest of data block */
  557. while(read_length-- > 0)
  558. sd_raw_rec_byte();
  559. /* read crc16 */
  560. sd_raw_rec_byte();
  561. sd_raw_rec_byte();
  562. if(length < interval)
  563. break;
  564. offset = offset - block_offset + 512;
  565. } while(!finished);
  566. /* deaddress card */
  567. unselect_card();
  568. /* let card some time to finish */
  569. sd_raw_rec_byte();
  570. return 1;
  571. #endif
  572. }
  573. #if DOXYGEN || SD_RAW_WRITE_SUPPORT
  574. /**
  575. * \ingroup sd_raw
  576. * Writes raw data to the card.
  577. *
  578. * \note If write buffering is enabled, you might have to
  579. * call sd_raw_sync() before disconnecting the card
  580. * to ensure all remaining data has been written.
  581. *
  582. * \param[in] offset The offset where to start writing.
  583. * \param[in] buffer The buffer containing the data to be written.
  584. * \param[in] length The number of bytes to write.
  585. * \returns 0 on failure, 1 on success.
  586. * \see sd_raw_write_interval, sd_raw_read, sd_raw_read_interval
  587. */
  588. uint8_t sd_raw_write(offset_t offset, const uint8_t* buffer, uintptr_t length)
  589. {
  590. if(sd_raw_locked())
  591. return 0;
  592. offset_t block_address;
  593. uint16_t block_offset;
  594. uint16_t write_length;
  595. while(length > 0)
  596. {
  597. /* determine byte count to write at once */
  598. block_offset = offset & 0x01ff;
  599. block_address = offset - block_offset;
  600. write_length = 512 - block_offset; /* write up to block border */
  601. if(write_length > length)
  602. write_length = length;
  603. /* Merge the data to write with the content of the block.
  604. * Use the cached block if available.
  605. */
  606. if(block_address != raw_block_address)
  607. {
  608. #if SD_RAW_WRITE_BUFFERING
  609. if(!sd_raw_sync())
  610. return 0;
  611. #endif
  612. if(block_offset || write_length < 512)
  613. {
  614. if(!sd_raw_read(block_address, raw_block, sizeof(raw_block)))
  615. return 0;
  616. }
  617. raw_block_address = block_address;
  618. }
  619. if(buffer != raw_block)
  620. {
  621. memcpy(raw_block + block_offset, buffer, write_length);
  622. #if SD_RAW_WRITE_BUFFERING
  623. raw_block_written = 0;
  624. if(length == write_length)
  625. return 1;
  626. #endif
  627. }
  628. /* address card */
  629. select_card();
  630. /* send single block request */
  631. #if SD_RAW_SDHC
  632. if(sd_raw_send_command(CMD_WRITE_SINGLE_BLOCK, (sd_raw_card_type & (1 << SD_RAW_SPEC_SDHC) ? block_address / 512 : block_address)))
  633. #else
  634. if(sd_raw_send_command(CMD_WRITE_SINGLE_BLOCK, block_address))
  635. #endif
  636. {
  637. unselect_card();
  638. return 0;
  639. }
  640. /* send start byte */
  641. sd_raw_send_byte(0xfe);
  642. /* write byte block */
  643. uint8_t* cache = raw_block;
  644. for(uint16_t i = 0; i < 512; ++i)
  645. sd_raw_send_byte(*cache++);
  646. /* write dummy crc16 */
  647. sd_raw_send_byte(0xff);
  648. sd_raw_send_byte(0xff);
  649. /* wait while card is busy */
  650. while(sd_raw_rec_byte() != 0xff);
  651. sd_raw_rec_byte();
  652. /* deaddress card */
  653. unselect_card();
  654. buffer += write_length;
  655. offset += write_length;
  656. length -= write_length;
  657. #if SD_RAW_WRITE_BUFFERING
  658. raw_block_written = 1;
  659. #endif
  660. }
  661. return 1;
  662. }
  663. #endif
  664. #if DOXYGEN || SD_RAW_WRITE_SUPPORT
  665. /**
  666. * \ingroup sd_raw
  667. * Writes a continuous data stream obtained from a callback function.
  668. *
  669. * This function starts writing at the specified offset. To obtain the
  670. * next bytes to write, it calls the callback function. The callback fills the
  671. * provided data buffer and returns the number of bytes it has put into the buffer.
  672. *
  673. * By returning zero, the callback may stop writing.
  674. *
  675. * \param[in] offset Offset where to start writing.
  676. * \param[in] buffer Pointer to a buffer which is used for the callback function.
  677. * \param[in] length Number of bytes to write in total. May be zero for endless writes.
  678. * \param[in] callback The function used to obtain the bytes to write.
  679. * \param[in] p An opaque pointer directly passed to the callback function.
  680. * \returns 0 on failure, 1 on success
  681. * \see sd_raw_read_interval, sd_raw_write, sd_raw_read
  682. */
  683. uint8_t sd_raw_write_interval(offset_t offset, uint8_t* buffer, uintptr_t length, sd_raw_write_interval_handler_t callback, void* p)
  684. {
  685. #if SD_RAW_SAVE_RAM
  686. #error "SD_RAW_WRITE_SUPPORT is not supported together with SD_RAW_SAVE_RAM"
  687. #endif
  688. if(!buffer || !callback)
  689. return 0;
  690. uint8_t endless = (length == 0);
  691. while(endless || length > 0)
  692. {
  693. uint16_t bytes_to_write = callback(buffer, offset, p);
  694. if(!bytes_to_write)
  695. break;
  696. if(!endless && bytes_to_write > length)
  697. return 0;
  698. /* as writing is always buffered, we directly
  699. * hand over the request to sd_raw_write()
  700. */
  701. if(!sd_raw_write(offset, buffer, bytes_to_write))
  702. return 0;
  703. offset += bytes_to_write;
  704. length -= bytes_to_write;
  705. }
  706. return 1;
  707. }
  708. #endif
  709. #if DOXYGEN || SD_RAW_WRITE_SUPPORT
  710. /**
  711. * \ingroup sd_raw
  712. * Writes the write buffer's content to the card.
  713. *
  714. * \note When write buffering is enabled, you should
  715. * call this function before disconnecting the
  716. * card to ensure all remaining data has been
  717. * written.
  718. *
  719. * \returns 0 on failure, 1 on success.
  720. * \see sd_raw_write
  721. */
  722. uint8_t sd_raw_sync()
  723. {
  724. #if SD_RAW_WRITE_BUFFERING
  725. if(raw_block_written)
  726. return 1;
  727. if(!sd_raw_write(raw_block_address, raw_block, sizeof(raw_block)))
  728. return 0;
  729. raw_block_written = 1;
  730. #endif
  731. return 1;
  732. }
  733. #endif
  734. /**
  735. * \ingroup sd_raw
  736. * Reads informational data from the card.
  737. *
  738. * This function reads and returns the card's registers
  739. * containing manufacturing and status information.
  740. *
  741. * \note: The information retrieved by this function is
  742. * not required in any way to operate on the card,
  743. * but it might be nice to display some of the data
  744. * to the user.
  745. *
  746. * \param[in] info A pointer to the structure into which to save the information.
  747. * \returns 0 on failure, 1 on success.
  748. */
  749. uint8_t sd_raw_get_info(struct sd_raw_info* info)
  750. {
  751. if(!info || !sd_raw_available())
  752. return 0;
  753. memset(info, 0, sizeof(*info));
  754. select_card();
  755. /* read cid register */
  756. if(sd_raw_send_command(CMD_SEND_CID, 0))
  757. {
  758. unselect_card();
  759. return 0;
  760. }
  761. while(sd_raw_rec_byte() != 0xfe);
  762. for(uint8_t i = 0; i < 18; ++i)
  763. {
  764. uint8_t b = sd_raw_rec_byte();
  765. switch(i)
  766. {
  767. case 0:
  768. info->manufacturer = b;
  769. break;
  770. case 1:
  771. case 2:
  772. info->oem[i - 1] = b;
  773. break;
  774. case 3:
  775. case 4:
  776. case 5:
  777. case 6:
  778. case 7:
  779. info->product[i - 3] = b;
  780. break;
  781. case 8:
  782. info->revision = b;
  783. break;
  784. case 9:
  785. case 10:
  786. case 11:
  787. case 12:
  788. info->serial |= (uint32_t) b << ((12 - i) * 8);
  789. break;
  790. case 13:
  791. info->manufacturing_year = b << 4;
  792. break;
  793. case 14:
  794. info->manufacturing_year |= b >> 4;
  795. info->manufacturing_month = b & 0x0f;
  796. break;
  797. }
  798. }
  799. /* read csd register */
  800. uint8_t csd_read_bl_len = 0;
  801. uint8_t csd_c_size_mult = 0;
  802. #if SD_RAW_SDHC
  803. uint16_t csd_c_size = 0;
  804. #else
  805. uint32_t csd_c_size = 0;
  806. #endif
  807. if(sd_raw_send_command(CMD_SEND_CSD, 0))
  808. {
  809. unselect_card();
  810. return 0;
  811. }
  812. while(sd_raw_rec_byte() != 0xfe);
  813. for(uint8_t i = 0; i < 18; ++i)
  814. {
  815. uint8_t b = sd_raw_rec_byte();
  816. if(i == 14)
  817. {
  818. if(b & 0x40)
  819. info->flag_copy = 1;
  820. if(b & 0x20)
  821. info->flag_write_protect = 1;
  822. if(b & 0x10)
  823. info->flag_write_protect_temp = 1;
  824. info->format = (b & 0x0c) >> 2;
  825. }
  826. else
  827. {
  828. #if SD_RAW_SDHC
  829. if(sd_raw_card_type & (1 << SD_RAW_SPEC_2))
  830. {
  831. switch(i)
  832. {
  833. case 7:
  834. b &= 0x3f;
  835. case 8:
  836. case 9:
  837. csd_c_size <<= 8;
  838. csd_c_size |= b;
  839. break;
  840. }
  841. if(i == 9)
  842. {
  843. ++csd_c_size;
  844. info->capacity = (offset_t) csd_c_size * 512 * 1024;
  845. }
  846. }
  847. else
  848. #endif
  849. {
  850. switch(i)
  851. {
  852. case 5:
  853. csd_read_bl_len = b & 0x0f;
  854. break;
  855. case 6:
  856. csd_c_size = b & 0x03;
  857. csd_c_size <<= 8;
  858. break;
  859. case 7:
  860. csd_c_size |= b;
  861. csd_c_size <<= 2;
  862. break;
  863. case 8:
  864. csd_c_size |= b >> 6;
  865. ++csd_c_size;
  866. break;
  867. case 9:
  868. csd_c_size_mult = b & 0x03;
  869. csd_c_size_mult <<= 1;
  870. break;
  871. case 10:
  872. csd_c_size_mult |= b >> 7;
  873. info->capacity = (uint32_t) csd_c_size << (csd_c_size_mult + csd_read_bl_len + 2);
  874. break;
  875. }
  876. }
  877. }
  878. }
  879. unselect_card();
  880. return 1;
  881. }