Skip to content

This example shows how to exchange data between the AVR DU microcontroller and a host computer using the USB Communication Device Class (CDC).

License

Notifications You must be signed in to change notification settings

microchip-pic-avr-examples/avr64du32-cnano-usb-cdc-datalogger-mplab-mcc

Repository files navigation

Microchip Technologies Inc.

USB Communication Device Class (CDC) Data Logger with AVR® DU

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.

Program Overview

The two images below showcase how the AVR DU program and Python script work:

Hardware Used

Software Used

Related Documentation

More details and code examples on the AVR64DU32 can be found at the following links:

Setup

Physical Setup

Place the AVR DU Curiosity Nano onto the Curiosity Nano Explorer board before programming device.

MCC Setup

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:

USB Devic Stack Setup

USB General setings

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.

USB Device Descriptors

In this example, the Device Descriptors section is left as default. For other applications, information on the product can be added here.

USB Interfaces

This example requires a communication and a data interface.

USB CDC Communication 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.

USB CDC Data Interface

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.

Two-Wire Interface Setup (TWI0 Peripheral)

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.

Serial Peripheral Interface Setup (SPI0 Host)

Select Mode 0 for the Serial Peripheral Interface (SPI) communication to the EEPROM.

USB Voltage Detection Setup

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.

Voltage Reference Setup (VREF)

The Voltage Reference is set to 2.048V.

Analog Comparator Setup (AC0)

AC Hardware Settings

Under this register, the Analog Comparator must be enabled by toggling "Enable" under "Hardware Settings".

AC MUX Control 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.

AC DAC Reference Settings

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).

RTC Setup

RTC Hardware Settings

In this example, the RTC overflow interrupt for taking temperature samples is configured with a period of six seconds. Configure this as desired.

RTC Interrupt Settings

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.

Global Interrupt Setup

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.

Pin Configuration

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 MCC

Generate the MCC project by clicking the Generate button.

USB Notes

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):

How to Program Curiosity Nano Board

Test this example by following these steps:

Programming the AVR

  1. 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.

  2. Find your project folder in the file explorer. Insert the peripherals directory and the main.c file from the github source code.

  3. 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.

  4. Add main.c by choosing Add Existing Item.

  5. Connect the PC to the DEBUGGER port on the Curiosity Nano Board by using a USB-C® cable.

  6. From the menu bar, choose Make and Program Device.

Running the Datalogger on the PC

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

Summary

By following this example, the user will:

  1. Understand the basics of CDC communication on the AVR DU, and how to send and receive data via USB to a host PC.
  2. Understand how EEPROM can be used to store temperature samples on the Curiosity Nano Explorer board.

About

This example shows how to exchange data between the AVR DU microcontroller and a host computer using the USB Communication Device Class (CDC).

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages