Subversion Repositories NaviCtrl

Rev

Rev 44 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 ingob 1
/*#######################################################################################*/
2
/* !!! THIS IS NOT FREE SOFTWARE !!!                                                     */
3
/*#######################################################################################*/
4
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5
// + Copyright (c) 2008 Ingo Busker, Holger Buss
171 ingob 6
// + Nur für den privaten Gebrauch / NON-COMMERCIAL USE ONLY
1 ingob 7
// + FOR NON COMMERCIAL USE ONLY
8
// + www.MikroKopter.com
9
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
10
// + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation), 
11
// + dass eine Nutzung (auch auszugsweise) nur für den privaten (nicht-kommerziellen) Gebrauch zulässig ist. 
12
// + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt 
13
// + bzgl. der Nutzungsbedingungen aufzunehmen. 
14
// + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen,
15
// + Verkauf von Luftbildaufnahmen, usw.
16
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
17
// + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht, 
18
// + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen
19
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
20
// + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts
21
// + auf anderen Webseiten oder sonstigen Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de"
22
// + eindeutig als Ursprung verlinkt werden
23
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
24
// + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion
25
// + Benutzung auf eigene Gefahr
26
// + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden
27
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
171 ingob 28
// + Die Portierung oder Nutzung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur 
1 ingob 29
// + mit unserer Zustimmung zulässig
30
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
31
// + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen
32
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
33
// + Redistributions of source code (with or without modifications) must retain the above copyright notice, 
34
// + this list of conditions and the following disclaimer.
35
// +   * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived
36
// +     from this software without specific prior written permission.
37
// +   * The use of this project (hardware, software, binary files, sources and documentation) is only permitted 
38
// +     for non-commercial use (directly or indirectly)
39
// +     Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted 
40
// +     with our written permission
41
// +   * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be 
42
// +     clearly linked as origin 
171 ingob 43
// +   * porting the sources to other systems or using the software on other systems (except hardware from www.mikrokopter.de) is not allowed
1 ingob 44
//
45
// +  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
46
// +  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47
// +  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48
// +  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
49
// +  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
50
// +  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
51
// +  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
52
// +  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
53
// +  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
54
// +  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
55
// +  POSSIBILITY OF SUCH DAMAGE. 
56
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
24 StephanB 57
#include <stdio.h>
58
#include <string.h>
59
#include "91x_lib.h"
41 ingob 60
#include "uart1.h"
24 StephanB 61
#include "sdc.h"
62
#include "ssc.h"
63
#include "timer.h"
1 ingob 64
#include "main.h"
24 StephanB 65
#include "crc16.h"
1 ingob 66
 
67
//________________________________________________________________________________________________________________________________________
24 StephanB 68
// Module name:                 sdc.c 
1 ingob 69
// Compiler used:               avr-gcc 3.4.5
24 StephanB 70
// Last Modifikation:   08.06.2008
71
// Version:                             1.07
72
// Authors:                             Stephan Busker, Gregor Stobrawa         
73
// Description:                 Source files for connecting to an sd-card using the SSC
1 ingob 74
//
75
//........................................................................................................................................
24 StephanB 76
// Functions:                   SD_Result_t     SDC_init(void);
77
//                                              u8                      SDC_PutCommand (u8 *cmd);
78
//                                              SD_Result_t     SDC_PutSector(u32 addr,u8 *Buffer);
79
//                                              SD_Result_t     SDC_GetSector(u32 addr,u8 *Buffer);
1 ingob 80
//
81
////........................................................................................................................................
82
// ext. functions:              extern void SSC_Init(void);
24 StephanB 83
//                                              extern u8   SSC_GetChar (void);
1 ingob 84
//                                              extern void SSC_PutChar (u8);
85
//                                              extern void SSC_Enable(void);
86
//                                              extern void SSC_Disable(void);
24 StephanB 87
//                                              extern void     SSC_ClearRxFifo();
1 ingob 88
//........................................................................................................................................
89
//
90
// URL:                                 www.Mikro-Control.de
91
// mailto:                              stephan.busker@mikro-control.de
92
//________________________________________________________________________________________________________________________________________
93
 
94
 
95
 
24 StephanB 96
#define CMD_GO_IDLE_STATE               0x00    /* CMD00: response R1 */
97
#define CMD_SEND_OP_COND                0x01    /* CMD01: response R1 */
98
#define CMD_SEND_IF_COND                0x08    /* CMD08: response R7 */
99
#define CMD_SEND_CSD                    0x09    /* CMD09: response R1 */
100
#define CMD_SEND_CID                    0x0A    /* CMD10: response R1 */
101
#define CMD_SEND_STATUS                 0x0D    /* CMD13: response R2 */
102
#define CMD_SET_BLOCKLEN                0x10    /* CMD16: arg0[31:0]: block length, response R1*/
103
#define CMD_READ_SINGLE_BLOCK   0x11    /* CMD17: arg0[31:0]: data address, response R1 */
104
#define CMD_WRITE_SINGLE_BLOCK  0x18    /* CMD24: arg0[31:0]: data address, response R1 */
105
#define CMD_APP_CMD                             0x37    /* CMD55: response R1 */
106
#define CMD_READ_OCR                    0x3A    /* CMD58: response R3 */
107
#define CMD_CRC_ON_OFF                  0x3B    /* CMD59: arg0[31:1]: stuff bits, arg0[0:0]: crc option, response R1 */                                                                         
108
#define ACMD_SEND_OP_COND               0x29    /* ACMD41: arg0[31]: stuff bits, arg0[30]: HCS, arg0[29:0] stuff bits*, response R1 */
1 ingob 109
 
24 StephanB 110
#define R1_NO_ERR                               0x00
111
#define R1_IDLE_STATE                   0x01
112
#define R1_ERASE_RESET                  0x02
113
#define R1_ILLEGAL_CMD                  0x04
114
#define R1_COM_CRC_ERR                  0x08
115
#define R1_ERASE_SEQUENCE_ERR   0x10
116
#define R1_ADDRESS_ERR                  0x20
117
#define R1_PARAMETER_ERR                0x40
118
#define R1_BAD_RESPONSE                 0x80
119
 
120
#define R2_NO_ERR                               0x00
121
#define R2_CARD_LOCKED                  0x01
122
#define R2_ERASE_WRITE_PROT_ERR 0x02
123
#define R2_UNKOWN_ERR                   0x04
124
#define R2_CARD_CTRL_ERR                0x08
125
#define R2_CARD_ECC_ERR             0x10
126
#define R2_WRITE_PROT_ERR               0x20
127
#define R2_ERASE_PARAM_ERR              0x40
128
#define R2_OUT_OF_RANGE_ERR             0x80
129
 
130
#define DATA_START_TOKEN                0xFE
131
#define DATA_RESPONSE_MASK              0x1F 
132
#define DATA_RESPONSE_OK                0x05
133
#define DATA_RESPONSE_CRC_ERR   0x0B
134
#define DATA_RESPONSE_WRITE_ERR 0x1D
135
 
136
typedef enum
137
{
138
        VER_UNKNOWN,
139
        VER_1X,
140
        VER_20
141
} SDVersion_t;
142
 
143
typedef struct
144
{
145
  u8 Valid;
146
  SDVersion_t Version;  // HW-Version 
147
  u32 Capacity;                 // Memory capacity  in bytes
148
  u8 CID[16];                   // CID register
149
  u8 CSD[16];                   // CSD register
150
} __attribute__((packed)) SDCardInfo_t;
151
 
152
volatile SDCardInfo_t SDCardInfo;
153
 
154
 
1 ingob 155
//________________________________________________________________________________________________________________________________________
24 StephanB 156
// Function:    CRC7(u8* cmd, u32 len);
1 ingob 157
// 
24 StephanB 158
// Description: This function calculated the CRC7 checksum used in the last byte of a spi command frame. 
1 ingob 159
//                              
160
//
24 StephanB 161
// Returnvalue: the function returns the crc7 including bit 0 set to 1
1 ingob 162
//________________________________________________________________________________________________________________________________________
163
 
24 StephanB 164
u8 CRC7(u8 * cmd, u32 len)
1 ingob 165
{
24 StephanB 166
        u8 i, a;
167
        u8 crc, Data;
1 ingob 168
 
24 StephanB 169
        crc = 0; // init CRC buffer
170
        for (a = 0; a < len ;a++) // for every byte in the msg
171
        {
172
                Data = cmd[a];
173
                for (i=0;i<8;i++) // for every bit in the byte
174
                {
175
                        crc <<= 1; // shift crc
176
                        if ((Data & 0x80)^(crc & 0x80)) crc ^=0x09;       //xor
177
                        Data <<= 1;     // shift data  for next bit
178
                }
179
        }
180
        crc = (crc<<1)|1; // set terminating bit to 1
181
        return(crc);
182
}
1 ingob 183
 
