Using Harmony v2 to Control USARTs

There are many ways you can control PIC32 Universal Synchronous Asynchronous Receiver Transmitters (USARTs) using theMPLAB® Harmony Software Framework. This page provides advice and guidance on doing this.

Harmony LibraryUseful for …Downside
USART Peripheral Library (PLIB)Generally speaking, you will not want to use USART PLIBs in your application (use USART drivers instead). This library is used by the USART driver library.Not applicable
USART Static DriversSending small amounts of data over one USART using the Byte-by-Byte Transfer Model.1) May require your application to keep track of how much data has been sent and received.
2) No Direct Memory Access (DMA) is available.
USART Dynamic Drivers1) Sending larger amounts of data over one USART with or without DMA.
2) Conserving program memory space if multiple USARTs are needed.
3) Allowing multiple clients to safely share a single USART without interfering with each other.
More overhead and program memory required than a single static driver.

Using Static Drivers to Control USARTs

If you are sending small amounts of data over one USART, you will probably want to use the static driver. This may require your application to keep track of how much data has been sent and received. It may also require your application to keep track of who is using this driver to prevent multiple users from corrupting each other's data. Static drivers will consume PIC32 core cycles for every transfer because the DMA is not available (DMA can only be used for dynamic drivers).

usart_config_example.png

1

Use the MPLAB Harmony Configurator (MHC) to Configure a USART Static Driver

Open the MHC and expand theHarmony Framework Configuration,Drivers, andUSART menus.

  • Check theUse USART Driver? box.
  • Use the drop-down window to change the Driver Implementation toStatic.
  • Configure the USART driver for your application's needs.
generate1.png

2

Use the MHC to Generate the USART Static Driver

Click theGenerate button to create and initialize the USART static driver. This will generate the source files needed for the USART static driver and add them to your project.

Click on theUSART Driver Library button at the bottom of this page to see all available static driver functions.

3

Use the USART Static Driver

Open theapp.c file and add your code to use the USART static driver.

file:app.c

rx_byte = {{DRV_USART0_ReadByte}} ();   // Read a byte from USART driver instance 0 {{DRV_USART0_WriteByte}} ('a');         // Write character 'a' to USART driver instance 0 {{DRV_USART1_WriteByte}} ('b');         // Write character 'b' to USART driver instance 1

Using Dynamic Drivers to Control USARTs

usart_config3.png

1

Use the MHC to Configure a USART Dynamic Driver

Open the MHC and expand theHarmony Framework Configuration,Drivers, andUSART menus.

  • Check theUse USART Driver? box.
  • Use the drop-down window to change the Driver Implementation toDynamic.
  • Configure the USART driver for your application's needs.

Each USART driver instance uses a specific USART peripheral (e.g.USART_ID_1 = USART1). You will use the MHC to determine which peripheral (USART Module ID) is used by each driver instance.
The MHC will assign a number (starting at 0) to each instance created. This instance number will be used to determine which driver you want to access.

generate1.png

2

Use the MHC to Generate the USART Dynamic Driver

Click theGenerate button to create and initialize the USART dynamic driver. Based on the way you configured the driver, the MHC modifies Harmony system source files (system_config.h,system_definitions.h,system_init.c,system_tasks.c) to add the data structures (system objects) used to configure the USART driver. It also uses the USART peripheral library to configure the peripheral you selected to use with each driver instance.

The MHC will then add the appropriate USART driver source files to your project based on the way you intend to use the driver (USART Data Transfer Model).

3

Create a Handle for the USART Driver Client Object

usart_handle.png

4

Open a Client to Use a Specific USART Driver Instance

To use a USART dynamic driver, you must first open it using theDRV_USART_Open() function. You need to specify the driver instance you want to open, and how you want to use the driver (ioIntent). This function will create a new structure (driver client object), and return a handle (pointer) for this structure. This handle will enable you to use the USART library functions with this driver.

usart_open.png

5

Use the USART Dynamic Driver

Once you have opened the driver for a specific USART instance, you can use the USART library functions by passing them the handle for the client driver object. Open theapp.c file and add your code to use the USART dynamic driver. Click on theUSART Driver Library button at the bottom of this page to see all available dynamic driver functions.

using_dynamic.png

 Learn More

 
Example Code and Projects
Learn more >
 
Entire USART Driver Interface
Learn more >
 
Entire USART PLIB Interface
Learn more >