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