Subversion Repositories NaviCtrl

Rev

Rev 678 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 678 Rev 687
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
*/
336
*/
337
        UART1_PutString("\r\n");
337
        UART1_PutString("\r\n");
338
        temp1 = pCID[14] & 0x0F;    // month 
338
        temp1 = pCID[14] & 0x0F;    // month 
339
        temp2 = ((pCID[14]>>4)|(pCID[13]<<4)) + 2000; // year 
339
        temp2 = ((pCID[14]>>4)|(pCID[13]<<4)) + 2000; // year 
340
        sprintf(text, " Manufac. Date: %i/%i\r\n\r\n",temp1, temp2);
340
        sprintf(text, " Manufac. Date: %i/%i\r\n\r\n",temp1, temp2);
341
        UART1_PutString(text);
341
        UART1_PutString(text);
342
}
342
}
343
 
343
 
344
//________________________________________________________________________________________________________________________________________
344
//________________________________________________________________________________________________________________________________________
345
// Funtion:     SDC_GetCID(u8 * pCID);
345
// Funtion:     SDC_GetCID(u8 * pCID);
346
// 
346
// 
347
// Description: This function reads the CIS register form the sd card in spi mode. 
347
// Description: This function reads the CIS register form the sd card in spi mode. 
348
//                              
348
//                              
349
//
349
//
350
// Returnvalue: the function returns error state
350
// Returnvalue: the function returns error state
351
//________________________________________________________________________________________________________________________________________
351
//________________________________________________________________________________________________________________________________________
352
 
352
 
353
SD_Result_t SDC_GetCID(u8 * pCID)
353
SD_Result_t SDC_GetCID(u8 * pCID)
354
{
354
{
355
        return SDC_GetData(CMD_SEND_CID, 0UL, pCID, 16);
355
        return SDC_GetData(CMD_SEND_CID, 0UL, pCID, 16);
356
}
356
}
357
 
357
 
358
//________________________________________________________________________________________________________________________________________
358
//________________________________________________________________________________________________________________________________________
359
// Funtion:     SDC_GetCSD(u8 * pCSD);
359
// Funtion:     SDC_GetCSD(u8 * pCSD);
360
// 
360
// 
361
// Description: This function reads the CSD register form the sd card in spi mode. 
361
// Description: This function reads the CSD register form the sd card in spi mode. 
362
//                              
362
//                              
363
//
363
//
364
// Returnvalue: the function returns error state
364
// Returnvalue: the function returns error state
365
//________________________________________________________________________________________________________________________________________
365
//________________________________________________________________________________________________________________________________________
366
 
366
 
