This example demonstrates how to use the USB Human Interface Device (HID) class as a mouse on the AVR® DU microcontroller (MCU), paired with the AVR64DU32 Curiosity Nano Evaluation Kit, to interface with the Curiosity Nano Explorer Board.
- USB 2.0 Specification
- USB Human Interface Device (HID) Specification
- Device Class Definition for HID 1.11
- MPLAB® X IDE 6.20 or newer
- MPLAB® XC8 Compiler 2.50 or newer
- MPLAB® Code Configurator (MCC) 5.7.1 plug-in 5.7.1 or newer
- AVR64DU32 Curiosity Nano (EV59F82A)
- Curiosity Nano Explorer (EV58G97A)
- 8x Jumper Cables
- 2x USB-C Cables
Insert the AVR64DU32 Curiosity Nano with pin headers into the Curiosity Nano Explorer.
Connect the 8x1 pins of the IO-Expander 2 to the pins PA0-PA7 as shown in the schematic below using the jumper cables.
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 this example is set up in MCC. An overview of the MCC setup is shown in the image below:
The configuration is set so the AVR DU will identify as an HID device on the highest level. The Mouse protocol is selected.
The Device Descriptors can be left as default with only the product name changed to this example.
The USB is set to be interrupt driven.
For this 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 as default.
The CLKCTRL module is located in the System Firmware drop-down menu. This example is configured to run on 24 MHz. The minimum oscillator frequency available for USB on the AVR DU is 12 MHz.
To ensure that the USB peripheral only tries to attach to the bus when the Curiosity Nano is connected to the PC, the Analog Comparator(AC) 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 by default is connected to Positive Pin 4 on the AC. The detection threshold is set using the Digital-to-Analog Converter(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 data sheet:
VDACREF = (DACREF / 256) * VREF
If the desired threshold is known, use the built-in feature of the MCC to obtain it. 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.
In the Hardware Settings, enable the RTC. Note down the RTC Clock Source Selection (32768 Hz /32 = 1024 Hz), as it will impact the Period Selection value needed for the Periodic Interrupt Timer. The 1 second period is used for power handling and does not need to be done very often.
The Periodic Interrupt Timer (PIT) is enabled for this example, and the period selection is set to RTC Clock Cycles 16. Together with the Prescaling factor set to 1, this gives us an interrupt frequency of 64 Hz. The PIT interrupt is used to read the state of the joystick/switches and needs to be updated more often than the power monitor.
Both the PIT and OVF Interrupt flag are enabled to allow for an interrupt routine.
Enable global interrupts in the Interrupt Manager under System Firmware to recognize the USB and RTC interrupts.
This example uses eight pins for joystick/switches and one for monitoring power. Setting inputs and outputs can be done in the Pin Grid View tab. The joystick and all the switches are set to input on PA0-PA7 together with PC3 for the Analog Comparator.
Naming and configuring the different pins can be done in the System>Pins menu. The input pins from the joystick/switches needs pull-up and inverted I/O.
This example can be tested by following these steps:
If using the provided MPLAB X project, skip to step 3.
- Generate the MCC code as described in the MCC section of this readme.
- Replace the contents of the generated
main.c
with the contents of the provided file. - Connect the Debugger plug on the Curiosity Nano Board to the PC and upload the C code.
- Connect the PC to the Target plug to start the USB communication.
- Moving the joystick will move the mouse. SW2 is left click and SW3 is right click.
This example has demonstrated how the USB HID class can be used on an AVR DU to function as a mouse.