TWI/I2C 21489 C code example

Hi,

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.


Thanks!

  • 0
    •  Analog Employees 
    on Aug 13, 2012 1:18 AM

    Hi,

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

    Best Regards,

    Jeyanthi

  • 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) ;

    *pTWIMCTL |= TWIMEN;

    Thank You!

    Deco

  • 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!

  • 0
    •  Analog Employees 
    on Aug 16, 2012 6:47 AM

    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,

    Jeyanthi

  • 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!

    Deco