41 ingob 184
u8 SDC_WaitForBusy(u32 timeout)
24 StephanB 185
{
186
        u8 rsp = 0;
41 ingob 187
        u32 timestamp = 0;
1 ingob 188
 
24 StephanB 189
        SSC_ClearRxFifo();
190
        SSC_Enable();                           // enable chipselect.
41 ingob 191
        timestamp = SetDelay(timeout);
24 StephanB 192
        do     
1 ingob 193
        {
24 StephanB 194
                rsp = SSC_GetChar();
41 ingob 195
                if(CheckDelay(timestamp)) break;
24 StephanB 196
        }while(rsp != 0xFF);            // wait while card is busy (data out low)
197
        return(rsp);
198
}
199
 
200
 
201
 
202
//________________________________________________________________________________________________________________________________________
203
// Function:    SDC_SendCMDR1(u8 CmdNo, u32 arg);
204
// 
205
// Description: This function send a command frame to the SD-Card in spi-mode. 
206
//                              
207
//
208
// Returnvalue: The function returns the first response byte like for R1 commands
209
//________________________________________________________________________________________________________________________________________
210
u8 SDC_SendCMDR1(u8 CmdNo, u32 arg)
211
{
212
        u8 r1;
41 ingob 213
        u16 timeout = 0;
24 StephanB 214
        u16 a;
215
        u8 cmd[6];
216
 
217
        SSC_ClearRxFifo();      // clear the rx fifo
218
        SSC_Enable();           // enable chipselect.
41 ingob 219
        SDC_WaitForBusy(500);   // wait 500ms until card is busy
24 StephanB 220
        SSC_ClearRxFifo();      // clear the rx fifo
221
        SSC_GetChar();      // dummy to sync
222
 
223
        cmd[0] = 0x40|CmdNo; // set command index
224
        cmd[1] = (arg & 0xFF000000)>>24;
225
        cmd[2] = (arg & 0x00FF0000)>>16;
226
        cmd[3] = (arg & 0x0000FF00)>>8;
227
        cmd[4] = (arg & 0x000000FF);
228
        cmd[5] = CRC7(cmd, 5); // update checksum 
229
        for (a = 0;a < 6; a++) // send the command sequence to the sdcard (6 bytes)
230
        {      
231
                SSC_PutChar(cmd[a]);
1 ingob 232
        }
24 StephanB 233
        SSC_ClearRxFifo();      // clear the rx fifo to discard the bytes received during the transmission of the 6 command bytes
41 ingob 234
 
24 StephanB 235
        do                                     
1 ingob 236
        {
24 StephanB 237
                r1 = SSC_GetChar();       // get byte from sd-card
41 ingob 238
                if (timeout++ >500) break;
24 StephanB 239
        }while(r1 == 0xFF); // wait for the response byte from sd-card.
240
        return(r1);
1 ingob 241
}
242
 
243
 
24 StephanB 244
//________________________________________________________________________________________________________________________________________
245
// Function:    SDC_SendACMDR1(u8 CmdNo, u32 arg);
41 ingob 246
//                                         
24 StephanB 247
// Description: This function send a application command frame to the SD-Card in spi-mode. 
248
//                              
249
//
250
// Returnvalue: The function returns the first response byte like for R1 commands
251
//________________________________________________________________________________________________________________________________________
252
u8 SDC_SendACMDR1(u8 CmdNo, u32 arg)
253
{
254
        u8 r1 = 0xFF;
255
        r1 = SDC_SendCMDR1(CMD_APP_CMD, 0UL);
256
        if(r1 & R1_BAD_RESPONSE) return(r1);
257
        r1 = SDC_SendCMDR1(CmdNo, arg);
258
        return(r1);
259
}
1 ingob 260
 
261
 
262
//________________________________________________________________________________________________________________________________________
24 StephanB 263
// Function:    SDC_GetData(u8 * cmd ,u8 *Buffer, u32 len);
1 ingob 264
// 
24 StephanB 265
// Description: This function sneds cmd an reads a datablock of len from the sd-card
1 ingob 266
//                              
267
//
24 StephanB 268
// Returnvalue: SD_Result_t
1 ingob 269
//________________________________________________________________________________________________________________________________________
270
 
