/* * i2c_init.c * * Created on: 5 сент. 2023 г. * Author: seklyuts */ #include "f28x_project.h" #include "i2c_init.h" // // Function to configure I2CA as Master Transmitter. // // // I2C GPIO pins // #define GPIO_PIN_SDAA 0U // GPIO number for I2C SDAA #define GPIO_PIN_SCLA 1U // GPIO number for I2C SCLA #define TIME_OVER 1000 uint16_t TimerTimeouts = 0, ErrI2c = 0, ErrI2c1 = 0, ErrI2c2 = 0, ErrI2c3 = 0, Addr=0, Addr1[255]; uint16_t RXdata, addrCount=0; void TimerBaseTimeoutInc(void) { TimerTimeouts++; } void I2CMasterGpioInit(void) { // //Configure I2C pins // GPIO_SetupPinMux(GPIO_PIN_SDAA, GPIO_MUX_CPU1, 6); GPIO_SetupPinOptions(GPIO_PIN_SDAA, GPIO_OUTPUT, GPIO_PULLUP); GPIO_SetupPinMux(GPIO_PIN_SCLA, GPIO_MUX_CPU1, 6); GPIO_SetupPinOptions(GPIO_PIN_SCLA, GPIO_OUTPUT, GPIO_PULLUP); } void I2CMasterInit(uint16_t I2C_OwnAddress, uint16_t I2CSlave_Address) { EALLOW; // // Must put I2C into reset before configuring it // I2caRegs.I2CMDR.all &= ~(0x20U); // // I2C configuration. Use a 400kHz I2CCLK with a 50% duty cycle. // //I2C_initMaster(base, DEVICE_SYSCLK_FREQ, 400000, I2C_DUTYCYCLE_50); = 1000000 / (I2CPSC+1) / (I2CCLKL + I2CCLKH) I2caRegs.I2CPSC.all = 49; // Prescaler - need 7-12 Mhz on module clk I2caRegs.I2CCLKL = 12; // NOTE: must be non zero I2caRegs.I2CCLKH = 12; // NOTE: must be non zero // // Configure Master as a Transmitter // I2caRegs.I2CMDR.bit.MST = 0x1; I2caRegs.I2CMDR.bit.TRX = 0x1; // // Set data count // // I2caRegs.I2CCNT = I2C_NUMBYTES; // // Set the bit count to 8 bits per data byte // I2caRegs.I2CMDR.bit.BC = 0x0U; // // Configure slave and own address // I2caRegs.I2COAR.all = I2C_OwnAddress; // Own address I2caRegs.I2CSAR.all = I2CSlave_Address; // Slave address // // Set emulation mode to FREE // I2caRegs.I2CMDR.bit.FREE = 0x1; // //Clear all status // I2caRegs.I2CSTR.all = 0xFFFF; // // Enable I2C Interrupts- RRDY // I2caRegs.I2CIER.all = 0x08; // // Take I2C out of reset // I2caRegs.I2CMDR.all |= 0x0020; } uint16_t j = 0; // // Function to send data over I2C. // void I2CWrite(uint16_t slaveAddr, uint16_t byteCount, bool sendStopCondition, uint16_t * I2C_TXdata) { // // Locals // uint16_t index = 0; // // Configure slave address // I2caRegs.I2CSAR.all = slaveAddr; // Slave address // // Configure I2C as Master Transmitter // I2caRegs.I2CMDR.bit.MST = 0x1; I2caRegs.I2CMDR.bit.TRX = 0x1; // //Set Data Count // I2caRegs.I2CCNT = byteCount; // // send Start condition // // I2caRegs.I2CMDR.bit.STP = 0x1; I2caRegs.I2CMDR.bit.STT = 0x1; // //transmit the bytes // for(index=0; index < byteCount; index++) { I2caRegs.I2CDXR.all= I2C_TXdata[index]; // //wait till byte is sent // TimerTimeouts = 0; while((I2caRegs.I2CSTR.bit.BYTESENT != 0x1)&&(TimerTimeouts < TIME_OVER)); if(TimerTimeouts >= TIME_OVER) ErrI2c++; // else {Addr1[addrCount] = slaveAddr; addrCount++; if(addrCount > 255) addrCount = 0;} // //clear the byte sent // I2caRegs.I2CSTR.bit.BYTESENT = 0x1; } // // Send STOP condition if specified // if(sendStopCondition) { I2caRegs.I2CMDR.bit.STP = 0x1; TimerTimeouts = 0; while((I2caRegs.I2CMDR.bit.STP != 0x0)&&(TimerTimeouts < TIME_OVER)); if(TimerTimeouts >= TIME_OVER) ErrI2c1++; I2caRegs.I2CSTR.bit.BYTESENT = 0x1; } } // // Function to read data over I2C. Returns the number of bytes read // uint16_t ttest=0; uint16_t I2CRead(uint16_t slaveAddr, uint16_t byteCount, bool sendStopCondition, uint16_t * I2C_RXdata) { I2caRegs.I2CMDR.bit.NACKMOD = 0x0; // // Configure slave address // I2caRegs.I2CSAR.all = slaveAddr; // // Configure I2C in Master Receiver mode // I2caRegs.I2CMDR.bit.MST = 0x1; I2caRegs.I2CMDR.bit.TRX = 0x0; uint16_t count = 0; I2caRegs.I2CCNT = byteCount; I2caRegs.I2CMDR.bit.STT = 0x1; // // Read the received data into RX buffer // TimerTimeouts = 0; while((count < (byteCount))&&(TimerTimeouts < TIME_OVER)) { if(I2caRegs.I2CSTR.bit.RRDY ==0x1) { RXdata = I2C_RXdata[count-1] = I2caRegs.I2CDRR.all; count++; } } if(TimerTimeouts >= TIME_OVER) ErrI2c2 += (byteCount - count); // // Send STOP condition // if(sendStopCondition) { I2caRegs.I2CMDR.bit.STP = 0x1; TimerTimeouts = 0; while((I2caRegs.I2CMDR.bit.STP != 0x0)&&(TimerTimeouts < TIME_OVER)); if(TimerTimeouts >= TIME_OVER) ErrI2c3++; I2caRegs.I2CSTR.bit.BYTESENT = 0x1; } return count; } uint16_t I2CWriteRead(uint16_t slaveAddr, uint16_t byteCount, bool sendStopCondition, uint16_t * I2C_RXdata) { // // Locals // // // Configure slave address // I2caRegs.I2CSAR.all = slaveAddr; // Slave address // // Configure I2C as Master Transmitter // I2caRegs.I2CMDR.bit.MST = 0x1; I2caRegs.I2CMDR.bit.TRX = 0x1; // //Set Data Count // I2caRegs.I2CCNT = 1; // // send Start condition // I2caRegs.I2CMDR.bit.STT = 0x1; // //transmit the bytes // I2caRegs.I2CDXR.all= I2C_RXdata[0]; // //wait till byte is sent // TimerTimeouts = 0; while((I2caRegs.I2CSTR.bit.BYTESENT != 0x1)&&(TimerTimeouts < TIME_OVER)); if(TimerTimeouts >= TIME_OVER) ErrI2c++; else Addr = slaveAddr; // //clear the byte sent // I2caRegs.I2CSTR.bit.BYTESENT = 0x1; // // Configure slave address // I2caRegs.I2CSAR.all = slaveAddr; // // Configure I2C in Master Receiver mode // I2caRegs.I2CMDR.bit.MST = 0x1; I2caRegs.I2CMDR.bit.TRX = 0x0; // //Set Data Count // I2caRegs.I2CCNT = byteCount; // // send Start condition // I2caRegs.I2CMDR.bit.STP = 0x1; I2caRegs.I2CMDR.bit.STT = 0x1; uint16_t count = 0; // // Read the received data into RX buffer // TimerTimeouts = 0; while((count < byteCount)&&(TimerTimeouts < TIME_OVER)) { if(count == (byteCount-1)) I2caRegs.I2CMDR.bit.NACKMOD = 0x1; if(I2caRegs.I2CSTR.bit.RRDY ==0x1) { I2C_RXdata[count] = I2caRegs.I2CDRR.all; count++; } } if(TimerTimeouts >= TIME_OVER) ErrI2c2 += (byteCount - count); I2caRegs.I2CMDR.bit.NACKMOD = 0x0; // // Send STOP condition // if(sendStopCondition) { I2caRegs.I2CMDR.bit.STP = 0x1; TimerTimeouts = 0; while((I2caRegs.I2CMDR.bit.STP != 0x0)&&(TimerTimeouts < TIME_OVER)); if(TimerTimeouts >= TIME_OVER) ErrI2c3++; I2caRegs.I2CSTR.bit.BYTESENT = 0x1; } return count; } void I2CWriteReadOnes(uint16_t slaveAddr) { // // Locals // // // Configure slave address // I2caRegs.I2CSAR.all = slaveAddr; // Slave address // // Configure I2C as Master Transmitter // I2caRegs.I2CMDR.bit.MST = 0x1; I2caRegs.I2CMDR.bit.TRX = 0x1; // //Set Data Count // I2caRegs.I2CCNT = 1; // // send Start condition // I2caRegs.I2CMDR.bit.STT = 0x1; // //transmit the bytes // I2caRegs.I2CDXR.all= 0x80; // //wait till byte is sent // TimerTimeouts = 0; while((I2caRegs.I2CSTR.bit.BYTESENT != 0x1)&&(TimerTimeouts < TIME_OVER)); if(TimerTimeouts >= TIME_OVER) ErrI2c++; else Addr = slaveAddr; // //clear the byte sent // I2caRegs.I2CSTR.bit.BYTESENT = 0x1; // // Configure slave address // I2caRegs.I2CSAR.all = slaveAddr; // // Configure I2C in Master Receiver mode // I2caRegs.I2CMDR.bit.MST = 0x1; I2caRegs.I2CMDR.bit.TRX = 0x0; // //Set Data Count // I2caRegs.I2CCNT = 0; // // send Start condition // I2caRegs.I2CMDR.bit.STT = 0x1; // // Read the received data into RX buffer // TimerTimeouts = 0; I2caRegs.I2CMDR.bit.NACKMOD = 0x1; while((I2caRegs.I2CSTR.bit.RRDY != 0x1)&&(TimerTimeouts < TIME_OVER)); if(TimerTimeouts >= TIME_OVER) ErrI2c2++; else RXdata = I2caRegs.I2CDRR.all; I2caRegs.I2CMDR.bit.NACKMOD = 0x0; // // Send STOP condition // I2caRegs.I2CMDR.bit.STP = 0x1; TimerTimeouts = 0; while((I2caRegs.I2CMDR.bit.STP != 0x0)&&(TimerTimeouts < TIME_OVER)); if(TimerTimeouts >= TIME_OVER) ErrI2c3++; I2caRegs.I2CSTR.bit.BYTESENT = 0x1; } void I2CWriteOnse(uint16_t slaveAddr) { // // Locals // //uint16_t index = 0; // // Configure slave address // I2caRegs.I2CSAR.all = slaveAddr; // Slave address // // Configure I2C as Master Transmitter // I2caRegs.I2CMDR.bit.MST = 0x1; I2caRegs.I2CMDR.bit.TRX = 0x1; // //Set Data Count // I2caRegs.I2CCNT = 2; // // send Start condition // I2caRegs.I2CMDR.bit.STT = 0x1; // //transmit the bytes // I2caRegs.I2CDXR.all= 0x80; // //wait till byte is sent // TimerTimeouts = 0; while((I2caRegs.I2CSTR.bit.BYTESENT != 0x1)&&(TimerTimeouts < TIME_OVER)); if(TimerTimeouts >= TIME_OVER) ErrI2c++; else Addr = slaveAddr; // //clear the byte sent // I2caRegs.I2CSTR.bit.BYTESENT = 0x1; I2caRegs.I2CDXR.all= RXdata; // //wait till byte is sent // TimerTimeouts = 0; while((I2caRegs.I2CSTR.bit.BYTESENT != 0x1)&&(TimerTimeouts < TIME_OVER)); if(TimerTimeouts >= TIME_OVER) ErrI2c++; else Addr = slaveAddr; // //clear the byte sent // I2caRegs.I2CSTR.bit.BYTESENT = 0x1; // // Send STOP condition if specified // I2caRegs.I2CMDR.bit.STP = 0x1; TimerTimeouts = 0; while((I2caRegs.I2CMDR.bit.STP != 0x0)&&(TimerTimeouts < TIME_OVER)); if(TimerTimeouts >= TIME_OVER) ErrI2c1++; I2caRegs.I2CSTR.bit.BYTESENT = 0x1; }