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 #include "config.h"
00044 #include "conf_sdmmc.h"
00045 #include "lib_mcu/usb/usb_drv.h"
00046 #include "lib_mcu/spi/spi_drv.h"
00047 #include "mmc_sd.h"
00048
00049
00050
00051
00052 static U32 gl_ptr_mem;
00053
00054 bit mmc_sd_init_done = FALSE;
00055 U8 r1;
00056 U16 r2;
00057
00058 U8 csd[16];
00059 volatile U32 capacity;
00060 volatile U32 mmc_sd_last_block_address;
00061 U16 erase_group_size;
00062 U8 card_type;
00063
00064
00065 #if (MMC_SD_RAM == ENABLED)
00066 U8 data_mem[513];
00067 #endif
00068 #if (MMC_SD_READ_CID == ENABLED)
00069 U8 cid[16];
00070 #endif
00071
00072
00073
00074
00085 void mmc_sd_spi_init(void)
00086 {
00087 Spi_select_master();
00088 Spi_set_mode(SPI_MODE_0);
00089 Spi_init_bus();
00090 Spi_set_rate(SPI_RATE_0);
00091 Spi_disable_ss();
00092 Spi_enable();
00093 }
00094
00095
00107 bit mmc_sd_init (void)
00108 {
00109 U16 retry;
00110
00111
00112 mmc_sd_spi_init();
00113
00114
00115 mmc_sd_init_done = FALSE;
00116 card_type = MMC_CARD;
00117 retry = 0;
00118 do
00119 {
00120
00121 r1 = mmc_sd_send_command(MMC_GO_IDLE_STATE, 0);
00122 Spi_write_data(0xFF);
00123
00124 retry++;
00125 if(retry > 100)
00126 return KO;
00127 }
00128 while(r1 != 0x01);
00129
00130
00131
00132 r1 = mmc_sd_send_command(SD_APP_CMD55,0);
00133 Spi_write_data(0xFF);
00134
00135 r1 = mmc_sd_send_command(SD_SEND_OP_COND_ACMD, 0);
00136 Spi_write_data(0xFF);
00137
00138 if ((r1&0xFE) == 0)
00139 {
00140 card_type = SD_CARD;
00141 }
00142 else
00143 {
00144 card_type = MMC_CARD;
00145
00146 retry = 0;
00147 do
00148 {
00149
00150 r1 = mmc_sd_send_command(MMC_GO_IDLE_STATE, 0);
00151 Spi_write_data(0xFF);
00152
00153 retry++;
00154 if(retry > 100)
00155 return KO;
00156 }
00157 while(r1 != 0x01);
00158 }
00159
00160
00161
00162 retry = 0;
00163 do
00164 {
00165
00166 r1 = mmc_sd_send_command(MMC_SEND_OP_COND, 0);
00167 Spi_write_data(0xFF);
00168
00169 retry++;
00170 if(retry == 50000)
00171 return KO;
00172 }
00173 while (r1);
00174
00175
00176 r1 = mmc_sd_send_command(MMC_CRC_ON_OFF, 0);
00177 Spi_write_data(0xFF);
00178
00179
00180 r1 = mmc_sd_send_command(MMC_SET_BLOCKLEN, 512);
00181 Spi_write_data(0xFF);
00182 if (r1 != 0x00)
00183 return KO;
00184
00185
00186 if (KO == mmc_sd_get_csd(csd))
00187 return KO;
00188
00189
00190 mmc_sd_get_capacity();
00191
00192
00193 #if (MMC_SD_READ_CID == ENABLED)
00194 if (KO == mmc_sd_get_cid(cid))
00195 return KO;
00196 #endif
00197
00198 mmc_sd_init_done = TRUE;
00199
00200 return(OK);
00201 }
00202
00203
00216
00217 U8 mmc_sd_send_command(U8 command, U32 arg)
00218 {
00219 Mmc_sd_select();
00220 r1 = mmc_sd_command(command, arg);
00221 Mmc_sd_unselect();
00222 return r1;
00223 }
00224
00237 U8 mmc_sd_command(U8 command, U32 arg)
00238 {
00239 U8 retry;
00240
00241 Spi_write_data(0xFF);
00242 Spi_write_data(command | 0x40);
00243 Spi_write_data(arg>>24);
00244 Spi_write_data(arg>>16);
00245 Spi_write_data(arg>>8 );
00246 Spi_write_data(arg );
00247 Spi_write_data(0x95);
00248
00249
00250
00251
00252 retry = 0;
00253 r1 = 0xFF;
00254 while((r1 = mmc_sd_send_and_read(0xFF)) == 0xFF)
00255 {
00256 retry++;
00257 if(retry > 10) break;
00258 }
00259 return r1;
00260 }
00261
00262
00263
00275 U8 mmc_sd_send_and_read(U8 data_to_send)
00276 {
00277 Spi_write_data(data_to_send);
00278 return (Spi_read_data());
00279 }
00280
00281
00282
00293 bit mmc_sd_get_csd(U8 *buffer)
00294 {
00295 U8 retry;
00296
00297
00298 if (KO == mmc_sd_wait_not_busy())
00299 return KO;
00300
00301 Mmc_sd_select();
00302
00303 r1 = mmc_sd_command(MMC_SEND_CSD, 0);
00304
00305 if(r1 != 0x00)
00306 {
00307 Mmc_sd_unselect();
00308 mmc_sd_init_done = FALSE;
00309 return KO;
00310 }
00311
00312 retry = 0;
00313 while((r1 = mmc_sd_send_and_read(0xFF)) != MMC_STARTBLOCK_READ)
00314 {
00315 if (retry > 8)
00316 {
00317 Mmc_sd_unselect();
00318 return KO;
00319 }
00320 retry++;
00321 }
00322 for (retry = 0; retry <16; retry++)
00323 {
00324 Spi_write_data(0xFF);
00325 buffer[retry] = Spi_read_data();
00326 }
00327 Spi_write_data(0xFF);
00328 Spi_write_data(0xFF);
00329 Spi_write_data(0xFF);
00330 Mmc_sd_unselect();
00331 return OK;
00332 }
00333
00334
00335
00346 bit mmc_sd_get_cid(U8 *buffer)
00347 {
00348 U8 retry;
00349
00350
00351 if (KO == mmc_sd_wait_not_busy())
00352 return KO;
00353
00354 Mmc_sd_select();
00355
00356 r1 = mmc_sd_command(MMC_SEND_CID, 0);
00357
00358 if(r1 != 0x00)
00359 {
00360 Mmc_sd_unselect();
00361 mmc_sd_init_done = FALSE;
00362 return KO;
00363 }
00364
00365 retry = 0;
00366 while((r2 = mmc_sd_send_and_read(0xFF)) != MMC_STARTBLOCK_READ)
00367 {
00368 if (retry > 8)
00369 {
00370 Mmc_sd_unselect();
00371 return KO;
00372 }
00373 retry++;
00374 }
00375
00376 for (retry = 0; retry <16; retry++)
00377 {
00378 Spi_write_data(0xFF);
00379 buffer[retry] = Spi_read_data();
00380 }
00381 Spi_write_data(0xFF);
00382 Spi_write_data(0xFF);
00383 Spi_write_data(0xFF);
00384 Mmc_sd_unselect();
00385 return OK;
00386 }
00387
00388
00389
00421 void mmc_sd_get_capacity(void)
00422 {
00423 U16 c_size;
00424 U8 c_size_mult;
00425 U8 read_bl_len;
00426 U8 erase_grp_size;
00427 U8 erase_grp_mult;
00428
00429
00430 c_size = ((csd[6] & 0x03) << 10) + (csd[7] << 2) + ((csd[8] & 0xC0) >> 6);
00431 c_size_mult = ((csd[9] & 0x03) << 1) + ((csd[10] & 0x80) >> 7);
00432 read_bl_len = csd[5] & 0x0F;
00433 if (card_type == MMC_CARD)
00434 {
00435 erase_grp_size = ((csd[10] & 0x7C) >> 2);
00436 erase_grp_mult = ((csd[10] & 0x03) << 3) | ((csd[11] & 0xE0) >> 5);
00437 }
00438 else
00439 {
00440 erase_grp_size = ((csd[10] & 0x3F) << 1) + ((csd[11] & 0x80) >> 7);
00441 erase_grp_mult = 0;
00442 }
00443
00444
00445 mmc_sd_last_block_address = ((U32)(c_size + 1) * (U32)((1 << (c_size_mult + 2)))) - 1;
00446 if (read_bl_len > 9)
00447 mmc_sd_last_block_address <<= (read_bl_len - 9);
00448
00449
00450 capacity = (1 << read_bl_len) * (mmc_sd_last_block_address + 1);
00451
00452
00453 erase_group_size = (erase_grp_size + 1) * (erase_grp_mult + 1);
00454 }
00455
00456
00457
00469 bit mmc_sd_get_status(void)
00470 {
00471 U8 retry, spireg;
00472
00473
00474 if (KO == mmc_sd_wait_not_busy())
00475 return KO;
00476
00477 Mmc_sd_select();
00478
00479
00480 Spi_write_data(MMC_SEND_STATUS | 0x40);
00481 Spi_write_data(0);
00482 Spi_write_data(0);
00483 Spi_write_data(0);
00484 Spi_write_data(0);
00485 Spi_write_data(0x95);
00486
00487
00488
00489
00490 retry = 0;
00491 r2 = 0xFFFF;
00492 spireg = 0xFF;
00493 while((spireg = mmc_sd_send_and_read(0xFF)) == 0xFF)
00494 {
00495 retry++;
00496 if(retry > 10)
00497 {
00498 Mmc_sd_unselect();
00499 return KO;
00500 }
00501 }
00502 r2 = ((U16)(spireg) << 8) + mmc_sd_send_and_read(0xFF);
00503
00504 Spi_write_data(0xFF);
00505 Mmc_sd_unselect();
00506
00507 return OK;
00508 }
00509
00510
00521 bit mmc_sd_wait_not_busy(void)
00522 {
00523 U16 retry;
00524
00525
00526 Mmc_sd_select();
00527 retry = 0;
00528 while((r1 = mmc_sd_send_and_read(0xFF)) != 0xFF)
00529 {
00530 retry++;
00531 if (retry == 50000)
00532 {
00533 Mmc_sd_unselect();
00534 return KO;
00535 }
00536 }
00537 Mmc_sd_unselect();
00538 return OK;
00539 }
00540
00541
00542
00556 bit mmc_sd_check_presence(void)
00557 {
00558 U16 retry;
00559
00560 retry = 0;
00561 if (mmc_sd_init_done == FALSE)
00562 {
00563
00564
00565 while ((r1 = mmc_sd_send_command(MMC_GO_IDLE_STATE, 0)) != 0x01)
00566 {
00567 Spi_write_data(0xFF);
00568 retry++;
00569 if (retry > 10)
00570 return KO;
00571 }
00572 return OK;
00573 }
00574 else
00575 {
00576
00577
00578
00579
00580
00581
00582
00583
00584
00585
00586
00587
00588
00589
00590
00591
00592
00593
00594
00595
00596 if ((r1 = mmc_sd_send_command(MMC_CRC_ON_OFF,0)) == 0x00)
00597 return OK;
00598 mmc_sd_init_done = FALSE;
00599 return KO;
00600 }
00601
00602 return KO;
00603 }
00604
00605
00618 bit mmc_sd_mem_check(void)
00619 {
00620 if (mmc_sd_check_presence() == OK)
00621 {
00622 if (mmc_sd_init_done == FALSE)
00623 {
00624 mmc_sd_init();
00625 }
00626 if (mmc_sd_init_done == TRUE)
00627 return OK;
00628 else
00629 return KO;
00630 }
00631 return KO;
00632 }
00633
00634
00635
00650 bit is_mmc_sd_write_pwd_locked(void)
00651 {
00652 if (card_type == MMC_CARD)
00653 {
00654 if (((csd[0] >> 2) & 0x0F) < 2)
00655 return KO;
00656 }
00657 if (KO == mmc_sd_get_status())
00658 return KO;
00659 if ((r2&0x0001) != 0)
00660 return OK;
00661
00662 return KO;
00663 }
00664
00665
00695 bit mmc_sd_lock_operation(U8 operation, U8 pwd_lg, U8 * pwd)
00696 {
00697 bit status = OK;
00698 U8 retry;
00699
00700
00701 if ((operation != OP_FORCED_ERASE) && (pwd_lg == 0))
00702 return KO;
00703
00704
00705 if (mmc_sd_wait_not_busy() == KO)
00706 return KO;
00707
00708
00709 if (operation == OP_FORCED_ERASE)
00710 r1 = mmc_sd_send_command(MMC_SET_BLOCKLEN, 1);
00711 else
00712 r1 = mmc_sd_send_command(MMC_SET_BLOCKLEN, pwd_lg+2);
00713 Spi_write_data(0xFF);
00714 Spi_write_data(0xFF);
00715 Spi_write_data(0xFF);
00716 if (r1 != 0x00)
00717 return KO;
00718
00719
00720 Mmc_sd_select();
00721
00722
00723 r1 = mmc_sd_command(MMC_LOCK_UNLOCK, 0);
00724
00725
00726 if(r1 != 0x00)
00727 {
00728 status = KO;
00729 }
00730
00731 Spi_write_data(0xFF);
00732
00733
00734 Spi_write_data(MMC_STARTBLOCK_WRITE);
00735
00736 Spi_write_data(operation);
00737 if (operation != OP_FORCED_ERASE)
00738 {
00739 Spi_write_data(pwd_lg);
00740 for(retry=0 ; retry<pwd_lg ; retry++)
00741 {
00742 Spi_write_data(*(pwd+retry));
00743 }
00744 }
00745 Spi_write_data(0xFF);
00746 Spi_write_data(0xFF);
00747
00748
00749 retry = 0;
00750 r1 = mmc_sd_send_and_read(0xFF);
00751 if ((r1 & MMC_DR_MASK) != MMC_DR_ACCEPT)
00752 status = KO;
00753
00754 Spi_write_data(0xFF);
00755 Mmc_sd_unselect();
00756
00757
00758 if (operation == OP_FORCED_ERASE)
00759 retry = 100;
00760 else
00761 retry = 10;
00762 while (mmc_sd_wait_not_busy() == KO)
00763 {
00764 retry--;
00765 if (retry == 0)
00766 {
00767 status = KO;
00768 break;
00769 }
00770 }
00771
00772
00773 if (KO == mmc_sd_get_status())
00774 status = KO;
00775 if ((r2&0x0002) != 0)
00776 status = KO;
00777
00778
00779 r1 = mmc_sd_send_command(MMC_SET_BLOCKLEN, 512);
00780 Spi_write_data(0xFF);
00781 if (r1 != 0x00)
00782 status = KO;
00783
00784 return status;
00785 }
00786
00787
00788
00799 bit mmc_sd_read_open (U32 pos)
00800 {
00801
00802 gl_ptr_mem = pos << 9;
00803
00804
00805 return mmc_sd_wait_not_busy();
00806 }
00807
00808
00818 void mmc_sd_read_close (void)
00819 {
00820
00821 }
00822
00823
00838 bit mmc_sd_write_open (U32 pos)
00839 {
00840
00841 gl_ptr_mem = pos << 9;
00842
00843
00844 return mmc_sd_wait_not_busy();
00845 }
00846
00847
00858 void mmc_sd_write_close (void)
00859 {
00860
00861 }
00862
00863
00864
00865 #if (MMC_SD_USB == ENABLE)
00866
00891 bit mmc_sd_read_sector(U16 nb_sector)
00892 {
00893 Byte i;
00894 U16 read_time_out;
00895
00896 do
00897 {
00898 Mmc_sd_select();
00899
00900 r1 = mmc_sd_command(MMC_READ_SINGLE_BLOCK, gl_ptr_mem);
00901
00902 if(r1 != 0x00)
00903 {
00904 Mmc_sd_unselect();
00905 return KO;
00906 }
00907
00908
00909 read_time_out = 30000;
00910 while((r1 = mmc_sd_send_and_read(0xFF)) == 0xFF)
00911 {
00912 read_time_out--;
00913 if (read_time_out == 0)
00914 {
00915 Mmc_sd_unselect();
00916 return KO;
00917 }
00918 }
00919
00920
00921 if (r1 != MMC_STARTBLOCK_READ)
00922 {
00923 Spi_write_data(0xFF);
00924 Mmc_sd_unselect();
00925 return KO;
00926 }
00927
00928
00929
00930
00931 for (i = 8; i != 0; i--)
00932 {
00933 Disable_interrupt();
00934
00935
00936
00937
00938 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00939 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00940 Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
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 Enable_interrupt();
01003
01004
01005
01006
01007 Usb_send_in();
01008
01009 while(Is_usb_write_enabled()==FALSE)
01010 {
01011 if(!Is_usb_endpoint_enabled())
01012 return KO;
01013 }
01014 }
01015
01016 gl_ptr_mem += 512;
01017 nb_sector--;
01018
01019 Spi_write_data(0xFF);
01020 Spi_write_data(0xFF);
01021
01022 Spi_write_data(0xFF);
01023 Spi_write_data(0xFF);
01024
01025 Mmc_sd_unselect();
01026 }
01027 while (nb_sector != 0);
01028
01029 return OK;
01030 }
01031
01032
01056 bit mmc_sd_write_sector (U16 nb_sector)
01057 {
01058 U8 i;
01059
01060 do
01061 {
01062
01063 i=0;
01064 while (KO == mmc_sd_wait_not_busy())
01065 {
01066 i++;
01067 if (i == 10)
01068 return KO;
01069 }
01070
01071 Mmc_sd_select();
01072
01073 r1 = mmc_sd_command(MMC_WRITE_BLOCK, gl_ptr_mem);
01074
01075 if(r1 != 0x00)
01076 {
01077 Mmc_sd_unselect();
01078 return KO;
01079 }
01080
01081 Spi_write_data(0xFF);
01082
01083 Spi_write_data(MMC_STARTBLOCK_WRITE);
01084
01085
01086
01087
01088 for (i = 8; i != 0; i--)
01089 {
01090
01091 while(!Is_usb_read_enabled())
01092 {
01093 if(!Is_usb_endpoint_enabled())
01094 return KO;
01095 }
01096
01097 Disable_interrupt();
01098
01099
01100
01101
01102
01103 Spi_write_data(Usb_read_byte());
01104 Spi_write_data(Usb_read_byte());
01105 Spi_write_data(Usb_read_byte());
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_ack_write();
01168
01169 Usb_ack_receive_out();
01170
01171 Enable_interrupt();
01172 }
01173
01174 Spi_write_data(0xFF);
01175 Spi_write_data(0xFF);
01176
01177
01178 Spi_write_data(0xFF);
01179 r1 = Spi_read_data();
01180 if( (r1&MMC_DR_MASK) != MMC_DR_ACCEPT)
01181 {
01182 Mmc_sd_unselect();
01183 return r1;
01184 }
01185
01186
01187 Spi_write_data(0xFF);
01188
01189
01190 Mmc_sd_unselect();
01191 gl_ptr_mem += 512;
01192 nb_sector--;
01193 }
01194 while (nb_sector != 0);
01195
01196
01197 i=0;
01198 while (KO == mmc_sd_wait_not_busy())
01199 {
01200 i++;
01201 if (i == 10)
01202 return KO;
01203 }
01204
01205 return OK;
01206 }
01207 #endif // (MMC_SD_USB == ENABLE)
01208
01209
01234
01235
01236
01237
01238
01239
01240
01241
01267
01268
01269
01270
01271
01272
01273
01274
01275
01295 bit mmc_sd_erase_sector_group(U32 adr_start, U32 adr_end)
01296 {
01297 U8 cmd;
01298
01299
01300 if (KO == mmc_sd_wait_not_busy())
01301 return KO;
01302
01303 Mmc_sd_select();
01304
01305
01306 if (card_type == MMC_CARD)
01307 { cmd = MMC_TAG_ERASE_GROUP_START; }
01308 else
01309 { cmd = SD_TAG_WR_ERASE_GROUP_START; }
01310 if ((r1 = mmc_sd_command(cmd,(adr_start << 9))) != 0)
01311 {
01312 Mmc_sd_unselect();
01313 return KO;
01314 }
01315 Spi_write_data(0xFF);
01316
01317
01318 if (card_type == MMC_CARD)
01319 { cmd = MMC_TAG_ERASE_GROUP_END; }
01320 else
01321 { cmd = SD_TAG_WR_ERASE_GROUP_END; }
01322 if ((r1 = mmc_sd_command(cmd,(adr_end << 9))) != 0)
01323 {
01324 Mmc_sd_unselect();
01325 return KO;
01326 }
01327 Spi_write_data(0xFF);
01328
01329
01330 if ((r1 = mmc_sd_command(MMC_ERASE,0)) != 0)
01331 {
01332 Mmc_sd_unselect();
01333 return KO;
01334 }
01335 Spi_write_data(0xFF);
01336
01337 Mmc_sd_unselect();
01338
01339 return OK;
01340 }
01341
01342
01343
01344 #if (MMC_SD_RAM == ENABLED)
01345
01360 bit mmc_sd_read_sector_to_ram(U8 *ram)
01361 {
01362 U16 i;
01363 U16 read_time_out;
01364
01365
01366 if (KO == mmc_sd_wait_not_busy())
01367 return KO;
01368
01369 Mmc_sd_select();
01370
01371 r1 = mmc_sd_command(MMC_READ_SINGLE_BLOCK, gl_ptr_mem);
01372
01373
01374 if (r1 != 0x00)
01375 {
01376 Mmc_sd_unselect();
01377 return KO;
01378 }
01379
01380
01381 read_time_out = 30000;
01382 while((r1 = mmc_sd_send_and_read(0xFF)) == 0xFF)
01383 {
01384 read_time_out--;
01385 if (read_time_out == 0)
01386 {
01387 Mmc_sd_unselect();
01388 return KO;
01389 }
01390 }
01391
01392
01393 if (r1 != MMC_STARTBLOCK_READ)
01394 {
01395 Spi_write_data(0xFF);
01396 Mmc_sd_unselect();
01397 return KO;
01398 }
01399
01400
01401 Disable_interrupt();
01402 for(i=0;i<MMC_SECTOR_SIZE;i++)
01403 {
01404 Spi_write_data(0xFF);
01405 *ram=Spi_read_data();
01406 ram++;
01407 }
01408 Enable_interrupt();
01409 gl_ptr_mem += 512;
01410
01411
01412 Spi_write_data(0xFF);
01413 Spi_write_data(0xFF);
01414
01415
01416 Spi_write_data(0xFF);
01417 Spi_write_data(0xFF);
01418
01419
01420 Mmc_sd_unselect();
01421
01422 return OK;
01423 }
01424
01425
01426
01443 bit mmc_sd_write_sector_from_ram(U8 *ram)
01444 {
01445 U16 i;
01446
01447
01448 if (KO == mmc_sd_wait_not_busy())
01449 return KO;
01450
01451 Mmc_sd_select();
01452
01453 r1 = mmc_sd_command(MMC_WRITE_BLOCK, gl_ptr_mem);
01454
01455 if(r1 != 0x00)
01456 {
01457 Mmc_sd_unselect();
01458 return KO;
01459 }
01460
01461 Spi_write_data(0xFF);
01462
01463
01464 Spi_write_data(MMC_STARTBLOCK_WRITE);
01465
01466 for(i=0;i<MMC_SECTOR_SIZE;i++)
01467 {
01468 Spi_write_data(*ram);
01469 ram++;
01470 }
01471
01472 Spi_write_data(0xFF);
01473 Spi_write_data(0xFF);
01474
01475
01476 r1 = mmc_sd_send_and_read(0xFF);
01477 if( (r1&MMC_DR_MASK) != MMC_DR_ACCEPT)
01478 {
01479 Spi_write_data(0xFF);
01480 Spi_write_data(0xFF);
01481 Mmc_sd_unselect();
01482 return KO;
01483
01484 }
01485
01486 Spi_write_data(0xFF);
01487 Spi_write_data(0xFF);
01488
01489
01490 Mmc_sd_unselect();
01491 gl_ptr_mem += 512;
01492
01493
01494 i=0;
01495 while (KO == mmc_sd_wait_not_busy())
01496 {
01497 i++;
01498 if (i == 10)
01499 return KO;
01500 }
01501
01502 return OK;
01503 }
01504
01505 #endif // (MMC_SD_RAM == ENABLE)
01506