Subversion Repositories Projects

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
231 killagreg 1
#include <avr/io.h>
2
#include <util/delay.h>
3
#include "fat16.h"
4
#include "sdc.h"
5
#include "ssc.h"
6
 
7
//________________________________________________________________________________________________________________________________________
8
// Module name:			mmc.c
9
// Compiler used:		avr-gcc 3.4.5
10
// Last Modifikation:	24.07.2007
11
// Version:				1.05
12
// Authors:				Stephan Busker
13
// Description:			Source files for connecting to an sdcard using the SSC
14
//
15
//........................................................................................................................................
16
// Functions:			u8			SDC_init(void);
17
//						u8 SDC_PutCommand (u8*CMD);
18
//						u8 SDC_PutSector(u32 addr,u8*Buffer);
19
//						u8 SDC_GetSector(u32 addr,u8*Buffer);
20
//						void SDC_GetBlock(u8*CMD,u8*Buffer,u16 Bytes);
21
//
22
////........................................................................................................................................
23
// ext. functions:		extern void SSC_Init(void);
24
//						extern u8 SSC_GetChar (void);
25
//						extern void SSC_PutChar (u8);
26
//						extern void SSC_Enable(void);
27
//						extern void SSC_Disable(void);
28
//........................................................................................................................................
29
//
30
// URL:					www.Mikro-Control.de
31
// mailto:				stephan.busker@mikro-control.de
32
//________________________________________________________________________________________________________________________________________
33
 
34
 
35
 
36
 
37
//________________________________________________________________________________________________________________________________________
38
// Funtion: 	SDC_Init(void);
39
//
40
// Description:	This function initialises the SDCard to spi-mode.
41
//
42
//
43
// Returnvalue: the function returns 0 if the initialisation was successfull otherwise the function returns an errorcode.
44
//________________________________________________________________________________________________________________________________________
45
 
46
u8 SDC_Init(void)
47
{
48
	u8 Timeout = 0;
49
	u8 CMD[] = {0x40,0x00,0x00,0x00,0x00,0x95};
50
	u16 a = 0;
51
	u8 	b = 0;
52
 
53
	SSC_Init();														// Initialise SSC to transmit data to the sdcard.
54
 
55
 
56
	_delay_ms(10);													// before initialising the sdcard wait for 10ms
57
 
58
 
59
	for (b=0;b<0x0f;b++) 											// sending 74Clocks brings the sdcard into spimode.
60
	{
61
		_delay_us(1);												// wait at least 1us between the characters.
62
		SSC_PutChar(0xff);
63
	}
64
 
65
 
66
	while(SDC_PutCommand (CMD) !=1)								// Sending CMD0 (Reset) to the sdcard.
67
	{
68
		if (Timeout++ > 200)
69
		{
70
			return(1);
71
		}
72
	}
73
 
74
	Timeout = 0;
75
	CMD[0] = 0x41;
76
	CMD[5] = 0xFF;
77
	while( SDC_PutCommand (CMD) !=0)								// Sending CMD1 to the sdcard.
78
	{
79
		if (Timeout++ > 100)
80
		{
81
			return(2);
82
		}
83
	}
84
 
85
	SSC_Disable();													// disable sdcard.
86
	return(0);
87
}
88
 
89
 
90
 
91
 
92
//________________________________________________________________________________________________________________________________________
93
// Funtion: 	SDC_PutCommand(* CMD);
94
//
95
// Description:	This function initialises the SDCard to spi-mode.
96
//
97
//
98
// Returnvalue: the function returns 0 if the initialisation was successfull otherwise the function returns an errorcode.
99
//________________________________________________________________________________________________________________________________________
100
 
101
u8 SDC_PutCommand (u8*CMD)
102
{
103
	u8 tmp = 0xff;
104
	u8 Timeout = 0;
105
	u16 a = 0;
106
 
107
 
108
	#ifdef SSC_RX_FIFO
109
	SSC_ClearRxFifo();
110
	#endif
111
	SSC_Disable();														// disable chipselect
112
	SSC_PutChar(0xFF);													// Send 8 Clocks to the sdcard while card is not selected.
113
	SSC_Enable();														// enable chipselect.
114
 
115
    if (*CMD == 0x41) _delay_ms(10);									// if command is CMD0 generate a short delay.
116
 
117
	for (a = 0;a<0x06;a++) 											// send the command sequence to the sdcard (6 bytes)
118
	{
119
		SSC_PutChar(*CMD++);
120
	}
121
 
122
	#ifdef SSC_RX_FIFO
123
	SSC_ClearRxFifo();
124
	#endif
125
	while (tmp == 0xff)													// Wait for response from sdcard.
126
	{
127
		tmp = SSC_GetChar();
128
		if (Timeout++ > 100)
129
		{
130
			break; 														// or timeout.
131
		}
132
	}
133
 
134
	return(tmp);
135
}
136
 
