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

  Company:
    Microchip Technology Inc.

  File Name:
    app_drv_i2c1.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_i2c1.h"
#include "app_drv_usart1.h"
#include "driver/i2c/drv_i2c.h"
#include "system/time/sys_time.h"
#include <stdio.h>
#include <string.h>

// *****************************************************************************
// *****************************************************************************
// Section: Global Data Definitions
// *****************************************************************************
// *****************************************************************************
#define MCP9800_ADDRESS 0x4D
#define MCP9800_REG_TA  0x0
#define MCP24AA_ADDRESS 0x50

DRV_HANDLE I2C1Handle_9800;
DRV_I2C_TRANSFER_HANDLE I2C1WriteReadHandle_9800;
volatile int I2C1Complete_9800 = false;
char I2C1WriteBuf_9800[20];
char I2C1ReadBuf_9800[20];

DRV_HANDLE I2C1Handle_24AA;
DRV_I2C_TRANSFER_HANDLE I2C1WriteReadHandle_24AA;
volatile int I2C1Complete_24AA = false;
char I2C1WriteBuf_24AA[20];
char I2C1ReadBuf_24AA[20];

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

  Summary:
    Holds application data

  Description:
    This structure holds the application's data.

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

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

APP_DRV_I2C1_DATA app_drv_i2c1Data;

// *****************************************************************************
// *****************************************************************************
// Section: Application Callback Functions
// *****************************************************************************
// *****************************************************************************
void I2C1TransferHandler_9800( DRV_I2C_TRANSFER_EVENT event,
                               DRV_I2C_TRANSFER_HANDLE transferHandle,
                               uintptr_t context )
{
    switch( event )
    {
    case DRV_I2C_TRANSFER_EVENT_PENDING:        break;
    case DRV_I2C_TRANSFER_EVENT_COMPLETE:
        I2C1Complete_9800 = true;
        break;
    case DRV_I2C_TRANSFER_EVENT_HANDLE_EXPIRED:
    case DRV_I2C_TRANSFER_EVENT_ERROR:
    case DRV_I2C_TRANSFER_EVENT_HANDLE_INVALID:
        app_drv_i2c1Data.state = APP_DRV_I2C1_STATE_ERROR;
        break;
    }
}

void I2C1TransferHandler_24AA( DRV_I2C_TRANSFER_EVENT event,
                               DRV_I2C_TRANSFER_HANDLE transferHandle,
                               uintptr_t context )
{
    switch( event )
    {
    case DRV_I2C_TRANSFER_EVENT_PENDING:        break;
    case DRV_I2C_TRANSFER_EVENT_COMPLETE:
        I2C1Complete_24AA = true;
        break;
    case DRV_I2C_TRANSFER_EVENT_HANDLE_EXPIRED:
    case DRV_I2C_TRANSFER_EVENT_ERROR:
    case DRV_I2C_TRANSFER_EVENT_HANDLE_INVALID:
        app_drv_i2c1Data.state = APP_DRV_I2C1_STATE_ERROR;
        break;
    }
}

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


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


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

/*******************************************************************************
  Function:
    void APP_DRV_I2C1_Initialize ( void )

  Remarks:
    See prototype in app_drv_i2c1.h.
 */

