📄 mmc_drv.lst
字号:
C51 COMPILER V7.50 MMC_DRV 02/16/2009 09:59:52 PAGE 1
C51 COMPILER V7.50, COMPILATION OF MODULE MMC_DRV
OBJECT MODULE PLACED IN mmc_drv.obj
COMPILER INVOKED BY: d:\Keil\C51\BIN\C51.EXE lib_mcu\mmc\mmc_drv.c LARGE BROWSE DEBUG OBJECTEXTEND PRINT(.\mmc_drv.lst)
-OBJECT(mmc_drv.obj)
line level source
1
2 #include "config.h" /* system configuration */
3 #include "mmc_drv.h" /* mmc driver definition */
4
5
6 #include "lib_mcu\usb\usb_drv.h" /* usb driver definition */
7 #include "lib_mcu\lcd\lcd_drv.h"
8 #include "lib_mcu\clock\clock.h"
9 #include "modules\mem\media.h" /* low level function definition */
10 #include "lib_mcu\serial\ser_drv.h"
11 #include <stdio.h>
12 #include <intrins.h>
13
14
15 extern bit bMediaWriteOpenFlag;
16 extern bit bMediaReadOpenFlag;
17 extern INT16U u16MediaBytePointer;
18 extern INT32U u32MediaSector;
19 extern xdata Byte gl_media_buffer[512];
20
21 static Byte mmc_state;
22 static bit mmc_ready; /* MMC in prog state */
23 static INT8U u8FifoPoint;
24
25 void mmc_set_prio (Byte);
26 void mmc_send_cmd (Byte, Uint32, Byte);
27 bit mmc_check_response (void);
28
29
30 /*****************************************************************************/
31 void mmc_set_prio (Byte priority)
32 {
33 1 if ((priority == 1) || (priority == 3)) /* set LSB priority bit */
34 1 {
35 2 IPL1 |= MSK_ESPI;
36 2 }
37 1 if ((priority == 2) || (priority == 3)) /* set MSB priority bit */
38 1 {
39 2 IPH1 |= MSK_ESPI;
40 2 }
41 1 }
42
43
44
45 /*****************************************************************************/
46 void mmc_send_cmd (Byte index, Uint32 argument, Byte response)
47 {
48 1 MMCMD = index;
49 1 MMCMD = ((Byte*)&argument)[0];
50 1 MMCMD = ((Byte*)&argument)[1];
51 1 MMCMD = ((Byte*)&argument)[2];
52 1 MMCMD = ((Byte*)&argument)[3];
53 1
54 1 switch (response)
C51 COMPILER V7.50 MMC_DRV 02/16/2009 09:59:52 PAGE 2
55 1 {
56 2 case MMC_RESP_R1:
57 2 case MMC_RESP_R4:
58 2 case MMC_RESP_R5:
59 2 {
60 3 MMCON0 |= MSK_RFMT; /* set 48 bits response */
61 3 MMCON0 &= ~MSK_CRCDIS; /* set response with CRC7 */
62 3 MMCON1 |= (MSK_CMDEN|MSK_RESPEN);
63 3 MMCON1 &= ~(MSK_CMDEN|MSK_RESPEN); /* send command with response */
64 3 break;
65 3 }
66 2 case MMC_RESP_R2:
67 2 {
68 3 MMCON0 &= ~(MSK_RFMT | MSK_CRCDIS); /* set 136 bits response with CRC7 */
69 3 MMCON1 |= (MSK_CMDEN|MSK_RESPEN);
70 3 MMCON1 &= ~(MSK_CMDEN|MSK_RESPEN); /* send command with response */
71 3 break;
72 3 }
73 2 case MMC_RESP_R3:
74 2 {
75 3 MMCON0 |= (MSK_RFMT | MSK_CRCDIS); /* set 48 bits response without CRC7 */
76 3 MMCON1 |= (MSK_CMDEN|MSK_RESPEN);
77 3 MMCON1 &= ~(MSK_CMDEN|MSK_RESPEN); /* send command with response */
78 3 break;
79 3 }
80 2 case MMC_NO_RESP:
81 2 default:
82 2 {
83 3 MMCON1 |= MSK_CMDEN;
84 3 MMCON1 &= ~MSK_CMDEN; /* send command without response */
85 3 break;
86 3 }
87 2 }
88 1 }
89
90
91 /*****************************************************************************/
92 void mmc_send_scmd (Byte index, Byte response)
93 {
94 1 MMCMD = index;
95 1 MMCMD = (Byte)MMC_NO_ARG;
96 1 MMCMD = (Byte)MMC_NO_ARG;
97 1 MMCMD = (Byte)MMC_NO_ARG;
98 1 MMCMD = (Byte)MMC_NO_ARG;
99 1
100 1 switch (response)
101 1 {
102 2 case MMC_RESP_R1:
103 2 case MMC_RESP_R4:
104 2 case MMC_RESP_R5:
105 2 case SD_RESP_R6:
106 2 {
107 3 MMCON0 |= MSK_RFMT; /* set 48 bits response */
108 3 MMCON0 &= ~MSK_CRCDIS; /* set response with CRC7 */
109 3 MMCON1 |= (MSK_CMDEN|MSK_RESPEN);
110 3 MMCON1 &= ~(MSK_CMDEN|MSK_RESPEN); /* send command with response */
111 3 break;
112 3 }
113 2 case MMC_RESP_R2:
114 2 {
115 3 MMCON0 &= ~(MSK_RFMT | MSK_CRCDIS); /* set 136 bits response with CRC7 */
116 3 MMCON1 |= (MSK_CMDEN|MSK_RESPEN);
C51 COMPILER V7.50 MMC_DRV 02/16/2009 09:59:52 PAGE 3
117 3 MMCON1 &= ~(MSK_CMDEN|MSK_RESPEN); /* send command with response */
118 3 break;
119 3 }
120 2 case MMC_RESP_R3:
121 2 {
122 3 MMCON0 |= (MSK_RFMT | MSK_CRCDIS); /* set 48 bits response without CRC7 */
123 3 MMCON1 |= (MSK_CMDEN|MSK_RESPEN);
124 3 MMCON1 &= ~(MSK_CMDEN|MSK_RESPEN); /* send command with response */
125 3 break;
126 3 }
127 2 case MMC_NO_RESP:
128 2 default:
129 2 {
130 3 MMCON1 |= MSK_CMDEN;
131 3 MMCON1 &= ~MSK_CMDEN; /* send command without response */
132 3 break;
133 3 }
134 2 }
135 1 }
136
137
138
139 /*****************************************************************************/
140 bit mmc_check_response (void)
141 {
142 1 if (Mmc_response_received())
143 1 {
144 2 /* response received */
145 2 if ((MMCON0 & MSK_CRCDIS) != 0)
146 2 { /* CRC7 not computed */
147 3 if ((MMSTA & MSK_RESPFS) != 0)
148 3 {
149 4 return (MMC_RESP_OK);
150 4 }
151 3 else
152 3 {
153 4 return (MMC_ERR_RESP); /* format error */
154 4 }
155 3 }
156 2 else
157 2 { /* CRC7 computed */
158 3 if ((MMSTA & (MSK_RESPFS | MSK_CRC7S)) == (MSK_RESPFS | MSK_CRC7S))
159 3 {
160 4 return (MMC_RESP_OK);
161 4 }
162 3 else
163 3 {
164 4 return (MMC_ERR_RESP); /* format or CRC7 error */
165 4 }
166 3 }
167 2 }
168 1 else
169 1 { /* no response received */
170 2 return (MMC_ERR_RESP);
171 2 }
172 1 }
173
174
175
176 /*****************************************************************************/
177 Uint32 mmc_read_response (void)
178 {
C51 COMPILER V7.50 MMC_DRV 02/16/2009 09:59:52 PAGE 4
179 1 Uint32 argument;
180 1
181 1 ((Byte*)&argument)[0] = MMCMD; /* dummy index read */
182 1 ((Byte*)&argument)[0] = MMCMD;
183 1 ((Byte*)&argument)[1] = MMCMD;
184 1 ((Byte*)&argument)[2] = MMCMD;
185 1 ((Byte*)&argument)[3] = MMCMD;
186 1
187 1 return argument;
188 1 }
189
190
191 //////////////////////////////////////////////////////////////////////////////
192 INT8U mmc_response_Try(void)
193 {
194 1 INT8U i;
195 1
196 1 for(i=0; i<100; i++)
197 1 {
198 2 if(mmc_check_response())
199 2 {
200 3 return OK;
201 3 }
202 2 }
203 1 return KO;
204 1 }
205
206
207 //////////////////////////////////////////////////////////////////////////////
208 bit Mmc_init(void)
209 {
210 1 INT8U ARRARY[17];
211 1
212 1 Mmc_reset();
213 1 Mmc_set_clock(30); //the clk speed must less than 400khz
214 1 Mmc_enable();
215 1 Mmc_disable_flow_ctrl();
216 1
217 1 mmc_send_scmd(MMC_GO_IDLE_STATE, MMC_NO_RESP); //reset
218 1 clock_vDelay(0xfff);
219 1
220 1 do
221 1 {
222 2 mmc_send_cmd(MMC_SEND_OP_COND, 0x00100000, MMC_RESP_R3); //CMD1
223 2 if(OK == mmc_response_Try())
224 2 {
225 3 ARRARY[0] = Mmc_rd_cmd();
226 3 ARRARY[0] = Mmc_rd_cmd();
227 3 ARRARY[1] = Mmc_rd_cmd();
228 3 ARRARY[2] = Mmc_rd_cmd();
229 3 ARRARY[3] = Mmc_rd_cmd();
230 3 }
231 2 }while(!ARRARY[0]);
232 1
233 1
234 1 mmc_send_scmd(MMC_ALL_SEND_CID, MMC_RESP_R2); //send cid
235 1 if(OK == mmc_response_Try())
236 1 {
237 2 ;
238 2 }
239 1
240 1 mmc_send_cmd(MMC_SET_RELATIVE_ADDRESS, 0x0001ffff, MMC_RESP_R1);//set address
C51 COMPILER V7.50 MMC_DRV 02/16/2009 09:59:52 PAGE 5
241 1 if(OK == mmc_response_Try())
242 1 {
243 2 ;
244 2 }
245 1
246 1 mmc_send_cmd(MMC_SELECT_CARD, 0x0001ffff, MMC_RESP_R1); //select addressed card
247 1 if(OK == mmc_response_Try())
248 1 {
249 2 ;
250 2 }
251 1
252 1 /*
253 1 mmc_send_cmd(MMC_SEND_CSD, 0x0001ffff, MMC_RESP_R2); //csd
254 1 if(OK == mmc_response_Try())
255 1 {
256 1 ARRARY[0] = Mmc_rd_cmd();
257 1 ARRARY[1] = Mmc_rd_cmd();
258 1 ARRARY[2] = Mmc_rd_cmd();
259 1 ARRARY[3] = Mmc_rd_cmd();
260 1 ARRARY[5] = Mmc_rd_cmd();
261 1 ARRARY[6] = Mmc_rd_cmd();
262 1
263 1 ser_putch(ARRARY[0]);
264 1 ser_putch(ARRARY[1]);
265 1 ser_putch(ARRARY[2]);
266 1 ser_putch(ARRARY[3]);
267 1 ser_putch(ARRARY[4]);
268 1 ser_putch(ARRARY[5]);
269 1 ser_putch(ARRARY[6]);
270 1 while(1);
271 1 //printch(0,"3-Success");
272 1 }*/
273 1
274 1 Mmc_set_clock(1); //speed up the clk freq
275 1 Mmc_set_single_block(); //
276 1 Mmc_set_block_size(9); //512 Byte per sector
277 1
278 1 mmc_send_cmd(MMC_SET_BLOCK_LEN, 512, MMC_RESP_R1);
279 1 if(OK == mmc_response_Try())
280 1 {
281 2 ;
282 2 }
283 1 return OK;
284 1 }
285
286
287 //////////////////////////////////////////////////////////////////////////////
288 bit mmc_read_open(INT32U u32MmcSector)
289 {
290 1 INT32U u32Address;
291 1 INT16U i;
292 1
293 1 u32Address = u32MmcSector;
294 1 u32Address <<= 9;
295 1
296 1 Mmc_read_block_cmd(u32Address);
297 1
298 1 if(OK == mmc_response_Try())
299 1 {
300 2 u32MediaSector = u32MmcSector; //remember the sector
301 2 u16MediaBytePointer = 0; //point to the first byte of the sector
302 2 bMediaReadOpenFlag = 1;
C51 COMPILER V7.50 MMC_DRV 02/16/2009 09:59:52 PAGE 6
303 2
304 2 Mmc_enable_flow_ctrl();
305 2 Mmc_set_read();
306 2 Mmc_enable_send();
307 2
308 2 while(!Mmc_read_ready());
309 2
310 2 for(i=0; i<512;)
311 2 {
312 3 gl_media_buffer[i++] = Mmc_rd_byte();
313 3 gl_media_buffer[i++] = Mmc_rd_byte();
314 3 gl_media_buffer[i++] = Mmc_rd_byte();
315 3 gl_media_buffer[i++] = Mmc_rd_byte();
316 3 gl_media_buffer[i++] = Mmc_rd_byte();
317 3 gl_media_buffer[i++] = Mmc_rd_byte();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -