00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00044 #ifndef __USBFUNCTION_H__
00045 #define __USBFUNCTION_H__
00046
00047
00048
00049
00050 #include "base_type.h"
00051 #include "mcu.h"
00052 #include "dbg.h"
00053 #include "UsbFunctionHW.h"
00054
00055
00056
00057
00058
00059 #define USB_USE_DMA 1
00060 #define USB_BUFFER_FAR 0
00061
00062
00063 #define IS_SET(x,y) ((x & y) > 0)
00064 #define IS_CLEARED(x,y) ((x & y) == 0)
00065 #define SET_MASK(x,y) x |= y
00066 #define CLEAR_MASK(x,y) x &= ~y
00067 #define EP_CONTROL(x) *(aEndpoint[x].EPnC)
00068 #define EP_STATUS(x) *(aEndpoint[x].EPnS)
00069
00070 #define EP_STATUS_IDLE 0
00071 #define EP_STATUS_CONFIGURED 1
00072 #define EP_STATUS_DISABLED 2
00073 #define EP_STATUS_ENABLED 4
00074 #define EP_STATUS_RECEIVING 8
00075 #define EP_STATUS_SENDING 16
00076 #define EP_STATUS_DMA_TRANSFER 32
00077 #define EP_STATUS_DMA_ENABLED 64
00078
00079 #define SET_USB_STATUS_FLAG(x) u8UsbStatus |= x // Set a flag in the USB Status #u8UsbStatus
00080 #define CLEAR_USB_STATUS_FLAG(x) u8UsbStatus &= ~x // Clear a flag in the USB Status #u8UsbStatus
00081 #define CLEAR_USB_STATUS_FLAGS() u8UsbStatus = 0 // Clear all flags in the USB Status #u8UsbStatus
00082 #define pu16EndpointStatusReg_DRQ (*pu16EndpointStatusReg & 0x0400)
00083 #define SET_pu16EndpointStatusReg_DRQ(x) *pu16EndpointStatusReg = ((*pu16EndpointStatusReg & 0xFBFF) | x << 10)
00084 #define SET_pu16EndpointStatusReg_BFINI(x) *pu16EndpointStatusReg = ((*pu16EndpointStatusReg & 0x7FFF) | x << 15)
00085
00086 #define MASK_EPC_PKS 0x007F
00087 #define MASK_EPC_PKS1 0x01FF
00088 #define MASK_EPC_STAL 0x0200
00089 #define MASK_EPC_NULE 0x0400
00090 #define MASK_EPC_DMAE 0x0800
00091 #define MASK_EPC_DIR 0x1000
00092 #define MASK_EPC_TYPE 0x6000
00093 #define MASK_EPC_EPEN 0x8000
00094
00095 #define MASK_EPS_DRQIIE 0x4000
00096 #define MASK_EPS_BFINI 0x8000
00097 #define MASK_EPS_SIZE1 0x01FF
00098 #define MASK_EPS_SIZE 0x007F
00099 #define MASK_EPS_SPK 0x0200
00100 #define MASK_EPS_DRQI 0x0400
00101 #define MASK_EPS_DRQO 0x0400
00102 #define MASK_EPS_DRQ 0x0400
00103 #define MASK_EPS_BUSY 0x0800
00104 #define MASK_EPS_SPKIE 0x2000
00105 #define MASK_EPS_DRQIE 0x4000
00106 #define MASK_EPS_DRQOIE 0x4000
00107 #define MASK_EPS_BFINI 0x8000
00108
00109 #define USBDESCR_DEVICE 1
00110 #define USBDESCR_CONFIG 2
00111 #define USBDESCR_STRING 3
00112 #define USBDESCR_INTERFACE 4
00113 #define USBDESCR_ENDPOINT 5
00114
00115 #define USBATTR_BUSPOWER 0x80
00116 #define USBATTR_SELFPOWER 0xC0
00117 #define USBATTR_REMOTEWAKE 0x20
00118
00119 #define USB_FUNC_INTR_IN 0xF000
00120 #define USB_FUNC_INTR_OUT 0xE000
00121 #define USB_FUNC_BULK_IN 0xD000
00122 #define USB_FUNC_BULK_OUT 0xC000
00123
00124 #define MAX_RECV_BUFFER 512
00125
00126 #define USB_DEVREQ_STDTYPE 0x00
00127 #define USB_DEVREQ_CLSTYPE 0x20
00128 #define USB_DEVREQ_VNDTYPE 0x40
00129
00130 #define USB_DEVREQ_GET_DESC 0x06
00131 #define USB_DEVREQ_SET_DESC 0x07
00132 #define USB_DEVREQ_SET_IDLE 0x0A
00133
00134 #define USB_GET_STATUS 0x00
00135 #define USB_CLEAR_FEATURE 0x01
00136 #define USB_SET_FEATURE 0x03
00137 #define USB_SET_ADDRESS 0x05
00138 #define USB_GET_DESCRIPTOR 0x06
00139 #define USB_SET_DESCRIPTOR 0x07
00140 #define USB_GET_CONFIGURATION 0x08
00141 #define USB_SET_CONFIGURATION 0x09
00142 #define USB_GET_INTERFACE 0x0A
00143 #define USB_SET_INTERFACE 0x0B
00144 #define USB_SYNCH_FRAME 0x0C
00145
00146
00147 #define USB_STATUS_CONNECTED 0x01
00148 #define USB_STATUS_DEVDESC 0x02
00149 #define USB_STATUS_CONFDESC 0x04
00150 #define USB_STATUS_CONFIGURED 0x08
00151
00152
00153 #define USB_REQUEST_TYPE_STANDARD 0
00154 #define USB_REQUEST_TYPE_CLASS 1
00155 #define USB_REQUEST_TYPE_VENDOR 2
00156
00157 #define USB_REQUEST_DEVICE_DESCRIPTOR 0x01
00158 #define USB_REQUEST_CONFIG_DESCRIPTOR 0x02
00159 #define USB_REQUEST_STRING_DESCRIPTOR 0x03
00160 #define USB_REQUEST_INTERF_DESCRIPTOR 0x04
00161 #define USB_REQUEST_ENDPNT_DESCRIPTOR 0x05
00162 #define USB_REQUEST_DEVQUA_DESCRIPTOR 0x06
00163 #define USB_REQUEST_OTHSPE_DESCRIPTOR 0x07
00164 #define USB_REQUEST_IFPOWR_DESCRIPTOR 0x08
00165 #define USB_REQUEST_REPORT_DESCRIPTOR 0x22
00166
00167 #define USB_EVENT_TARGET_GENERAL 0x01
00168 #define USB_EVENT_TARGET_MAINAPP 0x02
00169 #define USB_EVENT_TARGET_VENDOR 0x03
00170 #define USB_EVENT_TARGET_INTERFACE 0x04
00171
00172 #define USB_EVENT_CONNECT 0x01
00173 #define USB_EVENT_DISCONNECT 0x02
00174 #define USB_EVENT_RECEIVE 0x04
00175 #define USB_EVENT_SENT 0x08
00176 #define USB_EVENT_CLASSREQUEST 0x10
00177 #define USB_EVENT_CONFIGURED 0x20
00178 #define USB_EVENT_DECONFIGURED 0x40
00179
00180 #define USB_SENDING_MODE_INTERRUPT 0
00181 #define USB_SENDING_MODE_POLLED 1
00182 #define USB_SENDING_MODE_DMA 2
00183
00184 #define USB_EP0 0x00
00185 #define USB_EP_1 0x01
00186 #define USB_EP_2 0x02
00187 #define USB_EP_3 0x03
00188 #define USB_EP_4 0x04
00189 #define USB_EP_5 0x05
00190
00191 #define sendEP0(x,y) UsbFunction_SendData(0,x,y,1)
00192 #define sendEP1(x,y) UsbFunction_SendData(1,x,y,1)
00193 #define sendEP2(x,y) UsbFunction_SendData(2,x,y,1)
00194 #define sendEP3(x,y) UsbFunction_SendData(3,x,y,1)
00195 #define sendEP4(x,y) UsbFunction_SendData(4,x,y,1)
00196 #define sendEP5(x,y) UsbFunction_SendData(5,x,y,1)
00197
00198 #if (USB_USE_DMA == 1)
00199 #define DMA_OFFSET_BAPL 0
00200 #define DMA_OFFSET_BAPM 1
00201 #define DMA_OFFSET_BAPH 2
00202 #define DMA_OFFSET_DMACS 3
00203 #define DMA_OFFSET_IOAL 4
00204 #define DMA_OFFSET_IOAH 5
00205 #define DMA_OFFSET_DCTL 6
00206 #define DMA_OFFSET_DCTH 7
00207 #define DMA_DIR_HOST_OUT 0
00208 #define DMA_DIR_HOST_IN 1
00209 #endif
00210
00211
00212
00213
00214 #if (USB_BUFFER_FAR == 1)
00215 typedef __far uint8_t* puint8_t;
00216 typedef __far uint8_t fnuint8_t;
00217 #else
00218 typedef uint8_t* puint8_t;
00219 typedef uint8_t fnuint8_t;
00220 #endif
00221
00222
00223 typedef struct stc_usb_request
00224 {
00225 uint8_t Request;
00226 uint8_t Direction;
00227 uint8_t Type;
00228 uint8_t Target;
00229 uint16_t wValue;
00230 uint16_t wIndex;
00231 uint16_t wLength;
00232 } usb_request_t;
00233
00234 typedef struct stc_usb_buffer
00235 {
00236 puint8_t pu8Buffer;
00237 uint32_t u32BufferSize;
00238 uint32_t u32Position;
00239 uint32_t u32DataLength;
00240 } usb_buffer_t;
00241
00242 typedef struct stc_usb_event
00243 {
00244 uint8_t u8UsbStatus;
00245 uint8_t u8Event;
00246 usb_request_t * Request;
00247 usb_buffer_t * pstcEndpointBuffer;
00248 uint32_t u32DataSize;
00249 uint8_t u8Endpoint;
00250 } usb_event_t;
00251
00252 typedef struct stc_dma_handler
00253 {
00254 uint8_t u8DmaNumber;
00255 volatile uint8_t * DISELn;
00256 volatile uint8_t * pu8DmaDescriptor;
00257 } dma_handler_t;
00258
00259 typedef struct stc_usb_EP {
00260 uint8_t status;
00261 volatile uint8_t * EPnDTH;
00262 volatile uint8_t * EPnDTL;
00263 volatile uint16_t * EPnS;
00264 volatile uint16_t * EPnC;
00265 uint16_t u16Size;
00266 usb_buffer_t * pstcEndpointBuffer;
00267 uint8_t (* handler)(usb_event_t * stcEvent);
00268 dma_handler_t stcDmaHandler;
00269
00270 } usb_EP_t;
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286 void SetupEndpoints();
00287 void EnableEndpoints(void);
00288 void DisableEndpoints(void);
00289 void ConnectEvent();
00290 void DisconnectEvent();
00291
00292 #if USB_USE_DMA
00293 void SetupDma(uint8_t u8EndpointNumber,uint8_t u8InterruptNumber, uint16_t u16DataSize);
00294 void SendReceiveBufferViaDma(uint8_t u8EndpointNumber);
00295 void ReleaseDma(uint8_t u8EndpointNumber);
00296 void UsbFunction_ChangeDmaSettings(uint8_t u8EndpointNumber, volatile uint8_t * DISELn, volatile uint8_t * pu8DmaDescriptor);
00297 void UsbFunction_EnableReceiveDma(uint8_t u8EndpointNumber, uint32_t u32ReceiveSize);
00298 #endif
00299
00300
00301 uint8_t UsbFunction_GetStatus();
00302 uint8_t UsbFunction_GetEndpointStatus(uint8_t u8EndpointNumber);
00303 void UsbFunction_SetEventHandler(uint8_t,uint8_t,uint8_t,uint8_t (*) ());
00304 void UsbFunction_SetCustomEndpointBuffer(uint8_t,usb_buffer_t *);
00305 void UsbFunction_EnableSOF(void);
00306 void UsbFunction_DisableSOF(void);
00307 void UsbFunction_SetStartOfFrameHandler(void(* handler)(uint16_t u16FrameNumber));
00308 void UsbFunction_ClearStartOfFrameHandler(void);
00309 void UsbFunction_Initialize();
00310 boolean_t UsbFunction_Connect(boolean_t bAutoConnect);
00311 boolean_t UsbFunction_Disconnect(boolean_t bForceDisconnect);
00312 void UsbFunction_SendData(uint8_t,puint8_t,uint32_t, uint8_t);
00313 void UsbFunction_SetDataToSendEndpoint0(uint8_t *,uint32_t);
00314 void UsbFunction_EnableWakeup(boolean_t bEnableDisable);
00315 void UsbFunction_Stall(uint8_t u8EndpointNumber);
00316 void UsbFunction_StallRemove(uint8_t u8EndpointNumber);
00317
00318 void UsbReceiveSetup();
00319 uint8_t UsbGetDescriptor(usb_request_t *);
00320 uint8_t UsbRequestDecode(puint8_t,uint32_t);
00321 void UsbReceiveDataEndpoint0();
00322 uint32_t UsbReceiveData(uint8_t,puint8_t);
00323 uint8_t DummyEventHandler(usb_event_t *);
00324
00325 __interrupt void UsbFunction_EndpointIsr();
00326 __interrupt void UsbFunction_StatusControlIsr();
00327 __interrupt void UsbFunction_VbusIsr();
00328
00329
00330 #if (debug_enabled == 1)
00331 void DbgUsbRequest(usb_request_t * Request);
00332 void DbgUsbGetDescriptor(usb_request_t * ptRequest);
00333 #endif
00334
00335 #endif