00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include <string.h>
00019 #include <avr/io.h>
00020 #include "sd_mmc.h"
00021
00022 #include "uart.h"
00023 #include "spi.h"
00024
00050
00051
00052
00053 #define CMD_GO_IDLE_STATE 0x00
00054
00055 #define CMD_SEND_OP_COND 0x01
00056
00057 #define CMD_SEND_IF_COND 0x08
00058
00059 #define CMD_SEND_CSD 0x09
00060
00061 #define CMD_SEND_CID 0x0a
00062
00063 #define CMD_STOP_TRANSMISSION 0x0c
00064
00065 #define CMD_SEND_STATUS 0x0d
00066
00067 #define CMD_SET_BLOCKLEN 0x10
00068
00069 #define CMD_READ_SINGLE_BLOCK 0x11
00070
00071 #define CMD_READ_MULTIPLE_BLOCK 0x12
00072
00073 #define CMD_WRITE_SINGLE_BLOCK 0x18
00074
00075 #define CMD_WRITE_MULTIPLE_BLOCK 0x19
00076
00077 #define CMD_PROGRAM_CSD 0x1b
00078
00079 #define CMD_SET_WRITE_PROT 0x1c
00080
00081 #define CMD_CLR_WRITE_PROT 0x1d
00082
00083 #define CMD_SEND_WRITE_PROT 0x1e
00084
00085 #define CMD_TAG_SECTOR_START 0x20
00086
00087 #define CMD_TAG_SECTOR_END 0x21
00088
00089 #define CMD_UNTAG_SECTOR 0x22
00090
00091 #define CMD_TAG_ERASE_GROUP_START 0x23
00092
00093 #define CMD_TAG_ERASE_GROUP_END 0x24
00094
00095 #define CMD_UNTAG_ERASE_GROUP 0x25
00096
00097 #define CMD_ERASE 0x26
00098
00099 #define CMD_LOCK_UNLOCK 0x2a
00100
00101 #define CMD_READ_OCR 0x3a
00102
00103 #define CMD_CRC_ON_OFF 0x3b
00104
00105
00106
00107 #define R1_IDLE_STATE 0
00108 #define R1_ERASE_RESET 1
00109 #define R1_ILL_COMMAND 2
00110 #define R1_COM_CRC_ERR 3
00111 #define R1_ERASE_SEQ_ERR 4
00112 #define R1_ADDR_ERR 5
00113 #define R1_PARAM_ERR 6
00114
00115
00116 #define R2_CARD_LOCKED 0
00117 #define R2_WP_ERASE_SKIP 1
00118 #define R2_ERR 2
00119 #define R2_CARD_ERR 3
00120 #define R2_CARD_ECC_FAIL 4
00121 #define R2_WP_VIOLATION 5
00122 #define R2_INVAL_ERASE 6
00123 #define R2_OUT_OF_RANGE 7
00124 #define R2_CSD_OVERWRITE 7
00125 #define R2_IDLE_STATE (R1_IDLE_STATE + 8)
00126 #define R2_ERASE_RESET (R1_ERASE_RESET + 8)
00127 #define R2_ILL_COMMAND (R1_ILL_COMMAND + 8)
00128 #define R2_COM_CRC_ERR (R1_COM_CRC_ERR + 8)
00129 #define R2_ERASE_SEQ_ERR (R1_ERASE_SEQ_ERR + 8)
00130 #define R2_ADDR_ERR (R1_ADDR_ERR + 8)
00131 #define R2_PARAM_ERR (R1_PARAM_ERR + 8)
00132
00133 #define R3_OCR_MASK (0xffffffffUL)
00134 #define R3_IDLE_STATE (R1_IDLE_STATE + 32)
00135 #define R3_ERASE_RESET (R1_ERASE_RESET + 32)
00136 #define R3_ILL_COMMAND (R1_ILL_COMMAND + 32)
00137 #define R3_COM_CRC_ERR (R1_COM_CRC_ERR + 32)
00138 #define R3_ERASE_SEQ_ERR (R1_ERASE_SEQ_ERR + 32)
00139 #define R3_ADDR_ERR (R1_ADDR_ERR + 32)
00140 #define R3_PARAM_ERR (R1_PARAM_ERR + 32)
00141
00142 #define DR_STATUS_MASK 0x0e
00143 #define DR_STATUS_ACCEPTED 0x05
00144 #define DR_STATUS_CRC_ERR 0x0a
00145 #define DR_STATUS_WRITE_ERR 0x0c
00146
00147 #if !SD_RAW_SAVE_RAM
00148
00149
00150 static uint8_t raw_block[512];
00151
00152 static uint32_t raw_block_address;
00153 #if SD_RAW_WRITE_BUFFERING
00154
00155 static uint8_t raw_block_written;
00156 #endif
00157
00158 #endif
00159
00160
00161 static uint8_t sd_send_command_r1(uint8_t command, uint32_t arg);
00162 static uint16_t sd_send_command_r2(uint8_t command, uint32_t arg);
00163
00170 uint8_t sd_init()
00171 {
00172 uint8_t b;
00173
00174
00175 configure_pin_available();
00176 configure_pin_locked();
00177
00178
00179 configure_pin_mosi();
00180 configure_pin_sck();
00181 configure_pin_ss();
00182 configure_pin_miso();
00183
00184 DDRA |= (1 << DDA0);
00185
00186
00187 unselect_card();
00188
00189
00190 if(!sd_available())
00191 return 1;
00192
00193
00194
00195 b = spi_init();
00196
00197
00198 for(uint8_t i = 0; i < 10; ++i) {
00199
00200 spi_read_byte();
00201 }
00202
00203
00204 select_card();
00205
00206
00207 uint8_t response;
00208 for(uint16_t i = 0; ; ++i) {
00209 response = sd_send_command_r1(CMD_GO_IDLE_STATE, 0);
00210
00211 if(response == (1 << R1_IDLE_STATE))
00212 break;
00213 uart_putc('.');
00214 if(i == 0x1ff) {
00215
00216 unselect_card();
00217 return 1;
00218 }
00219 }
00220 uart_puts("Init request sent to card\r\n");
00221
00222
00223
00224 for(uint16_t i = 0; ; ++i) {
00225 response = sd_send_command_r1(CMD_SEND_OP_COND, 0);
00226
00227 if(!(response & (1 << R1_IDLE_STATE)))
00228 break;
00229 uart_putc('.');
00230
00231 if(i == 0x7fff) {
00232
00233 unselect_card();
00234 return 1;
00235 }
00236 }
00237 uart_puts("\r\nCard finished init\r\n");
00238
00239 for(uint16_t i = 0; ; ++i) {
00240 response = sd_send_command_r1(CMD_SET_BLOCKLEN, 512);
00241
00242
00243 if(!response)
00244 break;
00245 uart_putc('.');
00246
00247 if(i == 0x1ff) {
00248
00249 uart_puts("\n\rError setting block size\n\r");
00250 unselect_card();
00251 return 1;
00252 }
00253 }
00254 uart_puts("\n\rBlock size set.\n\r");
00255
00256
00257 unselect_card();
00258
00259
00260
00261
00262 b = spi_set_maxspeed();
00263
00264 #if !SD_RAW_SAVE_RAM
00265
00266 raw_block_address = 0xffffffff;
00267 #if SD_RAW_WRITE_BUFFERING
00268 raw_block_written = 1;
00269 #endif
00270
00271 if(!sd_read(0, raw_block, sizeof(raw_block)))
00272 return 1;
00273 #endif
00274
00275
00276 return 0;
00277 }
00278
00285 uint8_t sd_available()
00286 {
00287 return get_pin_available() == 0x00;
00288 }
00289
00296 uint8_t sd_locked()
00297 {
00298 return get_pin_locked() == 0x00;
00299 }
00300
00309 uint8_t sd_send_command_r1(uint8_t command, uint32_t arg)
00310 {
00311 uint8_t response;
00312
00313
00314 spi_read_byte();
00315
00316
00317 spi_write_byte(0x40 | command);
00318 spi_write_byte((arg >> 24) & 0xff);
00319 spi_write_byte((arg >> 16) & 0xff);
00320 spi_write_byte((arg >> 8) & 0xff);
00321 spi_write_byte((arg >> 0) & 0xff);
00322 spi_write_byte(command == CMD_GO_IDLE_STATE ? 0x95 : 0xff);
00323
00324
00325 for(uint8_t i = 0; i < 10; ++i)
00326 {
00327 response = spi_read_byte();
00328 if(response != 0xff)
00329 break;
00330 }
00331
00332 return response;
00333 }
00334
00343 uint16_t sd_send_command_r2(uint8_t command, uint32_t arg)
00344 {
00345 uint16_t response;
00346
00347
00348 spi_read_byte();
00349
00350
00351 spi_write_byte(0x40 | command);
00352 spi_write_byte((arg >> 24) & 0xff);
00353 spi_write_byte((arg >> 16) & 0xff);
00354 spi_write_byte((arg >> 8) & 0xff);
00355 spi_write_byte((arg >> 0) & 0xff);
00356 spi_write_byte(command == CMD_GO_IDLE_STATE ? 0x95 : 0xff);
00357
00358
00359 for(uint8_t i = 0; i < 10; ++i)
00360 {
00361 response = spi_read_byte();
00362 if(response != 0xff)
00363 break;
00364 }
00365 response <<= 8;
00366 response |= spi_read_byte();
00367
00368 return response;
00369 }
00370
00381 uint8_t sd_read(uint32_t offset, uint8_t* buffer, uint16_t length)
00382 {
00383 uint32_t block_address;
00384 uint16_t block_offset;
00385 uint16_t read_length;
00386 while(length > 0)
00387 {
00388
00389 block_address = offset & 0xfffffe00;
00390 block_offset = offset & 0x01ff;
00391 read_length = 512 - block_offset;
00392 if(read_length > length)
00393 read_length = length;
00394
00395 #if !SD_RAW_SAVE_RAM
00396
00397 if(block_address != raw_block_address)
00398 #endif
00399 {
00400 #if SD_RAW_WRITE_BUFFERING
00401 if(!sd_sync())
00402 return 0;
00403 #endif
00404
00405
00406 select_card();
00407
00408
00409 if(sd_send_command_r1(CMD_READ_SINGLE_BLOCK, block_address))
00410 {
00411 unselect_card();
00412 return 0;
00413 }
00414
00415
00416 while(spi_read_byte() != 0xfe);
00417
00418 #if SD_RAW_SAVE_RAM
00419
00420 uint16_t read_to = block_offset + read_length;
00421 for(uint16_t i = 0; i < 512; ++i)
00422 {
00423 uint8_t b = spi_read_byte();
00424 if(i >= block_offset && i < read_to)
00425 *buffer++ = b;
00426 }
00427 #else
00428
00429 uint8_t* cache = raw_block;
00430 for(uint16_t i = 0; i < 512; ++i)
00431 *cache++ = spi_read_byte();
00432 raw_block_address = block_address;
00433
00434 memcpy(buffer, raw_block + block_offset, read_length);
00435 buffer += read_length;
00436 #endif
00437
00438
00439 spi_read_byte();
00440 spi_read_byte();
00441
00442
00443 unselect_card();
00444
00445
00446 spi_read_byte();
00447 }
00448 #if !SD_RAW_SAVE_RAM
00449 else
00450 {
00451
00452 memcpy(buffer, raw_block + block_offset, read_length);
00453 buffer += read_length;
00454 }
00455 #endif
00456
00457 length -= read_length;
00458 offset += read_length;
00459 }
00460
00461 return 1;
00462 }
00463
00488 uint8_t sd_read_interval(uint32_t offset, uint8_t* buffer, uint16_t interval, uint16_t length, sd_raw_read_interval_handler_t callback, void* p)
00489 {
00490 if(!buffer || interval == 0 || length < interval || !callback)
00491 return 0;
00492
00493 #if !SD_RAW_SAVE_RAM
00494 while(length >= interval)
00495 {
00496
00497
00498
00499 if(!sd_read(offset, buffer, interval))
00500 return 0;
00501 if(!callback(buffer, offset, p))
00502 break;
00503 offset += interval;
00504 length -= interval;
00505 }
00506
00507 return 1;
00508 #else
00509
00510 select_card();
00511
00512 uint16_t block_offset;
00513 uint16_t read_length;
00514 uint8_t* buffer_cur;
00515 uint8_t finished = 0;
00516 do
00517 {
00518
00519 block_offset = offset & 0x01ff;
00520 read_length = 512 - block_offset;
00521
00522
00523 if(sd_send_command_r1(CMD_READ_SINGLE_BLOCK, offset & 0xfffffe00))
00524 {
00525 unselect_card();
00526 return 0;
00527 }
00528
00529
00530 while(spi_read_byte() != 0xfe);
00531
00532
00533 for(uint16_t i = 0; i < block_offset; ++i)
00534 spi_read_byte();
00535
00536
00537 do
00538 {
00539 if(read_length < interval || length < interval)
00540 break;
00541
00542 buffer_cur = buffer;
00543 for(uint16_t i = 0; i < interval; ++i)
00544 *buffer_cur++ = spi_read_byte();
00545
00546 if(!callback(buffer, offset + (512 - read_length), p))
00547 {
00548 finished = 1;
00549 break;
00550 }
00551
00552 read_length -= interval;
00553 length -= interval;
00554
00555 } while(read_length > 0 && length > 0);
00556
00557
00558 while(read_length-- > 0)
00559 spi_read_byte();
00560
00561
00562 spi_read_byte();
00563 spi_read_byte();
00564
00565 if(length < interval)
00566 break;
00567
00568 offset = (offset & 0xfffffe00) + 512;
00569
00570 } while(!finished);
00571
00572
00573 unselect_card();
00574
00575
00576 spi_read_byte();
00577
00578 return 1;
00579 #endif
00580 }
00581
00596 uint8_t sd_write(uint32_t offset, const uint8_t* buffer, uint16_t length)
00597 {
00598 #if SD_RAW_WRITE_SUPPORT
00599
00600 if(get_pin_locked())
00601 return 0;
00602
00603 uint32_t block_address;
00604 uint16_t block_offset;
00605 uint16_t write_length;
00606 while(length > 0)
00607 {
00608
00609 block_address = offset & 0xfffffe00;
00610 block_offset = offset & 0x01ff;
00611 write_length = 512 - block_offset;
00612 if(write_length > length)
00613 write_length = length;
00614
00615
00616
00617
00618 if(block_address != raw_block_address)
00619 {
00620 #if SD_RAW_WRITE_BUFFERING
00621 if(!sd_sync())
00622 return 0;
00623 #endif
00624
00625 if(block_offset || write_length < 512)
00626 {
00627 if(!sd_read(block_address, raw_block, sizeof(raw_block)))
00628 return 0;
00629 }
00630 raw_block_address = block_address;
00631 }
00632
00633 if(buffer != raw_block)
00634 {
00635 memcpy(raw_block + block_offset, buffer, write_length);
00636
00637 #if SD_RAW_WRITE_BUFFERING
00638 raw_block_written = 0;
00639
00640 if(length == write_length)
00641 return 1;
00642 #endif
00643 }
00644
00645
00646 select_card();
00647
00648
00649 if(sd_send_command_r1(CMD_WRITE_SINGLE_BLOCK, block_address))
00650 {
00651 unselect_card();
00652 return 0;
00653 }
00654
00655
00656 spi_write_byte(0xfe);
00657
00658
00659 uint8_t* cache = raw_block;
00660 for(uint16_t i = 0; i < 512; ++i)
00661 spi_write_byte(*cache++);
00662
00663
00664 spi_write_byte(0xff);
00665 spi_write_byte(0xff);
00666
00667
00668 while(spi_read_byte() != 0xff);
00669 spi_read_byte();
00670
00671
00672 unselect_card();
00673
00674 buffer += write_length;
00675 offset += write_length;
00676 length -= write_length;
00677
00678 #if SD_RAW_WRITE_BUFFERING
00679 raw_block_written = 1;
00680 #endif
00681 }
00682
00683 return 1;
00684 #else
00685 return 0;
00686 #endif
00687 }
00688
00707 uint8_t sd_write_interval(uint32_t offset, uint8_t* buffer, uint16_t length, sd_raw_write_interval_handler_t callback, void* p)
00708 {
00709 #if SD_RAW_WRITE_SUPPORT
00710
00711 #if SD_RAW_SAVE_RAM
00712 #error "SD_RAW_WRITE_SUPPORT is not supported together with SD_RAW_SAVE_RAM"
00713 #endif
00714
00715 if(!buffer || !callback)
00716 return 0;
00717
00718 uint8_t endless = (length == 0);
00719 while(endless || length > 0)
00720 {
00721 uint16_t bytes_to_write = callback(buffer, offset, p);
00722 if(!bytes_to_write)
00723 break;
00724 if(!endless && bytes_to_write > length)
00725 return 0;
00726
00727
00728
00729
00730 if(!sd_write(offset, buffer, bytes_to_write))
00731 return 0;
00732
00733 offset += bytes_to_write;
00734 length -= bytes_to_write;
00735 }
00736
00737 return 1;
00738
00739 #else
00740 return 0;
00741 #endif
00742 }
00743
00756 uint8_t sd_sync()
00757 {
00758 #if SD_RAW_WRITE_SUPPORT
00759 #if SD_RAW_WRITE_BUFFERING
00760 if(raw_block_written)
00761 return 1;
00762 if(!sd_write(raw_block_address, raw_block, sizeof(raw_block)))
00763 return 0;
00764 raw_block_written = 1;
00765 #endif
00766 return 1;
00767 #else
00768 return 0;
00769 #endif
00770 }
00771
00787 uint8_t sd_get_info(struct sd_raw_info* info)
00788 {
00789 if(!info || !sd_available())
00790 return 0;
00791
00792 memset(info, 0, sizeof(*info));
00793
00794 select_card();
00795
00796
00797 if(sd_send_command_r1(CMD_SEND_CID, 0))
00798 {
00799 unselect_card();
00800 return 0;
00801 }
00802 while(spi_read_byte() != 0xfe);
00803 for(uint8_t i = 0; i < 18; ++i)
00804 {
00805 uint8_t b = spi_read_byte();
00806
00807 switch(i)
00808 {
00809 case 0:
00810 info->manufacturer = b;
00811 break;
00812 case 1:
00813 case 2:
00814 info->oem[i - 1] = b;
00815 break;
00816 case 3:
00817 case 4:
00818 case 5:
00819 case 6:
00820 case 7:
00821 info->product[i - 3] = b;
00822 break;
00823 case 8:
00824 info->revision = b;
00825 break;
00826 case 9:
00827 case 10:
00828 case 11:
00829 case 12:
00830 info->serial |= (uint32_t) b << ((12 - i) * 8);
00831 break;
00832 case 13:
00833 info->manufacturing_year = b << 4;
00834 break;
00835 case 14:
00836 info->manufacturing_year |= b >> 4;
00837 info->manufacturing_month = b & 0x0f;
00838 break;
00839 }
00840 }
00841
00842
00843 uint8_t csd_read_bl_len = 0;
00844 uint8_t csd_c_size_mult = 0;
00845 uint16_t csd_c_size = 0;
00846 if(sd_send_command_r1(CMD_SEND_CSD, 0))
00847 {
00848 unselect_card();
00849 return 0;
00850 }
00851 while(spi_read_byte() != 0xfe);
00852 for(uint8_t i = 0; i < 18; ++i)
00853 {
00854 uint8_t b = spi_read_byte();
00855
00856 switch(i)
00857 {
00858 case 5:
00859 csd_read_bl_len = b & 0x0f;
00860 break;
00861 case 6:
00862 csd_c_size = (uint16_t) (b & 0x03) << 8;
00863 break;
00864 case 7:
00865 csd_c_size |= b;
00866 csd_c_size <<= 2;
00867 break;
00868 case 8:
00869 csd_c_size |= b >> 6;
00870 ++csd_c_size;
00871 break;
00872 case 9:
00873 csd_c_size_mult = (b & 0x03) << 1;
00874 break;
00875 case 10:
00876 csd_c_size_mult |= b >> 7;
00877
00878 info->capacity = (uint32_t) csd_c_size << (csd_c_size_mult + csd_read_bl_len + 2);
00879
00880 break;
00881 case 14:
00882 if(b & 0x40)
00883 info->flag_copy = 1;
00884 if(b & 0x20)
00885 info->flag_write_protect = 1;
00886 if(b & 0x10)
00887 info->flag_write_protect_temp = 1;
00888 info->format = (b & 0x0c) >> 2;
00889 break;
00890 }
00891 }
00892
00893 unselect_card();
00894
00895 return 1;
00896 }
00897