Adding USB functionality to your projects

Tips, Tricks and methods for programming, learn ways of making your programming life easier, and share your knowledge with others.

Moderators: Benj, Mods

Post Reply

Please rate this article out of 5: (1 being the worst and 5 being the best)

1
0
No votes
2
0
No votes
3
0
No votes
4
0
No votes
5
2
100%
 
Total votes: 2

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Adding USB functionality to your projects

Post by Benj »

Image

Using the new Flowcode USB pack it is possible to create interface devices that will plug directly into your user software. Examples of this included mice and keyboards, seeing as these examples are already included as part of the USB pack I decided to create a joystick example program.

A PC joystick is basically just a few potentiometers and switches which are used to control things like the X, Y, Throttle, POV HAT and standard input buttons. In a old 15 pin D-Type joystick the PC would control things like sampling the analogue and digital sensors to provide the joystick interface. For the USB version we are going to have to do all the peripheral sampling ourselves in the microcontroller and then transfer the sampled data to the PC.

Example Joystick Schematic
Image

To create a joystick the first thing that needs to be done is to create the HID descriptor. This descriptor is responsible for allowing the computer to detect that for example a joystick is present and also means that no other driver files are required to connect the device. The HID report descriptor was created by using the HID descriptor tool that is included inside the USB pack. The joystick example was opened and then exported as a H header file. This .h file was then opened and the contents pasted into the HID descriptor page of the USB HID component properties window.

The report descriptor contains all of the information regarding inputs and outputs from the system so the next thing we must do is modify the USB HID component properties to have the correct in and out packet sizes. This can be a little tricky to work out but a visit to the USB website should get you up to speed. The interrupt frequency can also be adjusted to match the speed of your main program loop.

So once the USB is all set up all we have to do is start monitoring the joystick hardware in a loop and when information changes we simply append the out data variable as required and then transmit to the PC. One warning regarding HID is that if you try sending data straight after startup then the USB routines will fail. The best method I have found for reliable operation is to only transmit data when the data changes. For example on a keyboard I only transmit data to the PC on a keydown or keyup event. This is the same for the joystick, data is only transferred if the data changes (or in the analogue case changes more then a couple of least significant bits). It only seems to be the USB HID component that is sensitive to this so dont worry about sending large amounts of data using the other two USB components.

The Flowcode USB pack can be found here as well as a short presentation on some of the features of the pack.
http://www.matrixmultimedia.com/FlowcodeUSBPack.php
Attachments
Joystick Demo.fcf
(24.99 KiB) Downloaded 733 times

Post Reply