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