3 * Copyright (c) 2006-2012 by Roland Riegel <feedback@roland-riegel.de>
5 * This file is free software; you can redistribute it and/or modify
6 * it under the terms of either the GNU General Public License version 2
7 * or the GNU Lesser General Public License version 2.1, both as
8 * published by the Free Software Foundation.
16 * \addtogroup sd_raw MMC/SD/SDHC card raw access
18 * This module implements read and write access to MMC, SD
19 * and SDHC cards. It serves as a low-level driver for the
20 * higher level modules such as partition and file system
27 * MMC/SD/SDHC raw access implementation (license: GPLv2 or LGPLv2.1)
29 * \author Roland Riegel
33 * \addtogroup sd_raw_config MMC/SD configuration
34 * Preprocessor defines to configure the MMC/SD support.
41 /* commands available in SPI mode */
43 /* CMD0: response R1 */
44 #define CMD_GO_IDLE_STATE 0x00
45 /* CMD1: response R1 */
46 #define CMD_SEND_OP_COND 0x01
47 /* CMD8: response R7 */
48 #define CMD_SEND_IF_COND 0x08
49 /* CMD9: response R1 */
50 #define CMD_SEND_CSD 0x09
51 /* CMD10: response R1 */
52 #define CMD_SEND_CID 0x0a
53 /* CMD12: response R1 */
54 #define CMD_STOP_TRANSMISSION 0x0c
55 /* CMD13: response R2 */
56 #define CMD_SEND_STATUS 0x0d
57 /* CMD16: arg0[31:0]: block length, response R1 */
58 #define CMD_SET_BLOCKLEN 0x10
59 /* CMD17: arg0[31:0]: data address, response R1 */
60 #define CMD_READ_SINGLE_BLOCK 0x11
61 /* CMD18: arg0[31:0]: data address, response R1 */
62 #define CMD_READ_MULTIPLE_BLOCK 0x12
63 /* CMD24: arg0[31:0]: data address, response R1 */
64 #define CMD_WRITE_SINGLE_BLOCK 0x18
65 /* CMD25: arg0[31:0]: data address, response R1 */
66 #define CMD_WRITE_MULTIPLE_BLOCK 0x19
67 /* CMD27: response R1 */
68 #define CMD_PROGRAM_CSD 0x1b
69 /* CMD28: arg0[31:0]: data address, response R1b */
70 #define CMD_SET_WRITE_PROT 0x1c
71 /* CMD29: arg0[31:0]: data address, response R1b */
72 #define CMD_CLR_WRITE_PROT 0x1d
73 /* CMD30: arg0[31:0]: write protect data address, response R1 */
74 #define CMD_SEND_WRITE_PROT 0x1e
75 /* CMD32: arg0[31:0]: data address, response R1 */
76 #define CMD_TAG_SECTOR_START 0x20
77 /* CMD33: arg0[31:0]: data address, response R1 */
78 #define CMD_TAG_SECTOR_END 0x21
79 /* CMD34: arg0[31:0]: data address, response R1 */
80 #define CMD_UNTAG_SECTOR 0x22
81 /* CMD35: arg0[31:0]: data address, response R1 */
82 #define CMD_TAG_ERASE_GROUP_START 0x23
83 /* CMD36: arg0[31:0]: data address, response R1 */
84 #define CMD_TAG_ERASE_GROUP_END 0x24
85 /* CMD37: arg0[31:0]: data address, response R1 */
86 #define CMD_UNTAG_ERASE_GROUP 0x25
87 /* CMD38: arg0[31:0]: stuff bits, response R1b */
88 #define CMD_ERASE 0x26
89 /* ACMD41: arg0[31:0]: OCR contents, response R1 */
90 #define CMD_SD_SEND_OP_COND 0x29
91 /* CMD42: arg0[31:0]: stuff bits, response R1b */
92 #define CMD_LOCK_UNLOCK 0x2a
93 /* CMD55: arg0[31:0]: stuff bits, response R1 */
95 /* CMD58: arg0[31:0]: stuff bits, response R3 */
96 #define CMD_READ_OCR 0x3a
97 /* CMD59: arg0[31:1]: stuff bits, arg0[0:0]: crc option, response R1 */
98 #define CMD_CRC_ON_OFF 0x3b
100 /* command responses */
101 /* R1: size 1 byte */
102 #define R1_IDLE_STATE 0
103 #define R1_ERASE_RESET 1
104 #define R1_ILL_COMMAND 2
105 #define R1_COM_CRC_ERR 3
106 #define R1_ERASE_SEQ_ERR 4
107 #define R1_ADDR_ERR 5
108 #define R1_PARAM_ERR 6
109 /* R1b: equals R1, additional busy bytes */
110 /* R2: size 2 bytes */
111 #define R2_CARD_LOCKED 0
112 #define R2_WP_ERASE_SKIP 1
114 #define R2_CARD_ERR 3
115 #define R2_CARD_ECC_FAIL 4
116 #define R2_WP_VIOLATION 5
117 #define R2_INVAL_ERASE 6
118 #define R2_OUT_OF_RANGE 7
119 #define R2_CSD_OVERWRITE 7
120 #define R2_IDLE_STATE (R1_IDLE_STATE + 8)
121 #define R2_ERASE_RESET (R1_ERASE_RESET + 8)
122 #define R2_ILL_COMMAND (R1_ILL_COMMAND + 8)
123 #define R2_COM_CRC_ERR (R1_COM_CRC_ERR + 8)
124 #define R2_ERASE_SEQ_ERR (R1_ERASE_SEQ_ERR + 8)
125 #define R2_ADDR_ERR (R1_ADDR_ERR + 8)
126 #define R2_PARAM_ERR (R1_PARAM_ERR + 8)
127 /* R3: size 5 bytes */
128 #define R3_OCR_MASK (0xffffffffUL)
129 #define R3_IDLE_STATE (R1_IDLE_STATE + 32)
130 #define R3_ERASE_RESET (R1_ERASE_RESET + 32)
131 #define R3_ILL_COMMAND (R1_ILL_COMMAND + 32)
132 #define R3_COM_CRC_ERR (R1_COM_CRC_ERR + 32)
133 #define R3_ERASE_SEQ_ERR (R1_ERASE_SEQ_ERR + 32)
134 #define R3_ADDR_ERR (R1_ADDR_ERR + 32)
135 #define R3_PARAM_ERR (R1_PARAM_ERR + 32)
136 /* Data Response: size 1 byte */
137 #define DR_STATUS_MASK 0x0e
138 #define DR_STATUS_ACCEPTED 0x05
139 #define DR_STATUS_CRC_ERR 0x0a
140 #define DR_STATUS_WRITE_ERR 0x0c
142 /* status bits for card types */
143 #define SD_RAW_SPEC_1 0
144 #define SD_RAW_SPEC_2 1
145 #define SD_RAW_SPEC_SDHC 2
148 /* static data buffer for acceleration */
149 static uint8_t raw_block[512];
150 /* offset where the data within raw_block lies on the card */
151 static offset_t raw_block_address;
152 #if SD_RAW_WRITE_BUFFERING
153 /* flag to remember if raw_block was written to the card */
154 static uint8_t raw_block_written;
158 /* card type state */
159 static uint8_t sd_raw_card_type;
161 /* private helper functions */
162 static void sd_raw_send_byte(uint8_t b);
163 static uint8_t sd_raw_rec_byte();
164 static uint8_t sd_raw_send_command(uint8_t command, uint32_t arg);
168 * Initializes memory card communication.
170 * \returns 0 on failure, 1 on success.
172 uint8_t sd_raw_init()
174 /* enable inputs for reading card status */
175 configure_pin_available();
176 configure_pin_locked();
178 /* enable outputs for MOSI, SCK, SS, input for MISO */
179 configure_pin_mosi();
182 configure_pin_miso();
186 /* initialize SPI with lowest frequency; max. 400kHz during identification mode of card */
187 SPCR = (0 << SPIE) | /* SPI Interrupt Enable */
188 (1 << SPE) | /* SPI Enable */
189 (0 << DORD) | /* Data Order: MSB first */
190 (1 << MSTR) | /* Master mode */
191 (0 << CPOL) | /* Clock Polarity: SCK low when idle */
192 (0 << CPHA) | /* Clock Phase: sample on rising SCK edge */
193 (1 << SPR1) | /* Clock Frequency: f_OSC / 128 */
195 SPSR &= ~(1 << SPI2X); /* No doubled clock frequency */
197 /* initialization procedure */
198 sd_raw_card_type = 0;
200 if(!sd_raw_available())
203 /* card needs 74 cycles minimum to start up */
204 for(uint8_t i = 0; i < 10; ++i)
206 /* wait 8 clock cycles */
215 for(uint16_t i = 0; ; ++i)
217 response = sd_raw_send_command(CMD_GO_IDLE_STATE, 0);
218 if(response == (1 << R1_IDLE_STATE))
229 /* check for version of SD card specification */
230 response = sd_raw_send_command(CMD_SEND_IF_COND, 0x100 /* 2.7V - 3.6V */ | 0xaa /* test pattern */);
231 if((response & (1 << R1_ILL_COMMAND)) == 0)
235 if((sd_raw_rec_byte() & 0x01) == 0)
236 return 0; /* card operation voltage range doesn't match */
237 if(sd_raw_rec_byte() != 0xaa)
238 return 0; /* wrong test pattern */
240 /* card conforms to SD 2 card specification */
241 sd_raw_card_type |= (1 << SD_RAW_SPEC_2);
246 /* determine SD/MMC card type */
247 sd_raw_send_command(CMD_APP, 0);
248 response = sd_raw_send_command(CMD_SD_SEND_OP_COND, 0);
249 if((response & (1 << R1_ILL_COMMAND)) == 0)
251 /* card conforms to SD 1 card specification */
252 sd_raw_card_type |= (1 << SD_RAW_SPEC_1);
260 /* wait for card to get ready */
261 for(uint16_t i = 0; ; ++i)
263 if(sd_raw_card_type & ((1 << SD_RAW_SPEC_1) | (1 << SD_RAW_SPEC_2)))
267 if(sd_raw_card_type & (1 << SD_RAW_SPEC_2))
270 sd_raw_send_command(CMD_APP, 0);
271 response = sd_raw_send_command(CMD_SD_SEND_OP_COND, arg);
275 response = sd_raw_send_command(CMD_SEND_OP_COND, 0);
278 if((response & (1 << R1_IDLE_STATE)) == 0)
289 if(sd_raw_card_type & (1 << SD_RAW_SPEC_2))
291 if(sd_raw_send_command(CMD_READ_OCR, 0))
297 if(sd_raw_rec_byte() & 0x40)
298 sd_raw_card_type |= (1 << SD_RAW_SPEC_SDHC);
306 /* set block size to 512 bytes */
307 if(sd_raw_send_command(CMD_SET_BLOCKLEN, 512))
316 /* switch to highest SPI frequency possible */
317 SPCR &= ~((1 << SPR1) | (1 << SPR0)); /* Clock Frequency: f_OSC / 4 */
318 SPSR |= (1 << SPI2X); /* Doubled Clock Frequency: f_OSC / 2 */
321 /* the first block is likely to be accessed first, so precache it here */
322 raw_block_address = (offset_t) -1;
323 #if SD_RAW_WRITE_BUFFERING
324 raw_block_written = 1;
326 if(!sd_raw_read(0, raw_block, sizeof(raw_block)))
335 * Checks wether a memory card is located in the slot.
337 * \returns 1 if the card is available, 0 if it is not.
339 uint8_t sd_raw_available()
341 return get_pin_available() == 0x00;
346 * Checks wether the memory card is locked for write access.
348 * \returns 1 if the card is locked, 0 if it is not.
350 uint8_t sd_raw_locked()
352 return get_pin_locked() == 0x00;
357 * Sends a raw byte to the memory card.
359 * \param[in] b The byte to sent.
360 * \see sd_raw_rec_byte
362 void sd_raw_send_byte(uint8_t b)
365 /* wait for byte to be shifted out */
366 while(!(SPSR & (1 << SPIF)));
367 SPSR &= ~(1 << SPIF);
372 * Receives a raw byte from the memory card.
374 * \returns The byte which should be read.
375 * \see sd_raw_send_byte
377 uint8_t sd_raw_rec_byte()
379 /* send dummy data for receiving some */
381 while(!(SPSR & (1 << SPIF)));
382 SPSR &= ~(1 << SPIF);
389 * Send a command to the memory card which responses with a R1 response (and possibly others).
391 * \param[in] command The command to send.
392 * \param[in] arg The argument for command.
393 * \returns The command answer.
395 uint8_t sd_raw_send_command(uint8_t command, uint32_t arg)
399 /* wait some clock cycles */
402 /* send command via SPI */
403 sd_raw_send_byte(0x40 | command);
404 sd_raw_send_byte((arg >> 24) & 0xff);
405 sd_raw_send_byte((arg >> 16) & 0xff);
406 sd_raw_send_byte((arg >> 8) & 0xff);
407 sd_raw_send_byte((arg >> 0) & 0xff);
410 case CMD_GO_IDLE_STATE:
411 sd_raw_send_byte(0x95);
413 case CMD_SEND_IF_COND:
414 sd_raw_send_byte(0x87);
417 sd_raw_send_byte(0xff);
421 /* receive response */
422 for(uint8_t i = 0; i < 10; ++i)
424 response = sd_raw_rec_byte();
434 * Reads raw data from the card.
436 * \param[in] offset The offset from which to read.
437 * \param[out] buffer The buffer into which to write the data.
438 * \param[in] length The number of bytes to read.
439 * \returns 0 on failure, 1 on success.
440 * \see sd_raw_read_interval, sd_raw_write, sd_raw_write_interval
442 uint8_t sd_raw_read(offset_t offset, uint8_t* buffer, uintptr_t length)
444 offset_t block_address;
445 uint16_t block_offset;
446 uint16_t read_length;
449 /* determine byte count to read at once */
450 block_offset = offset & 0x01ff;
451 block_address = offset - block_offset;
452 read_length = 512 - block_offset; /* read up to block border */
453 if(read_length > length)
454 read_length = length;
457 /* check if the requested data is cached */
458 if(block_address != raw_block_address)
461 #if SD_RAW_WRITE_BUFFERING
469 /* send single block request */
471 if(sd_raw_send_command(CMD_READ_SINGLE_BLOCK, (sd_raw_card_type & (1 << SD_RAW_SPEC_SDHC) ? block_address / 512 : block_address)))
473 if(sd_raw_send_command(CMD_READ_SINGLE_BLOCK, block_address))
480 /* wait for data block (start byte 0xfe) */
481 while(sd_raw_rec_byte() != 0xfe);
484 /* read byte block */
485 uint16_t read_to = block_offset + read_length;
486 for(uint16_t i = 0; i < 512; ++i)
488 uint8_t b = sd_raw_rec_byte();
489 if(i >= block_offset && i < read_to)
493 /* read byte block */
494 uint8_t* cache = raw_block;
495 for(uint16_t i = 0; i < 512; ++i)
496 *cache++ = sd_raw_rec_byte();
497 raw_block_address = block_address;
499 memcpy(buffer, raw_block + block_offset, read_length);
500 buffer += read_length;
510 /* let card some time to finish */
516 /* use cached data */
517 memcpy(buffer, raw_block + block_offset, read_length);
518 buffer += read_length;
522 length -= read_length;
523 offset += read_length;
531 * Continuously reads units of \c interval bytes and calls a callback function.
533 * This function starts reading at the specified offset. Every \c interval bytes,
534 * it calls the callback function with the associated data buffer.
536 * By returning zero, the callback may stop reading.
538 * \note Within the callback function, you can not start another read or
540 * \note This function only works if the following conditions are met:
541 * - (offset - (offset % 512)) % interval == 0
542 * - length % interval == 0
544 * \param[in] offset Offset from which to start reading.
545 * \param[in] buffer Pointer to a buffer which is at least interval bytes in size.
546 * \param[in] interval Number of bytes to read before calling the callback function.
547 * \param[in] length Number of bytes to read altogether.
548 * \param[in] callback The function to call every interval bytes.
549 * \param[in] p An opaque pointer directly passed to the callback function.
550 * \returns 0 on failure, 1 on success
551 * \see sd_raw_write_interval, sd_raw_read, sd_raw_write
553 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)
555 if(!buffer || interval == 0 || length < interval || !callback)
559 while(length >= interval)
561 /* as reading is now buffered, we directly
562 * hand over the request to sd_raw_read()
564 if(!sd_raw_read(offset, buffer, interval))
566 if(!callback(buffer, offset, p))
577 uint16_t block_offset;
578 uint16_t read_length;
580 uint8_t finished = 0;
583 /* determine byte count to read at once */
584 block_offset = offset & 0x01ff;
585 read_length = 512 - block_offset;
587 /* send single block request */
589 if(sd_raw_send_command(CMD_READ_SINGLE_BLOCK, (sd_raw_card_type & (1 << SD_RAW_SPEC_SDHC) ? offset / 512 : offset - block_offset)))
591 if(sd_raw_send_command(CMD_READ_SINGLE_BLOCK, offset - block_offset))
598 /* wait for data block (start byte 0xfe) */
599 while(sd_raw_rec_byte() != 0xfe);
601 /* read up to the data of interest */
602 for(uint16_t i = 0; i < block_offset; ++i)
605 /* read interval bytes of data and execute the callback */
608 if(read_length < interval || length < interval)
612 for(uint16_t i = 0; i < interval; ++i)
613 *buffer_cur++ = sd_raw_rec_byte();
615 if(!callback(buffer, offset + (512 - read_length), p))
621 read_length -= interval;
624 } while(read_length > 0 && length > 0);
626 /* read rest of data block */
627 while(read_length-- > 0)
634 if(length < interval)
637 offset = offset - block_offset + 512;
644 /* let card some time to finish */
651 #if DOXYGEN || SD_RAW_WRITE_SUPPORT
654 * Writes raw data to the card.
656 * \note If write buffering is enabled, you might have to
657 * call sd_raw_sync() before disconnecting the card
658 * to ensure all remaining data has been written.
660 * \param[in] offset The offset where to start writing.
661 * \param[in] buffer The buffer containing the data to be written.
662 * \param[in] length The number of bytes to write.
663 * \returns 0 on failure, 1 on success.
664 * \see sd_raw_write_interval, sd_raw_read, sd_raw_read_interval
666 uint8_t sd_raw_write(offset_t offset, const uint8_t* buffer, uintptr_t length)
671 offset_t block_address;
672 uint16_t block_offset;
673 uint16_t write_length;
676 /* determine byte count to write at once */
677 block_offset = offset & 0x01ff;
678 block_address = offset - block_offset;
679 write_length = 512 - block_offset; /* write up to block border */
680 if(write_length > length)
681 write_length = length;
683 /* Merge the data to write with the content of the block.
684 * Use the cached block if available.
686 if(block_address != raw_block_address)
688 #if SD_RAW_WRITE_BUFFERING
693 if(block_offset || write_length < 512)
695 if(!sd_raw_read(block_address, raw_block, sizeof(raw_block)))
698 raw_block_address = block_address;
701 if(buffer != raw_block)
703 memcpy(raw_block + block_offset, buffer, write_length);
705 #if SD_RAW_WRITE_BUFFERING
706 raw_block_written = 0;
708 if(length == write_length)
716 /* send single block request */
718 if(sd_raw_send_command(CMD_WRITE_SINGLE_BLOCK, (sd_raw_card_type & (1 << SD_RAW_SPEC_SDHC) ? block_address / 512 : block_address)))
720 if(sd_raw_send_command(CMD_WRITE_SINGLE_BLOCK, block_address))
727 /* send start byte */
728 sd_raw_send_byte(0xfe);
730 /* write byte block */
731 uint8_t* cache = raw_block;
732 for(uint16_t i = 0; i < 512; ++i)
733 sd_raw_send_byte(*cache++);
735 /* write dummy crc16 */
736 sd_raw_send_byte(0xff);
737 sd_raw_send_byte(0xff);
739 /* wait while card is busy */
740 while(sd_raw_rec_byte() != 0xff);
746 buffer += write_length;
747 offset += write_length;
748 length -= write_length;
750 #if SD_RAW_WRITE_BUFFERING
751 raw_block_written = 1;
759 #if DOXYGEN || SD_RAW_WRITE_SUPPORT
762 * Writes a continuous data stream obtained from a callback function.
764 * This function starts writing at the specified offset. To obtain the
765 * next bytes to write, it calls the callback function. The callback fills the
766 * provided data buffer and returns the number of bytes it has put into the buffer.
768 * By returning zero, the callback may stop writing.
770 * \param[in] offset Offset where to start writing.
771 * \param[in] buffer Pointer to a buffer which is used for the callback function.
772 * \param[in] length Number of bytes to write in total. May be zero for endless writes.
773 * \param[in] callback The function used to obtain the bytes to write.
774 * \param[in] p An opaque pointer directly passed to the callback function.
775 * \returns 0 on failure, 1 on success
776 * \see sd_raw_read_interval, sd_raw_write, sd_raw_read
778 uint8_t sd_raw_write_interval(offset_t offset, uint8_t* buffer, uintptr_t length, sd_raw_write_interval_handler_t callback, void* p)
781 #error "SD_RAW_WRITE_SUPPORT is not supported together with SD_RAW_SAVE_RAM"
784 if(!buffer || !callback)
787 uint8_t endless = (length == 0);
788 while(endless || length > 0)
790 uint16_t bytes_to_write = callback(buffer, offset, p);
793 if(!endless && bytes_to_write > length)
796 /* as writing is always buffered, we directly
797 * hand over the request to sd_raw_write()
799 if(!sd_raw_write(offset, buffer, bytes_to_write))
802 offset += bytes_to_write;
803 length -= bytes_to_write;
810 #if DOXYGEN || SD_RAW_WRITE_SUPPORT
813 * Writes the write buffer's content to the card.
815 * \note When write buffering is enabled, you should
816 * call this function before disconnecting the
817 * card to ensure all remaining data has been
820 * \returns 0 on failure, 1 on success.
823 uint8_t sd_raw_sync()
825 #if SD_RAW_WRITE_BUFFERING
826 if(raw_block_written)
828 if(!sd_raw_write(raw_block_address, raw_block, sizeof(raw_block)))
830 raw_block_written = 1;
838 * Reads informational data from the card.
840 * This function reads and returns the card's registers
841 * containing manufacturing and status information.
843 * \note: The information retrieved by this function is
844 * not required in any way to operate on the card,
845 * but it might be nice to display some of the data
848 * \param[in] info A pointer to the structure into which to save the information.
849 * \returns 0 on failure, 1 on success.
851 uint8_t sd_raw_get_info(struct sd_raw_info* info)
853 if(!info || !sd_raw_available())
856 memset(info, 0, sizeof(*info));
860 /* read cid register */
861 if(sd_raw_send_command(CMD_SEND_CID, 0))
866 while(sd_raw_rec_byte() != 0xfe);
867 for(uint8_t i = 0; i < 18; ++i)
869 uint8_t b = sd_raw_rec_byte();
874 info->manufacturer = b;
878 info->oem[i - 1] = b;
885 info->product[i - 3] = b;
894 info->serial |= (uint32_t) b << ((12 - i) * 8);
897 info->manufacturing_year = b << 4;
900 info->manufacturing_year |= b >> 4;
901 info->manufacturing_month = b & 0x0f;
906 /* read csd register */
907 uint8_t csd_read_bl_len = 0;
908 uint8_t csd_c_size_mult = 0;
910 uint16_t csd_c_size = 0;
912 uint32_t csd_c_size = 0;
914 uint8_t csd_structure = 0;
915 if(sd_raw_send_command(CMD_SEND_CSD, 0))
920 while(sd_raw_rec_byte() != 0xfe);
921 for(uint8_t i = 0; i < 18; ++i)
923 uint8_t b = sd_raw_rec_byte();
927 csd_structure = b >> 6;
934 info->flag_write_protect = 1;
936 info->flag_write_protect_temp = 1;
937 info->format = (b & 0x0c) >> 2;
942 if(csd_structure == 0x01)
957 info->capacity = (offset_t) csd_c_size * 512 * 1024;
960 else if(csd_structure == 0x00)
966 csd_read_bl_len = b & 0x0f;
969 csd_c_size = b & 0x03;
977 csd_c_size |= b >> 6;
981 csd_c_size_mult = b & 0x03;
982 csd_c_size_mult <<= 1;
985 csd_c_size_mult |= b >> 7;
987 info->capacity = (uint32_t) csd_c_size << (csd_c_size_mult + csd_read_bl_len + 2);