Simple generic HID example demonstrating bidirectional communication. The firmware is compatible with the AN82072 Application note of Cypress so that we could use that PC application to test the USB communication.

Dependencies:   USBDevice mbed

13_USBHID_demo

Simple generic HID example demonstrating bidirectional communication. The firmware is compatible with the AN82072 Application note of Cypress so that we could use that PC application to test the USB communication. We send and receive 8 byte packages, however not all of this data is really used.

Input report data (received by the PC):

  • Byte0: button status (0: pressed, 1: released)
  • Byte1-Byte4: raw ADC data (32 bit, big Endian format) - actually we use only the lowest 16 bits...

Output report data (sent by the PC):

  • Byte0: LED control for LED_RED (0: off, 1: on)
  • Byte1: PWM duty cycle control for LED_GREEN (0-100: duty cycle in %)

Hardware requirements:

  • FRDM-KL25Z board
  • Analog signal (potmeter or thermometer) connected to the A0 (PTB0) analog input
  • Pushbutton connected between D3 (PTA12) and GND.
  • Socket labelled as USB shoud be connected to Computer running the Generic HID UI.exe application

Software requirements:

  • We need files Generic HID UI.exe and CyUSB.dll from package AN82072.zip

Usage

  • Write VID and PID values (0x1234 and 0x0006 respectively) and click on the SET button. If the FRDM-KL25Z board is connected, the green "Connected" message should show up.
  • Use the control elements to control the LEDs (you must click the Update button to send a new PWM value).
  • Watch the data read from the ADC and the status of the D3 digital input.

/media/uploads/icserny/an82072.png

Files at this revision

API Documentation at this revision

Comitter:
icserny
Date:
Fri May 06 14:49:07 2016 +0000
Commit message:
First version;

Changed in this revision

USBDevice.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r f259361d7db8 USBDevice.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/USBDevice.lib	Fri May 06 14:49:07 2016 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/USBDevice/#d17693b10ae6
diff -r 000000000000 -r f259361d7db8 main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Fri May 06 14:49:07 2016 +0000
@@ -0,0 +1,35 @@
+#include "mbed.h"
+#include "USBHID.h"
+
+//We declare a USBHID device. By default input and output reports are 8 bytes long.
+USBHID hid(8, 8);
+
+HID_REPORT send_report;                                 //This report will contain data to be sent
+HID_REPORT recv_report;                                 //This report will contain data received
+
+DigitalOut LED_1(LED1);
+PwmOut LED_2(LED2);
+DigitalIn SW1(D3,PullUp);;
+AnalogIn adc(A0);
+
+int main(void)
+{
+    send_report.length = 8;
+    LED_1 = 1;
+    LED_2.period_ms(20);
+
+    while (1) {
+        uint16_t raw = adc.read_u16();                  //Read ADC (A0 chan)
+        for (int i = 0; i < send_report.length; i++)    //Fill the report
+            send_report.data[i] = 0x00;
+        send_report.data[0] = SW1.read();
+        send_report.data[3] = (raw>>8);
+        send_report.data[4] = (raw & 0xff);
+        hid.send(&send_report);                         //Send the report
+
+        if(hid.readNB(&recv_report)) {                  //try to read a msg
+            LED_1 = !recv_report.data[0];
+            LED_2.write(1.0 - recv_report.data[1]*0.01f);
+        }
+    }
+}
diff -r 000000000000 -r f259361d7db8 mbed.bld
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Fri May 06 14:49:07 2016 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/87f2f5183dfb
\ No newline at end of file