sd 32 update

Dependencies:   FreescaleIAP mbed-rtos mbed

Fork of COM_MNG_TMTC_SIMPLE by Shreesha S

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers cdms_sd.h Source File

cdms_sd.h

00001 
00002 
00003 
00004 SPI spi(PTE1, PTE3, PTE2);      // MOSI,MISO, CLOCK microcontroller(in order)     
00005 DigitalOut cs_sd(PTE22);
00006 
00007 Serial sd1(USBTX,USBRX);
00008 
00009 
00010 #define SD_COMMAND_TIMEOUT 5000
00011 
00012 #define SD_DBG             0
00013 
00014 #define R1_IDLE_STATE           (1 << 0)
00015 #define R1_ERASE_RESET          (1 << 1)
00016 #define R1_ILLEGAL_COMMAND      (1 << 2)
00017 #define R1_COM_CRC_ERROR        (1 << 3)
00018 #define R1_ERASE_SEQUENCE_ERROR (1 << 4)
00019 #define R1_ADDRESS_ERROR        (1 << 5)
00020 #define R1_PARAMETER_ERROR      (1 << 6)
00021 
00022 
00023 
00024 #define SD_MAX_CYCLES 10000
00025 
00026 extern uint8_t SD_INIT_FLAGS;
00027 
00028 
00029 
00030 int initialise_card();
00031 int initialise_card_v1();
00032 int initialise_card_v2();
00033 int disk_write(const uint8_t *, uint64_t);
00034 int disk_read(uint8_t *, uint64_t);
00035 int disk_initialize();
00036 int disk_erase(int,int);
00037 
00038 //void FCTN_CDMS_INIT_SD();
00039 //int FCTN_CDMS_WR_SD(const uint8_t *, uint64_t);
00040 //int FCTN_CDMS_RD_SD(uint8_t *, uint64_t);
00041 uint32_t FCTN_SD_MNGR(uint8_t);
00042 
00043 
00044 int cmd(int, int);
00045 int cmd58();
00046 int cmdx(int, int);
00047 int cmd8();
00048 int read(uint8_t*, uint32_t );
00049 int write(const uint8_t*, uint32_t );
00050 static uint32_t ext_bits(unsigned char *, int , int );
00051 int SD_WRITE(uint8_t*,uint32_t,uint8_t);
00052 void FCTN_CDMS_SD_INIT();
00053 int SD_READ(uint8_t*,uint32_t,uint8_t);
00054 #define SDCARD_FAIL 0
00055 #define SDCARD_V1   1
00056 #define SDCARD_V2   2
00057 #define SDCARD_V2HC 3
00058 
00059 
00060 
00061 
00062 
00063 
00064 
00065 
00066 
00067 
00068 
00069 int cdv;
00070 uint64_t sd_sectors();
00071 uint64_t sectors;
00072 
00073 void FCTN_CDMS_SD_INIT()
00074 {
00075    
00076     initialise_card();
00077     
00078     disk_initialize();
00079          
00080 }
00081 
00082     
00083 
00084 uint32_t  FCTN_SD_MNGR(uint8_t sid)
00085 
00086 {
00087    
00088     uint32_t SD_MNG_SECT=7000;
00089     
00090     uint32_t fsc;
00091     uint8_t buffer[512];
00092     int b=disk_read(buffer, SD_MNG_SECT);
00093     if(sid==0x0)
00094     {
00095         
00096         fsc=(uint32_t)(buffer[0]<<24)+(uint32_t)(buffer[1]<<16)+(uint32_t)(buffer[2]<<8)+(uint32_t)buffer[3];
00097         uint32_t next_fsc=fsc+1;
00098         buffer[0]=(uint8_t) (next_fsc>>24 & 0xFF);
00099         buffer[1]=(uint8_t) (next_fsc>>16 & 0xFF);
00100         buffer[2]=(uint8_t) (next_fsc>>8 & 0xFF);
00101         buffer[3]=(uint8_t) (next_fsc & 0xFF);
00102         buffer[511]+=2;
00103         disk_write(buffer,SD_MNG_SECT);
00104     }
00105     if(sid==0x1)
00106     {
00107         fsc=(uint32_t)(buffer[4]<<24)+(uint32_t)(buffer[5]<<16)+(uint32_t)(buffer[6]<<8)+(uint32_t)buffer[7];
00108         uint32_t next_fsc=fsc+1;
00109         buffer[4]=(uint8_t) (next_fsc>>24 & 0xFF);
00110         buffer[5]=(uint8_t) (next_fsc>>16 & 0xFF);
00111         buffer[6]=(uint8_t) (next_fsc>>8 & 0xFF);
00112         buffer[7]=(uint8_t) (next_fsc & 0xFF);
00113         buffer[511]+=2;
00114         disk_write(buffer,SD_MNG_SECT);
00115     }
00116     if(sid==0x2)
00117     {
00118         fsc=(uint32_t)(buffer[8]<<24)+(uint32_t)(buffer[9]<<16)+(uint32_t)(buffer[10]<<8)+(uint32_t)buffer[11];
00119         uint32_t next_fsc=fsc+1;
00120         buffer[8]=(uint8_t) (next_fsc>>24 & 0xFF);
00121         buffer[9]=(uint8_t) (next_fsc>>16 & 0xFF);
00122         buffer[10]=(uint8_t) (next_fsc>>8 & 0xFF);
00123         buffer[11]=(uint8_t) (next_fsc & 0xFF);
00124         buffer[511]+=2;
00125         disk_write(buffer,SD_MNG_SECT);
00126     }
00127      if(sid==0x3)
00128     {
00129         fsc=(uint32_t)(buffer[12]<<24)+(uint32_t)(buffer[13]<<16)+(uint32_t)(buffer[14]<<8)+(uint32_t)buffer[15];
00130         uint32_t next_fsc=fsc+1;
00131         buffer[12]=(uint8_t) (next_fsc>>24 & 0xFF);
00132         buffer[13]=(uint8_t) (next_fsc>>16 & 0xFF);
00133         buffer[14]=(uint8_t) (next_fsc>>8 & 0xFF);
00134         buffer[15]=(uint8_t) (next_fsc & 0xFF);
00135         buffer[511]+=2;
00136         disk_write(buffer,SD_MNG_SECT);
00137     }
00138      if(sid==0x4)
00139     {
00140         fsc=(uint32_t)(buffer[16]<<24)+(uint32_t)(buffer[17]<<16)+(uint32_t)(buffer[18]<<8)+(uint32_t)buffer[19];
00141         uint32_t next_fsc=fsc+1;
00142         buffer[16]=(uint8_t) (next_fsc>>24 & 0xFF);
00143         buffer[17]=(uint8_t) (next_fsc>>16 & 0xFF);
00144         buffer[18]=(uint8_t) (next_fsc>>8 & 0xFF);
00145         buffer[19]=(uint8_t) (next_fsc & 0xFF);
00146         buffer[511]+=2;
00147         disk_write(buffer,SD_MNG_SECT);
00148     }
00149     return fsc;
00150 }
00151 
00152 
00153 int SD_WRITE(uint8_t* buffer,uint32_t fsc,uint8_t sid)
00154 {
00155    
00156     uint32_t SD_SCP_FIRST=1001;
00157     uint32_t SD_SCP_LAST=2000;
00158     uint32_t SD_SFF_AT_FIRST=2001;
00159     uint32_t SD_SFF_AT_LAST = 3000;
00160     uint32_t SD_SFF_BT_FIRST =3001;
00161     uint32_t SD_SFF_BT_LAST=4000;
00162     uint32_t SD_HK_ARCH_FIRST=4001;
00163     uint32_t SD_HK_ARCH_LAST= 5000;
00164     uint32_t LOG_FIRST =5001;
00165     uint32_t LOG_LAST=6000;
00166     uint32_t SD_MNG_SECT=7000;
00167     uint32_t block_number;
00168     int result;
00169     if(sid==0x0)
00170     {
00171         block_number=SD_SCP_FIRST+fsc;
00172 //        printf("write_block_number=%d\r\n",block_number);
00173         result= disk_write(buffer,block_number);
00174         return result;
00175     }
00176    if(sid==0x1)
00177     {
00178         block_number=SD_SFF_AT_FIRST + fsc;
00179         result= disk_write(buffer,block_number);
00180         return result;
00181     }
00182      if(sid==0x2)
00183     {
00184         block_number=SD_SFF_BT_FIRST + fsc;
00185         result= disk_write(buffer,block_number);
00186         return result;
00187     }
00188      if(sid==0x3)
00189     {
00190         block_number=SD_HK_ARCH_FIRST+fsc;
00191 //        sd1.printf("Block number is %d \r\n",block_number);
00192         result= disk_write(buffer,block_number);
00193         return result;
00194     }
00195      if(sid==0x4)
00196     {
00197         block_number=LOG_FIRST +fsc;
00198         result= disk_write(buffer,block_number);
00199         return result;
00200     }
00201     return 1;
00202 }
00203 
00204 int SD_READ(uint8_t* buffer,uint32_t fsc,uint8_t sid)
00205 {
00206    
00207     uint32_t SD_SCP_FIRST=1001;
00208     uint32_t SD_SCP_LAST=2000;
00209     uint32_t SD_SFF_AT_FIRST=2001;
00210     uint32_t SD_SFF_AT_LAST = 3000;
00211     uint32_t SD_SFF_BT_FIRST =3001;
00212     uint32_t SD_SFF_BT_LAST=4000;
00213     uint32_t SD_HK_ARCH_FIRST=4001;
00214     uint32_t SD_HK_ARCH_LAST= 5000;
00215     uint32_t LOG_FIRST =5001;
00216     uint32_t LOG_LAST=6000;
00217     uint32_t SD_MNG_SECT=7000;
00218     uint32_t block_number;
00219     int result;
00220     if(sid==0x0)
00221     {
00222         block_number=SD_SCP_FIRST + fsc;
00223 //        sd1.printf("read_block_number=%d\r\n",block_number);
00224         result= disk_read(buffer,block_number);
00225     }
00226    else if(sid==0x1)
00227     {
00228         block_number=SD_SFF_AT_FIRST + fsc;
00229         result= disk_read(buffer,block_number);
00230     }
00231     else if(sid==0x2)
00232     {
00233         block_number=SD_SFF_BT_FIRST + fsc;
00234         result= disk_read(buffer,block_number);
00235     }
00236     else if(sid==0x3)
00237     {
00238         block_number=SD_HK_ARCH_FIRST + fsc;
00239         result= disk_read(buffer,block_number);
00240     }
00241     else if(sid==0x4)
00242     {
00243         block_number=LOG_FIRST +fsc;
00244         result= disk_read(buffer,block_number);
00245     }
00246     else
00247     {
00248         return 1;
00249     }
00250     return result;
00251 }
00252 
00253 
00254 int initialise_card()
00255 {
00256     // Set to 100kHz for initialisation, and clock card with cs_sd = 1
00257     spi.frequency(100000);           // changed on 31 12 2015 to 1 MHz 
00258     cs_sd = 1;
00259     for (int i = 0; i < 16; i++) {
00260         spi.write(0xFF);
00261     }
00262 
00263     // send CMD0, should return with all zeros except IDLE STATE set (bit 0)
00264     if (cmd(0, 0) != R1_IDLE_STATE) {
00265         debug("No disk, or could not put SD card in to spi idle state\r\n");
00266         return SDCARD_FAIL;
00267     }
00268 
00269 // send CMD8 to determine whther it is ver 2.x
00270     int r = cmd8();
00271     if (r == R1_IDLE_STATE) {
00272         printf("\rEntering v2\r\n");
00273         return initialise_card_v2();
00274 
00275     } else if (r == (R1_IDLE_STATE | R1_ILLEGAL_COMMAND)) {
00276         printf("\rEntering v1\r\n");
00277         return initialise_card_v1();
00278 
00279     } else {
00280         debug("\rNot in idle state after sending CMD8 (not an SD card?)\r\n");
00281         return SDCARD_FAIL;
00282     }
00283 }
00284 
00285 int initialise_card_v1()
00286 {
00287     for (int i = 0; i < SD_COMMAND_TIMEOUT; i++) {
00288         cmd(55, 0);
00289         if (cmd(41, 0) == 0) {
00290             printf("\rv1 initialization successfull\r\n");
00291             cdv = 512;
00292             debug_if(SD_DBG, "\n\rInit: SEDCARD_V1\n\r");
00293 
00294             return SDCARD_V1;
00295         }
00296     }
00297 
00298     debug("\rTimeout waiting for v1.x card\r\n");
00299     return SDCARD_FAIL;
00300 }
00301 
00302 
00303 int initialise_card_v2()
00304 {
00305     for (int i = 0; i < SD_COMMAND_TIMEOUT; i++) {
00306         wait_ms(50);
00307         cmd58();
00308         cmd(55, 0);
00309         if (cmd(41, 0x40000000) == 0) {
00310             printf("\rv2 initialization successfull\r\n");
00311             cmd58();
00312             debug_if(SD_DBG, "\n\rInit: SDCARD_V2\n\r");
00313             cdv = 1;
00314 
00315             return SDCARD_V2;
00316         }
00317     }
00318 
00319     debug("\rTimeout waiting for v2.x card\r\n");
00320     return SDCARD_FAIL;
00321 }
00322 
00323 int cmd(int cmd, int arg)
00324 {
00325     cs_sd = 0;
00326 
00327     // send a command
00328     spi.write(0x40 | cmd);
00329     spi.write(arg >> 24);
00330     spi.write(arg >> 16);
00331     spi.write(arg >> 8);
00332     spi.write(arg >> 0);
00333     spi.write(0x95);
00334 
00335     // wait for the repsonse (response[7] == 0)
00336     for (int i = 0; i < SD_COMMAND_TIMEOUT; i++) {
00337         int response = spi.write(0xFF);
00338         if (!(response & 0x80)) {
00339             cs_sd = 1;
00340             spi.write(0xFF);
00341             return response;
00342         }
00343     }
00344     cs_sd = 1;
00345     spi.write(0xFF);
00346     return -1; // timeout
00347 }
00348 
00349 
00350 int cmd58()
00351 {
00352     cs_sd = 0;
00353     int arg = 0;
00354 
00355     // send a command
00356     spi.write(0x40 | 58);
00357     spi.write(arg >> 24);
00358     spi.write(arg >> 16);
00359     spi.write(arg >> 8);
00360     spi.write(arg >> 0);
00361     spi.write(0x95);
00362 
00363     // wait for the repsonse (response[7] == 0)
00364     for (int i = 0; i < SD_COMMAND_TIMEOUT; i++) {
00365         int response = spi.write(0xFF);
00366         if (!(response & 0x80)) {
00367             int ocr = spi.write(0xFF) << 24;
00368             ocr |= spi.write(0xFF) << 16;
00369             ocr |= spi.write(0xFF) << 8;
00370             ocr |= spi.write(0xFF) << 0;
00371             cs_sd = 1;
00372             spi.write(0xFF);
00373             return response;
00374         }
00375     }
00376     cs_sd = 1;
00377     spi.write(0xFF);
00378     return -1; // timeout
00379 }
00380 
00381 
00382 int cmd8()
00383 {
00384     cs_sd = 0;
00385 
00386     // send a command
00387     spi.write(0x40 | 8); // CMD8
00388     spi.write(0x00);     // reserved
00389     spi.write(0x00);     // reserved
00390     spi.write(0x01);     // 3.3v
00391     spi.write(0xAA);     // check pattern
00392     spi.write(0x87);     // crc
00393 
00394     // wait for the repsonse (response[7] == 0)
00395     for (int i = 0; i < SD_COMMAND_TIMEOUT * 1000; i++) {
00396         char response[5];
00397         response[0] = spi.write(0xFF);
00398         if (!(response[0] & 0x80)) {
00399             for (int j = 1; j < 5; j++) {
00400                 response[i] = spi.write(0xFF);
00401             }
00402             cs_sd = 1;
00403             spi.write(0xFF);
00404             return response[0];
00405         }
00406     }
00407     cs_sd = 1;
00408     spi.write(0xFF);
00409     return -1; // timeout
00410 }
00411 
00412 uint64_t sd_sectors()
00413 {
00414     uint32_t c_size, c_size_mult, read_bl_len;
00415     uint32_t block_len, mult, blocknr, capacity;
00416     uint32_t hc_c_size;
00417     uint64_t blocks;
00418 
00419     // CMD9, Response R2 (R1 byte + 16-byte block read)
00420     if (cmdx(9, 0) != 0) {
00421         debug("\rDidn't get a response from the disk\n");
00422         return 0;
00423     }
00424 
00425     uint8_t cs_sdd[16];
00426     if (read(cs_sdd, 16) != 0) {
00427         debug("\rCouldn't read cs_sdd response from disk\n");
00428         return 0;
00429     }
00430 
00431     // cs_sdd_structure : cs_sdd[127:126]
00432     // c_size        : cs_sdd[73:62]
00433     // c_size_mult   : cs_sdd[49:47]
00434     // read_bl_len   : cs_sdd[83:80] - the *maximum* read block length
00435 
00436     int cs_sdd_structure = ext_bits(cs_sdd, 127, 126);
00437 
00438     switch (cs_sdd_structure) {
00439         case 0:
00440             cdv = 512;
00441             c_size = ext_bits(cs_sdd, 73, 62);
00442             c_size_mult = ext_bits(cs_sdd, 49, 47);
00443             read_bl_len = ext_bits(cs_sdd, 83, 80);
00444 
00445             block_len = 1 << read_bl_len;
00446             mult = 1 << (c_size_mult + 2);
00447             blocknr = (c_size + 1) * mult;
00448             capacity = blocknr * block_len;
00449             blocks = capacity / 512;
00450             debug_if(SD_DBG, "\n\rSDCard\n\rc_size: %d \n\rcapacity: %ld \n\rsectors: %lld\n\r", c_size, capacity, blocks);
00451             break;
00452 
00453         case 1:
00454             cdv = 1;
00455             hc_c_size = ext_bits(cs_sdd, 63, 48);
00456             blocks = (hc_c_size+1)*1024;
00457             debug_if(SD_DBG, "\n\rSDHC Card \n\rhc_c_size: %d\n\rcapacity: %lld \n\rsectors: %lld\n\r", hc_c_size, blocks*512, blocks);
00458             break;
00459 
00460         default:
00461             debug("cs_sdD struct unsupported\r\n");
00462             return 0;
00463     };
00464     return blocks;
00465 }
00466 
00467 int cmdx(int cmd, int arg)
00468 {
00469     cs_sd = 0;
00470 
00471     // send a command
00472     spi.write(0x40 | cmd);
00473     spi.write(arg >> 24);
00474     spi.write(arg >> 16);
00475     spi.write(arg >> 8);
00476     spi.write(arg >> 0);
00477     spi.write(0x95);
00478 
00479     // wait for the repsonse (response[7] == 0)
00480     for (int i = 0; i < SD_COMMAND_TIMEOUT; i++) {
00481         int response = spi.write(0xFF);
00482         if (!(response & 0x80)) {
00483             return response;
00484         }
00485     }
00486     cs_sd = 1;
00487     spi.write(0xFF);
00488     return -1; // timeout
00489 }
00490 
00491 static uint32_t ext_bits(unsigned char *data, int msb, int lsb)
00492 {
00493     uint32_t bits = 0;
00494     uint32_t size = 1 + msb - lsb;
00495     for (int i = 0; i < size; i++) {
00496         uint32_t position = lsb + i;
00497         uint32_t byte = 15 - (position >> 3);
00498         uint32_t bit = position & 0x7;
00499         uint32_t value = (data[byte] >> bit) & 1;
00500         bits |= value << i;
00501     }
00502     return bits;
00503 }
00504 
00505 int disk_initialize()
00506 {
00507     int i = initialise_card();
00508     debug_if(SD_DBG, "init card = %d\n", i);
00509     sectors = sd_sectors();
00510 
00511     // Set block length to 512 (CMD16)
00512     if (cmd(16, 512) != 0) {
00513         debug("\rSet 512-byte block timed out\r\n");
00514         return 1;
00515     } else {
00516         printf("\rDisk initialization successfull\r\n");
00517     }
00518 
00519     spi.frequency(1000000); // Set to 1MHz for data transfer
00520     return 0;
00521 }
00522 
00523 int disk_write(const uint8_t *buffer, uint64_t block_number)
00524 
00525 {
00526     // set write address for single block (CMD24)
00527     if (cmd(24, block_number * cdv) != 0) {
00528         return 1;
00529     }
00530 
00531     // send the data block
00532     write(buffer, 512);
00533     //printf("Written Successfully bro \n");
00534     return 0;
00535 }
00536 
00537 int write(const uint8_t*buffer, uint32_t length)
00538 {
00539     cs_sd = 0;
00540 
00541     // indicate start of block
00542     spi.write(0xFE);
00543 
00544     // write the data
00545     for (int i = 0; i < length; i++) {
00546         spi.write(buffer[i]);
00547     }
00548 
00549     // write the checksum
00550     spi.write(0xFF);
00551     spi.write(0xFF);
00552 
00553     // check the response token
00554     if ((spi.write(0xFF) & 0x1F) != 0x05) {
00555         cs_sd = 1;
00556         spi.write(0xFF);
00557         return 1;
00558     }
00559 
00560     // wait for write to finish
00561     while (spi.write(0xFF) == 0);
00562 
00563     cs_sd = 1;
00564     spi.write(0xFF);
00565     return 0;
00566 }
00567 
00568 int disk_read(uint8_t *buffer, uint64_t block_number)
00569 {
00570     // set read address for single block (CMD17)
00571     if (cmd(17, block_number * cdv) != 0) {
00572         return 1;
00573     }
00574 
00575     // receive the data
00576     read(buffer, 512);
00577     return 0;
00578 }
00579 
00580 int read(uint8_t *buffer, uint32_t length)
00581 {
00582     cs_sd = 0;
00583 
00584     // read until start byte (0xFF)
00585     while (spi.write(0xFF) != 0xFE);
00586 
00587     // read data
00588     for (int i = 0; i < length; i++) {
00589         buffer[i] = spi.write(0xFF);
00590     }
00591     spi.write(0xFF); // checksum
00592     spi.write(0xFF);
00593 
00594     cs_sd = 1;
00595     spi.write(0xFF);
00596     return 0;
00597 }
00598 
00599 int disk_erase(int startBlock, int totalBlocks)
00600 {
00601     if(cmd(32, startBlock * cdv) != 0) {
00602         return 1;
00603     }
00604     if (cmd(33, (startBlock+totalBlocks-1) * cdv) != 0) {
00605         return 1;
00606     }
00607     if (cmd(38,0) != 0) {
00608         return 1;
00609     }
00610     
00611     return 0; //normal return
00612 }