4.5 Application Example Using MPLAB Harmony v2 Driver

To create an application to display a message on a computer console using MPLAB Harmony v2 drivers follow these steps:
  1. Create the MPLAB Harmony v2 project.
  2. Configure using the MHC.
    1. Configure the UART pin: Go to the Pin Setting window of the MHC and configure the UART Transmit pin.
    2. Configure the USART driver as shown in the figure below (notice the highlighted options are modified):
      Note: In MPLAB Harmony, the driver corresponding to the UART and USART is the USART driver.
      Figure 4-7. Configuring MPLAB Harmony v2 USART Driver
  3. Generate the code using the MHC.
  4. Follow these steps to update the application:
    1. The UART initialization code is already generated by the MHC based on the configuration, as described in step 2.2, hence it does not required to be added.
    2. Update the app.c file and app.h file with application logic. The following figure shows the application logic to be developed in the app.c file, which has the following three states:
      1. Open the driver: This state is required for the driver model which supports multiple clients. For example, the dynamic driver. It can be skipped for static drivers as they are single client.
      2. Queue transfer requests: This state adds the transfer request in the queue. In this application, there is no back-to-back requests to be queued, but a queueing model has been used for representation.
      3. Check status of the transfer: This state checks the transfer status. The status of the transfer can be checked by these methods:
        1. Polling: Application continuously polls for transfer status using an API. In this example the polling method is used.
        2. Callback: Callback can be registered using a dedicated API which can 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_BufferAddWrite(appData.myUSARTHandle, &appData.bufferHandle, &consoleMsg[0], sizeof(consoleMsg));
            appData.state = APP_STATE_STATUS_CHECK;
            break;
        }
        case APP_STATE_STATUS_CHECK:
        {
            if (DRV_USART_TRANSFER_STATUS_TRANSMIT_EMPTY & DRV_USART_TransferStatus(appData.myUSARTHandle))
            {
                // Data has been transmitted, go to next state
                appData.state = APP_STATE_COMPLETE;
            }
            break;
        }
Note: If the user wants to use a dynamic driver or non-interrupt mode of a driver for this application, the application code in the app.c file must be the same. The only change is the MCC configuration change, as described in the step 2.2.