New Project :
---------------------------------------------------------------------
Microchip Embedded \ 32-bit MCC Harmony Project
---------------------------------------------------------------------
Framework Path : HarmonyFramework_MCC
---------------------------------------------------------------------
Location : C:\Exercises\Lab11_DRV_USART_SingleClient
Folder   : Lab11_DRV_USART_SingleClient
---------------------------------------------------------------------
Device Filter : ATSAMD21G18A
---------------------------------------------------------------------
Select MPLAB Harmony
---------------------------------------------------------------------
Required Content (Default selected)
csp 3.16.0
dev_packs 3.16.0
harmony-services 1.2.0
quick_docs 1.5.0
---------------------------------------------------------------------
Optional Content (Check Box) 
\usb 3.10.0
\core 3.12.0
\wireless_wifi 3.8.0
\mhc 3.8.5
---------------------------------------------------------------------
Finish
=====================================================================


!! Add below code in main.c !!
---------------------------------------------------------------------
SYS_TIME_HANDLE tmrHandle;

---------------------------------------------------------------------
    SYS_TIME_DelayMS(1000, &tmrHandle);

---------------------------------------------------------------------
        if( SYS_TIME_DelayIsComplete(tmrHandle) == true )
        {
            LED1_Toggle();
            SYS_TIME_DelayMS(1000, &tmrHandle);
        }

=====================================================================


!! Add below code in app_drv_usart1.h !!
---------------------------------------------------------------------
    // APP_DRV_USART1_STATE_SERVICE_TASKS,
    APP_DRV_USART1_STATE_READ,
    APP_DRV_USART1_STATE_READ_WAIT_COMPLETE,
    APP_DRV_USART1_STATE_WRITE,
    APP_DRV_USART1_STATE_WRITE_WAIT_COMPLETE,
    APP_DRV_USART1_STATE_ERROR,
    APP_DRV_USART1_STATE_IDLE,

=====================================================================


!! Add below code in app_drv_usart1.c !!
---------------------------------------------------------------------
    switch ( app_drv_usart1Data.state )
    {
    case APP_DRV_USART1_STATE_INIT:
        app_drv_usart1Data.state = APP_DRV_USART1_STATE_READ;
        break;

    case APP_DRV_USART1_STATE_READ:
        app_drv_usart1Data.state = APP_DRV_USART1_STATE_READ_WAIT_COMPLETE;
        break;

    case APP_DRV_USART1_STATE_READ_WAIT_COMPLETE:
        app_drv_usart1Data.state = APP_DRV_USART1_STATE_WRITE;
        break;

    case APP_DRV_USART1_STATE_WRITE:
        app_drv_usart1Data.state = APP_DRV_USART1_STATE_WRITE_WAIT_COMPLETE;
        break;

    case APP_DRV_USART1_STATE_WRITE_WAIT_COMPLETE:
        app_drv_usart1Data.state = APP_DRV_USART1_STATE_READ;
        break;

    case APP_DRV_USART1_STATE_ERROR:
        app_drv_usart1Data.state = APP_DRV_USART1_STATE_IDLE;
        break;

    case APP_DRV_USART1_STATE_IDLE:
        break;
    }

---------------------------------------------------------------------
#include "driver/usart/drv_usart.h"
#include <stdio.h>
#include <string.h>

---------------------------------------------------------------------
DRV_HANDLE Uart1Handle;
DRV_USART_BUFFER_HANDLE Uart1WriteTransferHandle;
DRV_USART_BUFFER_HANDLE Uart1ReadTransferHandle;
volatile int Uart1WriteComplete = false;
volatile int Uart1ReadComplete = false;
char Uart1WriteBuf[50];
char Uart1ReadBuf[50];

---------------------------------------------------------------------
        Uart1Handle = DRV_USART_Open(DRV_USART_INDEX_0, DRV_IO_INTENT_READWRITE);
        if(Uart1Handle == DRV_HANDLE_INVALID)
        {
            app_drv_usart1Data.state = APP_DRV_USART1_STATE_ERROR;
        }
        else
        {
            sprintf(Uart1WriteBuf, "DRV_USART1: Hello world\r\n");
            DRV_USART_WriteBufferAdd(Uart1Handle, Uart1WriteBuf, strlen(Uart1WriteBuf), &Uart1WriteTransferHandle);
            while( DRV_USART_BufferStatusGet(Uart1WriteTransferHandle)!=DRV_USART_BUFFER_EVENT_COMPLETE )
            {}
            app_drv_usart1Data.state = APP_DRV_USART1_STATE_READ;
        }