24 StephanB 271
SD_Result_t SDC_GetData(u8 CmdNo, u32 addr, u8 *Buffer, u32 len)
1 ingob 272
{
24 StephanB 273
        u8 rsp;        
41 ingob 274
        u16 a, crc16;
24 StephanB 275
        SD_Result_t result = SD_ERROR_UNKNOWN;
1 ingob 276
 
24 StephanB 277
        // send the command     
278
        rsp = SDC_SendCMDR1(CmdNo, addr);
279
        if (rsp != R1_NO_ERR)
1 ingob 280
        {
24 StephanB 281
                result = SD_ERROR_BAD_RESPONSE;
282
                goto end;
1 ingob 283
        }
284
        SSC_ClearRxFifo();
24 StephanB 285
        do
1 ingob 286
        {
24 StephanB 287
                rsp = SSC_GetChar();
288
                if((rsp & 0xF0) == 0x00) // data error token 
1 ingob 289
                {
24 StephanB 290
                        result = SD_ERROR_READ_DATA;
291
                        goto end;
1 ingob 292
                }
24 StephanB 293
        }while(rsp != DATA_START_TOKEN);
294
        // data start token received
295
        for (a = 0; a < len; a++)       // read the block from the SSC
296
        {
297
                Buffer[a] = SSC_GetChar();
1 ingob 298
        }
24 StephanB 299
        // Read two bytes CRC16-Data checksum
41 ingob 300
        crc16 = SSC_GetChar(); // highbyte fisrt        
301
        crc16 = (crc16<<8)|SSC_GetChar(); // lowbyte last
302
/*      if(crc16 != CRC16(Buffer, len)) result = SD_ERROR_CRC_DATA;
303
        else */result = SD_SUCCESS;
24 StephanB 304
 
305
        end:
306
        if(result != SD_SUCCESS)
307
        {
308
                sprintf(text,"Error %02X reading data from sd card (R1=%02X).\r\n", result, rsp);
309
                SerialPutString(text);
310
        }
311
        return(result);
312
}
1 ingob 313
 
24 StephanB 314
 
315
//________________________________________________________________________________________________________________________________________
316
// Function:    SDC_PrintCID(u8 * pCID);
317
// 
318
// Description: This function prints the CIS register in a human readable format. 
319
//                              
320
//
321
// Returnvalue: the function returns nothing
322
//________________________________________________________________________________________________________________________________________
323
 
324
void SDC_PrintCID(u8 * pCID)
325
{
326
        u8 text[50];
327
        u8 pn[6];
328
        u16 temp1, temp2;
329
 
41 ingob 330
        sprintf(text, "\r\n Manufacturer ID: %i\r\n", pCID[0]);
24 StephanB 331
        SerialPutString(text);
332
        memcpy(pn, &pCID[1], 2);
333
        pn[2] = '\0'; // terminate string
41 ingob 334
        sprintf(text, " Application ID: %s\r\n",pn);
24 StephanB 335
        SerialPutString(text);
336
        memcpy(pn, &pCID[3], 5);
337
        pn[5] = '\0'; // terminate string
41 ingob 338
        sprintf(text, " Product Name: %s\r\n",pn);
24 StephanB 339
        SerialPutString(text);
41 ingob 340
        sprintf(text, " Product Rev.: %i.%i\r\n",pCID[8]>>4, pCID[8]&0xF);
24 StephanB 341
        SerialPutString(text);
41 ingob 342
        SerialPutString(" Serial No.: ");
24 StephanB 343
        for(temp1 = 0; temp1<4; temp1++)
344
        {
345
                sprintf(text,"%02X", pCID[9+temp1]);
346
                SerialPutString(text);
347
        }
348
        SerialPutString("\r\n");
349
        temp1 = pCID[14] & 0x0F;    // month 
350
        temp2 = ((pCID[14]>>4)|(pCID[13]<<4)) + 2000; // year 
41 ingob 351
        sprintf(text, " Manufac. Date: %i/%i\r\n\r\n",temp1, temp2);
24 StephanB 352
        SerialPutString(text);
1 ingob 353
}
354
 
24 StephanB 355
//________________________________________________________________________________________________________________________________________
356
// Funtion:     SDC_GetCID(u8 * pCID);
357
// 
358
// Description: This function reads the CIS register form the sd card in spi mode. 
359
//                              
360
//
361
// Returnvalue: the function returns error state
362
//________________________________________________________________________________________________________________________________________
1 ingob 363
 
24 StephanB 364
SD_Result_t SDC_GetCID(u8 * pCID)
365
{
366
        return SDC_GetData(CMD_SEND_CID, 0UL, pCID, 16);
367
}
1 ingob 368
 
24 StephanB 369
//________________________________________________________________________________________________________________________________________
370
// Funtion:     SDC_GetCSD(u8 * pCSD);
371
// 
372
// Description: This function reads the CSD register form the sd card in spi mode. 
373
//                              
374
//
375
// Returnvalue: the function returns error state
376
//________________________________________________________________________________________________________________________________________
1 ingob 377
 
24 StephanB 378
SD_Result_t SDC_GetCSD(u8 * pCSD)
379
{
380
        return SDC_GetData(CMD_SEND_CSD, 0UL, pCSD, 16);
381
}
382
 
383
 
