This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Simple Transmit/Receive Program for C6713 I2C

Other Parts Discussed in Thread: CCSTUDIO, TEST2

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

  • Danielle,

    Attached is a sample of some code I used on the C6713 DSP chip, for an I2C master.

    Is is polled, rather than interrupt driven, with the polling triggered by a 1mS periodic SWI routine in DSPBios.  But you could do the polling based on some other event, if you like.

    I am not sure how familiar you are with I2C protocols, so here is a brief overview:
    The Start and Stop events are hardware sequences on the interface wires that define the beginning and
    end of a transfer.  A 'Device Address' is a 7-bit number that selects a slave device on the I2C bus).  The
    LSB of the Device Address byte is reserved for the Read/Write flag (0=write, 1=read, from the Master's perspective).  The sub-address is
    typically used to select a register within a device.  The sub-address is typically the first byte sent to the slave device, after the device address byte has been sent.

    After a sub-address byte has been sent, the master has the option of sending a 'Repeated Start' which is a Start condition followed by another device address byte, which gives the master an opportunity to set the R/W flag to 1, to turn the bus around  - i.e. to put the bus in Read mode.

    I hope this helps to get you started.

    Steve

    I2C_master_sample.zip
  • Thanks.  I was hoping to find example code using the csl functions that I've been attempting to work with, but I'll definitely look through your program, as it should at least help me to understand the process.  I appreciate the explanation as well.

    Still, does anyone happen to know why page 23 of SPRU175D states that the I2C module goes from master-transmitter mode to slave-receiver mode?  That still makes no sense to me.

  • Danielle,

    The I2C specification allows for multiple bus master devices.  At the end of a bus transfer, the DSP will revert to slave mode, to allow another bus master to start a transfer on the bus.  If you look at the flow diagram on page 23 again, you will notice that each new transfer by the DSP starts by checking to see if the bus is 'Busy' - i.e. if another bus master has a transaction in progress.

    The code sample I sent makes the assumption that it is the only master on the bus, which simplifies the code a bit.

    Steve

  • Danielle,

    One thing to think about is 'blocking I/O'.  The I2C bus interface involves many instances of 'wait for a status bit to change', or 'wait for data to arrive'.

    These wait states can be very long, relative to the time the DSP spends doing other tasks (like signal processing).

    The code example I sent is designed to be called periodically, and to then 'get in and get out' of the code as quickly as possible.  So the state machine will check to see if there is any change in status to process, and if it still needs to wait for the hardware, it will return control and try again on the next pass of the state machine invocation.

    A solution that sticks in a loop, waiting for some hardware event (like your PIC processor to respond), will be difficult to integrate into a signal processing system that has absolute time constraints.

    Steve

  • Steve,

    Thanks for the insight.  The master-transmitter to slave-receiver transition makes sense now, as does the bus busy check.  I completely forgot about the fact that more than one device can act as master.

    As for blocking I/O, that makes a lot of sense.  I'm planning to use this code within an interrupt-driven program for audio manipulation, so it might be a good idea for me to avoid those long wait times as well.

    Again, thank you so much for your help!  I think I can make this work now.

    Danielle

  • Steve, thanks for all your contributions.  I was reading through this thread to see if I could add anything but it looks like you have it pretty well covered!

    Danielle, could you please mark the post(s) that answer your question as "Verified" on the forum so that others will know the question is resolved?

    Thanks!  :)

  • Brad,

    I have been using I2C quite a bit, so that is one area that I have experience with.

    Most of my DSP/I2C experience has been with the C6726B chip, but I started out with a C6713 Eval board for my initial code development.

    Steve

  • Hi ,Brad Griffis 

    there is a demo code about I2C without using interrupt in the pspdriver,but now I want to read and write data on I2C bus using interrupt on dm6437.

    would you offer me a demo code about that? or what should i do?

    thank you 

     

  • Hello Sir,

    we tried using the attached code but while compiling the file we are coming across the following error

     [main.c] "C:\CCStudio_v3.1\C6000\cgtools\bin\cl6x" -g -fr"C:/CCStudio_v3.1/MyProjects/test2/Debug" -d"_DEBUG" -mv6700 -@"../../../../../CCStudio_v3.1/MyProjects/test2/Debug.lkf" "main.c"
    "C:/CCStudio_v3.1/C6000/csl/include/csl_chiphal.h", line 262: fatal error: #error NO CHIP DEFINED (use -dCHIP_XXXX where XXXX is chip number, i.e. 6201)
    1 fatal error detected in the compilation of "main.c".
    Compilation terminated.

    can you please help us with this problem.

    thanks,

    viral.

  • Sir,

     I need the c  source code for QPSK transmitter and receiver which can be run in ccs studio with dsk6713.If u can please reply me with the answer.

  • Steve,

    Do you have an example using this code?

    Thanks,

    John