=====================================================================
void Uart1TransferHandler( DRV_USART_BUFFER_EVENT event, DRV_USART_BUFFER_HANDLE bufferHandle, uintptr_t context )
{
    switch( event )
    {
    case DRV_USART_BUFFER_EVENT_PENDING: break;
    case DRV_USART_BUFFER_EVENT_COMPLETE:
        break;
    case DRV_USART_BUFFER_EVENT_HANDLE_EXPIRED:
    case DRV_USART_BUFFER_EVENT_ERROR:
    case DRV_USART_BUFFER_EVENT_HANDLE_INVALID:
        app_drv_usart1Data.state = APP_DRV_USART1_STATE_ERROR;
        break;
    }
}

---------------------------------------------------------------------
            DRV_USART_BufferEventHandlerSet(Uart1Handle, Uart1TransferHandler, 0);

---------------------------------------------------------------------
        if(bufferHandle == Uart1ReadTransferHandle)
            Uart1ReadComplete = true;

---------------------------------------------------------------------
        DRV_USART_ReadBufferAdd(Uart1Handle, Uart1ReadBuf, 1, &Uart1ReadTransferHandle);
        if(Uart1ReadTransferHandle == DRV_USART_BUFFER_HANDLE_INVALID)
        {
            app_drv_usart1Data.state = APP_DRV_USART1_STATE_ERROR;
        }
        else
        {
            app_drv_usart1Data.state = APP_DRV_USART1_STATE_READ_WAIT_COMPLETE;
        }

---------------------------------------------------------------------
        if(Uart1ReadComplete == true)
        {
            Uart1ReadComplete = false;
            app_drv_usart1Data.state = APP_DRV_USART1_STATE_WRITE;
        }

---------------------------------------------------------------------
        if(bufferHandle == Uart1WriteTransferHandle)
            Uart1WriteComplete = true;

---------------------------------------------------------------------
        sprintf(Uart1WriteBuf, "Receive : %s (DRV_USART1)\r\n", Uart1ReadBuf);
        DRV_USART_WriteBufferAdd(Uart1Handle, Uart1WriteBuf, strlen(Uart1WriteBuf), &Uart1WriteTransferHandle);
        if(Uart1WriteTransferHandle == DRV_USART_BUFFER_HANDLE_INVALID)
        {
            app_drv_usart1Data.state = APP_DRV_USART1_STATE_ERROR;
        }
        else
        {
            app_drv_usart1Data.state = APP_DRV_USART1_STATE_WRITE_WAIT_COMPLETE;
        }

---------------------------------------------------------------------
        if(Uart1WriteComplete == true)
        {
            Uart1WriteComplete = false;
            app_drv_usart1Data.state = APP_DRV_USART1_STATE_READ;				
        }

=====================================================================
#include <stdarg.h>

---------------------------------------------------------------------
void USART1printf( const char *format, ... )
{
    size_t len=0;
    va_list args = {0};

    va_start( args, format );
    len = vsnprintf( Uart1WriteBuf, sizeof(Uart1WriteBuf), format, args );
    va_end( args );

    if( len > 0 && len < sizeof(Uart1WriteBuf) )
    {
        Uart1WriteBuf[len] = '\0';
        DRV_USART_WriteBufferAdd(Uart1Handle, Uart1WriteBuf, strlen(Uart1WriteBuf), &Uart1WriteTransferHandle);
        while( DRV_USART_BufferStatusGet(Uart1WriteTransferHandle)!=DRV_USART_BUFFER_EVENT_COMPLETE )
        {}
    }	
}

---------------------------------------------------------------------
            USART1printf("DRV_USART1: Hello world\r\n");
            // sprintf(Uart1WriteBuf, "DRV_USART1: Hello world\r\n");
            // DRV_USART_WriteBufferAdd(Uart1Handle, Uart1WriteBuf, strlen(Uart1WriteBuf), &Uart1WriteTransferHandle);
            // while( DRV_USART_BufferStatusGet(Uart1WriteTransferHandle)!=DRV_USART_BUFFER_EVENT_COMPLETE )
            // {}

=====================================================================



!! Add below code in app_drv_usart1.h !!
---------------------------------------------------------------------
void USART1printf( const char *format, ... );

---------------------------------------------------------------------
