Recent Changes - Search:

SWUSB.LBX

Examples

edit SideBar

HowToUse

Project Organization

A BASCOM SWUSB project usually contains the following files:

  • swusb.lbx (the main library)
  • swusb-includes.bas
  • main body (MyProject.bas)

swusb.lbx consists of an ISR and a crc routine, both of which need to be declared in MyProject.bas:

'Include the software USB library
$lib "swusb.lbx"
$external _swusb
$external crcusb
Declare Function crcusb(buffer() as Byte, count as Byte) As Word

swusb-includes.bas Consists of constants, compile-time calculations of buffer sizes, and variable declarations required for USB communication.

MyProject.bas can be named whatever you like, but must have routines for handling USB communications. These routines are application-specific, so the implementation has been left out of the main library. See the examples. This is also where you will configure your USB inputs and interrupts.

Hardware Requirements

  • An interrupt (not a pin-change interrupt, but INT0/INT1)
  • A crystal
    • 12MHz or 15MHz
  • A device with 4k+ program flash
  • 3.3v operating voltage OR a level-shifting circuit (USB logic levels are 3.3v)

Buffers, Variables, and more on swusb-includes.bas

swusb-includes.bas is fairly well commented, so please take a look at it. I will try to elaborate on what is written there.

These variables and constants are names with a leading underscore ( _ ) to avoid potential namespace conflicts.

A shared receive buffer is used for all endpoints: _usb_rx_buffer(_usb_rx_bufsize). This buffer is large enough to hold two full eight-byte packets of data (plus PID and CRC bytes). A typical packet layout is as follows:

_usb_rx_buffer(1) 'PID byte
_usb_rx_buffer(2) 'Data Byte 1
               .
               .
               .
_usb_rx_buffer(9) 'Data Byte 8
_usb_rx_buffer(10)'CRCL Byte
_usb_rx_buffer(11)'CRCH Byte

Indexes 12 to 22 should be ignored. They are used to store data for packets that will be NAK'd and discarded.

Each endpoint has its own transmit buffer up to 8 bytes long:

_usb_tx_buffer()  'Transmit buffer for the control endpoint 0
_usb_tx_buffer2() 'Transmit buffer for first optional endpoint
_usb_tx_buffer3() 'Transmit buffer for second optional endpoint

The optional buffers will only be created/allocated if the optional endpoints have been defined.

In addition to the receive and transmit buffers, there are some important status bytes:

'Status flags for the received USB packet
Dim _usb_STATUS as Byte

'Status flags (token data) for the currently incoming USB packet
Dim _usb_STATUS2 as byte

'Bit positions of the STATUS and STATUS2 flags
Const _usb_RXC = 7      'USB Receive Complete / Data in RX buffer
Const _usb_RTR = 6      'USB Ready to Receive
Const _usb_Setup = 4    'USB Setup phase



Dim _usb_tx_status as byte
Dim _usb_tx_status2 as byte
Dim _usb_tx_status3 as byte
'Status flags for endpoints and sending a USB packet
Const _usb_RxSync = 7   'Receive sync bit for endpoint (DATAx toggling)
Const _usb_TxSync = 6   'Send sync bit for endpoint    (DatAx toggling)
Const _usb_TXC    = 5   'USB Transmit Complete flag
Const _usb_RTT    = 4   'USB Ready To Transmit flag
 
Edit - History - Print - Recent Changes - Search
Page last modified on February 22, 2012, at 01:12 AM