Using Harmony to Control USARTs

There are many ways you can control PIC32 USARTs using the MPLAB® Harmony software framework. This page provides advice and guidance on doing this.

Harmony Library Useful for … Downside
USART Peripheral Library 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. n/a
USART Static Drivers Sending 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 DMA available.
USART Dynamic Drivers 1) 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 others data. Static Drivers will consume PIC32 core cycles for every transfer because 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 MHC and expand the "Harmony Framework Configuration" - "Drivers" - "USART" menus.

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

2

Use MHC to generate the USART Static Driver

Click the "Generate" 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 the "USART Driver Library" button at the bottom of this page to see all available Static Driver functions.

3

Use the USART Static Driver

Open the "app.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 MPLAB® Harmony Configurator (MHC) to configure a USART Dynamic Driver

Open MHC and expand the "Harmony Framework Configuration" - "Drivers" - "USART" menus.

  • Check the box beside "Use USART Driver?"
  • Use the drop-down window to change the Driver Implementation to "Dynamic".
  • 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 MHC to determine which peripheral (USART Module ID) is used by each driver instance.
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 MHC to generate the USART Dynamic Driver

Click the "Generate" button to create and initialize the USART Dynamic Driver. Based on the way you configured the driver, 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.

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 the DRV_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 the "app.c" file and add your code to use the USART Dynamic Driver. Click on the "USART 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 >

20th Annual
Microchip MASTERs Conference 2016
Register now - Deadline: July 29

JW Marriott Desert Ridge Resort-Phoenix, AZ

© 2016 Microchip Technology, Inc.
Information contained on this site regarding device applications and the like is provided only for your convenience and may be superseded by updates. It is your responsibility to ensure that your application meets with your specifications. MICROCHIP MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY OR OTHERWISE, RELATED TO THE INFORMATION, INCLUDING BUT NOT LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY OR FITNESS FOR PURPOSE. Microchip disclaims all liability arising from this information and its use. Use of Microchip devices in life support and/or safety applications is entirely at the buyer's risk, and the buyer agrees to defend, indemnify and hold harmless Microchip from any and all damages, claims, suits, or expenses resulting from such use. No licenses are conveyed, implicitly or otherwise, under any Microchip intellectual property rights.