tiistai 5. toukokuuta 2015

STM32F4 Discovery USB HID, with sources

As promised, here it is. Minimalistic device-only USB HID example, adapted from STMicroelectronics libraries.

The zip included here includes (hopefully) all necessary sources to compile it, assuming you have ARM GCC based toolchain (including make) installed. Either run make directly or use included c.bat that invokes that for you. There is pre-built binaries too if you want to test this on your board first.

ZIP file here.

The example implements HID protocol with same VID/PID as Discovery examples. When running it will flash green led (PG13) continuously (but at quite high frequency, resulting it to just seem dim) and red led (PG14) when a USB packet is received from computer. It also will echo same packet with every byte within it increased by one back to computer.

Now, on PC side the easiest way I found was to use HID API library. As proper precompiled DLL for that was almost impossible to find so I included DLL (release build with visual studio 2013 directly from HID API sources) in zip too, but no other sources for PC side application as my test is based on ancient Borland C++ Builder 4 which you very likely you do not have; this also is the reason I had trouble with DLL as compiling the HID API DLL with that BCB was way too difficult (luckily I had some help). However, examples provided at HID API site should be easy enough to adapt for your preferred compiler though. (and I just realised that .lib file is not included there either, d'oh, as BCB lib will not work with other compilers... Well, I hope you will manage anyway) (and if you absolutely want to have the BCB project, drop me a line, I'll upload that too)

On some usage details;
Relevant (and somewhat modified) files from STM libraries are all placed in usblib directory, and main usb file is usb.c. In your main you only need to included usb.h and call usbInit(). To send packets use usbSend() function and on receive side you'll need to re-implement usbReceiveHandler() function to do your own processing. See comments here for further details.

Packet size is fixed to 64 bytes (1 byte report ID + 63 bytes user payload) so trying any other packet sizes will not work (read: will result an error) unless you change the report descriptors in sources. I've tried to add relevant comments here and there to make it easier to adapt it for your use but I expect it will take some studying anyway.

This is primarily for USB OTG HS (MicroUSB connector) on Discovery board with LCD, hopefully it will work on models without display too, as pin setup in usb_bsp.c is quite hardcoded for this setup (and actually will not work for any other type of interface as I've very likely broken other #ifdef branches in the process).



Ei kommentteja:

Lähetä kommentti