Subversion Repositories NaviCtrl

Rev

Rev 23 | Go to most recent revision | 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
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
57
#include "main.h"
58
 
59
 
60
//________________________________________________________________________________________________________________________________________
61
// Module name:                 mmc.c 
62
// Compiler used:               avr-gcc 3.4.5
63
// Last Modifikation:   24.07.2007
64
// Version:                             1.05
65
// Authors:                             Stephan Busker  
66
// Description:                 Source files for connecting to an sdcard using the SSC
67
//
68
//........................................................................................................................................
69
// Functions:                   u8                      SDC_init(void);
70
//                                              u8 SDC_PutCommand (u8 *CMD);
71
//                                              u8 SDC_PutSector(u32 addr,u8 *Buffer);
72
//                                              u8 SDC_GetSector(u32 addr,u8 *Buffer);
73
//                                              void SDC_GetBlock(u8 *CMD,u8 *Buffer,u16 Bytes);
74
//
75
////........................................................................................................................................
76
// ext. functions:              extern void SSC_Init(void);
77
//                                              extern u8 SSC_GetChar (void);
78
//                                              extern void SSC_PutChar (u8);
79
//                                              extern void SSC_Enable(void);
80
//                                              extern void SSC_Disable(void);
81
//........................................................................................................................................
82
//
83
// URL:                                 www.Mikro-Control.de
84
// mailto:                              stephan.busker@mikro-control.de
85
//________________________________________________________________________________________________________________________________________
86
 
87
 
88
 
89
 
90
//________________________________________________________________________________________________________________________________________
91
// Funtion:     SDC_Init(void);
92
// 
93
// Description: This function initialises the SDCard to spi-mode. 
94
//                              
95
//
96
// Returnvalue: the function returns 0 if the initialisation was successfull otherwise the function returns an errorcode.
97
//________________________________________________________________________________________________________________________________________
98
 
99
u8 SDC_Init(void)
100
{
101
        u16 Timeout = 0;
102
        u8 CMD[] = {0x40,0x00,0x00,0x00,0x00,0x95};
103
 
104
        u8 b;
105
 
106
        SSC_Init();                                                                                                             // Initialise SSC to transmit data to the sdcard.
107
        SerialPutString("Init SD...");
108
//      Delay_ms(10);
109
 
110
        for (b = 0;b<0x0f;b++)                                          // sending 74Clocks brings the sdcard into spimode.
111
        {
112
                SSC_PutChar(0xff);
113
        }
114
 
115
        SerialPutString("reset...");
116
        while(SDC_PutCommand (CMD) !=1)                                                 // Sending CMD0 (Reset) to the sdcard.
117
        {
118
                if (Timeout++ > 200)
119
                        {
120
 
121
                        return(1);                                                                             
122
                        }
123
        }
124
        SerialPutString("ok.");
125
        Timeout = 0;
126
        CMD[0] = 0x41;                                                                                                 
127
        CMD[5] = 0xFF;
128
        while( SDC_PutCommand (CMD) !=0)                                                        // Sending CMD1 to the sdcard.
129
        {
130
                if (Timeout++ > 100)
131
                        {
132
                        return(2);
133
                        }
134
        }
135
 
136
        SSC_Disable();                                                                                                  // disable sdcard.
137
        return(0);
138
}
139
 
140
 
141
 
142
 
143
//________________________________________________________________________________________________________________________________________
144
// Funtion:     SDC_PutCommand(* CMD);
145
// 
146
// Description: This function initialises the SDCard to spi-mode. 
147
//                              
148
//
149
// Returnvalue: the function returns 0 if the initialisation was successfull otherwise the function returns an errorcode.
150
//________________________________________________________________________________________________________________________________________
151
 
152
u8 SDC_PutCommand (u8 *CMD)
153
{
154
        u8 tmp = 0xff;
155
        u16 Timeout = 0;
156
        u16 a;
157
                SSC_ClearRxFifo();
158
        SSC_Disable();                                                                                                          // disable chipselect
159
        SSC_PutChar(0xFF);                                                                                                      // Send 8 Clocks to the sdcard while card is not selected.
160
        SSC_Enable();                                                                                                           // enable chipselect.
161
 
162
  //  if (*CMD == 0x41)  Delay_ms(10);          // if command is CMD0 generate a short delay. 
163
 
164
        for (a = 0;a<0x06;a++)                                                  // send the command sequence to the sdcard (6 bytes)
165
        {
166
 
167
                SSC_PutChar(*CMD++);
168
        }
169
 
170
        SSC_ClearRxFifo();
171
        while (tmp == 0xff)                                                                                             // Wait for response from sdcard.
172
        {
173
                tmp = SSC_GetChar();
174
                if (Timeout++ > 200)
175
                {
176
                        break;                                                                                                  // or timeout.
177
                }
178
        }
179
 
180
        return(tmp);
181
}
182
 
