Hello,
I'm attempting to create a simple I2C program for the C6713 DSK in which the C6713 acts as the master to a PIC16 slave. I've already managed to configure my master clock so that it runs at 100kHz, but I'm having trouble figuring out how to use the C6713 to transmit data to the PIC and then have the PIC send data back based upon what it's received from the C6713. I've read all of SPRU175D, and I've written the following code by following Figure 16 on page 23:
#include "csl.h"
#include "csl_i2c.h"
#include "csl_i2chal.h"
I2C_Handle hI2c;
I2C_Config i2c_config;
void main()
{
DSK6713_init(); // Initialize the board support library
CSL_init(); // Initialize the chip support library
// Open I2c device 0 and reset I2C module:
hI2c=I2C_open(I2C_DEV0,I2C_OPEN_RESET);
// Configure I2C module:
i2c_config.i2cclkh = 35; // Clock high
i2c_config.i2cclkl = 35; // Clock low
i2c_config.i2ccnt = 1; // Transfer 1 data word at a time
i2c_config.i2coar = 0; // Own address register (not needed in master mode)
i2c_config.i2cier = 0; // Interrupts are disabled
i2c_config.i2csar = 0xa1; // Slave address register
i2c_config.i2cmdr = 0x0620; // Mode register
i2c_config.i2cpsc = 13; // Results in prescaled clock frequency of 8MHz
I2C_reset(hI2c); // Reset I2C module to configure
I2C_config(hI2c,&i2c_config); // Configure I2C module using config structure
I2C_outOfReset(hI2c); // Take module out of reset mode
while(1)
{
while(I2C_bb(hI2c)==1) // Wait for bus to be free
{
}
i2c_config.i2cmdr = 0x2E20; // Write I2CMDR with 2E20h
start: // Represents "Read I2CSTR" box in flowchart
if(I2C_EVT_NACK==0 && ARDY==0) // ACK was returned, and MMRs cannot be updated
{
if(I2C_EVT_ICXRDY==0) //Send data is not being requested
goto start;
else
I2C_writeByte(hI2c,0x01); // Write I2CDXR to transmit "1" to PIC
}
else
{
// Reprogram the MMRs (Not sure how to do this)
I2C_sendStop(hI2C); // Generate STOP condition
}
}
}
While I haven't been able to test the above code (since I'm still waiting for some FETs to arrive so I can build a level shifting circuit), I have a feeling that it's incorrect. I2C is completely new to me, as is DSP. I'm not sure how to reprogram the MMRs, or even what the MMRs are (which is why that line is a comment/placeholder rather than actual code). Also, I'm very confused by the text at the bottom of Figure 16, which reads "The I2C module goes into slave-receiver mode". I understand why the I2C module would switch from transmitter to receiver while waiting to get data back from the slave, but could someone please explain why it switches from master to slave? I apologize for not being able to ask more specific questions, but I'm completely lost right now, so any information on how I can make this work would be much appreciated.
Thank you in advance!
Danielle