1 ingob 384
//________________________________________________________________________________________________________________________________________
24 StephanB 385
// Funtion:     SDC_Init(void);
1 ingob 386
// 
24 StephanB 387
// Description: This function initialises the SDCard to spi-mode. 
1 ingob 388
//                              
389
//
24 StephanB 390
// Returnvalue: the function returns 0 if the initialisation was successfull otherwise the function returns an errorcode.
1 ingob 391
//________________________________________________________________________________________________________________________________________
392
 
24 StephanB 393
SD_Result_t SDC_Init(void)
1 ingob 394
{
41 ingob 395
        u32 timeout = 0;
24 StephanB 396
        u8 text[50];
397
        u8 rsp[6]; // SD-SPI response buffer
398
        SD_Result_t result = SD_ERROR_UNKNOWN;
399
 
400
        if(SD_SWITCH) // init only if the SD-Switch is indicating a card in the slot
401
        {
41 ingob 402
                SerialPutString("\r\n SSC init...");
24 StephanB 403
                SSC_Init();
404
                SerialPutString("ok");
41 ingob 405
 
406
                SerialPutString("\r\n SDC init...");
24 StephanB 407
                SDCardInfo.Valid = 0;
408
                /* The host shall supply power to the card so that the voltage is reached to Vdd_min within 250ms and
409
                start to supply at least 74 SD clocks to the SD card with keeping cmd line to high. In case of SPI
410
                mode, CS shall be held to high during 74 clock cycles. */
411
                SSC_Disable(); // set SD_CS high
412
                SSC_ClearRxFifo();      // clear the rx fifo
1 ingob 413
 
41 ingob 414
                for (timeout = 0; timeout < 15; timeout++)      // 15*8 = 120 cycles
24 StephanB 415
                {
416
                        SSC_PutChar(0xFF);
417
                }
418
 
419
                // switch to idle state
420
                while(SDC_SendCMDR1(CMD_GO_IDLE_STATE, 0UL) != R1_IDLE_STATE)                                          
421
                {
41 ingob 422
                        if (timeout++ > 20)
24 StephanB 423
                        {
424
                                SerialPutString("reset timeout");
425
                                result = SD_ERROR_RESET;
426
                                goto end;                                                                              
427
                        }
428
                }
429
            // enable crc feature
430
/*              if(SDC_SendCMDR1(CMD_CRC_ON_OFF, 1UL) != R1_IDLE_STATE)
431
                {
41 ingob 432
                                sprintf(text,"Bad cmd59 R1=%02X.", rsp[0]);
24 StephanB 433
                                SerialPutString(text);
434
                                result = SD_ERROR_BAD_RESPONSE;
435
                                goto end;
436
                }*/
437
                // check for card hw version                                            
438
                // 2.7-3.6V Range = 0x01, check pattern 0xAA
439
                rsp[0] = SDC_SendCMDR1(CMD_SEND_IF_COND, 0x000001AA);
440
                // answer to cmd58 is an R7 response (R1+ 4Byte IFCond)
441
                if(rsp[0] & R1_BAD_RESPONSE)
442
                {
41 ingob 443
                        sprintf(text,"Bad cmd8 R1=%02X.", rsp[0]);
24 StephanB 444
                        SerialPutString(text);
445
                        result = SD_ERROR_BAD_RESPONSE;
446
                        goto end;
447
                }
448
                if(rsp[0] & R1_ILLEGAL_CMD)
449
                {
450
                        //Ver1.X SD Memory Card or not a SD Memory Card
451
                        SDCardInfo.Version = VER_1X;                   
452
                }
453
                else
454
                {
455
                   // Ver2.00 or later SD Memory Card
456
                   // reading the remaining bytes of the R7 response
457
                   SDCardInfo.Version = VER_20;
41 ingob 458
                   for(timeout = 1; timeout < 5; timeout++)
24 StephanB 459
                   {
41 ingob 460
                                rsp[timeout] = SSC_GetChar();
24 StephanB 461
                   }
462
                   //check pattern
463
                   if(rsp[4]!= 0xAA)
464
                   {
465
                                SerialPutString("Bad cmd8 R7 check pattern.\r\n");
466
                                result = SD_ERROR_BAD_RESPONSE;
467
                                goto end;
468
                   }
469
                   if ( (rsp[3] & 0x0F)!= 0x01 ) // voltage range is not 2.7-3.6V
470
                   {
471
 
472
                                SerialPutString("Card is incompatible to 3.3V.\r\n");
473
                                result = SD_ERROR_BAD_VOLTAGE_RANGE;
474
                                goto end;              
475
                   }
476
                }
477
 
478
                rsp[0] = SDC_SendCMDR1(CMD_READ_OCR, 0UL);
479
                // answer to cmd58 is an R3 response (R1 + 4Byte OCR)
480
                if(rsp[0] & R1_BAD_RESPONSE)
481
                {
41 ingob 482
                        sprintf(text,"Bad cmd58 R1 %02x.", rsp[0]);
24 StephanB 483
                        SerialPutString(text);
484
                        result = SD_ERROR_BAD_RESPONSE;
485
                        goto end;
486
                }
487
                if(rsp[0] & R1_ILLEGAL_CMD)
488
                {
41 ingob 489
                        SerialPutString("Not an SD-CARD.");
24 StephanB 490
                        result = SD_ERROR_NO_SDCARD;
491
                        goto end;
492
                }
493
                // read 4 bytes of OCR register
41 ingob 494
                for(timeout = 1; timeout < 5; timeout++)
24 StephanB 495
                {
41 ingob 496
                        rsp[timeout] = SSC_GetChar();
24 StephanB 497
                }
498
                //      NavicCtrl uses 3.3 V,  therefore check for bit 20 & 21 
499
                if((rsp[2] & 0x30) != 0x30)
500
                {
501
                        // supply voltage is not supported by sd-card
41 ingob 502
                        SerialPutString("Card is incompatible to 3.3V.");
24 StephanB 503
                        result = SD_ERROR_BAD_VOLTAGE_RANGE;
504
                        goto end;
505
                }
506
 
507
                // Initialize the sd-card sending continously ACMD_SEND_OP_COND (only supported by SD cards)
41 ingob 508
                timeout =  SetDelay(2000); // set timeout to 2000 ms (large cards tend to longer) 
24 StephanB 509
                do
510
                {
511
                        rsp[0] = SDC_SendACMDR1(ACMD_SEND_OP_COND, 0UL);
512
                        if(rsp[0] & R1_BAD_RESPONSE)
513
                        {
41 ingob 514
                                sprintf(text,"Bad Acmd41 R1=%02X.", rsp[0]);
24 StephanB 515
                                SerialPutString(text);
516
                                result = SD_ERROR_BAD_RESPONSE;
517
                                goto end;
518
                        }
41 ingob 519
                        if(CheckDelay(timeout))
24 StephanB 520
                        {
41 ingob 521
                            SerialPutString("Init timeout.");
24 StephanB 522
                                result = SD_ERROR_INITIALIZE;
523
                                goto end;
524
                        }
525
                } while(rsp[0] & R1_IDLE_STATE); // loop until idle state
526
 
527
                if(rsp[0] != R1_NO_ERR)
528
                {
41 ingob 529
                        SerialPutString("Init error.");
24 StephanB 530
                        result = SD_ERROR_INITIALIZE;
531
                        goto end;      
532
                }
533
                /* set block size to 512 bytes */
534
        if(SDC_SendCMDR1(CMD_SET_BLOCKLEN, 512UL) != R1_NO_ERR)
535
        {
41 ingob 536
                SerialPutString("Error setting block length to 512.");
24 StephanB 537
                        result = SD_ERROR_SET_BLOCKLEN;
538
                        goto end;
539
        }
41 ingob 540
 
541
                //SSC_Disable(); // set SD_CS high                                                                       
24 StephanB 542
                // here is the right place to inrease the SPI boud rate to maximum              
543
                //SSC_Enable(); // set SD_CS high
544
 
545
                // read CID register
546
                result = SDC_GetCID((u8 *)&SDCardInfo.CID);
547
                if(result != SD_SUCCESS)
548
                {
549
                        SerialPutString("Error reading CID.\r\n");
550
                        goto end;
551
                }
1 ingob 552
 
24 StephanB 553
                // read CSD register
554
                result = SDC_GetCSD((u8 *)&SDCardInfo.CSD);
555
                if(result != SD_SUCCESS)
556
                {
41 ingob 557
                        SerialPutString("Error reading CSD.");
24 StephanB 558
                        goto end;
559
                }
1 ingob 560
 
24 StephanB 561
                SerialPutString("ok\r\n");
562
 
563
                u8 c_size_mult, read_bl_len;
564
                u32 c_size;
565
 
566
                switch(SDCardInfo.CSD[0]>>6) // check CSD Version
567
                {
568
                case 0x00: // if CSD is V1.0 structure (2GB limit)
569
 
570
                        /*
571
                        memory capacity = BLOCKNR * BLOCK_LEN
572
                        BLOCKNR = (C_SIZE+1) * MULT
573
                        MULT = 2^(C_SIZE_MULT+2)
574
                        BLOCK_LEN = 2^READ_BL_LEN
575
 
576
                        C_SIZE      is 12 bits [73:62] in CSD register
577
                        C_SIZE_MULT is  3 bits [49:47] in CSD register
578
                        READ_BL_LEN is  4 bits [83:80] in CSD register
579
                        */
580
 
581
                        read_bl_len = (SDCardInfo.CSD[5] & 0x0F);               //CSD[05] -> [87:80]
582
                        c_size = ((u32)(SDCardInfo.CSD[6] & 0x03))<<10; //CSD[06] -> [79:72]
583
                        c_size |= ((u32)SDCardInfo.CSD[7])<<2;                  //CSD[07] -> [71:64]
584
                        c_size |= (u32)(SDCardInfo.CSD[8]>>6);                  //CSD[08] -> [63:56]
585
                        c_size_mult = (SDCardInfo.CSD[9] & 0x03)<<1;    //CSD[09] -> [55:48]
586
                        c_size_mult |=(SDCardInfo.CSD[10] & 0x80)>>7;   //CSD[10] -> [47:40]
587
                        SDCardInfo.Capacity = (u32)(c_size+1)*(1L<<(c_size_mult+2))*(1L<<read_bl_len);
588
                        break;
589
 
590
                case 0x01: // if CSD is V2.0 structure (HC SD-Card > 2GB) 
591
 
592
                        /*
593
                        memory capacity = (C_SIZE+1) * 512K byte
594
                        C_SIZE is 22 bits [69:48] in CSR register
595
                        */
596
 
597
                        c_size = ((u32)(SDCardInfo.CSD[7] & 0x3F))<<16; //CSD[07] -> [71:64]
598
                        c_size |= ((u32)SDCardInfo.CSD[8])<<8;                  //CSD[08] -> [63:56]
599
                        c_size |= (u32)SDCardInfo.CSD[9];                               //CSD[09] -> [55:48];
600
                        SDCardInfo.Capacity = (c_size + 1)* 512L * 1024L;
41 ingob 601
                        break;
602
 
603
                default: //unknown CSD Version
24 StephanB 604
                        SDCardInfo.Capacity = 0;
605
                        break; 
606
                }
607
 
608
                switch(SDCardInfo.Version)
609
                {
610
                        case VER_1X:
611
                                SerialPutString("  SD-CARD V1.x");
612
                                break;
613
                        case VER_20:
614
                                SerialPutString("  SD-CARD V2.0 or later");
615
                        default:
616
                                break;
617
                }
618
                u16 mb_size = (u16)(SDCardInfo.Capacity/(1024L*1024L));
619
                sprintf(text, "\r\n  Capacity = %i MB", mb_size);
620
                SerialPutString(text);
621
 
41 ingob 622
                SDC_PrintCID((u8 *)&SDCardInfo.CID);
24 StephanB 623
                SDCardInfo.Valid = 1;
624
                // jump point for error condition before
625
                end:
626
                SSC_Disable();
627
        }
628
        else
1 ingob 629
        {
24 StephanB 630
                SSC_Deinit();
631
                SDCardInfo.Valid = 0;
632
                result = SD_ERROR_NOCARD;
41 ingob 633
                SerialPutString("No Card in Slot.");
1 ingob 634
        }
24 StephanB 635
        return(result);
1 ingob 636
}
637
 
