📄 upsd3400_usb_app.lst
字号:
C51 COMPILER V7.50 UPSD3400_USB_APP 09/13/2005 18:00:20 PAGE 1
C51 COMPILER V7.50, COMPILATION OF MODULE UPSD3400_USB_APP
OBJECT MODULE PLACED IN upsd3400_usb_app.OBJ
COMPILER INVOKED BY: C:\Keil\C51\BIN\C51.EXE upsd3400_usb_app.c BROWSE DEBUG OBJECTEXTEND SRC(.\upsd3400_usb_app.SRC)
line level source
1 /*------------------------------------------------------------------------------
2 upsd3400_usb_app.c
3
4 Version:
5 September 13, 2005 Ver 1.1 - Updated disclaimer, renamed file.
6 March 22, 2005 - Version 1.0 - Initial Release.
7
8 Description: USB Mass Storage Class application module.
9
10 Compilers:
11 Raisonance, Keil
12
13 THIS INFORMATION (or THIS SOFTWARE or THIS DOCUMENT) IS FOR GUIDANCE ONLY. ST
14 MAKES NO WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, WITH REGARD TO THIS
15 SOFTWARE nor for any infringement of patents or other rights of third parties
16 which may result from its use. ST MICROELECTRONICS SHALL NOT BE HELD LIABLE FOR
17 ANY DIRECT, INDIRECT INCIDENTAL OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY
18 CLAIMS ARISING IN CONNECTION WITH OR ARISING FROM THE FURNISHING, PERFORMANCE,
19 OR USE OF THIS SOFTWARE. Specifications mentioned in this publication are
20 subject to change without notice. This publication supersedes and replaces all
21 information previously supplied. STMicroelectronics products are not authorized
22 for use as critical components in life support devices or systems without the
23 express written approval of STMicroelectronics.
24 ------------------------------------------------------------------------------*/
25
26 #pragma NOAREGS
27
28 #include "upsd3400.h"
29 #include "upsd3400_hardware.h"
30 #include "upsd3400_usb.h"
31 #include "upsd3400_upsd_usb.h"
32 #include "upsd3400_usb_app.h"
33
34 #define USB_MS_SectorSize 512
35 #define USB_MS_UnitSize (USB_MS_SectorSize*9*64)
36
37 extern xdata PSD_REGS UPSD_xreg;
38
39 data unsigned char gbCBWCB[16];
40 data unsigned long int gdwCBWTag;
41 data unsigned long int gdwCBWDataTransferLength;
42 data unsigned char gbCBWFlags;
43 data unsigned char gbCBWLUN;
44 data unsigned char gbCBWCBLength;
45 data unsigned char gbCSWStatus;
46 data unsigned char gbBulkPipeStage;
47
48 static data unsigned int TransferLength;
49 static data unsigned int PhysicalDataAddr;
50
51 extern unsigned char data usbState, ep0state, ep1state;
52 extern setup_buffer setupPacket;
53
54 #define USB_FIFO ((unsigned char volatile xdata *) USB_BASE_ADDR)
55
C51 COMPILER V7.50 UPSD3400_USB_APP 09/13/2005 18:00:20 PAGE 2
56 #define UMSC_IN_ENDPOINT SELEP1
57 #define UMSC_OUT_ENDPOINT SELEP1
58
59 #define UMSC_MASK_IN_ENDPOINT IN1F
60 #define UMSC_MASK_OUT_ENDPOINT OUT1F
61
62
63 void ReturnCSW(void)
64 /******************************************************************************
65 Function : void ReturnCSW(void)
66 Parameters : none
67 Description: Handle CSW package.
68 Command Status Wrapper (CSW) shall start on a packet boundary
69 and shall end as a short packet with exactly 13 (0Dh) bytes
70 transferred. Fields appear aligned to byte offsets equal to
71 a multiple of their byte size. All CSW transfers shall
72 be ordered with the LSB (byte 0) first (little endian).
73 ******************************************************************************/
74 {
75 1 USEL = INDIR | UMSC_IN_ENDPOINT; //Select EP1 IN
76 1
77 1 /*dCSWSignature:
78 1 Signature that helps identify this data packet as a CSW. The signature field
79 1 shall contain the value 53425355h (little endian), indicating CSW. */
80 1 USB_FIFO[ 0] = 0x55; // dCSWSignature
81 1 USB_FIFO[ 1] = 0x53;
82 1 USB_FIFO[ 2] = 0x42;
83 1 USB_FIFO[ 3] = 0x53;
84 1 /*dCSWTag:
85 1 The device shall set this field to the value received in the dCBWTag
86 1 of the associated CBW.*/
87 1 USB_FIFO[ 4] = *((uchar *)&gdwCBWTag+3); // dCSWTag, LSB read first
88 1 USB_FIFO[ 5] = *((uchar *)&gdwCBWTag+2);
89 1 USB_FIFO[ 6] = *((uchar *)&gdwCBWTag+1);
90 1 USB_FIFO[ 7] = *((uchar *)&gdwCBWTag+0);
91 1 /*dCSWDataResidue:
92 1 For Data-Out the device shall report in the dCSWDataResidue the difference
93 1 between
94 1 the amount of data expected as stated in the dCBWDataTransferLength,
95 1 and the actual amount of data processed by the device.
96 1 For Data-In the device shall report in the dCSWDataResidue the difference
97 1 between the amount of data expected as stated in the dCBWDataTransferLength
98 1 and the actual amount of relevant data sent by the device.
99 1 The dCSWDataResidue shall not exceed the value sent in the dCBWDataTransferLength.
100 1 We return always zero, it is simple.
101 1 */
102 1 USB_FIFO[ 8] = 0x00; // dCSWDataResidue
103 1 USB_FIFO[ 9] = 0x00;
104 1 USB_FIFO[10] = 0x00;
105 1 USB_FIFO[11] = 0x00;
106 1 /*bCSWStatus:
107 1 bCSWStatus indicates the success or failure of the command.
108 1 The device shall set this byte to zero if the command completed successfully.
109 1 A non-zero value shall indicate a failure during command execution according
110 1 to the following table:
111 1 00h Command Passed ("good status")
112 1 01h Command Failed
113 1 02h Phase Error
114 1 03h and 04h Reserved (Obsolete)
115 1 05h to FFh Reserved
116 1 */
117 1 USB_FIFO[12] = gbCSWStatus; // bCSWStatus
C51 COMPILER V7.50 UPSD3400_USB_APP 09/13/2005 18:00:20 PAGE 3
118 1
119 1 USIZE = 0x0D; // fire !
120 1
121 1 gbBulkPipeStage = BS_CSW_DONE_PHASE;
122 1 }
123
124 void SpecificIncrementSectorAddress(void)
125 /******************************************************************************
126 Function : void SpecificIncrementSectorAddress(void)
127 Parameters : none
128 Description: Increments PhysicalDataAddr of 512b (1 sector),
129 adjust PAGE and TransferLength.
130 Used in Read and Write Commands
131 ******************************************************************************/
132 {
133 1 PhysicalDataAddr += 64; // next sector physical address
134 1 if (PhysicalDataAddr < 0x8000)
135 1 {
136 2 PhysicalDataAddr += 0x8000; // adjust address to the top half of memory
137 2 UPSD_xreg.PAGE += 1; // adjust PAGE
138 2 }
139 1 TransferLength--; // decrement sector counter
140 1 }
141
142 // Flash parameters
143 #define NVM_DATA_POLL 0x80 // flash status "data poll" bit at DQ7
144 #define NVM_DATA_TOGGLE 0x40 // flash status "toggle poll" bit at DQ6
145 #define NVM_ERROR 0x20 // flash status "error" bit at DQ5
146
147
148 BOOL EraseMainFlashSector(void)
149 /******************************************************************************
150 Function : BOOL EraseMainFlashSector()
151 Parameters : (unsigned int addr,unsigned char wbyte)
152 Description: Erases Main Flash Sector
153 ******************************************************************************/
154 {
155 1 data unsigned char err,error,done;
156 1 data unsigned char poll;
157 1
158 1 done = FALSE;
159 1 err = FALSE;
160 1
161 1 ((unsigned char volatile xdata *) 0x8000)[0x0555] = 0xAA;
162 1 // unlock main flash, write 0xAA to addess 0xX555
163 1 ((unsigned char volatile xdata *) 0x8000)[0x0AAA] = 0x55;
164 1 // unlock main flash, write 0x55 to addess 0xXAAA
165 1 ((unsigned char volatile xdata *) 0x8000)[0x0555] = 0x80;
166 1 // write 0x80 command to erase sector
167 1 ((unsigned char volatile xdata *) 0x8000)[0x0555] = 0xAA;
168 1 // continue unlock sequence
169 1 ((unsigned char volatile xdata *) 0x8000)[0x0AAA] = 0x55;
170 1 // continue unlock sequence
171 1 ((unsigned char volatile xdata *) 0x8000)[0x0000] = 0x30;
172 1 // write 0x30 to sector address to erase
173 1
174 1 do // now use dat polling method to verify successful erase
175 1 {
176 2 poll = ((unsigned char volatile xdata *) 0x8000)[0x0000];
177 2 // read flash status from any address
178 2 // within the defined flash address space
179 2 error = poll & NVM_ERROR; // save timeout error bit at DQ5
C51 COMPILER V7.50 UPSD3400_USB_APP 09/13/2005 18:00:20 PAGE 4
180 2 poll = poll & NVM_DATA_POLL; // look at D7
181 2 if (poll == NVM_DATA_POLL) // compare DQ7
182 2 {
183 3 done = TRUE; // bulk erase OK, indicate successful exit criteria
184 3 }
185 2 else if (error == NVM_ERROR) // check for timeout error
186 2 {
187 3 err = TRUE; // indicate timeout error occurred
188 3 }
189 2
190 2 }
191 1 while((done == FALSE) && (err == FALSE));
192 1
193 1
194 1 if (err == TRUE) // make sure timeout error and dat poll didn't
195 1 // occur simultaneously
196 1 {
197 2 poll = ((unsigned char volatile xdata *) 0x8000)[0x000];
198 2 // Read flash status again
199 2 poll = poll & NVM_DATA_POLL; // get DQ7 of poll byte read from flash
200 2 if (poll == NVM_DATA_POLL) // compare DQ7
201 2 {
202 3 done = TRUE; // the flash erased OK at the same
203 3 // time timout error occured, indicate successful exit criteria
204 3 err = FALSE;
205 3 }
206 2 else
207 2 {
208 3 ((unsigned char volatile xdata *) 0x8000)[0x0555] = 0xF0;
209 3 // reset the flash array (short reset instruction)
210 3 }
211 2 }
212 1
213 1 return err;
214 1 }
215
216
217
218
219
220
221
222 BOOL WriteToMainFlash(unsigned int addr,unsigned char wdat)
223 /******************************************************************************
224 Function : BOOL WriteToMainFlash()
225 Parameters : (unsigned int addr,unsigned char wbyte)
226 Description: Writes 1 byte to Main Flash
227 ******************************************************************************/
228 {
229 1 data unsigned char err,error,done;
230 1 data unsigned char poll,dat;
231 1
232 1 err = FALSE;
233 1
234 1 if ( ((unsigned char volatile xdata *) 0)[addr] != wdat)
235 1 {
236 2 done = FALSE;
237 2 ((unsigned char volatile xdata *) 0)[0x8555] = 0xAA;
238 2 // unlock main flash, write 0xAA to addess 0xX555
239 2 ((unsigned char volatile xdata *) 0)[0x8AAA] = 0x55;
240 2 // unlock main flash, write 0x55 to addess 0xXAAA
241 2 ((unsigned char volatile xdata *) 0)[0x8555] = 0xA0;
C51 COMPILER V7.50 UPSD3400_USB_APP 09/13/2005 18:00:20 PAGE 5
242 2 // write 0xA0 command to program
243 2 ((unsigned char volatile xdata *) 0)[addr] = wdat;
244 2 // write byte to flash
245 2 dat = wdat & NVM_DATA_POLL; // get bit DQ7 of the original dat
246 2
247 2 do // now use dat polling method to verify successful write
248 2 {
249 3 poll = ((unsigned char volatile xdata *) 0)[addr];
250 3 // Read the location that was just programmed
251 3 error = poll & NVM_ERROR; // save timeout error bit at DQ5
252 3 poll = poll & NVM_DATA_POLL; // get DQ7 of poll byte read from flash
253 3
254 3 if (dat == poll) // compare DQ7
255 3 {
256 4 done = TRUE; // dat byte programmed into flash OK,
257 4 // indicate successful exit criteria
258 4 }
259 3 else if (error == NVM_ERROR ) // check for timeout error
260 3 {
261 4 err = TRUE; // indicate timeout error occurred
262 4 }
263 3
264 3 }
265 2 while((done == FALSE) && (err == FALSE));
266 2
267 2 if (err == TRUE) // make sure timeout error and dat poll didn't
268 2 // occur simultaneously
269 2 {
270 3 poll = ((unsigned char volatile xdata *) 0)[addr];
271 3 // Read location in flash again
272 3 poll = poll & NVM_DATA_POLL; // get DQ7 of poll byte read from flash
273 3 if (dat == poll) // compare DQ7
274 3 {
275 4 done = TRUE; // data byte programmed into flash OK at the same
276 4 // time timout error occured, indicate successful exit criteria
277 4 err = FALSE;
278 4 }
279 3 else
280 3 {
281 4 ((unsigned char volatile xdata *) 0)[0x8555] = 0xF0;
282 4 // reset the flash array (short reset instruction)
283 4 }
284 3 }
285 2 }
286 1 return err;
287 1 }
288
289
290
291
292
293
294
295 void WriteBufferToFlash(void)
296 /******************************************************************************
297 Function : void WriteBufferToFlash()
298 Parameters : (none)
299 Description: Receives (writes) TransferLength sectors with length of 512 Bytes
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -