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

Committer:
icserny
Date:
Fri May 06 14:49:07 2016 +0000
Revision:
0:f259361d7db8
First version;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
icserny 0:f259361d7db8 1 #include "mbed.h"
icserny 0:f259361d7db8 2 #include "USBHID.h"
icserny 0:f259361d7db8 3
icserny 0:f259361d7db8 4 //We declare a USBHID device. By default input and output reports are 8 bytes long.
icserny 0:f259361d7db8 5 USBHID hid(8, 8);
icserny 0:f259361d7db8 6
icserny 0:f259361d7db8 7 HID_REPORT send_report; //This report will contain data to be sent
icserny 0:f259361d7db8 8 HID_REPORT recv_report; //This report will contain data received
icserny 0:f259361d7db8 9
icserny 0:f259361d7db8 10 DigitalOut LED_1(LED1);
icserny 0:f259361d7db8 11 PwmOut LED_2(LED2);
icserny 0:f259361d7db8 12 DigitalIn SW1(D3,PullUp);;
icserny 0:f259361d7db8 13 AnalogIn adc(A0);
icserny 0:f259361d7db8 14
icserny 0:f259361d7db8 15 int main(void)
icserny 0:f259361d7db8 16 {
icserny 0:f259361d7db8 17 send_report.length = 8;
icserny 0:f259361d7db8 18 LED_1 = 1;
icserny 0:f259361d7db8 19 LED_2.period_ms(20);
icserny 0:f259361d7db8 20
icserny 0:f259361d7db8 21 while (1) {
icserny 0:f259361d7db8 22 uint16_t raw = adc.read_u16(); //Read ADC (A0 chan)
icserny 0:f259361d7db8 23 for (int i = 0; i < send_report.length; i++) //Fill the report
icserny 0:f259361d7db8 24 send_report.data[i] = 0x00;
icserny 0:f259361d7db8 25 send_report.data[0] = SW1.read();
icserny 0:f259361d7db8 26 send_report.data[3] = (raw>>8);
icserny 0:f259361d7db8 27 send_report.data[4] = (raw & 0xff);
icserny 0:f259361d7db8 28 hid.send(&send_report); //Send the report
icserny 0:f259361d7db8 29
icserny 0:f259361d7db8 30 if(hid.readNB(&recv_report)) { //try to read a msg
icserny 0:f259361d7db8 31 LED_1 = !recv_report.data[0];
icserny 0:f259361d7db8 32 LED_2.write(1.0 - recv_report.data[1]*0.01f);
icserny 0:f259361d7db8 33 }
icserny 0:f259361d7db8 34 }
icserny 0:f259361d7db8 35 }