SWUSB.LBX Examples |
Examples /
USBGamepadWhat is it?8-button gamepad (up, down, left, right, A, B, X, Y) Connect buttons to PORTA. This is not an analog joystick. A gamepad has digital inputs for the D-PAD. To make a joystick, modify the HID report descriptor and use two ADC channels for X-Y axes. gamepad.bas'Test setup is on the STK500 with Vtarget set to 3.6V 'Using an external 12MHz crystal ' 'Save about 38 bytes of code size $noramclear $HWSTACK=30 $SWSTACK=30 $FRAMESIZE=40 '24 bytes reserved '$regfile = "m644def.dat" '$regfile = "m8def.dat" $regfile = "ATtiny461.dat" $crystal = 12000000 $EEPROMHEX 'for STK500 programmer 'Include the software USB library $lib "swusb.lbx" $external _SWUSB $external crcusb Declare Sub USB_Reset() Declare Sub USB_ProcessSetup(TxState as byte) Declare Sub USB_Send(TxState as byte, ByVal Count as Byte) Declare Sub USB_SendDescriptor(TxState as byte, MaxLen as byte) Declare Function crcusb(buffer() as Byte, count as Byte) As Word '******************************************************************************* '*************************** Begin USB Configuration *************************** ' 'Set the following parameters to match your hardware configuration and USB 'device parameters. '******************************* USB Connections ******************************* 'Define the AVR port that the two USB pins are connected to _usb_port ALIAS PortB _usb_pin ALIAS PinB _usb_ddr ALIAS DdrB 'Define the D+ and D- pins. (put D+ on an interrupt pin) Const _usb_dplus = 6 Const _usb_dminus = 3 'Configure the pins as inputs Config PinB.6 = Input Config PinB.3 = Input 'disable pullups _usb_port._usb_dplus = 0 _usb_port._usb_dminus = 0 '******************************************************************************* '************************* USB Configuration Constants ************************* 'Use EEPROM or FLASH to store USB descriptors '1 = EEPROM, 0 = FLASH. Storing to EEPROM will reduce code size slightly. Const _usb_USE_EEPROM = 0 'Don't wait for sent packets to be ACK'd by the host before marking the 'transmission as complete. This option breaks the USB spec but improves 'throughput with faster polling speeds. 'This may cause reliability issues. Should leave set to 0 to be safe. Const _usb_Assume_Ack = 0 ' *************************** Device Descriptor ***************************** 'USB Vendor ID and Product ID (Assigned by USB-IF) Const _usb_VID = &hAAAA Const _usb_PID = &hEF02 'USB Device Release Number (BCD) Const _usb_DevRel = &h0001 'USB Release Spec (BCD) Const _usb_Spec = &h0110 'USB Device Class, subclass, and protocol (assigned by USB-IF). '&h00 = Class defined by interface. (HID is defined in the interface) '&hFF = Vendor-defined class (You must write your own PC driver) 'See http://www.usb.org/developers/defined_class for more information Const _usb_DevClass = 0 Const _usb_DevSubClass =0 Const _usb_DevProt = 0 'These are _indexes_ to UNICODE string descriptors for the manufacturer, 'product name, and serial number. 0 means there is no descriptor. Const _usb_iManufacturer = 1 Const _usb_iProduct = 2 Const _usb_iSerial = 0 'Number of configurations for this device. Don't change this unless 'you know what you are doing. Ordinarily it should just be 1. Const _usb_NumConfigs = 1 ' *************************** Config Descriptor ***************************** 'The number of interfaces for this device (Typically 1) Const _usb_NumIFaces = 1 'Configuration Number (do not edit) Const _usb_ConfigNum = 1 'Index of UNICODE string descriptor that describes this config (0 = None) Const _usb_iConfig = 2 '&H80 = device powered from USB bus. '&HC0 = self-powered (has a power supply) Const _usb_Powered = &hC0 'Required current in 2mA increments (500mA max) Const _usb_MaxPower = 150 '150 * 2mA = 300mA ' ************************** Interface Descriptor *************************** 'Number of interfaces for this device (1 or 2) Const _usb_IFaces = 1 'Interface number Const _usb_IfaceAddr = 0 'Alternate index Const _usb_Alternate = 0 'Number of endpoints for this interface (excluding endp 0) Const _usb_IFaceEndpoints = 1 'USB Interface Class, subclass, and protocol (assigned by USB-IF). '&h00 = RESERVED '&hFF = Vendor-defined class (You must write your own PC driver) ' Other values are USB interface device class. (such as HID) 'See http://www.usb.org/developers/defined_class for more information Const _usb_IFClass = 3 Const _usb_IFSubClass = 0 Const _usb_IFProtocol = 0 'Index to UNICODE string descriptor for this interface (0 = None) Const _usb_iIFace = 0 ' ************************* Optional HID Descriptor ************************* 'HID class devices are things like keyboard, mouse, joystick. 'See http://www.usb.org/developers/hidpage/ for the specification, 'tools, and resources. 'Note that for a HID device, the device class, subclass, and protocol 'must be 0. The interface class must be 3 (HID). 'Interface subclass and protocol must be 0 unless you are making a 'keyboard or a mouse that supports the predefined boot protocol. 'See pages 8 and 9 of the HID 1.11 specification PDF. 'Number of HID descriptors (EXCLUDING report and physical) 'If you are not making a HID device, then set this constant to 0 Const _usb_HIDs = 1 'BCD HID releasenumber. Current spec is 1.11 Const _usb_HID_Release = &h0111 'Country code from page 23 of the HID 1.11 specifications. 'Usually the country code is 0 unless you are making a keyboard. Const _usb_HID_Country = 0 'The number of report and physical descriptors for this HID 'Must be at least 1! All HID devices have at least 1 report descriptor. Const _usb_HID_NumDescriptors = 1 'Use a software tool to create the report descriptor and $INCLUDE it. ' ************************* Endpoint Descriptor(s) ************************** 'Endpoint 0 is not included here. These are only for optional 'endpoints. 'Note: HID devices require 1 interrupt IN endpoint 'Address of optional endpoints (Must be > 0. comment-out to not use) Const _usb_Endp2Addr = 1 'Const _usb_Endp3Addr = 2 'Valid types are 0 for control or 3 for interrupt Const _usb_Endp2Type = 3 Const _usb_Endp3Type = 0 'Directions are: 0=Out, 1=In. Ignored by control endpoints Const _usb_Endp2Direction = 1 Const _usb_Endp3Direction = 0 'Polling interval (ms) for interrupt endpoints. Ignored by control endpoints ' (Must be at least 10) Const _usb_Endp2Interval = 10 Const _usb_Endp3Interval = 10 '******************************************************************************* 'The includes need to go right here--between the configuration constants above 'and the start of the program below. The includes will automatically calculate 'constants based on the above configuration, dimension required variables, and 'allocate transmit and receive buffers. Nothing inside the includes file needs 'to be modified. $include "swusb-includes.bas" '******************************************************************************* '**************************** USB Interrupt And Init *************************** 'Set all the variables, flags, and sync bits to their initial states Call USB_Reset() Const _usb_INTF = intf0 Config INT0 = Rising On INT0 Usb_isr Nosave Enable INT0 Enable Interrupts '******************************************************************************* '*************************** End Of USB Configuration ************************** Config PORTA = Input PORTA = &hFF 'enable pullups Dim buttons_current as byte Dim buttons_last as byte Dim resetcounter as word Dim idlemode as byte Const DEBOUNCE_COUNT = 5 Dim debounce_state(DEBOUNCE_COUNT) as byte 'Dim debounced_port as byte Dim debounce_index as Byte Dim count as byte debounce_index = 1 Do resetcounter = 0 'Check for reset here while _usb_pin._usb_dminus = 0 incr resetcounter if resetcounter = 1000 then Call Usb_Reset() end if wend 'Check for received data if _usb_STATUS._usb_RXC = 1 then if _usb_STATUS._usb_Setup = 1 then 'Process a setup packet/Control message Call USB_ProcessSetup(_usb_TX_Status) 'else end if 'Reset the RXC bit and set the RTR bit (ready to receive a new packet) _usb_STATUS._usb_RTR = 1 _usb_STATUS._usb_RXC = 0 end if ' buttons_current = pinA ' toggle buttons_current 'Debounce the input debounce_state(debounce_index) = pina toggle debounce_state(debounce_index) debounce_index = debounce_index + 1 if debounce_index > DEBOUNCE_COUNT then debounce_index = 0 buttons_current = &hFF for count = 1 to DEBOUNCE_COUNT buttons_current = buttons_current AND debounce_state(count) next waitms 5 if buttons_current <> buttons_last or idlemode = 0 then 'Queue data to be sent on endpoint 2 (HID report) if _usb_tx_status2._usb_TXC = 1 then buttons_last = buttons_current _usb_tx_buffer2(2) = buttons_current 'Direction pad data _usb_tx_buffer2(3) = buttons_current 'Button data Call Usb_Send(_usb_Tx_Status2, 2) end if end if Loop End '******************************************************************************* '******************** Descriptors stored in EEPROM or FLASH ******************** ' Do not change the order of the descriptors! ' #if _usb_USE_EEPROM = 1 $EEPROM #else $DATA #endif 'Device Descriptor _usb_DeviceDescriptor: DATA 18, 18, _usb_DESC_DEVICE, _usb_SpecL, _usb_SpecH, _usb_DevClass DATA _usb_DevSubClass, _usb_DEVPROT, 8, _usb_VidL, _usb_VidH, _usb_PIDL DATA _usb_PIDH, _usb_DevRelL, _usb_DevRelH, _usb_iManufacturer DATA _usb_iProduct, _usb_iSerial, _usb_NumConfigs 'Retrieving the configuration descriptor also gets all the interface and 'endpoint descriptors for that configuration. It is not possible to retrieve 'only an interface or only an endpoint descriptor. Consequently, this is a 'large transaction of variable size. _usb_ConfigDescriptor: DATA _usb_Descr_Total, 9, _usb_DESC_CONFIG, _usb_Descr_TotalL DATA _usb_Descr_TotalH, _usb_NumIFaces, _usb_ConfigNum, _usb_iConfig DATA _usb_Powered, _usb_MaxPower '_usb_IFaceDescriptor DATA 9, _usb_DESC_IFACE, _usb_IfaceAddr, _usb_Alternate DATA _usb_IFaceEndpoints, _usb_IFClass, _usb_IFSubClass, _usb_IFProtocol DATA _usb_iIFace #if _usb_HIDs > 0 '_usb_HIDDescriptor DATA _usb_HID_DESCR_LEN, _usb_DESC_HID, _usb_HID_ReleaseL, _usb_HID_ReleaseH DATA _usb_HID_Country, _usb_HID_NumDescriptors 'Next follows a list of bType and wLength bytes/words for each report and 'physical descriptor. There must be at least 1 report descriptor. In practice, 'There are usually 0 physical descriptors and only 1 report descriptor. DATA _usb_DESC_REPORT DATA 48, 0 'End of report/physical descriptor list #endif #if _usb_Endpoints > 1 '_usb_EndpointDescriptor DATA 7, _usb_DESC_ENDPOINT, _usb_Endp2Attr, _usb_Endp2Type, 8, 0 DATA _usb_Endp2Interval #endif #if _usb_Endpoints > 2 '_usb_EndpointDescriptor DATA 7, _usb_DESC_ENDPOINT, _usb_Endp3Attr, _usb_Endp3Type, 8, 0 DATA _usb_Endp3Interval #endif #if _usb_HIDs > 0 _usb_HID_ReportDescriptor: DATA 49 ' Length = 52 bytes DATA &h5, &h1 ' USAGE_PAGE (Generic Desktop) DATA &h9, &h5 ' USAGE (Game Pad) DATA &ha1, &h1 ' COLLECTION (Application) DATA &h9, &h1 ' USAGE (Pointer) DATA &ha1, &h0 ' COLLECTION (Physical) DATA &h9, &h30 ' USAGE (X) DATA &h9, &h31 ' USAGE (Y) DATA &h15, &hff ' LOGICAL_MINIMUM (-1) DATA &h25, &h1 ' LOGICAL_MAXIMUM (1) DATA &h95, &h2 ' REPORT_COUNT (2) DATA &h75, &h2 ' REPORT_SIZE (2) DATA &h81, &h2 ' INPUT (Data,Var,Abs) DATA &hc0 ' END_COLLECTION DATA &h95, &h4 ' REPORT_COUNT (4) DATA &h75, &h1 ' REPORT_SIZE (1) DATA &h81, &h3 ' INPUT (Cnst,Var,Abs) DATA &h5, &h9 ' USAGE_PAGE (Button) DATA &h19, &h1 ' USAGE_MINIMUM (Button 1) DATA &h29, &h8 ' USAGE_MAXIMUM (Button 8) DATA &h25, &h1 ' LOGICAL_MAXIMUM (1) DATA &h15, &h0 ' LOGICAL_MINIMUM (0) DATA &h95, &h8 ' REPORT_COUNT (8) DATA &h75, &h1 ' REPORT_SIZE (1) DATA &h81, &h2 ' INPUT (Data,Var,Abs) 'DATA &h95, &h2 ' REPORT_COUNT (2) 'DATA &h81, &h3 ' INPUT (Cnst,Var,Abs) DATA &hc0 ' END_COLLECTION #endif '*****************************String descriptors******************************** 'Yes, they MUST be written like "t","e","s","t". Doing so pads them with '0's. If you write it like "test," I promise you it won't work. 'Default language descriptor (index 0) _USB_LANGDESCRIPTOR: DATA 4, 4, _usb_DESC_STRING, 09, 04 '&h0409 = English 'Manufacturer Descriptor (unicode) _USB_MANDESCRIPTOR: DATA 14,14, _usb_DESC_STRING DATA "o","l","l","o","p","a" 'Product Descriptor (unicode) _USB_PRODDESCRIPTOR: DATA 44,44,_usb_DESC_STRING DATA "o","l","l","o","p","a","'","s"," ","g","a","m","e","p","a","d"," " DATA "v","1",".","0" '******************************************************************************* '******************************************************************************* '******************************** Subroutines ********************************** '******************************************************************************* Sub USB_ProcessSetup(TxState as byte) SendDescriptor = 0 'Control transfers reset the sync bits like so TxState = _usb_Setup_Sync 'These are the standard device, interface, and endpoint requests that the 'USB spec requires that we support. SELECT CASE _usb_rx_buffer(2) 'Standard Device Requests CASE &B10000000: SELECT CASE _usb_rx_buffer(3) ' CASE _usb_REQ_GET_STATUS: CASE _usb_REQ_GET_DESCRIPTOR: SELECT CASE _usb_rx_buffer(5) CASE _usb_DESC_DEVICE: 'Send the device descriptor #if _usb_USE_EEPROM = 1 READEEPROM _usb_EEPROMADDRL, _USB_DEVICEDESCRIPTOR #else RESTORE _USB_DEVICEDESCRIPTOR #endif SendDescriptor = 1 CASE _usb_DESC_CONFIG: 'Send the configuration descriptor #if _usb_USE_EEPROM = 1 READEEPROM _usb_EEPROMADDRL, _USB_CONFIGDESCRIPTOR #else RESTORE _USB_CONFIGDESCRIPTOR #endif SendDescriptor = 1 CASE _usb_DESC_STRING: SELECT CASE _usb_rx_buffer(4) CASE 0: 'Send the language descriptor #if _usb_USE_EEPROM = 1 READEEPROM _usb_EEPROMADDRL, _USB_LANGDESCRIPTOR #else RESTORE _USB_LANGDESCRIPTOR #endif SendDescriptor = 1 CASE 1: 'Send the manufacturer descriptor #if _usb_USE_EEPROM = 1 READEEPROM _usb_EEPROMADDRL, _USB_MANDESCRIPTOR #else RESTORE _USB_MANDESCRIPTOR #endif SendDescriptor = 1 CASE 2: 'Send the product descriptor #if _usb_USE_EEPROM = 1 READEEPROM _usb_EEPROMADDRL, _USB_PRODDESCRIPTOR #else RESTORE _USB_PRODDESCRIPTOR #endif SendDescriptor = 1 END SELECT END SELECT ' CASE _usb_REQ_GET_CONFIG: END SELECT CASE &B00000000: SELECT CASE _usb_rx_buffer(3) ' CASE _usb_REQ_CLEAR_FEATURE: ' CASE _usb_REQ_SET_FEATURE: CASE _usb_REQ_SET_ADDRESS: 'USB status reporting for control writes Call Usb_Send(TxState, 0) While TxState._usb_TXC = 0 : Wend 'We are now addressed. _usb_DeviceID = _usb_rx_buffer(4) ' CASE _usb_REQ_SET_DESCRIPTOR: CASE _usb_REQ_SET_CONFIG: 'Have to do status reporting Call Usb_Send(TxState, 0) END SELECT 'Standard Interface Requests CASE &B10000001: SELECT CASE _usb_rx_buffer(3) ' CASE _usb_REQ_GET_STATUS: ' CASE _usb_REQ_GET_IFACE: CASE _usb_REQ_GET_DESCRIPTOR '_usb_rx_buffer(4) is the descriptor index and (5) is the type SELECT CASE _usb_rx_buffer(5) CASE _usb_DESC_REPORT: #if _usb_USE_EEPROM = 1 READEEPROM _usb_EEPROMADDRL, _USB_HID_REPORTDESCRIPTOR #else RESTORE _USB_HID_REPORTDESCRIPTOR #endif SendDescriptor = 1 ' CASE _usb_DESC_PHYSICAL ' CASE _USB_DESC_HID END SELECT END SELECT 'CASE &B00000001: 'SELECT CASE _usb_rx_buffer(3) ' CASE _usb_REQ_CLEAR_FEATURE: ' CASE _usb_REQ_SET_FEATURE: ' CASE _usb_REQ_SET_IFACE: 'END SELECT 'Standard Endpoint Requests 'CASE &B10000010: 'SELECT CASE _usb_rx_buffer(3) ' CASE _usb_REQ_GET_STATUS: 'END SELECT 'CASE &B00000010: 'SELECT CASE _usb_rx_buffer(3) ' CASE _usb_REQ_CLEAR_FEATURE: ' CASE _usb_REQ_SET_FEATURE: 'END SELECT 'Class specific requests (useful for HID) 'CASE &b10100001: 'Class specific GET requests 'SELECT CASE _usb_rx_buffer(3) 'CASE _usb_REQ_GET_REPORT: 'CASE _usb_REQ_GET_IDLE: 'CASE _usb_REQ_GET_PROTOCOL: 'END SELECT '0-byte answer 'Call Usb_Send(TxState, 0) CASE &b00100001: 'Class specific SET requests SELECT CASE _usb_rx_buffer(3) 'CASE _usb_REQ_SET_REPORT: CASE _usb_REQ_SET_IDLE: idlemode = 1 'Do status reporting Call Usb_Send(TxState, 0) 'CASE _usb_REQ_SET_PROTOCOL: END SELECT END SELECT if SendDescriptor = 1 then Call USB_SendDescriptor(TxState, _usb_rx_buffer(8)) end if End Sub Sub USB_SendDescriptor(TxState as byte, MaxLen as byte) 'Break the descriptor into packets and send to TxState Local size as Byte Local i as Byte Local j as Byte Local timeout as word #if _usb_USE_EEPROM = 1 'EEPROM access is a little funky. The size has already been fetched 'and stored in _usb_EEPROMADDRL, and the address of the descriptor 'is now in the EEAR register pair. size = _usb_EEPROMADDRL 'Fetch the location of the descriptor and use it as an address pointer push R24 in R24, EEARL sts {_USB_EEPROMADDRL}, R24 in R24, eearH sts {_USB_EEPROMADDRH}, R24 pop R24 #else READ size #endif if MaxLen < size then size = MaxLen i = 2 for j = 1 to size incr i #if _usb_USE_EEPROM = 1 incr _usb_EEPROMADDR READEEPROM TxState(i), _usb_EEPROMADDR #else READ TxState(i) #endif if i = 10 OR J = size then i = i - 2 Call Usb_Send(TxState, i) While TxState._usb_TXC = 0 timeout = 0 'To prevent an infinite loop, check for reset here while _usb_pin._usb_dminus = 0 incr timeout if timeout = 1000 then ' Call Usb_Reset() exit sub end if wend WEND i = 2 end if next End Sub Sub USB_Send(TxState as byte, ByVal Count as byte) 'Calculates and adds the CRC16,adds the DATAx PID, 'and signals to the ISR that the data is ready to be sent. ' '"Count" is the DATA payload size. Range is 0 to 8. Do not exceed 8! 'Reset all the flags except TxSync and RxSync TxState = TxState AND _usb_SyncMask 'Calculate the 16-bit CRC _usb_crc = crcusb(TxState(3) , Count) 'Bytes to transmit will be PID + DATA payload + CRC16 Count = Count + 3 TxState = TxState + Count TxState(Count) = low(_usb_crc) incr Count TxState(Count) = high(_usb_crc) 'Add the appropriate DATAx PID TxState(2) = _usb_PID_DATA1 If TxState._usb_TxSync = 0 then TxState(2) = _usb_PID_DATA0 end if 'The last step is to signal that the packet is Ready To Transmit TxState._usb_RTT = 1 TxState._usb_TXC = 0 End Sub Sub USB_Reset() 'Reset the receive flags _usb_STATUS._usb_RTR = 1 _usb_STATUS._usb_RXC = 0 'Reset the transmit flags _usb_TX_Status = _usb_Endp_Init #if varexist("_usb_Endp2Addr") _usb_TX_Status2 = _usb_Endp_Init #endif #if varexist("_usb_Endp3Addr") _usb_TX_Status3 = _usb_Endp_Init #endif 'Reset the device ID to 0 _usb_DeviceID = 0 idlemode = 0 End Sub |