638
 
639
//________________________________________________________________________________________________________________________________________
24 StephanB 640
// Funtion:     SDC_Deinit(void);
1 ingob 641
// 
24 StephanB 642
// Description: This function deinitialises the SDCard interface. 
1 ingob 643
//                              
644
//
24 StephanB 645
// Returnvalue: the function returns 0 if the initialisation was successfull otherwise the function returns an errorcode.
1 ingob 646
//________________________________________________________________________________________________________________________________________
647
 
24 StephanB 648
SD_Result_t SDC_Deinit(void)
649
{
41 ingob 650
        SerialPutString("\r\n SDC deinit...");
24 StephanB 651
        SSC_Deinit();
1 ingob 652
 
24 StephanB 653
        SDCardInfo.Valid = 0;
654
        SDCardInfo.Capacity = 0;
655
        SDCardInfo.Version = VER_UNKNOWN;
1 ingob 656
 
41 ingob 657
        SerialPutString("ok");
24 StephanB 658
        return(SD_SUCCESS);
1 ingob 659
}
660
 
661
 
662
//________________________________________________________________________________________________________________________________________
24 StephanB 663
// Funtion:     SDC_PutSector(void);
1 ingob 664
// 
24 StephanB 665
// Description: This function writes one sector of data to the SSC 
1 ingob 666
//                              
667
//
24 StephanB 668
// Returnvalue: SD_Result_t
1 ingob 669
//________________________________________________________________________________________________________________________________________
670
 
