2.4.1 USB Host Getting Started Application on SAM9X75 Early Access Evaluation Board
Description
This application demonstrates the ability of the MPLAB Harmony USB Host HID Client Driver to enumerate and operate a HID keyboard device on SAM9X75 Early Access Evaluation Board. This application creates a USB Host application that uses the USB Host Layer, HID Client driver and HID Keyboard Usage driver to enumerate a USB keyboard and understand keyboard press release events. The keyboard events are displayed using a terminal emulator on a personal computer. A green LED will turn ON when keyboard is connected and will turn OFF when the keyboard is disconnected.
Key Highlights of SAM9X75 Early Access Evaluation Board
- MIPI and LVDS LCD Interfaces.
- External Non-Volatile Memories like NAND, and SD card interfaces.
- Additional sensors can be interfaced using click boards through an on-board mikroBUS connector.
- Two mechanical programmable buttons.
- One User Input Switch and one RGB LED.
- UART, USB and CAN Interfaces.
- Raspberry pi connectors.
Modules/Technology Used
Peripheral Modules:
- USB
- TC0
- DBGU
Hardware Used
- SAM9X75 Early Access Evaluation Board.
- A commercially available USB Keyboard.
- SD Card.
Jumper Settings
Software/Tools Used
This project has been verified to work with the following versions of software tools:
Hardware Setup
- Power up the board by connecting 5V/2A power adapter to the SAM9X75 Early Access Evaluation Board.
- Press the Start(nSTART switch) button on the board.
- Connect the USB Keyboard to the J29-B connector of the SAM9X75 Early Access Evaluation Board.
- Connect the UART port (J34) on board to the computer using a USB to 5V Serial UART Cable(to enable debug com port).
- Connect external JTAG debugger to J37.

Developing a USB Host Demo Application
- Open MPLAB® X IDE from the main menu.
- Create a New Project by clicking
the New Project icon
or by selecting File -> New Project.
- In the New Project window, under
Projectschoose Application Project(s). and click Next.
- In the Select Device
dialog window, fill in or select the information for below:
- Family: Fill configuration name as 32-bit MCUs (PIC32C/SAM).
- Device : From drop down list select SAM9X75.
- In the Select Compiler
window, for Compiler Toolchains select XC32 COMPILER, and Click
Next.
- Enter Project Location ,
Project Folder and Project Name. Click Finish
- After the project is created, MCC
will be automatically launched. (To launch MCC manually, from main menu,
click on Tools -> Embedded -> MPLAB® Code
Configurator or click MCC button in the MPLAB® X IDE tool bar.)
It will launch Content manger Wizard. Then, select MPLAB® Harmony.
- In addition to the required
packages(csp, dev_packs), download the optional packages usb, bsp, core and then
click Finish. Content download will take some time. Wait till all the
contents are downloaded.
- A project graph will be
displayed. From Device Resource add Board Support Packages for
SAM9X75 Evaluation Board BSP to Project Graph.
- From Device Resource,
Libraries -> Host Stack , add HID Client Driver to
the Project Graph. The user will be prompted to allow auto-connection and
auto-activation of several components. Click on Yes for all of them
except FreeRTOS.
- Choosing the HID Client
Driver template automatically populates the rest of the project
components. This can be seen in the way the project graph is set up and
connected.
- From Device Resource,
Peripherals -> add DBGU to the Project Graph. This
helps to print the debug messages (console serial communication messages) via
comm port.
- In the project graph,
right click on TMR in Time and then add satisfiers TC0.
- From the project graph
window, click the HID Client Driver and in the Configuration
Options choose Use Keyboard Driver.
- From the project graph
window, click the USB Host Port HS Driver and in the Configuration
Options enter Port Selection as 0x02 (SAM9X75 Early Access
Evaluation Board has two host std Type A receptacle's, user can choose the one
they want to enable(0x01 or 0x02)).
- Refer section USB Clock
Controller of the SAM9X75 data sheet (DS60001813) which says The USB
device and host ports requires both the 48 MHz signal and the peripheral
clock.
Main clock= 24MHz, UPLL Clock= 24MHz*40/2 =480MHz Here UPLL Multiplier= 39 , USB Clock= 480MHz/10 = 48MHz Here USB Divider= 9.
Based on this calculations, configure the USB and peripherals clock signals as mentioned in the next step.
- From the Project Graph
window, click the System and in the Configuration Options, put the
clock settings as shown below:
- Ensure all the pins are
configured as below:PA26 and PA27 are for serial debugging. PC19, PC20, PC21 are for LEDs red, blue, and green respectively. PC27 and PC29 are VBUS Enable of USB.
- Save all and then click
Generate Code. This will generate code for all the Device Resource
that have been added in the project graph.
- Up to this point in the project
creation process, the user used MPLAB Code Configurator(MCC) to configure and
generate code to initialize the device (SAM9X75) and initialize USB host
driver.
Refer initialization.c in project_directory/src/config folder to find all the device resource added in the project graph got initialized. All that is left is for the user to write the application code in the app.c file. Documentation for each of the peripheral libraries or diver libraries can be accessed as follows:
- Peripheral libraries APIs can be accessed as a HTML file (*.html) from the Harmony 3 Framework path. (/framework_path/csp/docs/index.html)
- BSP libraries APIs can be found in bsp.h
- USB host driver APIs can be accessed as a HTML file (*.html) from the Harmony 3 Framework path. (/framework_path/usb/docs/index.html)
- APIs used for this application
are as follows:
- bool DBGU_Write( void* buffer, const size_t size );
- bool DBGU_WriteIsBusy( void );
- bool USB_HOST_HID_KEYBOARD_EventHandlerSet(USB_HOST_HID_KEYBOARD_EVENT_HANDLER appKeyboardEventHandler);
- bool USB_HOST_BusEnable(USB_HOST_BUS bus);
- bool USB_HOST_BusIsEnabled(USB_HOST_BUS bus);
- LED_GREEN_On();
- Sample code main.c and its header file can be found inside project_directory/src folder.
- Right click on the project and
click Properties. For Connected Hardware Tool -> select
connected hardware debugger used, for Compiler Toolchain -> select
XC32 and click Apply.
- From J-32/J-Link, in Option
categories choose Communication and for JTAG Method,
select 4-wire JTAG.
- Select Bootstrap for
Categories and select the Use Bootstrap checkbox. For bootstrap
file -> select the at91bootstrap elf file from the project
directory. Click on Apply and OK.
- By default, MPLAB X only produces
ELF and Hex format output files. To generate application
output in binary format, a post build step needs to be added to the project
properties. To do this project properties, building, click the check box
Execute this line after build, then enter the below
command.
${MP_CC_DIR}/xc32-objcopy -O binary ${DISTDIR}/${PROJECTNAME}.${IMAGE_TYPE}.elf ${DISTDIR}/harmony.bin
- Clean and build the project. The user should see a message on the output console that the project was successfully built. This completes the development of the USB host getting started application.
- The harmony.bin binaries will be available at ./firmware/sam9x75_ea.X/dist/default/production.
Debugging Application Project on MPLAB® X IDE
- Open the project (sam9x75_ea_usb_host_getting_started/firmware/sam9x75_ea.X) in MPLAB® X IDE.
- In the project properties, ensure SAM9X75 is selected as the Device and for Connected Hardware Tool, select connected hardware debugger with the board to program/debug the application.
- Build the code and debug the code by clicking on the Debug button in MPLAB® X IDE tool bar.
- Run the application by clicking the Run button in MPLAB® X IDE tool bar.
- Ensure Console Serial
communications between the Host PC and the SAM9X75 EA Evaluation Board take
place through UART debug port J34. A terminal emulation program running on the
Host PC communicates with the SAM9X75 Early Access Evaluation DBGU UART port.
Ensure the terminal emulation program (Eg: PUTTY) is configured to the COM port
and settings are:
- Speed: 115200
- Data: 8
- Parity: None
- Stop Bits: 1
- Once the USB host operation is
enabled, the user will get the debug message on the serial console as Connect
Keyboard. Once Keyboard is connected, green LED will turn ON and the
user will get the messages as shown below.
Once the Keyboard is disconnected, green LED will turn OFF.
Running the Pre-built Harmony Application from SD Card
The pre-built application bin file can be programmed by following the below steps.
Steps to program the bin file on SD card:
- Take a SD Card formatted with FAT32 file system.
- Copy the boot.bin and harmony.bin files from project_directory/hex folder to the SD card using the PC.
- Insert the SD card to J8 on the SAM9X75 Early Access Evaluation Board.
Steps to run the bin file from SD card:
- Press the reset button.
- Once the USB host operation is
enabled, the user will get the debug message on the serial console as Connect
Keyboard. Once keyboard is connected, green LED will turn ON and the user
will get the messages as shown below.
Once the keyboard is disconnected, green LED will turn OFF.
Comments
This application demo builds and works out of box by following the instructions above in Running the Demo section. If the user needs to enhance/customize this application demo, should use the MPLAB Harmony v3 Software framework. Refer links below to setup and build the applications using MPLAB Harmony.
- How to Setup MPLAB Harmony v3 Software Development Framework
- How to Build an Application by Adding a New PLIB, Driver, or Middleware to an Existing MPLAB Harmony v3 Project
- Click Here to learn more about USB
- MPLAB® Harmony v3 is also configurable through MPLAB® Code Configurator (MCC)- Refer to the below links for specific instructions to use MPLAB® Harmony v3 with MCC.