USB Library
Universal Serial Bus (USB) provides a serial bus standard for connecting a wide variety of devices, including computers, cell phones, game consoles, PDA’s, etc.
USB Library contains HID routines that support HID class devices, and also the generic routines that can be used with vendor specified drivers.
USB HID Class
The HID class consists primarily of devices that are used by humans to control the operation of computer systems. Typical examples of HID class devices include :
- Keyboards and pointing devices, for example: standard mouse devices, trackballs, and joysticks.
- Front-panel controls, for example: knobs, switches, buttons, and sliders.
- Controls that might be found on devices such as telephones, VCR remote controls, games or simulation devices, for example: data gloves, throttles, steering wheels, and rudder pedals.
- Devices that may not require human interaction but provide data in a similar format to HID class devices, for example, bar-code readers, thermometers, or voltmeters.
Many typical HID class devices include indicators, specialized displays, audio feedback, and force or tactile feedback. Therefore, the HID class definition includes support for various types of output directed to the end user.
Descriptor File
Each project based on the USB library should include a descriptor source file which contains vendor id and name, product id and name, report length, and other relevant information. To create a descriptor file, use the integrated USB HID terminal of mikroBasic PRO for PIC (Tools › USB HID Terminal). The default name for descriptor file is USBdsc.mbas, but you may rename it.
Important :
- The USB library routines have been changed. Please, have this in mind when migrating projects from previous versions of the compiler.
- Also, this relates to the descriptor source file, so it is necessary to create a new descriptor file in order to make your project work.
Library Routines
- HID_Enable
- HID_Read
- HID_Write
- HID_Disable
- USB_Interrupt_Proc
- USB_Polling_Proc
- Gen_Enable
- Gen_Read
- Gen_Write
HID_Enable
| Prototype |
sub procedure HID_Enable(dim readbuff as ^byte, dim writebuff as ^byte) |
|---|---|
| Description |
Enables USB HID communication. |
| Parameters |
These parameters are used for HID communication. |
| Returns |
Nothing. |
| Requires |
Nothing. |
| Example |
HID_Enable(@readbuff,@writebuff) |
| Notes |
This function needs to be called before using other routines of USB HID Library. |
HID_Read
| Prototype |
sub function HID_Read() as byte |
|---|---|
| Description |
Receives message from host and stores it in the Read Buffer. |
| Parameters |
None. |
| Returns |
If the data reading has failed, the function returns 0. Otherwise, it returns number of characters received from the host. |
| Requires |
USB HID needs to be enabled before using this function. See HID_Enable. |
| Example |
while(HID_Read() = 0) wend |
| Notes |
None. |
HID_Write
| Prototype |
sub function HID_Write(dim writebuff as ^byte, dim len as byte) as byte |
|---|---|
| Description |
Function sends data from Write Buffer |
| Parameters |
|
| Returns |
If the data transmitting has failed, the function returns 0. Otherwise, it returns number of transmitted bytes. |
| Requires |
USB HID needs to be enabled before using this function. See HID_Enable. |
| Example |
while(HID_Write(@writebuff,64) = 0) wend |
| Notes |
Function call needs to be repeated as long as data is not successfuly sent. |
HID_Disable
| Prototype |
sub procedure HID_Disable() |
|---|---|
| Description |
Disables USB HID communication. |
| Parameters |
None. |
| Returns |
Nothing. |
| Requires |
USB HID needs to be enabled before using this function. See HID_Enable. |
| Example |
HID_Disable() |
| Notes |
None. |
USB_Interrupt_Proc
| Prototype |
sub procedure USB_Interrupt_Proc() |
|---|---|
| Description |
This routine is used for servicing various USB bus events. Should be called inside USB interrupt routine. |
| Parameters |
None. |
| Returns |
Nothing. |
| Requires |
Nothing. |
| Example |
sub procedure interrupt() USB_Interrupt_Proc() end sub |
| Notes |
Do not use this function with USB_Polling_Proc, only one should be used. To enable servicing through interrupt, |
USB_Polling_Proc
| Prototype |
sub procedure USB_Polling_Proc() |
|---|---|
| Description |
This routine is used for servicing various USB bus events. It should be periodically, preferably every 100 microseconds. |
| Parameters |
None. |
| Returns |
Nothing. |
| Requires |
Nothing. |
| Example |
while TRUE
USB_Polling_Proc()
kk = HID_Read()
if (kk <> 0) then
for cnt = 0 to 63
writebuff[cnt] = readbuff[cnt]
next cnt
HID_Write(@writebuff,64)
end if
wend
|
| Notes |
Do not use this functions with USB_Interrupt_Proc. To enable servicing by polling, |
Gen_Enable
| Prototype |
sub procedure Gen_Enable(dim readbuff as ^byte, dim writebuff as ^byte) |
|---|---|
| Description |
Initialize the USB module of the MCU. |
| Parameters |
|
| Returns |
Nothing. |
| Requires |
USB needs to be enabled before using this function. See Gen_Enable. |
| Example |
Gen_Enable(@readbuff,@writebuff) |
| Notes |
None. |
Gen_Read
| Prototype |
sub function Gen_Read(dim readbuff as ^byte, dim length as byte, dim ep as byte) as byte |
|---|---|
| Description |
Generic routine that receives the specified data from the specified endpoint. |
| Parameters |
|
| Returns |
Returns the number of received bytes, otherwise 0. |
| Requires |
USB needs to be enabled before using this function. See Gen_Enable. |
| Example |
while(Gen_Read(@readbuff,64,1) = 0) wend |
| Notes |
None. |
Gen_Write
| Prototype |
sub function Gen_Write(dim writebuff as ^byte, dim as length as byte, dim ep as byte) as byte |
|---|---|
| Description |
Sends the specified data to the specified endpoint. |
| Parameters |
|
| Returns |
Returns the number of transmitted bytes, otherwise 0. |
| Requires |
USB needs to be enabled before using this function. See Gen_Enable. |
| Example |
while(Gen_Write(@writebuff,64,1) = 0) wend |
| Notes |
None. |
Library Example
This example establishes connection with the HID terminal that is active on the PC. Upon connection establishment, the HID Device Name will appear in the respective window. After that software will wait for data and
it will return received data back. Examples uses USBdsc.mbas descriptor file, which is in the same folder, and can be created by the HID Terminal.
program HID_Read_Write_Interrupt
dim cnt as char
dim readbuff as byte[64] absolute 0x500 ' Buffers should be in USB RAM, please consult datasheet
dim writebuff as byte[64] absolute 0x540
sub procedure Interrupt()
USB_Interrupt_Proc() ' USB servicing is done inside the interrupt
end sub
main:
ADCON1 = ADCON1 or 0x0F ' Configure all ports with analog function as digital
CMCON = CMCON or 7 ' Disable comparators
HID_Enable(@readbuff,@writebuff) ' Enable HID communication
while TRUE
while(HID_Read() = 0)
wend
for cnt=0 to 63
writebuff[cnt] = readbuff[cnt]
next cnt
while(HID_Write(@writebuff,64) = 0)
wend
wend
end.
HW Connection

USB connection scheme
What do you think about this topic ? Send us feedback!