24 StephanB 671
SD_Result_t SDC_PutSector(u32 addr, const u8 *Buffer)
672
{
673
        u8 rsp;
41 ingob 674
        u16 a, crc16;
24 StephanB 675
        u16 timeout = 0;
676
        SD_Result_t result = SD_ERROR_UNKNOWN;
677
 
678
        addr = addr << 9; // convert sectoradress to byteadress
679
        rsp = SDC_SendCMDR1(CMD_WRITE_SINGLE_BLOCK, addr);
680
        if (rsp != R1_NO_ERR)
1 ingob 681
        {
24 StephanB 682
                result = SD_ERROR_BAD_RESPONSE;
683
                goto end;
1 ingob 684
        }
685
        SSC_ClearRxFifo();             
24 StephanB 686
        for (a=0;a<20;a++)                                      // at least one byte
1 ingob 687
        {
24 StephanB 688
                SSC_GetChar();
1 ingob 689
        }
41 ingob 690
        crc16 = CRC16(Buffer, 512);         // calc checksum for data block
24 StephanB 691
        SSC_PutChar(DATA_START_TOKEN);          // send data start of header to the SSC 
1 ingob 692
 
24 StephanB 693
        for (a=0;a<512;a++)                                     // transmit one sector (normaly 512bytes) of data to the sdcard.
694
        {
695
                SSC_PutChar(Buffer[a]);
696
        }
697
        // write two bytes of crc16 to the sdcard
41 ingob 698
        SSC_PutChar((u8)(crc16>>8));            // write high byte first
699
        SSC_PutChar((u8)(0x00FF&crc16));        // lowbyte last
24 StephanB 700
        SSC_ClearRxFifo();
701
        do                                                                      // wait for data response token
702
        {
703
                rsp = SSC_GetChar();
41 ingob 704
                if(timeout++ > 500)
705
                {
706
                        result = SD_ERROR_TIMEOUT;
707
                        goto end;
708
                }
24 StephanB 709
        }while((rsp & 0x11) != 0x01 );
710
        // analyse data response token
711
        switch(rsp & DATA_RESPONSE_MASK)
712
        {
713
                case DATA_RESPONSE_OK:
714
                        result = SD_SUCCESS;
715
                        break;
716
                case DATA_RESPONSE_CRC_ERR:
717
                        result = SD_ERROR_CRC_DATA;
718
                        goto end;
719
                        break;
720
                case DATA_RESPONSE_WRITE_ERR:
721
                        result = SD_ERROR_WRITE_DATA;
722
                        goto end;
723
                        break;
724
                default:
725
                        result = SD_ERROR_UNKNOWN;
726
                        goto end;
727
                        break;
728
 
729
        }
41 ingob 730
        // wait 2 seconds until the sdcard is busy.
731
        rsp = SDC_WaitForBusy(2000);
24 StephanB 732
        if(rsp != 0xFF)
733
        {
41 ingob 734
                result =  SD_ERROR_TIMEOUT;
735
                goto end;
24 StephanB 736
        }
41 ingob 737
 
24 StephanB 738
        // check card status
739
        rsp = SDC_SendCMDR1(CMD_SEND_STATUS, 0);
41 ingob 740
        // first byte of R2 response is like R1 response
24 StephanB 741
        if(rsp != R1_NO_ERR)
742
        {
743
                result =  SD_ERROR_BAD_RESPONSE;
41 ingob 744
                SSC_GetChar(); // read out 2nd byte
24 StephanB 745
                goto end;
746
        }
747
        // 2nd byte of r2 response
748
        rsp = SSC_GetChar();
749
        if(rsp != R2_NO_ERR)
750
        {
751
                result =  SD_ERROR_WRITE_DATA;
752
                SSC_GetChar();
753
                goto end;
754
        }
755
        end:
756
        if(result != SD_SUCCESS)
757
        {
758
                sprintf(text,"Error %02X writing data to sd card (R=%02X).\r\n", result, rsp);
759
                SerialPutString(text);
760
        }
761
        return(result);
41 ingob 762
}
24 StephanB 763
 
764
 
765
//________________________________________________________________________________________________________________________________________
766
// Funtion:     SDC_GetSector(u32 addr,u8 *Buffer);
767
// 
768
// Description: This function reads one sector of data from the SSC
769
//                              
770
//
771
// Returnvalue: SD_Result_t
772
//________________________________________________________________________________________________________________________________________
773
 
774
SD_Result_t SDC_GetSector(u32 addr,u8 *Buffer)
775
{
776
        addr = addr << 9; // convert sectoradress to byteadress
777
        return SDC_GetData(CMD_READ_SINGLE_BLOCK, addr, Buffer, 512);
1 ingob 778
}
779
 
780
 
781