For many projects it would be cool to build a custom USB Joystick device, either as custom game controller for Windows or any USB host which can be used with a USB Joystick. Instead buying one, why not build my version? All what I need is a USB capable board, some kind of input (potentiometer, push buttons) and some software, and I have my USB Joystick:
In this article I’m using the NXP FRDM-KL25Z (ARM Cortex-M0+) with a Joystick (e.g. from Dx.com) shield:
The USB HID Joystick class is very similar to the USB Keyboard one, so follow the “Using the FRDM-KL25Z as USB Keyboard” tutorial for the basic USB setup.
Select the HID Joystick Device:
Then configure the HID device settings, especially the CPU/device used:
The component offers methods to set buttons, throttle, hat position and X/Y joystick position:
I have published on GitHub an example project which uses X/Y analog position, 4 hat switches, a throttle setting plus 3 push buttons:
The USB HID device shows up with two devices in the Windows device manager:
Inside the Windows Control Panel, there is a dialog to set up any USB game controller:
That dialog shows the controls and is used to calibrate them:
Summary
Building a DIY game controller is easy with the addition of the USB HID Joystick component and software. With a USB capable device and a joystick shield I’m able to create my gaming controller.
Happy Gaming 🙂
Links
-
USB and Using Freescale USB Stack withKinetis L devices: https://cache.freescale.com/files/32bit/doc/app_note/AN4748.pdf
- Tutorial about USB HID Device descriptor: http://eleccelerator.com/tutorial-about-usb-hid-report-descriptors/
- Creating a USB keyboard device: https://mcuoneclipse.com/2013/06/30/using-the-frdm-kl25z-as-usb-keyboard/
- McuOnEclipse components on SourceForge: https://sourceforge.net/projects/mcuoneclipse/files/PEx%20Components/
- Example project on GitHub: https://github.com/ErichStyger/mcuoneclipse/tree/master/Examples/KDS/FRDM-KL25Z/FRDM-KL25Z_USB_HID_Joystick
Hi Erich,
HID Joystick Device class is not available in mine SDK 2.0. It must be in MCUC1. How to get it?
Regards,
Marceli
LikeLike
Hi Marceli,
everything is on GitHub (https://github.com/ErichStyger/McuOnEclipse_PEx), so if you follow things there, you always have the latest and greatest.
For the ones not using GitHub, I have published a point release on SourceForge (https://sourceforge.net/projects/mcuoneclipse/files/PEx%20Components/) until I have time to make a full component release.
LikeLike
Hi Eric,
I have opened Joystick example from GitHub. It is setup in Processor Express for dual axis joystick. I need to extend for four axis and use more switches. How to do it?
Regards,
Marceli
LikeLike
Hi Marceli,
to add more push buttons, simply indrease the number in the component. For the multiple axis, you have to go into the USB code and extend it. Best if you could then extend the component code and contribute it back?
I don’t have a joystick need for more axis, but others would certainly welcome your contribution.
Erich
LikeLike
Hi Erich,
It seems is simple to extend the code making new selectable methods for three and four axis by PE. What CodeWarrior environment I need use to make it very fast.
Regards,
Marceli
LikeLike
Do you really need CodeWarrior? This is a legacy IDE, I rather recommend that you use the MCUXpresso IDE. There you can add Processor Expert too: https://mcuoneclipse.com/2017/04/09/mcuxpresso-ide-installing-processor-expert-into-eclipse-neon/
LikeLike
Pingback: McuOnEclipse Components: 09-July-2017 Release | MCU on Eclipse
Doesn’t build, followed as best I could considering the differences between the examples and 3.2 and I was trying on a K64F but USB0_init was not built and I was unable to select USB1 and code seemed to create a mixed USB1 and a USB0 device with init being 1 and HID connected to 0.
Trying to use a 64F for the ethernet
LikeLike
Not sure what is wrong on your side. I would first start with a project without any ethernet support. If it is USB1 or USB0 should not matter. Just make sure you have configured USBx_Init properly.
LikeLike
To complete design and communicate with PC I need HID VC# example code for Windows 10.
LikeLike
Hi Erich,
So I was trying to do the DIY USB joystick but I keep getting this error: “Description Resource Path Location Type
Generator: FAILURE: at line 10: Property not found: “TxBuffer” (file: Drivers\Common\FSL_USB_HID_Joystick_DeviceSettings.Inc) FRDM-KL25Z_USB_HID_Joystick HIDJ1 Processor Expert Problem”
Can you help me please?
P.S.: it let me comment on this post now 🙂
LikeLike
Is this tutorial with KDS or with codewarrior? Because here it looks like codewarrior but on github the folder the project is in is KDS.
LikeLike
It is for KDS. But it works the same way with minor changes (project creation) for CodeWarrior too.
LikeLike
Have you already loaded the latest version of the components (https://mcuoneclipse.com/2018/04/01/mcuoneclipse-components-1-apr-2018-release/)?
LikeLike
I believe I have found the problem you have reported and fixed it (https://github.com/ErichStyger/McuOnEclipse_PEx/commit/b0bbf4dcf9feb31c4adc2d6eb5b9cfad181beae3).
I have sent you as well the updated component by email to your Yahoo email address.
I hope this fixes the problem, and thank you so much for reporting it!
Erich
LikeLike
Thank you for fixing it so quickly!
LikeLike
Hi Erich,
Thank yo very much for sharing, can this joystick do the calibration in Windows like the commercial one does?
LikeLike
I have not implemented this, but it would be certainly doable on the technical side.
LikeLike
Hi Erich,
I am using you lib of joystick to increment functions on PE component. I’d like to know if you have some code to add in you lib to read data from USB HID. I’m tring to implement but any help, would be great.
LikeLike
You mean a for a generic HID? Look fort the code inside the MCUXpresso SDK which implements a generic HID.
LikeLike