183
 
184
 
185
 
186
//________________________________________________________________________________________________________________________________________
187
// Funtion:     SDC_PutSector(void);
188
// 
189
// Description: This function writes one sector of data to the SSC 
190
//                              
191
//
192
// Returnvalue: none
193
//________________________________________________________________________________________________________________________________________
194
 
195
u8 SDC_PutSector(u32 addr,u8 *Buffer)
196
{
197
        u8 tmp;
198
        u8 CMD[] = {0x58,0x00,0x00,0x00,0x00,0xFF};
199
        u16 a;
200
        addr = addr << 9;                                                                                                       // convert sectoradress to byteadress
201
 
202
        CMD[1] = ((addr & 0xFF000000) >>24 );
203
        CMD[2] = ((addr & 0x00FF0000) >>16 );
204
        CMD[3] = ((addr & 0x0000FF00) >>8 );
205
 
206
        tmp = SDC_PutCommand (CMD);                                                                             // send command to sdcard.
207
        if (tmp != 0)
208
        {
209
                return(tmp);
210
        }
211
        SSC_ClearRxFifo();             
212
        for (a=0;a<100;a++)                                                             // wait until sdcard is ready
213
        {
214
                SSC_GetChar();
215
        }
216
 
217
        SSC_PutChar(0xFE);                                                                                                      // send start of header to the SSC      
218
 
219
        for (a=0;a<512;a++)                                                             // transmitt one sector (normaly 512bytes) of data to the sdcard.
220
        {
221
                SSC_PutChar(*Buffer++);
222
        }
223
 
224
        SSC_PutChar(0xFF);                                                                                                      // write two bytes of crc to the sdcard (not used in spi-mode)
225
        SSC_PutChar(0xFF);
226
        SSC_ClearRxFifo();
227
        while (SSC_GetChar() != 0xff){};                                                                        // wait untile the sdcard is ready.
228
 
229
        SSC_Disable();                                                                                                          // disable sdcard.
230
 
231
        return(0);
232
}
233
 
234
 
235
 
236
//________________________________________________________________________________________________________________________________________
237
// Funtion:     SDC_GetSector(u32 addr,u8 *Buffer);
238
// 
239
// Description: This function reads one sector of data from the SSC
240
//                              
241
//
242
// Returnvalue: none
243
//________________________________________________________________________________________________________________________________________
244
 
245
u8 SDC_GetSector(u32 addr,u8 *Buffer)
246
{      
247
        u8 CMD[] = {0x51,0x00,0x00,0x00,0x00,0xFF};
248
 
249
        addr = addr << 9;                                                                                                       // convert sectoradress to byteadress.
250
 
251
        CMD[1] = ((addr & 0xFF000000) >>24 );
252
        CMD[2] = ((addr & 0x00FF0000) >>16 );
253
        CMD[3] = ((addr & 0x0000FF00) >>8 );
254
 
255
   // sprintf(text,"\n\rGetSector:  (%lu)", (u32)addr );        SerialPutString(text);
256
 
257
        SDC_GetBlock(CMD,Buffer,512);                                                                   // read specified sector from sdcard.
258
 
259
        return(0);
260
}
261
 
262
 
263
 
264
//________________________________________________________________________________________________________________________________________
265
// Funtion:     SDC_GetBlock(void);
266
// 
267
// Description: This function reads one block of data of int bytes from the SSC.
268
//                              
269
//
270
// Returnvalue: the function returns 0 if the initialisation was successfull otherwise the function returns an errorcode.
271
//________________________________________________________________________________________________________________________________________
272
 
273
void SDC_GetBlock(u8 *CMD,u8 *Buffer,u16 Bytes)
274
{      
275
        if (SDC_PutCommand (CMD) != 0)                                                                          // Send command to the sdcard.
276
        {
277
                return;
278
        }
279
        SSC_ClearRxFifo();             
280
//      SerialPutString("\n\rWait.");
281
        while (SSC_GetChar() != 0xfe){};                                                                                // wait until the sdcard is ready to transmitt data.
282
//      SerialPutString("Ok.");
283
        u16 a;
284
        for (a=0;a<Bytes;a++)                                                                   // read the block from the SSC (normaly 512Bytes)
285
        {
286
                *Buffer++ = SSC_GetChar();
287
        }
288
 
289
        SSC_GetChar();                                                                                                                  // Read two bytes CRC- checksum (not used in spi-mode)
290
        SSC_GetChar(); 
291
        SSC_Disable();                                                                                                                  // disable sdcard.
292
}
293
 
294
 
295