📄 main.lst
字号:
C51 COMPILER V7.00 Beta 6 MAIN 02/19/2003 15:59:28 PAGE 1
C51 COMPILER V7.00 Beta 6, COMPILATION OF MODULE MAIN
OBJECT MODULE PLACED IN main.OBJ
COMPILER INVOKED BY: C:\Keil\C51\BIN\C51.EXE main.c LARGE BROWSE DEBUG OBJECTEXTEND
stmt level source
1 /////////////////////////////////////////////////////////////////////////////
2 // main.c
3 //
4 // Main application file.
5 //
6 // Author: Jon Moore
7 //
8 // Revision History:
9 //
10 // 07/22/02 (JAM) V1.0.0 - Initial coding.
11 // 11/02/02 (JAM) V1.0.5 - Added display of selected flash in LCD.
12 // 01/21/03 (JAM) V1.0.7 - Fixed '0 length packet' bug.
13
14 /*---------------------------------------------------------------------------
15 Copyright (c) 2002-2003 ST Microelectronics
16 This example demo code is provided as is and has no warranty,
17 implied or otherwise. You are free to use/modify any of the provided
18 code at your own risk in your applications with the expressed limitation
19 of liability (see below) so long as your product using the code contains
20 at least one uPSD products (device).
21
22 LIMITATION OF LIABILITY: NEITHER STMicroelectronics NOR ITS VENDORS OR
23 AGENTS SHALL BE LIABLE FOR ANY LOSS OF PROFITS, LOSS OF USE, LOSS OF DATA,
24 INTERRUPTION OF BUSINESS, NOR FOR INDIRECT, SPECIAL, INCIDENTAL OR
25 CONSEQUENTIAL DAMAGES OF ANY KIND WHETHER UNDER THIS AGREEMENT OR
26 OTHERWISE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
27 --------------------------------------------------------------------------*/
28
29 #include <string.h>
30 #include "general.h"
31 #include "upsd3200.h"
32 #include "upsd_xreg.h"
33 #include "upsd_usb.h"
34 #include "upsd_cfg.h"
35
36 #include "lcd_io.h"
37 #include "timer.h"
38
39 #include "app_intr.h"
40 #include "ISD51_U1.h"
41 /////////////////// Prototypes
42
43 extern uchar flash_write_with_poll(volatile uchar xdata *addr, uchar dat);
44 extern uchar flash_boot_write_with_poll(volatile uchar xdata *addr, uchar dat);
45 extern uchar flash_erase_sector(volatile uchar xdata* addr);
46 extern uchar flash_boot_erase_sector(volatile uchar xdata* addr);
47 extern void flash_reset();
48 extern void flash_boot_reset();
49
50 extern void initXREG();
51
52 /////////////////// Globals
53
54 #define ISD51_DEBUG 0
55 #define DEMO_TITLE_STR "USB DEMO V1.0.7\r\n"
C51 COMPILER V7.00 Beta 6 MAIN 02/19/2003 15:59:28 PAGE 2
56
57 extern char xdata LCD_buffer[]; // LCD mirror buffer (lcd_io.c)
58 extern PSD_REGS UPSD_xreg; // UPSD registers (upsd_cfg.c)
59 extern int counter; // Incremented in do_timer0 (timer_func.c)
60
61 MCU_CMD status; // Status from last command executed
62 uchar returnStatus; // Set TRUE if we should return status record to host
63 MCU_CMD currentCmd; // Current command we're working on
64 MCU_CMD rcvReport; // Incoming report
65 uchar rcvIndex; // Current byte position in incoming report
66 MCU_CMD txReport; // Outgoing report
67 uchar txIndex; // Current byte position in outgoing report
68
69 uchar g_debug0;
70 uchar g_debug1;
71 uchar g_debug2;
72
73 xdata uchar testBuf[256];
74
75 /////////////////// initISD()
76 //
77 // Initializes serial port for Keil ISD debugger.
78 //
79 // NOTE: RCAP2L is currently set for a 24 Mhz DK3000 board frequency.
80
81 #if (ISD51_DEBUG)
static void initISD(void)
{
T2CON = 0x34; // Use Timer 2 as baudrate generator
RCAP2H = 0xFF;
RCAP2L = 0xC6; // 9600 baud with 24 MHz clock
SCON = 0x50; // Enable serial uart & receiver
RI=0;
TI=0;
EA = 1; // Enable global interrupt flag
}
#endif
95
96 /////////////////// OnTransmitEP1()
97 //
98 // Handler for successful data transmission on endpoint EP1 IN.
99 //
100 // Sends next segment of LCD buffer to PC as a HID input report.
101
102 static void OnTransmitEP1()
103 {
104 1 static int bufIndex = 0; // Current position in LCD buffer
105 1 char txBuf[INPUT_REPORT_SIZE]; // Buffer to send back to PC
106 1 int nBytes = 7; // Num bytes of LCD data
107 1
108 1 // Store current index into LCD buffer in first byte of tx buffer
109 1 txBuf[0] = bufIndex;
110 1 if (nBytes > (LCD_BUFFER_SIZE - bufIndex))
111 1 {
112 2 nBytes = LCD_BUFFER_SIZE - bufIndex;
113 2 }
114 1 memcpy(txBuf + 1, LCD_buffer + bufIndex, nBytes);
115 1
116 1 // Transmit input report to host
117 1 TransmitDataEPx(1, txBuf, INPUT_REPORT_SIZE);
C51 COMPILER V7.00 Beta 6 MAIN 02/19/2003 15:59:28 PAGE 3
118 1
119 1 if ((bufIndex += nBytes) >= LCD_BUFFER_SIZE)
120 1 {
121 2 // Wrap around to start of LCD buffer for next transmission
122 2 bufIndex = 0;
123 2 }
124 1 }
125
126 /////////////////// initIPs()
127
128 static void initIPs()
129 {
130 1 UsbInitialize();
131 1 }
132
133 /////////////////// main()
134
135 void main()
136 {
137 1 // Initialize globals
138 1 g_debug0 = 0;
139 1 g_debug1 = 0;
140 1 g_debug2 = 0;
141 1
142 1 memset((uchar*)&status, 0, sizeof(status));
143 1 memset((uchar*)¤tCmd, 0, sizeof(currentCmd));
144 1 counter = 0;
145 1
146 1 // We are not currently transmitting or receiving feature/input reports
147 1 rcvIndex = CMD_SIZE;
148 1 txIndex = CMD_SIZE;
149 1
150 1 // Disable watchdog
151 1 WDKEY=WD_OFF;
152 1
153 1 #if (ISD51_DEBUG)
initISD(); // init In-system debugger
//ISDwait(); // initialize uVision2 Debugger and continue program run
#endif
157 1
158 1 initIPs(); // init IP blocks in uPSD
159 1 initXREG(); // init extended regs in xdata
160 1
161 1 timer0_initialize();
162 1 initLCD();
163 1 printfLCD(DEMO_TITLE_STR);
164 1
165 1 while (1)
166 1 {
167 2 //ISDcheck();
168 2 g_debug0 = currentCmd.u.cmd;
169 2
170 2 // Indicate which flash we are running out of: main (M) or boot (B)
171 2
172 2 g_debug2 = ((UPSD_xreg.VM == 0x92) ? 'B' : 'M');
173 2 printfLCD("%x %x %x %c\r", (uchar)(counter >> 8), g_debug0, g_debug1, g_debug2);
174 2
175 2 switch (currentCmd.u.cmd)
176 2 {
177 3 case CMD_ERASE:
178 3
179 3 if (currentCmd.u.erase.flash == PRIMARY_FLASH)
C51 COMPILER V7.00 Beta 6 MAIN 02/19/2003 15:59:28 PAGE 4
180 3 {
181 4 status.u.status.ret = flash_erase_sector(
182 4 (volatile uchar xdata*) currentCmd.u.erase.address);
183 4 }
184 3 else
185 3 {
186 4 status.u.status.ret = flash_boot_erase_sector(
187 4 (volatile uchar xdata*) currentCmd.u.erase.address);
188 4 }
189 3
190 3 // Done
191 3 currentCmd.u.cmd = 0;
192 3 break;
193 3
194 3 default:
195 3
196 3 break;
197 3 }
198 2 }
199 1 }
200
201 /////////////////// WriteBufferToFlash()
202
203 static void WriteBufferToFlash(uchar flash, uint16 address, uchar* buf, uint16 cb)
204 {
205 1 // Assume desired flash segment is mapped into high 32k of xdata space
206 1 volatile uchar xdata* p = (volatile uchar xdata*) address;
207 1
208 1 if (flash == PRIMARY_FLASH)
209 1 {
210 2 while (cb--)
211 2 {
212 3 flash_write_with_poll(p, *buf++);
213 3 status.u.status.checkSum += *p++;
214 3 }
215 2 }
216 1 else
217 1 {
218 2 while (cb--)
219 2 {
220 3 flash_boot_write_with_poll(p, *buf++);
221 3 status.u.status.checkSum += *p++;
222 3 }
223 2 }
224 1 }
225
226 /////////////////// ReadBufferFromFlash()
227
228 static void ReadBufferFromFlash(uint16 address, uchar* buf, uint16 cb)
229 {
230 1 // Assume desired flash segment is mapped into high 32k of xdata space
231 1 volatile uchar xdata* p = (volatile uchar xdata*) address;
232 1
233 1 while (cb--)
234 1 {
235 2 *buf++ = *p++;
236 2 }
237 1 }
238
239 /////////////////// OnDeviceConfigured()
240 //
241 // Called after device is completely configured.
C51 COMPILER V7.00 Beta 6 MAIN 02/19/2003 15:59:28 PAGE 5
242
243 void OnDeviceConfigured()
244 {
245 1 // Set up first tx on EP1
246 1 OnTransmitEP1();
247 1
248 1 // Disable EP0 IN
249 1 UCON0 &= ~uTX0E;
250 1 }
251
252 /////////////////// PrepareTransmitSegment()
253 //
254 // Prepare next segment of feature report for transmission.
255 //
256 // index - current byte index into txReport.
257
258 static void PrepareTransmitSegment(uchar index)
259 {
260 1 uchar cbData;
261 1 uchar i;
262 1
263 1 if (returnStatus)
264 1 {
265 2 // Prepare the whole status report on first call
266 2 if (index == 0)
267 2 {
268 3 status.u.cmd = CMD_STATUS;
269 3 status.u.status.currentCmd = currentCmd.u.cmd;
270 3 status.u.status.page = UPSD_xreg.PAGE;
271 3 status.u.status.vm = UPSD_xreg.VM;
272 3
273 3 memcpy(&txReport, &status, CMD_SIZE);
274 3 }
275 2
276 2 return;
277 2 }
278 1
279 1 switch (currentCmd.u.cmd)
280 1 {
281 2 case CMD_READ:
282 2
283 2 // First segment needs 0 command byte to indicate data
284 2 if (index == 0)
285 2 {
286 3 cbData = min(currentCmd.u.rw.nBytes, EP0_SIZE - 1);
287 3 index = 1;
288 3 txReport.u.cmd = 0;
289 3 }
290 2 else
291 2 {
292 3 cbData = min(currentCmd.u.rw.nBytes, EP0_SIZE);
293 3 }
294 2
295 2 ReadBufferFromFlash(
296 2 currentCmd.u.rw.address,
297 2 txReport.u.buffer + index,
298 2 cbData);
299 2
300 2 currentCmd.u.rw.address += cbData;
301 2 if ((currentCmd.u.rw.nBytes -= cbData) == 0)
302 2 {
303 3 // All done
C51 COMPILER V7.00 Beta 6 MAIN 02/19/2003 15:59:28 PAGE 6
304 3 currentCmd.u.cmd = 0;
305 3 }
306 2
307 2 break;
308 2
309 2 default:
310 2
311 2 for (i = 0; i < EP0_SIZE; i++)
312 2 {
313 3 txReport.u.buffer[index + i] = 0;
314 3 }
315 2 break;
316 2 }
317 1 }
318
319 /////////////////// OnReportSegmentReceived()
320 //
321 // Called as each EP0_SIZE segment of a report is received.
322
323 static void OnReportSegmentReceived(uchar cbReceived)
324 {
325 1 uchar cbData;
326 1 uchar index;
327 1
328 1 // If this is data coming in (not a new command) ...
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -