Getting Started with USB on SAM MCUs Using MPLAB® Harmony v3

Last modified by Microchip on 2023/11/10 11:02

Objective

MPLAB® Harmony v3 is a flexible, fully-integrated, embedded software development framework for 32-bit microcontrollers (MCUs) and microprocessors (MPUs).

MPLAB Harmony v3 includes the MPLAB Code Configurator (MCC) tool, a set of modular peripheral libraries, drivers, system services, middleware, and numerous example applications, all of which are designed to help you quickly and easily develop powerful and efficient embedded software for Microchip’s 32-bit PIC® and SAM devices.

This tutorial shows you how to use MCC to create a simple USB CDC class serial terminal menu application and will help you get started on developing USB-based applications for SAM MCUs using the MPLAB Harmony v3 software framework.

The following image is a screenshot of this training module application's output:

Training module application output

Figure: Serial Terminal Output

The training module application makes use of the SAM E70 Xplained Evaluation Kit and the I/O1 Xplained Pro Extension Kit (sold separately).

This application reads the current room temperature from the AT30TSE758A temperature sensor chip on the I/O1 Xplained Pro every second using I²C. This is indicated by a blinking orange LED on the I/O1 Xplained Pro. Further, it also writes these temperature values into the sensor chip's own integrated EEPROM using the same I²C peripheral instance. The application also uses an ADC channel to read the output voltage of the TEMT6000 ambient light sensor on the I/O1 Xplained Pro Extension Kit and displays it as a percentage of the sensor's full-scale output when requested through the serial terminal menu. This serial terminal menu is made available through the SAM E70 Xplained Evaluation Kit's Target USB port by configuring it as a USB CDC class device. This menu allows you to fetch temperature, light sensor data, and even toggle the LED on the SAME70 Xplained Evaluation Kit.

The application you create will utilize the following Harmony v3 components:

  • I²C Driver (TWIHS) to fetch the temperature and access the EEPROM on the AT30TSE758A
  • Timer System Service (TC) to periodically sample temperature sensor data every second
  • CDC Function Driver of the USB Device Stack (USBHS) for the serial terminal menu accessible via PC
  • ADC Peripheral Library (AFEC) to read the TEMT6000 ambient light sensor output voltage
  • PIO Peripheral Library (PIO) to toggle some LEDs and multiplex pin functions

​The name of the peripheral used by a driver or library (as per the device datasheet) is mentioned in brackets for reference.

Back to Top

Two Ways to Use This Tutorial

  1. Create the project from scratch:
    • Use the provided source files and follow the step-by-step instructions provided below
  2. Use the attached project as an example:
    • Build the attached project and program the SAM E70 Xplained Evaluation Kit to observe the output

Lab Objectives

  1. Create an MPLAB X Integrated Development Environment (IDE) Harmony v3 project for a SAME70 microcontroller from scratch.
  2. Use MCC to configure and generate code for the Harmony TIME System Service.
  3. Use MCC to configure and generate code for the Harmony I²C Driver and USB Device Stack Middleware (CDC Function Driver).
  4. Use MCC to configure and generate Harmony Peripheral Libraries for TC, TWIHS, AFEC, and PIO.
  5. Use the Harmony Driver, System Service, Middleware, and Peripheral Library APIs to implement the application.

Back to Top

Materials

Hardware Tools

​The SAM E70 Xplained Evaluation Kit includes an on-board Embedded Debugger (EDBG). No external tools are necessary to program or debug the ATSAME70Q21B. For programming or debugging, the EDBG connects to the host PC through the DEBUG USB Micro-B connector on the SAM E70 Evaluation Kit.

Hardware Connection Setup

SAM E70 Xplained Evaluation Kit

Figure: Hardware Setup

Apart from the hardware tools listed above, the following cables are required:

  • 1 USB Type-A male to Micro-B male cable for programming or debugging
  • 1 USB Type-A male to Micro-B male cable for the USB CDC serial port

Connection Diagram

Connection diagram for the SAME70 Xplained

Figure: Connection Diagram

  • I/O1 Xplained Pro is connected to the SAM E70 Xplained Evaluation Kit using the EXT1 connector
  • The serial terminal over the USB CDC interface is accessed via the TARGET USB Port
  • The on-board Embedded Debugger (EDBG) is accessed via the DEBUG USB Port

Back to Top

Software Tools

​This project has been verified to work with the following versions of software tools:
MPLAB X IDE v6.10, MPLAB XC32 Compiler v4.30

Because we regularly update our tools, occasionally you may discover an issue while using the newer versions. If you suspect that to be the case, we recommend that you double-check and use the same versions that the project was tested with.

For this lab, download the following packages using Harmony Content Manager:

Package NameVersion UsedDescription
mccv5.3.7Contains the current implementation of the MCC tool.
dev_packsv3.17.0Describes all peripherals, memory, etc. of supported 32-bit devices.
corev3.13.1Includes Drivers and Services with simple-to-use abstractions of peripherals and shared resources.
cspv3.17.0Chip Support Package: Includes the Peripheral Libraries (PLIBs).
bspv3.16.1Board Support Package: Includes templates and configuration data for supported development boards.
usbv3.11.0-E2Includes USB Middleware and Drivers.

The contents of these packages are detailed below.

  • DEV_PACKS: The following table shows the summary of contents
FolderDescription
MicrochipPeripheral register specific definitions
armCore Specific Register Definitions (CMSIS)
  • CORE: The following table shows the summary of contents
FolderDescription
appsExample applications for core library components
configCore module configuration scripts
docsCore module library help documentation
driverCore module peripheral device drivers
osalMPLAB Harmony Operating System Abstraction Layer
systemMPLAB Harmony system services
templatesApplication and system file templates
  • CSP: The following table shows the summary of contents
FolderDescription
appsExample applications for CSP library components
archInitialization and starter code templates and data
docsCSP library help documentation
peripheralPeripheral library templates and configuration data
  • BSP: The following table shows the summary of contents
FolderDescription
boardsBSP templates and configuration data
  • USB: The following table shows the summary of contents
FolderDescription
appsExample applications for USB library components
configUSB module configuration scripts
docsUSB module library help documentation
driverUSB module driver source files
middlewareUSB middleware source files
templatesApplication and system file templates

Back to Top

Overview

This lab shows you how to create an MPLAB X Harmony v3 project from scratch and configure/generate code for the following:

  • Harmony v3 Peripheral Libraries for TC, Inter-Integrated Circuit (I²C), ADC, and PIO peripherals
  • Harmony v3 Driver for I²C
  • Harmony v3 USB High-Speed Driver and USB Device Stack Middleware (CDC Function Driver)
  • Harmony v3 TIME System Service

The application is divided into three tasks:

  • Sensor Task
  • Electrically Erasable Programmable Read-Only Memory (EEPROM) Task
  • USB Task

Each task runs its own state machines. The Sensor, EEPROM, and USB tasks (in that order) are called from SYS_Tasks() routine which is run in an infinite loop.

Application state machine

Figure: Application state machine

After the initialization of the I²C sensor client, the sensor task waits for the one-second periodic timer event generated by the TIME system service. On every one-second periodic timer event, the sensor task submits a temperature sensor read request through the I²C interface and also toggles the LED on the I/O1 Xplained Pro. When a temperature sensor read is completed, the sensor task notifies the EEPROM task to store the read temperature value in the EEPROM.

Sensor task state machine

Figure: Sensor task state machine

After the initialization of the I²C EEPROM client, for every notification from the sensor task, the EEPROM task writes the temperature value to the EEPROM.

EEPROM task state machine

Figure: EEPROM task state machine

After the initialization of the USB Device layer, the USB task waits for the device to be enumerated and configured by the USB Host. Once configured, it waits for a character to be sent via the USB CDC serial port. If the character sent is a valid command, the corresponding action is performed. If the character is not a valid command, it starts waiting for another character to be sent via the USB CDC serial port.

USB task state machine

Figure: USB task state machine

Back to Top

Lab Source Files and Solutions

This ZIP archive contains the completed project for this lab. It also contains the source files needed to perform the lab step-by-step as per the section "Procedure" on this page.

‍ZIP

​You are free to extract the contents of the archive to any directory of your choice. See the notes below.

Note:

  1. The project location of a Harmony v3 project is independent of the location of the Harmony Framework path (i.e., you do not need to create or place a Harmony v3 project in a relative path under the Harmony v3 framework folder). The project can be created or placed in any directory of your choice.
  2. The Microchip Code Configurator (MCC) generates all of the required source files, header files, and libraries (if any) under your project folder.
  3. This is unlike older Harmony v2 projects that are required to be created in a location under the Harmony framework.

Extracting the ZIP archive creates the following folders:

  • getting_started_drivers_middleware contains the lab solution in the firmware folder and source files in the dev_files folder
    • dev_files contains the application source files and other support files (if any) required to perform the lab
    • firmware contains the completed lab solution project. You can directly build this project and program the hardware to observe the output

Back to Top

Procedure

​All steps must be completed before you will be ready to build, program, and run the application.

Lab Index

Step 1: Create and Configure Harmony v3 Project

  • Step 1.1 - Install the MPLAB Code Configurator (MCC) Plug-in in MPLAB X IDE
  • Step 1.2 - Create MPLAB Harmony v3 Project using MPLAB X IDE
  • Step 1.3 - Open MPLAB Code Configurator and Configure Project Settings

Step 2: Configure TIME System Service, I²C, USB, and ADC

  • Step 2.1 - Configure TIME System Service
  • Step 2.2 - Configure I²C Driver
  • Step 2.3 - Configure USB Device Stack Middleware
  • Step 2.4 - Configure ADC Peripheral Library

Step 3: Configure Clocks, Pins, and Application Tasks

  • Step 3.1 - Configure Clocks
  • Step 3.2 - Configure Pins
  • Step 3.3 - Add and Configure Application Tasks

Step 4: Generate Code
Step 5: Add Application Code to the Project
Step 6: Build, Program and Observe the Output

Back to Top