📄 sd.lst
字号:
C51 COMPILER V7.02a SD 12/22/2010 21:07:57 PAGE 1
C51 COMPILER V7.02a, COMPILATION OF MODULE SD
OBJECT MODULE PLACED IN sd.OBJ
COMPILER INVOKED BY: C:\Keil\C51\BIN\C51.EXE sd.c LARGE BROWSE DEBUG OBJECTEXTEND
stmt level source
1
2 #include "sd.h"
3 BUFFER_TYPE sectorBuffer;
4 unsigned int readPos=0;
5 unsigned char sectorPos=0;
6 unsigned char LBA_Opened=0; //Set to 1 when a sector is opened.
7 unsigned char Init_Flag; //Set it to 1 when Init is processing.
8 void delay(unsigned int time)
9 {
10 1 while(time--);
11 1 }
12
13 //****************************************************************************
14 // Port Init
15 void MMC_Port_Init()
16 //****************************************************************************
17 {
18 1 SPI_SCL=1;
19 1 SPI_DO =1;
20 1 SPI_CS=1;
21 1 }
22
23 //****************************************************************************
24 //Routine for sending a byte to MMC/SD-Card
25 void Write_Byte_MMC(unsigned char value)
26 //****************************************************************************
27 {
28 1 unsigned char i;
29 1
30 1 SPI_BY=0;
31 1 //Software SPI
32 1 for (i=0;i<8;i++)
33 1 { //write a byte
34 2 if (((value>>(7-i))&0x01)==0x01)
35 2 SPI_DI=1; //Send bit by bit(MSB First)
36 2 else SPI_DI=0;
37 2 SPI_SCL=0; //set Clock Impuls low
38 2 if(Init_Flag)
39 2 delay(8);
40 2 SPI_SCL=1; //set Clock Impuls High
41 2 if(Init_Flag)
42 2 delay(8);
43 2 }//write a byte
44 1 //MMC_BUSY_LED=1;
45 1 }
46
47 //****************************************************************************
48 //Routine for reading a byte from MMC/SD-Card
49 unsigned char Read_Byte_MMC()
50 //****************************************************************************
51 {
52 1 unsigned char temp=0;
53 1 unsigned char i;
54 1
55 1 SPI_BY=0;
C51 COMPILER V7.02a SD 12/22/2010 21:07:57 PAGE 2
56 1 //Software SPI
57 1 for (i=0;i<8;i++) //MSB First
58 1 {
59 2 SPI_DO=1;
60 2 SPI_SCL=0; //Clock Impuls (Low)
61 2 if(Init_Flag)
62 2 delay(8);
63 2 temp=(temp<<1)+(unsigned char)SPI_DO; //read mmc data out pin
64 2 SPI_SCL=1; //set Clock Impuls High
65 2 if(Init_Flag)
66 2 delay(8);
67 2 }
68 1 SPI_BY=1;
69 1 return (temp);
70 1 }
71
72 //****************************************************************************
73 //Send a Command to MMC/SD-Card
74 //Return: the second byte of response register of MMC/SD-Card
75 unsigned char Write_Command_MMC(unsigned char *CMD)
76 //****************************************************************************
77 {
78 1 unsigned char tmp;
79 1 unsigned char retry=0;
80 1 unsigned char i;
81 1
82 1 //set MMC_Chip_Select to high (MMC/SD-Card disable)
83 1 SPI_CS=1;
84 1 //send 8 Clock Impulse
85 1 Write_Byte_MMC(0xFF);
86 1 //set MMC_Chip_Select to low (MMC/SD-Card active)
87 1 SPI_CS=0;
88 1
89 1 //send 6 Byte Command to MMC/SD-Card
90 1 for (i=0;i<0x06;i++)
91 1 {
92 2 Write_Byte_MMC(*CMD++);
93 2 }
94 1
95 1 //get 16 bit response
96 1 Read_Byte_MMC(); //read the first byte,ignore it.
97 1 do
98 1 { //Only last 8 bit is used here.Read it out.
99 2 tmp = Read_Byte_MMC();
100 2 retry++;
101 2 }
102 1 while((tmp==0xff)&&(retry<100));
103 1 return(tmp);
104 1 }
105
106 //****************************************************************************
107 //Routine for Init MMC/SD card(SPI-MODE)
108 unsigned char MMC_Init()
109 //****************************************************************************
110 {
111 1 unsigned char retry,temp;
112 1 unsigned char i;
113 1 unsigned char CMD[] = {0x40,0x00,0x00,0x00,0x00,0x95};
114 1 MMC_Port_Init(); //Init SPI port
115 1
116 1 delay(200);
117 1
C51 COMPILER V7.02a SD 12/22/2010 21:07:57 PAGE 3
118 1 Init_Flag=1; //Set the init flag
119 1
120 1 for (i=0;i<0x0f;i++)
121 1 {
122 2 Write_Byte_MMC(0xff); //send 74 clock at least!!!
123 2 }
124 1
125 1 retry=0;
126 1 do
127 1 { //retry 200 times to send CMD0 command
128 2 temp=Write_Command_MMC(CMD);
129 2 retry++;
130 2 if(retry==200)
131 2 { //time out
132 3 return(INIT_CMD0_ERROR);//CMD0 Error!
133 3 }
134 2 }
135 1 while(temp!=1);
136 1 CMD[0] = 0x41; //Command 1
137 1 CMD[5] = 0xFF;
138 1 retry=0;
139 1 do
140 1 { //retry 100 times to send CMD1 command
141 2 temp=Write_Command_MMC(CMD);
142 2 retry++;
143 2 if(retry==100)
144 2 { //time out
145 3 return(INIT_CMD1_ERROR);//CMD1 Error!
146 3 }
147 2 }
148 1 while(temp!=0);
149 1
150 1 Init_Flag=0; //Init is completed,clear the flag
151 1
152 1 SPI_CS=1; //set MMC_Chip_Select to high
153 1 return(0x55); //All commands have been taken.
154 1 }
155
156 //****************************************************************************
157 //Routine for reading data Registers of MMC/SD-Card
158 //Return 0 if no Error.
159 unsigned char MMC_Read_Block(unsigned char *CMD,unsigned char *Buffer,unsigned int Bytes)
160 //****************************************************************************
161 {
162 1 unsigned int i;
163 1 unsigned retry,temp;
164 1
165 1 //Send Command CMD to MMC/SD-Card
166 1 retry=0;
167 1 do
168 1 { //Retry 100 times to send command.
169 2 temp=Write_Command_MMC(CMD);
170 2 retry++;
171 2 if(retry==100)
172 2 {
173 3 return(READ_BLOCK_ERROR); //block write Error!
174 3 }
175 2 }
176 1 while(temp!=0);
177 1
178 1 //Read Start Byte form MMC/SD-Card (FEh/Start Byte)
179 1 while (Read_Byte_MMC()!=0xfe);
C51 COMPILER V7.02a SD 12/22/2010 21:07:57 PAGE 4
180 1
181 1 //Write blocks(normal 512Bytes) to MMC/SD-Card
182 1 for (i=0;i<Bytes;i++)
183 1 {
184 2 *Buffer++ = Read_Byte_MMC();
185 2 }
186 1
187 1 //CRC-Byte
188 1 Read_Byte_MMC();//CRC - Byte
189 1 Read_Byte_MMC();//CRC - Byte
190 1
191 1 //set MMC_Chip_Select to high (MMC/SD-Card invalid)
192 1 SPI_CS=1;
193 1 return(0);
194 1 }
195
196 //***************************************************************************
197 //Routine for reading CSD Registers from MMC/SD-Card (16Bytes)
198 //Return 0 if no Error.
199 unsigned char Read_CSD_MMC(unsigned char *Buffer)
200 //***************************************************************************
201 {
202 1 //Command for reading CSD Registers
203 1 unsigned char CMD[] = {0x49,0x00,0x00,0x00,0x00,0xFF};
204 1 unsigned char temp;
205 1 temp=MMC_Read_Block(CMD,Buffer,16); //read 16 bytes
206 1
207 1 return(temp);
208 1 }
209
210 //***************************************************************************
211 //Routine for reading CID Registers from MMC/SD-Card (16Bytes)
212 //Return 0 if no Error.
213 unsigned char Read_CID_MMC(unsigned char *Buffer)
214 //***************************************************************************
215 {
216 1 //Command for reading CID Registers
217 1 unsigned char CMD[] = {0x4A,0x00,0x00,0x00,0x00,0xFF};
218 1 unsigned char temp;
219 1 temp=MMC_Read_Block(CMD,Buffer,16); //read 16 bytes
220 1
221 1 return(temp);
222 1 }
223
224 //****************************************************************************
225 //returns the :
226 // size of the card in MB ( ret * 1024^2) == bytes
227 // sector count and multiplier MB are in u08 == C_SIZE / (2^(9-C_SIZE_MULT))
228 // name of the media
229 void MMC_get_volume_info(void)
230 //****************************************************************************
231 { unsigned char i;
232 1 unsigned char c_temp[5];
233 1 VOLUME_INFO_TYPE MMC_volume_Info,*vinf;
234 1 //send_s("SD CARD Information Read!!");
235 1 vinf=&MMC_volume_Info; //Init the pointoer;
236 1 // read the CSD register
237 1 Read_CSD_MMC(sectorBuffer.dat);
238 1 // get the C_SIZE value. bits [73:62] of data
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -