I2C code testing

Dependencies:   FreescaleIAP SimpleDMA mbed-rtos mbed

Fork of COM_MNG_TMTC_SIMPLE_pl123 by Siva ram

Committer:
ee12b079
Date:
Mon Feb 29 13:21:40 2016 +0000
Revision:
132:d4a4461214ad
Parent:
99:efde3df6cd94
i2c integration in progress

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ee12b079 86:a26f5f22631d 1 //SPI spi(PTE1, PTE3, PTE2); // MOSI,MISO, CLOCK microcontroller(in order)
ee12b079 86:a26f5f22631d 2 //DigitalOut cs_sd(PTE22);
ee12b079 86:a26f5f22631d 3
ee12b079 86:a26f5f22631d 4 //Serial sd1(USBTX,USBRX);
ee12b079 86:a26f5f22631d 5
ee12b079 86:a26f5f22631d 6
ee12b079 86:a26f5f22631d 7 #define SD_COMMAND_TIMEOUT 5000
ee12b079 86:a26f5f22631d 8
ee12b079 86:a26f5f22631d 9 #define SD_DBG 0
ee12b079 86:a26f5f22631d 10
ee12b079 86:a26f5f22631d 11 #define R1_IDLE_STATE (1 << 0)
ee12b079 86:a26f5f22631d 12 #define R1_ERASE_RESET (1 << 1)
ee12b079 86:a26f5f22631d 13 #define R1_ILLEGAL_COMMAND (1 << 2)
ee12b079 86:a26f5f22631d 14 #define R1_COM_CRC_ERROR (1 << 3)
ee12b079 86:a26f5f22631d 15 #define R1_ERASE_SEQUENCE_ERROR (1 << 4)
ee12b079 86:a26f5f22631d 16 #define R1_ADDRESS_ERROR (1 << 5)
ee12b079 86:a26f5f22631d 17 #define R1_PARAMETER_ERROR (1 << 6)
ee12b079 86:a26f5f22631d 18
ee12b079 86:a26f5f22631d 19
ee12b079 86:a26f5f22631d 20
ee12b079 86:a26f5f22631d 21 #define SD_MAX_CYCLES 10000
ee12b079 86:a26f5f22631d 22
ee12b079 86:a26f5f22631d 23 extern uint8_t SD_INIT_FLAGS;
ee12b079 86:a26f5f22631d 24
ee12b079 86:a26f5f22631d 25
ee12b079 86:a26f5f22631d 26
ee12b079 86:a26f5f22631d 27 int initialise_card();
ee12b079 86:a26f5f22631d 28 int initialise_card_v1();
ee12b079 86:a26f5f22631d 29 int initialise_card_v2();
ee12b079 86:a26f5f22631d 30 int disk_write(const uint8_t *, uint64_t);
ee12b079 86:a26f5f22631d 31 int disk_read(uint8_t *, uint64_t);
ee12b079 86:a26f5f22631d 32 int disk_initialize();
ee12b079 86:a26f5f22631d 33 int disk_erase(int,int);
ee12b079 86:a26f5f22631d 34
ee12b079 86:a26f5f22631d 35 //void FCTN_CDMS_INIT_SD();
ee12b079 86:a26f5f22631d 36 //int FCTN_CDMS_WR_SD(const uint8_t *, uint64_t);
ee12b079 86:a26f5f22631d 37 //int FCTN_CDMS_RD_SD(uint8_t *, uint64_t);
ee12b079 92:bce22b38c440 38 uint32_t FCTN_SD_MNGR(uint8_t);
ee12b079 86:a26f5f22631d 39
ee12b079 86:a26f5f22631d 40
ee12b079 86:a26f5f22631d 41 int cmd(int, int);
ee12b079 86:a26f5f22631d 42 int cmd58();
ee12b079 86:a26f5f22631d 43 int cmdx(int, int);
ee12b079 86:a26f5f22631d 44 int cmd8();
ee12b079 86:a26f5f22631d 45 int read(uint8_t*, uint32_t );
ee12b079 86:a26f5f22631d 46 int write(const uint8_t*, uint32_t );
ee12b079 86:a26f5f22631d 47 static uint32_t ext_bits(unsigned char *, int , int );
ee12b079 92:bce22b38c440 48 int SD_WRITE(uint8_t*,uint32_t,uint8_t);
ee12b079 86:a26f5f22631d 49 void FCTN_CDMS_SD_INIT();
ee12b079 92:bce22b38c440 50 int SD_READ(uint8_t*,uint32_t,uint8_t);
ee12b079 86:a26f5f22631d 51 #define SDCARD_FAIL 0
ee12b079 86:a26f5f22631d 52 #define SDCARD_V1 1
ee12b079 86:a26f5f22631d 53 #define SDCARD_V2 2
ee12b079 86:a26f5f22631d 54 #define SDCARD_V2HC 3
ee12b079 86:a26f5f22631d 55
ee12b079 86:a26f5f22631d 56
ee12b079 86:a26f5f22631d 57
ee12b079 86:a26f5f22631d 58
ee12b079 86:a26f5f22631d 59
ee12b079 86:a26f5f22631d 60
ee12b079 86:a26f5f22631d 61
ee12b079 86:a26f5f22631d 62
ee12b079 86:a26f5f22631d 63
ee12b079 86:a26f5f22631d 64
ee12b079 86:a26f5f22631d 65
ee12b079 86:a26f5f22631d 66 int cdv;
ee12b079 86:a26f5f22631d 67 uint64_t sd_sectors();
ee12b079 86:a26f5f22631d 68 uint64_t sectors;
ee12b079 86:a26f5f22631d 69
ee12b079 86:a26f5f22631d 70 void FCTN_CDMS_SD_INIT()
ee12b079 86:a26f5f22631d 71 {
ee12b079 86:a26f5f22631d 72
ee12b079 86:a26f5f22631d 73 initialise_card();
ee12b079 86:a26f5f22631d 74
ee12b079 86:a26f5f22631d 75 disk_initialize();
ee12b079 86:a26f5f22631d 76
ee12b079 86:a26f5f22631d 77 }
ee12b079 86:a26f5f22631d 78
ee12b079 86:a26f5f22631d 79
ee12b079 86:a26f5f22631d 80
ee12b079 92:bce22b38c440 81 uint32_t FCTN_SD_MNGR(uint8_t sid)
ee12b079 86:a26f5f22631d 82
ee12b079 86:a26f5f22631d 83 {
ee12b079 92:bce22b38c440 84
ee12b079 86:a26f5f22631d 85 uint32_t SD_MNG_SECT=7000;
ee12b079 92:bce22b38c440 86
ee12b079 92:bce22b38c440 87 uint32_t fsc;
ee12b079 92:bce22b38c440 88 uint8_t buffer[512];
ee12b079 92:bce22b38c440 89 int b=disk_read(buffer, SD_MNG_SECT);
ee12b079 86:a26f5f22631d 90 if(sid==0x0)
ee12b079 86:a26f5f22631d 91 {
ee12b079 92:bce22b38c440 92
ee12b079 92:bce22b38c440 93 fsc=(uint32_t)(buffer[0]<<24)+(uint32_t)(buffer[1]<<16)+(uint32_t)(buffer[2]<<8)+(uint32_t)buffer[3];
ee12b079 92:bce22b38c440 94 uint32_t next_fsc=fsc+1;
ee12b079 92:bce22b38c440 95 buffer[0]=(uint8_t) (next_fsc>>24 & 0xFF);
ee12b079 92:bce22b38c440 96 buffer[1]=(uint8_t) (next_fsc>>16 & 0xFF);
ee12b079 92:bce22b38c440 97 buffer[2]=(uint8_t) (next_fsc>>8 & 0xFF);
ee12b079 92:bce22b38c440 98 buffer[3]=(uint8_t) (next_fsc & 0xFF);
ee12b079 86:a26f5f22631d 99 buffer[511]+=2;
ee12b079 86:a26f5f22631d 100 disk_write(buffer,SD_MNG_SECT);
ee12b079 86:a26f5f22631d 101 }
ee12b079 86:a26f5f22631d 102 if(sid==0x1)
ee12b079 86:a26f5f22631d 103 {
ee12b079 92:bce22b38c440 104 fsc=(uint32_t)(buffer[4]<<24)+(uint32_t)(buffer[5]<<16)+(uint32_t)(buffer[6]<<8)+(uint32_t)buffer[7];
ee12b079 92:bce22b38c440 105 uint32_t next_fsc=fsc+1;
ee12b079 92:bce22b38c440 106 buffer[4]=(uint8_t) (next_fsc>>24 & 0xFF);
ee12b079 92:bce22b38c440 107 buffer[5]=(uint8_t) (next_fsc>>16 & 0xFF);
ee12b079 92:bce22b38c440 108 buffer[6]=(uint8_t) (next_fsc>>8 & 0xFF);
ee12b079 92:bce22b38c440 109 buffer[7]=(uint8_t) (next_fsc & 0xFF);
ee12b079 86:a26f5f22631d 110 buffer[511]+=2;
ee12b079 86:a26f5f22631d 111 disk_write(buffer,SD_MNG_SECT);
ee12b079 86:a26f5f22631d 112 }
ee12b079 86:a26f5f22631d 113 if(sid==0x2)
ee12b079 86:a26f5f22631d 114 {
ee12b079 92:bce22b38c440 115 fsc=(uint32_t)(buffer[8]<<24)+(uint32_t)(buffer[9]<<16)+(uint32_t)(buffer[10]<<8)+(uint32_t)buffer[11];
ee12b079 92:bce22b38c440 116 uint32_t next_fsc=fsc+1;
ee12b079 92:bce22b38c440 117 buffer[8]=(uint8_t) (next_fsc>>24 & 0xFF);
ee12b079 92:bce22b38c440 118 buffer[9]=(uint8_t) (next_fsc>>16 & 0xFF);
ee12b079 92:bce22b38c440 119 buffer[10]=(uint8_t) (next_fsc>>8 & 0xFF);
ee12b079 92:bce22b38c440 120 buffer[11]=(uint8_t) (next_fsc & 0xFF);
ee12b079 86:a26f5f22631d 121 buffer[511]+=2;
ee12b079 86:a26f5f22631d 122 disk_write(buffer,SD_MNG_SECT);
ee12b079 86:a26f5f22631d 123 }
ee12b079 86:a26f5f22631d 124 if(sid==0x3)
ee12b079 86:a26f5f22631d 125 {
ee12b079 92:bce22b38c440 126 fsc=(uint32_t)(buffer[12]<<24)+(uint32_t)(buffer[13]<<16)+(uint32_t)(buffer[14]<<8)+(uint32_t)buffer[15];
ee12b079 92:bce22b38c440 127 uint32_t next_fsc=fsc+1;
ee12b079 92:bce22b38c440 128 buffer[12]=(uint8_t) (next_fsc>>24 & 0xFF);
ee12b079 92:bce22b38c440 129 buffer[13]=(uint8_t) (next_fsc>>16 & 0xFF);
ee12b079 92:bce22b38c440 130 buffer[14]=(uint8_t) (next_fsc>>8 & 0xFF);
ee12b079 92:bce22b38c440 131 buffer[15]=(uint8_t) (next_fsc & 0xFF);
ee12b079 86:a26f5f22631d 132 buffer[511]+=2;
ee12b079 86:a26f5f22631d 133 disk_write(buffer,SD_MNG_SECT);
ee12b079 86:a26f5f22631d 134 }
ee12b079 86:a26f5f22631d 135 if(sid==0x4)
ee12b079 86:a26f5f22631d 136 {
ee12b079 92:bce22b38c440 137 fsc=(uint32_t)(buffer[16]<<24)+(uint32_t)(buffer[17]<<16)+(uint32_t)(buffer[18]<<8)+(uint32_t)buffer[19];
ee12b079 92:bce22b38c440 138 uint32_t next_fsc=fsc+1;
ee12b079 92:bce22b38c440 139 buffer[16]=(uint8_t) (next_fsc>>24 & 0xFF);
ee12b079 92:bce22b38c440 140 buffer[17]=(uint8_t) (next_fsc>>16 & 0xFF);
ee12b079 92:bce22b38c440 141 buffer[18]=(uint8_t) (next_fsc>>8 & 0xFF);
ee12b079 92:bce22b38c440 142 buffer[19]=(uint8_t) (next_fsc & 0xFF);
ee12b079 86:a26f5f22631d 143 buffer[511]+=2;
ee12b079 86:a26f5f22631d 144 disk_write(buffer,SD_MNG_SECT);
ee12b079 86:a26f5f22631d 145 }
ee12b079 86:a26f5f22631d 146 return fsc;
ee12b079 86:a26f5f22631d 147 }
ee12b079 86:a26f5f22631d 148
ee12b079 86:a26f5f22631d 149
ee12b079 92:bce22b38c440 150 int SD_WRITE(uint8_t* buffer,uint32_t fsc,uint8_t sid)
ee12b079 86:a26f5f22631d 151 {
ee12b079 86:a26f5f22631d 152
ee12b079 86:a26f5f22631d 153 uint32_t SD_SCP_FIRST=1001;
ee12b079 86:a26f5f22631d 154 uint32_t SD_SCP_LAST=2000;
ee12b079 86:a26f5f22631d 155 uint32_t SD_SFF_AT_FIRST=2001;
ee12b079 86:a26f5f22631d 156 uint32_t SD_SFF_AT_LAST = 3000;
ee12b079 86:a26f5f22631d 157 uint32_t SD_SFF_BT_FIRST =3001;
ee12b079 86:a26f5f22631d 158 uint32_t SD_SFF_BT_LAST=4000;
ee12b079 86:a26f5f22631d 159 uint32_t SD_HK_ARCH_FIRST=4001;
ee12b079 86:a26f5f22631d 160 uint32_t SD_HK_ARCH_LAST= 5000;
ee12b079 86:a26f5f22631d 161 uint32_t LOG_FIRST =5001;
ee12b079 86:a26f5f22631d 162 uint32_t LOG_LAST=6000;
ee12b079 86:a26f5f22631d 163 uint32_t SD_MNG_SECT=7000;
ee12b079 86:a26f5f22631d 164 uint32_t block_number;
ee12b079 86:a26f5f22631d 165 int result;
ee12b079 86:a26f5f22631d 166 if(sid==0x0)
ee12b079 86:a26f5f22631d 167 {
ee12b079 92:bce22b38c440 168 block_number=SD_SCP_FIRST+fsc;
ee12b079 86:a26f5f22631d 169 // printf("write_block_number=%d\r\n",block_number);
ee12b079 86:a26f5f22631d 170 result= disk_write(buffer,block_number);
ee12b079 86:a26f5f22631d 171 return result;
ee12b079 86:a26f5f22631d 172 }
ee12b079 86:a26f5f22631d 173 if(sid==0x1)
ee12b079 86:a26f5f22631d 174 {
ee12b079 86:a26f5f22631d 175 block_number=SD_SFF_AT_FIRST + fsc;
ee12b079 86:a26f5f22631d 176 result= disk_write(buffer,block_number);
ee12b079 86:a26f5f22631d 177 return result;
ee12b079 86:a26f5f22631d 178 }
ee12b079 86:a26f5f22631d 179 if(sid==0x2)
ee12b079 86:a26f5f22631d 180 {
ee12b079 86:a26f5f22631d 181 block_number=SD_SFF_BT_FIRST + fsc;
ee12b079 86:a26f5f22631d 182 result= disk_write(buffer,block_number);
ee12b079 86:a26f5f22631d 183 return result;
ee12b079 86:a26f5f22631d 184 }
ee12b079 86:a26f5f22631d 185 if(sid==0x3)
ee12b079 86:a26f5f22631d 186 {
ee12b079 86:a26f5f22631d 187 block_number=SD_HK_ARCH_FIRST+fsc;
ee12b079 86:a26f5f22631d 188 // sd1.printf("Block number is %d \r\n",block_number);
ee12b079 86:a26f5f22631d 189 result= disk_write(buffer,block_number);
ee12b079 86:a26f5f22631d 190 return result;
ee12b079 86:a26f5f22631d 191 }
ee12b079 86:a26f5f22631d 192 if(sid==0x4)
ee12b079 86:a26f5f22631d 193 {
ee12b079 86:a26f5f22631d 194 block_number=LOG_FIRST +fsc;
ee12b079 86:a26f5f22631d 195 result= disk_write(buffer,block_number);
ee12b079 86:a26f5f22631d 196 return result;
ee12b079 86:a26f5f22631d 197 }
ee12b079 86:a26f5f22631d 198 return 1;
ee12b079 86:a26f5f22631d 199 }
ee12b079 86:a26f5f22631d 200
ee12b079 92:bce22b38c440 201 int SD_READ(uint8_t* buffer,uint32_t fsc,uint8_t sid)
ee12b079 86:a26f5f22631d 202 {
ee12b079 86:a26f5f22631d 203
ee12b079 86:a26f5f22631d 204 uint32_t SD_SCP_FIRST=1001;
ee12b079 86:a26f5f22631d 205 uint32_t SD_SCP_LAST=2000;
ee12b079 86:a26f5f22631d 206 uint32_t SD_SFF_AT_FIRST=2001;
ee12b079 86:a26f5f22631d 207 uint32_t SD_SFF_AT_LAST = 3000;
ee12b079 86:a26f5f22631d 208 uint32_t SD_SFF_BT_FIRST =3001;
ee12b079 86:a26f5f22631d 209 uint32_t SD_SFF_BT_LAST=4000;
ee12b079 86:a26f5f22631d 210 uint32_t SD_HK_ARCH_FIRST=4001;
ee12b079 86:a26f5f22631d 211 uint32_t SD_HK_ARCH_LAST= 5000;
ee12b079 86:a26f5f22631d 212 uint32_t LOG_FIRST =5001;
ee12b079 86:a26f5f22631d 213 uint32_t LOG_LAST=6000;
ee12b079 86:a26f5f22631d 214 uint32_t SD_MNG_SECT=7000;
ee12b079 86:a26f5f22631d 215 uint32_t block_number;
ee12b079 86:a26f5f22631d 216 int result;
ee12b079 86:a26f5f22631d 217 if(sid==0x0)
ee12b079 86:a26f5f22631d 218 {
ee12b079 92:bce22b38c440 219 block_number=SD_SCP_FIRST + fsc;
ee12b079 86:a26f5f22631d 220 // sd1.printf("read_block_number=%d\r\n",block_number);
ee12b079 86:a26f5f22631d 221 result= disk_read(buffer,block_number);
ee12b079 86:a26f5f22631d 222 }
ee12b079 86:a26f5f22631d 223 else if(sid==0x1)
ee12b079 86:a26f5f22631d 224 {
ee12b079 86:a26f5f22631d 225 block_number=SD_SFF_AT_FIRST + fsc;
ee12b079 86:a26f5f22631d 226 result= disk_read(buffer,block_number);
ee12b079 86:a26f5f22631d 227 }
ee12b079 86:a26f5f22631d 228 else if(sid==0x2)
ee12b079 86:a26f5f22631d 229 {
ee12b079 86:a26f5f22631d 230 block_number=SD_SFF_BT_FIRST + fsc;
ee12b079 86:a26f5f22631d 231 result= disk_read(buffer,block_number);
ee12b079 86:a26f5f22631d 232 }
ee12b079 86:a26f5f22631d 233 else if(sid==0x3)
ee12b079 86:a26f5f22631d 234 {
ee12b079 86:a26f5f22631d 235 block_number=SD_HK_ARCH_FIRST + fsc;
ee12b079 86:a26f5f22631d 236 result= disk_read(buffer,block_number);
ee12b079 86:a26f5f22631d 237 }
ee12b079 86:a26f5f22631d 238 else if(sid==0x4)
ee12b079 86:a26f5f22631d 239 {
ee12b079 86:a26f5f22631d 240 block_number=LOG_FIRST +fsc;
ee12b079 86:a26f5f22631d 241 result= disk_read(buffer,block_number);
ee12b079 86:a26f5f22631d 242 }
ee12b079 86:a26f5f22631d 243 else
ee12b079 86:a26f5f22631d 244 {
ee12b079 86:a26f5f22631d 245 return 1;
ee12b079 86:a26f5f22631d 246 }
ee12b079 86:a26f5f22631d 247 return result;
ee12b079 86:a26f5f22631d 248 }
ee12b079 86:a26f5f22631d 249
ee12b079 86:a26f5f22631d 250
ee12b079 86:a26f5f22631d 251 int initialise_card()
ee12b079 86:a26f5f22631d 252 {
ee12b079 86:a26f5f22631d 253 // Set to 100kHz for initialisation, and clock card with cs_sd = 1
ee12b079 86:a26f5f22631d 254 spi.frequency(100000); // changed on 31 12 2015 to 1 MHz
ee12b079 86:a26f5f22631d 255 cs_sd = 1;
ee12b079 86:a26f5f22631d 256 for (int i = 0; i < 16; i++) {
ee12b079 86:a26f5f22631d 257 spi.write(0xFF);
ee12b079 86:a26f5f22631d 258 }
ee12b079 86:a26f5f22631d 259
ee12b079 86:a26f5f22631d 260 // send CMD0, should return with all zeros except IDLE STATE set (bit 0)
ee12b079 86:a26f5f22631d 261 if (cmd(0, 0) != 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;
ee12b079 86:a26f5f22631d 264 }
ee12b079 86:a26f5f22631d 265
ee12b079 86:a26f5f22631d 266 // send CMD8 to determine whther it is ver 2.x
ee12b079 86:a26f5f22631d 267 int r = cmd8();
ee12b079 86:a26f5f22631d 268 if (r == R1_IDLE_STATE) {
ee12b079 96:4ca92f9775e0 269 // printf("\rEntering v2\r\n");
ee12b079 86:a26f5f22631d 270 return initialise_card_v2();
ee12b079 86:a26f5f22631d 271
ee12b079 86:a26f5f22631d 272 } else if (r == (R1_IDLE_STATE | R1_ILLEGAL_COMMAND)) {
ee12b079 96:4ca92f9775e0 273 // printf("\rEntering v1\r\n");
ee12b079 86:a26f5f22631d 274 return initialise_card_v1();
ee12b079 86:a26f5f22631d 275
ee12b079 86:a26f5f22631d 276 } else {
ee12b079 86:a26f5f22631d 277 debug("\rNot in idle state after sending CMD8 (not an SD card?)\r\n");
ee12b079 86:a26f5f22631d 278 return SDCARD_FAIL;
ee12b079 86:a26f5f22631d 279 }
ee12b079 86:a26f5f22631d 280 }
ee12b079 86:a26f5f22631d 281
ee12b079 86:a26f5f22631d 282 int initialise_card_v1()
ee12b079 86:a26f5f22631d 283 {
ee12b079 86:a26f5f22631d 284 for (int i = 0; i < SD_COMMAND_TIMEOUT; i++) {
ee12b079 86:a26f5f22631d 285 cmd(55, 0);
ee12b079 86:a26f5f22631d 286 if (cmd(41, 0) == 0) {
ee12b079 96:4ca92f9775e0 287 gPC.puts("\rv1 initialization successfull\r\n");
ee12b079 86:a26f5f22631d 288 cdv = 512;
ee12b079 86:a26f5f22631d 289 debug_if(SD_DBG, "\n\rInit: SEDCARD_V1\n\r");
ee12b079 86:a26f5f22631d 290
ee12b079 86:a26f5f22631d 291 return SDCARD_V1;
ee12b079 86:a26f5f22631d 292 }
ee12b079 86:a26f5f22631d 293 }
ee12b079 86:a26f5f22631d 294
ee12b079 86:a26f5f22631d 295 debug("\rTimeout waiting for v1.x card\r\n");
ee12b079 86:a26f5f22631d 296 return SDCARD_FAIL;
ee12b079 86:a26f5f22631d 297 }
ee12b079 86:a26f5f22631d 298
ee12b079 86:a26f5f22631d 299
ee12b079 86:a26f5f22631d 300 int initialise_card_v2()
ee12b079 86:a26f5f22631d 301 {
ee12b079 86:a26f5f22631d 302 for (int i = 0; i < SD_COMMAND_TIMEOUT; i++) {
ee12b079 86:a26f5f22631d 303 wait_ms(50);
ee12b079 86:a26f5f22631d 304 cmd58();
ee12b079 86:a26f5f22631d 305 cmd(55, 0);
ee12b079 86:a26f5f22631d 306 if (cmd(41, 0x40000000) == 0) {
ee12b079 99:efde3df6cd94 307 #if DEBUG
ee12b079 99:efde3df6cd94 308 gPC.puts("\rv2 initialization successfull\r\n");
ee12b079 99:efde3df6cd94 309 #endif
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 86:a26f5f22631d 313
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 86:a26f5f22631d 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_initialize()
ee12b079 86:a26f5f22631d 505 {
ee12b079 86:a26f5f22631d 506 int i = initialise_card();
ee12b079 86:a26f5f22631d 507 debug_if(SD_DBG, "init card = %d\n", i);
ee12b079 86:a26f5f22631d 508 sectors = sd_sectors();
ee12b079 86:a26f5f22631d 509
ee12b079 86:a26f5f22631d 510 // Set block length to 512 (CMD16)
ee12b079 86:a26f5f22631d 511 if (cmd(16, 512) != 0) {
ee12b079 86:a26f5f22631d 512 debug("\rSet 512-byte block timed out\r\n");
ee12b079 86:a26f5f22631d 513 return 1;
ee12b079 86:a26f5f22631d 514 } else {
ee12b079 96:4ca92f9775e0 515 // printf("\rDisk initialization successfull\r\n");
ee12b079 86:a26f5f22631d 516 }
ee12b079 86:a26f5f22631d 517
ee12b079 86:a26f5f22631d 518 spi.frequency(1000000); // Set to 1MHz for data transfer
ee12b079 86:a26f5f22631d 519 return 0;
ee12b079 86:a26f5f22631d 520 }
ee12b079 86:a26f5f22631d 521
ee12b079 86:a26f5f22631d 522 int disk_write(const uint8_t *buffer, uint64_t block_number)
ee12b079 86:a26f5f22631d 523
ee12b079 86:a26f5f22631d 524 {
ee12b079 86:a26f5f22631d 525 // set write address for single block (CMD24)
ee12b079 86:a26f5f22631d 526 if (cmd(24, block_number * cdv) != 0) {
ee12b079 86:a26f5f22631d 527 return 1;
ee12b079 86:a26f5f22631d 528 }
ee12b079 86:a26f5f22631d 529
ee12b079 86:a26f5f22631d 530 // send the data block
ee12b079 86:a26f5f22631d 531 write(buffer, 512);
ee12b079 86:a26f5f22631d 532 //printf("Written Successfully bro \n");
ee12b079 86:a26f5f22631d 533 return 0;
ee12b079 86:a26f5f22631d 534 }
ee12b079 86:a26f5f22631d 535
ee12b079 86:a26f5f22631d 536 int write(const uint8_t*buffer, uint32_t length)
ee12b079 86:a26f5f22631d 537 {
ee12b079 86:a26f5f22631d 538 cs_sd = 0;
ee12b079 86:a26f5f22631d 539
ee12b079 86:a26f5f22631d 540 // indicate start of block
ee12b079 86:a26f5f22631d 541 spi.write(0xFE);
ee12b079 86:a26f5f22631d 542
ee12b079 86:a26f5f22631d 543 // write the data
ee12b079 86:a26f5f22631d 544 for (int i = 0; i < length; i++) {
ee12b079 86:a26f5f22631d 545 spi.write(buffer[i]);
ee12b079 86:a26f5f22631d 546 }
ee12b079 86:a26f5f22631d 547
ee12b079 86:a26f5f22631d 548 // write the checksum
ee12b079 86:a26f5f22631d 549 spi.write(0xFF);
ee12b079 86:a26f5f22631d 550 spi.write(0xFF);
ee12b079 86:a26f5f22631d 551
ee12b079 86:a26f5f22631d 552 // check the response token
ee12b079 86:a26f5f22631d 553 if ((spi.write(0xFF) & 0x1F) != 0x05) {
ee12b079 86:a26f5f22631d 554 cs_sd = 1;
ee12b079 86:a26f5f22631d 555 spi.write(0xFF);
ee12b079 86:a26f5f22631d 556 return 1;
ee12b079 86:a26f5f22631d 557 }
ee12b079 86:a26f5f22631d 558
ee12b079 86:a26f5f22631d 559 // wait for write to finish
ee12b079 86:a26f5f22631d 560 while (spi.write(0xFF) == 0);
ee12b079 86:a26f5f22631d 561
ee12b079 86:a26f5f22631d 562 cs_sd = 1;
ee12b079 86:a26f5f22631d 563 spi.write(0xFF);
ee12b079 86:a26f5f22631d 564 return 0;
ee12b079 86:a26f5f22631d 565 }
ee12b079 86:a26f5f22631d 566
ee12b079 86:a26f5f22631d 567 int disk_read(uint8_t *buffer, uint64_t block_number)
ee12b079 86:a26f5f22631d 568 {
ee12b079 86:a26f5f22631d 569 // set read address for single block (CMD17)
ee12b079 86:a26f5f22631d 570 if (cmd(17, block_number * cdv) != 0) {
ee12b079 86:a26f5f22631d 571 return 1;
ee12b079 86:a26f5f22631d 572 }
ee12b079 86:a26f5f22631d 573
ee12b079 86:a26f5f22631d 574 // receive the data
ee12b079 86:a26f5f22631d 575 read(buffer, 512);
ee12b079 86:a26f5f22631d 576 return 0;
ee12b079 86:a26f5f22631d 577 }
ee12b079 86:a26f5f22631d 578
ee12b079 86:a26f5f22631d 579 int read(uint8_t *buffer, uint32_t length)
ee12b079 86:a26f5f22631d 580 {
ee12b079 86:a26f5f22631d 581 cs_sd = 0;
ee12b079 86:a26f5f22631d 582
ee12b079 86:a26f5f22631d 583 // read until start byte (0xFF)
ee12b079 86:a26f5f22631d 584 while (spi.write(0xFF) != 0xFE);
ee12b079 86:a26f5f22631d 585
ee12b079 86:a26f5f22631d 586 // read data
ee12b079 86:a26f5f22631d 587 for (int i = 0; i < length; i++) {
ee12b079 86:a26f5f22631d 588 buffer[i] = spi.write(0xFF);
ee12b079 86:a26f5f22631d 589 }
ee12b079 86:a26f5f22631d 590 spi.write(0xFF); // checksum
ee12b079 86:a26f5f22631d 591 spi.write(0xFF);
ee12b079 86:a26f5f22631d 592
ee12b079 86:a26f5f22631d 593 cs_sd = 1;
ee12b079 86:a26f5f22631d 594 spi.write(0xFF);
ee12b079 86:a26f5f22631d 595 return 0;
ee12b079 86:a26f5f22631d 596 }
ee12b079 86:a26f5f22631d 597
ee12b079 86:a26f5f22631d 598 int disk_erase(int startBlock, int totalBlocks)
ee12b079 86:a26f5f22631d 599 {
ee12b079 86:a26f5f22631d 600 if(cmd(32, startBlock * cdv) != 0) {
ee12b079 86:a26f5f22631d 601 return 1;
ee12b079 86:a26f5f22631d 602 }
ee12b079 86:a26f5f22631d 603 if (cmd(33, (startBlock+totalBlocks-1) * cdv) != 0) {
ee12b079 86:a26f5f22631d 604 return 1;
ee12b079 86:a26f5f22631d 605 }
ee12b079 86:a26f5f22631d 606 if (cmd(38,0) != 0) {
ee12b079 86:a26f5f22631d 607 return 1;
ee12b079 86:a26f5f22631d 608 }
ee12b079 86:a26f5f22631d 609
ee12b079 86:a26f5f22631d 610 return 0; //normal return
ee12b079 92:bce22b38c440 611 }