There are many ways you can control PIC32 Universal Synchronous Asynchronous Receiver Transmitters (USARTs) using the MPLAB® Harmony Software Framework. This page provides advice and guidance on doing this.
Harmony Library | Useful 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 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 Direct Memory Access (DMA) is 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 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).
1
Use the MPLAB Harmony Configurator (MHC) to Configure a USART Static Driver
Open the MHC and expand the Harmony Framework Configuration, Drivers, and USART menus.
- Check the Use USART Driver? box.
- Use the drop-down window to change the Driver Implementation to Static.
- Configure the USART driver for your application's needs.
2
Use the 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
1
Use the MHC to Configure a USART Dynamic Driver
Open the MHC and expand the Harmony Framework Configuration, Drivers, and USART menus.
- Check the Use USART Driver? box.
- 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 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.
2
Use the 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, 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).
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.
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.