4.6 Application Example Using MPLAB Harmony v3 Driver
To create an application to display a message on a computer console using the MPLAB Harmony v3 drivers, follow these steps:
- Create an MPLAB Harmony v3 project.
- Configure using the MCC.
- Configure the UART pin: Launch the pin manager, go to the Pin Setting window of the MCC and configure the UART Transmit pin.
- Follow these steps to
configure the UART PLIB:
- Add the UART6 Peripheral Library from the Device Resources window.
- Click on the added UART6 PLIB box on the project graph to see its configuration options in the window on the right.
- Keep the Interrupt
mode enabled and change the baud rate to 115,200 as shown in the
following figure:
Figure 4-8. Configure UART PLIB
- Follow these steps to
configure the USART Driver:
- Add the USART Driver from the Device Resources window.
- Add the Core (HarmonyCore) component when asked in the pop up window.
- Do not add the FreeRTOS component when asked in the pop up window.
- Right-click on the
USART driver instance 0 dependency box, and satisfy the dependency
with the UART6 PLIB as shown in the following figure:Note: In MPLAB Harmony, the driver corresponding to the UART and USART, is the USART driver.
Figure 4-9. Configuring MPLAB Harmony v3 USART Driver 1 - Click on the upper
side of the USART driver box on the project graph to see the USART
driver common configuration options in the window on the right. Let
the Driver Mode be Asynchronous as shown in the following
figure:
Figure 4-10. Configuring MPLAB Harmony v3 USART Driver 2 - Click on the lower
side of the USART driver box on the project graph to see the USART
driver instance 0 configuration options in the window on the right.
Leave the configurations set as is. Notice the ‘PLIB Used’ is
automatically configured as UART6. If UART6 is not shown, that means
the USART driver is not yet connected with the UART6 PLIB, and this
connection needs to be made.
Figure 4-11. Configuring MPLAB Harmony v3 USART Driver 3
- Generate the code using the MCC.
- Update the application:
- The UART PLIB and driver initialization code is already generated by the MCC, which is based on the configuration described in the step 2.2 and 2.3.
- Update the
app.c
file and theapp.h
file with application logic. The following figure shows the application logic to be developed in theapp.c
file, which has three states:- Open the driver: This state is compulsory for MPLAB Harmony v3 drivers as they are multi-client. Which means even if driver has one client, the application needs to open the driver before using it.
- Queue transfer request: This state adds the transfer request in the queue.
- Check status of the
transfer: This state checks the transfer status. As with MPLAB
Harmony v2, the status of the transfer can be checked by these
methods:
- Polling: Application continuously polls for the transfer status using an API. In this example the polling method is used.
- Callback: Callback can be registered using a dedicated API which will be called once the transfer completes.
uint8_t consoleMsg[] = "Hello World\n\r";
void APP_Tasks ( void )
{
/* Check the application's current state. */
switch ( appData.state )
{
/* Application's initial state. */
case APP_STATE_OPEN_DRIVER:
{
appData.myUSARTHandle = DRV_USART_Open(DRV_USART_INDEX_0, DRV_IO_INTENT_READWRITE|DRV_IO_INTENT_NONBLOCKING);
if (appData.myUSARTHandle != DRV_HANDLE_INVALID)
{
appData.state = APP_STATE_ADD_REQUEST;
}
break;
}
case APP_STATE_ADD_REQUEST:
{
DRV_USART_WriteBufferAdd(appData.myUSARTHandle, &consoleMsg[0], sizeof(consoleMsg), &appData.bufferHandle);
appData.state = APP_STATE_STATUS_CHECK;
break;
}
case APP_STATE_STATUS_CHECK:
{
if (DRV_USART_BUFFER_EVENT_COMPLETE & DRV_USART_BufferStatusGet(appData.bufferHandle))
{
// Data has been transmitted, go to next state
appData.state = APP_STATE_COMPLETE;
}
break;
}