00001
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046 #include "config.h"
00047 #include "conf_sdmmc.h"
00048 #include "lib_mcu/usb/usb_drv.h"
00049 #include "lib_mcu/spi/spi_drv.h"
00050 #include "mmc_sd.h"
00051
00052
00053
00054
00055 static U32 gl_ptr_mem;
00056
00057 bit mmc_sd_init_done = FALSE;
00058 U8 r1;
00059 U16 r2;
00060
00061 U8 csd[16];
00062 volatile U32 capacity;
00063 volatile U32 mmc_sd_last_block_address;
00064 U16 erase_group_size;
00065 U8 card_type;
00066
00067
00068 #if (MMC_SD_RAM == ENABLED)
00069 U8 data_mem[513];
00070 #endif
00071 #if (MMC_SD_READ_CID == ENABLED)
00072 U8 cid[16];
00073 #endif
00074
00075
00076
00077
00088 void mmc_sd_spi_init(void)
00089 {
00090 Spi_select_master();
00091 Spi_set_mode(SPI_MODE_0);
00092 Spi_init_bus();
00093 Spi_set_rate(SPI_RATE_0);
00094 Spi_disable_ss();
00095 Spi_enable();
00096 }
00097
00098
00110 bit mmc_sd_init (void)
00111 {
00112 U16 retry;
00113
00114
00115 mmc_sd_spi_init();
00116
00117
00118 mmc_sd_init_done = FALSE;
00119 card_type = MMC_CARD;
00120 retry = 0;
00121 do
00122 {
00123
00124 r1 = mmc_sd_send_command(MMC_GO_IDLE_STATE, 0);
00125 Spi_write_data(0xFF);
00126
00127 retry++;
00128 if(retry > 100)
00129 return KO;
00130 }
00131 while(r1 != 0x01);
00132
00133
00134
00135 r1 = mmc_sd_send_command(SD_APP_CMD55,0);
00136 Spi_write_data(0xFF);
00137
00138 r1 = mmc_sd_send_command(SD_SEND_OP_COND_ACMD, 0);
00139 Spi_write_data(0xFF);
00140
00141 if ((r1&0xFE) == 0)
00142 {
00143 card_type = SD_CARD;
00144 }
00145 else
00146 {
00147 card_type = MMC_CARD;
00148
00149 retry = 0;
00150 do
00151 {
00152
00153 r1 = mmc_sd_send_command(MMC_GO_IDLE_STATE, 0);
00154 Spi_write_data(0xFF);
00155
00156 retry++;
00157 if(retry > 100)
00158 return KO;
00159 }
00160 while(r1 != 0x01);
00161 }
00162
00163
00164
00165 retry = 0;
00166 do
00167 {
00168
00169 r1 = mmc_sd_send_command(MMC_SEND_OP_COND, 0);
00170 Spi_write_data(0xFF);
00171
00172 retry++;
00173 if(retry == 50000)
00174 return KO;
00175 }
00176 while (r1);
00177
00178
00179 r1 = mmc_sd_send_command(MMC_CRC_ON_OFF, 0);
00180 Spi_write_data(0xFF);
00181
00182
00183 r1 = mmc_sd_send_command(MMC_SET_BLOCKLEN, 512);
00184 Spi_write_data(0xFF);
00185 if (r1 != 0x00)
00186 return KO;
00187
00188
00189 if (KO == mmc_sd_get_csd(csd))
00190 return KO;
00191
00192
00193 mmc_sd_get_capacity();
00194
00195
00196 #if (MMC_SD_READ_CID == ENABLED)
00197 if (KO == mmc_sd_get_cid(cid))
00198 return KO;
00199 #endif
00200
00201 mmc_sd_init_done = TRUE;
00202
00203 return(OK);
00204 }
00205
00206
00219
00220 U8 mmc_sd_send_command(U8 command, U32 arg)
00221 {
00222 Mmc_sd_select();
00223 r1 = mmc_sd_command(command, arg);
00224 Mmc_sd_unselect();
00225 return r1;
00226 }
00227
00240 U8 mmc_sd_command(U8 command, U32 arg)
00241 {
00242 U8 retry;
00243
00244 Spi_write_data(0xFF);
00245 Spi_write_data(command | 0x40);
00246 Spi_write_data(arg>>24);
00247 Spi_write_data(arg>>16);
00248 Spi_write_data(arg>>8 );
00249 Spi_write_data(arg );
00250 Spi_write_data(0x95);
00251
00252
00253
00254
00255 retry = 0;
00256 r1 = 0xFF;
00257 while((r1 = mmc_sd_send_and_read(0xFF)) == 0xFF)
00258 {
00259 retry++;
00260 if(retry > 10) break;
00261 }
00262 return r1;
00263 }
00264
00265
00266
00278 U8 mmc_sd_send_and_read(U8 data_to_send)
00279 {
00280 Spi_write_data(data_to_send);
00281 return (Spi_read_data());
00282 }
00283
00284
00285
00296 bit mmc_sd_get_csd(U8 *buffer)
00297 {
00298 U8 retry;
00299
00300
00301 if (KO == mmc_sd_wait_not_busy())
00302 return KO;
00303
00304 Mmc_sd_select();
00305
00306 r1 = mmc_sd_command(MMC_SEND_CSD, 0);
00307
00308 if(r1 != 0x00)
00309 {
00310 Mmc_sd_unselect();
00311 mmc_sd_init_done = FALSE;
00312 return KO;
00313 }
00314
00315 retry = 0;
00316 while((r1 = mmc_sd_send_and_read(0xFF)) != MMC_STARTBLOCK_READ)
00317 {
00318 if (retry > 8)
00319 {
00320 Mmc_sd_unselect();
00321 return KO;
00322 }
00323 retry++;
00324 }
00325 for (retry = 0; retry <16; retry++)
00326 {
00327 Spi_write_data(0xFF);
00328 buffer[retry] = Spi_read_data();
00329 }
00330 Spi_write_data(0xFF);
00331 Spi_write_data(0xFF);
00332 Spi_write_data(0xFF);
00333 Mmc_sd_unselect();
00334 return OK;
00335 }
00336
00337
00338
00349 bit mmc_sd_get_cid(U8 *buffer)
00350 {
00351 U8 retry;
00352
00353
00354 if (KO == mmc_sd_wait_not_busy())
00355 return KO;
00356
00357 Mmc_sd_select();
00358
00359 r1 = mmc_sd_command(MMC_SEND_CID, 0);
00360
00361 if(r1 != 0x00)
00362 {
00363 Mmc_sd_unselect();
00364 mmc_sd_init_done = FALSE;
00365 return KO;
00366 }
00367
00368 retry = 0;
00369 while((r2 = mmc_sd_send_and_read(0xFF)) != MMC_STARTBLOCK_READ)
00370 {
00371 if (retry > 8)
00372 {
00373 Mmc_sd_unselect();
00374 return KO;
00375 }
00376 retry++;
00377 }
00378
00379 for (retry = 0; retry <16; retry++)
00380 {
00381 Spi_write_data(0xFF);
00382 buffer[retry] = Spi_read_data();
00383 }
00384 Spi_write_data(0xFF);
00385 Spi_write_data(0xFF);
00386 Spi_write_data(0xFF);
00387 Mmc_sd_unselect();
00388 return OK;
00389 }
00390
00391
00392
00424 void mmc_sd_get_capacity(void)
00425 {
00426 U16 c_size;
00427 U8 c_size_mult;
00428 U8 read_bl_len;
00429 U8 erase_grp_size;
00430 U8 erase_grp_mult;
00431
00432
00433 c_size = ((csd[6] & 0x03) << 10) + (csd[7] << 2) + ((csd[8] & 0xC0) >> 6);
00434 c_size_mult = ((csd[9] & 0x03) << 1) + ((csd[10] & 0x80) >> 7);
00435 read_bl_len = csd[5] & 0x0F;
00436 if (card_type == MMC_CARD)
00437 {
00438 erase_grp_size = ((csd[10] & 0x7C) >> 2);
00439 erase_grp_mult = ((csd[10] & 0x03) << 3) | ((csd[11] & 0xE0) >> 5);
00440 }
00441 else
00442 {
00443 erase_grp_size = ((csd[10] & 0x3F) << 1) + ((csd[11] & 0x80) >> 7);
00444 erase_grp_mult = 0;
00445 }
00446
00447
00448 mmc_sd_last_block_address = ((U32)(c_size + 1) * (U32)((1 << (c_size_mult + 2)))) - 1;
00449 if (read_bl_len > 9)
00450 mmc_sd_last_block_address <<= (read_bl_len - 9);
00451
00452
00453 capacity = (1 << read_bl_len) * (mmc_sd_last_block_address + 1);
00454
00455
00456 erase_group_size = (erase_grp_size + 1) * (erase_grp_mult + 1);
00457 }
00458
00459
00460
00472 bit mmc_sd_get_status(void)
00473 {
00474 U8 retry, spireg;
00475
00476
00477 if (KO == mmc_sd_wait_not_busy())
00478 return KO;
00479
00480 Mmc_sd_select();
00481
00482
00483 Spi_write_data(MMC_SEND_STATUS | 0x40);
00484 Spi_write_data(0);
00485 Spi_write_data(0);
00486 Spi_write_data(0);
00487 Spi_write_data(0);
00488 Spi_write_data(0x95);
00489
00490
00491
00492
00493 retry = 0;
00494 r2 = 0xFFFF;
00495 spireg = 0xFF;
00496 while((spireg = mmc_sd_send_and_read(0xFF)) == 0xFF)
00497 {
00498 retry++;
00499 if(retry > 10)
00500 {
00501 Mmc_sd_unselect();
00502 return KO;
00503 }
00504 }
00505 r2 = ((U16)(spireg) << 8) + mmc_sd_send_and_read(0xFF);
00506
00507 Spi_write_data(0xFF);
00508 Mmc_sd_unselect();
00509
00510 return OK;
00511 }
00512
00513
00524 bit mmc_sd_wait_not_busy(void)
00525 {
00526 U16 retry;
00527
00528
00529 Mmc_sd_select();
00530 retry = 0;
00531 while((r1 = mmc_sd_send_and_read(0xFF)) != 0xFF)
00532 {
00533 retry++;
00534 if (retry == 50000)
00535 {
00536 Mmc_sd_unselect();
00537 return KO;
00538 }
00539 }
00540 Mmc_sd_unselect();
00541 return OK;
00542 }
00543
00544
00545
00559 bit mmc_sd_check_presence(void)
00560 {
00561 U16 retry;
00562
00563 retry = 0;
00564 if (mmc_sd_init_done == FALSE)
00565 {
00566
00567
00568 while ((r1 = mmc_sd_send_command(MMC_GO_IDLE_STATE, 0)) != 0x01)
00569 {
00570 Spi_write_data(0xFF);
00571 retry++;
00572 if (retry > 10)
00573 return KO;
00574 }
00575 return OK;
00576 }
00577 else
00578 {
00579
00580
00581
00582
00583
00584
00585
00586
00587
00588
00589
00590
00591
00592
00593
00594
00595
00596
00597
00598
00599 if ((r1 = mmc_sd_send_command(MMC_CRC_ON_OFF,0)) == 0x00)
00600 return OK;
00601 mmc_sd_init_done = FALSE;
00602 return KO;
00603 }
00604
00605 return KO;
00606 }
00607
00608
00621 bit mmc_sd_mem_check(void)
00622 {
00623 if (mmc_sd_check_presence() == OK)
00624 {
00625 if (mmc_sd_init_done == FALSE)
00626 {
00627 mmc_sd_init();
00628 }
00629 if (mmc_sd_init_done == TRUE)
00630 return OK;
00631 else
00632 return KO;
00633 }
00634 return KO;
00635 }
00636
00637
00638
00653 bit is_mmc_sd_write_pwd_locked(void)
00654 {
00655 if (card_type == MMC_CARD)
00656 {
00657 if (((csd[0] >> 2) & 0x0F) < 2)
00658 return KO;
00659 }
00660 if (KO == mmc_sd_get_status())
00661 return KO;
00662 if ((r2&0x0001) != 0)
00663 return OK;
00664
00665 return KO;
00666 }
00667
00668
00698 bit mmc_sd_lock_operation(U8 operation, U8 pwd_lg, U8 * pwd)
00699 {
00700 bit status = OK;
00701 U8 retry;
00702
00703
00704 if ((operation != OP_FORCED_ERASE) && (pwd_lg == 0))
00705 return KO;
00706
00707
00708 if (mmc_sd_wait_not_busy() == KO)
00709 return KO;
00710
00711
00712 if (operation == OP_FORCED_ERASE)
00713 r1 = mmc_sd_send_command(MMC_SET_BLOCKLEN, 1);
00714 else
00715 r1 = mmc_sd_send_command(MMC_SET_BLOCKLEN, pwd_lg+2);
00716 Spi_write_data(0xFF);
00717 Spi_write_data(0xFF);
00718 Spi_write_data(0xFF);
00719 if (r1 != 0x00)
00720 return KO;
00721
00722
00723 Mmc_sd_select();
00724
00725
00726 r1 = mmc_sd_command(MMC_LOCK_UNLOCK, 0);
00727
00728
00729 if(r1 != 0x00)
00730 {
00731 status = KO;
00732 }
00733
00734 Spi_write_data(0xFF);
00735
00736
00737 Spi_write_data(MMC_STARTBLOCK_WRITE);
00738
00739 Spi_write_data(operation);
00740 if (operation != OP_FORCED_ERASE)
00741 {
00742 Spi_write_data(pwd_lg);
00743 for(retry=0 ; retry<pwd_lg ; retry++)
00744 {
00745 Spi_write_data(*(pwd+retry));
00746 }
00747 }
00748 Spi_write_data(0xFF);
00749 Spi_write_data(0xFF);
00750
00751
00752 retry = 0;
00753 r1 = mmc_sd_send_and_read(0xFF);
00754 if ((r1 & MMC_DR_MASK) != MMC_DR_ACCEPT)
00755 status = KO;
00756
00757 Spi_write_data(0xFF);
00758 Mmc_sd_unselect();
00759
00760
00761 if (operation == OP_FORCED_ERASE)
00762 retry = 100;
00763 else
00764 retry = 10;
00765 while (mmc_sd_wait_not_busy() == KO)
00766 {
00767 retry--;
00768 if (retry == 0)
00769 {
00770 status = KO;
00771 break;
00772 }
00773 }
00774
00775
00776 if (KO == mmc_sd_get_status())
00777 status = KO;
00778 if ((r2&0x0002) != 0)
00779 status = KO;
00780
00781
00782 r1 = mmc_sd_send_command(MMC_SET_BLOCKLEN, 512);
00783 Spi_write_data(0xFF);
00784 if (r1 != 0x00)
00785 status = KO;
00786
00787 return status;
00788 }
00789
00790
00791
00802 bit mmc_sd_read_open (U32 pos)
00803 {
00804
00805 gl_ptr_mem = pos << 9;
00806
00807
00808 return mmc_sd_wait_not_busy();
00809 }
00810
00811
00821 void mmc_sd_read_close (void)
00822 {
00823
00824 }
00825
00826
00841 bit mmc_sd_write_open (U32 pos)
00842 {
00843
00844 gl_ptr_mem = pos << 9;
00845
00846
00847 return mmc_sd_wait_not_busy();
00848 }
00849
00850
00861 void mmc_sd_write_close (void)
00862 {
00863
00864 }
00865
00866
00867
00868 #if (MMC_SD_USB == ENABLE)
00869
00894 bit mmc_sd_read_sector(U16 nb_sector)
00895 {
00896 Byte i;
00897 U16 read_time_out;
00898
00899 do
00900 {
00901 Mmc_sd_select();
00902
00903 r1 = mmc_sd_command(MMC_READ_SINGLE_BLOCK, gl_ptr_mem);
00904
00905 if(r1 != 0x00)
00906 {
00907 Mmc_sd_unselect();
00908 return KO;
00909 }
00910
00911
00912 read_time_out = 30000;
00913 while((r1 = mmc_sd_send_and_read(0xFF)) == 0xFF)
00914 {
00915 read_time_out--;
00916 if (read_time_out == 0)
00917 {
00918 Mmc_sd_unselect();
00919 return KO;
00920 }
00921 }
00922
00923
00924 if (r1 != MMC_STARTBLOCK_READ)
00925 {
00926 Spi_write_data(0xFF);
00927 Mmc_sd_unselect();
00928 return KO;
00929 }
00930
00931
00932
00933
00934 for (i = 8; i != 0; i--)
00935 {
00936 Disable_interrupt();
00937
00938
00939
00940
00941 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00942 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00943 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00944 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00945 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00946 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00947 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00948 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00949 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00950 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00951 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00952 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00953 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00954 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00955 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00956 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00957 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00958 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00959 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00960 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00961 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00962 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00963 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00964 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00965 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00966 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00967 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00968 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00969 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00970 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00971 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00972 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00973 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00974 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00975 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00976 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00977 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00978 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00979 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00980 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00981 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00982 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00983 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00984 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00985 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00986 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00987 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00988 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00989 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00990 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00991 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00992 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00993 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00994 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00995 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00996 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00997 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00998 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00999 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
01000 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
01001 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
01002 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
01003 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
01004 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
01005 Enable_interrupt();
01006
01007
01008
01009
01010 Usb_send_in();
01011
01012 while(Is_usb_write_enabled()==FALSE)
01013 {
01014 if(!Is_usb_endpoint_enabled())
01015 return KO;
01016 }
01017 }
01018
01019 gl_ptr_mem += 512;
01020 nb_sector--;
01021
01022 Spi_write_data(0xFF);
01023 Spi_write_data(0xFF);
01024
01025 Spi_write_data(0xFF);
01026 Spi_write_data(0xFF);
01027
01028 Mmc_sd_unselect();
01029 }
01030 while (nb_sector != 0);
01031
01032 return OK;
01033 }
01034
01035
01059 bit mmc_sd_write_sector (U16 nb_sector)
01060 {
01061 U8 i;
01062
01063 do
01064 {
01065
01066 i=0;
01067 while (KO == mmc_sd_wait_not_busy())
01068 {
01069 i++;
01070 if (i == 10)
01071 return KO;
01072 }
01073
01074 Mmc_sd_select();
01075
01076 r1 = mmc_sd_command(MMC_WRITE_BLOCK, gl_ptr_mem);
01077
01078 if(r1 != 0x00)
01079 {
01080 Mmc_sd_unselect();
01081 return KO;
01082 }
01083
01084 Spi_write_data(0xFF);
01085
01086 Spi_write_data(MMC_STARTBLOCK_WRITE);
01087
01088
01089
01090
01091 for (i = 8; i != 0; i--)
01092 {
01093
01094 while(!Is_usb_read_enabled())
01095 {
01096 if(!Is_usb_endpoint_enabled())
01097 return KO;
01098 }
01099
01100 Disable_interrupt();
01101
01102
01103
01104
01105
01106 Spi_write_data(Usb_read_byte());
01107 Spi_write_data(Usb_read_byte());
01108 Spi_write_data(Usb_read_byte());
01109 Spi_write_data(Usb_read_byte());
01110 Spi_write_data(Usb_read_byte());
01111 Spi_write_data(Usb_read_byte());
01112 Spi_write_data(Usb_read_byte());
01113 Spi_write_data(Usb_read_byte());
01114 Spi_write_data(Usb_read_byte());
01115 Spi_write_data(Usb_read_byte());
01116 Spi_write_data(Usb_read_byte());
01117 Spi_write_data(Usb_read_byte());
01118 Spi_write_data(Usb_read_byte());
01119 Spi_write_data(Usb_read_byte());
01120 Spi_write_data(Usb_read_byte());
01121 Spi_write_data(Usb_read_byte());
01122 Spi_write_data(Usb_read_byte());
01123 Spi_write_data(Usb_read_byte());
01124 Spi_write_data(Usb_read_byte());
01125 Spi_write_data(Usb_read_byte());
01126 Spi_write_data(Usb_read_byte());
01127 Spi_write_data(Usb_read_byte());
01128 Spi_write_data(Usb_read_byte());
01129 Spi_write_data(Usb_read_byte());
01130 Spi_write_data(Usb_read_byte());
01131 Spi_write_data(Usb_read_byte());
01132 Spi_write_data(Usb_read_byte());
01133 Spi_write_data(Usb_read_byte());
01134 Spi_write_data(Usb_read_byte());
01135 Spi_write_data(Usb_read_byte());
01136 Spi_write_data(Usb_read_byte());
01137 Spi_write_data(Usb_read_byte());
01138 Spi_write_data(Usb_read_byte());
01139 Spi_write_data(Usb_read_byte());
01140 Spi_write_data(Usb_read_byte());
01141 Spi_write_data(Usb_read_byte());
01142 Spi_write_data(Usb_read_byte());
01143 Spi_write_data(Usb_read_byte());
01144 Spi_write_data(Usb_read_byte());
01145 Spi_write_data(Usb_read_byte());
01146 Spi_write_data(Usb_read_byte());
01147 Spi_write_data(Usb_read_byte());
01148 Spi_write_data(Usb_read_byte());
01149 Spi_write_data(Usb_read_byte());
01150 Spi_write_data(Usb_read_byte());
01151 Spi_write_data(Usb_read_byte());
01152 Spi_write_data(Usb_read_byte());
01153 Spi_write_data(Usb_read_byte());
01154 Spi_write_data(Usb_read_byte());
01155 Spi_write_data(Usb_read_byte());
01156 Spi_write_data(Usb_read_byte());
01157 Spi_write_data(Usb_read_byte());
01158 Spi_write_data(Usb_read_byte());
01159 Spi_write_data(Usb_read_byte());
01160 Spi_write_data(Usb_read_byte());
01161 Spi_write_data(Usb_read_byte());
01162 Spi_write_data(Usb_read_byte());
01163 Spi_write_data(Usb_read_byte());
01164 Spi_write_data(Usb_read_byte());
01165 Spi_write_data(Usb_read_byte());
01166 Spi_write_data(Usb_read_byte());
01167 Spi_write_data(Usb_read_byte());
01168 Spi_write_data(Usb_read_byte());
01169 Spi_write_data(Usb_read_byte());
01170 Spi_ack_write();
01171
01172 Usb_ack_receive_out();
01173
01174 Enable_interrupt();
01175 }
01176
01177 Spi_write_data(0xFF);
01178 Spi_write_data(0xFF);
01179
01180
01181 Spi_write_data(0xFF);
01182 r1 = Spi_read_data();
01183 if( (r1&MMC_DR_MASK) != MMC_DR_ACCEPT)
01184 {
01185 Mmc_sd_unselect();
01186 return r1;
01187 }
01188
01189
01190 Spi_write_data(0xFF);
01191
01192
01193 Mmc_sd_unselect();
01194 gl_ptr_mem += 512;
01195 nb_sector--;
01196 }
01197 while (nb_sector != 0);
01198
01199
01200 i=0;
01201 while (KO == mmc_sd_wait_not_busy())
01202 {
01203 i++;
01204 if (i == 10)
01205 return KO;
01206 }
01207
01208 return OK;
01209 }
01210 #endif // (MMC_SD_USB == ENABLE)
01211
01212
01237
01238
01239
01240
01241
01242
01243
01244
01270
01271
01272
01273
01274
01275
01276
01277
01278
01298 bit mmc_sd_erase_sector_group(U32 adr_start, U32 adr_end)
01299 {
01300 U8 cmd;
01301
01302
01303 if (KO == mmc_sd_wait_not_busy())
01304 return KO;
01305
01306 Mmc_sd_select();
01307
01308
01309 if (card_type == MMC_CARD)
01310 { cmd = MMC_TAG_ERASE_GROUP_START; }
01311 else
01312 { cmd = SD_TAG_WR_ERASE_GROUP_START; }
01313 if ((r1 = mmc_sd_command(cmd,(adr_start << 9))) != 0)
01314 {
01315 Mmc_sd_unselect();
01316 return KO;
01317 }
01318 Spi_write_data(0xFF);
01319
01320
01321 if (card_type == MMC_CARD)
01322 { cmd = MMC_TAG_ERASE_GROUP_END; }
01323 else
01324 { cmd = SD_TAG_WR_ERASE_GROUP_END; }
01325 if ((r1 = mmc_sd_command(cmd,(adr_end << 9))) != 0)
01326 {
01327 Mmc_sd_unselect();
01328 return KO;
01329 }
01330 Spi_write_data(0xFF);
01331
01332
01333 if ((r1 = mmc_sd_command(MMC_ERASE,0)) != 0)
01334 {
01335 Mmc_sd_unselect();
01336 return KO;
01337 }
01338 Spi_write_data(0xFF);
01339
01340 Mmc_sd_unselect();
01341
01342 return OK;
01343 }
01344
01345
01346
01347 #if (MMC_SD_RAM == ENABLED)
01348
01363 bit mmc_sd_read_sector_to_ram(U8 *ram)
01364 {
01365 U16 i;
01366 U16 read_time_out;
01367
01368
01369 if (KO == mmc_sd_wait_not_busy())
01370 return KO;
01371
01372 Mmc_sd_select();
01373
01374 r1 = mmc_sd_command(MMC_READ_SINGLE_BLOCK, gl_ptr_mem);
01375
01376
01377 if (r1 != 0x00)
01378 {
01379 Mmc_sd_unselect();
01380 return KO;
01381 }
01382
01383
01384 read_time_out = 30000;
01385 while((r1 = mmc_sd_send_and_read(0xFF)) == 0xFF)
01386 {
01387 read_time_out--;
01388 if (read_time_out == 0)
01389 {
01390 Mmc_sd_unselect();
01391 return KO;
01392 }
01393 }
01394
01395
01396 if (r1 != MMC_STARTBLOCK_READ)
01397 {
01398 Spi_write_data(0xFF);
01399 Mmc_sd_unselect();
01400 return KO;
01401 }
01402
01403
01404 Disable_interrupt();
01405 for(i=0;i<MMC_SECTOR_SIZE;i++)
01406 {
01407 Spi_write_data(0xFF);
01408 *ram=Spi_read_data();
01409 ram++;
01410 }
01411 Enable_interrupt();
01412 gl_ptr_mem += 512;
01413
01414
01415 Spi_write_data(0xFF);
01416 Spi_write_data(0xFF);
01417
01418
01419 Spi_write_data(0xFF);
01420 Spi_write_data(0xFF);
01421
01422
01423 Mmc_sd_unselect();
01424
01425 return OK;
01426 }
01427
01428
01429
01446 bit mmc_sd_write_sector_from_ram(U8 *ram)
01447 {
01448 U16 i;
01449
01450
01451 if (KO == mmc_sd_wait_not_busy())
01452 return KO;
01453
01454 Mmc_sd_select();
01455
01456 r1 = mmc_sd_command(MMC_WRITE_BLOCK, gl_ptr_mem);
01457
01458 if(r1 != 0x00)
01459 {
01460 Mmc_sd_unselect();
01461 return KO;
01462 }
01463
01464 Spi_write_data(0xFF);
01465
01466
01467 Spi_write_data(MMC_STARTBLOCK_WRITE);
01468
01469 for(i=0;i<MMC_SECTOR_SIZE;i++)
01470 {
01471 Spi_write_data(*ram);
01472 ram++;
01473 }
01474
01475 Spi_write_data(0xFF);
01476 Spi_write_data(0xFF);
01477
01478
01479 r1 = mmc_sd_send_and_read(0xFF);
01480 if( (r1&MMC_DR_MASK) != MMC_DR_ACCEPT)
01481 {
01482 Spi_write_data(0xFF);
01483 Spi_write_data(0xFF);
01484 Mmc_sd_unselect();
01485 return KO;
01486
01487 }
01488
01489 Spi_write_data(0xFF);
01490 Spi_write_data(0xFF);
01491
01492
01493 Mmc_sd_unselect();
01494 gl_ptr_mem += 512;
01495
01496
01497 i=0;
01498 while (KO == mmc_sd_wait_not_busy())
01499 {
01500 i++;
01501 if (i == 10)
01502 return KO;
01503 }
01504
01505 return OK;
01506 }
01507
01508 #endif // (MMC_SD_RAM == ENABLE)
01509