FM

Dependencies:   SimpleDMA eeprom mbed-rtos mbed FreescaleIAP

Fork of CDMS_CODE by shubham c

Committer:
chaithanyarss
Date:
Wed Jan 04 10:55:22 2017 +0000
Revision:
343:cd7b1734f7a0
Parent:
328:2242ebc71be8
Testing EEPROM

Who changed what in which revision?

UserRevisionLine numberNew contents of line
chaithanyarss 328:2242ebc71be8 1 #ifndef CDMS_SD_INLCUDED
chaithanyarss 328:2242ebc71be8 2 #define CDMS_SD_INLCUDED
chaithanyarss 328:2242ebc71be8 3
chaithanyarss 327:5a967a66c10f 4 //SPI spi(PTE1, PTE3, PTE2); // MOSI,MISO, CLOCK microcontroller(in order)
ee12b079 86:a26f5f22631d 5 //DigitalOut cs_sd(PTE22);
ee12b079 86:a26f5f22631d 6
ee12b079 86:a26f5f22631d 7 //Serial sd1(USBTX,USBRX);
ee12b079 86:a26f5f22631d 8
ee12b079 86:a26f5f22631d 9
chaithanyarss 261:1e54415b34d3 10 #define SD_COMMAND_TIMEOUT 325
ee12b079 86:a26f5f22631d 11
chaithanyarss 328:2242ebc71be8 12 #define SD_DBG 1
ee12b079 86:a26f5f22631d 13
ee12b079 86:a26f5f22631d 14 #define R1_IDLE_STATE (1 << 0)
ee12b079 86:a26f5f22631d 15 #define R1_ERASE_RESET (1 << 1)
ee12b079 86:a26f5f22631d 16 #define R1_ILLEGAL_COMMAND (1 << 2)
ee12b079 86:a26f5f22631d 17 #define R1_COM_CRC_ERROR (1 << 3)
ee12b079 86:a26f5f22631d 18 #define R1_ERASE_SEQUENCE_ERROR (1 << 4)
ee12b079 86:a26f5f22631d 19 #define R1_ADDRESS_ERROR (1 << 5)
ee12b079 86:a26f5f22631d 20 #define R1_PARAMETER_ERROR (1 << 6)
ee12b079 86:a26f5f22631d 21
chaithanyarss 327:5a967a66c10f 22 uint32_t SD_DATABLOCK_START[] = {0, 1001, 11001, 21001, 31001, 41001};
chaithanyarss 327:5a967a66c10f 23 uint32_t SD_DATABLOCK_END[] = {0, 11000, 21000, 31000, 41000, 51000};
chaithanyarss 327:5a967a66c10f 24 uint32_t SD_MNG_SECT=80000;
ee12b079 86:a26f5f22631d 25
ee12b079 194:ab528911780d 26 extern uint8_t SD_INIT_FLAGS;
ee12b079 86:a26f5f22631d 27
ee12b079 86:a26f5f22631d 28 int initialise_card();
ee12b079 86:a26f5f22631d 29 int initialise_card_v1();
ee12b079 86:a26f5f22631d 30 int initialise_card_v2();
ee12b079 86:a26f5f22631d 31 int disk_write(const uint8_t *, uint64_t);
ee12b079 86:a26f5f22631d 32 int disk_read(uint8_t *, uint64_t);
ee12b079 86:a26f5f22631d 33 int disk_erase(int,int);
ee12b079 209:63e9c8f8b5d2 34 int disk_read_statusbits(uint8_t *);
ee12b079 86:a26f5f22631d 35
chaithanyarss 326:e424f70b86c0 36 void FCTN_SD_MNGR();
ee12b079 194:ab528911780d 37 int INCREMENT_SD_LIB(uint8_t);
ee12b079 86:a26f5f22631d 38
ee12b079 86:a26f5f22631d 39
ee12b079 86:a26f5f22631d 40 int cmd(int, int);
ee12b079 86:a26f5f22631d 41 int cmd58();
ee12b079 86:a26f5f22631d 42 int cmdx(int, int);
ee12b079 86:a26f5f22631d 43 int cmd8();
ee12b079 86:a26f5f22631d 44 int read(uint8_t*, uint32_t );
ee12b079 86:a26f5f22631d 45 int write(const uint8_t*, uint32_t );
ee12b079 86:a26f5f22631d 46 static uint32_t ext_bits(unsigned char *, int , int );
ee12b079 92:bce22b38c440 47 int SD_WRITE(uint8_t*,uint32_t,uint8_t);
ee12b079 194:ab528911780d 48 int FCTN_CDMS_SD_INIT();
ee12b079 195:6a549c0e9287 49 uint8_t SD_READ(uint8_t*,uint32_t,uint8_t);
chaithanyarss 261:1e54415b34d3 50 #define SDCARD_FAIL 4
ee12b079 86:a26f5f22631d 51 #define SDCARD_V1 1
ee12b079 86:a26f5f22631d 52 #define SDCARD_V2 2
ee12b079 86:a26f5f22631d 53 #define SDCARD_V2HC 3
ee12b079 86:a26f5f22631d 54
ee12b079 86:a26f5f22631d 55 int cdv;
ee12b079 86:a26f5f22631d 56 uint64_t sd_sectors();
ee12b079 86:a26f5f22631d 57 uint64_t sectors;
ee12b079 86:a26f5f22631d 58
ee12b079 194:ab528911780d 59 int FCTN_CDMS_SD_INIT()
chaithanyarss 327:5a967a66c10f 60 {
ee12b079 194:ab528911780d 61 int i = initialise_card();
chaithanyarss 261:1e54415b34d3 62 if( i == 4)
chaithanyarss 327:5a967a66c10f 63 return 4;
ee12b079 194:ab528911780d 64 debug_if(SD_DBG, "init card = %d\n", i);
ee12b079 194:ab528911780d 65 sectors = sd_sectors();
ee12b079 194:ab528911780d 66
ee12b079 194:ab528911780d 67 // Set block length to 512 (CMD16)
ee12b079 194:ab528911780d 68 if (cmd(16, 512) != 0) {
ee12b079 194:ab528911780d 69 debug("\rSet 512-byte block timed out\r\n");
ee12b079 194:ab528911780d 70 return 1;
ee12b079 194:ab528911780d 71 } else {
chaithanyarss 327:5a967a66c10f 72 //printf("\rDisk initialization successfull\r\n");
ee12b079 194:ab528911780d 73 }
chaithanyarss 261:1e54415b34d3 74 SD_STATUS = DEVICE_POWERED;
ee12b079 194:ab528911780d 75 spi.frequency(1000000); // Set to 1MHz for data transfer
ee12b079 194:ab528911780d 76 return 0;
ee12b079 86:a26f5f22631d 77 }
ee12b079 86:a26f5f22631d 78
ee12b079 209:63e9c8f8b5d2 79 void FCTN_SD_MNGR()
ee12b079 86:a26f5f22631d 80 {
ee12b079 92:bce22b38c440 81 uint32_t fsc;
chaithanyarss 327:5a967a66c10f 82 uint32_t fsc_old;
ee12b079 92:bce22b38c440 83 uint8_t buffer[512];
chaithanyarss 261:1e54415b34d3 84 int b;
chaithanyarss 327:5a967a66c10f 85 if(SD_STATUS == DEVICE_POWERED) {
chaithanyarss 327:5a967a66c10f 86 b=disk_read(buffer, SD_MNG_SECT);
ee12b079 195:6a549c0e9287 87
chaithanyarss 327:5a967a66c10f 88 for(int i = 0; i < 5; i++ ) {
chaithanyarss 327:5a967a66c10f 89 fsc= (uint32_t)(buffer[0 + i*8]<<24)+(uint32_t)(buffer[1 + i*8]<<16)+(uint32_t)(buffer[2 + i*8]<<8)+(uint32_t)buffer[3 + i*8];
chaithanyarss 327:5a967a66c10f 90 fsc_old= (uint32_t)(buffer[4 + i*8]<<24)+(uint32_t)(buffer[5 + i*8]<<16)+(uint32_t)(buffer[6 + i*8]<<8)+(uint32_t)buffer[7 + i*8];
chaithanyarss 327:5a967a66c10f 91 FSC_CURRENT[1 + i] = fsc;
chaithanyarss 327:5a967a66c10f 92 FSC_OLD[1 + i] = fsc_old;
chaithanyarss 327:5a967a66c10f 93 }
chaithanyarss 261:1e54415b34d3 94 }
ee12b079 194:ab528911780d 95 }
ee12b079 194:ab528911780d 96
ee12b079 194:ab528911780d 97 int INCREMENT_SD_LIB(uint8_t sid)
ee12b079 194:ab528911780d 98 {
ee12b079 194:ab528911780d 99 uint32_t fsc;
chaithanyarss 327:5a967a66c10f 100 uint32_t fsc_old;
ee12b079 209:63e9c8f8b5d2 101 int i;
ee12b079 194:ab528911780d 102 uint8_t buffer[512];
chaithanyarss 327:5a967a66c10f 103
spacelab 308:7c3872365d58 104 disk_read(buffer,SD_MNG_SECT);
ee12b079 209:63e9c8f8b5d2 105 SD_MNG_SECT += SD_LIB_WRITES/(int)0xFFFF;
chaithanyarss 327:5a967a66c10f 106 if(SD_MNG_SECT != SD_LIB_BLK_CURRENT) {
chaithanyarss 324:c0a5228cc666 107 SD_LIB_BLK_CURRENT = SD_MNG_SECT;
chaithanyarss 343:cd7b1734f7a0 108 WRITE_TO_EEPROM(16, (int32_t)SD_LIB_BLK_CURRENT);
chaithanyarss 343:cd7b1734f7a0 109 //FCTN_CDMS_WR_FLASH(16,SD_LIB_BLK_CURRENT);
chaithanyarss 324:c0a5228cc666 110 }
ee12b079 209:63e9c8f8b5d2 111 SD_LIB_WRITES = SD_LIB_WRITES%(int)0xFFFF;
spacelab 308:7c3872365d58 112 disk_write(buffer,SD_MNG_SECT);
chaithanyarss 327:5a967a66c10f 113
ee12b079 194:ab528911780d 114 disk_read(buffer,SD_MNG_SECT);
chaithanyarss 328:2242ebc71be8 115 if(sid>=0x01 && sid <=0x05)
chaithanyarss 328:2242ebc71be8 116 {
chaithanyarss 328:2242ebc71be8 117 fsc=(uint32_t)(buffer[0 + ((sid-1)*8)]<<24)+(uint32_t)(buffer[1 + ((sid-1)*8)]<<16)+(uint32_t)(buffer[2 + ((sid-1)*8)]<<8)+(uint32_t)buffer[3 + ((sid-1)*8)];
chaithanyarss 328:2242ebc71be8 118 fsc_old=(uint32_t)(buffer[4 + ((sid-1)*8)]<<24)+(uint32_t)(buffer[5 + ((sid-1)*8)]<<16)+(uint32_t)(buffer[6 + ((sid-1)*8)]<<8)+(uint32_t)buffer[7 + ((sid-1)*8)];
ee12b079 203:424308159a56 119 fsc++;
chaithanyarss 328:2242ebc71be8 120 buffer[0 + ((sid-1)*8)]=(uint8_t) (fsc>>24 & 0xFF);
chaithanyarss 328:2242ebc71be8 121 buffer[1 + ((sid-1)*8)]=(uint8_t) (fsc>>16 & 0xFF);
chaithanyarss 328:2242ebc71be8 122 buffer[2 + ((sid-1)*8)]=(uint8_t) (fsc>>8 & 0xFF);
chaithanyarss 328:2242ebc71be8 123 buffer[3 + ((sid-1)*8)]=(uint8_t) (fsc & 0xFF);
chaithanyarss 328:2242ebc71be8 124 if(fsc > SD_DATABLOCK_END[sid]-SD_DATABLOCK_START[sid]+1) {
chaithanyarss 327:5a967a66c10f 125 fsc_old = fsc_old+1;
chaithanyarss 328:2242ebc71be8 126 buffer[4 + ((sid-1)*8)]=(uint8_t) (fsc_old>>24 & 0xFF);
chaithanyarss 328:2242ebc71be8 127 buffer[5 + ((sid-1)*8)]=(uint8_t) (fsc_old>>16 & 0xFF);
chaithanyarss 328:2242ebc71be8 128 buffer[6 + ((sid-1)*8)]=(uint8_t) (fsc_old>>8 & 0xFF);
chaithanyarss 328:2242ebc71be8 129 buffer[7 + ((sid-1)*8)]=(uint8_t) (fsc_old & 0xFF);
ee12b079 194:ab528911780d 130 }
chaithanyarss 327:5a967a66c10f 131
ee12b079 203:424308159a56 132 i = disk_write(buffer,SD_MNG_SECT);
chaithanyarss 327:5a967a66c10f 133 if(i == 0) {
chaithanyarss 328:2242ebc71be8 134 FSC_CURRENT[sid] = fsc;
chaithanyarss 328:2242ebc71be8 135 FSC_OLD[sid] = fsc_old;
ee12b079 209:63e9c8f8b5d2 136 return i;
ee12b079 209:63e9c8f8b5d2 137 }
ee12b079 194:ab528911780d 138 }
ee12b079 194:ab528911780d 139 return -1;
ee12b079 86:a26f5f22631d 140 }
ee12b079 86:a26f5f22631d 141
ee12b079 86:a26f5f22631d 142
ee12b079 92:bce22b38c440 143 int SD_WRITE(uint8_t* buffer,uint32_t fsc,uint8_t sid)
ee12b079 86:a26f5f22631d 144 {
spacelab 308:7c3872365d58 145 uint32_t block_number;
ee12b079 194:ab528911780d 146 int result = 10;
chaithanyarss 327:5a967a66c10f 147 if(SD_STATUS == DEVICE_POWERED) {
chaithanyarss 327:5a967a66c10f 148 if(sid==0x01) {
chaithanyarss 327:5a967a66c10f 149 block_number=SD_DATABLOCK_START[1]+(fsc%(SD_DATABLOCK_END[1]-SD_DATABLOCK_START[1]+1));
chaithanyarss 327:5a967a66c10f 150 //block_number=SD_DATABLOCK_START[1]+fsc;
chaithanyarss 327:5a967a66c10f 151 result= disk_write(buffer,block_number);
chaithanyarss 327:5a967a66c10f 152 if(result == 0) {
chaithanyarss 327:5a967a66c10f 153 if(INCREMENT_SD_LIB(sid) == 0)
chaithanyarss 327:5a967a66c10f 154 SD_LIB_WRITES++;
chaithanyarss 327:5a967a66c10f 155 }
chaithanyarss 327:5a967a66c10f 156 return result;
chaithanyarss 327:5a967a66c10f 157 }
chaithanyarss 327:5a967a66c10f 158 if(sid==0x02) {
chaithanyarss 327:5a967a66c10f 159 block_number= SD_DATABLOCK_START[2]+(fsc%(SD_DATABLOCK_END[2] - SD_DATABLOCK_START[2]+1));
chaithanyarss 327:5a967a66c10f 160 //block_number= SD_DATABLOCK_START[2]+fsc;
chaithanyarss 327:5a967a66c10f 161 result= disk_write(buffer,block_number);
chaithanyarss 327:5a967a66c10f 162 if(result == 0) {
chaithanyarss 327:5a967a66c10f 163 if(INCREMENT_SD_LIB(sid) == 0)
chaithanyarss 327:5a967a66c10f 164 SD_LIB_WRITES++;
chaithanyarss 327:5a967a66c10f 165 }
chaithanyarss 327:5a967a66c10f 166 return result;
ee12b079 209:63e9c8f8b5d2 167 }
chaithanyarss 327:5a967a66c10f 168 if(sid==0x03) {
chaithanyarss 327:5a967a66c10f 169 block_number= SD_DATABLOCK_START[3] +(fsc%(SD_DATABLOCK_END[3] - SD_DATABLOCK_START[3] +1));
chaithanyarss 327:5a967a66c10f 170 //block_number= SD_DATABLOCK_START[3] +fsc;
chaithanyarss 327:5a967a66c10f 171 result= disk_write(buffer,block_number);
chaithanyarss 327:5a967a66c10f 172 if(result == 0) {
chaithanyarss 327:5a967a66c10f 173 if(INCREMENT_SD_LIB(sid) == 0)
chaithanyarss 327:5a967a66c10f 174 SD_LIB_WRITES++;
chaithanyarss 327:5a967a66c10f 175 }
chaithanyarss 327:5a967a66c10f 176 return result;
ee12b079 209:63e9c8f8b5d2 177 }
chaithanyarss 327:5a967a66c10f 178 if(sid==0x04) {
chaithanyarss 327:5a967a66c10f 179 block_number=SD_DATABLOCK_START[4] +(fsc%(SD_DATABLOCK_END[4] - SD_DATABLOCK_START[4] +1));
chaithanyarss 327:5a967a66c10f 180 //block_number=SD_DATABLOCK_START[4] +fsc;
chaithanyarss 327:5a967a66c10f 181 result= disk_write(buffer,block_number);
chaithanyarss 327:5a967a66c10f 182 if(result == 0) {
chaithanyarss 327:5a967a66c10f 183 if(INCREMENT_SD_LIB(sid) == 0)
chaithanyarss 327:5a967a66c10f 184 SD_LIB_WRITES++;
chaithanyarss 327:5a967a66c10f 185 }
chaithanyarss 327:5a967a66c10f 186 return result;
chaithanyarss 327:5a967a66c10f 187 }
chaithanyarss 327:5a967a66c10f 188 if(sid==0x05) {
chaithanyarss 327:5a967a66c10f 189 block_number= SD_DATABLOCK_START[5] +(fsc%(SD_DATABLOCK_START[5] - SD_DATABLOCK_START[5] +1));
chaithanyarss 327:5a967a66c10f 190 //block_number= SD_DATABLOCK_START[5] +fsc;
chaithanyarss 327:5a967a66c10f 191 result= disk_write(buffer,block_number);
chaithanyarss 327:5a967a66c10f 192 if(result == 0) {
chaithanyarss 327:5a967a66c10f 193 if(INCREMENT_SD_LIB(sid) == 0)
chaithanyarss 327:5a967a66c10f 194 SD_LIB_WRITES++;
chaithanyarss 327:5a967a66c10f 195 }
chaithanyarss 327:5a967a66c10f 196 return result;
chaithanyarss 327:5a967a66c10f 197 }
ee12b079 86:a26f5f22631d 198 }
chaithanyarss 327:5a967a66c10f 199 // return 1;
ee12b079 86:a26f5f22631d 200 }
ee12b079 86:a26f5f22631d 201
ee12b079 195:6a549c0e9287 202 uint8_t SD_READ(uint8_t* buffer,uint32_t fsc,uint8_t sid)
ee12b079 86:a26f5f22631d 203 {
spacelab 308:7c3872365d58 204 FCTN_SD_MNGR();
ee12b079 209:63e9c8f8b5d2 205 uint32_t block_number;
ee12b079 209:63e9c8f8b5d2 206 int result;
ee12b079 295:699801854b71 207 //if(SD_SW_EN_DS == 1)
ee12b079 295:699801854b71 208 // return 0x89;
chaithanyarss 327:5a967a66c10f 209 if(sid==0x01) {
chaithanyarss 327:5a967a66c10f 210 if(!(FSC_OLD[1]<=fsc && fsc<=FSC_CURRENT[1])) {
ee12b079 195:6a549c0e9287 211 return 0x86;
ee12b079 195:6a549c0e9287 212 }
chaithanyarss 327:5a967a66c10f 213 block_number=SD_DATABLOCK_START[1]+(fsc%(SD_DATABLOCK_END[1]-SD_DATABLOCK_START[1]+1));
ee12b079 86:a26f5f22631d 214 result= disk_read(buffer,block_number);
chaithanyarss 327:5a967a66c10f 215 } else if(sid==0x02) {
chaithanyarss 327:5a967a66c10f 216 if(!(FSC_OLD[2]<=fsc && fsc<=FSC_CURRENT[2])) {
ee12b079 195:6a549c0e9287 217 return 0x86;
ee12b079 195:6a549c0e9287 218 }
chaithanyarss 327:5a967a66c10f 219 block_number= SD_DATABLOCK_START[2]+(fsc%(SD_DATABLOCK_END[2] - SD_DATABLOCK_START[2]+1));
ee12b079 86:a26f5f22631d 220 result= disk_read(buffer,block_number);
chaithanyarss 327:5a967a66c10f 221 } else if(sid==0x03) {
chaithanyarss 327:5a967a66c10f 222 if(!(FSC_OLD[3]<=fsc && fsc<=FSC_CURRENT[3])) {
ee12b079 195:6a549c0e9287 223 return 0x86;
ee12b079 195:6a549c0e9287 224 }
chaithanyarss 327:5a967a66c10f 225 block_number= SD_DATABLOCK_START[3] +(fsc%(SD_DATABLOCK_END[3] - SD_DATABLOCK_START[3] +1));
ee12b079 86:a26f5f22631d 226 result= disk_read(buffer,block_number);
chaithanyarss 327:5a967a66c10f 227 } else if(sid==0x04) {
chaithanyarss 327:5a967a66c10f 228 if(!(FSC_OLD[4]<=fsc && fsc<=FSC_CURRENT[4])) {
ee12b079 195:6a549c0e9287 229 return 0x86;
ee12b079 195:6a549c0e9287 230 }
chaithanyarss 327:5a967a66c10f 231 block_number=SD_DATABLOCK_START[4] +(fsc%(SD_DATABLOCK_END[4] - SD_DATABLOCK_START[4] +1));
ee12b079 86:a26f5f22631d 232 result= disk_read(buffer,block_number);
chaithanyarss 327:5a967a66c10f 233 } else if(sid==0x05) {
chaithanyarss 327:5a967a66c10f 234 if(!(FSC_OLD[5]<=fsc && fsc<=FSC_CURRENT[5])) {
ee12b079 195:6a549c0e9287 235 return 0x86;
ee12b079 195:6a549c0e9287 236 }
chaithanyarss 327:5a967a66c10f 237 block_number= SD_DATABLOCK_START[5] +(fsc%(SD_DATABLOCK_START[5] - SD_DATABLOCK_START[5] +1));
ee12b079 86:a26f5f22631d 238 result= disk_read(buffer,block_number);
chaithanyarss 327:5a967a66c10f 239 } else {
ee12b079 195:6a549c0e9287 240 return 0x02;
ee12b079 86:a26f5f22631d 241 }
ee12b079 195:6a549c0e9287 242 if(result == 0)
ee12b079 195:6a549c0e9287 243 return 0xA0;
ee12b079 195:6a549c0e9287 244 else
spacelab 308:7c3872365d58 245 return 0x88;
chaithanyarss 304:7cc4fe191a54 246 return 0xA0;
ee12b079 86:a26f5f22631d 247 }
ee12b079 86:a26f5f22631d 248
ee12b079 86:a26f5f22631d 249
ee12b079 86:a26f5f22631d 250 int initialise_card()
ee12b079 86:a26f5f22631d 251 {
ee12b079 86:a26f5f22631d 252 // Set to 100kHz for initialisation, and clock card with cs_sd = 1
chaithanyarss 327:5a967a66c10f 253 spi.frequency(100000); // changed on 31 12 2015 to 1 MHz
ee12b079 86:a26f5f22631d 254 cs_sd = 1;
ee12b079 86:a26f5f22631d 255 for (int i = 0; i < 16; i++) {
ee12b079 86:a26f5f22631d 256 spi.write(0xFF);
ee12b079 86:a26f5f22631d 257 }
ee12b079 194:ab528911780d 258 uint8_t R1_response = cmd(0,0);
ee12b079 194:ab528911780d 259 gPC.printf("0x%02X",R1_response);
ee12b079 86:a26f5f22631d 260 // send CMD0, should return with all zeros except IDLE STATE set (bit 0)
ee12b079 194:ab528911780d 261 if (R1_response != R1_IDLE_STATE) {
ee12b079 86:a26f5f22631d 262 debug("No disk, or could not put SD card in to spi idle state\r\n");
ee12b079 86:a26f5f22631d 263 return SDCARD_FAIL;
chaithanyarss 327:5a967a66c10f 264 } else
chaithanyarss 327:5a967a66c10f 265 gPC.puts("SD Card is in IDLE state\n\r");
ee12b079 86:a26f5f22631d 266
ee12b079 194:ab528911780d 267 // send CMD8 to determine whther it is ver 2.x
ee12b079 86:a26f5f22631d 268 int r = cmd8();
ee12b079 86:a26f5f22631d 269 if (r == R1_IDLE_STATE) {
ee12b079 194:ab528911780d 270 gPC.puts("Entering V2\r");
ee12b079 194:ab528911780d 271 int q = initialise_card_v2();
ee12b079 194:ab528911780d 272 return q;
ee12b079 86:a26f5f22631d 273
ee12b079 86:a26f5f22631d 274 } else if (r == (R1_IDLE_STATE | R1_ILLEGAL_COMMAND)) {
ee12b079 194:ab528911780d 275 gPC.puts("Entering V1");
ee12b079 86:a26f5f22631d 276 return initialise_card_v1();
ee12b079 86:a26f5f22631d 277
ee12b079 86:a26f5f22631d 278 } else {
ee12b079 86:a26f5f22631d 279 debug("\rNot in idle state after sending CMD8 (not an SD card?)\r\n");
ee12b079 86:a26f5f22631d 280 return SDCARD_FAIL;
ee12b079 86:a26f5f22631d 281 }
ee12b079 86:a26f5f22631d 282 }
ee12b079 86:a26f5f22631d 283
ee12b079 86:a26f5f22631d 284 int initialise_card_v1()
ee12b079 86:a26f5f22631d 285 {
ee12b079 86:a26f5f22631d 286 for (int i = 0; i < SD_COMMAND_TIMEOUT; i++) {
ee12b079 86:a26f5f22631d 287 cmd(55, 0);
ee12b079 86:a26f5f22631d 288 if (cmd(41, 0) == 0) {
ee12b079 96:4ca92f9775e0 289 gPC.puts("\rv1 initialization successfull\r\n");
ee12b079 86:a26f5f22631d 290 cdv = 512;
ee12b079 86:a26f5f22631d 291 debug_if(SD_DBG, "\n\rInit: SEDCARD_V1\n\r");
ee12b079 86:a26f5f22631d 292 return SDCARD_V1;
ee12b079 86:a26f5f22631d 293 }
ee12b079 86:a26f5f22631d 294 }
ee12b079 86:a26f5f22631d 295
ee12b079 86:a26f5f22631d 296 debug("\rTimeout waiting for v1.x card\r\n");
ee12b079 86:a26f5f22631d 297 return SDCARD_FAIL;
ee12b079 86:a26f5f22631d 298 }
ee12b079 86:a26f5f22631d 299
ee12b079 86:a26f5f22631d 300
ee12b079 86:a26f5f22631d 301 int initialise_card_v2()
ee12b079 86:a26f5f22631d 302 {
ee12b079 86:a26f5f22631d 303 for (int i = 0; i < SD_COMMAND_TIMEOUT; i++) {
ee12b079 86:a26f5f22631d 304 wait_ms(50);
ee12b079 86:a26f5f22631d 305 cmd58();
ee12b079 86:a26f5f22631d 306 cmd(55, 0);
ee12b079 86:a26f5f22631d 307 if (cmd(41, 0x40000000) == 0) {
ee12b079 194:ab528911780d 308 if (DEBUG)
ee12b079 194:ab528911780d 309 gPC.puts("\rv2 initialization successfull\r\n");
ee12b079 86:a26f5f22631d 310 cmd58();
ee12b079 86:a26f5f22631d 311 debug_if(SD_DBG, "\n\rInit: SDCARD_V2\n\r");
ee12b079 86:a26f5f22631d 312 cdv = 1;
ee12b079 322:7d906d34aaff 313 FCTN_SD_MNGR();
ee12b079 86:a26f5f22631d 314 return SDCARD_V2;
ee12b079 86:a26f5f22631d 315 }
ee12b079 86:a26f5f22631d 316 }
ee12b079 86:a26f5f22631d 317
ee12b079 86:a26f5f22631d 318 debug("\rTimeout waiting for v2.x card\r\n");
ee12b079 86:a26f5f22631d 319 return SDCARD_FAIL;
ee12b079 86:a26f5f22631d 320 }
ee12b079 86:a26f5f22631d 321
ee12b079 86:a26f5f22631d 322 int cmd(int cmd, int arg)
ee12b079 86:a26f5f22631d 323 {
ee12b079 86:a26f5f22631d 324 cs_sd = 0;
ee12b079 86:a26f5f22631d 325
ee12b079 86:a26f5f22631d 326 // send a command
ee12b079 86:a26f5f22631d 327 spi.write(0x40 | cmd);
ee12b079 86:a26f5f22631d 328 spi.write(arg >> 24);
ee12b079 86:a26f5f22631d 329 spi.write(arg >> 16);
ee12b079 86:a26f5f22631d 330 spi.write(arg >> 8);
ee12b079 86:a26f5f22631d 331 spi.write(arg >> 0);
ee12b079 86:a26f5f22631d 332 spi.write(0x95);
ee12b079 86:a26f5f22631d 333
ee12b079 86:a26f5f22631d 334 // wait for the repsonse (response[7] == 0)
ee12b079 86:a26f5f22631d 335 for (int i = 0; i < SD_COMMAND_TIMEOUT; i++) {
ee12b079 86:a26f5f22631d 336 int response = spi.write(0xFF);
ee12b079 86:a26f5f22631d 337 if (!(response & 0x80)) {
ee12b079 86:a26f5f22631d 338 cs_sd = 1;
ee12b079 86:a26f5f22631d 339 spi.write(0xFF);
ee12b079 86:a26f5f22631d 340 return response;
ee12b079 86:a26f5f22631d 341 }
ee12b079 86:a26f5f22631d 342 }
ee12b079 86:a26f5f22631d 343 cs_sd = 1;
ee12b079 86:a26f5f22631d 344 spi.write(0xFF);
ee12b079 86:a26f5f22631d 345 return -1; // timeout
ee12b079 86:a26f5f22631d 346 }
ee12b079 86:a26f5f22631d 347
ee12b079 86:a26f5f22631d 348
ee12b079 86:a26f5f22631d 349 int cmd58()
ee12b079 86:a26f5f22631d 350 {
ee12b079 86:a26f5f22631d 351 cs_sd = 0;
ee12b079 86:a26f5f22631d 352 int arg = 0;
ee12b079 86:a26f5f22631d 353
ee12b079 86:a26f5f22631d 354 // send a command
ee12b079 86:a26f5f22631d 355 spi.write(0x40 | 58);
ee12b079 86:a26f5f22631d 356 spi.write(arg >> 24);
ee12b079 86:a26f5f22631d 357 spi.write(arg >> 16);
ee12b079 86:a26f5f22631d 358 spi.write(arg >> 8);
ee12b079 86:a26f5f22631d 359 spi.write(arg >> 0);
ee12b079 86:a26f5f22631d 360 spi.write(0x95);
ee12b079 86:a26f5f22631d 361
ee12b079 86:a26f5f22631d 362 // wait for the repsonse (response[7] == 0)
ee12b079 86:a26f5f22631d 363 for (int i = 0; i < SD_COMMAND_TIMEOUT; i++) {
ee12b079 86:a26f5f22631d 364 int response = spi.write(0xFF);
ee12b079 86:a26f5f22631d 365 if (!(response & 0x80)) {
ee12b079 86:a26f5f22631d 366 int ocr = spi.write(0xFF) << 24;
ee12b079 86:a26f5f22631d 367 ocr |= spi.write(0xFF) << 16;
ee12b079 86:a26f5f22631d 368 ocr |= spi.write(0xFF) << 8;
ee12b079 86:a26f5f22631d 369 ocr |= spi.write(0xFF) << 0;
ee12b079 86:a26f5f22631d 370 cs_sd = 1;
ee12b079 86:a26f5f22631d 371 spi.write(0xFF);
ee12b079 86:a26f5f22631d 372 return response;
ee12b079 86:a26f5f22631d 373 }
ee12b079 86:a26f5f22631d 374 }
ee12b079 86:a26f5f22631d 375 cs_sd = 1;
ee12b079 86:a26f5f22631d 376 spi.write(0xFF);
ee12b079 86:a26f5f22631d 377 return -1; // timeout
ee12b079 86:a26f5f22631d 378 }
ee12b079 86:a26f5f22631d 379
ee12b079 86:a26f5f22631d 380
ee12b079 86:a26f5f22631d 381 int cmd8()
ee12b079 86:a26f5f22631d 382 {
ee12b079 86:a26f5f22631d 383 cs_sd = 0;
ee12b079 86:a26f5f22631d 384
ee12b079 86:a26f5f22631d 385 // send a command
ee12b079 86:a26f5f22631d 386 spi.write(0x40 | 8); // CMD8
ee12b079 86:a26f5f22631d 387 spi.write(0x00); // reserved
ee12b079 86:a26f5f22631d 388 spi.write(0x00); // reserved
ee12b079 86:a26f5f22631d 389 spi.write(0x01); // 3.3v
ee12b079 86:a26f5f22631d 390 spi.write(0xAA); // check pattern
ee12b079 86:a26f5f22631d 391 spi.write(0x87); // crc
ee12b079 86:a26f5f22631d 392
ee12b079 86:a26f5f22631d 393 // wait for the repsonse (response[7] == 0)
ee12b079 86:a26f5f22631d 394 for (int i = 0; i < SD_COMMAND_TIMEOUT * 1000; i++) {
ee12b079 86:a26f5f22631d 395 char response[5];
ee12b079 86:a26f5f22631d 396 response[0] = spi.write(0xFF);
ee12b079 86:a26f5f22631d 397 if (!(response[0] & 0x80)) {
ee12b079 86:a26f5f22631d 398 for (int j = 1; j < 5; j++) {
ee12b079 86:a26f5f22631d 399 response[i] = spi.write(0xFF);
ee12b079 86:a26f5f22631d 400 }
ee12b079 86:a26f5f22631d 401 cs_sd = 1;
ee12b079 86:a26f5f22631d 402 spi.write(0xFF);
ee12b079 86:a26f5f22631d 403 return response[0];
ee12b079 86:a26f5f22631d 404 }
ee12b079 86:a26f5f22631d 405 }
ee12b079 86:a26f5f22631d 406 cs_sd = 1;
ee12b079 86:a26f5f22631d 407 spi.write(0xFF);
ee12b079 86:a26f5f22631d 408 return -1; // timeout
ee12b079 86:a26f5f22631d 409 }
ee12b079 86:a26f5f22631d 410
ee12b079 86:a26f5f22631d 411 uint64_t sd_sectors()
ee12b079 86:a26f5f22631d 412 {
ee12b079 86:a26f5f22631d 413 uint32_t c_size, c_size_mult, read_bl_len;
ee12b079 86:a26f5f22631d 414 uint32_t block_len, mult, blocknr, capacity;
ee12b079 86:a26f5f22631d 415 uint32_t hc_c_size;
ee12b079 86:a26f5f22631d 416 uint64_t blocks;
ee12b079 86:a26f5f22631d 417
ee12b079 86:a26f5f22631d 418 // CMD9, Response R2 (R1 byte + 16-byte block read)
ee12b079 86:a26f5f22631d 419 if (cmdx(9, 0) != 0) {
ee12b079 86:a26f5f22631d 420 debug("\rDidn't get a response from the disk\n");
ee12b079 86:a26f5f22631d 421 return 0;
ee12b079 86:a26f5f22631d 422 }
ee12b079 86:a26f5f22631d 423
ee12b079 86:a26f5f22631d 424 uint8_t cs_sdd[16];
ee12b079 86:a26f5f22631d 425 if (read(cs_sdd, 16) != 0) {
ee12b079 86:a26f5f22631d 426 debug("\rCouldn't read cs_sdd response from disk\n");
ee12b079 86:a26f5f22631d 427 return 0;
ee12b079 86:a26f5f22631d 428 }
ee12b079 86:a26f5f22631d 429
ee12b079 86:a26f5f22631d 430 // cs_sdd_structure : cs_sdd[127:126]
ee12b079 86:a26f5f22631d 431 // c_size : cs_sdd[73:62]
ee12b079 86:a26f5f22631d 432 // c_size_mult : cs_sdd[49:47]
ee12b079 86:a26f5f22631d 433 // read_bl_len : cs_sdd[83:80] - the *maximum* read block length
ee12b079 86:a26f5f22631d 434
ee12b079 86:a26f5f22631d 435 int cs_sdd_structure = ext_bits(cs_sdd, 127, 126);
ee12b079 86:a26f5f22631d 436
ee12b079 86:a26f5f22631d 437 switch (cs_sdd_structure) {
ee12b079 86:a26f5f22631d 438 case 0:
ee12b079 86:a26f5f22631d 439 cdv = 512;
ee12b079 86:a26f5f22631d 440 c_size = ext_bits(cs_sdd, 73, 62);
ee12b079 86:a26f5f22631d 441 c_size_mult = ext_bits(cs_sdd, 49, 47);
ee12b079 86:a26f5f22631d 442 read_bl_len = ext_bits(cs_sdd, 83, 80);
ee12b079 86:a26f5f22631d 443
ee12b079 86:a26f5f22631d 444 block_len = 1 << read_bl_len;
ee12b079 86:a26f5f22631d 445 mult = 1 << (c_size_mult + 2);
ee12b079 86:a26f5f22631d 446 blocknr = (c_size + 1) * mult;
ee12b079 86:a26f5f22631d 447 capacity = blocknr * block_len;
ee12b079 86:a26f5f22631d 448 blocks = capacity / 512;
ee12b079 86:a26f5f22631d 449 debug_if(SD_DBG, "\n\rSDCard\n\rc_size: %d \n\rcapacity: %ld \n\rsectors: %lld\n\r", c_size, capacity, blocks);
ee12b079 86:a26f5f22631d 450 break;
ee12b079 86:a26f5f22631d 451
ee12b079 86:a26f5f22631d 452 case 1:
ee12b079 86:a26f5f22631d 453 cdv = 1;
ee12b079 86:a26f5f22631d 454 hc_c_size = ext_bits(cs_sdd, 63, 48);
ee12b079 86:a26f5f22631d 455 blocks = (hc_c_size+1)*1024;
ee12b079 86:a26f5f22631d 456 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);
ee12b079 86:a26f5f22631d 457 break;
ee12b079 86:a26f5f22631d 458
ee12b079 86:a26f5f22631d 459 default:
ee12b079 86:a26f5f22631d 460 debug("cs_sdD struct unsupported\r\n");
ee12b079 86:a26f5f22631d 461 return 0;
ee12b079 86:a26f5f22631d 462 };
ee12b079 86:a26f5f22631d 463 return blocks;
ee12b079 86:a26f5f22631d 464 }
ee12b079 86:a26f5f22631d 465
ee12b079 86:a26f5f22631d 466 int cmdx(int cmd, int arg)
ee12b079 86:a26f5f22631d 467 {
ee12b079 86:a26f5f22631d 468 cs_sd = 0;
ee12b079 86:a26f5f22631d 469
ee12b079 86:a26f5f22631d 470 // send a command
ee12b079 86:a26f5f22631d 471 spi.write(0x40 | cmd);
ee12b079 86:a26f5f22631d 472 spi.write(arg >> 24);
ee12b079 86:a26f5f22631d 473 spi.write(arg >> 16);
ee12b079 86:a26f5f22631d 474 spi.write(arg >> 8);
ee12b079 86:a26f5f22631d 475 spi.write(arg >> 0);
ee12b079 86:a26f5f22631d 476 spi.write(0x95);
ee12b079 86:a26f5f22631d 477
ee12b079 86:a26f5f22631d 478 // wait for the repsonse (response[7] == 0)
ee12b079 86:a26f5f22631d 479 for (int i = 0; i < SD_COMMAND_TIMEOUT; i++) {
ee12b079 86:a26f5f22631d 480 int response = spi.write(0xFF);
ee12b079 86:a26f5f22631d 481 if (!(response & 0x80)) {
ee12b079 86:a26f5f22631d 482 return response;
ee12b079 86:a26f5f22631d 483 }
ee12b079 86:a26f5f22631d 484 }
ee12b079 86:a26f5f22631d 485 cs_sd = 1;
ee12b079 86:a26f5f22631d 486 spi.write(0xFF);
ee12b079 194:ab528911780d 487 return 1; // timeout
ee12b079 86:a26f5f22631d 488 }
ee12b079 86:a26f5f22631d 489
ee12b079 86:a26f5f22631d 490 static uint32_t ext_bits(unsigned char *data, int msb, int lsb)
ee12b079 86:a26f5f22631d 491 {
ee12b079 86:a26f5f22631d 492 uint32_t bits = 0;
ee12b079 86:a26f5f22631d 493 uint32_t size = 1 + msb - lsb;
ee12b079 86:a26f5f22631d 494 for (int i = 0; i < size; i++) {
ee12b079 86:a26f5f22631d 495 uint32_t position = lsb + i;
ee12b079 86:a26f5f22631d 496 uint32_t byte = 15 - (position >> 3);
ee12b079 86:a26f5f22631d 497 uint32_t bit = position & 0x7;
ee12b079 86:a26f5f22631d 498 uint32_t value = (data[byte] >> bit) & 1;
ee12b079 86:a26f5f22631d 499 bits |= value << i;
ee12b079 86:a26f5f22631d 500 }
ee12b079 86:a26f5f22631d 501 return bits;
ee12b079 86:a26f5f22631d 502 }
ee12b079 86:a26f5f22631d 503
ee12b079 86:a26f5f22631d 504 int disk_write(const uint8_t *buffer, uint64_t block_number)
ee12b079 86:a26f5f22631d 505
ee12b079 86:a26f5f22631d 506 {
ee12b079 86:a26f5f22631d 507 // set write address for single block (CMD24)
ee12b079 86:a26f5f22631d 508 if (cmd(24, block_number * cdv) != 0) {
chaithanyarss 303:b49b486a7107 509 CDMS_WR_SD_FAULT_COUNTER++;
ee12b079 86:a26f5f22631d 510 return 1;
ee12b079 86:a26f5f22631d 511 }
chaithanyarss 327:5a967a66c10f 512
ee12b079 209:63e9c8f8b5d2 513 uint64_t temp;
ee12b079 209:63e9c8f8b5d2 514 int r = write(buffer, 512);
chaithanyarss 327:5a967a66c10f 515 if(r == 0 ) {
ee12b079 209:63e9c8f8b5d2 516 temp = FCTN_CDMS_RD_RTC();
spacelab 313:5d06a08baf0e 517 TIME_LATEST_SD_WR = temp >> 7; //corrected by samp:TIME_LATEST_SD_WRD = temp >> 7;
ee12b079 209:63e9c8f8b5d2 518 }
ee12b079 209:63e9c8f8b5d2 519 return r;
ee12b079 86:a26f5f22631d 520 }
ee12b079 86:a26f5f22631d 521
ee12b079 86:a26f5f22631d 522 int write(const uint8_t*buffer, uint32_t length)
ee12b079 86:a26f5f22631d 523 {
ee12b079 86:a26f5f22631d 524 cs_sd = 0;
ee12b079 86:a26f5f22631d 525
ee12b079 86:a26f5f22631d 526 // indicate start of block
ee12b079 86:a26f5f22631d 527 spi.write(0xFE);
ee12b079 86:a26f5f22631d 528
ee12b079 86:a26f5f22631d 529 // write the data
ee12b079 86:a26f5f22631d 530 for (int i = 0; i < length; i++) {
ee12b079 86:a26f5f22631d 531 spi.write(buffer[i]);
ee12b079 86:a26f5f22631d 532 }
ee12b079 86:a26f5f22631d 533
ee12b079 86:a26f5f22631d 534 // write the checksum
ee12b079 86:a26f5f22631d 535 spi.write(0xFF);
ee12b079 86:a26f5f22631d 536 spi.write(0xFF);
ee12b079 86:a26f5f22631d 537
ee12b079 86:a26f5f22631d 538 // check the response token
ee12b079 86:a26f5f22631d 539 if ((spi.write(0xFF) & 0x1F) != 0x05) {
ee12b079 86:a26f5f22631d 540 cs_sd = 1;
ee12b079 86:a26f5f22631d 541 spi.write(0xFF);
chaithanyarss 303:b49b486a7107 542 CDMS_WR_SD_FAULT_COUNTER++;
ee12b079 86:a26f5f22631d 543 return 1;
ee12b079 86:a26f5f22631d 544 }
ee12b079 86:a26f5f22631d 545
ee12b079 86:a26f5f22631d 546 // wait for write to finish
ee12b079 86:a26f5f22631d 547 while (spi.write(0xFF) == 0);
ee12b079 86:a26f5f22631d 548
ee12b079 86:a26f5f22631d 549 cs_sd = 1;
ee12b079 86:a26f5f22631d 550 spi.write(0xFF);
ee12b079 86:a26f5f22631d 551 return 0;
ee12b079 86:a26f5f22631d 552 }
ee12b079 86:a26f5f22631d 553
ee12b079 86:a26f5f22631d 554 int disk_read(uint8_t *buffer, uint64_t block_number)
ee12b079 86:a26f5f22631d 555 {
ee12b079 86:a26f5f22631d 556 // set read address for single block (CMD17)
ee12b079 86:a26f5f22631d 557 if (cmd(17, block_number * cdv) != 0) {
ee12b079 209:63e9c8f8b5d2 558 SD_RD_ERROR = 1;
ee12b079 86:a26f5f22631d 559 return 1;
ee12b079 86:a26f5f22631d 560 }
ee12b079 86:a26f5f22631d 561
ee12b079 86:a26f5f22631d 562 // receive the data
ee12b079 86:a26f5f22631d 563 read(buffer, 512);
ee12b079 209:63e9c8f8b5d2 564 uint64_t temp = FCTN_CDMS_RD_RTC();
ee12b079 209:63e9c8f8b5d2 565 TIME_LATEST_SD_RD = temp >> 7;
ee12b079 86:a26f5f22631d 566 return 0;
ee12b079 86:a26f5f22631d 567 }
ee12b079 86:a26f5f22631d 568
ee12b079 86:a26f5f22631d 569 int read(uint8_t *buffer, uint32_t length)
ee12b079 86:a26f5f22631d 570 {
ee12b079 86:a26f5f22631d 571 cs_sd = 0;
ee12b079 86:a26f5f22631d 572
ee12b079 86:a26f5f22631d 573 // read until start byte (0xFF)
ee12b079 86:a26f5f22631d 574 while (spi.write(0xFF) != 0xFE);
ee12b079 86:a26f5f22631d 575
ee12b079 86:a26f5f22631d 576 // read data
ee12b079 86:a26f5f22631d 577 for (int i = 0; i < length; i++) {
ee12b079 86:a26f5f22631d 578 buffer[i] = spi.write(0xFF);
ee12b079 86:a26f5f22631d 579 }
ee12b079 86:a26f5f22631d 580 spi.write(0xFF); // checksum
ee12b079 86:a26f5f22631d 581 spi.write(0xFF);
ee12b079 86:a26f5f22631d 582
ee12b079 86:a26f5f22631d 583 cs_sd = 1;
ee12b079 86:a26f5f22631d 584 spi.write(0xFF);
ee12b079 86:a26f5f22631d 585 return 0;
ee12b079 86:a26f5f22631d 586 }
ee12b079 86:a26f5f22631d 587
ee12b079 86:a26f5f22631d 588 int disk_erase(int startBlock, int totalBlocks)
ee12b079 86:a26f5f22631d 589 {
ee12b079 86:a26f5f22631d 590 if(cmd(32, startBlock * cdv) != 0) {
ee12b079 86:a26f5f22631d 591 return 1;
ee12b079 86:a26f5f22631d 592 }
ee12b079 86:a26f5f22631d 593 if (cmd(33, (startBlock+totalBlocks-1) * cdv) != 0) {
ee12b079 86:a26f5f22631d 594 return 1;
ee12b079 86:a26f5f22631d 595 }
ee12b079 86:a26f5f22631d 596 if (cmd(38,0) != 0) {
ee12b079 86:a26f5f22631d 597 return 1;
ee12b079 86:a26f5f22631d 598 }
chaithanyarss 327:5a967a66c10f 599
ee12b079 86:a26f5f22631d 600 return 0; //normal return
ee12b079 209:63e9c8f8b5d2 601 }
ee12b079 209:63e9c8f8b5d2 602
ee12b079 209:63e9c8f8b5d2 603 int disk_read_statusbits(uint8_t *buffer)
ee12b079 209:63e9c8f8b5d2 604 {
ee12b079 209:63e9c8f8b5d2 605 if (cmd(17, 0) != 0) {
chaithanyarss 327:5a967a66c10f 606 SD_RD_ERROR = 1;
ee12b079 209:63e9c8f8b5d2 607 return -1;
ee12b079 209:63e9c8f8b5d2 608 }
ee12b079 209:63e9c8f8b5d2 609
ee12b079 209:63e9c8f8b5d2 610 // receive the data
ee12b079 209:63e9c8f8b5d2 611 return read(buffer,64);
chaithanyarss 328:2242ebc71be8 612 }
chaithanyarss 328:2242ebc71be8 613
chaithanyarss 328:2242ebc71be8 614 #endif