working version
Dependencies: mbed mbed-rtos SimpleDMA FreescaleIAP eeprom
Fork of CDMS_CODE_FM_28JAN2017 by
cdms_sd.h@357:f3d48d62e00e, 2022-04-03 (annotated)
- Committer:
- samp1234
- Date:
- Sun Apr 03 15:35:53 2022 +0000
- Revision:
- 357:f3d48d62e00e
- Parent:
- 356:197c93dc2012
First commit test
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
samp1234 | 346:204497974293 | 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 | |
chaithanyarss | 261:1e54415b34d3 | 7 | #define SD_COMMAND_TIMEOUT 325 |
ee12b079 | 86:a26f5f22631d | 8 | |
samp1234 | 346:204497974293 | 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 | |
samp1234 | 346:204497974293 | 19 | |
samp1234 | 346:204497974293 | 20 | |
samp1234 | 346:204497974293 | 21 | #define SD_MAX_CYCLES 10000 |
samp1234 | 346:204497974293 | 22 | |
samp1234 | 354:eb6c863756a8 | 23 | uint32_t SD_SCP_FIRST=10001; |
samp1234 | 354:eb6c863756a8 | 24 | uint32_t SD_SCP_LAST=20000; |
samp1234 | 356:197c93dc2012 | 25 | uint32_t SD_SFF_AT_FIRST=30001; |
samp1234 | 356:197c93dc2012 | 26 | uint32_t SD_SFF_AT_LAST = 40000; |
samp1234 | 356:197c93dc2012 | 27 | uint32_t SD_SFF_BT_FIRST =20001; |
samp1234 | 356:197c93dc2012 | 28 | uint32_t SD_SFF_BT_LAST=30000; |
samp1234 | 354:eb6c863756a8 | 29 | uint32_t SD_HK_ARCH_FIRST=40001; |
samp1234 | 354:eb6c863756a8 | 30 | uint32_t SD_HK_ARCH_LAST= 50000; |
samp1234 | 354:eb6c863756a8 | 31 | uint32_t LOG_FIRST =50001; |
samp1234 | 354:eb6c863756a8 | 32 | uint32_t LOG_LAST=60000; |
samp1234 | 354:eb6c863756a8 | 33 | uint32_t SD_MNG_SECT = 80000; |
ee12b079 | 86:a26f5f22631d | 34 | |
ee12b079 | 194:ab528911780d | 35 | extern uint8_t SD_INIT_FLAGS; |
ee12b079 | 86:a26f5f22631d | 36 | |
ee12b079 | 86:a26f5f22631d | 37 | int initialise_card(); |
ee12b079 | 86:a26f5f22631d | 38 | int initialise_card_v1(); |
ee12b079 | 86:a26f5f22631d | 39 | int initialise_card_v2(); |
ee12b079 | 86:a26f5f22631d | 40 | int disk_write(const uint8_t *, uint64_t); |
ee12b079 | 86:a26f5f22631d | 41 | int disk_read(uint8_t *, uint64_t); |
ee12b079 | 86:a26f5f22631d | 42 | int disk_erase(int,int); |
ee12b079 | 209:63e9c8f8b5d2 | 43 | int disk_read_statusbits(uint8_t *); |
ee12b079 | 86:a26f5f22631d | 44 | |
chaithanyarss | 326:e424f70b86c0 | 45 | void FCTN_SD_MNGR(); |
ee12b079 | 194:ab528911780d | 46 | int INCREMENT_SD_LIB(uint8_t); |
ee12b079 | 86:a26f5f22631d | 47 | |
ee12b079 | 86:a26f5f22631d | 48 | |
ee12b079 | 86:a26f5f22631d | 49 | int cmd(int, int); |
ee12b079 | 86:a26f5f22631d | 50 | int cmd58(); |
ee12b079 | 86:a26f5f22631d | 51 | int cmdx(int, int); |
ee12b079 | 86:a26f5f22631d | 52 | int cmd8(); |
ee12b079 | 86:a26f5f22631d | 53 | int read(uint8_t*, uint32_t ); |
ee12b079 | 86:a26f5f22631d | 54 | int write(const uint8_t*, uint32_t ); |
ee12b079 | 86:a26f5f22631d | 55 | static uint32_t ext_bits(unsigned char *, int , int ); |
ee12b079 | 92:bce22b38c440 | 56 | int SD_WRITE(uint8_t*,uint32_t,uint8_t); |
ee12b079 | 194:ab528911780d | 57 | int FCTN_CDMS_SD_INIT(); |
ee12b079 | 195:6a549c0e9287 | 58 | uint8_t SD_READ(uint8_t*,uint32_t,uint8_t); |
chaithanyarss | 261:1e54415b34d3 | 59 | #define SDCARD_FAIL 4 |
ee12b079 | 86:a26f5f22631d | 60 | #define SDCARD_V1 1 |
ee12b079 | 86:a26f5f22631d | 61 | #define SDCARD_V2 2 |
ee12b079 | 86:a26f5f22631d | 62 | #define SDCARD_V2HC 3 |
ee12b079 | 86:a26f5f22631d | 63 | |
ee12b079 | 86:a26f5f22631d | 64 | int cdv; |
ee12b079 | 86:a26f5f22631d | 65 | uint64_t sd_sectors(); |
ee12b079 | 86:a26f5f22631d | 66 | uint64_t sectors; |
ee12b079 | 86:a26f5f22631d | 67 | |
ee12b079 | 194:ab528911780d | 68 | int FCTN_CDMS_SD_INIT() |
samp1234 | 346:204497974293 | 69 | { |
ee12b079 | 194:ab528911780d | 70 | int i = initialise_card(); |
chaithanyarss | 261:1e54415b34d3 | 71 | if( i == 4) |
samp1234 | 346:204497974293 | 72 | return 4; |
ee12b079 | 194:ab528911780d | 73 | debug_if(SD_DBG, "init card = %d\n", i); |
ee12b079 | 194:ab528911780d | 74 | sectors = sd_sectors(); |
ee12b079 | 194:ab528911780d | 75 | |
ee12b079 | 194:ab528911780d | 76 | // Set block length to 512 (CMD16) |
ee12b079 | 194:ab528911780d | 77 | if (cmd(16, 512) != 0) { |
ee12b079 | 194:ab528911780d | 78 | debug("\rSet 512-byte block timed out\r\n"); |
ee12b079 | 194:ab528911780d | 79 | return 1; |
ee12b079 | 194:ab528911780d | 80 | } else { |
samp1234 | 346:204497974293 | 81 | //printf("\rDisk initialization successfull\r\n"); |
ee12b079 | 194:ab528911780d | 82 | } |
chaithanyarss | 261:1e54415b34d3 | 83 | SD_STATUS = DEVICE_POWERED; |
ee12b079 | 194:ab528911780d | 84 | spi.frequency(1000000); // Set to 1MHz for data transfer |
ee12b079 | 194:ab528911780d | 85 | return 0; |
ee12b079 | 86:a26f5f22631d | 86 | } |
ee12b079 | 86:a26f5f22631d | 87 | |
ee12b079 | 209:63e9c8f8b5d2 | 88 | void FCTN_SD_MNGR() |
ee12b079 | 86:a26f5f22631d | 89 | { |
ee12b079 | 92:bce22b38c440 | 90 | uint32_t fsc; |
samp1234 | 346:204497974293 | 91 | uint32_t start_fsc; |
ee12b079 | 92:bce22b38c440 | 92 | uint8_t buffer[512]; |
chaithanyarss | 261:1e54415b34d3 | 93 | int b; |
samp1234 | 346:204497974293 | 94 | if(SD_STATUS == DEVICE_POWERED){ |
samp1234 | 346:204497974293 | 95 | b=disk_read(buffer, SD_MNG_SECT); |
samp1234 | 346:204497974293 | 96 | |
samp1234 | 346:204497974293 | 97 | fsc=(uint32_t)(buffer[0]<<24)+(uint32_t)(buffer[1]<<16)+(uint32_t)(buffer[2]<<8)+(uint32_t)buffer[3]; |
samp1234 | 346:204497974293 | 98 | start_fsc=(uint32_t)(buffer[4]<<24)+(uint32_t)(buffer[5]<<16)+(uint32_t)(buffer[6]<<8)+(uint32_t)buffer[7]; |
samp1234 | 346:204497974293 | 99 | FSC_CURRENT[1] = fsc; |
samp1234 | 346:204497974293 | 100 | FSC_OLD[1] = start_fsc; |
samp1234 | 346:204497974293 | 101 | |
samp1234 | 346:204497974293 | 102 | fsc=(uint32_t)(buffer[8]<<24)+(uint32_t)(buffer[9]<<16)+(uint32_t)(buffer[10]<<8)+(uint32_t)buffer[11]; |
samp1234 | 346:204497974293 | 103 | start_fsc=(uint32_t)(buffer[12]<<24)+(uint32_t)(buffer[13]<<16)+(uint32_t)(buffer[14]<<8)+(uint32_t)buffer[15]; |
samp1234 | 346:204497974293 | 104 | FSC_CURRENT[2] = fsc; |
samp1234 | 346:204497974293 | 105 | FSC_OLD[2] = start_fsc; |
ee12b079 | 195:6a549c0e9287 | 106 | |
samp1234 | 346:204497974293 | 107 | fsc=(uint32_t)(buffer[16]<<24)+(uint32_t)(buffer[17]<<16)+(uint32_t)(buffer[18]<<8)+(uint32_t)buffer[19]; |
samp1234 | 346:204497974293 | 108 | start_fsc=(uint32_t)(buffer[20]<<24)+(uint32_t)(buffer[21]<<16)+(uint32_t)(buffer[22]<<8)+(uint32_t)buffer[23]; |
samp1234 | 346:204497974293 | 109 | FSC_CURRENT[3] = fsc; |
samp1234 | 346:204497974293 | 110 | FSC_OLD[3] = start_fsc; |
samp1234 | 346:204497974293 | 111 | |
samp1234 | 346:204497974293 | 112 | fsc=(uint32_t)(buffer[24]<<24)+(uint32_t)(buffer[25]<<16)+(uint32_t)(buffer[26]<<8)+(uint32_t)buffer[27]; |
samp1234 | 346:204497974293 | 113 | start_fsc=(uint32_t)(buffer[28]<<24)+(uint32_t)(buffer[29]<<16)+(uint32_t)(buffer[30]<<8)+(uint32_t)buffer[31]; |
samp1234 | 346:204497974293 | 114 | FSC_CURRENT[4] = fsc; |
samp1234 | 346:204497974293 | 115 | FSC_OLD[4] = start_fsc; |
samp1234 | 346:204497974293 | 116 | |
samp1234 | 346:204497974293 | 117 | fsc=(uint32_t)(buffer[32]<<24)+(uint32_t)(buffer[33]<<16)+(uint32_t)(buffer[34]<<8)+(uint32_t)buffer[35]; |
samp1234 | 346:204497974293 | 118 | start_fsc=(uint32_t)(buffer[36]<<24)+(uint32_t)(buffer[37]<<16)+(uint32_t)(buffer[38]<<8)+(uint32_t)buffer[39]; |
samp1234 | 346:204497974293 | 119 | FSC_CURRENT[5] = fsc; |
samp1234 | 346:204497974293 | 120 | FSC_OLD[5] = start_fsc; |
chaithanyarss | 261:1e54415b34d3 | 121 | } |
ee12b079 | 194:ab528911780d | 122 | } |
ee12b079 | 194:ab528911780d | 123 | |
ee12b079 | 194:ab528911780d | 124 | int INCREMENT_SD_LIB(uint8_t sid) |
ee12b079 | 194:ab528911780d | 125 | { |
ee12b079 | 194:ab528911780d | 126 | uint32_t fsc; |
samp1234 | 346:204497974293 | 127 | uint32_t start_fsc; |
ee12b079 | 209:63e9c8f8b5d2 | 128 | int i; |
ee12b079 | 194:ab528911780d | 129 | uint8_t buffer[512]; |
samp1234 | 346:204497974293 | 130 | |
spacelab | 308:7c3872365d58 | 131 | disk_read(buffer,SD_MNG_SECT); |
ee12b079 | 209:63e9c8f8b5d2 | 132 | SD_MNG_SECT += SD_LIB_WRITES/(int)0xFFFF; |
samp1234 | 346:204497974293 | 133 | if(SD_MNG_SECT != SD_LIB_BLK_CURRENT) |
samp1234 | 346:204497974293 | 134 | { |
chaithanyarss | 324:c0a5228cc666 | 135 | SD_LIB_BLK_CURRENT = SD_MNG_SECT; |
samp1234 | 357:f3d48d62e00e | 136 | //FCTN_CDMS_WR_FLASH(3,SD_LIB_BLK_CURRENT); |
samp1234 | 357:f3d48d62e00e | 137 | WRITE_TO_EEPROM(3,SD_LIB_BLK_CURRENT); |
chaithanyarss | 324:c0a5228cc666 | 138 | } |
ee12b079 | 209:63e9c8f8b5d2 | 139 | SD_LIB_WRITES = SD_LIB_WRITES%(int)0xFFFF; |
spacelab | 308:7c3872365d58 | 140 | disk_write(buffer,SD_MNG_SECT); |
samp1234 | 346:204497974293 | 141 | // gPC.printf("in increment_SD_LIB = %d, %d,%d\n\r", FSC_CURRENT[1],FSC_CURRENT[2],FSC_CURRENT[3]); |
samp1234 | 346:204497974293 | 142 | // SD_MNG_SECT += SD_LIB_WRITES/(int)0xFFFF; |
samp1234 | 346:204497974293 | 143 | // SD_LIB_WRITES = SD_LIB_WRITES%(int)0xFFFF; |
samp1234 | 346:204497974293 | 144 | |
ee12b079 | 194:ab528911780d | 145 | disk_read(buffer,SD_MNG_SECT); |
samp1234 | 346:204497974293 | 146 | if(sid==0x01) |
samp1234 | 346:204497974293 | 147 | { |
samp1234 | 346:204497974293 | 148 | fsc=(uint32_t)(buffer[0]<<24)+(uint32_t)(buffer[1]<<16)+(uint32_t)(buffer[2]<<8)+(uint32_t)buffer[3]; |
samp1234 | 346:204497974293 | 149 | start_fsc=(uint32_t)(buffer[4]<<24)+(uint32_t)(buffer[5]<<16)+(uint32_t)(buffer[6]<<8)+(uint32_t)buffer[7]; |
samp1234 | 346:204497974293 | 150 | fsc++; |
samp1234 | 346:204497974293 | 151 | buffer[0]=(uint8_t) (fsc>>24 & 0xFF); |
samp1234 | 346:204497974293 | 152 | buffer[1]=(uint8_t) (fsc>>16 & 0xFF); |
samp1234 | 346:204497974293 | 153 | buffer[2]=(uint8_t) (fsc>>8 & 0xFF); |
samp1234 | 346:204497974293 | 154 | buffer[3]=(uint8_t) (fsc & 0xFF); |
samp1234 | 346:204497974293 | 155 | if(fsc > SD_SCP_LAST-SD_SCP_FIRST+1) |
samp1234 | 346:204497974293 | 156 | { |
samp1234 | 346:204497974293 | 157 | start_fsc = start_fsc+1; |
samp1234 | 346:204497974293 | 158 | buffer[4]=(uint8_t) (start_fsc>>24 & 0xFF); |
samp1234 | 346:204497974293 | 159 | buffer[5]=(uint8_t) (start_fsc>>16 & 0xFF); |
samp1234 | 346:204497974293 | 160 | buffer[6]=(uint8_t) (start_fsc>>8 & 0xFF); |
samp1234 | 346:204497974293 | 161 | buffer[7]=(uint8_t) (start_fsc & 0xFF); |
samp1234 | 346:204497974293 | 162 | } |
samp1234 | 346:204497974293 | 163 | |
samp1234 | 346:204497974293 | 164 | i = disk_write(buffer,SD_MNG_SECT); |
samp1234 | 346:204497974293 | 165 | if(i == 0) |
samp1234 | 346:204497974293 | 166 | { |
samp1234 | 346:204497974293 | 167 | FSC_CURRENT[1] = fsc; |
samp1234 | 346:204497974293 | 168 | FSC_OLD[1] = start_fsc; |
samp1234 | 346:204497974293 | 169 | return i; |
samp1234 | 346:204497974293 | 170 | } |
samp1234 | 346:204497974293 | 171 | } |
samp1234 | 346:204497974293 | 172 | if(sid==0x02) |
chaithanyarss | 328:2242ebc71be8 | 173 | { |
samp1234 | 346:204497974293 | 174 | fsc=(uint32_t)(buffer[8]<<24)+(uint32_t)(buffer[9]<<16)+(uint32_t)(buffer[10]<<8)+(uint32_t)buffer[11]; |
samp1234 | 346:204497974293 | 175 | start_fsc=(uint32_t)(buffer[12]<<24)+(uint32_t)(buffer[13]<<16)+(uint32_t)(buffer[14]<<8)+(uint32_t)buffer[15]; |
samp1234 | 346:204497974293 | 176 | fsc++; |
samp1234 | 346:204497974293 | 177 | buffer[8]=(uint8_t) (fsc>>24 & 0xFF); |
samp1234 | 346:204497974293 | 178 | buffer[9]=(uint8_t) (fsc>>16 & 0xFF); |
samp1234 | 346:204497974293 | 179 | buffer[10]=(uint8_t) (fsc>>8 & 0xFF); |
samp1234 | 346:204497974293 | 180 | buffer[11]=(uint8_t) (fsc & 0xFF); |
samp1234 | 346:204497974293 | 181 | if(fsc > SD_SFF_AT_LAST-SD_SFF_AT_FIRST+1) |
samp1234 | 346:204497974293 | 182 | { |
samp1234 | 346:204497974293 | 183 | start_fsc = start_fsc+1; |
samp1234 | 346:204497974293 | 184 | buffer[12]=(uint8_t) (start_fsc>>24 & 0xFF); |
samp1234 | 346:204497974293 | 185 | buffer[13]=(uint8_t) (start_fsc>>16 & 0xFF); |
samp1234 | 346:204497974293 | 186 | buffer[14]=(uint8_t) (start_fsc>>8 & 0xFF); |
samp1234 | 346:204497974293 | 187 | buffer[15]=(uint8_t) (start_fsc & 0xFF); |
samp1234 | 346:204497974293 | 188 | } |
samp1234 | 346:204497974293 | 189 | i = disk_write(buffer,SD_MNG_SECT); |
samp1234 | 346:204497974293 | 190 | if(i == 0) |
samp1234 | 346:204497974293 | 191 | { |
samp1234 | 346:204497974293 | 192 | FSC_CURRENT[2] = fsc; |
samp1234 | 346:204497974293 | 193 | FSC_OLD[2] = start_fsc; |
samp1234 | 346:204497974293 | 194 | return i; |
samp1234 | 346:204497974293 | 195 | } |
samp1234 | 346:204497974293 | 196 | } |
samp1234 | 346:204497974293 | 197 | if(sid==0x03) |
samp1234 | 346:204497974293 | 198 | { |
samp1234 | 346:204497974293 | 199 | fsc=(uint32_t)(buffer[16]<<24)+(uint32_t)(buffer[17]<<16)+(uint32_t)(buffer[18]<<8)+(uint32_t)buffer[19]; |
samp1234 | 346:204497974293 | 200 | start_fsc=(uint32_t)(buffer[20]<<24)+(uint32_t)(buffer[21]<<16)+(uint32_t)(buffer[22]<<8)+(uint32_t)buffer[23]; |
ee12b079 | 203:424308159a56 | 201 | fsc++; |
samp1234 | 346:204497974293 | 202 | buffer[16]=(uint8_t) (fsc>>24 & 0xFF); |
samp1234 | 346:204497974293 | 203 | buffer[17]=(uint8_t) (fsc>>16 & 0xFF); |
samp1234 | 346:204497974293 | 204 | buffer[18]=(uint8_t) (fsc>>8 & 0xFF); |
samp1234 | 346:204497974293 | 205 | buffer[19]=(uint8_t) (fsc & 0xFF); |
samp1234 | 346:204497974293 | 206 | if(fsc > SD_SFF_BT_LAST-SD_SFF_BT_FIRST+1) |
samp1234 | 346:204497974293 | 207 | { |
samp1234 | 346:204497974293 | 208 | start_fsc = start_fsc+1; |
samp1234 | 346:204497974293 | 209 | buffer[20]=(uint8_t) (start_fsc>>24 & 0xFF); |
samp1234 | 346:204497974293 | 210 | buffer[21]=(uint8_t) (start_fsc>>16 & 0xFF); |
samp1234 | 346:204497974293 | 211 | buffer[22]=(uint8_t) (start_fsc>>8 & 0xFF); |
samp1234 | 346:204497974293 | 212 | buffer[23]=(uint8_t) (start_fsc & 0xFF); |
samp1234 | 346:204497974293 | 213 | } |
samp1234 | 346:204497974293 | 214 | i = disk_write(buffer,SD_MNG_SECT); |
samp1234 | 346:204497974293 | 215 | if(i == 0) |
samp1234 | 346:204497974293 | 216 | { |
samp1234 | 346:204497974293 | 217 | FSC_CURRENT[3] = fsc; |
samp1234 | 346:204497974293 | 218 | FSC_OLD[3] = start_fsc; |
samp1234 | 346:204497974293 | 219 | return i; |
ee12b079 | 194:ab528911780d | 220 | } |
samp1234 | 346:204497974293 | 221 | } |
samp1234 | 346:204497974293 | 222 | if(sid==0x04) |
samp1234 | 346:204497974293 | 223 | { |
samp1234 | 346:204497974293 | 224 | fsc=(uint32_t)(buffer[24]<<24)+(uint32_t)(buffer[25]<<16)+(uint32_t)(buffer[26]<<8)+(uint32_t)buffer[27]; |
samp1234 | 346:204497974293 | 225 | start_fsc=(uint32_t)(buffer[28]<<24)+(uint32_t)(buffer[29]<<16)+(uint32_t)(buffer[30]<<8)+(uint32_t)buffer[31]; |
samp1234 | 346:204497974293 | 226 | fsc++; |
samp1234 | 346:204497974293 | 227 | buffer[24]=(uint8_t) (fsc>>24 & 0xFF); |
samp1234 | 346:204497974293 | 228 | buffer[25]=(uint8_t) (fsc>>16 & 0xFF); |
samp1234 | 346:204497974293 | 229 | buffer[26]=(uint8_t) (fsc>>8 & 0xFF); |
samp1234 | 346:204497974293 | 230 | buffer[27]=(uint8_t) (fsc & 0xFF); |
samp1234 | 346:204497974293 | 231 | if(fsc > SD_HK_ARCH_LAST-SD_HK_ARCH_FIRST+1) |
samp1234 | 346:204497974293 | 232 | { |
samp1234 | 346:204497974293 | 233 | start_fsc = start_fsc+1; |
samp1234 | 346:204497974293 | 234 | buffer[28]=(uint8_t) (start_fsc>>24 & 0xFF); |
samp1234 | 346:204497974293 | 235 | buffer[29]=(uint8_t) (start_fsc>>16 & 0xFF); |
samp1234 | 346:204497974293 | 236 | buffer[30]=(uint8_t) (start_fsc>>8 & 0xFF); |
samp1234 | 346:204497974293 | 237 | buffer[31]=(uint8_t) (start_fsc & 0xFF); |
samp1234 | 346:204497974293 | 238 | } |
ee12b079 | 203:424308159a56 | 239 | i = disk_write(buffer,SD_MNG_SECT); |
samp1234 | 346:204497974293 | 240 | if(i == 0) |
samp1234 | 346:204497974293 | 241 | { |
samp1234 | 346:204497974293 | 242 | FSC_CURRENT[4] = fsc; |
samp1234 | 346:204497974293 | 243 | FSC_OLD[4] = start_fsc; |
samp1234 | 346:204497974293 | 244 | return i; |
samp1234 | 346:204497974293 | 245 | } |
samp1234 | 346:204497974293 | 246 | } |
samp1234 | 346:204497974293 | 247 | if(sid==0x05) |
samp1234 | 346:204497974293 | 248 | { |
samp1234 | 346:204497974293 | 249 | fsc=(uint32_t)(buffer[32]<<24)+(uint32_t)(buffer[33]<<16)+(uint32_t)(buffer[34]<<8)+(uint32_t)buffer[35]; |
samp1234 | 346:204497974293 | 250 | start_fsc=(uint32_t)(buffer[36]<<24)+(uint32_t)(buffer[37]<<16)+(uint32_t)(buffer[38]<<8)+(uint32_t)buffer[39]; |
samp1234 | 346:204497974293 | 251 | fsc++; |
samp1234 | 346:204497974293 | 252 | buffer[32]=(uint8_t) (fsc>>24 & 0xFF); |
samp1234 | 346:204497974293 | 253 | buffer[33]=(uint8_t) (fsc>>16 & 0xFF); |
samp1234 | 346:204497974293 | 254 | buffer[34]=(uint8_t) (fsc>>8 & 0xFF); |
samp1234 | 346:204497974293 | 255 | buffer[35]=(uint8_t) (fsc & 0xFF); |
samp1234 | 346:204497974293 | 256 | if(fsc > LOG_LAST-LOG_FIRST+1) |
samp1234 | 346:204497974293 | 257 | { |
samp1234 | 346:204497974293 | 258 | start_fsc = start_fsc+1; |
samp1234 | 346:204497974293 | 259 | buffer[36]=(uint8_t) (start_fsc>>24 & 0xFF); |
samp1234 | 346:204497974293 | 260 | buffer[37]=(uint8_t) (start_fsc>>16 & 0xFF); |
samp1234 | 346:204497974293 | 261 | buffer[38]=(uint8_t) (start_fsc>>8 & 0xFF); |
samp1234 | 346:204497974293 | 262 | buffer[39]=(uint8_t) (start_fsc & 0xFF); |
samp1234 | 346:204497974293 | 263 | } |
samp1234 | 346:204497974293 | 264 | i = disk_write(buffer,SD_MNG_SECT); |
samp1234 | 346:204497974293 | 265 | if(i == 0) |
samp1234 | 346:204497974293 | 266 | { |
samp1234 | 346:204497974293 | 267 | FSC_CURRENT[5] = fsc; |
samp1234 | 346:204497974293 | 268 | FSC_OLD[5] = start_fsc; |
ee12b079 | 209:63e9c8f8b5d2 | 269 | return i; |
ee12b079 | 209:63e9c8f8b5d2 | 270 | } |
ee12b079 | 194:ab528911780d | 271 | } |
ee12b079 | 194:ab528911780d | 272 | return -1; |
ee12b079 | 86:a26f5f22631d | 273 | } |
ee12b079 | 86:a26f5f22631d | 274 | |
ee12b079 | 86:a26f5f22631d | 275 | |
ee12b079 | 92:bce22b38c440 | 276 | int SD_WRITE(uint8_t* buffer,uint32_t fsc,uint8_t sid) |
ee12b079 | 86:a26f5f22631d | 277 | { |
spacelab | 308:7c3872365d58 | 278 | uint32_t block_number; |
ee12b079 | 194:ab528911780d | 279 | int result = 10; |
samp1234 | 346:204497974293 | 280 | if(SD_STATUS == DEVICE_POWERED){ |
samp1234 | 346:204497974293 | 281 | if(sid==0x01) |
samp1234 | 346:204497974293 | 282 | { |
samp1234 | 346:204497974293 | 283 | block_number=SD_SCP_FIRST+(fsc%(SD_SCP_LAST-SD_SCP_FIRST+1))-1; |
samp1234 | 346:204497974293 | 284 | //block_number=SD_SCP_FIRST+fsc; |
samp1234 | 346:204497974293 | 285 | result= disk_write(buffer,block_number); |
samp1234 | 346:204497974293 | 286 | if(result == 0) |
samp1234 | 346:204497974293 | 287 | { |
samp1234 | 346:204497974293 | 288 | if(INCREMENT_SD_LIB(sid) == 0) |
samp1234 | 346:204497974293 | 289 | SD_LIB_WRITES++; |
chaithanyarss | 327:5a967a66c10f | 290 | } |
samp1234 | 346:204497974293 | 291 | return result; |
samp1234 | 346:204497974293 | 292 | } |
samp1234 | 346:204497974293 | 293 | if(sid==0x02) |
samp1234 | 346:204497974293 | 294 | { |
samp1234 | 346:204497974293 | 295 | block_number= SD_SFF_AT_FIRST+(fsc%(SD_SFF_AT_LAST - SD_SFF_AT_FIRST+1))-1; |
samp1234 | 346:204497974293 | 296 | //block_number= SD_SFF_AT_FIRST+fsc; |
samp1234 | 346:204497974293 | 297 | result= disk_write(buffer,block_number); |
samp1234 | 346:204497974293 | 298 | if(result == 0) |
samp1234 | 346:204497974293 | 299 | { |
samp1234 | 346:204497974293 | 300 | if(INCREMENT_SD_LIB(sid) == 0) |
samp1234 | 346:204497974293 | 301 | SD_LIB_WRITES++; |
ee12b079 | 209:63e9c8f8b5d2 | 302 | } |
samp1234 | 346:204497974293 | 303 | return result; |
samp1234 | 346:204497974293 | 304 | } |
samp1234 | 346:204497974293 | 305 | if(sid==0x03) |
samp1234 | 346:204497974293 | 306 | { |
samp1234 | 346:204497974293 | 307 | block_number= SD_SFF_BT_FIRST +(fsc%(SD_SFF_BT_LAST - SD_SFF_BT_FIRST +1))-1; |
samp1234 | 346:204497974293 | 308 | //block_number= SD_SFF_BT_FIRST +fsc; |
samp1234 | 346:204497974293 | 309 | result= disk_write(buffer,block_number); |
samp1234 | 346:204497974293 | 310 | if(result == 0) |
samp1234 | 346:204497974293 | 311 | { |
samp1234 | 346:204497974293 | 312 | if(INCREMENT_SD_LIB(sid) == 0) |
samp1234 | 346:204497974293 | 313 | SD_LIB_WRITES++; |
ee12b079 | 209:63e9c8f8b5d2 | 314 | } |
samp1234 | 346:204497974293 | 315 | return result; |
samp1234 | 346:204497974293 | 316 | } |
samp1234 | 346:204497974293 | 317 | if(sid==0x04) |
samp1234 | 346:204497974293 | 318 | { |
samp1234 | 346:204497974293 | 319 | block_number=SD_HK_ARCH_FIRST +(fsc%(SD_HK_ARCH_LAST - SD_HK_ARCH_FIRST +1))-1; |
samp1234 | 346:204497974293 | 320 | //block_number=SD_HK_ARCH_FIRST +fsc; |
samp1234 | 346:204497974293 | 321 | result= disk_write(buffer,block_number); |
samp1234 | 346:204497974293 | 322 | if(result == 0) |
samp1234 | 346:204497974293 | 323 | { |
samp1234 | 346:204497974293 | 324 | if(INCREMENT_SD_LIB(sid) == 0) |
samp1234 | 346:204497974293 | 325 | SD_LIB_WRITES++; |
chaithanyarss | 327:5a967a66c10f | 326 | } |
samp1234 | 346:204497974293 | 327 | return result; |
samp1234 | 346:204497974293 | 328 | } |
samp1234 | 346:204497974293 | 329 | if(sid==0x05) |
samp1234 | 346:204497974293 | 330 | { |
samp1234 | 356:197c93dc2012 | 331 | block_number= LOG_FIRST +(fsc%(LOG_LAST - LOG_FIRST +1))-1; |
samp1234 | 346:204497974293 | 332 | //block_number= LOG_FIRST +fsc; |
samp1234 | 346:204497974293 | 333 | result= disk_write(buffer,block_number); |
samp1234 | 346:204497974293 | 334 | if(result == 0) |
samp1234 | 346:204497974293 | 335 | { |
samp1234 | 346:204497974293 | 336 | if(INCREMENT_SD_LIB(sid) == 0) |
samp1234 | 346:204497974293 | 337 | SD_LIB_WRITES++; |
chaithanyarss | 327:5a967a66c10f | 338 | } |
samp1234 | 346:204497974293 | 339 | return result; |
ee12b079 | 86:a26f5f22631d | 340 | } |
samp1234 | 346:204497974293 | 341 | } |
samp1234 | 346:204497974293 | 342 | // return 1; |
ee12b079 | 86:a26f5f22631d | 343 | } |
ee12b079 | 86:a26f5f22631d | 344 | |
ee12b079 | 195:6a549c0e9287 | 345 | uint8_t SD_READ(uint8_t* buffer,uint32_t fsc,uint8_t sid) |
ee12b079 | 86:a26f5f22631d | 346 | { |
spacelab | 308:7c3872365d58 | 347 | FCTN_SD_MNGR(); |
ee12b079 | 209:63e9c8f8b5d2 | 348 | uint32_t block_number; |
ee12b079 | 209:63e9c8f8b5d2 | 349 | int result; |
ee12b079 | 295:699801854b71 | 350 | //if(SD_SW_EN_DS == 1) |
ee12b079 | 295:699801854b71 | 351 | // return 0x89; |
samp1234 | 346:204497974293 | 352 | if(sid==0x01) |
samp1234 | 346:204497974293 | 353 | { |
samp1234 | 346:204497974293 | 354 | if(!(FSC_OLD[1]<=fsc && fsc<=FSC_CURRENT[1])){ |
ee12b079 | 195:6a549c0e9287 | 355 | return 0x86; |
ee12b079 | 195:6a549c0e9287 | 356 | } |
samp1234 | 346:204497974293 | 357 | block_number=SD_SCP_FIRST+(fsc%(SD_SCP_LAST-SD_SCP_FIRST+1))-1; |
ee12b079 | 86:a26f5f22631d | 358 | result= disk_read(buffer,block_number); |
samp1234 | 346:204497974293 | 359 | } |
samp1234 | 346:204497974293 | 360 | else if(sid==0x02) |
samp1234 | 346:204497974293 | 361 | { |
samp1234 | 346:204497974293 | 362 | if(!(FSC_OLD[2]<=fsc && fsc<=FSC_CURRENT[2])){ |
ee12b079 | 195:6a549c0e9287 | 363 | return 0x86; |
ee12b079 | 195:6a549c0e9287 | 364 | } |
samp1234 | 346:204497974293 | 365 | block_number= SD_SFF_AT_FIRST+(fsc%(SD_SFF_AT_LAST - SD_SFF_AT_FIRST+1))-1; |
ee12b079 | 86:a26f5f22631d | 366 | result= disk_read(buffer,block_number); |
samp1234 | 346:204497974293 | 367 | } |
samp1234 | 346:204497974293 | 368 | else if(sid==0x03) |
samp1234 | 346:204497974293 | 369 | { |
samp1234 | 346:204497974293 | 370 | if(!(FSC_OLD[3]<=fsc && fsc<=FSC_CURRENT[3])){ |
ee12b079 | 195:6a549c0e9287 | 371 | return 0x86; |
ee12b079 | 195:6a549c0e9287 | 372 | } |
samp1234 | 346:204497974293 | 373 | block_number= SD_SFF_BT_FIRST +(fsc%(SD_SFF_BT_LAST - SD_SFF_BT_FIRST +1))-1; |
ee12b079 | 86:a26f5f22631d | 374 | result= disk_read(buffer,block_number); |
samp1234 | 346:204497974293 | 375 | } |
samp1234 | 346:204497974293 | 376 | else if(sid==0x04) |
samp1234 | 346:204497974293 | 377 | { |
samp1234 | 346:204497974293 | 378 | if(!(FSC_OLD[4]<=fsc && fsc<=FSC_CURRENT[4])){ |
ee12b079 | 195:6a549c0e9287 | 379 | return 0x86; |
ee12b079 | 195:6a549c0e9287 | 380 | } |
samp1234 | 346:204497974293 | 381 | block_number=SD_HK_ARCH_FIRST +(fsc%(SD_HK_ARCH_LAST - SD_HK_ARCH_FIRST +1))-1; |
ee12b079 | 86:a26f5f22631d | 382 | result= disk_read(buffer,block_number); |
samp1234 | 346:204497974293 | 383 | } |
samp1234 | 346:204497974293 | 384 | else if(sid==0x05) |
samp1234 | 346:204497974293 | 385 | { |
samp1234 | 346:204497974293 | 386 | if(!(FSC_OLD[5]<=fsc && fsc<=FSC_CURRENT[5])){ |
ee12b079 | 195:6a549c0e9287 | 387 | return 0x86; |
ee12b079 | 195:6a549c0e9287 | 388 | } |
samp1234 | 356:197c93dc2012 | 389 | block_number= LOG_FIRST +(fsc%(LOG_LAST - LOG_FIRST +1))-1; |
ee12b079 | 86:a26f5f22631d | 390 | result= disk_read(buffer,block_number); |
samp1234 | 346:204497974293 | 391 | } |
samp1234 | 346:204497974293 | 392 | else |
samp1234 | 346:204497974293 | 393 | { |
ee12b079 | 195:6a549c0e9287 | 394 | return 0x02; |
ee12b079 | 86:a26f5f22631d | 395 | } |
ee12b079 | 195:6a549c0e9287 | 396 | if(result == 0) |
ee12b079 | 195:6a549c0e9287 | 397 | return 0xA0; |
ee12b079 | 195:6a549c0e9287 | 398 | else |
spacelab | 308:7c3872365d58 | 399 | return 0x88; |
chaithanyarss | 304:7cc4fe191a54 | 400 | return 0xA0; |
ee12b079 | 86:a26f5f22631d | 401 | } |
ee12b079 | 86:a26f5f22631d | 402 | |
ee12b079 | 86:a26f5f22631d | 403 | |
ee12b079 | 86:a26f5f22631d | 404 | int initialise_card() |
ee12b079 | 86:a26f5f22631d | 405 | { |
ee12b079 | 86:a26f5f22631d | 406 | // Set to 100kHz for initialisation, and clock card with cs_sd = 1 |
samp1234 | 346:204497974293 | 407 | spi.frequency(100000); // changed on 31 12 2015 to 1 MHz |
ee12b079 | 86:a26f5f22631d | 408 | cs_sd = 1; |
ee12b079 | 86:a26f5f22631d | 409 | for (int i = 0; i < 16; i++) { |
ee12b079 | 86:a26f5f22631d | 410 | spi.write(0xFF); |
ee12b079 | 86:a26f5f22631d | 411 | } |
ee12b079 | 194:ab528911780d | 412 | uint8_t R1_response = cmd(0,0); |
ee12b079 | 194:ab528911780d | 413 | gPC.printf("0x%02X",R1_response); |
ee12b079 | 86:a26f5f22631d | 414 | // send CMD0, should return with all zeros except IDLE STATE set (bit 0) |
ee12b079 | 194:ab528911780d | 415 | if (R1_response != R1_IDLE_STATE) { |
ee12b079 | 86:a26f5f22631d | 416 | debug("No disk, or could not put SD card in to spi idle state\r\n"); |
ee12b079 | 86:a26f5f22631d | 417 | return SDCARD_FAIL; |
samp1234 | 346:204497974293 | 418 | } |
samp1234 | 346:204497974293 | 419 | else |
samp1234 | 346:204497974293 | 420 | gPC.puts("SD Card is in IDLE state\n\r"); |
ee12b079 | 86:a26f5f22631d | 421 | |
ee12b079 | 194:ab528911780d | 422 | // send CMD8 to determine whther it is ver 2.x |
ee12b079 | 86:a26f5f22631d | 423 | int r = cmd8(); |
ee12b079 | 86:a26f5f22631d | 424 | if (r == R1_IDLE_STATE) { |
ee12b079 | 194:ab528911780d | 425 | gPC.puts("Entering V2\r"); |
ee12b079 | 194:ab528911780d | 426 | int q = initialise_card_v2(); |
ee12b079 | 194:ab528911780d | 427 | return q; |
ee12b079 | 86:a26f5f22631d | 428 | |
ee12b079 | 86:a26f5f22631d | 429 | } else if (r == (R1_IDLE_STATE | R1_ILLEGAL_COMMAND)) { |
ee12b079 | 194:ab528911780d | 430 | gPC.puts("Entering V1"); |
ee12b079 | 86:a26f5f22631d | 431 | return initialise_card_v1(); |
ee12b079 | 86:a26f5f22631d | 432 | |
ee12b079 | 86:a26f5f22631d | 433 | } else { |
ee12b079 | 86:a26f5f22631d | 434 | debug("\rNot in idle state after sending CMD8 (not an SD card?)\r\n"); |
ee12b079 | 86:a26f5f22631d | 435 | return SDCARD_FAIL; |
ee12b079 | 86:a26f5f22631d | 436 | } |
ee12b079 | 86:a26f5f22631d | 437 | } |
ee12b079 | 86:a26f5f22631d | 438 | |
ee12b079 | 86:a26f5f22631d | 439 | int initialise_card_v1() |
ee12b079 | 86:a26f5f22631d | 440 | { |
ee12b079 | 86:a26f5f22631d | 441 | for (int i = 0; i < SD_COMMAND_TIMEOUT; i++) { |
ee12b079 | 86:a26f5f22631d | 442 | cmd(55, 0); |
ee12b079 | 86:a26f5f22631d | 443 | if (cmd(41, 0) == 0) { |
ee12b079 | 96:4ca92f9775e0 | 444 | gPC.puts("\rv1 initialization successfull\r\n"); |
ee12b079 | 86:a26f5f22631d | 445 | cdv = 512; |
ee12b079 | 86:a26f5f22631d | 446 | debug_if(SD_DBG, "\n\rInit: SEDCARD_V1\n\r"); |
ee12b079 | 86:a26f5f22631d | 447 | return SDCARD_V1; |
ee12b079 | 86:a26f5f22631d | 448 | } |
ee12b079 | 86:a26f5f22631d | 449 | } |
ee12b079 | 86:a26f5f22631d | 450 | |
ee12b079 | 86:a26f5f22631d | 451 | debug("\rTimeout waiting for v1.x card\r\n"); |
ee12b079 | 86:a26f5f22631d | 452 | return SDCARD_FAIL; |
ee12b079 | 86:a26f5f22631d | 453 | } |
ee12b079 | 86:a26f5f22631d | 454 | |
ee12b079 | 86:a26f5f22631d | 455 | |
ee12b079 | 86:a26f5f22631d | 456 | int initialise_card_v2() |
ee12b079 | 86:a26f5f22631d | 457 | { |
ee12b079 | 86:a26f5f22631d | 458 | for (int i = 0; i < SD_COMMAND_TIMEOUT; i++) { |
ee12b079 | 86:a26f5f22631d | 459 | wait_ms(50); |
ee12b079 | 86:a26f5f22631d | 460 | cmd58(); |
ee12b079 | 86:a26f5f22631d | 461 | cmd(55, 0); |
ee12b079 | 86:a26f5f22631d | 462 | if (cmd(41, 0x40000000) == 0) { |
ee12b079 | 194:ab528911780d | 463 | if (DEBUG) |
ee12b079 | 194:ab528911780d | 464 | gPC.puts("\rv2 initialization successfull\r\n"); |
ee12b079 | 86:a26f5f22631d | 465 | cmd58(); |
ee12b079 | 86:a26f5f22631d | 466 | debug_if(SD_DBG, "\n\rInit: SDCARD_V2\n\r"); |
ee12b079 | 86:a26f5f22631d | 467 | cdv = 1; |
ee12b079 | 322:7d906d34aaff | 468 | FCTN_SD_MNGR(); |
ee12b079 | 86:a26f5f22631d | 469 | return SDCARD_V2; |
ee12b079 | 86:a26f5f22631d | 470 | } |
ee12b079 | 86:a26f5f22631d | 471 | } |
ee12b079 | 86:a26f5f22631d | 472 | |
ee12b079 | 86:a26f5f22631d | 473 | debug("\rTimeout waiting for v2.x card\r\n"); |
ee12b079 | 86:a26f5f22631d | 474 | return SDCARD_FAIL; |
ee12b079 | 86:a26f5f22631d | 475 | } |
ee12b079 | 86:a26f5f22631d | 476 | |
ee12b079 | 86:a26f5f22631d | 477 | int cmd(int cmd, int arg) |
ee12b079 | 86:a26f5f22631d | 478 | { |
ee12b079 | 86:a26f5f22631d | 479 | cs_sd = 0; |
ee12b079 | 86:a26f5f22631d | 480 | |
ee12b079 | 86:a26f5f22631d | 481 | // send a command |
ee12b079 | 86:a26f5f22631d | 482 | spi.write(0x40 | cmd); |
ee12b079 | 86:a26f5f22631d | 483 | spi.write(arg >> 24); |
ee12b079 | 86:a26f5f22631d | 484 | spi.write(arg >> 16); |
ee12b079 | 86:a26f5f22631d | 485 | spi.write(arg >> 8); |
ee12b079 | 86:a26f5f22631d | 486 | spi.write(arg >> 0); |
ee12b079 | 86:a26f5f22631d | 487 | spi.write(0x95); |
ee12b079 | 86:a26f5f22631d | 488 | |
ee12b079 | 86:a26f5f22631d | 489 | // wait for the repsonse (response[7] == 0) |
ee12b079 | 86:a26f5f22631d | 490 | for (int i = 0; i < SD_COMMAND_TIMEOUT; i++) { |
ee12b079 | 86:a26f5f22631d | 491 | int response = spi.write(0xFF); |
ee12b079 | 86:a26f5f22631d | 492 | if (!(response & 0x80)) { |
ee12b079 | 86:a26f5f22631d | 493 | cs_sd = 1; |
ee12b079 | 86:a26f5f22631d | 494 | spi.write(0xFF); |
ee12b079 | 86:a26f5f22631d | 495 | return response; |
ee12b079 | 86:a26f5f22631d | 496 | } |
ee12b079 | 86:a26f5f22631d | 497 | } |
ee12b079 | 86:a26f5f22631d | 498 | cs_sd = 1; |
ee12b079 | 86:a26f5f22631d | 499 | spi.write(0xFF); |
ee12b079 | 86:a26f5f22631d | 500 | return -1; // timeout |
ee12b079 | 86:a26f5f22631d | 501 | } |
ee12b079 | 86:a26f5f22631d | 502 | |
ee12b079 | 86:a26f5f22631d | 503 | |
ee12b079 | 86:a26f5f22631d | 504 | int cmd58() |
ee12b079 | 86:a26f5f22631d | 505 | { |
ee12b079 | 86:a26f5f22631d | 506 | cs_sd = 0; |
ee12b079 | 86:a26f5f22631d | 507 | int arg = 0; |
ee12b079 | 86:a26f5f22631d | 508 | |
ee12b079 | 86:a26f5f22631d | 509 | // send a command |
ee12b079 | 86:a26f5f22631d | 510 | spi.write(0x40 | 58); |
ee12b079 | 86:a26f5f22631d | 511 | spi.write(arg >> 24); |
ee12b079 | 86:a26f5f22631d | 512 | spi.write(arg >> 16); |
ee12b079 | 86:a26f5f22631d | 513 | spi.write(arg >> 8); |
ee12b079 | 86:a26f5f22631d | 514 | spi.write(arg >> 0); |
ee12b079 | 86:a26f5f22631d | 515 | spi.write(0x95); |
ee12b079 | 86:a26f5f22631d | 516 | |
ee12b079 | 86:a26f5f22631d | 517 | // wait for the repsonse (response[7] == 0) |
ee12b079 | 86:a26f5f22631d | 518 | for (int i = 0; i < SD_COMMAND_TIMEOUT; i++) { |
ee12b079 | 86:a26f5f22631d | 519 | int response = spi.write(0xFF); |
ee12b079 | 86:a26f5f22631d | 520 | if (!(response & 0x80)) { |
ee12b079 | 86:a26f5f22631d | 521 | int ocr = spi.write(0xFF) << 24; |
ee12b079 | 86:a26f5f22631d | 522 | ocr |= spi.write(0xFF) << 16; |
ee12b079 | 86:a26f5f22631d | 523 | ocr |= spi.write(0xFF) << 8; |
ee12b079 | 86:a26f5f22631d | 524 | ocr |= spi.write(0xFF) << 0; |
ee12b079 | 86:a26f5f22631d | 525 | cs_sd = 1; |
ee12b079 | 86:a26f5f22631d | 526 | spi.write(0xFF); |
ee12b079 | 86:a26f5f22631d | 527 | return response; |
ee12b079 | 86:a26f5f22631d | 528 | } |
ee12b079 | 86:a26f5f22631d | 529 | } |
ee12b079 | 86:a26f5f22631d | 530 | cs_sd = 1; |
ee12b079 | 86:a26f5f22631d | 531 | spi.write(0xFF); |
ee12b079 | 86:a26f5f22631d | 532 | return -1; // timeout |
ee12b079 | 86:a26f5f22631d | 533 | } |
ee12b079 | 86:a26f5f22631d | 534 | |
ee12b079 | 86:a26f5f22631d | 535 | |
ee12b079 | 86:a26f5f22631d | 536 | int cmd8() |
ee12b079 | 86:a26f5f22631d | 537 | { |
ee12b079 | 86:a26f5f22631d | 538 | cs_sd = 0; |
ee12b079 | 86:a26f5f22631d | 539 | |
ee12b079 | 86:a26f5f22631d | 540 | // send a command |
ee12b079 | 86:a26f5f22631d | 541 | spi.write(0x40 | 8); // CMD8 |
ee12b079 | 86:a26f5f22631d | 542 | spi.write(0x00); // reserved |
ee12b079 | 86:a26f5f22631d | 543 | spi.write(0x00); // reserved |
ee12b079 | 86:a26f5f22631d | 544 | spi.write(0x01); // 3.3v |
ee12b079 | 86:a26f5f22631d | 545 | spi.write(0xAA); // check pattern |
ee12b079 | 86:a26f5f22631d | 546 | spi.write(0x87); // crc |
ee12b079 | 86:a26f5f22631d | 547 | |
ee12b079 | 86:a26f5f22631d | 548 | // wait for the repsonse (response[7] == 0) |
ee12b079 | 86:a26f5f22631d | 549 | for (int i = 0; i < SD_COMMAND_TIMEOUT * 1000; i++) { |
ee12b079 | 86:a26f5f22631d | 550 | char response[5]; |
ee12b079 | 86:a26f5f22631d | 551 | response[0] = spi.write(0xFF); |
ee12b079 | 86:a26f5f22631d | 552 | if (!(response[0] & 0x80)) { |
ee12b079 | 86:a26f5f22631d | 553 | for (int j = 1; j < 5; j++) { |
ee12b079 | 86:a26f5f22631d | 554 | response[i] = spi.write(0xFF); |
ee12b079 | 86:a26f5f22631d | 555 | } |
ee12b079 | 86:a26f5f22631d | 556 | cs_sd = 1; |
ee12b079 | 86:a26f5f22631d | 557 | spi.write(0xFF); |
ee12b079 | 86:a26f5f22631d | 558 | return response[0]; |
ee12b079 | 86:a26f5f22631d | 559 | } |
ee12b079 | 86:a26f5f22631d | 560 | } |
ee12b079 | 86:a26f5f22631d | 561 | cs_sd = 1; |
ee12b079 | 86:a26f5f22631d | 562 | spi.write(0xFF); |
ee12b079 | 86:a26f5f22631d | 563 | return -1; // timeout |
ee12b079 | 86:a26f5f22631d | 564 | } |
ee12b079 | 86:a26f5f22631d | 565 | |
ee12b079 | 86:a26f5f22631d | 566 | uint64_t sd_sectors() |
ee12b079 | 86:a26f5f22631d | 567 | { |
ee12b079 | 86:a26f5f22631d | 568 | uint32_t c_size, c_size_mult, read_bl_len; |
ee12b079 | 86:a26f5f22631d | 569 | uint32_t block_len, mult, blocknr, capacity; |
ee12b079 | 86:a26f5f22631d | 570 | uint32_t hc_c_size; |
ee12b079 | 86:a26f5f22631d | 571 | uint64_t blocks; |
ee12b079 | 86:a26f5f22631d | 572 | |
ee12b079 | 86:a26f5f22631d | 573 | // CMD9, Response R2 (R1 byte + 16-byte block read) |
ee12b079 | 86:a26f5f22631d | 574 | if (cmdx(9, 0) != 0) { |
ee12b079 | 86:a26f5f22631d | 575 | debug("\rDidn't get a response from the disk\n"); |
ee12b079 | 86:a26f5f22631d | 576 | return 0; |
ee12b079 | 86:a26f5f22631d | 577 | } |
ee12b079 | 86:a26f5f22631d | 578 | |
ee12b079 | 86:a26f5f22631d | 579 | uint8_t cs_sdd[16]; |
ee12b079 | 86:a26f5f22631d | 580 | if (read(cs_sdd, 16) != 0) { |
ee12b079 | 86:a26f5f22631d | 581 | debug("\rCouldn't read cs_sdd response from disk\n"); |
ee12b079 | 86:a26f5f22631d | 582 | return 0; |
ee12b079 | 86:a26f5f22631d | 583 | } |
ee12b079 | 86:a26f5f22631d | 584 | |
ee12b079 | 86:a26f5f22631d | 585 | // cs_sdd_structure : cs_sdd[127:126] |
ee12b079 | 86:a26f5f22631d | 586 | // c_size : cs_sdd[73:62] |
ee12b079 | 86:a26f5f22631d | 587 | // c_size_mult : cs_sdd[49:47] |
ee12b079 | 86:a26f5f22631d | 588 | // read_bl_len : cs_sdd[83:80] - the *maximum* read block length |
ee12b079 | 86:a26f5f22631d | 589 | |
ee12b079 | 86:a26f5f22631d | 590 | int cs_sdd_structure = ext_bits(cs_sdd, 127, 126); |
ee12b079 | 86:a26f5f22631d | 591 | |
ee12b079 | 86:a26f5f22631d | 592 | switch (cs_sdd_structure) { |
ee12b079 | 86:a26f5f22631d | 593 | case 0: |
ee12b079 | 86:a26f5f22631d | 594 | cdv = 512; |
ee12b079 | 86:a26f5f22631d | 595 | c_size = ext_bits(cs_sdd, 73, 62); |
ee12b079 | 86:a26f5f22631d | 596 | c_size_mult = ext_bits(cs_sdd, 49, 47); |
ee12b079 | 86:a26f5f22631d | 597 | read_bl_len = ext_bits(cs_sdd, 83, 80); |
ee12b079 | 86:a26f5f22631d | 598 | |
ee12b079 | 86:a26f5f22631d | 599 | block_len = 1 << read_bl_len; |
ee12b079 | 86:a26f5f22631d | 600 | mult = 1 << (c_size_mult + 2); |
ee12b079 | 86:a26f5f22631d | 601 | blocknr = (c_size + 1) * mult; |
ee12b079 | 86:a26f5f22631d | 602 | capacity = blocknr * block_len; |
ee12b079 | 86:a26f5f22631d | 603 | blocks = capacity / 512; |
ee12b079 | 86:a26f5f22631d | 604 | 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 | 605 | break; |
ee12b079 | 86:a26f5f22631d | 606 | |
ee12b079 | 86:a26f5f22631d | 607 | case 1: |
ee12b079 | 86:a26f5f22631d | 608 | cdv = 1; |
ee12b079 | 86:a26f5f22631d | 609 | hc_c_size = ext_bits(cs_sdd, 63, 48); |
ee12b079 | 86:a26f5f22631d | 610 | blocks = (hc_c_size+1)*1024; |
ee12b079 | 86:a26f5f22631d | 611 | 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 | 612 | break; |
ee12b079 | 86:a26f5f22631d | 613 | |
ee12b079 | 86:a26f5f22631d | 614 | default: |
ee12b079 | 86:a26f5f22631d | 615 | debug("cs_sdD struct unsupported\r\n"); |
ee12b079 | 86:a26f5f22631d | 616 | return 0; |
ee12b079 | 86:a26f5f22631d | 617 | }; |
ee12b079 | 86:a26f5f22631d | 618 | return blocks; |
ee12b079 | 86:a26f5f22631d | 619 | } |
ee12b079 | 86:a26f5f22631d | 620 | |
ee12b079 | 86:a26f5f22631d | 621 | int cmdx(int cmd, int arg) |
ee12b079 | 86:a26f5f22631d | 622 | { |
ee12b079 | 86:a26f5f22631d | 623 | cs_sd = 0; |
ee12b079 | 86:a26f5f22631d | 624 | |
ee12b079 | 86:a26f5f22631d | 625 | // send a command |
ee12b079 | 86:a26f5f22631d | 626 | spi.write(0x40 | cmd); |
ee12b079 | 86:a26f5f22631d | 627 | spi.write(arg >> 24); |
ee12b079 | 86:a26f5f22631d | 628 | spi.write(arg >> 16); |
ee12b079 | 86:a26f5f22631d | 629 | spi.write(arg >> 8); |
ee12b079 | 86:a26f5f22631d | 630 | spi.write(arg >> 0); |
ee12b079 | 86:a26f5f22631d | 631 | spi.write(0x95); |
ee12b079 | 86:a26f5f22631d | 632 | |
ee12b079 | 86:a26f5f22631d | 633 | // wait for the repsonse (response[7] == 0) |
ee12b079 | 86:a26f5f22631d | 634 | for (int i = 0; i < SD_COMMAND_TIMEOUT; i++) { |
ee12b079 | 86:a26f5f22631d | 635 | int response = spi.write(0xFF); |
ee12b079 | 86:a26f5f22631d | 636 | if (!(response & 0x80)) { |
ee12b079 | 86:a26f5f22631d | 637 | return response; |
ee12b079 | 86:a26f5f22631d | 638 | } |
ee12b079 | 86:a26f5f22631d | 639 | } |
ee12b079 | 86:a26f5f22631d | 640 | cs_sd = 1; |
ee12b079 | 86:a26f5f22631d | 641 | spi.write(0xFF); |
ee12b079 | 194:ab528911780d | 642 | return 1; // timeout |
ee12b079 | 86:a26f5f22631d | 643 | } |
ee12b079 | 86:a26f5f22631d | 644 | |
ee12b079 | 86:a26f5f22631d | 645 | static uint32_t ext_bits(unsigned char *data, int msb, int lsb) |
ee12b079 | 86:a26f5f22631d | 646 | { |
ee12b079 | 86:a26f5f22631d | 647 | uint32_t bits = 0; |
ee12b079 | 86:a26f5f22631d | 648 | uint32_t size = 1 + msb - lsb; |
ee12b079 | 86:a26f5f22631d | 649 | for (int i = 0; i < size; i++) { |
ee12b079 | 86:a26f5f22631d | 650 | uint32_t position = lsb + i; |
ee12b079 | 86:a26f5f22631d | 651 | uint32_t byte = 15 - (position >> 3); |
ee12b079 | 86:a26f5f22631d | 652 | uint32_t bit = position & 0x7; |
ee12b079 | 86:a26f5f22631d | 653 | uint32_t value = (data[byte] >> bit) & 1; |
ee12b079 | 86:a26f5f22631d | 654 | bits |= value << i; |
ee12b079 | 86:a26f5f22631d | 655 | } |
ee12b079 | 86:a26f5f22631d | 656 | return bits; |
ee12b079 | 86:a26f5f22631d | 657 | } |
ee12b079 | 86:a26f5f22631d | 658 | |
ee12b079 | 86:a26f5f22631d | 659 | int disk_write(const uint8_t *buffer, uint64_t block_number) |
ee12b079 | 86:a26f5f22631d | 660 | |
ee12b079 | 86:a26f5f22631d | 661 | { |
ee12b079 | 86:a26f5f22631d | 662 | // set write address for single block (CMD24) |
ee12b079 | 86:a26f5f22631d | 663 | if (cmd(24, block_number * cdv) != 0) { |
chaithanyarss | 303:b49b486a7107 | 664 | CDMS_WR_SD_FAULT_COUNTER++; |
ee12b079 | 86:a26f5f22631d | 665 | return 1; |
ee12b079 | 86:a26f5f22631d | 666 | } |
samp1234 | 346:204497974293 | 667 | |
ee12b079 | 209:63e9c8f8b5d2 | 668 | uint64_t temp; |
ee12b079 | 209:63e9c8f8b5d2 | 669 | int r = write(buffer, 512); |
samp1234 | 346:204497974293 | 670 | if(r == 0 ){ |
ee12b079 | 209:63e9c8f8b5d2 | 671 | temp = FCTN_CDMS_RD_RTC(); |
samp1234 | 356:197c93dc2012 | 672 | TIME_LATEST_SD_WR = temp >> 7; |
ee12b079 | 209:63e9c8f8b5d2 | 673 | } |
ee12b079 | 209:63e9c8f8b5d2 | 674 | return r; |
ee12b079 | 86:a26f5f22631d | 675 | } |
ee12b079 | 86:a26f5f22631d | 676 | |
ee12b079 | 86:a26f5f22631d | 677 | int write(const uint8_t*buffer, uint32_t length) |
ee12b079 | 86:a26f5f22631d | 678 | { |
ee12b079 | 86:a26f5f22631d | 679 | cs_sd = 0; |
ee12b079 | 86:a26f5f22631d | 680 | |
ee12b079 | 86:a26f5f22631d | 681 | // indicate start of block |
ee12b079 | 86:a26f5f22631d | 682 | spi.write(0xFE); |
ee12b079 | 86:a26f5f22631d | 683 | |
ee12b079 | 86:a26f5f22631d | 684 | // write the data |
ee12b079 | 86:a26f5f22631d | 685 | for (int i = 0; i < length; i++) { |
ee12b079 | 86:a26f5f22631d | 686 | spi.write(buffer[i]); |
ee12b079 | 86:a26f5f22631d | 687 | } |
ee12b079 | 86:a26f5f22631d | 688 | |
ee12b079 | 86:a26f5f22631d | 689 | // write the checksum |
ee12b079 | 86:a26f5f22631d | 690 | spi.write(0xFF); |
ee12b079 | 86:a26f5f22631d | 691 | spi.write(0xFF); |
ee12b079 | 86:a26f5f22631d | 692 | |
ee12b079 | 86:a26f5f22631d | 693 | // check the response token |
ee12b079 | 86:a26f5f22631d | 694 | if ((spi.write(0xFF) & 0x1F) != 0x05) { |
ee12b079 | 86:a26f5f22631d | 695 | cs_sd = 1; |
ee12b079 | 86:a26f5f22631d | 696 | spi.write(0xFF); |
chaithanyarss | 303:b49b486a7107 | 697 | CDMS_WR_SD_FAULT_COUNTER++; |
ee12b079 | 86:a26f5f22631d | 698 | return 1; |
ee12b079 | 86:a26f5f22631d | 699 | } |
ee12b079 | 86:a26f5f22631d | 700 | |
ee12b079 | 86:a26f5f22631d | 701 | // wait for write to finish |
ee12b079 | 86:a26f5f22631d | 702 | while (spi.write(0xFF) == 0); |
ee12b079 | 86:a26f5f22631d | 703 | |
ee12b079 | 86:a26f5f22631d | 704 | cs_sd = 1; |
ee12b079 | 86:a26f5f22631d | 705 | spi.write(0xFF); |
ee12b079 | 86:a26f5f22631d | 706 | return 0; |
ee12b079 | 86:a26f5f22631d | 707 | } |
ee12b079 | 86:a26f5f22631d | 708 | |
ee12b079 | 86:a26f5f22631d | 709 | int disk_read(uint8_t *buffer, uint64_t block_number) |
ee12b079 | 86:a26f5f22631d | 710 | { |
ee12b079 | 86:a26f5f22631d | 711 | // set read address for single block (CMD17) |
ee12b079 | 86:a26f5f22631d | 712 | if (cmd(17, block_number * cdv) != 0) { |
ee12b079 | 209:63e9c8f8b5d2 | 713 | SD_RD_ERROR = 1; |
ee12b079 | 86:a26f5f22631d | 714 | return 1; |
ee12b079 | 86:a26f5f22631d | 715 | } |
ee12b079 | 86:a26f5f22631d | 716 | |
ee12b079 | 86:a26f5f22631d | 717 | // receive the data |
ee12b079 | 86:a26f5f22631d | 718 | read(buffer, 512); |
ee12b079 | 209:63e9c8f8b5d2 | 719 | uint64_t temp = FCTN_CDMS_RD_RTC(); |
ee12b079 | 209:63e9c8f8b5d2 | 720 | TIME_LATEST_SD_RD = temp >> 7; |
ee12b079 | 86:a26f5f22631d | 721 | return 0; |
ee12b079 | 86:a26f5f22631d | 722 | } |
ee12b079 | 86:a26f5f22631d | 723 | |
ee12b079 | 86:a26f5f22631d | 724 | int read(uint8_t *buffer, uint32_t length) |
ee12b079 | 86:a26f5f22631d | 725 | { |
ee12b079 | 86:a26f5f22631d | 726 | cs_sd = 0; |
ee12b079 | 86:a26f5f22631d | 727 | |
ee12b079 | 86:a26f5f22631d | 728 | // read until start byte (0xFF) |
ee12b079 | 86:a26f5f22631d | 729 | while (spi.write(0xFF) != 0xFE); |
ee12b079 | 86:a26f5f22631d | 730 | |
ee12b079 | 86:a26f5f22631d | 731 | // read data |
ee12b079 | 86:a26f5f22631d | 732 | for (int i = 0; i < length; i++) { |
ee12b079 | 86:a26f5f22631d | 733 | buffer[i] = spi.write(0xFF); |
ee12b079 | 86:a26f5f22631d | 734 | } |
ee12b079 | 86:a26f5f22631d | 735 | spi.write(0xFF); // checksum |
ee12b079 | 86:a26f5f22631d | 736 | spi.write(0xFF); |
ee12b079 | 86:a26f5f22631d | 737 | |
ee12b079 | 86:a26f5f22631d | 738 | cs_sd = 1; |
ee12b079 | 86:a26f5f22631d | 739 | spi.write(0xFF); |
ee12b079 | 86:a26f5f22631d | 740 | return 0; |
ee12b079 | 86:a26f5f22631d | 741 | } |
ee12b079 | 86:a26f5f22631d | 742 | |
ee12b079 | 86:a26f5f22631d | 743 | int disk_erase(int startBlock, int totalBlocks) |
ee12b079 | 86:a26f5f22631d | 744 | { |
ee12b079 | 86:a26f5f22631d | 745 | if(cmd(32, startBlock * cdv) != 0) { |
ee12b079 | 86:a26f5f22631d | 746 | return 1; |
ee12b079 | 86:a26f5f22631d | 747 | } |
ee12b079 | 86:a26f5f22631d | 748 | if (cmd(33, (startBlock+totalBlocks-1) * cdv) != 0) { |
ee12b079 | 86:a26f5f22631d | 749 | return 1; |
ee12b079 | 86:a26f5f22631d | 750 | } |
ee12b079 | 86:a26f5f22631d | 751 | if (cmd(38,0) != 0) { |
ee12b079 | 86:a26f5f22631d | 752 | return 1; |
ee12b079 | 86:a26f5f22631d | 753 | } |
samp1234 | 346:204497974293 | 754 | |
ee12b079 | 86:a26f5f22631d | 755 | return 0; //normal return |
ee12b079 | 209:63e9c8f8b5d2 | 756 | } |
ee12b079 | 209:63e9c8f8b5d2 | 757 | |
ee12b079 | 209:63e9c8f8b5d2 | 758 | int disk_read_statusbits(uint8_t *buffer) |
ee12b079 | 209:63e9c8f8b5d2 | 759 | { |
ee12b079 | 209:63e9c8f8b5d2 | 760 | if (cmd(17, 0) != 0) { |
samp1234 | 346:204497974293 | 761 | SD_RD_ERROR = 1; |
ee12b079 | 209:63e9c8f8b5d2 | 762 | return -1; |
ee12b079 | 209:63e9c8f8b5d2 | 763 | } |
ee12b079 | 209:63e9c8f8b5d2 | 764 | |
ee12b079 | 209:63e9c8f8b5d2 | 765 | // receive the data |
ee12b079 | 209:63e9c8f8b5d2 | 766 | return read(buffer,64); |
samp1234 | 346:204497974293 | 767 | } |