/* * i2c_init.c * * Created on: 5 сент. 2023 г. * Author: seklyuts */ #include "f28x_project.h" // // Function to configure I2CA as Master Transmitter. // #define I2C_SLAVE_ADDRESS 0x6AU #define I2C_OWN_ADDRESS 0x30U #define MAX_BUFFER_SIZE 0x10 #define I2C_NUMBYTES 0x2U uint16_t I2C_TXdata[MAX_BUFFER_SIZE]; uint16_t I2C_RXdata[MAX_BUFFER_SIZE]; // // I2C GPIO pins // #define GPIO_PIN_SDAA 26U // GPIO number for I2C SDAA #define GPIO_PIN_SCLA 27U // GPIO number for I2C SCLA void I2CMasterGpioInit(void) { // //Configure I2C pins // GPIO_SetupPinMux(GPIO_PIN_SDAA, GPIO_MUX_CPU1, 11); GPIO_SetupPinOptions(GPIO_PIN_SDAA, GPIO_OUTPUT, GPIO_PULLUP); GPIO_SetupPinMux(GPIO_PIN_SCLA, GPIO_MUX_CPU1, 11); 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); I2caRegs.I2CPSC.all = 0xB; // Prescaler - need 7-12 Mhz on module clk I2caRegs.I2CCLKL = 0x7; // NOTE: must be non zero I2caRegs.I2CCLKH = 0x8; // 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; } // // Function to send data over I2C. // void I2CWrite(uint16_t slaveAddr, uint16_t byteCount, bool sendStopCondition) { // // 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.STT = 0x1; // //transmit the bytes // for(index=0; index < I2C_NUMBYTES; index++) { I2caRegs.I2CDXR.all= I2C_TXdata[index]; // //wait till byte is sent // while(I2caRegs.I2CSTR.bit.BYTESENT != 0x1); // //clear the byte sent // I2caRegs.I2CSTR.bit.BYTESENT = 0x1; } // // Send STOP condition if specified // if(sendStopCondition) { I2caRegs.I2CMDR.bit.STP = 0x1; while(I2caRegs.I2CMDR.bit.STP != 0x0); I2caRegs.I2CSTR.bit.BYTESENT = 0x1; } } // // Function to read data over I2C. Returns the number of bytes read // uint16_t I2CRead(uint16_t slaveAddr, uint16_t byteCount, bool sendStopCondition) { // // 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.STT = 0x1; uint16_t count = 0; // // Read the received data into RX buffer // while(count < I2C_NUMBYTES) { if(I2caRegs.I2CSTR.bit.RRDY ==0x1) { I2C_RXdata[count] = I2caRegs.I2CDRR.all; count++; } } // // Send STOP condition // if(sendStopCondition) { I2caRegs.I2CMDR.bit.STP = 0x1; while(I2caRegs.I2CMDR.bit.STP != 0x0); I2caRegs.I2CSTR.bit.BYTESENT = 0x1; } return count; }