Rev 516 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1 | ingob | 1 | /*#######################################################################################*/ |
2 | /* !!! THIS IS NOT FREE SOFTWARE !!! */ |
||
3 | /*#######################################################################################*/ |
||
4 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
5 | // + www.MikroKopter.com |
||
6 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
360 | holgerb | 7 | // + Software Nutzungsbedingungen (english version: see below) |
8 | // + der Fa. HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland - nachfolgend Lizenzgeber genannt - |
||
9 | // + Der Lizenzgeber räumt dem Kunden ein nicht-ausschließliches, zeitlich und räumlich* unbeschränktes Recht ein, die im den |
||
10 | // + Mikrocontroller verwendete Firmware für die Hardware Flight-Ctrl, Navi-Ctrl, BL-Ctrl, MK3Mag & PC-Programm MikroKopter-Tool |
||
11 | // + - nachfolgend Software genannt - nur für private Zwecke zu nutzen. |
||
12 | // + Der Einsatz dieser Software ist nur auf oder mit Produkten des Lizenzgebers zulässig. |
||
1 | ingob | 13 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
360 | holgerb | 14 | // + Die vom Lizenzgeber gelieferte Software ist urheberrechtlich geschützt. Alle Rechte an der Software sowie an sonstigen im |
15 | // + Rahmen der Vertragsanbahnung und Vertragsdurchführung überlassenen Unterlagen stehen im Verhältnis der Vertragspartner ausschließlich dem Lizenzgeber zu. |
||
16 | // + Die in der Software enthaltenen Copyright-Vermerke, Markenzeichen, andere Rechtsvorbehalte, Seriennummern sowie |
||
17 | // + sonstige der Programmidentifikation dienenden Merkmale dürfen vom Kunden nicht verändert oder unkenntlich gemacht werden. |
||
18 | // + Der Kunde trifft angemessene Vorkehrungen für den sicheren Einsatz der Software. Er wird die Software gründlich auf deren |
||
19 | // + Verwendbarkeit zu dem von ihm beabsichtigten Zweck testen, bevor er diese operativ einsetzt. |
||
20 | // + Die Haftung des Lizenzgebers wird - soweit gesetzlich zulässig - begrenzt in Höhe des typischen und vorhersehbaren |
||
21 | // + Schadens. Die gesetzliche Haftung bei Personenschäden und nach dem Produkthaftungsgesetz bleibt unberührt. Dem Lizenzgeber steht jedoch der Einwand |
||
22 | // + des Mitverschuldens offen. |
||
23 | // + Der Kunde trifft angemessene Vorkehrungen für den Fall, dass die Software ganz oder teilweise nicht ordnungsgemäß arbeitet. |
||
24 | // + Er wird die Software gründlich auf deren Verwendbarkeit zu dem von ihm beabsichtigten Zweck testen, bevor er diese operativ einsetzt. |
||
25 | // + Der Kunde wird er seine Daten vor Einsatz der Software nach dem Stand der Technik sichern. |
||
26 | // + Der Kunde ist darüber unterrichtet, dass der Lizenzgeber seine Daten im zur Vertragsdurchführung erforderlichen Umfang |
||
27 | // + und auf Grundlage der Datenschutzvorschriften erhebt, speichert, verarbeitet und, sofern notwendig, an Dritte übermittelt. |
||
28 | // + *) Die räumliche Nutzung bezieht sich nur auf den Einsatzort, nicht auf die Reichweite der programmierten Software. |
||
29 | // + #### ENDE DER NUTZUNGSBEDINGUNGEN ####' |
||
30 | // + Hinweis: Informationen über erweiterte Nutzungsrechte (wie z.B. Nutzung für nicht-private Zwecke) sind auf Anfrage per Email an info(@)hisystems.de verfügbar. |
||
1 | ingob | 31 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
360 | holgerb | 32 | // + Software LICENSING TERMS |
1 | ingob | 33 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
360 | holgerb | 34 | // + of HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland, Germany - the Licensor - |
35 | // + The Licensor grants the customer a non-exclusive license to use the microcontroller firmware of the Flight-Ctrl, Navi-Ctrl, BL-Ctrl, and MK3Mag hardware |
||
36 | // + (the Software) exclusively for private purposes. The License is unrestricted with respect to time and territory*. |
||
37 | // + The Software may only be used with the Licensor's products. |
||
38 | // + The Software provided by the Licensor is protected by copyright. With respect to the relationship between the parties to this |
||
39 | // + agreement, all rights pertaining to the Software and other documents provided during the preparation and execution of this |
||
40 | // + agreement shall be the property of the Licensor. |
||
41 | // + The information contained in the Software copyright notices, trademarks, other legal reservations, serial numbers and other |
||
42 | // + features that can be used to identify the program may not be altered or defaced by the customer. |
||
43 | // + The customer shall be responsible for taking reasonable precautions |
||
44 | // + for the safe use of the Software. The customer shall test the Software thoroughly regarding its suitability for the |
||
45 | // + intended purpose before implementing it for actual operation. The Licensor's liability shall be limited to the extent of typical and |
||
46 | // + foreseeable damage to the extent permitted by law, notwithstanding statutory liability for bodily injury and product |
||
47 | // + liability. However, the Licensor shall be entitled to the defense of contributory negligence. |
||
48 | // + The customer will take adequate precautions in the case, that the software is not working properly. The customer will test |
||
49 | // + the software for his purpose before any operational usage. The customer will backup his data before using the software. |
||
50 | // + The customer understands that the Licensor collects, stores and processes, and, where required, forwards, customer data |
||
51 | // + to third parties to the extent necessary for executing the agreement, subject to applicable data protection and privacy regulations. |
||
52 | // + *) The territory aspect only refers to the place where the Software is used, not its programmed range. |
||
53 | // + #### END OF LICENSING TERMS #### |
||
54 | // + Note: For information on license extensions (e.g. commercial use), please contact us at info(@)hisystems.de. |
||
1 | ingob | 55 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
24 | StephanB | 56 | #include <stdio.h> |
57 | #include <string.h> |
||
58 | #include "91x_lib.h" |
||
41 | ingob | 59 | #include "uart1.h" |
24 | StephanB | 60 | #include "sdc.h" |
61 | #include "ssc.h" |
||
119 | killagreg | 62 | #include "timer1.h" |
1 | ingob | 63 | #include "main.h" |
24 | StephanB | 64 | #include "crc16.h" |
1 | ingob | 65 | |
384 | ingob | 66 | |
67 | |||
1 | ingob | 68 | //________________________________________________________________________________________________________________________________________ |
24 | StephanB | 69 | // Module name: sdc.c |
1 | ingob | 70 | // Compiler used: avr-gcc 3.4.5 |
24 | StephanB | 71 | // Last Modifikation: 08.06.2008 |
72 | // Version: 1.07 |
||
73 | // Authors: Stephan Busker, Gregor Stobrawa |
||
74 | // Description: Source files for connecting to an sd-card using the SSC |
||
1 | ingob | 75 | // |
76 | //........................................................................................................................................ |
||
24 | StephanB | 77 | // Functions: SD_Result_t SDC_init(void); |
78 | // u8 SDC_PutCommand (u8 *cmd); |
||
79 | // SD_Result_t SDC_PutSector(u32 addr,u8 *Buffer); |
||
80 | // SD_Result_t SDC_GetSector(u32 addr,u8 *Buffer); |
||
1 | ingob | 81 | // |
82 | ////........................................................................................................................................ |
||
83 | // ext. functions: extern void SSC_Init(void); |
||
24 | StephanB | 84 | // extern u8 SSC_GetChar (void); |
1 | ingob | 85 | // extern void SSC_PutChar (u8); |
86 | // extern void SSC_Enable(void); |
||
87 | // extern void SSC_Disable(void); |
||
24 | StephanB | 88 | // extern void SSC_ClearRxFifo(); |
1 | ingob | 89 | //........................................................................................................................................ |
90 | // |
||
91 | // URL: www.Mikro-Control.de |
||
92 | // mailto: stephan.busker@mikro-control.de |
||
93 | //________________________________________________________________________________________________________________________________________ |
||
94 | |||
95 | |||
96 | |||
24 | StephanB | 97 | #define CMD_GO_IDLE_STATE 0x00 /* CMD00: response R1 */ |
98 | #define CMD_SEND_OP_COND 0x01 /* CMD01: response R1 */ |
||
99 | #define CMD_SEND_IF_COND 0x08 /* CMD08: response R7 */ |
||
100 | #define CMD_SEND_CSD 0x09 /* CMD09: response R1 */ |
||
101 | #define CMD_SEND_CID 0x0A /* CMD10: response R1 */ |
||
102 | #define CMD_SEND_STATUS 0x0D /* CMD13: response R2 */ |
||
103 | #define CMD_SET_BLOCKLEN 0x10 /* CMD16: arg0[31:0]: block length, response R1*/ |
||
104 | #define CMD_READ_SINGLE_BLOCK 0x11 /* CMD17: arg0[31:0]: data address, response R1 */ |
||
105 | #define CMD_WRITE_SINGLE_BLOCK 0x18 /* CMD24: arg0[31:0]: data address, response R1 */ |
||
106 | #define CMD_APP_CMD 0x37 /* CMD55: response R1 */ |
||
107 | #define CMD_READ_OCR 0x3A /* CMD58: response R3 */ |
||
108 | #define CMD_CRC_ON_OFF 0x3B /* CMD59: arg0[31:1]: stuff bits, arg0[0:0]: crc option, response R1 */ |
||
109 | #define ACMD_SEND_OP_COND 0x29 /* ACMD41: arg0[31]: stuff bits, arg0[30]: HCS, arg0[29:0] stuff bits*, response R1 */ |
||
1 | ingob | 110 | |
24 | StephanB | 111 | #define R1_NO_ERR 0x00 |
112 | #define R1_IDLE_STATE 0x01 |
||
113 | #define R1_ERASE_RESET 0x02 |
||
114 | #define R1_ILLEGAL_CMD 0x04 |
||
115 | #define R1_COM_CRC_ERR 0x08 |
||
116 | #define R1_ERASE_SEQUENCE_ERR 0x10 |
||
117 | #define R1_ADDRESS_ERR 0x20 |
||
118 | #define R1_PARAMETER_ERR 0x40 |
||
119 | #define R1_BAD_RESPONSE 0x80 |
||
120 | |||
121 | #define R2_NO_ERR 0x00 |
||
122 | #define R2_CARD_LOCKED 0x01 |
||
123 | #define R2_ERASE_WRITE_PROT_ERR 0x02 |
||
124 | #define R2_UNKOWN_ERR 0x04 |
||
125 | #define R2_CARD_CTRL_ERR 0x08 |
||
126 | #define R2_CARD_ECC_ERR 0x10 |
||
127 | #define R2_WRITE_PROT_ERR 0x20 |
||
128 | #define R2_ERASE_PARAM_ERR 0x40 |
||
129 | #define R2_OUT_OF_RANGE_ERR 0x80 |
||
130 | |||
131 | #define DATA_START_TOKEN 0xFE |
||
132 | #define DATA_RESPONSE_MASK 0x1F |
||
133 | #define DATA_RESPONSE_OK 0x05 |
||
134 | #define DATA_RESPONSE_CRC_ERR 0x0B |
||
135 | #define DATA_RESPONSE_WRITE_ERR 0x1D |
||
136 | |||
137 | volatile SDCardInfo_t SDCardInfo; |
||
384 | ingob | 138 | u8 SDCardWriteRetryCounterMax; |
24 | StephanB | 139 | |
140 | |||
1 | ingob | 141 | //________________________________________________________________________________________________________________________________________ |
24 | StephanB | 142 | // Function: CRC7(u8* cmd, u32 len); |
1 | ingob | 143 | // |
24 | StephanB | 144 | // Description: This function calculated the CRC7 checksum used in the last byte of a spi command frame. |
1 | ingob | 145 | // |
146 | // |
||
24 | StephanB | 147 | // Returnvalue: the function returns the crc7 including bit 0 set to 1 |
1 | ingob | 148 | //________________________________________________________________________________________________________________________________________ |
149 | |||
24 | StephanB | 150 | u8 CRC7(u8 * cmd, u32 len) |
1 | ingob | 151 | { |
24 | StephanB | 152 | u8 i, a; |
153 | u8 crc, Data; |
||
1 | ingob | 154 | |
24 | StephanB | 155 | crc = 0; // init CRC buffer |
156 | for (a = 0; a < len ;a++) // for every byte in the msg |
||
157 | { |
||
158 | Data = cmd[a]; |
||
159 | for (i=0;i<8;i++) // for every bit in the byte |
||
160 | { |
||
161 | crc <<= 1; // shift crc |
||
162 | if ((Data & 0x80)^(crc & 0x80)) crc ^=0x09; //xor |
||
163 | Data <<= 1; // shift data for next bit |
||
164 | } |
||
165 | } |
||
166 | crc = (crc<<1)|1; // set terminating bit to 1 |
||
167 | return(crc); |
||
168 | } |
||
1 | ingob | 169 | |
41 | ingob | 170 | u8 SDC_WaitForBusy(u32 timeout) |
24 | StephanB | 171 | { |
172 | u8 rsp = 0; |
||
41 | ingob | 173 | u32 timestamp = 0; |
1 | ingob | 174 | |
24 | StephanB | 175 | SSC_ClearRxFifo(); |
384 | ingob | 176 | SSC_Enable(); // enable chipselect. SSC_Disable |
41 | ingob | 177 | timestamp = SetDelay(timeout); |
24 | StephanB | 178 | do |
1 | ingob | 179 | { |
24 | StephanB | 180 | rsp = SSC_GetChar(); |
378 | holgerb | 181 | if(CheckDelay(timestamp)) break; |
380 | holgerb | 182 | }while(rsp != 0xFF); // wait while card is busy (data out low) |
24 | StephanB | 183 | return(rsp); |
184 | } |
||
185 | |||
186 | |||
187 | |||
188 | //________________________________________________________________________________________________________________________________________ |
||
189 | // Function: SDC_SendCMDR1(u8 CmdNo, u32 arg); |
||
190 | // |
||
191 | // Description: This function send a command frame to the SD-Card in spi-mode. |
||
192 | // |
||
193 | // |
||
194 | // Returnvalue: The function returns the first response byte like for R1 commands |
||
195 | //________________________________________________________________________________________________________________________________________ |
||
196 | u8 SDC_SendCMDR1(u8 CmdNo, u32 arg) |
||
197 | { |
||
198 | u8 r1; |
||
41 | ingob | 199 | u16 timeout = 0; |
24 | StephanB | 200 | u16 a; |
201 | u8 cmd[6]; |
||
202 | |||
203 | SSC_ClearRxFifo(); // clear the rx fifo |
||
204 | SSC_Enable(); // enable chipselect. |
||
378 | holgerb | 205 | SDC_WaitForBusy(600); // wait 500ms until card is busy |
24 | StephanB | 206 | SSC_ClearRxFifo(); // clear the rx fifo |
207 | SSC_GetChar(); // dummy to sync |
||
208 | |||
209 | cmd[0] = 0x40|CmdNo; // set command index |
||
210 | cmd[1] = (arg & 0xFF000000)>>24; |
||
211 | cmd[2] = (arg & 0x00FF0000)>>16; |
||
212 | cmd[3] = (arg & 0x0000FF00)>>8; |
||
213 | cmd[4] = (arg & 0x000000FF); |
||
214 | cmd[5] = CRC7(cmd, 5); // update checksum |
||
215 | for (a = 0;a < 6; a++) // send the command sequence to the sdcard (6 bytes) |
||
216 | { |
||
217 | SSC_PutChar(cmd[a]); |
||
1 | ingob | 218 | } |
24 | StephanB | 219 | SSC_ClearRxFifo(); // clear the rx fifo to discard the bytes received during the transmission of the 6 command bytes |
41 | ingob | 220 | |
24 | StephanB | 221 | do |
1 | ingob | 222 | { |
24 | StephanB | 223 | r1 = SSC_GetChar(); // get byte from sd-card |
41 | ingob | 224 | if (timeout++ >500) break; |
380 | holgerb | 225 | }while(r1 == 0xFF); // wait for the response byte from sd-card. |
24 | StephanB | 226 | return(r1); |
1 | ingob | 227 | } |
228 | |||
229 | |||
24 | StephanB | 230 | //________________________________________________________________________________________________________________________________________ |
231 | // Function: SDC_SendACMDR1(u8 CmdNo, u32 arg); |
||
41 | ingob | 232 | // |
24 | StephanB | 233 | // Description: This function send a application command frame to the SD-Card in spi-mode. |
234 | // |
||
235 | // |
||
236 | // Returnvalue: The function returns the first response byte like for R1 commands |
||
237 | //________________________________________________________________________________________________________________________________________ |
||
238 | u8 SDC_SendACMDR1(u8 CmdNo, u32 arg) |
||
239 | { |
||
240 | u8 r1 = 0xFF; |
||
241 | r1 = SDC_SendCMDR1(CMD_APP_CMD, 0UL); |
||
242 | if(r1 & R1_BAD_RESPONSE) return(r1); |
||
243 | r1 = SDC_SendCMDR1(CmdNo, arg); |
||
244 | return(r1); |
||
245 | } |
||
1 | ingob | 246 | |
247 | |||
248 | //________________________________________________________________________________________________________________________________________ |
||
24 | StephanB | 249 | // Function: SDC_GetData(u8 * cmd ,u8 *Buffer, u32 len); |
1 | ingob | 250 | // |
24 | StephanB | 251 | // Description: This function sneds cmd an reads a datablock of len from the sd-card |
1 | ingob | 252 | // |
253 | // |
||
24 | StephanB | 254 | // Returnvalue: SD_Result_t |
1 | ingob | 255 | //________________________________________________________________________________________________________________________________________ |
256 | |||
24 | StephanB | 257 | SD_Result_t SDC_GetData(u8 CmdNo, u32 addr, u8 *Buffer, u32 len) |
1 | ingob | 258 | { |
24 | StephanB | 259 | u8 rsp; |
41 | ingob | 260 | u16 a, crc16; |
146 | killagreg | 261 | u32 timeout; |
24 | StephanB | 262 | SD_Result_t result = SD_ERROR_UNKNOWN; |
1 | ingob | 263 | |
24 | StephanB | 264 | // send the command |
265 | rsp = SDC_SendCMDR1(CmdNo, addr); |
||
266 | if (rsp != R1_NO_ERR) |
||
1 | ingob | 267 | { |
24 | StephanB | 268 | result = SD_ERROR_BAD_RESPONSE; |
269 | goto end; |
||
1 | ingob | 270 | } |
271 | SSC_ClearRxFifo(); |
||
146 | killagreg | 272 | timeout = SetDelay(500); |
24 | StephanB | 273 | do |
1 | ingob | 274 | { |
24 | StephanB | 275 | rsp = SSC_GetChar(); |
146 | killagreg | 276 | if( ( (rsp & 0xF0) == 0x00 ) || CheckDelay(timeout) ) // data error token or timeout |
1 | ingob | 277 | { |
24 | StephanB | 278 | result = SD_ERROR_READ_DATA; |
279 | goto end; |
||
1 | ingob | 280 | } |
380 | holgerb | 281 | }while(rsp != DATA_START_TOKEN); |
24 | StephanB | 282 | // data start token received |
283 | for (a = 0; a < len; a++) // read the block from the SSC |
||
284 | { |
||
285 | Buffer[a] = SSC_GetChar(); |
||
1 | ingob | 286 | } |
24 | StephanB | 287 | // Read two bytes CRC16-Data checksum |
146 | killagreg | 288 | crc16 = SSC_GetChar(); // highbyte first |
41 | ingob | 289 | crc16 = (crc16<<8)|SSC_GetChar(); // lowbyte last |
290 | /* if(crc16 != CRC16(Buffer, len)) result = SD_ERROR_CRC_DATA; |
||
364 | ingob | 291 | else */ |
292 | result = SD_SUCCESS; |
||
24 | StephanB | 293 | end: |
384 | ingob | 294 | SSC_Disable(); // disable CS |
24 | StephanB | 295 | if(result != SD_SUCCESS) |
296 | { |
||
297 | sprintf(text,"Error %02X reading data from sd card (R1=%02X).\r\n", result, rsp); |
||
110 | killagreg | 298 | UART1_PutString(text); |
24 | StephanB | 299 | } |
300 | return(result); |
||
301 | } |
||
1 | ingob | 302 | |
24 | StephanB | 303 | |
304 | //________________________________________________________________________________________________________________________________________ |
||
305 | // Function: SDC_PrintCID(u8 * pCID); |
||
306 | // |
||
307 | // Description: This function prints the CIS register in a human readable format. |
||
308 | // |
||
309 | // |
||
310 | // Returnvalue: the function returns nothing |
||
311 | //________________________________________________________________________________________________________________________________________ |
||
312 | |||
313 | void SDC_PrintCID(u8 * pCID) |
||
314 | { |
||
315 | u8 pn[6]; |
||
316 | u16 temp1, temp2; |
||
317 | |||
41 | ingob | 318 | sprintf(text, "\r\n Manufacturer ID: %i\r\n", pCID[0]); |
110 | killagreg | 319 | UART1_PutString(text); |
24 | StephanB | 320 | memcpy(pn, &pCID[1], 2); |
321 | pn[2] = '\0'; // terminate string |
||
41 | ingob | 322 | sprintf(text, " Application ID: %s\r\n",pn); |
110 | killagreg | 323 | UART1_PutString(text); |
24 | StephanB | 324 | memcpy(pn, &pCID[3], 5); |
325 | pn[5] = '\0'; // terminate string |
||
41 | ingob | 326 | sprintf(text, " Product Name: %s\r\n",pn); |
110 | killagreg | 327 | UART1_PutString(text); |
41 | ingob | 328 | sprintf(text, " Product Rev.: %i.%i\r\n",pCID[8]>>4, pCID[8]&0xF); |
110 | killagreg | 329 | UART1_PutString(text); |
330 | UART1_PutString(" Serial No.: "); |
||
24 | StephanB | 331 | for(temp1 = 0; temp1<4; temp1++) |
332 | { |
||
333 | sprintf(text,"%02X", pCID[9+temp1]); |
||
110 | killagreg | 334 | UART1_PutString(text); |
24 | StephanB | 335 | } |
110 | killagreg | 336 | UART1_PutString("\r\n"); |
24 | StephanB | 337 | temp1 = pCID[14] & 0x0F; // month |
338 | temp2 = ((pCID[14]>>4)|(pCID[13]<<4)) + 2000; // year |
||
41 | ingob | 339 | sprintf(text, " Manufac. Date: %i/%i\r\n\r\n",temp1, temp2); |
110 | killagreg | 340 | UART1_PutString(text); |
1 | ingob | 341 | } |
342 | |||
24 | StephanB | 343 | //________________________________________________________________________________________________________________________________________ |
344 | // Funtion: SDC_GetCID(u8 * pCID); |
||
345 | // |
||
346 | // Description: This function reads the CIS register form the sd card in spi mode. |
||
347 | // |
||
348 | // |
||
349 | // Returnvalue: the function returns error state |
||
350 | //________________________________________________________________________________________________________________________________________ |
||
1 | ingob | 351 | |
24 | StephanB | 352 | SD_Result_t SDC_GetCID(u8 * pCID) |
353 | { |
||
354 | return SDC_GetData(CMD_SEND_CID, 0UL, pCID, 16); |
||
355 | } |
||
1 | ingob | 356 | |
24 | StephanB | 357 | //________________________________________________________________________________________________________________________________________ |
358 | // Funtion: SDC_GetCSD(u8 * pCSD); |
||
359 | // |
||
360 | // Description: This function reads the CSD register form the sd card in spi mode. |
||
361 | // |
||
362 | // |
||
363 | // Returnvalue: the function returns error state |
||
364 | //________________________________________________________________________________________________________________________________________ |
||
1 | ingob | 365 | |
24 | StephanB | 366 | SD_Result_t SDC_GetCSD(u8 * pCSD) |
367 | { |
||
368 | return SDC_GetData(CMD_SEND_CSD, 0UL, pCSD, 16); |
||
369 | } |
||
370 | |||
1 | ingob | 371 | //________________________________________________________________________________________________________________________________________ |
24 | StephanB | 372 | // Funtion: SDC_Init(void); |
1 | ingob | 373 | // |
24 | StephanB | 374 | // Description: This function initialises the SDCard to spi-mode. |
1 | ingob | 375 | // |
376 | // |
||
24 | StephanB | 377 | // Returnvalue: the function returns 0 if the initialisation was successfull otherwise the function returns an errorcode. |
1 | ingob | 378 | //________________________________________________________________________________________________________________________________________ |
379 | |||
450 | holgerb | 380 | SD_Result_t SDC_Init(unsigned char print) |
1 | ingob | 381 | { |
41 | ingob | 382 | u32 timeout = 0; |
450 | holgerb | 383 | u16 speed; |
24 | StephanB | 384 | u8 rsp[6]; // SD-SPI response buffer |
385 | SD_Result_t result = SD_ERROR_UNKNOWN; |
||
450 | holgerb | 386 | if(SPI_Speed < 500) SPI_Speed = 500; // nur zur Sicherheit |
387 | speed = SPI_Speed; // weil das bei SSC_Init() auf 400 gesetzt werden würde |
||
384 | ingob | 388 | |
389 | //SDCardWriteRetryCounterMax = 0; |
||
24 | StephanB | 390 | |
391 | if(SD_SWITCH) // init only if the SD-Switch is indicating a card in the slot |
||
392 | { |
||
450 | holgerb | 393 | if(print) UART1_PutString("\r\n SSC init..."); |
24 | StephanB | 394 | SSC_Init(); |
450 | holgerb | 395 | if(print) UART1_PutString("ok"); |
41 | ingob | 396 | |
450 | holgerb | 397 | if(print) UART1_PutString("\r\n SDC init..."); |
24 | StephanB | 398 | SDCardInfo.Valid = 0; |
399 | /* The host shall supply power to the card so that the voltage is reached to Vdd_min within 250ms and |
||
400 | start to supply at least 74 SD clocks to the SD card with keeping cmd line to high. In case of SPI |
||
401 | mode, CS shall be held to high during 74 clock cycles. */ |
||
402 | SSC_Disable(); // set SD_CS high |
||
403 | SSC_ClearRxFifo(); // clear the rx fifo |
||
1 | ingob | 404 | |
41 | ingob | 405 | for (timeout = 0; timeout < 15; timeout++) // 15*8 = 120 cycles |
24 | StephanB | 406 | { |
407 | SSC_PutChar(0xFF); |
||
408 | } |
||
409 | |||
410 | // switch to idle state |
||
411 | while(SDC_SendCMDR1(CMD_GO_IDLE_STATE, 0UL) != R1_IDLE_STATE) |
||
412 | { |
||
41 | ingob | 413 | if (timeout++ > 20) |
24 | StephanB | 414 | { |
110 | killagreg | 415 | UART1_PutString("reset timeout"); |
24 | StephanB | 416 | result = SD_ERROR_RESET; |
417 | goto end; |
||
418 | } |
||
419 | } |
||
420 | // enable crc feature |
||
421 | /* if(SDC_SendCMDR1(CMD_CRC_ON_OFF, 1UL) != R1_IDLE_STATE) |
||
422 | { |
||
41 | ingob | 423 | sprintf(text,"Bad cmd59 R1=%02X.", rsp[0]); |
110 | killagreg | 424 | UART1_PutString(text); |
24 | StephanB | 425 | result = SD_ERROR_BAD_RESPONSE; |
426 | goto end; |
||
427 | }*/ |
||
428 | // check for card hw version |
||
429 | // 2.7-3.6V Range = 0x01, check pattern 0xAA |
||
430 | rsp[0] = SDC_SendCMDR1(CMD_SEND_IF_COND, 0x000001AA); |
||
431 | // answer to cmd58 is an R7 response (R1+ 4Byte IFCond) |
||
432 | if(rsp[0] & R1_BAD_RESPONSE) |
||
433 | { |
||
41 | ingob | 434 | sprintf(text,"Bad cmd8 R1=%02X.", rsp[0]); |
110 | killagreg | 435 | UART1_PutString(text); |
24 | StephanB | 436 | result = SD_ERROR_BAD_RESPONSE; |
437 | goto end; |
||
438 | } |
||
439 | if(rsp[0] & R1_ILLEGAL_CMD) |
||
440 | { |
||
441 | //Ver1.X SD Memory Card or not a SD Memory Card |
||
442 | SDCardInfo.Version = VER_1X; |
||
443 | } |
||
444 | else |
||
445 | { |
||
446 | // Ver2.00 or later SD Memory Card |
||
447 | // reading the remaining bytes of the R7 response |
||
448 | SDCardInfo.Version = VER_20; |
||
41 | ingob | 449 | for(timeout = 1; timeout < 5; timeout++) |
24 | StephanB | 450 | { |
41 | ingob | 451 | rsp[timeout] = SSC_GetChar(); |
24 | StephanB | 452 | } |
453 | //check pattern |
||
454 | if(rsp[4]!= 0xAA) |
||
455 | { |
||
110 | killagreg | 456 | UART1_PutString("Bad cmd8 R7 check pattern.\r\n"); |
24 | StephanB | 457 | result = SD_ERROR_BAD_RESPONSE; |
458 | goto end; |
||
459 | } |
||
460 | if ( (rsp[3] & 0x0F)!= 0x01 ) // voltage range is not 2.7-3.6V |
||
461 | { |
||
462 | |||
110 | killagreg | 463 | UART1_PutString("Card is incompatible to 3.3V.\r\n"); |
24 | StephanB | 464 | result = SD_ERROR_BAD_VOLTAGE_RANGE; |
465 | goto end; |
||
466 | } |
||
467 | } |
||
468 | |||
469 | rsp[0] = SDC_SendCMDR1(CMD_READ_OCR, 0UL); |
||
470 | // answer to cmd58 is an R3 response (R1 + 4Byte OCR) |
||
471 | if(rsp[0] & R1_BAD_RESPONSE) |
||
472 | { |
||
41 | ingob | 473 | sprintf(text,"Bad cmd58 R1 %02x.", rsp[0]); |
110 | killagreg | 474 | UART1_PutString(text); |
24 | StephanB | 475 | result = SD_ERROR_BAD_RESPONSE; |
476 | goto end; |
||
477 | } |
||
478 | if(rsp[0] & R1_ILLEGAL_CMD) |
||
479 | { |
||
110 | killagreg | 480 | UART1_PutString("Not an SD-CARD."); |
24 | StephanB | 481 | result = SD_ERROR_NO_SDCARD; |
482 | goto end; |
||
483 | } |
||
484 | // read 4 bytes of OCR register |
||
41 | ingob | 485 | for(timeout = 1; timeout < 5; timeout++) |
24 | StephanB | 486 | { |
41 | ingob | 487 | rsp[timeout] = SSC_GetChar(); |
24 | StephanB | 488 | } |
489 | // NavicCtrl uses 3.3 V, therefore check for bit 20 & 21 |
||
490 | if((rsp[2] & 0x30) != 0x30) |
||
491 | { |
||
492 | // supply voltage is not supported by sd-card |
||
110 | killagreg | 493 | UART1_PutString("Card is incompatible to 3.3V."); |
24 | StephanB | 494 | result = SD_ERROR_BAD_VOLTAGE_RANGE; |
495 | goto end; |
||
496 | } |
||
497 | |||
498 | // Initialize the sd-card sending continously ACMD_SEND_OP_COND (only supported by SD cards) |
||
41 | ingob | 499 | timeout = SetDelay(2000); // set timeout to 2000 ms (large cards tend to longer) |
24 | StephanB | 500 | do |
501 | { |
||
502 | rsp[0] = SDC_SendACMDR1(ACMD_SEND_OP_COND, 0UL); |
||
503 | if(rsp[0] & R1_BAD_RESPONSE) |
||
504 | { |
||
41 | ingob | 505 | sprintf(text,"Bad Acmd41 R1=%02X.", rsp[0]); |
110 | killagreg | 506 | UART1_PutString(text); |
24 | StephanB | 507 | result = SD_ERROR_BAD_RESPONSE; |
508 | goto end; |
||
509 | } |
||
41 | ingob | 510 | if(CheckDelay(timeout)) |
24 | StephanB | 511 | { |
110 | killagreg | 512 | UART1_PutString("Init timeout."); |
24 | StephanB | 513 | result = SD_ERROR_INITIALIZE; |
514 | goto end; |
||
515 | } |
||
516 | } while(rsp[0] & R1_IDLE_STATE); // loop until idle state |
||
517 | |||
518 | if(rsp[0] != R1_NO_ERR) |
||
519 | { |
||
110 | killagreg | 520 | UART1_PutString("Init error."); |
24 | StephanB | 521 | result = SD_ERROR_INITIALIZE; |
522 | goto end; |
||
523 | } |
||
524 | /* set block size to 512 bytes */ |
||
525 | if(SDC_SendCMDR1(CMD_SET_BLOCKLEN, 512UL) != R1_NO_ERR) |
||
526 | { |
||
110 | killagreg | 527 | UART1_PutString("Error setting block length to 512."); |
24 | StephanB | 528 | result = SD_ERROR_SET_BLOCKLEN; |
529 | goto end; |
||
530 | } |
||
41 | ingob | 531 | |
379 | holgerb | 532 | SSC_Disable(); // set SD_CS high |
533 | // here is the right place to inrease the SPI baud rate to maximum |
||
24 | StephanB | 534 | |
450 | holgerb | 535 | SSC_Speed(speed); |
536 | if(print) { sprintf(text,"\r\n Setting SPI speed to %d ", SPI_Speed);UART1_PutString(text); } |
||
537 | |||
399 | holgerb | 538 | SSC_Enable(); // set SD_CS high |
539 | |||
540 | // read CID register |
||
541 | result = SDC_GetCID((u8 *)&SDCardInfo.CID); |
||
542 | if(result != SD_SUCCESS) |
||
543 | { |
||
544 | |||
450 | holgerb | 545 | if(print) UART1_PutString(" failed - Set interface to low speed..."); |
399 | holgerb | 546 | SSC_Disable(); // set SD_CS high |
547 | // here is the right place to inrease the SPI baud rate to maximum |
||
450 | holgerb | 548 | SPI_Speed = 1000; |
549 | SSC_Speed(SPI_Speed); |
||
399 | holgerb | 550 | SSC_Enable(); // set SD_CS high |
551 | result = SDC_GetCID((u8 *)&SDCardInfo.CID); |
||
552 | } |
||
553 | |||
554 | if(result != SD_SUCCESS) |
||
555 | { |
||
556 | UART1_PutString("Error reading CID.\r\n"); |
||
557 | goto end; |
||
558 | } |
||
559 | |||
1 | ingob | 560 | |
24 | StephanB | 561 | // read CSD register |
562 | result = SDC_GetCSD((u8 *)&SDCardInfo.CSD); |
||
563 | if(result != SD_SUCCESS) |
||
564 | { |
||
110 | killagreg | 565 | UART1_PutString("Error reading CSD."); |
24 | StephanB | 566 | goto end; |
567 | } |
||
1 | ingob | 568 | |
450 | holgerb | 569 | if(print) UART1_PutString("ok\r\n"); |
24 | StephanB | 570 | |
571 | u8 c_size_mult, read_bl_len; |
||
572 | u32 c_size; |
||
573 | |||
574 | switch(SDCardInfo.CSD[0]>>6) // check CSD Version |
||
575 | { |
||
576 | case 0x00: // if CSD is V1.0 structure (2GB limit) |
||
577 | |||
578 | /* |
||
579 | memory capacity = BLOCKNR * BLOCK_LEN |
||
580 | BLOCKNR = (C_SIZE+1) * MULT |
||
581 | MULT = 2^(C_SIZE_MULT+2) |
||
582 | BLOCK_LEN = 2^READ_BL_LEN |
||
583 | |||
584 | C_SIZE is 12 bits [73:62] in CSD register |
||
585 | C_SIZE_MULT is 3 bits [49:47] in CSD register |
||
586 | READ_BL_LEN is 4 bits [83:80] in CSD register |
||
587 | */ |
||
588 | |||
589 | read_bl_len = (SDCardInfo.CSD[5] & 0x0F); //CSD[05] -> [87:80] |
||
590 | c_size = ((u32)(SDCardInfo.CSD[6] & 0x03))<<10; //CSD[06] -> [79:72] |
||
591 | c_size |= ((u32)SDCardInfo.CSD[7])<<2; //CSD[07] -> [71:64] |
||
592 | c_size |= (u32)(SDCardInfo.CSD[8]>>6); //CSD[08] -> [63:56] |
||
593 | c_size_mult = (SDCardInfo.CSD[9] & 0x03)<<1; //CSD[09] -> [55:48] |
||
594 | c_size_mult |=(SDCardInfo.CSD[10] & 0x80)>>7; //CSD[10] -> [47:40] |
||
595 | SDCardInfo.Capacity = (u32)(c_size+1)*(1L<<(c_size_mult+2))*(1L<<read_bl_len); |
||
596 | break; |
||
597 | |||
598 | case 0x01: // if CSD is V2.0 structure (HC SD-Card > 2GB) |
||
599 | |||
600 | /* |
||
601 | memory capacity = (C_SIZE+1) * 512K byte |
||
602 | C_SIZE is 22 bits [69:48] in CSR register |
||
603 | */ |
||
604 | |||
605 | c_size = ((u32)(SDCardInfo.CSD[7] & 0x3F))<<16; //CSD[07] -> [71:64] |
||
606 | c_size |= ((u32)SDCardInfo.CSD[8])<<8; //CSD[08] -> [63:56] |
||
607 | c_size |= (u32)SDCardInfo.CSD[9]; //CSD[09] -> [55:48]; |
||
608 | SDCardInfo.Capacity = (c_size + 1)* 512L * 1024L; |
||
41 | ingob | 609 | break; |
610 | |||
611 | default: //unknown CSD Version |
||
24 | StephanB | 612 | SDCardInfo.Capacity = 0; |
613 | break; |
||
614 | } |
||
615 | |||
450 | holgerb | 616 | if(print) switch(SDCardInfo.Version) |
24 | StephanB | 617 | { |
618 | case VER_1X: |
||
149 | killagreg | 619 | UART1_PutString(" SD-CARD V1.x"); |
24 | StephanB | 620 | break; |
621 | case VER_20: |
||
149 | killagreg | 622 | UART1_PutString(" SD-CARD V2.0 or later"); |
24 | StephanB | 623 | default: |
624 | break; |
||
625 | } |
||
450 | holgerb | 626 | if(print) |
627 | { |
||
628 | u16 mb_size = (u16)(SDCardInfo.Capacity/(1024L*1024L)); |
||
629 | sprintf(text, "\r\n Capacity = %i MB", mb_size); |
||
630 | UART1_PutString(text); |
||
631 | } |
||
24 | StephanB | 632 | |
450 | holgerb | 633 | if(print) SDC_PrintCID((u8 *)&SDCardInfo.CID); |
24 | StephanB | 634 | SDCardInfo.Valid = 1; |
635 | // jump point for error condition before |
||
636 | end: |
||
384 | ingob | 637 | //SSC_Disable(); |
638 | ; |
||
24 | StephanB | 639 | } |
640 | else |
||
1 | ingob | 641 | { |
24 | StephanB | 642 | SSC_Deinit(); |
643 | SDCardInfo.Valid = 0; |
||
644 | result = SD_ERROR_NOCARD; |
||
110 | killagreg | 645 | UART1_PutString("No Card in Slot."); |
1 | ingob | 646 | } |
24 | StephanB | 647 | return(result); |
1 | ingob | 648 | } |
649 | |||
650 | |||
651 | //________________________________________________________________________________________________________________________________________ |
||
24 | StephanB | 652 | // Funtion: SDC_Deinit(void); |
1 | ingob | 653 | // |
24 | StephanB | 654 | // Description: This function deinitialises the SDCard interface. |
1 | ingob | 655 | // |
656 | // |
||
324 | killagreg | 657 | // Returnvalue: the function returns 0 if the deinitialisation was successfull otherwise the function returns an errorcode. |
1 | ingob | 658 | //________________________________________________________________________________________________________________________________________ |
659 | |||
24 | StephanB | 660 | SD_Result_t SDC_Deinit(void) |
661 | { |
||
110 | killagreg | 662 | UART1_PutString("\r\n SDC deinit..."); |
364 | ingob | 663 | // SSC_Deinit(); |
1 | ingob | 664 | |
24 | StephanB | 665 | SDCardInfo.Valid = 0; |
666 | SDCardInfo.Capacity = 0; |
||
667 | SDCardInfo.Version = VER_UNKNOWN; |
||
1 | ingob | 668 | |
110 | killagreg | 669 | UART1_PutString("ok"); |
24 | StephanB | 670 | return(SD_SUCCESS); |
1 | ingob | 671 | } |
672 | |||
673 | |||
674 | //________________________________________________________________________________________________________________________________________ |
||
24 | StephanB | 675 | // Funtion: SDC_PutSector(void); |
1 | ingob | 676 | // |
24 | StephanB | 677 | // Description: This function writes one sector of data to the SSC |
1 | ingob | 678 | // |
679 | // |
||
24 | StephanB | 680 | // Returnvalue: SD_Result_t |
1 | ingob | 681 | //________________________________________________________________________________________________________________________________________ |
682 | |||
24 | StephanB | 683 | SD_Result_t SDC_PutSector(u32 addr, const u8 *Buffer) |
684 | { |
||
384 | ingob | 685 | u8 retryCounter = 0; |
686 | u8 rsp; |
||
41 | ingob | 687 | u16 a, crc16; |
24 | StephanB | 688 | u16 timeout = 0; |
384 | ingob | 689 | SD_Result_t result; |
24 | StephanB | 690 | |
691 | addr = addr << 9; // convert sectoradress to byteadress |
||
384 | ingob | 692 | |
693 | while(retryCounter < 10) |
||
694 | { |
||
695 | result = SD_ERROR_UNKNOWN; |
||
696 | |||
24 | StephanB | 697 | rsp = SDC_SendCMDR1(CMD_WRITE_SINGLE_BLOCK, addr); |
698 | if (rsp != R1_NO_ERR) |
||
1 | ingob | 699 | { |
24 | StephanB | 700 | result = SD_ERROR_BAD_RESPONSE; |
701 | goto end; |
||
1 | ingob | 702 | } |
703 | SSC_ClearRxFifo(); |
||
24 | StephanB | 704 | for (a=0;a<20;a++) // at least one byte |
1 | ingob | 705 | { |
24 | StephanB | 706 | SSC_GetChar(); |
1 | ingob | 707 | } |
41 | ingob | 708 | crc16 = CRC16(Buffer, 512); // calc checksum for data block |
24 | StephanB | 709 | SSC_PutChar(DATA_START_TOKEN); // send data start of header to the SSC |
1 | ingob | 710 | |
24 | StephanB | 711 | for (a=0;a<512;a++) // transmit one sector (normaly 512bytes) of data to the sdcard. |
712 | { |
||
713 | SSC_PutChar(Buffer[a]); |
||
714 | } |
||
715 | // write two bytes of crc16 to the sdcard |
||
41 | ingob | 716 | SSC_PutChar((u8)(crc16>>8)); // write high byte first |
717 | SSC_PutChar((u8)(0x00FF&crc16)); // lowbyte last |
||
24 | StephanB | 718 | SSC_ClearRxFifo(); |
719 | do // wait for data response token |
||
720 | { |
||
721 | rsp = SSC_GetChar(); |
||
41 | ingob | 722 | if(timeout++ > 500) |
723 | { |
||
724 | result = SD_ERROR_TIMEOUT; |
||
725 | goto end; |
||
726 | } |
||
24 | StephanB | 727 | }while((rsp & 0x11) != 0x01 ); |
324 | killagreg | 728 | |
24 | StephanB | 729 | // analyse data response token |
730 | switch(rsp & DATA_RESPONSE_MASK) |
||
731 | { |
||
732 | case DATA_RESPONSE_OK: |
||
733 | result = SD_SUCCESS; |
||
734 | break; |
||
735 | case DATA_RESPONSE_CRC_ERR: |
||
736 | result = SD_ERROR_CRC_DATA; |
||
737 | goto end; |
||
738 | break; |
||
739 | case DATA_RESPONSE_WRITE_ERR: |
||
740 | result = SD_ERROR_WRITE_DATA; |
||
741 | goto end; |
||
742 | break; |
||
743 | default: |
||
744 | result = SD_ERROR_UNKNOWN; |
||
745 | goto end; |
||
746 | break; |
||
747 | |||
748 | } |
||
41 | ingob | 749 | // wait 2 seconds until the sdcard is busy. |
750 | rsp = SDC_WaitForBusy(2000); |
||
24 | StephanB | 751 | if(rsp != 0xFF) |
752 | { |
||
41 | ingob | 753 | result = SD_ERROR_TIMEOUT; |
754 | goto end; |
||
24 | StephanB | 755 | } |
41 | ingob | 756 | |
24 | StephanB | 757 | // check card status |
758 | rsp = SDC_SendCMDR1(CMD_SEND_STATUS, 0); |
||
41 | ingob | 759 | // first byte of R2 response is like R1 response |
24 | StephanB | 760 | if(rsp != R1_NO_ERR) |
761 | { |
||
762 | result = SD_ERROR_BAD_RESPONSE; |
||
41 | ingob | 763 | SSC_GetChar(); // read out 2nd byte |
24 | StephanB | 764 | goto end; |
765 | } |
||
766 | // 2nd byte of r2 response |
||
767 | rsp = SSC_GetChar(); |
||
768 | if(rsp != R2_NO_ERR) |
||
769 | { |
||
770 | result = SD_ERROR_WRITE_DATA; |
||
771 | SSC_GetChar(); |
||
772 | goto end; |
||
773 | } |
||
774 | end: |
||
384 | ingob | 775 | //SSC_Disable(); // disable CS |
24 | StephanB | 776 | if(result != SD_SUCCESS) |
777 | { |
||
450 | holgerb | 778 | |
512 | holgerb | 779 | // sprintf(text,"\r\nError %02X writing data to sd card (R=%02X)", result, rsp); |
780 | // UART1_PutString(text); |
||
384 | ingob | 781 | retryCounter++; |
782 | |||
450 | holgerb | 783 | if(SPI_Speed > 1000) |
784 | { |
||
785 | SPI_Speed -= 200; |
||
512 | holgerb | 786 | // sprintf(text," new Speed = %d", SPI_Speed); |
787 | // UART1_PutString(text); |
||
450 | holgerb | 788 | } |
789 | SDC_Init(0); |
||
790 | } |
||
791 | else |
||
792 | { |
||
512 | holgerb | 793 | // if(retryCounter == 1) UART1_PutString(" Problem solved\r\n"); |
450 | holgerb | 794 | break; // Success -> end retry loop |
795 | } |
||
796 | |||
384 | ingob | 797 | } |
798 | if (retryCounter > SDCardWriteRetryCounterMax) |
||
799 | { SDCardWriteRetryCounterMax = retryCounter; |
||
516 | holgerb | 800 | // DebugOut.Analog[] = SDCardWriteRetryCounterMax; |
24 | StephanB | 801 | } |
384 | ingob | 802 | |
24 | StephanB | 803 | return(result); |
41 | ingob | 804 | } |
24 | StephanB | 805 | |
806 | |||
807 | //________________________________________________________________________________________________________________________________________ |
||
808 | // Funtion: SDC_GetSector(u32 addr,u8 *Buffer); |
||
809 | // |
||
810 | // Description: This function reads one sector of data from the SSC |
||
811 | // |
||
812 | // |
||
813 | // Returnvalue: SD_Result_t |
||
814 | //________________________________________________________________________________________________________________________________________ |
||
815 | |||
816 | SD_Result_t SDC_GetSector(u32 addr,u8 *Buffer) |
||
817 | { |
||
818 | addr = addr << 9; // convert sectoradress to byteadress |
||
819 | return SDC_GetData(CMD_READ_SINGLE_BLOCK, addr, Buffer, 512); |
||
1 | ingob | 820 | } |
821 | |||
822 | |||
823 |