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:

  1. Create an MPLAB Harmony v3 project.
  2. Configure using the MCC.
    1. Configure the UART pin: Launch the pin manager, go to the Pin Setting window of the MCC and configure the UART Transmit pin.
    2. Follow these steps to configure the UART PLIB:
      1. Add the UART6 Peripheral Library from the Device Resources window.
      2. Click on the added UART6 PLIB box on the project graph to see its configuration options in the window on the right.
      3. 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
    3. Follow these steps to configure the USART Driver:
      1. Add the USART Driver from the Device Resources window.
      2. Add the Core (HarmonyCore) component when asked in the pop up window.
      3. Do not add the FreeRTOS component when asked in the pop up window.
      4. 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
      5. 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
      6. 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
  3. Generate the code using the MCC.
  4. Update the application:
    1. 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.
    2. Update the app.c file and the app.h file with application logic. The following figure shows the application logic to be developed in the app.c file, which has three states:
      1. 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.
      2. Queue transfer request: This state adds the transfer request in the queue.
      3. 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:
        1. Polling: Application continuously polls for the transfer status using an API. In this example the polling method is used.
        2. 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;
        }