Post Go back to editing

TWI/I2C 21489 C code example


I'm trying to develop a link between the ADSP 21489 and ADT7411. I have to use the I2C protocol. Could someone point me to a C code example demonstrating an I2C transaction for the ADSP 21489.


  • Hi,

          Please see the attached example code. This uses the TWI in master mode. You may use this as reference.

    Best Regards,


  • Hi Jeyanthi,

    Thanks for your post. After posting my question, I looked around the Analog Devices Forum and found some examples in assembly (posted by Analog Devices Engineers) and started the work in C. I also came across the example that you attached. That example was posted by someone else in another post thread since the examples provided by Analog Devices were only available in Assembly. The attached example is a good example of TWI Master, however I have a couple of questions for you.

    1. Inside the TWI_Transmit_Data_Polling function, it is sending N bytes (in this case 10). Would that really work? Don't we need to send 1 byte in each transmission and for each transmission repeat the sequence of events by resending the slave address before the data?

    2. In the same TWI_Transmit_Data_Polling, wouldn't we need to restart (TWIRSTART) ? Is this in the Master_TWI_Init taking care of that when init is called ?

    // Enable the master control register

    *pTWIMCTL = (N << 6) ;


    Thank You!


  • Hi Jeyanthi,

    Could you take a look at the attached code ? Do you see any issues ? I'm not able to write any bytes. It gets stuck in the first while loop while checking TWIFIFOSTAT (FIFO Status Register). Any input would be appreciated.

    Thank You!

  • Hi Deco,

             To answer your questions:

    1. The TWI hardware takes care of sending the slave address once the register is programmed. The user need not have to send the address everytime. The code I have shared sends N bytes as programmed.

    2. If you want to use the Repeated Start condition then you can set this bit.

    I had gone through the code you have sent. I am not clear on what exactly you are trying to do. I can see that the master is enabled multiple times as below:

    *pTWIMCTL = TWIMEN | (1<<6);

        *pTWIMADDR = device_address;
        *pTWIMCTL = TWIMEN | (1<<6) | TWIRSTART;
        while(*pTWIFIFOSTAT & TWITXS) {};
        // enable rx transfer
        *pTWIMCTL = TWIMEN | (1<<6) | TWIMDIR;

    Can you enable the master only once for more no of data and check what happens to the TWITXS status? Also check for the error conditions in the current code which might explain on why the data is not transferred.

    I would also recommend you to use the interrupt mode of operation.

    Best Regards,


  • Hi Jeyanthi,

    Thanks again for your response. I'm trying to write to and read from a Analog Devices Temperature Sensor. The file I attached in previous post was for attempting to read a register from Sensor. Let me explain, what I was attempting to do. On the ADT7411 Manual,  page 30, It describes steps to be able to read a register from sensor "Writing to the Address Pointer Register for a Subsequent Read: To read data from a particular register, the address pointer register must contain the address of that register. If it does not, the correct address must be written to the address pointer register by performing a single-byte write operation, as shown in Figure 34. The write operation consists of the serial bus address followed by the address pointer byte. No data is written to any of the data registers. A read operation is then performed to read the register." Since the read is essentially a write and then a read, I decided to focus on getting the write working first (please see attached file). For the write, I set the device (sensor) address and then write the register address as per instructed by the Temperature Sensor manual "Writing Data to a Register: All registers are 8-bit registers so only one byte of data can be written to each register. Writing a single byte of data to one of these read/write registers consists of the serial bus address, the data register address written to the address pointer register, followed by the data byte written to the selected data register"

    The problem: I'm trying to write 1 byte to the temperature sensor (fast mode) and a specific register in the sensor. I'm polling until the transmission is successful. No data is being transmitted. After setting the master mode adress and writing sensor register, I enable transfer and it gets stuck at the  while(*pTWIFIFOSTAT & TWITXS) {}; When I look at Visual DSP 5.0 TWI Registers TWIFIFOSTAT is 1 and TWITXS is 1. Data is not transferred. Do you have any recommendations ?

    Thanks for your help!


  • Hi Deco,

                I will go through the datasheet of the part you have mentioned and come back with more information. You may also refer the temperature sensor interface application note for the sensor available on the EZ-KIT:

    This EE-Note has an example code associated with this which uses the TWI for programming the sensor interface. You may use this as reference as well.

    Best Regards,


  • Hi Jeyanthi,

    I figured out the issue and now able to talk to sensor. The problem had to do with incorrect SRU programming and HW configuration . Thanks for your help!


  • Where did you end up with the SRU programming.

    This is what you had.

    SRU(LOW,DPI_PB11_I); // Since TWI output is an open-drain output, the TWI pin is connected
                         // to logic level low
    SRU(TWI_DATA_PBEN_O,DPI_PBEN11_I); // TWI data output connected to DPI pin 11 input
    SRU(DPI_PB11_O, TWI_DATA_I); // DPI pin 11 output connected to TWI data input
    SRU(LOW,DPI_PB12_I); // Since TWI output is an open-drain output, the TWI pin is connected
                         // to logic level low
    SRU2(TWI_CLK_PBEN_O,DPI_PBEN12_I); // TWI clock connected to DPI pin 12

    Greg Rasche

  • Hi Greg,

    Were you able to get the code working ?

    Please let us know the problems that you are facing in your code



  • This works - greg

        // TWI Data
        SRU(LOW,DPI_PB11_I);      // Since I2C output is an open drain output the I2C pin is connected to logic level low
        SRU(TWI_DATA_PBEN_O,DPI_PBEN11_I);//I2C SDATA connected to DPI pin 11.  Disables DPI pin 11 as input
        SRU(DPI_PB11_O,TWI_DATA_I); // I2C SDATA input is connected to DPI pin 11 output


        // TWI Clock
        SRU(TWI_CLK_PBEN_O,DPI_PBEN12_I); //I2C Clock connected to DPI pin 12