BOOTLOADER
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros
usbdevice.c
Go to the documentation of this file.
1 /******************************************************
2 * Function: s3c2440 usbdevice driver
3 *
4 * File: usbdevice.c
5 * Author: Book Chen
6 * Date: 2008.07.18
7 *******************************************************
8 */
9 #include "includes.h"
10 
11 #define UsbDeviceIdleState 0
12 #define UsbDeviceInitial0State 1
13 #define UsbDeviceInitial1State 2
14 #define UsbDeviceInitial2State 3
15 #define UsbDeviceDataState 4
16 
17 #define Ep0IdleState 0
18 
19 //NOTE: The ep0State value in a same group should be added by 1.
20 #define EP0_STATE_GD_DEV_0 (10) //10-10=0
21 #define EP0_STATE_GD_DEV_1 (11) //11-10=1
22 #define EP0_STATE_GD_DEV_2 (12) //12-10=2
23 
24 #define EP0_STATE_GD_CFG_0 (20)
25 #define EP0_STATE_GD_CFG_1 (21)
26 #define EP0_STATE_GD_CFG_2 (22)
27 #define EP0_STATE_GD_CFG_3 (23)
28 #define EP0_STATE_GD_CFG_4 (24)
29 
30 #define EP0_STATE_GD_STR_I0 (30)
31 #define EP0_STATE_GD_STR_I1 (31)
32 #define EP0_STATE_GD_STR_I2 (32)
33 
34 #define EP0_STATE_GD_CFG_ONLY_0 (40)
35 #define EP0_STATE_GD_CFG_ONLY_1 (41)
36 #define EP0_STATE_GD_IF_ONLY_0 (42)
37 #define EP0_STATE_GD_IF_ONLY_1 (43)
38 #define EP0_STATE_GD_EP0_ONLY_0 (44)
39 #define EP0_STATE_GD_EP1_ONLY_0 (45)
40 
41 #define USBDEVICE_DOWNLOAD_ADDRESS 0x30400000
42 #define USBDEVICE_UPLOAD_ADDRESS 0x30300000
43 
44 //======
45 // Endpoint 0
46 //======
47 #define CLR_EP0_OUT_PKT_RDY() rEP0_CSR=(Ep0Csr&(~EP0_WR_BITS)| \
48  EP0_SERVICED_OUT_PKT_RDY)
49 #define CLR_EP0_OUTPKTRDY_DATAEND() rEP0_CSR=( Ep0Csr & (~EP0_WR_BITS)| \
50  (EP0_SERVICED_OUT_PKT_RDY|EP0_DATA_END) )
51 #define SET_EP0_IN_PKT_RDY() rEP0_CSR=(Ep0Csr & (~EP0_WR_BITS)| \
52  (EP0_IN_PKT_READY) )
53 #define SET_EP0_INPKTRDY_DATAEND() rEP0_CSR=(Ep0Csr&(~EP0_WR_BITS)| \
54  (EP0_IN_PKT_READY|EP0_DATA_END))
55 #define CLR_EP0_SETUP_END() rEP0_CSR=(Ep0Csr&(~EP0_WR_BITS)| \
56  (EP0_SERVICED_SETUP_END))
57 #define CLR_EP0_SENT_STALL() rEP0_CSR=(Ep0Csr & (~EP0_WR_BITS)& \
58  (~EP0_SENT_STALL) )
59 
60 #define FLUSH_EP0_FIFO() {register i;while(rOUT_FIFO_CNT1_REG)i=rEP0_FIFO;}
61 
62 //======
63 // Endpoint 1
64 //======
65 #define SET_EP1_IN_PKT_READY() rIN_CSR1_REG=(InCsr1Reg&(~EPI_WR_BITS)\
66  |EPI_IN_PKT_READY )
67 #define SET_EP1_SEND_STALL() rIN_CSR1_REG=(InCsr1Reg&(~EPI_WR_BITS)\
68  |EPI_SEND_STALL))
69 #define CLR_EP1_SENT_STALL() rIN_CSR1_REG=(InCsr1Reg&(~EPI_WR_BITS)\
70  &(~EPI_SENT_STALL))
71 #define FLUSH_EP1_FIFO() rIN_CSR1_REG=(InCsr1Reg&(~EPI_WR_BITS)\
72  |EPI_FIFO_FLUSH))
73 
74 //======
75 // Endpoint 3
76 //======
77 #define CLR_EP3_OUT_PKT_READY() rOUT_CSR1_REG=(OutCsr3 &(~ EPO_WR_BITS)\
78  &(~EPO_OUT_PKT_READY) )
79 #define SET_EP3_SEND_STALL() rOUT_CSR1_REG=(OutCsr3 & (~EPO_WR_BITS)\
80  |EPO_SEND_STALL))
81 #define CLR_EP3_SENT_STALL() rOUT_CSR1_REG=(OutCsr3&(~EPO_WR_BITS)\
82  &(~EPO_SENT_STALL))
83 #define FLUSH_EP3_FIFO() rOUT_CSR1_REG=(OutCsr3 & (~EPO_WR_BITS)\
84  |EPO_FIFO_FLUSH) )
85 
92 
93 static const INT8U DescripString0[]={4,STRING_TYPE,LANGID_US_L,LANGID_US_H}; //codes representing languages
94 static const INT8U DescripString1[]={ // manufacturer string
95  (0x14+2),STRING_TYPE,
96  'S',0x0,'y',0x0,'s',0x0,'t',0x0,'e',0x0,'m',0x0,' ',0x0,'M',0x0,
97  'C',0x0,'U',0x0};
98 
99 static const INT8U DescripString2[]={ //product string
100  (0x2a+2),STRING_TYPE,
101  'S',0x0,'E',0x0,'C',0x0,' ',0x0,'S',0x0,'3',0x0,'C',0x0,'2',0x0,
102  '4',0x0,'1',0x0,'0',0x0,'X',0x0,' ',0x0,'T',0x0,'e',0x0,'s',0x0,
103  't',0x0,' ',0x0,'B',0x0,'/',0x0,'D',0x0};
104 
105 void F_UsbDeviceInit(void);
106 void F_UsbDeviceSvc(void);
111 void F_UsbDeviceHwInit(void);
112 void F_UsbDeviceDescripInit(INT8U Function);
114 void F_UsbDeviceEp0Init(void);
115 void F_UsbDeviceEp1Init(void);
116 void F_UsbDeviceEp2Init(void);
117 void F_UsbDeviceEp3Init(void);
118 void F_UsbDeviceEp4Init(void);
119 void F_UsbDeviceEp0Isr(void);
120 void F_UsbDeviceEp1Isr(void);
121 void F_UsbDeviceEp2Isr(void);
122 void F_UsbDeviceEp3Isr(void);
123 void F_UsbDeviceEp4Isr(void);
124 void __irq UsbDeviceIsr(void);
125 
126 void F_UsbDeviceInit(void){
132  UsbDeviceCtrl.Function=0;
134  UsbDeviceCtrl.TxAddress=USBDEVICE_UPLOAD_ADDRESS;
135  UsbDeviceCtrl.ConfigStatus=TRUE;
136  UsbDeviceCtrl.UserId=ID_USBDEVICE;
137  UsbDeviceCtrl.InUse=FALSE;
138  UsbDeviceCtrl.State=UsbDeviceIdleState;
139 }
140 void F_UsbDeviceSvc(void){
141  switch(UsbDeviceCtrl.State){
142  case UsbDeviceIdleState:
143  break;
145  //To enhance the USB signal quality.
146  //CLKOUT 0,1=OUTPUT to reduce the power consumption.
147  rGPHCON=(rGPHCON&~(0xf<<18))|(0x5<<18);
148  rGPGCON&=0xfff3ffff; //GPG9 input
149  //F_ClockUpllSet(); // set usb pll 48mhz
150  F_UsbDeviceHwInit(); // hardware initial
151  F_UsbDeviceDescripInit(1); // description initial
152  UsbDeviceCtrl.State=UsbDeviceInitial1State;
153  break;
155  //Delay(100);
156  rGPGCON |= 0x00040000;
157  rGPGDAT |= 0x0200; //GPG9 ouput 1
158  //pISR_USBD =(unsigned)IsrUsbd;
159  //ClearPending(BIT_USBD);
160  //EnableIrq(BIT_USBD);
161  UsbDeviceCtrl.State=UsbDeviceInitial2State;
162  break;
165  UsbDeviceCtrl.State=UsbDeviceDataState;
166  }
167  break;
168  case UsbDeviceDataState:
169  UsbDeviceCtrl.State=UsbDeviceIdleState;
170  break;
171  }
172 }
174  if(UsbDeviceCtrl.InUse==TRUE) return TRUE;
175  return FALSE;
176 }
178  if(UsbDeviceCtrl.InUse==FALSE){
179  UsbDeviceCtrl.InUse=TRUE;
180  UsbDeviceCtrl.UserId=UserId;
181  return TRUE;
182  }
183  return FALSE;
184 }
186  if((UsbDeviceCtrl.InUse==TRUE)&&(UsbDeviceCtrl.UserId==UserId)){
187  UsbDeviceCtrl.InUse=FALSE;
188  return TRUE;
189  }
190  return FALSE;
191 }
193  if((UsbDeviceCtrl.InUse==TRUE)&&(UsbDeviceCtrl.UserId==UserId)){
194  UsbDeviceCtrl.State=UsbDeviceInitial0State;
195  return TRUE;
196  }
197  return FALSE;
198 }
200  UsbDeviceCtrl.Function=Function-1;
201  //Standard device descriptor
202  UsbDeviceCtrl.DeviceDescrip.bLength=0x12; //EP0_DEV_DESC_SIZE=0x12 bytes
204  UsbDeviceCtrl.DeviceDescrip.bcdUSBL=0x10;
205  UsbDeviceCtrl.DeviceDescrip.bcdUSBH=0x01; //Ver 1.10
206  UsbDeviceCtrl.DeviceDescrip.bDeviceClass=0xFF; //0x0
207  UsbDeviceCtrl.DeviceDescrip.bDeviceSubClass=0x0;
208  UsbDeviceCtrl.DeviceDescrip.bDeviceProtocol=0x0;
209  UsbDeviceCtrl.DeviceDescrip.bMaxPacketSize0=0x8;
210  UsbDeviceCtrl.DeviceDescrip.idVendorL=0x45;
211  UsbDeviceCtrl.DeviceDescrip.idVendorH=0x53;
212  if(Function==1)
213  UsbDeviceCtrl.DeviceDescrip.idProductL=0x34;
214  else
215  UsbDeviceCtrl.DeviceDescrip.idProductL=0x33;
216  UsbDeviceCtrl.DeviceDescrip.idProductH=0x12;
217  UsbDeviceCtrl.DeviceDescrip.bcdDeviceL=0x00;
218  UsbDeviceCtrl.DeviceDescrip.bcdDeviceH=0x01;
219  UsbDeviceCtrl.DeviceDescrip.iManufacturer=0x1; //index of string descriptor
220  UsbDeviceCtrl.DeviceDescrip.iProduct=0x2; //index of string descriptor
221  UsbDeviceCtrl.DeviceDescrip.iSerialNumber=0x0;
222  UsbDeviceCtrl.DeviceDescrip.bNumConfigurations=0x1;
223  //Standard configuration descriptor
224  UsbDeviceCtrl.ConfigurationDescrip.bLength=0x9;
226  UsbDeviceCtrl.ConfigurationDescrip.wTotalLengthL=0x20; //<cfg desc>+<if desc>+<endp0 desc>+<endp1 desc>
227  UsbDeviceCtrl.ConfigurationDescrip.wTotalLengthH=0;
228  UsbDeviceCtrl.ConfigurationDescrip.bNumInterfaces=1;
229  //descConf.bConfigurationValue=2; //why 2? There's no reason.
230  UsbDeviceCtrl.ConfigurationDescrip.bConfigurationValue=1;
231  UsbDeviceCtrl.ConfigurationDescrip.iConfiguration=0;
232  UsbDeviceCtrl.ConfigurationDescrip.bmAttributes=CONF_ATTR_DEFAULT; //bus powered only.
233  UsbDeviceCtrl.ConfigurationDescrip.maxPower=25; //draws 50mA current from the USB bus.
234  //Standard interface descriptor
235  UsbDeviceCtrl.InterfaceDescrip.bLength=0x9;
237  UsbDeviceCtrl.InterfaceDescrip.bInterfaceNumber=0x0;
238  UsbDeviceCtrl.InterfaceDescrip.bAlternateSetting=0x0; //?
239  UsbDeviceCtrl.InterfaceDescrip.bNumEndpoints=2; //# of endpoints except EP0
240  UsbDeviceCtrl.InterfaceDescrip.bInterfaceClass=0xff; //0x0 ?
241  UsbDeviceCtrl.InterfaceDescrip.bInterfaceSubClass=0x0;
242  UsbDeviceCtrl.InterfaceDescrip.bInterfaceProtocol=0x0;
243  UsbDeviceCtrl.InterfaceDescrip.iInterface=0x0;
244  //Standard endpoint0 descriptor
245  UsbDeviceCtrl.EndpointDescrip0.bLength=0x7;
247  UsbDeviceCtrl.EndpointDescrip0.bEndpointAddress=1|EP_ADDR_IN; // 2400Xendpoint 1 is IN endpoint.
249  UsbDeviceCtrl.EndpointDescrip0.wMaxPacketSizeL=EP1_PKT_SIZE; //64
250  UsbDeviceCtrl.EndpointDescrip0.wMaxPacketSizeH=0x0;
251  UsbDeviceCtrl.EndpointDescrip0.bInterval=0x0; //not used
252  //Standard endpoint1 descriptor
253  UsbDeviceCtrl.EndpointDescrip1.bLength=0x7;
255  UsbDeviceCtrl.EndpointDescrip1.bEndpointAddress=3|EP_ADDR_OUT; // 2400X endpoint 3 is OUT endpoint.
257  UsbDeviceCtrl.EndpointDescrip1.wMaxPacketSizeL=EP3_PKT_SIZE; //64
258  UsbDeviceCtrl.EndpointDescrip1.wMaxPacketSizeH=0x0;
259  UsbDeviceCtrl.EndpointDescrip1.bInterval=0x0; //not used
260 }
261 void F_UsbDeviceHwInit(void){
262 // *** End point information ***
263 // EP0: control
264 // EP1: bulk in end point
265 // EP2: not used
266 // EP3: bulk out end point
267 // EP4: not used
268 
269  rPWR_REG=PWR_REG_DEFAULT_VALUE; //disable suspend mode
270 
271  rINDEX_REG=0;
272  rMAXP_REG=FIFO_SIZE_8; //EP0 max packit size = 8
274  //EP0:clear OUT_PKT_RDY & SETUP_END
275  rINDEX_REG=1;
276  #if (EP1_PKT_SIZE==32)
277  rMAXP_REG=FIFO_SIZE_32; //EP1:max packit size = 32
278  #else
279  rMAXP_REG=FIFO_SIZE_64; //EP1:max packit size = 64
280  #endif
282  rIN_CSR2_REG=EPI_MODE_IN|EPI_IN_DMA_INT_MASK|EPI_BULK; //IN mode, IN_DMA_INT=masked
285 
286  rINDEX_REG=2;
287  rMAXP_REG=FIFO_SIZE_64; //EP2:max packit size = 64
288  rIN_CSR1_REG=EPI_FIFO_FLUSH|EPI_CDT|EPI_BULK; //EPI_BULK in IN_CSR1???
289  rIN_CSR2_REG=EPI_MODE_IN|EPI_IN_DMA_INT_MASK; //IN mode, IN_DMA_INT=masked, disable interrupt
292 
293  rINDEX_REG=3;
294  #if (EP3_PKT_SIZE==32)
295  rMAXP_REG=FIFO_SIZE_32; //EP3:max packit size = 32
296  #else
297  rMAXP_REG=FIFO_SIZE_64; //EP3:max packit size = 64
298  #endif
300  rIN_CSR2_REG=EPI_MODE_OUT|EPI_IN_DMA_INT_MASK; //OUT mode, IN_DMA_INT=masked
302  //clear OUT_PKT_RDY, data_toggle_bit.
303  //The data toggle bit should be cleared when initialization.
305 
306  rINDEX_REG=4;
307  rMAXP_REG=FIFO_SIZE_64; //EP4:max packit size = 64
309  rIN_CSR2_REG=EPI_MODE_OUT|EPI_IN_DMA_INT_MASK; //OUT mode, IN_DMA_INT=masked
311  //clear OUT_PKT_RDY, data_toggle_bit.
312  //The data toggle bit should be cleared when initialization.
314 
317  //Clear all usbd pending bits
318 
319  //EP0,1,3 & reset interrupt are enabled
322  //ep0State=EP0_STATE_INIT;
323 }
324 //======
325 // end point 0
326 //======
328  EndPoint0Ctrl.Status=0;
329  EndPoint0Ctrl.State=Ep0IdleState;
330 }
332  INT32U Index;
333 
334  for(Index=0;Index<Length;Index++){
335  pData[Index]=(INT8U)rEP0_FIFO;
336  }
337 }
339  INT32U Index;
340 
341  for(Index=0;Index<Length;Index++){
342  rEP0_FIFO=pData[Index];
343  }
344 }
345 void F_UsbDeviceEp0Isr(void){
346  INT8U Ep0Csr;
347 
348  rINDEX_REG=0;
349  Ep0Csr=rEP0_CSR;
350  if(Ep0Csr&EP0_SETUP_END){
352  if(Ep0Csr&EP0_OUT_PKT_READY){
353  FLUSH_EP0_FIFO();
355  }
356  UsbDeviceCtrl.Ep0State=Ep0IdleState;
357  return;
358  }
359  if(Ep0Csr&EP0_SENT_STALL)
360  {
362  if(Ep0Csr&EP0_OUT_PKT_READY){
364  }
365  UsbDeviceCtrl.Ep0State=Ep0IdleState;
366  return;
367  }
368  if((Ep0Csr&EP0_OUT_PKT_READY)&&
369  (UsbDeviceCtrl.Ep0State==Ep0IdleState)){
371  switch(UsbDeviceCtrl.SetupData.bRequest){
372  case GET_DESCRIPTOR:
373  switch(UsbDeviceCtrl.SetupData.bValueH){
374  case DEVICE_TYPE:
376  UsbDeviceCtrl.Ep0State=EP0_STATE_GD_DEV_0;
377  break;
378  case CONFIGURATION_TYPE:
380  //bLengthH should be used for bLength=0x209 at WIN2K.
381  if(((UsbDeviceCtrl.SetupData.bLengthH<<8)+UsbDeviceCtrl.SetupData.bLengthL)>0x9)
382  UsbDeviceCtrl.Ep0State=EP0_STATE_GD_CFG_0; //for WIN98,WIN2K
383  else
384  UsbDeviceCtrl.Ep0State=EP0_STATE_GD_CFG_ONLY_0; //for WIN2K
385  break;
386  case STRING_TYPE:
388  switch(UsbDeviceCtrl.SetupData.bValueL){
389  case 0:
390  UsbDeviceCtrl.Ep0State=EP0_STATE_GD_STR_I0;
391  break;
392  case 1:
393  UsbDeviceCtrl.Ep0State=EP0_STATE_GD_STR_I1;
394  break;
395  case 2:
396  UsbDeviceCtrl.Ep0State=EP0_STATE_GD_STR_I2;
397  break;
398  default:
399  break;
400  }
401  UsbDeviceCtrl.Ep0SubState=0;
402  break;
403  case INTERFACE_TYPE:
405  UsbDeviceCtrl.Ep0State=EP0_STATE_GD_IF_ONLY_0; //for WIN98
406  break;
407  case ENDPOINT_TYPE:
409  switch(UsbDeviceCtrl.SetupData.bValueL&0xf)
410  {
411  case 0:
412  UsbDeviceCtrl.Ep0State=EP0_STATE_GD_EP0_ONLY_0;
413  break;
414  case 1:
415  UsbDeviceCtrl.Ep0State=EP0_STATE_GD_EP1_ONLY_0;
416  break;
417  default:
418  break;
419  }
420  break;
421  default:
422  break;
423  }
424  break;
425  case SET_ADDRESS:
426  rFUNC_ADDR_REG=UsbDeviceCtrl.SetupData.bValueL|0x80;
427  CLR_EP0_OUTPKTRDY_DATAEND(); //Because of no data control transfers.
428  UsbDeviceCtrl.Ep0State=Ep0IdleState;
429  break;
430  case SET_CONFIGURATION:
431  CLR_EP0_OUTPKTRDY_DATAEND(); //Because of no data control transfers.
432  UsbDeviceCtrl.Ep0State=Ep0IdleState;
433  UsbDeviceCtrl.ConfigStatus=TRUE;
434  break;
435  default:
436  CLR_EP0_OUTPKTRDY_DATAEND(); //Because of no data control transfers.
437  UsbDeviceCtrl.Ep0State=Ep0IdleState;
438  break;
439  }
440  }
441  switch(UsbDeviceCtrl.Ep0State){
442  case Ep0IdleState:
443  break;
444 
445  case EP0_STATE_GD_DEV_0: //=== GET_DESCRIPTOR:DEVICE ===
446  F_UsbDeviceEp0PacketWrite((INT8U *)&UsbDeviceCtrl.DeviceDescrip,8); //EP0_PKT_SIZE
448  UsbDeviceCtrl.Ep0State=EP0_STATE_GD_DEV_1;
449  break;
450 
451  case EP0_STATE_GD_DEV_1:
452  F_UsbDeviceEp0PacketWrite((INT8U *)&UsbDeviceCtrl.DeviceDescrip+8,8);
454  UsbDeviceCtrl.Ep0State=EP0_STATE_GD_DEV_2;
455  break;
456 
457  case EP0_STATE_GD_DEV_2:
458  F_UsbDeviceEp0PacketWrite((INT8U *)&UsbDeviceCtrl.DeviceDescrip+16,2);
460  UsbDeviceCtrl.Ep0State=Ep0IdleState;
461  break;
462 
463  //=== GET_DESCRIPTOR:CONFIGURATION+INTERFACE+ENDPOINT0+ENDPOINT1 ===
464  //Windows98 gets these 4 descriptors all together by issuing only a request.
465  //Windows2000 gets each descriptor seperately.
466  case EP0_STATE_GD_CFG_0:
467  F_UsbDeviceEp0PacketWrite((INT8U *)&UsbDeviceCtrl.ConfigurationDescrip,8); //EP0_PKT_SIZE
469  UsbDeviceCtrl.Ep0State=EP0_STATE_GD_CFG_1;
470  break;
471 
472  case EP0_STATE_GD_CFG_1:
473  F_UsbDeviceEp0PacketWrite((INT8U *)&UsbDeviceCtrl.ConfigurationDescrip+8,1);
474  F_UsbDeviceEp0PacketWrite((INT8U *)&UsbDeviceCtrl.InterfaceDescrip,7);
476  UsbDeviceCtrl.Ep0State=EP0_STATE_GD_CFG_2;
477  break;
478 
479  case EP0_STATE_GD_CFG_2:
480  F_UsbDeviceEp0PacketWrite((INT8U *)&UsbDeviceCtrl.InterfaceDescrip+7,2);
481  F_UsbDeviceEp0PacketWrite((INT8U *)&UsbDeviceCtrl.EndpointDescrip0,6);
483  UsbDeviceCtrl.Ep0State=EP0_STATE_GD_CFG_3;
484  break;
485 
486  case EP0_STATE_GD_CFG_3:
487  F_UsbDeviceEp0PacketWrite((INT8U *)&UsbDeviceCtrl.EndpointDescrip0+6,1);
488  F_UsbDeviceEp0PacketWrite((INT8U *)&UsbDeviceCtrl.EndpointDescrip1,7);
490  UsbDeviceCtrl.Ep0State=EP0_STATE_GD_CFG_4;
491  break;
492 
493  case EP0_STATE_GD_CFG_4:
495  UsbDeviceCtrl.Ep0State=Ep0IdleState;
496  break;
497 
498  case EP0_STATE_GD_CFG_ONLY_0: //=== GET_DESCRIPTOR:CONFIGURATION ONLY===
499  F_UsbDeviceEp0PacketWrite((INT8U *)&UsbDeviceCtrl.ConfigurationDescrip,8); //EP0_PKT_SIZE
501  UsbDeviceCtrl.Ep0State=EP0_STATE_GD_CFG_ONLY_1;
502  break;
503 
505  F_UsbDeviceEp0PacketWrite((INT8U *)&UsbDeviceCtrl.ConfigurationDescrip+8,1);
507  UsbDeviceCtrl.Ep0State=Ep0IdleState;
508  break;
509 
510  case EP0_STATE_GD_IF_ONLY_0: //=== GET_DESCRIPTOR:INTERFACE ONLY===
511  F_UsbDeviceEp0PacketWrite((INT8U *)&UsbDeviceCtrl.InterfaceDescrip,8);
513  UsbDeviceCtrl.Ep0State=EP0_STATE_GD_IF_ONLY_1;
514  break;
515 
517  F_UsbDeviceEp0PacketWrite((INT8U *)&UsbDeviceCtrl.InterfaceDescrip+8,1);
519  UsbDeviceCtrl.Ep0State=Ep0IdleState;
520  break;
521 
522  case EP0_STATE_GD_EP0_ONLY_0: //=== GET_DESCRIPTOR:ENDPOINT 0 ONLY===
523  F_UsbDeviceEp0PacketWrite((INT8U *)&UsbDeviceCtrl.EndpointDescrip0,7);
525  UsbDeviceCtrl.Ep0State=Ep0IdleState;
526  break;
527 
528  case EP0_STATE_GD_EP1_ONLY_0: //=== GET_DESCRIPTOR:ENDPOINT 1 ONLY===
529  F_UsbDeviceEp0PacketWrite((INT8U *)&UsbDeviceCtrl.EndpointDescrip1,7);
531  UsbDeviceCtrl.Ep0State=Ep0IdleState;
532  break;
533 
534 
535  case EP0_STATE_GD_STR_I0: //=== GET_DESCRIPTOR:STRING ===
536  F_UsbDeviceEp0PacketWrite((INT8U *)DescripString0,4);
538  UsbDeviceCtrl.Ep0State=Ep0IdleState;
539  UsbDeviceCtrl.Ep0SubState=0;
540  break;
541 
542  case EP0_STATE_GD_STR_I1:
543  if((UsbDeviceCtrl.Ep0SubState*EP0_PKT_SIZE+EP0_PKT_SIZE)<sizeof(DescripString1)){
544  F_UsbDeviceEp0PacketWrite((INT8U *)DescripString1+(UsbDeviceCtrl.Ep0SubState*EP0_PKT_SIZE),
545  EP0_PKT_SIZE);
547  UsbDeviceCtrl.Ep0State=EP0_STATE_GD_STR_I1;
548  UsbDeviceCtrl.Ep0SubState++;
549  }
550  else{
551  F_UsbDeviceEp0PacketWrite((INT8U *)DescripString1+(UsbDeviceCtrl.Ep0SubState*EP0_PKT_SIZE),
552  sizeof(DescripString1)-(UsbDeviceCtrl.Ep0SubState*EP0_PKT_SIZE));
554  UsbDeviceCtrl.Ep0State=Ep0IdleState;
555  UsbDeviceCtrl.Ep0SubState=0;
556  }
557  break;
558  case EP0_STATE_GD_STR_I2:
559  if((UsbDeviceCtrl.Ep0SubState*EP0_PKT_SIZE+EP0_PKT_SIZE)<sizeof(DescripString2)){
560  F_UsbDeviceEp0PacketWrite((INT8U *)DescripString2+(UsbDeviceCtrl.Ep0SubState*EP0_PKT_SIZE),
561  EP0_PKT_SIZE);
563  UsbDeviceCtrl.Ep0State=EP0_STATE_GD_STR_I2;
564  UsbDeviceCtrl.Ep0SubState++;
565  }
566  else{
567  F_UsbDeviceEp0PacketWrite((INT8U *)DescripString2+(UsbDeviceCtrl.Ep0SubState*EP0_PKT_SIZE),
568  sizeof(DescripString2)-(UsbDeviceCtrl.Ep0SubState*EP0_PKT_SIZE));
570  UsbDeviceCtrl.Ep0State=Ep0IdleState;
571  UsbDeviceCtrl.Ep0SubState=0;
572  }
573  break;
574  default:
575  break;
576  }
577 }
578 //======
579 // end point 1
580 //======
582  EndPoint1Ctrl.TransferIndex=0;
583  EndPoint1Ctrl.Status=0;
584  EndPoint1Ctrl.State=Ep0IdleState;
585 }
587  INT32U Index;
588 
589  for(Index=0;Index<Length;Index++){
590  rEP0_FIFO=pData[Index];
591  }
592 }
594  INT8U InCsr1Reg;
595  INT8U i;
596 
597  rINDEX_REG=1;
598  InCsr1Reg=rIN_CSR1_REG;
599  for(i=0;i<EP1_PKT_SIZE;i++)
600  EndPoint1Ctrl.Data[i]=(INT8U)(EndPoint1Ctrl.TransferIndex+i);
601  F_UsbDeviceEp1PacketWrite(UsbDeviceCtrl.pTxData, EP1_PKT_SIZE);
602  UsbDeviceCtrl.pTxData+=EP1_PKT_SIZE;
603  if(UsbDeviceCtrl.TxLength>EP1_PKT_SIZE)
604  UsbDeviceCtrl.TxLength-=EP1_PKT_SIZE;
605  else UsbDeviceCtrl.TxLength=0;
606  if(UsbDeviceCtrl.TxLength==0) EndPoint3Ctrl.Status=0; // tx done
608 }
609 void F_UsbDeviceEp1Isr(void){
610  INT8U InCsr1Reg;
611 
612  rINDEX_REG=1;
613  InCsr1Reg=rIN_CSR1_REG;
614  if(InCsr1Reg&EPI_SENT_STALL){
616  return;
617  }
618  EndPoint1Ctrl.TransferIndex++;
619  if(EndPoint3Ctrl.ActionType==0x01234567)
621 }
622 //======
623 // end point 2
624 //======
626  EndPoint2Ctrl.Status=0;
627  EndPoint2Ctrl.State=Ep0IdleState;
628 }
630  INT32U Index;
631 
632  for(Index=0;Index<Length;Index++)
633  {
634  rEP0_FIFO=pData[Index];
635  }
636 }
637 //======
638 // end point 3
639 //======
641  EndPoint3Ctrl.ActionType=0;
642  EndPoint3Ctrl.Status=0;
643  EndPoint3Ctrl.State=Ep0IdleState;
644 }
646  INT32U Index;
647 
648  for(Index=0;Index<Length;Index++)
649  {
650  pData[Index]=(INT8U)rEP3_FIFO;
651  }
652 }
654  INT32 i;
655 
656  for(i=0;i<Length;i++){
657  pData[i]=(INT8U)rEP3_FIFO;
658  EndPoint3Ctrl.CheckSum+=pData[i];
659  }
660 }
661 void F_UsbDeviceEp3Isr(void){
662  INT8U OutCsr3;
663  INT8U Packet[16];
664  INT8U *pPacket;
665  INT32U FifoByteCount;
666 
667 
668  rINDEX_REG=3;
669  OutCsr3=rOUT_CSR1_REG;
670  if(OutCsr3&EPO_OUT_PKT_READY){
671  FifoByteCount=rOUT_FIFO_CNT1_REG;
672  if(UsbDeviceCtrl.Function){
673  F_UsbDeviceEp3PacketRead(EndPoint3Ctrl.Data, FifoByteCount);
675  if(*(INT32U *)EndPoint3Ctrl.Data==0x01234567)
676  {
677  EndPoint3Ctrl.ActionType=0x01234567;
678  UsbDeviceCtrl.TxAddress=USBDEVICE_UPLOAD_ADDRESS;
679  UsbDeviceCtrl.pTxData=(INT8U *)USBDEVICE_UPLOAD_ADDRESS;
680  UsbDeviceCtrl.TxLength=*(INT32U *)(EndPoint3Ctrl.Data+4);
682  }
683  return;
684  }
685  if(UsbDeviceCtrl.RxLength==0){
686  EndPoint3Ctrl.CheckSum=0;
687  pPacket=Packet;
688  F_UsbDeviceEp3PacketRead(pPacket,8);
689  UsbDeviceCtrl.RxAddress=*(INT32U *)pPacket;
690  UsbDeviceCtrl.RxLength=*(INT32U *)(pPacket+4);
691  UsbDeviceCtrl.pRxData=(INT8U *)UsbDeviceCtrl.RxAddress;
692  F_UsbDeviceEp3CheckSumGet(UsbDeviceCtrl.pRxData,FifoByteCount-8); //The first 8-bytes are deleted.
693  UsbDeviceCtrl.pRxData+=FifoByteCount-8;
694 
695  #if USBDMA
696  //CLR_EP3_OUT_PKT_READY() is not executed.
697  //So, USBD may generate NAK until DMA2 is configured for USB_EP3;
699  return;
700  #endif
701  }
702  else
703  {
704  F_UsbDeviceEp3CheckSumGet(UsbDeviceCtrl.pRxData,FifoByteCount);
705  UsbDeviceCtrl.pRxData+=FifoByteCount; //fifoCnt=64
706  }
708  return;
709  }
710  //I think that EPO_SENT_STALL will not be set to 1.
711  if(OutCsr3&EPO_SENT_STALL){
713  return;
714  }
715 }
716 //======
717 // end point 4
718 //======
720  EndPoint4Ctrl.Status=0;
721  EndPoint4Ctrl.State=Ep0IdleState;
722 }
724  INT32U Index;
725 
726  for(Index=0;Index<Length;Index++)
727  {
728  pData[Index]=(INT8U)rEP4_FIFO;
729  }
730 }
732  if(UsbDeviceCtrl.ConfigStatus==TRUE) return TRUE;
733  else return FALSE;
734 }
735 void F_UsbDeviceEp2Isr(void){}
736 void F_UsbDeviceEp4Isr(void){}
737 void __irq UsbDeviceIsr(void){
738  INT8U UsbIntReg;
739  INT8U EndpointIntReg;
740  INT8U IndexReg;
741 
742  IndexReg=rINDEX_REG;
743  UsbIntReg=rUSB_INT_REG;
744  EndpointIntReg=rEP_INT_REG;
745  // usb device event
746  if(UsbIntReg&SUSPEND_INT) rUSB_INT_REG = SUSPEND_INT;
747  if(UsbIntReg&RESUME_INT) rUSB_INT_REG = RESUME_INT;
748  if(UsbIntReg&RESET_INT){
751  }
752  // usb endpoint event
753  if(EndpointIntReg&EP0_INT){
755  F_UsbDeviceEp0Isr(); // default pipe...descriptors rx,tx
756  }
757  if(EndpointIntReg&EP1_INT){
759  F_UsbDeviceEp1Isr(); // bulk in
760  }
761  if(EndpointIntReg&EP2_INT){
763  F_UsbDeviceEp2Isr(); // no implement
764  }
765  if(EndpointIntReg&EP3_INT){
767  F_UsbDeviceEp3Isr(); // bulk out
768  }
769  if(EndpointIntReg&EP4_INT){
770  rEP_INT_REG = EP4_INT;
771  F_UsbDeviceEp4Isr(); // no implement
772  }
774  rINDEX_REG=IndexReg;
775 }
776