📄 sd.lst
字号:
C51 COMPILER V7.50 SD 07/13/2005 10:21:50 PAGE 1
C51 COMPILER V7.50, COMPILATION OF MODULE SD
OBJECT MODULE PLACED IN sd.OBJ
COMPILER INVOKED BY: e:\Keil\C51\BIN\C51.EXE sd.c BROWSE DEBUG OBJECTEXTEND
line level source
1 //#define __SD
2 #include "sd.h"
3 //#undef __SD
4
5 unsigned char SDCommand( unsigned char c1,
6 unsigned char c2,
7 unsigned char c3,
8 unsigned char c4,
9 unsigned char c5)
10 {
11 1
12 1 SD_XCS = SD_NOT_SELECTED; //put SD on-line.
13 1
14 1 // Provide clock edges before and after asserting SD CS
15 1 SPI8Clocks(8);
16 1 SD_XCS = SD_SELECTED;
17 1 SPI8Clocks(8);
18 1
19 1 // The bus should be stable high now
20 1 if (SPI_RESULT_BYTE != 0xff)
21 1 {
22 2 SD_XCS = SD_NOT_SELECTED;
23 2 return LINE_BUSY;
24 2 }
25 1
26 1 // Send the SD command
27 1 SPIPutCharWithoutWaiting(c1);
28 1 SPIPutChar(c2);
29 1 SPIPutChar(c3);
30 1 SPIPutChar(c4);
31 1 SPIPutChar(c5);
32 1 SPIPutChar(0x95); /* Valid CRC for init, then don't care */
33 1 SPIWait();
34 1 /* Now ok to use c1..c5 as temporaries (dirty but kool) */
35 1
36 1 // Wait for R1 style response (bit 7 low) from SD
37 1
38 1 c1=100;
39 1 while((c1--)&&((c2=SPIGetChar())&0x80)) //wait for R1 or timeout
40 1 ;
41 1
42 1 return c2; //return the R1 response
43 1 }
44
45
46 unsigned char SDWaitForData()
47 {
48 1 unsigned char c;
49 1 // Wait until something else than 0xff is read from the bus
50 1 do
51 1 {
52 2 c=SPIGetChar();
53 2 } while ((c == 0xff));
54 1
55 1
C51 COMPILER V7.50 SD 07/13/2005 10:21:50 PAGE 2
56 1 // desired 0xFE data start token?
57 1 if (c != 0xfe)
58 1 {
59 2 /*
60 2 SPI8Clocks(200);
61 2 SPI8Clocks(200);
62 2 SPI8Clocks(200);
63 2 */
64 2 SD_XCS = SD_NOT_SELECTED; //Drop SD offline now.
65 2 return c;
66 2 }
67 1 return DATA_COMING;
68 1 }
69
70
71 void SDGetData(unsigned int amountOctets)
72 {
73 1 while (amountOctets--)
74 1 {
75 2 *dataBufPtr++=SPIGetChar();
76 2 }
77 1 }
78
79 void PerformBlockRead()
80 {
81 1 unsigned int i;
82 1 for(i=0;i<512;i++)
83 1 {
84 2 SPIPutCharWithoutWaiting(0xff);
85 2 SPIWait();
86 2 *dataBufPtr++=SPI_RESULT_BYTE;
87 2 }
88 1 }
89
90 unsigned char SeekSector(unsigned char cmd_sn,unsigned long sectorN)
91 {
92 1 unsigned char c;
93 1 unsigned char * address;
94 1 address =(unsigned char *)§orN;
95 1 do
96 1 {
97 2 c=SDCommand(cmd_sn,address[2],address[1],address[0], 0);
98 2 address[1] = address[1] >> 1; //convert back to blocks
99 2
100 2 //if SD returs "nothing" (0xff), toggle chip select and retry
101 2 if (c==0xff)
102 2 {
103 3 RebootSD();
104 3 }
105 2 if (c==LINE_BUSY)
106 2 { //SD says "LINE_BUSY"
107 3 c=0xff; //try again
108 3 }
109 2 }while(c==0xff); //repeat until we get signal from SD.
110 1 SD_XCS = SD_NOT_SELECTED;
111 1 return 0; //ok return
112 1 }
113
114
115 /* Wait for data start token and read 512 bytes to global buffer */
116 unsigned char ReadSector()
117 {
C51 COMPILER V7.50 SD 07/13/2005 10:21:50 PAGE 3
118 1 SD_XCS = SD_SELECTED;
119 1 if(SDWaitForData()==DATA_COMING)
120 1 PerformBlockRead();
121 1 else return ERR;
122 1 /* generate SPI clock edges to finish up the command */
123 1
124 1 SPI8Clocks(4); //Send 8*4=32 clocks (4 ff's) to SD to be nice.
125 1 SD_XCS = SD_NOT_SELECTED;
126 1 SPI8Clocks(4); //Again, give the poor SD some clocks, it likes them.
127 1 return 0; //ok return
128 1 }
129
130 unsigned char ReadDiskSector ( unsigned long sectorN )
131 {
132 1 if (SeekSector(CMD17,sectorN)) return ERR; //seek error
133 1 if (ReadSector())
134 1 {
135 2 return ERR; //read error
136 2 }
137 1 return 0; /* All OK return */
138 1 }
139
140
141
142
143 unsigned char WriteSector()
144 {
145 1 unsigned c1;
146 1 unsigned short i;
147 1 SPIPutCharWithoutWaiting(START_BLOCK_TOKEN);
148 1 for(i=0;i<512;i++)
149 1 {
150 2 SPIPutChar(dataBufPtr[i]);
151 2 }
152 1 SPIWait();
153 1 c1=SPIGetChar();
154 1 if(c1==DATA_REC_NO_ERR)
155 1 return 0;
156 1 else return c1;
157 1 }
158
159 unsigned char WriteDiskSector(unsigned long sectorN)
160 {
161 1 if (SeekSector(CMD24,sectorN)) return ERR; //seek error
162 1 if (WriteSector())
163 1 {
164 2 return ERR; //read error
165 2 }
166 1 SD_XCS = SD_NOT_SELECTED;
167 1 SPI8Clocks(4);
168 1 return 0; /* All OK return */
169 1 }
170
171 unsigned char GetCID()
172 {
173 1 if (SDCommand(0x4a,0,0,0,0)&0xfe)
174 1 return 4; /* no storage info */
175 1 SDWaitForData();
176 1 SDGetData(30);
177 1 }
178
179 unsigned char RebootSD()
C51 COMPILER V7.50 SD 07/13/2005 10:21:50 PAGE 4
180 {
181 1 unsigned char c;
182 1 if(SDCommand(0x40,0,0,0,0)==0x01)
183 1 {
184 2 SPI8Clocks(2);
185 2 SDCommand(0x7B,0,0,0,0);
186 2 }
187 1 else return ERR;
188 1 c=255;
189 1 while ((c--)&&(SDCommand(0x41,0,0,0,16)))
190 1 {
191 2 Delay(10);
192 2 if (c==1)
193 2 return TIMEOUT; /* Not able to power up SD */
194 2 }
195 1 return 0;
196 1 }
197
198 unsigned char InitSD()
199 {
200 1 unsigned char c;
201 1 SD_XCS = SD_NOT_SELECTED;
202 1 /* Allow SD some time and clock cycles to reset */
203 1 for (c=0; c<100; c++)
204 1 {
205 2 SPIPutCharWithoutWaiting(0xff);
206 2 SPIWait();
207 2 }
208 1 SD_XCS = SD_SELECTED;
209 1 Delay(20);
210 1 if (RebootSD()) return 1; //not able to powerup;
211 1 //An existing SD card should be able to respond now.
212 1
213 1 GetCID();
214 1
215 1 /* Set Block Size of 512 bytes (2 == 512 << 8) */
216 1 if ((c=SDCommand(0x50,0,0,2,0))) return c|0x80;
217 1
218 1 SD_XCS = SD_NOT_SELECTED;
219 1 SPIPutCharWithoutWaiting(0xff);
220 1 for (c=0; c<100; c++)
221 1 {
222 2 SPIPutChar(0xff);
223 2 }
224 1 SPIWait();
225 1 }
226
227 void SPI8Clocks ( unsigned char nClocks )
228 {
229 1 while (nClocks--)
230 1 {
231 2 SPIPutCharWithoutWaiting(0xff);
232 2 SPIWait();
233 2 }
234 1 }
235
236 unsigned char SPIGetChar (void)
237 {
238 1 SPIPutCharWithoutWaiting(0xff); /* send 0xff */
239 1 SPIWait(); /* wait for the byte to transfer */
240 1 return SPI_RESULT_BYTE; /* Return the received byte */
241 1 }
C51 COMPILER V7.50 SD 07/13/2005 10:21:50 PAGE 5
242
243 void Delay(unsigned short i)
244 {
245 1 while(--i) ;
246 1 }
247
248
249
250
MODULE INFORMATION: STATIC OVERLAYABLE
CODE SIZE = 708 ----
CONSTANT SIZE = ---- ----
XDATA SIZE = ---- ----
PDATA SIZE = ---- ----
DATA SIZE = 2 26
IDATA SIZE = ---- ----
BIT SIZE = ---- ----
END OF MODULE INFORMATION.
C51 COMPILATION COMPLETE. 0 WARNING(S), 0 ERROR(S)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -