Dummy program to demonstrate problems: working code

Dependencies:   SLCD mbed-rtos mbed

Fork of MNG_TC by Shreesha S

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SDCard_initionalisation.h Source File

SDCard_initionalisation.h

00001 
00002 //Serial PC(USBTX, USBRX);
00003 DigitalOut cs_SDCard(D9);
00004 
00005 #define SD_COMMAND_TIMEOUT 5000
00006 
00007 #define SD_DBG             0
00008 
00009 #define R1_IDLE_STATE           (1 << 0)
00010 #define R1_ERASE_RESET          (1 << 1)
00011 #define R1_ILLEGAL_COMMAND      (1 << 2)
00012 #define R1_COM_CRC_ERROR        (1 << 3)
00013 #define R1_ERASE_SEQUENCE_ERROR (1 << 4)
00014 #define R1_ADDRESS_ERROR        (1 << 5)
00015 #define R1_PARAMETER_ERROR      (1 << 6)
00016 //SPI spi(PTA16, PTA17, PTA15); // mosi, miso, sclk
00017 
00018 
00019 int initialise_card();
00020 int initialise_card_v1();
00021 int initialise_card_v2();
00022 int disk_initialize();
00023 //int disk_write(const uint8_t *, uint64_t);
00024 
00025 int disk_erase(int,int);
00026 
00027 uint64_t sd_sectors();
00028 uint64_t sectors;
00029 
00030 int cmd(int, int);
00031 int cmd58();
00032 int cmdx(int, int);
00033 int cmd8();
00034 int read(uint8_t*, uint32_t );
00035 int write(const uint8_t*, uint32_t );
00036 static uint32_t ext_bits(unsigned char *, int , int );
00037 
00038 int cdv;
00039 
00040 #define SDCARD_FAIL 0
00041 #define SDCARD_V1   1
00042 #define SDCARD_V2   2
00043 #define SDCARD_V2HC 3
00044 
00045 int count_bro;
00046 int i;
00047 int random[1000];
00048 
00049 
00050 void initialisation_SDCard(){ 
00051     cs_adf =1;
00052 //    start_block_num =10 ;           // Read from TC
00053 //    end_block_num =Science_TMframe::SDC_address ;            // Read from TC    
00054     printf("welcome\n");
00055     initialise_card();
00056     int result= initialise_card();
00057     printf("initialise card result=%d\n",result);
00058     disk_initialize();
00059    
00060 }
00061 
00062 
00063 
00064 
00065 int initialise_card()
00066 {
00067     // Set to 100kHz for initialisation, and clock card with cs_SDCard = 1
00068     spi.frequency(100000);
00069     cs_SDCard = 1;
00070     for (int i = 0; i < 16; i++) {
00071         spi.write(0xFF);
00072     }
00073 
00074     // send CMD0, should return with all zeros except IDLE STATE set (bit 0)
00075     if (cmd(0, 0) != R1_IDLE_STATE) {
00076         debug("No disk, or could not put SD card in to SPI idle state\n");
00077         return SDCARD_FAIL;
00078     }
00079 
00080 // send CMD8 to determine whther it is ver 2.x
00081     int r = cmd8();
00082     if (r == R1_IDLE_STATE) {
00083         printf("Entering v2 bro\n");
00084         return initialise_card_v2();
00085 
00086     } else if (r == (R1_IDLE_STATE | R1_ILLEGAL_COMMAND)) {
00087         printf("Entering v1 bro\n");
00088         return initialise_card_v1();
00089 
00090     } else {
00091         debug("Not in idle state after sending CMD8 (not an SD card?)\n");
00092         return SDCARD_FAIL;
00093     }
00094 }
00095 
00096 int initialise_card_v1()
00097 {
00098     for (int i = 0; i < SD_COMMAND_TIMEOUT; i++) {
00099         cmd(55, 0);
00100         if (cmd(41, 0) == 0) {
00101             printf("Yuppie v1 successful\n");
00102             cdv = 512;
00103             debug_if(SD_DBG, "\n\rInit: SEDCARD_V1\n\r");
00104 
00105             return SDCARD_V1;
00106         }
00107     }
00108 
00109     debug("Timeout waiting for v1.x card\n");
00110     return SDCARD_FAIL;
00111 }
00112 
00113 
00114 int initialise_card_v2()
00115 {
00116     for (int i = 0; i < SD_COMMAND_TIMEOUT; i++) {
00117         wait_ms(50);
00118         cmd58();
00119         cmd(55, 0);
00120         if (cmd(41, 0x40000000) == 0) {
00121             printf("Yuppie,v2 successful\n");
00122             cmd58();
00123             debug_if(SD_DBG, "\n\rInit: SDCARD_V2\n\r");
00124             cdv = 1;
00125 
00126             return SDCARD_V2;
00127         }
00128     }
00129 
00130     debug("Timeout waiting for v2.x card\n");
00131     return SDCARD_FAIL;
00132 }
00133 
00134 int cmd(int cmd, int arg)
00135 {
00136     cs_SDCard = 0;
00137 
00138     // send a command
00139     spi.write(0x40 | cmd);
00140     spi.write(arg >> 24);
00141     spi.write(arg >> 16);
00142     spi.write(arg >> 8);
00143     spi.write(arg >> 0);
00144     spi.write(0x95);
00145 
00146     // wait for the repsonse (response[7] == 0)
00147     for (int i = 0; i < SD_COMMAND_TIMEOUT; i++) {
00148         int response = spi.write(0xFF);
00149         if (!(response & 0x80)) {
00150             cs_SDCard = 1;
00151             spi.write(0xFF);
00152             return response;
00153         }
00154     }
00155     cs_SDCard = 1;
00156     spi.write(0xFF);
00157     return -1; // timeout
00158 }
00159 
00160 
00161 int cmd58()
00162 {
00163     cs_SDCard = 0;
00164     int arg = 0;
00165 
00166     // send a command
00167     spi.write(0x40 | 58);
00168     spi.write(arg >> 24);
00169     spi.write(arg >> 16);
00170     spi.write(arg >> 8);
00171     spi.write(arg >> 0);
00172     spi.write(0x95);
00173 
00174     // wait for the repsonse (response[7] == 0)
00175     for (int i = 0; i < SD_COMMAND_TIMEOUT; i++) {
00176         int response = spi.write(0xFF);
00177         if (!(response & 0x80)) {
00178             int ocr = spi.write(0xFF) << 24;
00179             ocr |= spi.write(0xFF) << 16;
00180             ocr |= spi.write(0xFF) << 8;
00181             ocr |= spi.write(0xFF) << 0;
00182             cs_SDCard = 1;
00183             spi.write(0xFF);
00184             return response;
00185         }
00186     }
00187     cs_SDCard = 1;
00188     spi.write(0xFF);
00189     return -1; // timeout
00190 }
00191 
00192 
00193 int cmd8()
00194 {
00195     cs_SDCard = 0;
00196 
00197     // send a command
00198     spi.write(0x40 | 8); // CMD8
00199     spi.write(0x00);     // reserved
00200     spi.write(0x00);     // reserved
00201     spi.write(0x01);     // 3.3v
00202     spi.write(0xAA);     // check pattern
00203     spi.write(0x87);     // crc
00204 
00205     // wait for the repsonse (response[7] == 0)
00206     for (int i = 0; i < SD_COMMAND_TIMEOUT * 1000; i++) {
00207         char response[5];
00208         response[0] = spi.write(0xFF);
00209         if (!(response[0] & 0x80)) {
00210             for (int j = 1; j < 5; j++) {
00211                 response[i] = spi.write(0xFF);
00212             }
00213             cs_SDCard = 1;
00214             spi.write(0xFF);
00215             return response[0];
00216         }
00217     }
00218     cs_SDCard = 1;
00219     spi.write(0xFF);
00220     return -1; // timeout
00221 }
00222 
00223 uint64_t sd_sectors()
00224 {
00225     uint32_t c_size, c_size_mult, read_bl_len;
00226     uint32_t block_len, mult, blocknr, capacity;
00227     uint32_t hc_c_size;
00228     uint64_t blocks;
00229 
00230     // CMD9, Response R2 (R1 byte + 16-byte block read)
00231     if (cmdx(9, 0) != 0) {
00232         debug("Didn't get a response from the disk\n");
00233         return 0;
00234     }
00235 
00236     uint8_t csd[16];
00237     if (read(csd, 16) != 0) {
00238         debug("Couldn't read csd response from disk\n");
00239         return 0;
00240     }
00241 
00242     // csd_structure : csd[127:126]
00243     // c_size        : csd[73:62]
00244     // c_size_mult   : csd[49:47]
00245     // read_bl_len   : csd[83:80] - the *maximum* read block length
00246 
00247     int csd_structure = ext_bits(csd, 127, 126);
00248 
00249     switch (csd_structure) {
00250         case 0:
00251             cdv = 512;
00252             c_size = ext_bits(csd, 73, 62);
00253             c_size_mult = ext_bits(csd, 49, 47);
00254             read_bl_len = ext_bits(csd, 83, 80);
00255 
00256             block_len = 1 << read_bl_len;
00257             mult = 1 << (c_size_mult + 2);
00258             blocknr = (c_size + 1) * mult;
00259             capacity = blocknr * block_len;
00260             blocks = capacity / 512;
00261             debug_if(SD_DBG, "\n\rSDCard\n\rc_size: %d \n\rcapacity: %ld \n\rsectors: %lld\n\r", c_size, capacity, blocks);
00262             break;
00263 
00264         case 1:
00265             cdv = 1;
00266             hc_c_size = ext_bits(csd, 63, 48);
00267             blocks = (hc_c_size+1)*1024;
00268             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);
00269             break;
00270 
00271         default:
00272             debug("CSD struct unsupported\r\n");
00273             return 0;
00274     };
00275     return blocks;
00276 }
00277 
00278 int cmdx(int cmd, int arg)
00279 {
00280     cs_SDCard = 0;
00281 
00282     // send a command
00283     spi.write(0x40 | cmd);
00284     spi.write(arg >> 24);
00285     spi.write(arg >> 16);
00286     spi.write(arg >> 8);
00287     spi.write(arg >> 0);
00288     spi.write(0x95);
00289 
00290     // wait for the repsonse (response[7] == 0)
00291     for (int i = 0; i < SD_COMMAND_TIMEOUT; i++) {
00292         int response = spi.write(0xFF);
00293         if (!(response & 0x80)) {
00294             return response;
00295         }
00296     }
00297     cs_SDCard = 1;
00298     spi.write(0xFF);
00299     return -1; // timeout
00300 }
00301 
00302 static uint32_t ext_bits(unsigned char *data, int msb, int lsb)
00303 {
00304     uint32_t bits = 0;
00305     uint32_t size = 1 + msb - lsb;
00306     for (int i = 0; i < size; i++) {
00307         uint32_t position = lsb + i;
00308         uint32_t byte = 15 - (position >> 3);
00309         uint32_t bit = position & 0x7;
00310         uint32_t value = (data[byte] >> bit) & 1;
00311         bits |= value << i;
00312     }
00313     return bits;
00314 }
00315 
00316 int disk_initialize()
00317 {
00318     int i = initialise_card();
00319     debug_if(SD_DBG, "init card = %d\n", i);
00320     sectors = sd_sectors();
00321 
00322     // Set block length to 512 (CMD16)
00323     if (cmd(16, 512) != 0) {
00324         debug("Set 512-byte block timed out\n");
00325         return 1;
00326     } else {
00327         printf("Hey,block init succesful\n");
00328     }
00329 
00330     spi.frequency(1000000); // Set to 1MHz for data transfer
00331     return 0;
00332 }
00333 
00334 int disk_write(const uint8_t *buffer, uint64_t block_number)
00335 
00336 {
00337     // set write address for single block (CMD24)
00338     if (cmd(24, block_number * cdv) != 0) {
00339         return 1;
00340     }
00341 
00342     // send the data block
00343     write(buffer, 512);
00344     //printf("Written Successfully bro \n");
00345     return 0;
00346 }
00347 
00348 int write(const uint8_t*buffer, uint32_t length)
00349 {
00350     cs_SDCard = 0;
00351 
00352     // indicate start of block
00353     spi.write(0xFE);
00354 
00355     // write the data
00356     for (int i = 0; i < length; i++) {
00357         spi.write(buffer[i]);
00358     }
00359 
00360     // write the checksum
00361     spi.write(0xFF);
00362     spi.write(0xFF);
00363 
00364     // check the response token
00365     if ((spi.write(0xFF) & 0x1F) != 0x05) {
00366         cs_SDCard = 1;
00367         spi.write(0xFF);
00368         return 1;
00369     }
00370 
00371     // wait for write to finish
00372     while (spi.write(0xFF) == 0);
00373 
00374     cs_SDCard = 1;
00375     spi.write(0xFF);
00376     return 0;
00377 }
00378 
00379 int disk_read(uint8_t *buffer, uint64_t block_number)
00380 {
00381     // set read address for single block (CMD17)
00382     if (cmd(17, block_number * cdv) != 0) {
00383         return 1;
00384     }
00385 
00386     // receive the data
00387     read(buffer, 512);
00388     return 0;
00389 }
00390 
00391 int read(uint8_t *buffer, uint32_t length)
00392 {
00393     cs_SDCard = 0;
00394 
00395     // read until start byte (0xFF)
00396     while (spi.write(0xFF) != 0xFE);
00397 
00398     // read data
00399     for (int i = 0; i < length; i++) {
00400         buffer[i] = spi.write(0xFF);
00401     }
00402     spi.write(0xFF); // checksum
00403     spi.write(0xFF);
00404 
00405     cs_SDCard = 1;
00406     spi.write(0xFF);
00407     return 0;
00408 }
00409 
00410 int disk_erase(int startBlock, int totalBlocks)
00411 {
00412     if(cmd(32, startBlock * cdv) != 0) {
00413         return 1;
00414     }
00415     if (cmd(33, (startBlock+totalBlocks-1) * cdv) != 0) {
00416         return 1;
00417     }
00418     if (cmd(38,0) != 0) {
00419         return 1;
00420     }
00421     
00422     return 0; //normal return
00423 }