void APP_DRV_I2C1_Initialize ( void )
{
    /* Place the App state machine in its initial state. */
    app_drv_i2c1Data.state = APP_DRV_I2C1_STATE_INIT;



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


/******************************************************************************
  Function:
    void APP_DRV_I2C1_Tasks ( void )

  Remarks:
    See prototype in app_drv_i2c1.h.
 */

void APP_DRV_I2C1_Tasks ( void )
{
    switch ( app_drv_i2c1Data.state )
    {
    case APP_DRV_I2C1_STATE_INIT:
        I2C1Handle_9800 = DRV_I2C_Open(DRV_I2C_INDEX_0, DRV_IO_INTENT_READWRITE);
        if(I2C1Handle_9800 == DRV_HANDLE_INVALID)
        {
            app_drv_i2c1Data.state = APP_DRV_I2C1_STATE_ERROR;
            break;
        }
        DRV_I2C_TransferEventHandlerSet(I2C1Handle_9800, I2C1TransferHandler_9800, 0);
		
        I2C1Handle_24AA = DRV_I2C_Open(DRV_I2C_INDEX_0, DRV_IO_INTENT_READWRITE);
        if(I2C1Handle_24AA == DRV_HANDLE_INVALID)
        {
            app_drv_i2c1Data.state = APP_DRV_I2C1_STATE_ERROR;
            break;
        }
		
        I2C1WriteBuf_24AA[0] = 0;
        I2C1WriteBuf_24AA[1] = 0x24;
        DRV_I2C_WriteTransferAdd( I2C1Handle_24AA, MCP24AA_ADDRESS,
                                         I2C1WriteBuf_24AA, 2, &I2C1WriteReadHandle_24AA );
        while( DRV_I2C_TransferStatusGet(I2C1WriteReadHandle_24AA) != 
                                                       DRV_I2C_TRANSFER_EVENT_COMPLETE) {}
        SYS_TIME_HANDLE TimeHandle;
        SYS_TIME_DelayMS(10, &TimeHandle );
        while(!SYS_TIME_DelayIsComplete(TimeHandle)) {}

        USART1printf("Wrote 0x24 to EEPROM 24AA\r\n");

        DRV_I2C_TransferEventHandlerSet(I2C1Handle_24AA, I2C1TransferHandler_24AA, 0);

        app_drv_i2c1Data.state = APP_DRV_I2C1_STATE_WRITE_READ_9800;
        break;

    case APP_DRV_I2C1_STATE_WRITE_READ_9800:
        I2C1WriteBuf_9800[0] = MCP9800_REG_TA;
        DRV_I2C_WriteReadTransferAdd( I2C1Handle_9800, MCP9800_ADDRESS,
                    I2C1WriteBuf_9800, 1, I2C1ReadBuf_9800, 2, &I2C1WriteReadHandle_9800 );
        if(I2C1WriteReadHandle_9800 == DRV_I2C_TRANSFER_HANDLE_INVALID)
        {
            app_drv_i2c1Data.state = APP_DRV_I2C1_STATE_ERROR;
            break;
        }

        app_drv_i2c1Data.state = APP_DRV_I2C1_STATE_WAIT_COMPLETE_9800;
        break;

    case APP_DRV_I2C1_STATE_WAIT_COMPLETE_9800:
        if(I2C1Complete_9800 == true)
        {
            I2C1Complete_9800 = false;
            USART1printf("Temp = %2.1f\r",
                         (float)((I2C1ReadBuf_9800[0]<<4)|(I2C1ReadBuf_9800[1]>>4))/16.0f);

            app_drv_i2c1Data.state = APP_DRV_I2C1_STATE_WRITE_READ_24AA; // Modify this Line
        }
        break;

    case APP_DRV_I2C1_STATE_WRITE_READ_24AA:
        I2C1WriteBuf_24AA[0] = 0;
        DRV_I2C_WriteReadTransferAdd( I2C1Handle_24AA, MCP24AA_ADDRESS,
                    I2C1WriteBuf_24AA, 1, I2C1ReadBuf_24AA, 1, &I2C1WriteReadHandle_24AA );
        if(I2C1WriteReadHandle_24AA == DRV_I2C_TRANSFER_HANDLE_INVALID)
        {
            app_drv_i2c1Data.state = APP_DRV_I2C1_STATE_ERROR;
            break;
        }

        app_drv_i2c1Data.state = APP_DRV_I2C1_STATE_WAIT_COMPLETE_24AA;
        break;

    case APP_DRV_I2C1_STATE_WAIT_COMPLETE_24AA:
        if(I2C1Complete_24AA == true)
        {
            I2C1Complete_24AA = false;
            USART1printf("\t\tEEPROM = 0x%X\r", I2C1ReadBuf_24AA[0]);
			
            app_drv_i2c1Data.state = APP_DRV_I2C1_STATE_WRITE_READ_9800;
        }
        break;

    case APP_DRV_I2C1_STATE_ERROR:
        USART1printf("DRV_I2C1 error!\r\n");

        app_drv_i2c1Data.state = APP_DRV_I2C1_STATE_IDLE;
        break;

    case APP_DRV_I2C1_STATE_IDLE:
        break;
    }
}


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