VLSI Solution Oy / MegaLib Documentation

usblowlib.h

Go to the documentation of this file.
00001 
00004 #ifndef USBLOWLIB_H
00005 
00006 #define USBLOWLIB_H
00007 
00008 #define AUDIO_ISOC_OUT_EP     0x01 
00009 #define MSC_BULK_OUT_ENDPOINT 0x03 
00010 #define MSC_BULK_IN_ENDPOINT  0x02 
00012 #define ENDPOINT_SIZE_0 64  
00013 #define ENDPOINT_SIZE_1 512 
00015 /*
00016  * Interrupts
00017  */
00018 #define BRESET_INT   0x8000 
00019 #define SOF_INT      0x4000 
00020 #define RX_INT       0x2000 
00021 #define TX_HOLD_INT  0x1000 
00022 #define TX_INT       0x0800 
00023 #define NAK_SENT_INT 0x0400 
00024 #define SETUP_INFO   0x0080 
00026 /*
00027  * PID tokens
00028  */
00029 #define P_SETUP   0x10
00030 #define P_DATA    0x20
00031 #define PID_SOF   0x05
00032 #define PID_SETUP 0x0D
00033 #define PID_IN    0x09
00034 #define PID_OUT   0x01
00035 #define PID_DATA0 0x03
00036 #define PID_DATA1 0x0B
00037 #define PID_ACK   0x02
00038 #define PID_NACK  0x0A
00039 #define PID_STALL 0x0E
00040 
00041 /*
00042  * USB addresses
00043  */
00044 
00045 #define USB_CONFIG  (USB_BASE)
00046 #define USB_CONTROL (USB_BASE+1)
00047 #define USB_STATUS  (USB_BASE+2)
00048 
00049 #define USB_STF_BUS_RESET (1<<15)
00050 #define USB_STF_SOF       (1<<14)
00051 #define USB_STF_RX        (1<<13)
00052 #define USB_STF_TX_READY  (1<<12)
00053 #define USB_STF_TX_EMPTY  (1<<11)
00054 #define USB_STF_NAK       (1<<10)
00055 #define USB_STF_SETUP     (1<<7)
00056 #define USB_STM_LAST_EP   (15<<0)
00057 
00058 #define USB_RDPTR   (USB_BASE+3) 
00059 #define USB_WRPTR   (USB_BASE+4) 
00061 #define USB_EP_SEND0 (USB_BASE+8)
00062 #define USB_EP_SEND1 (USB_BASE+9)
00063 #define USB_EP_SEND2 (USB_BASE+10)
00064 #define USB_EP_SEND3 (USB_BASE+11)
00065 //#define USB_EP_SEND4 (USB_BASE+12)
00066 //#define USB_EP_SEND5 (USB_BASE+13)
00067 //#define USB_EP_SEND6 (USB_BASE+14)
00068 //#define USB_EP_SEND7 (USB_BASE+15)
00069 
00070 #define USB_EP_ST0 (USB_BASE+16)
00071 #define USB_EP_ST1 (USB_BASE+17)
00072 #define USB_EP_ST2 (USB_BASE+18)
00073 #define USB_EP_ST3 (USB_BASE+19)
00074 //#define USB_EP_ST4 (USB_BASE+20)
00075 //#define USB_EP_ST5 (USB_BASE+21)
00076 //#define USB_EP_ST6 (USB_BASE+22)
00077 //#define USB_EP_ST7 (USB_BASE+23)
00078 
00079 #define USB_STF_OUT_BULK (0<<14)
00080 #define USB_STF_OUT_INT  (1<<14)
00081 #define USB_STF_OUT_ISO  (3<<14)
00082 #define USB_STF_OUT_ENABLE (1<<13)
00083 #define USB_STF_OUT_STALL  (1<<12)
00084 #define USB_STF_OUT_STALL_SENT (1<<11)
00085 #define USB_STF_OUT_EP_SIZE (1<<8)
00086 #define USB_STF_IN_BULK (0<<6)
00087 #define USB_STF_IN_INT  (1<<6)
00088 #define USB_STF_IN_ISO  (3<<6) 
00089 #define USB_STF_IN_ENABLE (1<<5)
00090 #define USB_STF_IN_STALL (1<<4)
00091 #define USB_STF_IN_STALL_SENT (1<<3)
00092 #define USB_STF_IN_NACK_SENT (1<<2)
00093 #define USB_STF_IN_EMPTY (1<<1)
00094 #define USB_STF_IN_FORCE_NACK (1<<0)
00095 
00096 
00097 #define RING_BUF_SIZE 1024
00098 
00099 #ifdef ASM
00100 /* Assembler-specific stuff */
00101 
00102 /* stp = -64 .. 63, bufsz 1..64 */
00103 #macro MAKEMOD stp,bufsz
00104         (0x2000|(((stp)&127)<<6)|(((bufsz)-1)&63))
00105 #endm
00106 
00107 /* stp = 1, bufsz 1..8192 */
00108 #macro MAKEMODF bufsz
00109         (0x8000|(((bufsz)-1)&8191))
00110 #endm
00111 
00112 /* stp = -1, bufsz 1..8192 */
00113 #macro MAKEMODB bufsz
00114         (0xA000|(((bufsz)-1)&8191))
00115 #endm
00116 
00117 #else
00118 /* Here you could put C-specific definitions, like typedefs, C macros, prototypes etc... */
00119 
00128 auto void RingBufCopyX(register __i2 u_int16 *d,
00129                       register __i0 const u_int16 *s,
00130                       register __a0 u_int16 n);
00131 //auto void RingBufCopyY(register __i2 __y u_int16 *d,
00132 //                    register __i0 const u_int16 *s,
00133 //                    register __a0 u_int16 n);
00134 
00136 typedef struct usbpkt {
00137   u_int16 length;                          
00138   u_int16 payload[(ENDPOINT_SIZE_1+1)>>1]; 
00139 } USBPacket;
00140 
00141 
00142 #define DT_LANGUAGES 0
00143 #define DT_VENDOR 1
00144 #define DT_MODEL 2
00145 #define DT_SERIAL 3
00146 #define DT_DEVICE 4
00147 #define DT_CONFIGURATION 5
00148 
00149 extern struct USBVARS {
00162   const u_int16 *descriptorTable[6];
00163 
00168   u_int16 configurationDescriptorSize;
00169 
00171   USBPacket pkt;
00172 
00173   u_int32 totbytes; 
00176   u_int16 ExtraZeroLengthPacketNeeded[4];
00177 
00179   const u_int16 *XmitBuf[4];
00180 
00182   u_int16 XmitLength[4];
00183 
00185   u_int16 EPReady[4];// = {1,1,1,1};
00186 
00187   u_int16 lastSofTimeout; 
00189   u_int16 configuration;  
00190   u_int16 interfaces;  
00191   u_int16 lastSofFill; 
00192   u_int32 lastSofTime; 
00193 } USB;
00194 
00203 int USBStartTransmission(u_int16 ep, const void *buf, 
00204                          u_int16 length, u_int16 requestedLength);
00208 void USBContinueTransmission(u_int16 ep);
00212 void InitUSBDescriptors(u_int16 initDescriptors);
00216 void RealInitUSBDescriptors(u_int16 initDescriptors);
00220 void InitUSB(u_int16 initDescriptors);
00225 void USBResetEndpoint(register __c0 int ep);
00230 u_int16 USBReceivePacket(USBPacket *packet);
00235 void USBSendZeroLengthPacketToEndpoint0(void);
00238 void USBHandler(void);
00242 void RealUSBHandler();
00245 void DecodeSetupPacket(void);
00248 void RealDecodeSetupPacket(void);
00252 void USBCheckForSetupPacket(void);
00257 u_int16 USBXmitLeft(u_int16 endpoint);
00261 void USBSingleStallEndpoint(register __c0 u_int16 ep);
00265 void USBStallEndpoint(register __c0 int ep);
00269 void USBResetStall(register __c0 int ep);
00274 u_int16 USBIsEndpointStalled(register int ep);
00276 u_int16 SwapWord(register __a1 u_int16 d);
00282 auto u_int16 USBIsAttached(void);
00287 auto u_int16 USBIsDetached(void); /*Snapshot only, USBWantsSuspend() required*/
00294 auto u_int16 USBWantsSuspend(void);
00295 
00296 
00297 #define USB_MASS_STORAGE 1
00298 #define USB_AUDIO 2
00299 
00303 void MSCPacketFromPC(USBPacket *setupPacket);
00306 void RealMSCPacketFromPC(USBPacket *setupPacket);
00307 u_int16 MscSendCsw(u_int16 status);
00308 void DiskProtocolError(char errorcode);
00309 
00310 
00311 #define AUDIO_DELAY_FRAMES 12 /* 12*44.1 = 529.2 samples */
00312 #define AUDIO_DELAY_FRAMES_STR "\x0c"
00313 
00315 void AudioPacketFromUSB(u_int16 *data, s_int16 words);
00316 
00317 #endif
00318 #endif

All software copyright 2005-2006 VLSI Solution OY. Redistribution of this software is strictly forbidden.