things are working

Dependencies:   FreescaleIAP mbed-rtos mbed

Fork of CDMS_DEC_2016_jan by Team Fox

Committer:
pradeepvk2208
Date:
Thu Jan 21 14:46:28 2016 +0000
Revision:
1:ad3b8a8032e2
Parent:
0:bcbd76c86cde
cdms_updated

Who changed what in which revision?

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