USART Static Driver Library for Harmony v2

Creating USART Static Drivers

The static driver library is used to controlindividual Universal Synchronous/Asynchronous Receiver/Transmitter (USART) peripherals (USART1or USART2or …) TheMPLAB® Harmony Configurator (MHC) is used to configure which USART peripheral is controlled by a specific USART driver instance. These custom drivers are created by MHC when you generate the files (click the "Generate" button.) Here are some examples of static drivers:

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.

If the static drivers shown above were configured by MHC as shown in the picture below, then the character 'a' will be transmitted on USART4 and 'b' will be transmitted on USART2.

usart_config_example.png

file:drv_usart_static.c

// *****************************************************************************// *****************************************************************************// Section: Instance 0 static driver functions// *****************************************************************************// *****************************************************************************void DRV_USART0_Initialize(void){    /* Initialize USART */    PLIB_USART_BaudRateSet(USART_ID_4, SYS_CLK_PeripheralFrequencyGet(CLK_BUS_PERIPHERAL_2), 9600);    PLIB_USART_HandshakeModeSelect(USART_ID_4, USART_HANDSHAKE_MODE_FLOW_CONTROL);    PLIB_USART_OperationModeSelect(USART_ID_4, USART_ENABLE_TX_RX_USED);    PLIB_USART_LineControlModeSelect(USART_ID_4, USART_8N1);    PLIB_USART_TransmitterEnable(USART_ID_4);    PLIB_USART_TransmitterInterruptModeSelect(USART_ID_4, USART_TRANSMIT_FIFO_NOT_FULL);    PLIB_USART_ReceiverEnable(USART_ID_4);    PLIB_USART_ReceiverInterruptModeSelect(USART_ID_4, USART_RECEIVE_FIFO_ONE_CHAR);    PLIB_USART_Enable(USART_ID_4);}bool DRV_USART0_ReceiverBufferIsEmpty(void){   return (!PLIB_USART_ReceiverDataIsAvailable(USART_ID_4));}uint8_t DRV_USART0_ReadByte(void){   if(PLIB_USART_ReceiverOverrunHasOccurred(USART_ID_4))   {      PLIB_USART_ReceiverOverrunErrorClear(USART_ID_4);   }   return (PLIB_USART_ReceiverByteReceive(USART_ID_4));}void DRV_USART0_WriteByte(const uint8_t byte){   while(PLIB_USART_TransmitterBufferIsFull(USART_ID_4))   {   }   PLIB_USART_TransmitterByteSend(USART_ID_4, byte);}

Harmony static drivers are simpler to use than dynamic drivers because they don't rely on an object-oriented (data structure-oriented) programming model. Data structures (objects) defining the drivers are not needed for the static drivers because the static driver functions are generated (customized) by MHC, based on the selections you've made in MHC.

USART Static Driver Functions
DRV_USART0_Initialize()
Initialize USART driver instance 0 (not the non-existant USART0 peripheral)
DRV_USART0_ReceiverBufferIsEmpty()
Returns true if USART instance 0 receiver buffer is empty
DRV_USART0_ReadByte()
Receive a byte from USART instance 0
DRV_USART0_WriteByte('c')
Transmit the character 'c' to USART instance 0

The static driver functions shown above are for USART driver instance 0. The "0" in USART0" indicates which driver instance is being controlled. In other words, if you need two static drivers, MHC will generate a group of "USART0" functions and a group of "USART1" functions (to control driver instance 0 and instance 1 respectively.)

The static driver Application Programming Interface (API) is much smaller than the dynamic driver API and only implements aByte-by-Byte Transfer Model. These are all the functions implemented for a single USART static driver instance.


Using USART Static Drivers

Once you have used MHC to generate the USART static drivers, you can use them as shown in the example below:

Using the USART Static Driver

using_static.png

You do not need to understand how MHC creates static drivers to use them. If you want these details, click the "USART Static Driver Details" button below. For a higher-level understanding of how to use USART static drivers, click on the "Harmony USART Tutorial" button below.

 
USART static driver Details
Learn more >

 Learn More

 
Harmony USART Tutorial
Learn more >
 
Example Code and Projects
Learn more >
 
Entire USART Driver Interface
Learn more >
 
USART Hardware Description
Learn more >