/*******************************************************************************
  MPLAB Harmony Application Source File

  Company:
    Microchip Technology Inc.

  File Name:
    app_drv_usart2.c

  Summary:
    This file contains the source code for the MPLAB Harmony application.

  Description:
    This file contains the source code for the MPLAB Harmony application.  It
    implements the logic of the application's state machine and it may call
    API routines of other MPLAB Harmony modules in the system, such as drivers,
    system services, and middleware.  However, it does not call any of the
    system interfaces (such as the "Initialize" and "Tasks" functions) of any of
    the modules in the system or make any assumptions about when those functions
    are called.  That is the responsibility of the configuration-specific system
    files.
 *******************************************************************************/

// *****************************************************************************
// *****************************************************************************
// Section: Included Files
// *****************************************************************************
// *****************************************************************************

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

// *****************************************************************************
// *****************************************************************************
// Section: Global Data Definitions
// *****************************************************************************
// *****************************************************************************
DRV_HANDLE  Uart2Handle;
DRV_USART_BUFFER_HANDLE Uart2WriteTransferHandle;
DRV_USART_BUFFER_HANDLE Uart2ReadTransferHandle;
volatile int Uart2WriteComplete = false;
volatile int Uart2ReadComplete = false;
char Uart2WriteBuf[50];
char Uart2ReadBuf[50];

// *****************************************************************************
/* Application Data

  Summary:
    Holds application data

  Description:
    This structure holds the application's data.

  Remarks:
    This structure should be initialized by the APP_DRV_USART2_Initialize function.

    Application strings and buffers are be defined outside this structure.
*/

APP_DRV_USART2_DATA app_drv_usart2Data;

void USART2write( unsigned char *pdata, int len )
{
    if( len > 0 && len < sizeof(Uart2WriteBuf) )
    {
        memcpy(Uart2WriteBuf, pdata, len);

        DRV_USART_WriteBufferAdd(Uart2Handle, Uart2WriteBuf, len, &Uart2WriteTransferHandle);

        while( DRV_USART_BufferStatusGet(Uart2WriteTransferHandle)!=DRV_USART_BUFFER_EVENT_COMPLETE )
        {}
    }
}

// *****************************************************************************
// *****************************************************************************
// Section: Application Callback Functions
// *****************************************************************************
// *****************************************************************************
void Uart2TransferHandler( 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:
        if(bufferHandle == Uart2ReadTransferHandle)
            Uart2ReadComplete = true;
        if(bufferHandle == Uart2WriteTransferHandle)
            Uart2WriteComplete = true;
        break;
    case DRV_USART_BUFFER_EVENT_HANDLE_EXPIRED:
    case DRV_USART_BUFFER_EVENT_ERROR:
    case DRV_USART_BUFFER_EVENT_HANDLE_INVALID:
        app_drv_usart2Data.state = APP_DRV_USART2_STATE_ERROR;
        break;
    }
}

// *****************************************************************************
// *****************************************************************************
// Section: Application Local Functions
// *****************************************************************************
// *****************************************************************************


/* TODO:  Add any necessary local functions.
*/


// *****************************************************************************
// *****************************************************************************
// Section: Application Initialization and State Machine Functions
// *****************************************************************************
// *****************************************************************************

/*******************************************************************************
  Function:
    void APP_DRV_USART2_Initialize ( void )

  Remarks:
    See prototype in app_drv_usart2.h.
 */

void APP_DRV_USART2_Initialize ( void )
{
    /* Place the App state machine in its initial state. */
    app_drv_usart2Data.state = APP_DRV_USART2_STATE_INIT;



    /* TODO: Initialize your application's state machine and other
     * parameters.
     */
}


/******************************************************************************
  Function:
    void APP_DRV_USART2_Tasks ( void )

  Remarks:
    See prototype in app_drv_usart2.h.
 */

void APP_DRV_USART2_Tasks ( void )
{
    switch ( app_drv_usart2Data.state )
    {
    case APP_DRV_USART2_STATE_INIT:
        Uart2Handle = DRV_USART_Open(DRV_USART_INDEX_1, DRV_IO_INTENT_READWRITE);
        if(Uart2Handle == DRV_HANDLE_INVALID)
        {
            app_drv_usart2Data.state = APP_DRV_USART2_STATE_ERROR;
        }
        else
        {
            USART2write((unsigned char*)"DRV_USART2: Hello world\r\n", 25);
            // sprintf(Uart2WriteBuf, "DRV_USART2: Hello world\r\n");
            // DRV_USART_WriteBufferAdd(Uart2Handle, Uart2WriteBuf, strlen(Uart2WriteBuf), &Uart2WriteTransferHandle);
            // while( DRV_USART_BufferStatusGet(Uart2WriteTransferHandle)!=DRV_USART_BUFFER_EVENT_COMPLETE )
            // {}

            DRV_USART_BufferEventHandlerSet(Uart2Handle, Uart2TransferHandler, 0);

            app_drv_usart2Data.state = APP_DRV_USART2_STATE_READ;
        }
        break;

    case APP_DRV_USART2_STATE_READ:
        DRV_USART_ReadBufferAdd(Uart2Handle, Uart2ReadBuf, 1, &Uart2ReadTransferHandle);
        if(Uart2ReadTransferHandle == DRV_USART_BUFFER_HANDLE_INVALID)
        {
            app_drv_usart2Data.state = APP_DRV_USART2_STATE_ERROR;
        }
        else
        {
            app_drv_usart2Data.state = APP_DRV_USART2_STATE_READ_WAIT_COMPLETE;
        }
        break;

    case APP_DRV_USART2_STATE_READ_WAIT_COMPLETE:
        if(Uart2ReadComplete == true)
        {
            Uart2ReadComplete = false;
            app_drv_usart2Data.state = APP_DRV_USART2_STATE_READ; // Modify this Line
            app_drv_usart1Data.state = APP_DRV_USART1_STATE_WRITE;				
        }
        break;

    case APP_DRV_USART2_STATE_WRITE:
        sprintf(Uart2WriteBuf, "Receive : %s (DRV_USART1)\r\n", Uart1ReadBuf); // Modify this Line
        DRV_USART_WriteBufferAdd(Uart2Handle, Uart2WriteBuf, strlen(Uart2WriteBuf), &Uart2WriteTransferHandle);
        if(Uart2WriteTransferHandle == DRV_USART_BUFFER_HANDLE_INVALID)
        {
            app_drv_usart2Data.state = APP_DRV_USART2_STATE_ERROR;
        }
        else
        {
            app_drv_usart2Data.state = APP_DRV_USART2_STATE_WRITE_WAIT_COMPLETE;
        }
        break;

    case APP_DRV_USART2_STATE_WRITE_WAIT_COMPLETE:
        if(Uart2WriteComplete == true)
        {
            Uart2WriteComplete = false;
            app_drv_usart2Data.state = APP_DRV_USART2_STATE_READ_WAIT_COMPLETE; // Modify this Line				
        }
        break;

    case APP_DRV_USART2_STATE_ERROR:
        app_drv_usart2Data.state = APP_DRV_USART2_STATE_IDLE;
        break;

    case APP_DRV_USART2_STATE_IDLE:
        break;
    }
}


/*******************************************************************************
 End of File
 */
