MotorControlModuleSDFM_TMS3.../Projects/epwm_test/src/Peripherals/i2c_init.c

244 lines
5.5 KiB
C
Raw Normal View History

/*
* i2c_init.c
*
* Created on: 5 <EFBFBD><EFBFBD><EFBFBD><EFBFBD>. 2023 <EFBFBD>.
* 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, 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;
//
// I2C Interrupts
//
I2caRegs.I2CIER.all = 0x08;
//
// Take I2C out of reset
//
I2caRegs.I2CMDR.all |= 0x0020;
EDIS;
}
void I2CWriteRes(void)
{
I2caRegs.I2CSAR.all = 0xFF;
I2caRegs.I2CMDR.bit.MST = 0x1;
I2caRegs.I2CMDR.bit.TRX = 0x0;
I2caRegs.I2CCNT = 0;
I2caRegs.I2CMDR.bit.STT = 0x1;
I2caRegs.I2CSTR.bit.BYTESENT = 0x1;
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;
I2caRegs.I2CMDR.bit.STP = 0x1;
I2caRegs.I2CSTR.bit.BYTESENT = 0x1;
}
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));
I2caRegs.I2CSTR.bit.BYTESENT = 0x1;
if(TimerTimeouts >= TIME_OVER) {ErrI2c++; return;}
}
//
// Send STOP condition if specified
//
if(sendStopCondition)
{
I2caRegs.I2CMDR.bit.STP = 0x1;
TimerTimeouts = 0;
while((I2caRegs.I2CMDR.bit.STP != 0x0)&&(TimerTimeouts < TIME_OVER));
I2caRegs.I2CSTR.bit.BYTESENT = 0x1;
if(TimerTimeouts >= TIME_OVER) {ErrI2c1++; return;}
}
}
//
// 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(count == (byteCount-1)) {I2caRegs.I2CMDR.bit.NACKMOD = 0x1; I2caRegs.I2CMDR.bit.STP = 0x1;}
if(I2caRegs.I2CSTR.bit.RRDY ==0x1)
{
RXdata = I2C_RXdata[count] = I2caRegs.I2CDRR.all;
count++;
}
}
if(TimerTimeouts >= TIME_OVER) {ErrI2c2 += (byteCount - count); return 0;}
//
// Send STOP condition
//
if(sendStopCondition)
{
I2caRegs.I2CMDR.bit.STP = 0x1;
TimerTimeouts = 0;
while((I2caRegs.I2CMDR.bit.STP != 0x0)&&(TimerTimeouts < TIME_OVER));
I2caRegs.I2CSTR.bit.BYTESENT = 0x1;
if(TimerTimeouts >= TIME_OVER) {ErrI2c3++; return 0;}
}
return count;
}