//#############################################################################
// $Copyright:
// Copyright (C) 2017-2023 Texas Instruments Incorporated - http://www.ti.com/
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
//   Redistributions of source code must retain the above copyright
//   notice, this list of conditions and the following disclaimer.
//
//   Redistributions in binary form must reproduce the above copyright
//   notice, this list of conditions and the following disclaimer in the
//   documentation and/or other materials provided with the
//   distribution.
//
//   Neither the name of Texas Instruments Incorporated nor the names of
//   its contributors may be used to endorse or promote products derived
//   from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// $
//#############################################################################


//! \file   /solutions/tidm_02010_dmpfc/common/include/communication.h
//!
//! \brief  header file to be included in all labs
//!         support for dmpfc board with F28002x/F28003x/F280013x
//!


#ifndef COMMUNICATION_H
#define COMMUNICATION_H


//*****************************************************************************
//
// If building with a C++ compiler, make all of the definitions in this header
// have a C binding.
//
//*****************************************************************************
#ifdef __cplusplus
extern "C"
{
#endif

//*****************************************************************************
//
//! \defgroup COMMUNICATION
//! @{
//
//*****************************************************************************

// Included Files
#include "driverlib.h"
#include "device.h"

// I2C message states for I2CMsg struct
#define I2C_HIGH_ADDR               0x00
#define I2C_LOW_ADDR                0x30
#define I2C_FIFO_LEVEL              16
#define I2C_NUM_BYTES               8
#define I2C_BUFFER_SIZE             8       // Max is currently 14 because of
                                            // 2 address bytes and the 16-byte
                                            // FIFO

#define I2C_MAX_7_BIT_ADDRESS       127U
#define I2C_MAX_10_BIT_ADDRESS      1023U

// I2C message states for I2CMsg struct
#define I2C_STATUS_INACTIVE         0x0000  // Message not in use, do not send
#define I2C_STATUS_SEND_WITHSTOP    0x0010  // Send message with stop bit
#define I2C_STATUS_WRITE_BUSY       0x0011  // Message sent, wait for stop
#define I2C_STATUS_SEND_NOSTOP      0x0020  // Send message without stop bit
#define I2C_STATUS_SEND_NOSTOP_BUSY 0x0021  // Message sent, wait for ARDY
#define I2C_STATUS_RESTART          0x0022  // Ready to become master-receiver
#define I2C_STATUS_READ_BUSY        0x0023  // Wait for stop before reading data

// Error messages for read and write functions
#define I2C_DO_AT24C32_D1           0x0100
#define I2C_DO_TPL1401_D1           0x0200
#define I2C_DO_TPL1401_D2           0x0300
#define I2C_DO_OPT3001_D1           0x0400

#define I2C_ERROR_BUS_BUSY          0x1000
#define I2C_ERROR_NACK_RECEIVED     0x2000
#define I2C_ERROR_ARBITRATION_LOST  0x3000
#define I2C_ERROR_NO_DEVICE         0x4000
#define I2C_ERROR_STOP_NOT_READY    0x5555
#define I2C_SUCCESS                 0x0000

#define I2C_AT24C32_D1_ADDR         0x0050

// Typedefs
typedef struct _I2CMsg_Obj_
{
    uint16_t msgStatus;                 // Word stating what state msg is in.
                                        // See MSG_STATUS_* defines above.
    uint16_t busStatus;

    uint16_t activeDevice;

    uint16_t *pTxMsgBuffer;             // Pointer to RX message buffer
    uint16_t *pRxMsgBuffer;             // Pointer to RX message buffer

    uint16_t slaveAddr;                 // Slave address tied to the message
    uint16_t numOfAddrBytes;
    uint16_t numOfDataBytes;            // Number of valid bytes in message
    uint16_t memoryHighAddr;             // EEPROM address of data associated
                                         // with message (high byte).
    uint16_t memoryLowAddr;              // EEPROM address of data associated
                                         // with message (low byte).
    uint16_t delayTime_us;              //  Delay time in microsecs (us)
} I2CMsg_Obj;


//! \brief      Defines the I2CMsg handle
typedef struct _I2CMsg_Obj_ *I2CMsg_Handle;


extern I2CMsg_Obj i2cMsgData;
extern I2CMsg_Handle i2cMsgHandle;

extern uint16_t i2cMsgTxBuffer [I2C_BUFFER_SIZE];  // Array holding message data.
extern uint16_t i2cMsgRxBuffer [I2C_BUFFER_SIZE];  // Array holding message data.

// I2C functions
// I2C Scan Bus State
extern void I2C_scanBusState(I2CMsg_Handle msgHandle, uint32_t i2cBase);

// Function to send the data that is to be written to the EEPROM
extern uint16_t I2C_writeData(I2CMsg_Handle msgHandle, uint32_t i2cBase);

// Function to prepare for the data that is to be read from the EEPROM
extern uint16_t I2C_readData(I2CMsg_Handle msgHandle, uint32_t i2cBase);

extern uint16_t I2C_checkBusStatus(uint32_t i2cBase);
extern uint16_t I2C_handleNACK(uint32_t i2cBase);

extern void I2C_writeAT24C32(I2CMsg_Handle msgHandle, uint32_t i2cBase);
extern void I2C_readAT24C32(I2CMsg_Handle msgHandle, uint32_t i2cBase);


//*****************************************************************************
//
// Close the Doxygen group.
//! @}
//
//*****************************************************************************

//*****************************************************************************
//
// Mark the end of the C bindings section for C++ compilers.
//
//*****************************************************************************
#ifdef __cplusplus
}
#endif // extern "C"

#endif // end of COMMUNICATION_H definition
