This example shows how the HID class can be used on an AVR DU microcontroller with the AVR64DU32 Curiosity Nano Board to simulate a set of key-presses by pushing the physical button on the Curiosity Nano Board.
- USB Human Interface Device (HID) Specification
- Device Class Definition for HID 1.11
- USB 2.0 Specification
- MPLAB® X IDE 6.20.0 or newer
- MPLAB® XC8 2.46.0 or newer compiler
- MPLAB® Code Configurator (MCC) plugin 5.5.0 or newer
The AVR DU Curiosity Nano development board has two USB-C ports. The port labeled "Debugger" is used to program the device with the example code. This port must be connected to the PC when uploading the code to the device. After the programming is done, the cable connected to the "Debugger" port may be disconnected if you wish, but it will not affect the example if it remains plugged in. To test the device after the code has been uploaded, the port "Target" needs to be connected to the PC while testing.
The Curiosity Nano has an Adjustable Target Regulator that regulates voltage to 3.3V by default. Since the USB peripheral in the AVR64DU32 needs to run at 3.3V, it either needs a 3.3V supply connected on the VUSB pin or a 5.0V supply on VDD with the internal USB Voltage Regulator active. With the Curiosity Nano default settings, the VBUS pinheader on the board must be connected with the included jumper to supply 3.3V directly to VUSB.
This section shows how the example is set up in MCC.
The configuration is set so that the AVR DU will identify as an HID device on the highest level. The Keyboard protocol is selected to allow the device to mimic a keyboard.
The Device Descriptors can be left as default.
For this example project, an HID interface is needed. It does not need any modification beyond being added.
The interface created in the last step does not need any modification, and is best left to default.
Go to the Configurations tab in the USB Device Stack and change the country code. This example will use the US keyboard.
This example is configured to run on 12 MHz. The minimum oscillator frequency available for USB on the AVR DU is 12MHz.
Three pins are used for this example, with VBUS on PC3 set as input with "Digital Input Buffer disabled", SW0 on PF6 set to input with "Sense Falling Edge", and LED0 on PF2 set to output with "Interrupt disabled but input buffer enabled".
To ensure that the USB peripheral only tries to attach to the bus when plugging in the Curiosity Nano to the PC, the Analog Comparator will periodically check if the voltage is within the acceptable range for VBUS.
In the VREF peripheral, the Voltage Reference is set to 2.048V.
The Analog Comparator must be enabled by toggling Enable under Hardware Settings.
The positive input is set to the USB DETECT pin on the curiosity nano which is connected to Positive Pin 4 on the AC by default. The detection threshold is set using the Digital-to-Analog Comparator (DAC) reference (DACREF) which is selected as the negative input to the AC.
To calculate the DACREF value we can use the formula from the datasheet:
V_DACREF = (DACREF / 256) * V_REF
If we know the threshold we want, we can use the built in-feature of MCC to do it for us. We want to use the value 0.32V as a threshold due to the voltage divider implemented on the Curiosity Nano. Inputting this value into the Requested Voltage field will populate the DACREF value.
Refer to the AVR64DU32 Curiosity Nano User Guide for more information on how the USB DETECT is implemented on the Curiosity Nano.
On the Hardware settings, the "Enable RTC" can be disabled (Enabled by default), and note down the RTC Clock Source Selection, as it will impact the Period Selection value needed for the Periodic Interrupt Timer.
The Periodic Interrupt Timer (PIT) is enabled for this example, and the period selection is set to RTC Clock Cycles 1024. This value must be changed according to the clock source selection.
The Periodic Interrupt Timer's Interrupt Flag is also enabled to allow for an interrupt routine.
Enable global interrupts in the Interrupt Manager under System Firmware to recognize the Button and RTC interrupts.
This example can be tested by following these steps:
- Upload code by connecting the hardware as explained in "Physical Setup".
- Connect the PC to the "Target" plug on the Curiosity Nano Board by using a USB-C cable.
- Open any program that allows for text input, and press the button "SW0-PF6" on the Curiosity Nano-board.
This example has shown how the USB HID class can be used on an AVR DU to simulate a set of key-presses using only the on-board button on the Curiosity Nano kit.