I2C code testing

Dependencies:   FreescaleIAP SimpleDMA mbed-rtos mbed

Fork of COM_MNG_TMTC_SIMPLE_pl123 by Siva ram

Committer:
ee12b079
Date:
Tue Jan 19 11:06:00 2016 +0000
Revision:
82:c55f29ad4240
updated SD card  and files

Who changed what in which revision?

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