/* * I2C.c * * Created on: Jun 6, 2018 * Author: avi */ #include #include #include "include.h" #include "inc/hw_memmap.h" #include "inc/hw_types.h" #include "inc/tm4c1294ncpdt.h" #include "driverlib/gpio.h" #include "driverlib/pin_map.h" #include "driverlib/rom.h" #include "driverlib/rom_map.h" #include "driverlib/sysctl.h" #include "driverlib/i2c.h" #include "I2C.h" #include "inc/hw_i2c.h" //#include "i2c_fifo.h" #define I2C_DELAY 3000 uint8_t Main_Mux_Flag = UNKNOWN; //uint32_t I2C_BUSY_DELAY = 100; uint32_t I2C_2_3_BUSY_DELAY = 3000; //I2C2_BASE, I2C3_BASE (3000 only for first communication changed to 100 in main) uint32_t I2C_4_BUSY_DELAY = 3000; //I2C4_BASE (RFID + Dispensers) (RFUD working only with 3000) void InitI2C2() { // //Enable GPIO for Configuring the I2C Interface Pins SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOG); // // Wait for the Peripheral to be ready for programming // while(!SysCtlPeripheralReady(SYSCTL_PERIPH_GPIOG)); // // Configure Pins for I2C2 Master Interface // GPIOPinConfigure(GPIO_PG2_I2C2SCL); GPIOPinConfigure(GPIO_PG3_I2C2SDA); GPIOPinTypeI2C(GPIO_PORTG_BASE, GPIO_PIN_3); GPIOPinTypeI2CSCL(GPIO_PORTG_BASE, GPIO_PIN_2); // GPIOPadConfigSet(GPIO_PORTG_BASE,GPIO_PIN_2,GPIO_STRENGTH_2MA,GPIO_PIN_TYPE_STD_WPU); // needed if using the EVB // GPIOPadConfigSet(GPIO_PORTG_BASE,GPIO_PIN_3,GPIO_STRENGTH_2MA,GPIO_PIN_TYPE_STD_WPU); } void Configuring_Master_I2C2(/*uint32_t ui32SysClock*/) { uint32_t ui32SysClock; ui32SysClock = SysCtlClockFreqSet((SYSCTL_OSC_MAIN | SYSCTL_USE_PLL | SYSCTL_XTAL_25MHZ | SYSCTL_CFG_VCO_480), 120000000); // // Stop the Clock, Reset and Enable I2C Module // in Master Function // SysCtlPeripheralDisable(SYSCTL_PERIPH_I2C2); SysCtlPeripheralReset(SYSCTL_PERIPH_I2C2); SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C2); // // Wait for the Peripheral to be ready for programming // while(!SysCtlPeripheralReady(SYSCTL_PERIPH_I2C2)); // // Initialize and Configure the Master Module // #ifdef I2C_2_Data_Transfer_Rate_400kbps I2CMasterInitExpClk(I2C2_BASE, ui32SysClock, true);//The last parameter sets the I2C data transfer rate: false for 100kbps and true for 400kbps #else I2CMasterInitExpClk(I2C2_BASE, ui32SysClock, false);//The last parameter sets the I2C data transfer rate: false for 100kbps and true for 400kbps #endif //i2c_fifo_setup(I2C2_BASE); } //-------------------------------------------------------------- void InitI2C3() { // //Enable GPIO for Configuring the I2C Interface Pins SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOR); // // Wait for the Peripheral to be ready for programming // while(!SysCtlPeripheralReady(SYSCTL_PERIPH_GPIOR)); // // Configure Pins for I2C2 Master Interface // GPIOPinConfigure(GPIO_PR4_I2C3SCL); GPIOPinConfigure(GPIO_PR5_I2C3SDA); GPIOPinTypeI2C(GPIO_PORTR_BASE, GPIO_PIN_5); GPIOPinTypeI2CSCL(GPIO_PORTR_BASE, GPIO_PIN_4); // GPIOPadConfigSet(GPIO_PORTR_BASE,GPIO_PIN_4,GPIO_STRENGTH_2MA,GPIO_PIN_TYPE_STD_WPU); // needed if using the EVB // GPIOPadConfigSet(GPIO_PORTR_BASE,GPIO_PIN_5,GPIO_STRENGTH_2MA,GPIO_PIN_TYPE_STD_WPU); } void Configuring_Master_I2C3(/*uint32_t ui32SysClock*/) { uint32_t ui32SysClock; ui32SysClock = SysCtlClockFreqSet((SYSCTL_OSC_MAIN | SYSCTL_USE_PLL | SYSCTL_XTAL_25MHZ | SYSCTL_CFG_VCO_480), 120000000); // // Stop the Clock, Reset and Enable I2C Module // in Master Function // SysCtlPeripheralDisable(SYSCTL_PERIPH_I2C3); SysCtlPeripheralReset(SYSCTL_PERIPH_I2C3); SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C3); // // Wait for the Peripheral to be ready for programming // while(!SysCtlPeripheralReady(SYSCTL_PERIPH_I2C3)); // // Initialize and Configure the Master Module // #ifdef I2C_3_Data_Transfer_Rate_400kbps I2CMasterInitExpClk(I2C3_BASE, ui32SysClock, true);//The last parameter sets the I2C data transfer rate: false for 100kbps and true for 400kbps #else I2CMasterInitExpClk(I2C3_BASE, ui32SysClock, false);//The last parameter sets the I2C data transfer rate: false for 100kbps and true for 400kbps #endif //i2c_fifo_setup(I2C3_BASE); } void InitI2C4() { // //Enable GPIO for Configuring the I2C Interface Pins SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOR); // // Wait for the Peripheral to be ready for programming // while(!SysCtlPeripheralReady(SYSCTL_PERIPH_GPIOR)); // // Configure Pins for I2C2 Master Interface // GPIOPinConfigure(GPIO_PR6_I2C4SCL); GPIOPinConfigure(GPIO_PR7_I2C4SDA); GPIOPinTypeI2C(GPIO_PORTR_BASE, GPIO_PIN_7); GPIOPinTypeI2CSCL(GPIO_PORTR_BASE, GPIO_PIN_6); // GPIOPadConfigSet(GPIO_PORTR_BASE,GPIO_PIN_6,GPIO_STRENGTH_2MA,GPIO_PIN_TYPE_STD_WPU); // needed if using the EVB // GPIOPadConfigSet(GPIO_PORTR_BASE,GPIO_PIN_6,GPIO_STRENGTH_2MA,GPIO_PIN_TYPE_STD_WPU); } void Configuring_Master_I2C4(/*uint32_t ui32SysClock*/) { uint32_t ui32SysClock; ui32SysClock = SysCtlClockFreqSet((SYSCTL_OSC_MAIN | SYSCTL_USE_PLL | SYSCTL_XTAL_25MHZ | SYSCTL_CFG_VCO_480), 120000000); // // Stop the Clock, Reset and Enable I2C Module // in Master Function // SysCtlPeripheralDisable(SYSCTL_PERIPH_I2C4); SysCtlPeripheralReset(SYSCTL_PERIPH_I2C4); SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C4); // // Wait for the Peripheral to be ready for programming // while(!SysCtlPeripheralReady(SYSCTL_PERIPH_I2C4)); // // Initialize and Configure the Master Module // #ifdef I2C_4_Data_Transfer_Rate_400kbps I2CMasterInitExpClk(I2C4_BASE, ui32SysClock, true);//The last parameter sets the I2C data transfer rate: false for 100kbps and true for 400kbps #else I2CMasterInitExpClk(I2C4_BASE, ui32SysClock, false);//The last parameter sets the I2C data transfer rate: false for 100kbps and true for 400kbps #endif //i2c_fifo_setup(I2C4_BASE); } void Init_All_I2C() { //I2C switches address: 0xE0, 0xE4, 0xE3 InitI2C4(); Configuring_Master_I2C4(); //I2C switches address: 0xE0, 0xE4, 0xE3 + I2C-ADC address: 0x40, 0x44, 0x46 InitI2C3(); Configuring_Master_I2C3(); //I2C-DAC address: 0x98 + I2C EEPROM address: 0xA0 InitI2C2(); Configuring_Master_I2C2(); } void Write_I2C(uint32_t I2C_BASE,uint32_t Slave_Add, uint8_t Byte2Write) { //#define SLAVE_ADDRESS_EXT (0xE0>>1) //Slave_Add = Slave_Add #define NUM_OF_I2CBYTES 1 uint8_t g_ui8MasterBytes = 1 ; uint8_t g_ui8MasterTxData = Byte2Write; //TODO uint8_t g_ui8MasterBytesLength = 1; // ***************************************************************************** // // Define for I2C Module // // ***************************************************************************** // Send the Slave Address with RnW as Transmit // and First Data Byte. Based on Number of bytes the // command would be either START or FINISH // I2CMasterSlaveAddrSet(I2C_BASE, Slave_Add, false); //TODO Add here 'for' to send more than 1 byte I2CMasterDataPut(I2C_BASE, g_ui8MasterTxData); if(g_ui8MasterBytes == g_ui8MasterBytesLength) { I2CMasterControl(I2C_BASE, I2C_MASTER_CMD_SINGLE_SEND); } else { I2CMasterControl(I2C_BASE, I2C_MASTER_CMD_BURST_SEND_START); } SysCtlDelay(I2C_DELAY);//15000 } uint8_t Read_I2C(uint32_t I2C_BASE,uint32_t Slave_Add) { // Start Read from configured slave I2CMasterSlaveAddrSet(I2C_BASE, Slave_Add, true); I2CMasterControl(I2C_BASE, I2C_MASTER_CMD_BURST_RECEIVE_START); SysCtlDelay(I2C_DELAY);//15000 uint8_t get = I2CMasterDataGet(I2C_BASE); I2CMasterControl(I2C_BASE, I2C_MASTER_CMD_BURST_RECEIVE_FINISH); SysCtlDelay(I2C_DELAY);//15000 //uint8_t get1 = I2CMasterDataGet(I2C4_BASE); return get; } uint8_t Write_Single_I2C(uint32_t I2C_BASE,uint32_t Slave_Add, uint8_t Byte2Write) { #define NUM_OF_I2CBYTES 1 //uint8_t g_ui8MasterBytes = 1 ; uint8_t g_ui8MasterTxData = Byte2Write; //TODO //uint8_t g_ui8MasterBytesLength = 1; // ***************************************************************************** // // Define for I2C Module // // ***************************************************************************** // Send the Slave Address with RnW as Transmit // and First Data Byte. Based on Number of bytes the // command would be either START or FINISH // I2CMasterSlaveAddrSet(I2C_BASE, Slave_Add, false); I2CMasterDataPut(I2C_BASE, g_ui8MasterTxData); I2CMasterControl(I2C_BASE, I2C_MASTER_CMD_SINGLE_SEND); SysCtlDelay(I2C_DELAY);//15000 return OK; } uint8_t Read_Single_I2C(uint32_t I2C_BASE,uint32_t Slave_Add) { // Start Read from configured slave I2CMasterSlaveAddrSet(I2C_BASE, Slave_Add, true); I2CMasterControl(I2C_BASE, I2C_MASTER_CMD_SINGLE_RECEIVE); SysCtlDelay(I2C_DELAY);//15000 uint8_t get = I2CMasterDataGet(I2C_BASE); return get; } uint32_t I2C_control(uint32_t I2C_ID, uint32_t Slave_Add, bool RW,uint32_t Byte2Write ) { uint32_t I2C_BASE; if (I2C_ID == 4) I2C_BASE = I2C4_BASE; else if (I2C_ID == 3) I2C_BASE = I2C3_BASE; else if (I2C_ID == 2) I2C_BASE = I2C2_BASE; else return ERROR; /* if ( (Slave_Add != 0xE0) && (Slave_Add != 0xE4) && (Slave_Add != 0xE3) & (Slave_Add != 0x40) && (Slave_Add != 0x44) && (Slave_Add != 0x46) & (Slave_Add != 0x98) && (Slave_Add != 0xA0) ) return ERROR;*/ Slave_Add = Slave_Add>>1; Byte2Write = Byte2Write & 0xFF; if(RW == 0 ) { Write_I2C(I2C_BASE, Slave_Add,Byte2Write); return 0; } else { //uint8_t value = Read_I2C(I2C_BASE, Slave_Add); uint8_t value = Read_Single_I2C(I2C_BASE, Slave_Add); return value; } } //-------------------------------------------------------------------------------------- #define I2C_ID_MUX_TCA9548A_0xE4 0x03 #define I2C3_MAIN_MUX_TCA9548A_0xE4 0xE4//Slave_Add uint32_t Block_Main_MUX_TCA9548A_0xE4() ////unused I2C MUX { uint32_t status= OK; uint8_t Write_Buf[1]; Write_Buf[0] = 0x00;//Block all status = I2C_Write(I2C_ID_MUX_TCA9548A_0xE4, I2C3_MAIN_MUX_TCA9548A_0xE4, Write_Buf, 1); return status; }