367
SD_Result_t SDC_GetCSD(u8 * pCSD)
367
SD_Result_t SDC_GetCSD(u8 * pCSD)
368
{
368
{
369
        return SDC_GetData(CMD_SEND_CSD, 0UL, pCSD, 16);
369
        return SDC_GetData(CMD_SEND_CSD, 0UL, pCSD, 16);
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(unsigned char print)
381
SD_Result_t SDC_Init(unsigned char print)
382
{
382
{
383
        u32 timeout = 0;
383
        u32 timeout = 0;
384
        u16 speed;
384
        u16 speed;
385
        u8 rsp[6]; // SD-SPI response buffer
385
        u8 rsp[6]; // SD-SPI response buffer
386
        SD_Result_t result = SD_ERROR_UNKNOWN;
386
        SD_Result_t result = SD_ERROR_UNKNOWN;
387
        if(SPI_Speed < 500) SPI_Speed = 500; // nur zur Sicherheit
387
        if(SPI_Speed < 500) SPI_Speed = 500; // nur zur Sicherheit
388
        speed = SPI_Speed; // weil das bei SSC_Init() auf 400 gesetzt werden würde
388
        speed = SPI_Speed; // weil das bei SSC_Init() auf 400 gesetzt werden würde
389
 
389
 
390
        //SDCardWriteRetryCounterMax = 0;
390
        //SDCardWriteRetryCounterMax = 0;
391
               
391
               
392
        if(SD_SWITCH) // init only if the SD-Switch is indicating a card in the slot
392
        if(SD_SWITCH) // init only if the SD-Switch is indicating a card in the slot
393
        {
393
        {
394
                if(print)               UART1_PutString("\r\n SSC init...");
394
                if(print)               UART1_PutString("\r\n SSC init...");
395
                SSC_Init();
395
                SSC_Init();
396
                if(print)               UART1_PutString("ok");
396
                if(print)               UART1_PutString("ok");
397
 
397
 
398
                if(print)               UART1_PutString("\r\n SDC init...");
398
                if(print)               UART1_PutString("\r\n SDC init...");
399
                SDCardInfo.Valid = 0;
399
                SDCardInfo.Valid = 0;
400
                /* The host shall supply power to the card so that the voltage is reached to Vdd_min within 250ms and
400
                /* The host shall supply power to the card so that the voltage is reached to Vdd_min within 250ms and
401
                start to supply at least 74 SD clocks to the SD card with keeping cmd line to high. In case of SPI
401
                start to supply at least 74 SD clocks to the SD card with keeping cmd line to high. In case of SPI
402
                mode, CS shall be held to high during 74 clock cycles. */
402
                mode, CS shall be held to high during 74 clock cycles. */
403
                SSC_Disable(); // set SD_CS high
403
                SSC_Disable(); // set SD_CS high
404
                SSC_ClearRxFifo();      // clear the rx fifo
404
                SSC_ClearRxFifo();      // clear the rx fifo
405
       
405
       
406
                for (timeout = 0; timeout < 15; timeout++)      // 15*8 = 120 cycles
406
                for (timeout = 0; timeout < 15; timeout++)      // 15*8 = 120 cycles
407
                {
407
                {
408
                        SSC_PutChar(0xFF);
408
                        SSC_PutChar(0xFF);
409
                }
409
                }
410
       
410
       
411
                // switch to idle state
411
                // switch to idle state
412
                while(SDC_SendCMDR1(CMD_GO_IDLE_STATE, 0UL) != R1_IDLE_STATE)                                          
412
                while(SDC_SendCMDR1(CMD_GO_IDLE_STATE, 0UL) != R1_IDLE_STATE)                                          
413
                {
413
                {
414
                        if (timeout++ > 20)
414
                        if (timeout++ > 20)
415
                        {
415
                        {
416
                                UART1_PutString("reset timeout");
416
                                UART1_PutString("reset timeout");
417
                                result = SD_ERROR_RESET;
417
                                result = SD_ERROR_RESET;
418
                                goto end;                                                                              
418
                                goto end;                                                                              
419
                        }
419
                        }
420
                }
420
                }
421
            // enable crc feature
421
            // enable crc feature
422
/*              if(SDC_SendCMDR1(CMD_CRC_ON_OFF, 1UL) != R1_IDLE_STATE)
422
/*              if(SDC_SendCMDR1(CMD_CRC_ON_OFF, 1UL) != R1_IDLE_STATE)
423
                {
423
                {
424
                                sprintf(text,"Bad cmd59 R1=%02X.", rsp[0]);
424
                                sprintf(text,"Bad cmd59 R1=%02X.", rsp[0]);
425
                                UART1_PutString(text);
425
                                UART1_PutString(text);
426
                                result = SD_ERROR_BAD_RESPONSE;
426
                                result = SD_ERROR_BAD_RESPONSE;
427
                                goto end;
427
                                goto end;
428
                }*/
428
                }*/
429
                // check for card hw version                                            
429
                // check for card hw version                                            
430
                // 2.7-3.6V Range = 0x01, check pattern 0xAA
430
                // 2.7-3.6V Range = 0x01, check pattern 0xAA
431
                rsp[0] = SDC_SendCMDR1(CMD_SEND_IF_COND, 0x000001AA);
431
                rsp[0] = SDC_SendCMDR1(CMD_SEND_IF_COND, 0x000001AA);
432
                // answer to cmd58 is an R7 response (R1+ 4Byte IFCond)
432
                // answer to cmd58 is an R7 response (R1+ 4Byte IFCond)
433
                if(rsp[0] & R1_BAD_RESPONSE)
433
                if(rsp[0] & R1_BAD_RESPONSE)
434
                {
434
                {
435
                        sprintf(text,"Bad cmd8 R1=%02X.", rsp[0]);
435
                        sprintf(text,"Bad cmd8 R1=%02X.", rsp[0]);
436
                        UART1_PutString(text);
436
                        UART1_PutString(text);
437
                        result = SD_ERROR_BAD_RESPONSE;
437
                        result = SD_ERROR_BAD_RESPONSE;
438
                        goto end;
438
                        goto end;
439
                }
439
                }
440
                if(rsp[0] & R1_ILLEGAL_CMD)
440
                if(rsp[0] & R1_ILLEGAL_CMD)
441
                {
441
                {
442
                        //Ver1.X SD Memory Card or not a SD Memory Card
442
                        //Ver1.X SD Memory Card or not a SD Memory Card
443
                        SDCardInfo.Version = VER_1X;                   
443
                        SDCardInfo.Version = VER_1X;                   
444
                }
444
                }
445
                else
445
                else
446
                {
446
                {
447
                   // Ver2.00 or later SD Memory Card
447
                   // Ver2.00 or later SD Memory Card
448
                   // reading the remaining bytes of the R7 response
448
                   // reading the remaining bytes of the R7 response
449
                   SDCardInfo.Version = VER_20;
449
                   SDCardInfo.Version = VER_20;
450
                   for(timeout = 1; timeout < 5; timeout++)
450
                   for(timeout = 1; timeout < 5; timeout++)
451
                   {
451
                   {
452
                                rsp[timeout] = SSC_GetChar();
452
                                rsp[timeout] = SSC_GetChar();
453
                   }
453
                   }
454
                   //check pattern
454
                   //check pattern
455
                   if(rsp[4]!= 0xAA)
455
                   if(rsp[4]!= 0xAA)
456
                   {
456
                   {
457
                                UART1_PutString("Bad cmd8 R7 check pattern.\r\n");
457
                                UART1_PutString("Bad cmd8 R7 check pattern.\r\n");
458
                                result = SD_ERROR_BAD_RESPONSE;
458
                                result = SD_ERROR_BAD_RESPONSE;
459
                                goto end;
459
                                goto end;
460
                   }
460
                   }
461
                   if ( (rsp[3] & 0x0F)!= 0x01 ) // voltage range is not 2.7-3.6V
461
                   if ( (rsp[3] & 0x0F)!= 0x01 ) // voltage range is not 2.7-3.6V
462
                   {
462
                   {
463
                               
463
                               
464
                                UART1_PutString("Card is incompatible to 3.3V.\r\n");
464
                                UART1_PutString("Card is incompatible to 3.3V.\r\n");
465
                                result = SD_ERROR_BAD_VOLTAGE_RANGE;
465
                                result = SD_ERROR_BAD_VOLTAGE_RANGE;
466
                                goto end;              
466
                                goto end;              
467
                   }
467
                   }
468
                }
468
                }
469
       
469
       
470
                rsp[0] = SDC_SendCMDR1(CMD_READ_OCR, 0UL);
470
                rsp[0] = SDC_SendCMDR1(CMD_READ_OCR, 0UL);
471
                // answer to cmd58 is an R3 response (R1 + 4Byte OCR)
471
                // answer to cmd58 is an R3 response (R1 + 4Byte OCR)
472
                if(rsp[0] & R1_BAD_RESPONSE)
472
                if(rsp[0] & R1_BAD_RESPONSE)
473
                {
473
                {
474
                        sprintf(text,"Bad cmd58 R1 %02x.", rsp[0]);
474
                        sprintf(text,"Bad cmd58 R1 %02x.", rsp[0]);
475
                        UART1_PutString(text);
475
                        UART1_PutString(text);
476
                        result = SD_ERROR_BAD_RESPONSE;
476
                        result = SD_ERROR_BAD_RESPONSE;
477
                        goto end;
477
                        goto end;
478
                }
478
                }
479
                if(rsp[0] & R1_ILLEGAL_CMD)
479
                if(rsp[0] & R1_ILLEGAL_CMD)
480
                {
480
                {
481
                        UART1_PutString("Not an SD-CARD.");
481
                        UART1_PutString("Not an SD-CARD.");
482
                        result = SD_ERROR_NO_SDCARD;
482
                        result = SD_ERROR_NO_SDCARD;
483
                        goto end;
483
                        goto end;
484
                }
484
                }
485
                // read 4 bytes of OCR register
485
                // read 4 bytes of OCR register
486
                for(timeout = 1; timeout < 5; timeout++)
486
                for(timeout = 1; timeout < 5; timeout++)
487
                {
487
                {
488
                        rsp[timeout] = SSC_GetChar();
488
                        rsp[timeout] = SSC_GetChar();
489
                }
489
                }
490
                //      NavicCtrl uses 3.3 V,  therefore check for bit 20 & 21 
490
                //      NavicCtrl uses 3.3 V,  therefore check for bit 20 & 21 
491
                if((rsp[2] & 0x30) != 0x30)
491
                if((rsp[2] & 0x30) != 0x30)
492
                {
492
                {
493
                        // supply voltage is not supported by sd-card
493
                        // supply voltage is not supported by sd-card
494
                        UART1_PutString("Card is incompatible to 3.3V.");
494
                        UART1_PutString("Card is incompatible to 3.3V.");
495
                        result = SD_ERROR_BAD_VOLTAGE_RANGE;
495
                        result = SD_ERROR_BAD_VOLTAGE_RANGE;
496
                        goto end;
496
                        goto end;
497
                }
497
                }
498
               
498
               
499
                // Initialize the sd-card sending continously ACMD_SEND_OP_COND (only supported by SD cards)
499
                // Initialize the sd-card sending continously ACMD_SEND_OP_COND (only supported by SD cards)
500
                timeout =  SetDelay(2000); // set timeout to 2000 ms (large cards tend to longer) 
500
                timeout =  SetDelay(2000); // set timeout to 2000 ms (large cards tend to longer) 
501
                do
501
                do
502
                {
502
                {
503
                        rsp[0] = SDC_SendACMDR1(ACMD_SEND_OP_COND, 0UL);
503
                        rsp[0] = SDC_SendACMDR1(ACMD_SEND_OP_COND, 0UL);
504
                        if(rsp[0] & R1_BAD_RESPONSE)
504
                        if(rsp[0] & R1_BAD_RESPONSE)
505
                        {
505
                        {
506
                                sprintf(text,"Bad Acmd41 R1=%02X.", rsp[0]);
506
                                sprintf(text,"Bad Acmd41 R1=%02X.", rsp[0]);
507
                                UART1_PutString(text);
507
                                UART1_PutString(text);
508
                                result = SD_ERROR_BAD_RESPONSE;
508
                                result = SD_ERROR_BAD_RESPONSE;
509
                                goto end;
509
                                goto end;
510
                        }
510
                        }
511
                        if(CheckDelay(timeout))
511
                        if(CheckDelay(timeout))
512
                        {
512
                        {
513
                            UART1_PutString("Init timeout.");
513
                            UART1_PutString("Init timeout.");
514
                                result = SD_ERROR_INITIALIZE;
514
                                result = SD_ERROR_INITIALIZE;
515
                                goto end;
515
                                goto end;
516
                        }
516
                        }
517
                } while(rsp[0] & R1_IDLE_STATE); // loop until idle state
517
                } while(rsp[0] & R1_IDLE_STATE); // loop until idle state
518
               
518
               
519
                if(rsp[0] != R1_NO_ERR)
519
                if(rsp[0] != R1_NO_ERR)
520
                {
520
                {
521
                        UART1_PutString("Init error.");
521
                        UART1_PutString("Init error.");
522
                        result = SD_ERROR_INITIALIZE;
522
                        result = SD_ERROR_INITIALIZE;
523
                        goto end;      
523
                        goto end;      
524
                }
524
                }
525
                /* set block size to 512 bytes */
525
                /* set block size to 512 bytes */
526
        if(SDC_SendCMDR1(CMD_SET_BLOCKLEN, 512UL) != R1_NO_ERR)
526
        if(SDC_SendCMDR1(CMD_SET_BLOCKLEN, 512UL) != R1_NO_ERR)
527
        {
527
        {
528
                UART1_PutString("Error setting block length to 512.");
528
                UART1_PutString("Error setting block length to 512.");
529
                        result = SD_ERROR_SET_BLOCKLEN;
529
                        result = SD_ERROR_SET_BLOCKLEN;
530
                        goto end;
530
                        goto end;
531
        }
531
        }
532
               
532
               
533
                SSC_Disable(); // set SD_CS high                                                                         
533
                SSC_Disable(); // set SD_CS high                                                                         
534
                // here is the right place to inrease the SPI baud rate to maximum              
534
                // here is the right place to inrease the SPI baud rate to maximum              
535
 
535
 
536
        SSC_Speed(speed);
536
        SSC_Speed(speed);
537
                if(print) { sprintf(text,"\r\n Setting SD-Card speed to %d ", SPI_Speed);UART1_PutString(text); }
537
                if(print) { sprintf(text,"\r\n Setting SD-Card speed to %d ", SPI_Speed);UART1_PutString(text); }
538
               
538
               
539
        SSC_Enable(); // set SD_CS high
539
        SSC_Enable(); // set SD_CS high
540
 
540
 
541
               // read CID register
541
               // read CID register
542
               result = SDC_GetCID((u8 *)&SDCardInfo.CID);
542
               result = SDC_GetCID((u8 *)&SDCardInfo.CID);
543
               if(result != SD_SUCCESS)
543
               if(result != SD_SUCCESS)
544
               {
544
               {
545
                       
545
                       
546
                                if(print) UART1_PutString(" failed - Set interface to low speed...");
546
                                if(print) UART1_PutString(" failed - Set interface to low speed...");
547
                       SSC_Disable(); // set SD_CS high                                                                          
547
                       SSC_Disable(); // set SD_CS high                                                                          
548
                       // here is the right place to inrease the SPI baud rate to maximum                
548
                       // here is the right place to inrease the SPI baud rate to maximum                
549
                       SPI_Speed = 1000;
549
                       SPI_Speed = 1000;
550
                                           SSC_Speed(SPI_Speed);
550
                                           SSC_Speed(SPI_Speed);
551
                       SSC_Enable(); // set SD_CS high
551
                       SSC_Enable(); // set SD_CS high
552
                       result = SDC_GetCID((u8 *)&SDCardInfo.CID);
552
                       result = SDC_GetCID((u8 *)&SDCardInfo.CID);
553
               }
553
               }
554
       
554
       
555
               if(result != SD_SUCCESS)
555
               if(result != SD_SUCCESS)
556
               {
556
               {
557
                       UART1_PutString("Error reading CID.\r\n");
557
                       UART1_PutString("Error reading CID.\r\n");
558
                       goto end;
558
                       goto end;
559
               }
559
               }
560
 
560
 
561
       
561
       
562
                // read CSD register
562
                // read CSD register
563
                result = SDC_GetCSD((u8 *)&SDCardInfo.CSD);
563
                result = SDC_GetCSD((u8 *)&SDCardInfo.CSD);
564
                if(result != SD_SUCCESS)
564
                if(result != SD_SUCCESS)
565
                {
565
                {
566
                        UART1_PutString("Error reading CSD.");
566
                        UART1_PutString("Error reading CSD.");
567
                        goto end;
567
                        goto end;
568
                }
568
                }
569
       
569
       
570
                if(print) UART1_PutString("ok\r\n");
570
                if(print) UART1_PutString("ok\r\n");
571
       
571
       
572
                u8 c_size_mult, read_bl_len;
572
                u8 c_size_mult, read_bl_len;
573
                u32 c_size;
573
                u32 c_size;
574
       
574
       
575
                switch(SDCardInfo.CSD[0]>>6) // check CSD Version
575
                switch(SDCardInfo.CSD[0]>>6) // check CSD Version
576
                {
576
                {
577
                case 0x00: // if CSD is V1.0 structure (2GB limit)
577
                case 0x00: // if CSD is V1.0 structure (2GB limit)
578
       
578
       
579
                        /*
579
                        /*
580
                        memory capacity = BLOCKNR * BLOCK_LEN
580
                        memory capacity = BLOCKNR * BLOCK_LEN
581
                        BLOCKNR = (C_SIZE+1) * MULT
581
                        BLOCKNR = (C_SIZE+1) * MULT
582
                        MULT = 2^(C_SIZE_MULT+2)
582
                        MULT = 2^(C_SIZE_MULT+2)
583
                        BLOCK_LEN = 2^READ_BL_LEN
583
                        BLOCK_LEN = 2^READ_BL_LEN
584
               
584
               
585
                        C_SIZE      is 12 bits [73:62] in CSD register
585
                        C_SIZE      is 12 bits [73:62] in CSD register
586
                        C_SIZE_MULT is  3 bits [49:47] in CSD register
586
                        C_SIZE_MULT is  3 bits [49:47] in CSD register
587
                        READ_BL_LEN is  4 bits [83:80] in CSD register
587
                        READ_BL_LEN is  4 bits [83:80] in CSD register
588
                        */
588
                        */
589
       
589
       
590
                        read_bl_len = (SDCardInfo.CSD[5] & 0x0F);               //CSD[05] -> [87:80]
590
                        read_bl_len = (SDCardInfo.CSD[5] & 0x0F);               //CSD[05] -> [87:80]
591
                        c_size = ((u32)(SDCardInfo.CSD[6] & 0x03))<<10; //CSD[06] -> [79:72]
591
                        c_size = ((u32)(SDCardInfo.CSD[6] & 0x03))<<10; //CSD[06] -> [79:72]
592
                        c_size |= ((u32)SDCardInfo.CSD[7])<<2;                  //CSD[07] -> [71:64]
592
                        c_size |= ((u32)SDCardInfo.CSD[7])<<2;                  //CSD[07] -> [71:64]
593
                        c_size |= (u32)(SDCardInfo.CSD[8]>>6);                  //CSD[08] -> [63:56]
593
                        c_size |= (u32)(SDCardInfo.CSD[8]>>6);                  //CSD[08] -> [63:56]
594
                        c_size_mult = (SDCardInfo.CSD[9] & 0x03)<<1;    //CSD[09] -> [55:48]
594
                        c_size_mult = (SDCardInfo.CSD[9] & 0x03)<<1;    //CSD[09] -> [55:48]
595
                        c_size_mult |=(SDCardInfo.CSD[10] & 0x80)>>7;   //CSD[10] -> [47:40]
595
                        c_size_mult |=(SDCardInfo.CSD[10] & 0x80)>>7;   //CSD[10] -> [47:40]
596
                        SDCardInfo.Capacity = (u32)(c_size+1)*(1L<<(c_size_mult+2))*(1L<<read_bl_len);
596
                        SDCardInfo.Capacity = (u32)(c_size+1)*(1L<<(c_size_mult+2))*(1L<<read_bl_len);
597
                        break;
597
                        break;
598
       
598
       
599
                case 0x01: // if CSD is V2.0 structure (HC SD-Card > 2GB) 
599
                case 0x01: // if CSD is V2.0 structure (HC SD-Card > 2GB) 
600
       
600
       
601
                        /*
601
                        /*
602
                        memory capacity = (C_SIZE+1) * 512K byte
602
                        memory capacity = (C_SIZE+1) * 512K byte
603
                        C_SIZE is 22 bits [69:48] in CSR register
603
                        C_SIZE is 22 bits [69:48] in CSR register
604
                        */
604
                        */
605
       
605
       
606
                        c_size = ((u32)(SDCardInfo.CSD[7] & 0x3F))<<16; //CSD[07] -> [71:64]
606
                        c_size = ((u32)(SDCardInfo.CSD[7] & 0x3F))<<16; //CSD[07] -> [71:64]
607
                        c_size |= ((u32)SDCardInfo.CSD[8])<<8;                  //CSD[08] -> [63:56]
607
                        c_size |= ((u32)SDCardInfo.CSD[8])<<8;                  //CSD[08] -> [63:56]
608
                        c_size |= (u32)SDCardInfo.CSD[9];                               //CSD[09] -> [55:48];
608
                        c_size |= (u32)SDCardInfo.CSD[9];                               //CSD[09] -> [55:48];
609
                        SDCardInfo.Capacity = (c_size + 1)* 512L * 1024L;
609
                        SDCardInfo.Capacity = (c_size + 1)* 512L * 1024L;
610
                        break;
610
                        break;
611
       
611
       
612
                default: //unknown CSD Version
612
                default: //unknown CSD Version
613
                        SDCardInfo.Capacity = 0;
613
                        SDCardInfo.Capacity = 0;
614
                        break; 
614
                        break; 
615
                }
615
                }
616
       
616
       
617
                if(print) switch(SDCardInfo.Version)
617
                if(print) switch(SDCardInfo.Version)
618
                {
618
                {
619
                        case VER_1X:
619
                        case VER_1X:
620
                                UART1_PutString(" SD-CARD V1.x");
620
                                UART1_PutString(" SD-CARD V1.x");
621
                                break;
621
                                break;
622
                        case VER_20:
622
                        case VER_20:
623
                                UART1_PutString(" SD-CARD V2.0 or later");
623
                                UART1_PutString(" SD-CARD V2.0 or later");
624
                        default:
624
                        default:
625
                                break;
625
                                break;
626
                }
626
                }
627
                if(print)
627
                if(print)
628
                {
628
                {
629
                 u16 mb_size = (u16)(SDCardInfo.Capacity/(1024L*1024L));
629
                 u16 mb_size = (u16)(SDCardInfo.Capacity/(1024L*1024L));
630
                 sprintf(text, "\r\n Capacity = %i MB", mb_size);
630
                 sprintf(text, "\r\n Capacity = %i MB", mb_size);
631
                 UART1_PutString(text);
631
                 UART1_PutString(text);
632
                }
632
                }
633
       
633
       
634
                if(print) SDC_PrintCID((u8 *)&SDCardInfo.CID);
634
                if(print) SDC_PrintCID((u8 *)&SDCardInfo.CID);
635
                SDCardInfo.Valid = 1;
635
                SDCardInfo.Valid = 1;
636
                // jump point for error condition before
636
                // jump point for error condition before
637
                end:
637
                end:
638
                //SSC_Disable();
638
                //SSC_Disable();
639
                ;
639
                ;
640
        }
640
        }
641
        else
641
        else
642
        {
642
        {
643
                SSC_Deinit();
643
                SSC_Deinit();
644
                SDCardInfo.Valid = 0;
644
                SDCardInfo.Valid = 0;
645
                result = SD_ERROR_NOCARD;
645
                result = SD_ERROR_NOCARD;
646
                UART1_PutString("No Card in Slot.");
646
                UART1_PutString("No Card in Slot.");
647
        }
647
        }
648
        return(result);
648
        return(result);
649
}
649
}
650
 
650
 
651
 
651
 
652
//________________________________________________________________________________________________________________________________________
652
//________________________________________________________________________________________________________________________________________
653
// Funtion:     SDC_Deinit(void);
653
// Funtion:     SDC_Deinit(void);
654
// 
654
// 
655
// Description: This function deinitialises the SDCard interface. 
655
// Description: This function deinitialises the SDCard interface. 
656
//                              
656
//                              
657
//
657
//
658
// Returnvalue: the function returns 0 if the deinitialisation was successfull otherwise the function returns an errorcode.
658
// Returnvalue: the function returns 0 if the deinitialisation was successfull otherwise the function returns an errorcode.
659
//________________________________________________________________________________________________________________________________________
659
//________________________________________________________________________________________________________________________________________
660
 
660
 
661
SD_Result_t SDC_Deinit(void)
661
SD_Result_t SDC_Deinit(void)
662
{
662
{
663
        UART1_PutString("\r\n SDC deinit...");
663
        UART1_PutString("\r\n SDC deinit...");
664
        // SSC_Deinit();
664
        // SSC_Deinit();
665
 
665
 
666
        SDCardInfo.Valid = 0;
666
        SDCardInfo.Valid = 0;
667
        SDCardInfo.Capacity = 0;
667
        SDCardInfo.Capacity = 0;
668
        SDCardInfo.Version = VER_UNKNOWN;
668
        SDCardInfo.Version = VER_UNKNOWN;
669
 
669
 
670
        UART1_PutString("ok");
670
        UART1_PutString("ok");
671
        return(SD_SUCCESS);
671
        return(SD_SUCCESS);
672
}
672
}
673
 
673
 
674
 
674
 
675
//________________________________________________________________________________________________________________________________________
675
//________________________________________________________________________________________________________________________________________
676
// Funtion:     SDC_PutSector(void);
676
// Funtion:     SDC_PutSector(void);
677
// 
677
// 
678
// Description: This function writes one sector of data to the SSC 
678
// Description: This function writes one sector of data to the SSC 
679
//                              
679
//                              
680
//
680
//
681
// Returnvalue: SD_Result_t
681
// Returnvalue: SD_Result_t
682
//________________________________________________________________________________________________________________________________________
682
//________________________________________________________________________________________________________________________________________
683
 
683
 
684
SD_Result_t SDC_PutSector(u32 addr, const u8 *Buffer)
684
SD_Result_t SDC_PutSector(u32 addr, const u8 *Buffer)
685
{
685
{
686
    u8 retryCounter = 0;
686
    u8 retryCounter = 0;
687
    u8 rsp;
687
    u8 rsp;
688
        u16 a, crc16;
688
        u16 a, crc16;
689
        u16 timeout = 0;
689
        u16 timeout = 0;
690
        SD_Result_t result;
690
        SD_Result_t result;
691
 
691
 
692
        addr = addr << 9; // convert sectoradress to byteadress
692
        addr = addr << 9; // convert sectoradress to byteadress
693
 
693
 
694
  while(retryCounter < 10)
694
  while(retryCounter < 10)
695
  {    
695
  {    
696
    result = SD_ERROR_UNKNOWN;
696
    result = SD_ERROR_UNKNOWN;
697
 
697
 
698
        rsp = SDC_SendCMDR1(CMD_WRITE_SINGLE_BLOCK, addr);
698
        rsp = SDC_SendCMDR1(CMD_WRITE_SINGLE_BLOCK, addr);
699
        if (rsp != R1_NO_ERR)
699
        if (rsp != R1_NO_ERR)
700
        {
700
        {
701
                result = SD_ERROR_BAD_RESPONSE;
701
                result = SD_ERROR_BAD_RESPONSE;
702
                goto end;
702
                goto end;
703
        }
703
        }
704
        SSC_ClearRxFifo();             
704
        SSC_ClearRxFifo();             
705
        for (a=0;a<20;a++)                                      // at least one byte
705
        for (a=0;a<20;a++)                                      // at least one byte
706
        {
706
        {
707
                SSC_GetChar();
707
                SSC_GetChar();
708
        }
708
        }
709
        crc16 = CRC16(Buffer, 512);         // calc checksum for data block
709
        crc16 = CRC16(Buffer, 512);         // calc checksum for data block
710
        SSC_PutChar(DATA_START_TOKEN);          // send data start of header to the SSC 
710
        SSC_PutChar(DATA_START_TOKEN);          // send data start of header to the SSC 
711
       
711
       
712
        for (a=0;a<512;a++)                                     // transmit one sector (normaly 512bytes) of data to the sdcard.
712
        for (a=0;a<512;a++)                                     // transmit one sector (normaly 512bytes) of data to the sdcard.
713
        {
713
        {
714
                SSC_PutChar(Buffer[a]);
714
                SSC_PutChar(Buffer[a]);
715
        }
715
        }
716
        // write two bytes of crc16 to the sdcard
716
        // write two bytes of crc16 to the sdcard
717
        SSC_PutChar((u8)(crc16>>8));            // write high byte first
717
        SSC_PutChar((u8)(crc16>>8));            // write high byte first
718
        SSC_PutChar((u8)(0x00FF&crc16));        // lowbyte last
718
        SSC_PutChar((u8)(0x00FF&crc16));        // lowbyte last
719
        SSC_ClearRxFifo();
719
        SSC_ClearRxFifo();
720
        do                                                                      // wait for data response token
720
        do                                                                      // wait for data response token
721
        {
721
        {
722
                rsp = SSC_GetChar();
722
                rsp = SSC_GetChar();
723
                if(timeout++ > 500)
723
                if(timeout++ > 500)
724
                {
724
                {
725
                        result = SD_ERROR_TIMEOUT;
725
                        result = SD_ERROR_TIMEOUT;
726
                        goto end;
726
                        goto end;
727
                }
727
                }
728
        }while((rsp & 0x11) != 0x01 );
728
        }while((rsp & 0x11) != 0x01 );
729
       
729
       
730
        // analyse data response token
730
        // analyse data response token
731
        switch(rsp & DATA_RESPONSE_MASK)
731
        switch(rsp & DATA_RESPONSE_MASK)
732
        {
732
        {
733
                case DATA_RESPONSE_OK:
733
                case DATA_RESPONSE_OK:
734
                        result = SD_SUCCESS;
734
                        result = SD_SUCCESS;
735
                        break;
735
                        break;
736
                case DATA_RESPONSE_CRC_ERR:
736
                case DATA_RESPONSE_CRC_ERR:
737
                        result = SD_ERROR_CRC_DATA;
737
                        result = SD_ERROR_CRC_DATA;
738
                        goto end;
738
                        goto end;
739
                        break;
739
                        break;
740
                case DATA_RESPONSE_WRITE_ERR:
740
                case DATA_RESPONSE_WRITE_ERR:
741
                        result = SD_ERROR_WRITE_DATA;
741
                        result = SD_ERROR_WRITE_DATA;
742
                        goto end;
742
                        goto end;
743
                        break;
743
                        break;
744
                default:
744
                default:
745
                        result = SD_ERROR_UNKNOWN;
745
                        result = SD_ERROR_UNKNOWN;
746
                        goto end;
746
                        goto end;
747
                        break;
747
                        break;
748
 
748
 
749
        }
749
        }
750
        // wait 2 seconds until the sdcard is busy.
750
        // wait 2 seconds until the sdcard is busy.
751
        rsp = SDC_WaitForBusy(2000);
751
        rsp = SDC_WaitForBusy(2000);
752
        if(rsp != 0xFF)
752
        if(rsp != 0xFF)
753
        {
753
        {
754
                result =  SD_ERROR_TIMEOUT;
754
                result =  SD_ERROR_TIMEOUT;
755
                goto end;
755
                goto end;
756
        }
756
        }
757
 
757
 
758
        // check card status
758
        // check card status
759
        rsp = SDC_SendCMDR1(CMD_SEND_STATUS, 0);
759
        rsp = SDC_SendCMDR1(CMD_SEND_STATUS, 0);
760
        // first byte of R2 response is like R1 response
760
        // first byte of R2 response is like R1 response
761
        if(rsp != R1_NO_ERR)
761
        if(rsp != R1_NO_ERR)
762
        {
762
        {
763
                result =  SD_ERROR_BAD_RESPONSE;
763
                result =  SD_ERROR_BAD_RESPONSE;
764
                SSC_GetChar(); // read out 2nd byte
764
                SSC_GetChar(); // read out 2nd byte
765
                goto end;
765
                goto end;
766
        }
766
        }
767
        // 2nd byte of r2 response
767
        // 2nd byte of r2 response
768
        rsp = SSC_GetChar();
768
        rsp = SSC_GetChar();
769
        if(rsp != R2_NO_ERR)
769
        if(rsp != R2_NO_ERR)
770
        {
770
        {
771
                result =  SD_ERROR_WRITE_DATA;
771
                result =  SD_ERROR_WRITE_DATA;
772
                SSC_GetChar();
772
                SSC_GetChar();
773
                goto end;
773
                goto end;
774
        }
774
        }
775
        end:
775
        end:
776
        //SSC_Disable();   // disable CS
776
        //SSC_Disable();   // disable CS
777
        if(result != SD_SUCCESS)
777
        if(result != SD_SUCCESS)
778
        {
778
        {
779
 
779
 
780
//              sprintf(text,"\r\nError %02X writing data to sd card (R=%02X)", result, rsp);
780
//              sprintf(text,"\r\nError %02X writing data to sd card (R=%02X)", result, rsp);
781
//              UART1_PutString(text);
781
//              UART1_PutString(text);
782
                retryCounter++;
782
                retryCounter++;
783
 
783
 
784
                if(SPI_Speed > 1000)
784
                if(SPI_Speed > 1000)
785
                {
785
                {
786
                        SPI_Speed -= 200;  
786
                        SPI_Speed -= 200;  
787
//                      sprintf(text," new Speed = %d", SPI_Speed);
787
//                      sprintf(text," new Speed = %d", SPI_Speed);
788
//                      UART1_PutString(text);
788
//                      UART1_PutString(text);
789
                }
789
                }
790
                SDC_Init(0);
790
                SDC_Init(0);
791
        }
791
        }
792
        else
792
        else
793
        {
793
        {
794
//       if(retryCounter == 1) UART1_PutString(" Problem solved\r\n");   
794
//       if(retryCounter == 1) UART1_PutString(" Problem solved\r\n");   
795
         break;      // Success -> end retry loop
795
         break;      // Success -> end retry loop
796
        }
796
        }
797
 
797
 
798
  }
798
  }
799
        if (retryCounter > SDCardWriteRetryCounterMax)
799
        if (retryCounter > SDCardWriteRetryCounterMax)
800
        { SDCardWriteRetryCounterMax = retryCounter;
800
        { SDCardWriteRetryCounterMax = retryCounter;
801
//        DebugOut.Analog[] = SDCardWriteRetryCounterMax;
801
//        DebugOut.Analog[] = SDCardWriteRetryCounterMax;
802
        }
802
        }
803
 
803
 
804
        return(result);
804
        return(result);
805
}
805
}
806
 
806
 
807
 
807
 
808
//________________________________________________________________________________________________________________________________________
808
//________________________________________________________________________________________________________________________________________
809
// Funtion:     SDC_GetSector(u32 addr,u8 *Buffer);
809
// Funtion:     SDC_GetSector(u32 addr,u8 *Buffer);
810
// 
810
// 
811
// Description: This function reads one sector of data from the SSC
811
// Description: This function reads one sector of data from the SSC
812
//                              
812
//                              
813
//
813
//
814
// Returnvalue: SD_Result_t
814
// Returnvalue: SD_Result_t
815
//________________________________________________________________________________________________________________________________________
815
//________________________________________________________________________________________________________________________________________
816
 
816
 
817
SD_Result_t SDC_GetSector(u32 addr,u8 *Buffer)
817
SD_Result_t SDC_GetSector(u32 addr,u8 *Buffer)
818
{
818
{
819
        addr = addr << 9; // convert sectoradress to byteadress
819
        addr = addr << 9; // convert sectoradress to byteadress
820
        return SDC_GetData(CMD_READ_SINGLE_BLOCK, addr, Buffer, 512);
820
        return SDC_GetData(CMD_READ_SINGLE_BLOCK, addr, Buffer, 512);
821
}
821
}
822
 
822
 
823
 
823
 
824
 
824
 
825
 
825