#include "DSP28x_Project.h" // Device Headerfile and Examples Include File #define I2C_SLAVE_ADDR 0xC0 #define I2C_NUMBYTES 1 #define I2C_SUBADDR1 0x02 #define I2C_SUBADDR2 0x08 #define I2C_SUBADDR3 0x04 #define I2C_SUBADDR4 0x10 int sendI2C(); int receiveI2C(); void I2CA_Init(void); char data = 0x80; char rxData = 0; void main (void){ InitSysCtrl(); InitGpio(); //Initialize GPIO control registers InitI2CGpio(); //Initiazlze I2C pins. DINT; I2CA_Init(); //Initialize I2C control registers EALLOW; GpioCtrlRegs.GPBDIR.bit.GPIO48 = 1; GpioCtrlRegs.GPBDIR.bit.GPIO49 = 1; //Power on Camera GpioDataRegs.GPBDAT.bit.GPIO48 = 0; GpioDataRegs.GPBDAT.bit.GPIO49 = 0; EDIS; sendI2C(); receiveI2C(); } void I2CA_Init(void){ // Initialize I2C EALLOW; I2caRegs.I2CSAR = 0xC0; // Slave address - Camera control code #if (CPU_FRQ_150MHZ) // Default - For 150MHz SYSCLKOUT I2caRegs.I2CPSC.all = 14; // Prescaler - need 7-12 Mhz on module clk (150/15 = 10MHz) #endif #if (CPU_FRQ_100MHZ) // For 100 MHz SYSCLKOUT I2caRegs.I2CPSC.all = 9; // Prescaler - need 7-12 Mhz on module clk (100/10 = 10MHz) #endif I2caRegs.I2CCLKL = 10; // NOTE: must be non zero I2caRegs.I2CCLKH = 5; // NOTE: must be non zero I2caRegs.I2CIER.all = 0x24; // Enable SCD & ARDY interrupts I2caRegs.I2CMDR.bit.IRS = 1; // Take I2C out of reset // Stop I2C when suspended // Set to master mode I2caRegs.I2CFFTX.all = 0x6000; // Enable FIFO mode and TXFIFO I2caRegs.I2CFFRX.all = 0x2040; // Enable RXFIFO, clear RXFFINT, EDIS; return; } int sendI2C(){ EALLOW; while(I2caRegs.I2CSTR.bit.BB == 1){}; I2caRegs.I2CSAR = I2C_SLAVE_ADDR; //Set slave address //I2caRegs.I2CCNT = I2C_NUMBYTES+1; //Set count to 1 characters plus 1 address bytes I2caRegs.I2CFFTX.all = 0x6000; // Enable FIFO mode and TXFIFO I2caRegs.I2CDXR = I2C_SUBADDR1; //Send camera high address I2caRegs.I2CDXR = data; I2caRegs.I2CMDR.bit.TRX = 1; //Set to Transmit mode I2caRegs.I2CMDR.bit.MST = 1; //Set to Master mode I2caRegs.I2CMDR.bit.FREE = 1; //Run in FREE mode I2caRegs.I2CMDR.bit.STP = 1; //Stop when internal counter becomes 0 I2caRegs.I2CMDR.bit.STT = 1; //Send the start bit, transmission will follow DELAY_US(10); //while(I2caRegs.I2CSTR.bit.XRDY == 0){}; //Do nothing till data is shifted out EDIS; return 1; } int receiveI2C(){ EALLOW; while(I2caRegs.I2CSTR.bit.BB == 1){} //Wait for transmission I2caRegs.I2CSAR = I2C_SLAVE_ADDR; //Set slave address //I2caRegs.I2CCNT = 1; //Set count to 2 address bytes I2caRegs.I2CDXR = I2C_SUBADDR1; //Send camera high address I2caRegs.I2CMDR.bit.TRX = 1; //Set to Transmit mode I2caRegs.I2CMDR.bit.MST = 1; //Set to Master mode I2caRegs.I2CMDR.bit.FREE = 1; //Run in FREE mode I2caRegs.I2CMDR.bit.STP = 0; //Dont release the bus after Tx I2caRegs.I2CMDR.bit.STT = 1; //Send the start bit, transmission will follow while(I2caRegs.I2CSTR.bit.BB == 1){} //Wait for transmission //while(I2caRegs.I2CSTR.bit.XRDY == 0){}; //Do nothing till data is shifted out //I2caRegs.I2CCNT = I2C_NUMBYTES+1; //read byte from camera I2caRegs.I2CMDR.bit.TRX = 0; //Set to Recieve mode I2caRegs.I2CMDR.bit.MST = 1; //Set to Master mode I2caRegs.I2CMDR.bit.FREE = 1; //Run in FREE mode I2caRegs.I2CMDR.bit.STP = 1; //Stop when internal counter becomes 0 I2caRegs.I2CMDR.bit.STT = 1; //Repeated start, Reception will follow //while(I2caRegs.I2CSTR.bit.BB == 1){} while(I2caRegs.I2CSTR.bit.RRDY == 0){}; //I2CDRR not ready to read? rxData = I2caRegs.I2CDRR; while(I2caRegs.I2CSTR.bit.RRDY == 0){}; //I2CDRR not ready to read? rxData = I2caRegs.I2CDRR; EDIS; return 1; }