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 #ifndef _usb_h_
00032 #define _usb_h_
00033
00034 #include <Max3421e.h>
00035 #include "ch9.h"
00036
00037
00038 #define bmREQ_GET_DESCR USB_SETUP_DEVICE_TO_HOST|USB_SETUP_TYPE_STANDARD|USB_SETUP_RECIPIENT_DEVICE //get descriptor request type
00039 #define bmREQ_SET USB_SETUP_HOST_TO_DEVICE|USB_SETUP_TYPE_STANDARD|USB_SETUP_RECIPIENT_DEVICE //set request type for all but 'set feature' and 'set interface'
00040 #define bmREQ_CL_GET_INTF USB_SETUP_DEVICE_TO_HOST|USB_SETUP_TYPE_CLASS|USB_SETUP_RECIPIENT_INTERFACE //get interface request type
00041
00042 #define bmREQ_HIDOUT USB_SETUP_HOST_TO_DEVICE|USB_SETUP_TYPE_CLASS|USB_SETUP_RECIPIENT_INTERFACE
00043 #define bmREQ_HIDIN USB_SETUP_DEVICE_TO_HOST|USB_SETUP_TYPE_CLASS|USB_SETUP_RECIPIENT_INTERFACE
00044 #define bmREQ_HIDREPORT USB_SETUP_DEVICE_TO_HOST|USB_SETUP_TYPE_STANDARD|USB_SETUP_RECIPIENT_INTERFACE
00045
00046 #define USB_XFER_TIMEOUT 5000 //USB transfer timeout in milliseconds, per section 9.2.6.1 of USB 2.0 spec
00047 #define USB_NAK_LIMIT 32000 //NAK limit for a transfer. o meand NAKs are not counted
00048 #define USB_RETRY_LIMIT 3 //retry limit for a transfer
00049 #define USB_SETTLE_DELAY 200 //settle delay in milliseconds
00050 #define USB_NAK_NOWAIT 1 //used in Richard's PS2/Wiimote code
00051
00052 #define USB_NUMDEVICES 2 //number of USB devices
00053
00054
00055
00056 #define USB_STATE_MASK 0xf0
00057
00058 #define USB_STATE_DETACHED 0x10
00059 #define USB_DETACHED_SUBSTATE_INITIALIZE 0x11
00060 #define USB_DETACHED_SUBSTATE_WAIT_FOR_DEVICE 0x12
00061 #define USB_DETACHED_SUBSTATE_ILLEGAL 0x13
00062 #define USB_ATTACHED_SUBSTATE_SETTLE 0x20
00063 #define USB_ATTACHED_SUBSTATE_RESET_DEVICE 0x30
00064 #define USB_ATTACHED_SUBSTATE_WAIT_RESET_COMPLETE 0x40
00065 #define USB_ATTACHED_SUBSTATE_WAIT_SOF 0x50
00066 #define USB_ATTACHED_SUBSTATE_GET_DEVICE_DESCRIPTOR_SIZE 0x60
00067 #define USB_STATE_ADDRESSING 0x70
00068 #define USB_STATE_CONFIGURING 0x80
00069 #define USB_STATE_RUNNING 0x90
00070 #define USB_STATE_ERROR 0xa0
00071
00072
00073
00074
00075 typedef struct {
00076 union {
00077 byte bmRequestType;
00078 struct {
00079 byte recipient: 5;
00080 byte type: 2;
00081 byte direction: 1;
00082 };
00083 }ReqType_u;
00084 byte bRequest;
00085 union {
00086 unsigned int wValue;
00087 struct {
00088 byte wValueLo;
00089 byte wValueHi;
00090 };
00091 }wVal_u;
00092 unsigned int wIndex;
00093 unsigned int wLength;
00094 } SETUP_PKT, *PSETUP_PKT;
00095
00096
00097
00098
00099 typedef struct {
00100 byte epAddr;
00101 byte Attr;
00102 unsigned int MaxPktSize;
00103 byte Interval;
00104 byte sndToggle;
00105 byte rcvToggle;
00106
00107 } EP_RECORD;
00108
00109 typedef struct {
00110 EP_RECORD* epinfo;
00111 byte devclass;
00112 } DEV_RECORD;
00113
00114
00115
00116 class USB : public MAX3421E {
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127 public:
00128 USB( void );
00129 byte getUsbTaskState( void );
00130 void setUsbTaskState( byte state );
00131 EP_RECORD* getDevTableEntry( byte addr, byte ep );
00132 void setDevTableEntry( byte addr, EP_RECORD* eprecord_ptr );
00133 byte ctrlReq( byte addr, byte ep, byte bmReqType, byte bRequest, byte wValLo, byte wValHi, unsigned int wInd, unsigned int nbytes, char* dataptr, unsigned int nak_limit = USB_NAK_LIMIT );
00134
00135 byte getDevDescr( byte addr, byte ep, unsigned int nbytes, char* dataptr, unsigned int nak_limit = USB_NAK_LIMIT );
00136 byte getConfDescr( byte addr, byte ep, unsigned int nbytes, byte conf, char* dataptr, unsigned int nak_limit = USB_NAK_LIMIT );
00137 byte getStrDescr( byte addr, byte ep, unsigned int nbytes, byte index, unsigned int langid, char* dataptr, unsigned int nak_limit = USB_NAK_LIMIT );
00138 byte setAddr( byte oldaddr, byte ep, byte newaddr, unsigned int nak_limit = USB_NAK_LIMIT );
00139 byte setConf( byte addr, byte ep, byte conf_value, unsigned int nak_limit = USB_NAK_LIMIT );
00140
00141 byte setProto( byte addr, byte ep, byte interface, byte protocol, unsigned int nak_limit = USB_NAK_LIMIT );
00142 byte getProto( byte addr, byte ep, byte interface, char* dataptr, unsigned int nak_limit = USB_NAK_LIMIT );
00143 byte getReportDescr( byte addr, byte ep, unsigned int nbytes, char* dataptr, unsigned int nak_limit = USB_NAK_LIMIT );
00144 byte setReport( byte addr, byte ep, unsigned int nbytes, byte interface, byte report_type, byte report_id, char* dataptr, unsigned int nak_limit = USB_NAK_LIMIT );
00145 byte getReport( byte addr, byte ep, unsigned int nbytes, byte interface, byte report_type, byte report_id, char* dataptr, unsigned int nak_limit = USB_NAK_LIMIT );
00146 byte getIdle( byte addr, byte ep, byte interface, byte reportID, char* dataptr, unsigned int nak_limit = USB_NAK_LIMIT );
00147 byte setIdle( byte addr, byte ep, byte interface, byte reportID, byte duration, unsigned int nak_limit = USB_NAK_LIMIT );
00148
00149 byte ctrlData( byte addr, byte ep, unsigned int nbytes, char* dataptr, boolean direction, unsigned int nak_limit = USB_NAK_LIMIT );
00150 byte ctrlStatus( byte ep, boolean direction, unsigned int nak_limit = USB_NAK_LIMIT );
00151 byte inTransfer( byte addr, byte ep, unsigned int nbytes, char* data, unsigned int nak_limit = USB_NAK_LIMIT );
00152 int newInTransfer( byte addr, byte ep, unsigned int nbytes, char* data, unsigned int nak_limit = USB_NAK_LIMIT);
00153 byte outTransfer( byte addr, byte ep, unsigned int nbytes, char* data, unsigned int nak_limit = USB_NAK_LIMIT );
00154 byte dispatchPkt( byte token, byte ep, unsigned int nak_limit = USB_NAK_LIMIT );
00155 void Task( void );
00156 private:
00157 void init();
00158 };
00159
00160
00161 inline byte USB::getDevDescr( byte addr, byte ep, unsigned int nbytes, char* dataptr, unsigned int nak_limit ) {
00162 return( ctrlReq( addr, ep, bmREQ_GET_DESCR, USB_REQUEST_GET_DESCRIPTOR, 0x00, USB_DESCRIPTOR_DEVICE, 0x0000, nbytes, dataptr, nak_limit ));
00163 }
00164
00165 inline byte USB::getConfDescr( byte addr, byte ep, unsigned int nbytes, byte conf, char* dataptr, unsigned int nak_limit ) {
00166 return( ctrlReq( addr, ep, bmREQ_GET_DESCR, USB_REQUEST_GET_DESCRIPTOR, conf, USB_DESCRIPTOR_CONFIGURATION, 0x0000, nbytes, dataptr, nak_limit ));
00167 }
00168
00169 inline byte USB::getStrDescr( byte addr, byte ep, unsigned int nbytes, byte index, unsigned int langid, char* dataptr, unsigned int nak_limit ) {
00170 return( ctrlReq( addr, ep, bmREQ_GET_DESCR, USB_REQUEST_GET_DESCRIPTOR, index, USB_DESCRIPTOR_STRING, langid, nbytes, dataptr, nak_limit ));
00171 }
00172
00173 inline byte USB::setAddr( byte oldaddr, byte ep, byte newaddr, unsigned int nak_limit ) {
00174 return( ctrlReq( oldaddr, ep, bmREQ_SET, USB_REQUEST_SET_ADDRESS, newaddr, 0x00, 0x0000, 0x0000, NULL, nak_limit ));
00175 }
00176
00177 inline byte USB::setConf( byte addr, byte ep, byte conf_value, unsigned int nak_limit ) {
00178 return( ctrlReq( addr, ep, bmREQ_SET, USB_REQUEST_SET_CONFIGURATION, conf_value, 0x00, 0x0000, 0x0000, NULL, nak_limit ));
00179 }
00180
00181 inline byte USB::setProto( byte addr, byte ep, byte interface, byte protocol, unsigned int nak_limit ) {
00182 return( ctrlReq( addr, ep, bmREQ_HIDOUT, HID_REQUEST_SET_PROTOCOL, protocol, 0x00, interface, 0x0000, NULL, nak_limit ));
00183 }
00184 inline byte USB::getProto( byte addr, byte ep, byte interface, char* dataptr, unsigned int nak_limit ) {
00185 return( ctrlReq( addr, ep, bmREQ_HIDIN, HID_REQUEST_GET_PROTOCOL, 0x00, 0x00, interface, 0x0001, dataptr, nak_limit ));
00186 }
00187
00188 inline byte USB::getReportDescr( byte addr, byte ep, unsigned int nbytes, char* dataptr, unsigned int nak_limit ) {
00189 return( ctrlReq( addr, ep, bmREQ_HIDREPORT, USB_REQUEST_GET_DESCRIPTOR, 0x00, HID_DESCRIPTOR_REPORT, 0x0000, nbytes, dataptr, nak_limit ));
00190 }
00191 inline byte USB::setReport( byte addr, byte ep, unsigned int nbytes, byte interface, byte report_type, byte report_id, char* dataptr, unsigned int nak_limit ) {
00192 return( ctrlReq( addr, ep, bmREQ_HIDOUT, HID_REQUEST_SET_REPORT, report_id, report_type, interface, nbytes, dataptr, nak_limit ));
00193 }
00194 inline byte USB::getReport( byte addr, byte ep, unsigned int nbytes, byte interface, byte report_type, byte report_id, char* dataptr, unsigned int nak_limit ) {
00195 return( ctrlReq( addr, ep, bmREQ_HIDIN, HID_REQUEST_GET_REPORT, report_id, report_type, interface, nbytes, dataptr, nak_limit ));
00196 }
00197
00198 inline byte USB::getIdle( byte addr, byte ep, byte interface, byte reportID, char* dataptr, unsigned int nak_limit ) {
00199 return( ctrlReq( addr, ep, bmREQ_HIDIN, HID_REQUEST_GET_IDLE, reportID, 0, interface, 0x0001, dataptr, nak_limit ));
00200 }
00201 inline byte USB::setIdle( byte addr, byte ep, byte interface, byte reportID, byte duration, unsigned int nak_limit ) {
00202 return( ctrlReq( addr, ep, bmREQ_HIDOUT, HID_REQUEST_SET_IDLE, reportID, duration, interface, 0x0000, NULL, nak_limit ));
00203 }
00204 #endif //_usb_h_