This is a simple Generic HID demo, which is more or less compatible with the USB HID PnP demo example from the Microchip Applications Library, so that the PC application from that package can be used with our firmware as well (after two small modification). The differences: 1. We use 12 bit ADC resolution (instead of 10 bit) 2. We use Vid=0x0483, Pid=0x5750 which are the official STM32 Custom Human Interface Device identifiers from ST Microelectronics.

Dependencies:   USBDevice mbed

Committer:
cspista
Date:
Tue Sep 20 05:57:25 2022 +0000
Revision:
1:86e3171a1f42
Parent:
0:471646bf5f7e
Few typos were corrected

Who changed what in which revision?

UserRevisionLine numberNew contents of line
cspista 0:471646bf5f7e 1 /* LAB10_USBHID_pnp
cspista 0:471646bf5f7e 2 *
cspista 0:471646bf5f7e 3 * This is a simple Generic HID demo, which is more or less
cspista 0:471646bf5f7e 4 * compatible with the USB HID PnP demo example from the
cspista 0:471646bf5f7e 5 * Microchip Applications Library, so that the PC application
cspista 0:471646bf5f7e 6 * from that package can be used with our firmware as well
cspista 0:471646bf5f7e 7 * (after two small modification).
cspista 0:471646bf5f7e 8 *
cspista 0:471646bf5f7e 9 * The differences:
cspista 0:471646bf5f7e 10 * - We use 12 bit ADC resolution (instead of 10 bit)
cspista 0:471646bf5f7e 11 * - We use Vid=0x0483, Pid=0x5750 which are the official
cspista 0:471646bf5f7e 12 * STM32 Custom Human Interface Device identifiers from
cspista 0:471646bf5f7e 13 * ST Microelectronics.
cspista 0:471646bf5f7e 14 */
cspista 0:471646bf5f7e 15
cspista 0:471646bf5f7e 16 #include "mbed.h"
cspista 0:471646bf5f7e 17 #include "USBHID.h"
cspista 0:471646bf5f7e 18
cspista 0:471646bf5f7e 19 //We declare a USBHID device. Here the input and output reports are 64 bytes long.
cspista 0:471646bf5f7e 20 USBHID hid(64,64,0x0483,0x5750); // Vid/Pid: STM32 Custom Human Interface Device
cspista 0:471646bf5f7e 21
cspista 0:471646bf5f7e 22 HID_REPORT send_report; //This report will contain data to be sent
cspista 0:471646bf5f7e 23 HID_REPORT recv_report; //This report will contain data received
cspista 0:471646bf5f7e 24
cspista 1:86e3171a1f42 25 DigitalOut LED_1(LED1); //Buitin LED at PA_5
cspista 1:86e3171a1f42 26 DigitalIn SW1(BUTTON1,PullUp); //Builtin button at PC_13
cspista 1:86e3171a1f42 27 AnalogIn adc(A0); //Analog input at A0 (PA_0)
cspista 0:471646bf5f7e 28
cspista 0:471646bf5f7e 29 int main(void) {
cspista 0:471646bf5f7e 30 uint16_t val = 0;
cspista 0:471646bf5f7e 31 send_report.length = 64;
cspista 0:471646bf5f7e 32 LED_1 = 0;
cspista 0:471646bf5f7e 33 for (int i = 0; i < send_report.length; i++) // Fill the report
cspista 0:471646bf5f7e 34 send_report.data[i] = 0x00;
cspista 0:471646bf5f7e 35 while (1) {
cspista 0:471646bf5f7e 36 if(hid.readNB(&recv_report)) { // try to read a msg
cspista 0:471646bf5f7e 37 switch(recv_report.data[0]) {
cspista 0:471646bf5f7e 38 case 0x80: //Toggle LED state
cspista 0:471646bf5f7e 39 LED_1 = !LED_1;
cspista 0:471646bf5f7e 40 break;
cspista 0:471646bf5f7e 41 case 0x81: //Get push button state
cspista 0:471646bf5f7e 42 send_report.data[0] = recv_report.data[0];
cspista 0:471646bf5f7e 43 send_report.data[1] = SW1;
cspista 0:471646bf5f7e 44 send_report.data[2] = 0;
cspista 0:471646bf5f7e 45 hid.send(&send_report); // Send the report
cspista 0:471646bf5f7e 46 break;
cspista 0:471646bf5f7e 47 case 0x37: //Read POT command.
cspista 0:471646bf5f7e 48 send_report.data[0] = recv_report.data[0];
cspista 0:471646bf5f7e 49 val = adc.read_u16()>>4; // Convert to 12-bits
cspista 0:471646bf5f7e 50 send_report.data[1] = val & 0xFF; // Measured value LSB
cspista 0:471646bf5f7e 51 send_report.data[2] = val >> 8; // Measured value MSB
cspista 0:471646bf5f7e 52 hid.send(&send_report); // Send the report
cspista 0:471646bf5f7e 53 break;
cspista 0:471646bf5f7e 54 default: {
cspista 0:471646bf5f7e 55 send_report.data[0] = 0xFF; // Invalid command
cspista 0:471646bf5f7e 56 hid.send(&send_report); // Send the report
cspista 0:471646bf5f7e 57 }
cspista 0:471646bf5f7e 58 }
cspista 0:471646bf5f7e 59 }
cspista 0:471646bf5f7e 60 }
cspista 0:471646bf5f7e 61 }