This example demonstrates how to establish communication between the AVR DU microcontroller and a host computer using the Universal Serial Bus (USB) Communication Device Class (CDC).
In this setup, the AVR DU is connected to a temperature sensor and an EEPROM memory. Both the temperature sensor and the memory are peripherals on the Curiosity Nano Explorer board. The AVR DU samples and stores temperature data every six seconds, and it is configured to store up to 42 samples (256 bytes). These temperature samples can be read by the host PC using the USB CDC.
The two images below showcase how the AVR DU program and Python script work:
- MPLAB® X IDE 6.20 or newer
- MPLAB® XC8 2.46 or a newer compiler
- MPLAB® Code Configurator (MCC) Melody core 5.7.1 or newer
- AVR-Dx Series Device Pack v2.4.286 or newer
More details and code examples on the AVR64DU32 can be found at the following links:
Place the AVR DU Curiosity Nano onto the Curiosity Nano Explorer board before programming device.
This project uses the following MCC device libraries and drivers:
- USB - for communication with the host PC
- I2C - for communication with the temperature sensor
- SPI - for communication with the EEPROM memory
- AC - to measure the voltage on the USB connection
- RTC - to generate interrupts that tell the device to sample temperature data and monitor the USB connection
After setting up the drivers, the project will look similar to this:
The configurator is set so that the AVR DU will identify as a CDC device on the highest level. The toggle switch for adding the Virtual Serial example code is also enabled.
In this example, the Device Descriptors section is left as default. For other applications, information on the product can be added here.
This example requires a communication and a data interface.
The communication interface configures the communication flow in the setup. The Abstract Control Model (ACM) is used as a subclass, but few of the supported commands are needed. However, three Functional Descriptors are required:
- Header - Required for Functional Descriptors
- ACM - Shows the host what commands the device supports (D1 indicates that device supports basic Line Coding and Control Line State commands)
- Union - Tells the Host the relationship between two or more interfaces
You can copy the settings from the screenshot below.
All communication is sent over the data interface for this Virtual Serial Port application. The Packet Size field determines how much data can be transmitted in one USB packet. The data sent can be bigger or smaller and the stack will transmit less or split it up into smaller packages.
The Two-Wire Interface (TWI) is used by the I2C host and must be configured as interrupt driven to communicate with the temperature sensor. Enable both the read and the write interrupts.
Select Mode 0 for the Serial Peripheral Interface (SPI) communication to the EEPROM.
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 (AC) will periodically check if the voltage is within the acceptable range for VBUS.
The Voltage Reference is set to 2.048V.
Under this register, the Analog Comparator must be enabled by toggling "Enable" under "Hardware Settings".
To measure the correct values, the positive input must be connected to AINP4, while the negative input is set to the reference pin DAC Reference for the Analog Comparator.
The target voltage can be set directly in the "Requested Voltage (V)" input under "Hardware Settings". The DACREF register must be set to a value that can detect the correct voltage level at minimum 0.4V (DACREF = 50).
In this example, the RTC overflow interrupt for taking temperature samples is configured with a period of six seconds. Configure this as desired.
Enable the Periodic Interrupt Timer (PIT), used for USB voltage checks, and set the period to 1024 cycles. Enable the Overflow and the Periodic Interrupt in the interrupt settings.
The Interrupt Manager is found in the System Firmware drop-down menu. Here, global interrupts must be enabled for the interrupt driven communication to work.
Configure pins PA7 and PD6 from the Pin Grid View as GPIO outputs. The remaining pins are already automatically configured.
Open Pins from the System Firmware drop-down menu. Rename PA7 to EEPROM_CS (EEPROM chip select) and PD6 to LED0, and select Start High for both.
Generate the MCC project by clicking the Generate button.
In this project, the USB sends a complete EEPROM page of 256 bytes to the host computer on read commands. Therefore, the USB transmit and receive buffers must be configured to have a size of 256 bytes. Set the define in usb_config.h
.
The current USB driver has an echo functionallity implemented by default. To remove this, go to the usb/usb_cdc/usb_cdc_virtual_serial_port.c
file. Find the USB_CDCDataReceived()
function and comment out/remove the USB_TransferWriteStart()
function call (line 150):
Test this example by following these steps:
-
Create a new MPLAB project and generate the MCC code as explained in the MCC Setup section. Right click the
main.c
file and remove it from the project. -
Find your project folder in the file explorer. Insert the
peripherals
directory and themain.c
file from the github source code. -
Add the
peripherals
folder to the Source Files and Header Files, by right clicking and choosing Add Existing Items From Folders. Remember to select the correct "files of type" to match the Source or Header files.
-
Add
main.c
by choosing Add Existing Item. -
Connect the PC to the DEBUGGER port on the Curiosity Nano Board by using a USB-C® cable.
After the AVR has been successfully programmed, connect the AVR to the PC through the USB-C
TARGET port. This will light LED0 on the Explorer Board. Go to the python_code
directory from the downloaded source code.
Make sure to install pyserial using pip install pyserial
before starting the script.
Run the datalogger.py
script and interact with the AVR.
The script can optionally be run with the argument --serialPortNum {COM PORT}
, which manually assings the specified serial port to the program.
When the program is up and running, the AVR can receive the following commands:
- read: Read the stored temperature data, which will be printed to the terminal
- status: Print the number of samples stored and the capacity
- reset: Wipe the memory
- save: Save the sample data to a CSV file
- exit: Exit the Python script
By following this example, the user will:
- Understand the basics of CDC communication on the AVR DU, and how to send and receive data via USB to a host PC.
- Understand how EEPROM can be used to store temperature samples on the Curiosity Nano Explorer board.