137
 
138
 
139
 
140
//________________________________________________________________________________________________________________________________________
141
// Funtion: 	SDC_PutSector(void);
142
//
143
// Description:	This function writes one sector of data to the SSC
144
//
145
//
146
// Returnvalue: none
147
//________________________________________________________________________________________________________________________________________
148
 
149
u8 SDC_PutSector(u32 addr,u8*Buffer)
150
{
151
	u8 tmp;
152
	u8 CMD[] = {0x58,0x00,0x00,0x00,0x00,0xFF};
153
 
154
	addr = addr << 9; 													// convert sectoradress to byteadress
155
 
156
	CMD[1] = ((addr & 0xFF000000) >>24 );
157
	CMD[2] = ((addr & 0x00FF0000) >>16 );
158
	CMD[3] = ((addr & 0x0000FF00) >>8 );
159
 
160
	tmp = SDC_PutCommand (CMD);										// send command to sdcard.
161
	if (tmp != 0)
162
	{
163
		return(tmp);
164
	}
165
 
166
	#ifdef SSC_RX_FIFO
167
	SSC_ClearRxFifo();
168
	#endif
169
	for (u8 a=0;a<100;a++)								// wait until sdcard is ready
170
	{
171
		SSC_GetChar();
172
	}
173
 
174
	SSC_PutChar(0xFE);													// send start of header to the SSC
175
 
176
	for (u16 a=0;a<512;a++)								// transmitt one sector (normaly 512bytes) of data to the sdcard.
177
	{
178
		SSC_PutChar(*Buffer++);
179
	}
180
 
181
	SSC_PutChar(0xFF); 													// write two bytes of crc to the sdcard (not used in spi-mode)
182
	SSC_PutChar(0xFF);
183
	#ifdef SSC_RX_FIFO
184
	SSC_ClearRxFifo();
185
	#endif
186
 
187
	while (SSC_GetChar() != 0xff){};									// wait untile the sdcard is ready.
188
 
189
	SSC_Disable();														// disable sdcard.
190
 
191
	return(0);
192
}
193
 
194
 
195
 
196
//________________________________________________________________________________________________________________________________________
197
// Funtion: 	SDC_GetSector(u32 addr,u8*Buffer);
198
//
199
// Description:	This function reads one sector of data from the SSC
200
//
201
//
202
// Returnvalue: none
203
//________________________________________________________________________________________________________________________________________
204
 
205
u8 SDC_GetSector(u32 addr,u8*Buffer)
206
{
207
	u8 CMD[] = {0x51,0x00,0x00,0x00,0x00,0xFF};
208
 
209
	addr = addr << 9; 													// convert sectoradress to byteadress.
210
 
211
	CMD[1] = ((addr & 0xFF000000) >>24 );
212
	CMD[2] = ((addr & 0x00FF0000) >>16 );
213
	CMD[3] = ((addr & 0x0000FF00) >>8 );
214
 
215
	SDC_GetBlock(CMD,Buffer,512);									// read specified sector from sdcard.
216
 
217
	return(0);
218
}
219
 
220
 
221
 
222
//________________________________________________________________________________________________________________________________________
223
// Funtion: 	SDC_GetBlock(void);
224
//
225
// Description:	This function reads one block of data of s16 bytes from the SSC.
226
//
227
//
228
// Returnvalue: the function returns 0 if the initialisation was successfull otherwise the function returns an errorcode.
229
//________________________________________________________________________________________________________________________________________
230
 
231
void SDC_GetBlock(u8*CMD,u8*Buffer,u16 Bytes)
232
{
233
	if (SDC_PutCommand (CMD) != 0)										// Send command to the sdcard.
234
	{
235
		return;
236
	}
237
 
238
	#ifdef SSC_RX_FIFO
239
	SSC_ClearRxFifo();
240
	#endif
241
	while (SSC_GetChar() != 0xfe){};										// wait until the sdcard is ready to transmitt data.
242
 
243
	for (u16 a=0;a<Bytes;a++)									// read the block from the SSC (normaly 512Bytes)
244
	{
245
		*Buffer++ = SSC_GetChar();
246
	}
247
 
248
	SSC_GetChar();															// Read two bytes CRC- checksum (not used in spi-mode)
249
	SSC_GetChar();
250
	SSC_Disable();															// disable sdcard.
251
}
252
 
253
 
254