📄 halsd.lst
字号:
C51 COMPILER V8.01 HALSD 04/17/2008 09:46:24 PAGE 1
C51 COMPILER V8.01, COMPILATION OF MODULE HALSD
OBJECT MODULE PLACED IN .\Debug\halsd.obj
COMPILER INVOKED BY: C:\Keil\C51\BIN\C51.EXE sd\halsd.c LARGE OPTIMIZE(9,SIZE) NOINTPROMOTE INCDIR(.\common;.\e2prom;.\f
-ile;.\fmtx;.\ir;.\keyboard;.\lcm;.\led;.\main;.\matrixkey;.\mp3ctrl;.\sd;.\usbhost;.\include;.\lib) DEBUG OBJECTEXTEND P
-RINT(.\Debug\halsd.lst) OBJECT(.\Debug\halsd.obj)
line level source
1 /****************************************************************
2 * Viaon Technology (Suzhou) Co.,Ltd
3 *
4 * Copyright 2007, Viaon Technology (Suzhou) Co.,Ltd,Suzhou,China
5 * All rights reserved.
6 *
7 *
8 * Filename: halsd.c
9 *
10 * Programmer: Greg
11 *
12 * Created: 1/2/2008
13 *
14 * Description: public functions
15 *
16 *
17 * Change History (most recent first): 2008.1.2
18 ****************************************************************/
19 #include "va4010reg.h"
20 #include "utiltypedef.h"
21 #include "common.h"
22 #include "halsd.h"
23 #include "halir.h"
24 #include "timer.h"
25 #include "halsdif.h"
26 #include "halinfo.h"
27 #include "hali2c.h"
28
29 #define RESP_TIMES 9
30 #define UNKW_CARD 0X00
31 #define SD_CARD 0X01
32 #define MMC_CARD 0X02
33 #define SDHC_CARD 0X03
34
35 #define SPI_WRITE(a) { SPI_DATA=(a); while(!(SPI_STA0&SPI_SPTEF)); }
36
37 // SD/MMC commands, this byte including start_bit(0), transmission_bit(1), cmd_idx(6bits)
38 #define SD_RESET 0x40 + 0
39 #define SD_INIT 0x40 + 1
40 #define SD_READ_CSD 0x40 + 9
41 #define SD_READ_CID 0x40 + 10
42 //
43 #define SD_STOP_TRANSMISSION 0x40 + 12
44 #define SD_SEND_STATUS 0x40 + 13
45 //
46 #define SD_SET_BLOCKLEN 0x40 + 16
47 #define SD_READ_BLOCK 0x40 + 17
48 #define SD_READ_MULTI_BLOCK 0x40 + 18
49 //
50 #define SD_WRITE_BLOCK 0x40 + 24 //0X58
51 #define SD_WRITE_MULTI_BLOCK 0x40 + 25
52 ///////////////////////////////////////////
53 #define CMD0_GO_IDLE_STATE (0x40 + 0)
C51 COMPILER V8.01 HALSD 04/17/2008 09:46:24 PAGE 2
54 #define CMD1_SEND_OP_COND (0x40 + 1)
55
56 #define CMD8_SEND_IF_COND (0X40 + 8)
57 #define CMD58_READ_OCR (0X40 + 58) //0X7A
58 #define CMD55_APP_CMD (0x40 + 55) //0x77
59 #define ACMD41_SEND_OP_COND (0x40 + 41) //0x69
60 #define CMD16_SET_BLOCKLEN (0x40 + 16) //0x50
61 #define CMD17_READ_SINGLE_BLOCK (0x40 + 17)//0x51
62
63
64 #if 0
#define SDSS_OUT_ENABLE() (P1_DDR|=0X10)
#define SDSS_ENABLE() do{P1&=(~0X10); DelayUs(1);} while(0)
#define SDSS_DISABLE() do{DelayUs(6); P1|=(0X10);} while(0)
#else
69 #define SDSS_OUT_ENABLE()
70 #define SDSS_ENABLE()
71 #define SDSS_DISABLE()
72 #endif
73
74 extern BYTE DBUF[512];
75 //extern BYTE SysTicks;
76
77 #define BYTE_PER_SEC 512
78
79 #define SDMMC_RESPONSE_IDLE 0X01
80 #define SDMMC_RESPONSE_READY 0X00
81 #define SDMMC_RESPONSE_ILLEGAL 0X05
82
83
84 #ifdef ENABLE_SD_CARD
85 static volatile BYTE data sdmmc;
86 static BYTE cmdarg[4];
87 //static BYTE data cmdarg[4];
88
89 //static BYTE halSDSPICmdResp(BYTE cmd, BYTE *pArg, BYTE crc7)
90 static BYTE halSDSPICmdResp(BYTE cmd, BYTE crc7)
91 {
92 1 BYTE i;
93 1 //BYTE crc7 = 0x95; //CRC7 for CMD0
94 1 BYTE tmp;
95 1 BYTE ret = 0xff;
96 1
97 1 SDSS_ENABLE();
98 1 DelayMs(1);
99 1
100 1 //6 Bytes CMD, including 1Byte CRC7+end_bit, CRC7 will be ignored.
101 1 //But CRC7 will be checked for the first cmd CMD0
102 1 SPI_WRITE(cmd);
103 1 SPI_WRITE(cmdarg[0]); //MSB of address
104 1 SPI_WRITE(cmdarg[1]);
105 1 SPI_WRITE(cmdarg[2]);
106 1 SPI_WRITE(cmdarg[3]); //LSB of address
107 1 SPI_WRITE(crc7); //dummy checksum
108 1
109 1 SPI_WRITE(0xFF); //Dummy write for NCR
110 1
111 1 //Waitting response form sd card, and consume the NCR(1->8)
112 1 //UartOutText("R1: --------------\r\n");
113 1 for(i=0; i<RESP_TIMES; i++){
114 2 //SPI_WRITE(0xFF);
115 2 while(!(SPI_STA0&SPI_RRDY));
C51 COMPILER V8.01 HALSD 04/17/2008 09:46:24 PAGE 3
116 2 tmp = SPI_DATA;
117 2 //UartOutValue(tmp, 2);
118 2
119 2 if(tmp != 0XFF){
120 3 ret = tmp;
121 3 break;
122 3 }
123 2 SPI_WRITE(0xFF);//????????????
124 2 }
125 1
126 1 DelayMs(1);
127 1 SDSS_DISABLE();
128 1
129 1 return ret;
130 1 }
131
132 static void halSDSPIACMD41(BYTE card, BYTE max)
133 {
134 1 BYTE data i;
135 1 //BYTE cmdarg[4];
136 1 BYTE data rept = 0;
137 1 BYTE data resp;
138 1
139 1 // WDT = 0X1F;
140 1 SysTicks = 0;
141 1 cmdarg[1] = 0x00;
142 1 cmdarg[2] = 0x00;
143 1 cmdarg[3] = 0x00;
144 1 while(1){
145 2 cmdarg[0] = 0x00;
146 2 if(SDMMC_RESPONSE_IDLE == halSDSPICmdResp(CMD55_APP_CMD, 0xff)){
147 3 if(card == SDHC_CARD)
148 3 cmdarg[0] = 0x40; // HCS: High capacity support
149 3
150 3 //while(1){
151 3 for(i=0; i<200; i++){
152 4 resp = halSDSPICmdResp(ACMD41_SEND_OP_COND, 0xff);
153 4 if(SDMMC_RESPONSE_READY == resp) {
154 5 sdmmc = card;
155 5 rept = max;
156 5 //UartOutText("CARD rept 0x"); UartOutValue(rept, 2);
157 5 break;
158 5 }else if(SDMMC_RESPONSE_ILLEGAL == resp){
159 5 //rept = max;
160 5 //break;
161 5 }
162 4 }
163 3 }
164 2
165 2 if((rept>=max) || (SysTicks > 9)){
166 3 break;
167 3 }
168 2 }
169 1
170 1 return;
171 1 }
172
173 BYTE halSDInit(void)
174 {
175 1 BYTE data i;
176 1 BYTE data rept;
177 1 BYTE data resp;
C51 COMPILER V8.01 HALSD 04/17/2008 09:46:24 PAGE 4
178 1 //BYTE cmdarg[4];
179 1 // volatile BYTE bSDFlag = SD_ENUMGOING_FLAG;
180 1
181 1 SDSS_OUT_ENABLE();
182 1 SDSS_DISABLE();
183 1
184 1 //WDT = 0X1F;
185 1 UartOutText("SDSPIInit() \r\n");
186 1 #ifdef SDENUM_RETRY_ENABLE
Write_One_Byte(SDENUM_PAGE_ADDR<<E2PROM_PAGE_SIZEEXP, SD_ENUMGOING_FLAG);
#endif
189 1 // SPI_CTL1
190 1 // SPI_CTL1 = SPI_750K; // set speed
191 1 SPI_CTL1 = SD_SPI_CLK; // set speed
192 1 //SPI_CTL1 = SPI_188K; // set speed
193 1
194 1 // SPI_CTL0
195 1 // SPI_LSB=0 Send MSB first
196 1 // SPI_CPOL=1 SCK high if IDLE
197 1 // SPI_CPHA=1 SPI Mode 1
198 1 // SPI_MST=1 I am the master
199 1 // SPI_TIE=0 No SPI TX Interrupt
200 1 // SPI_RIE=0 No SPI RX Interrupt
201 1 // SPI_EN=1 SPI Enable
202 1 // SPI_SSOEN=0 Output enable for select
203 1 //SPI_CTL0 = 0x4E;
204 1 //SPI_CTL0 = 0x48; //CPOL, CPHA is 0
205 1
206 1 // give min 74 clock pulses before sending commands, about 1ms?
207 1 sdmmc = UNKW_CARD;
208 1
209 1 // Just kill some time
210 1 SysTicks = 0;
211 1 while(SysTicks < 10);
212 1
213 1 WDT = 0X1F;
214 1 while(1){
215 2 SPI_CTL0 = 0XCE;
216 2 for(i=0; i<0x10; i++)
217 2 SPI_WRITE(0xFF);
218 2 SPI_CTL0 = 0X4E;
219 2
220 2 //DelayUs(10); //Wait some time for SPI_CTL0
221 2 *((DWORD *)cmdarg) = 0;
222 2 if(SDMMC_RESPONSE_IDLE == halSDSPICmdResp(CMD0_GO_IDLE_STATE, 0x95)){
223 3 break;
224 3 }
225 2 }
226 1
227 1 UartOutText("CMD0_GO_IDLE_STATE done \r\n");
228 1
229 1 UartOutText("CMD8_SEND_IF_COND \r\n");
230 1 cmdarg[0] = 0x00;
231 1 cmdarg[1] = 0x00;
232 1 cmdarg[2] = 0x01; //Voltage Supplied: 2.7 -3.6V
233 1 cmdarg[3] = 0xaa; //Check pattern
234 1 resp = halSDSPICmdResp(CMD8_SEND_IF_COND, 0x87);
235 1 if(SDMMC_RESPONSE_IDLE == resp){
236 2 //SD2.0: CMD8 Supported //Response R7
237 2 SPI_WRITE(0xFF);
238 2 SPI_WRITE(0xFF);
239 2 SPI_WRITE(0xFF);
C51 COMPILER V8.01 HALSD 04/17/2008 09:46:24 PAGE 5
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -