/* 
 * File:   I2CBUS.h
 * Author: chang
 *
 * Created on November 5, 2024, 7:56 AM
 */

#ifndef I2CBUS_H
#define	I2CBUS_H

#ifdef	__cplusplus
extern "C" {
#endif

#include <stdint.h>
#include <stdbool.h>


    
typedef struct {
    uint8_t I2C_Peripheral_No;  //1 for CAN1 or 2 for CAN2
	uint16_t LogicRule; 
    //uint16_t I2CAddr[MAX_SEQ4ALL_I2C_DEVICE];  //i2c slave address , 7 bits :0 -127  10 bits: 0-1023 , we need for storing each operation's Slave address variable
                                         //index is Seq Number because we need to give out all sequences in simulink
    uint16_t * I2CAddr;
    uint8_t Max_Seq_QTY;  // put into here , easily to code
    uint8_t PresentSeqNo;  //eaily to code
    uint16_t Max_ByteQTY4read;    // for all seqs.  put into here , easily to code
	uint16_t Max_ByteQTY4write;  
    //volatile uint8_t * pReadData;
    //volatile uint8_t * pReadData[MAX_SEQ4ALL_I2C_DEVICE];   //because we read data later (depends on simulibk sample time, but bus access , next time is 1 ms lator), so it may loss reading data
    volatile uint8_t * * pReadData;
    //uint16_t QTY4Read[MAX_SEQ4ALL_I2C_DEVICE];  //actualy need how many to read in current transaction. Index is Seq Number because we need to give out all sequences in simulink
    uint16_t * QTY4Read;
    //volatile uint8_t * pWriteData;  
    //volatile uint8_t * pWriteData[MAX_SEQ4ALL_I2C_DEVICE];  //because we prepare all wring data at the same time in simulink. So if share, it will lose writing data
    volatile uint8_t * * pWriteData;
    //uint16_t QTY4Write[MAX_SEQ4ALL_I2C_DEVICE];  //actualy need how many to write in current transaction. Index is Seq Number because we need to give out all sequences in simulink
    uint16_t * QTY4Write;
    //bool SuccessWR;
    uint16_t SuccessWR;   //bit0: - seq0 bit1: seq1,....
    uint8_t state;  //low layer's state:  physical hardware layer state 
}I2CBUS_t;    
    
  
//#Define I2C Start from Simulink
#define PLATFORM_PIC24_DSPIC30_DSPIC33
#define I2C2_SEQ_MAX_QTY               4
#define USE_I2C2                       1

extern I2CBUS_t I2C_myObj2;

#define I2C2_MAX_BYTE_QTY4READ         4
#define I2C2_MAX_BYTE_QTY4WRITE        5

//#Define I2C End from Simulink
 




void I2CBUS_init(I2CBUS_t * pI2CMyOBJ, uint8_t I2C_Peripheral_No, uint16_t * I2Caddr, uint8_t MAxSeq_Qty, uint16_t * QTY4Read,uint16_t * QTY4Write,
        uint16_t Max_ByteQTY4read, uint16_t Max_ByteQTY4write, volatile uint8_t * Virtual4pReadData[], volatile uint8_t * Virtual4pWriteData[]);

void I2CBus_Poll(I2CBUS_t * pI2CMyOBJ);


#ifdef	__cplusplus
}
#endif

#endif	/* CANBUS_H */































































