Tool/software: Code Composer Studio
Hi everyone,
I'm trying to establish I2C communication between the LAUNCHXL-F28027 and Adafruit MCP9808 Precision I2C Temperature Sensor.
I didn't find the eeprom example that intuative so i based my code upon this:
jaraidaniel.blogspot.com/.../i2c-on-c2000-launchpad.html
So basically i wanted to constently read temperature from sensor, here is my code:
#include "DSP28x_Project.h" // Device Headerfile and Examples Include File
Uint16 data[I2C_MAX_BUFFER_SIZE];
void main(void)
{
uint16_t i;
// WARNING: Always ensure you call memcpy before running any functions from RAM
// InitSysCtrl includes a call to a RAM based function and without a call to
// memcpy first, the processor will go "into the weeds"
#ifdef _FLASH
memcpy(&RamfuncsRunStart, &RamfuncsLoadStart, (size_t)&RamfuncsLoadSize);
#endif
// Step 1. Initialize System Control:
// PLL, WatchDog, enable Peripheral Clocks
// This example function is found in the f2802x_SysCtrl.c file.
InitSysCtrl();
// Step 2. Initialize GPIO:
// This example function is found in the f2802x_Gpio.c file and
// illustrates how to set the GPIO to it's default state.
//InitGpio();
// Setup only the GP I/O only for I2C functionality
//InitI2CGpio();
EALLOW;
// GpioCtrlRegs.GPAPUD.bit.GPIO28 = 0; // Enable pull-up for GPIO28 (SDAA)
// GpioCtrlRegs.GPAPUD.bit.GPIO29 = 0; // Enable pull-up for GPIO29 (SCLA)
GpioCtrlRegs.GPBPUD.bit.GPIO32 = 0; // Enable pull-up for GPIO32 (SDAA)
GpioCtrlRegs.GPBPUD.bit.GPIO33 = 0; // Enable pull-up for GPIO33 (SCLA)
// GpioCtrlRegs.GPAQSEL2.bit.GPIO28 = 3; // Asynch input GPIO28 (SDAA)
// GpioCtrlRegs.GPAQSEL2.bit.GPIO29 = 3; // Asynch input GPIO29 (SCLA)
GpioCtrlRegs.GPBQSEL1.bit.GPIO32 = 3; // Asynch input GPIO32 (SDAA)
GpioCtrlRegs.GPBQSEL1.bit.GPIO33 = 3; // Asynch input GPIO33 (SCLA)
// GpioCtrlRegs.GPAMUX2.bit.GPIO28 = 2; // Configure GPIO28 for SDAA operation
// GpioCtrlRegs.GPAMUX2.bit.GPIO29 = 2; // Configure GPIO29 for SCLA operation
GpioCtrlRegs.GPBMUX1.bit.GPIO32 = 1; // Configure GPIO32 for SDAA operation
GpioCtrlRegs.GPBMUX1.bit.GPIO33 = 1; // Configure GPIO33 for SCLA operation
EDIS;
// Step 4. Initialize all the Device Peripherals:
// Step 5. User specific code
//CLK_enableI2cClock(myClk);
//*************************** configuring the I2C module clocks.
I2caRegs.I2CMDR.bit.IRS = 0; //put the I2C module to reset state to config clocks
I2caRegs.I2CPSC.all = 5;//setting IPSC to 5 results in a module clock of 10 MHz (module clock = CPU clock / (IPSC + 1))
//The master clock
//Tmod * (ICCH + d) and Tmod * (ICCL + d) formulas respectively, where Tmod = 1/module clock, and d = 5
//both high-time and low-time to be 12.5 us, resulting in 25 us periodic time, equivalent to 40 kHz I2C frequency
I2caRegs.I2CCLKL = 120;
I2caRegs.I2CCLKH = 120;
I2caRegs.I2CMDR.bit.IRS = 1;//put the I2C module out of reset state
//****************************The bus is now configured and ready to operate.
// load slave address
I2caRegs.I2CSAR = 0x18;
// wait for STOP condition
while (I2caRegs.I2CMDR.bit.STP != 0); //bus is not occupied
// master mode
I2caRegs.I2CMDR.bit.MST = 1; //resets after each stop condition
// generate START condition
I2caRegs.I2CMDR.bit.STT = 1;// resets once the START condition is issued
//*****************************send (write) the register address from which we want to read from to the slave device
// repeat mode
I2caRegs.I2CMDR.bit.RM = 1;// enabling repeat mode, in which a data byte is transmitted each time the I2CDXR register is written to, until a STOP or REPEATED START condition is generated
// transmit mode
I2caRegs.I2CMDR.bit.TRX = 1;//set the TRX bit to switch into transmit mode
// wait for XRDY flag to transmit data
while (I2caRegs.I2CSTR.bit.XRDY != 1);//wait for the XRDY (transmit mode ready) bit of the status register, which means that the data transmit register is ready to accept new data.
//load the byte we want to send, and wait for the transmission to complete by waiting either for an ACK or NACK (error) from the slave
// load data into the transmit register
I2caRegs.I2CDXR = 0x05; //Temperature register (TA)
// wait for XRDY flag to transmit data or ARDY if we get NACKed
while (I2caRegs.I2CSTR.bit.XRDY != 1 || I2caRegs.I2CSTR.bit.ARDY != 1);
// if a NACK is received, clear the NACK bit and stop the transfer
if (I2caRegs.I2CSTR.bit.NACK == 1) {
I2caRegs.I2CMDR.bit.STP = 1;
I2caRegs.I2CSTR.all = I2C_CLR_NACK_BIT;
return I2C_NACK_ERROR;
}
//hopefully, the slave device responded with an ACK, and we can continue our read operation.
//Now that the slave device knows what to send (what we want to read)
// issue repeated start
I2caRegs.I2CMDR.bit.STT = 1;
// non-repeat mode for reading
I2caRegs.I2CMDR.bit.RM = 0;
// set data count
I2caRegs.I2CCNT = 2;// I2C Data Count Register (I2CCNT) determines how many bytes to receive
// generate STOP condition when the data counter counts down to 0
I2caRegs.I2CMDR.bit.STP = 1;
// receiver mode
I2caRegs.I2CMDR.bit.TRX = 0;
//****************************read the bytes the slave device sent us
for (i = 0; i < 2; ++i) {
// wait until the data receive register is ready to be read
while (I2caRegs.I2CSTR.bit.RRDY != 1);
data[i] = I2caRegs.I2CDRR;
}
} // end of main
After runing the code, i checked the SCL pin with oscilloscope and i found nothing. i also checked the I2CDRR register but the value dosn't change.
Could please see what's wrong with the code? or suggest another one?
Thanks for your help,
Cheers,
Younes,