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.

UBL with I2C

Hi,

I am looking for I2C access in UBL and I need some basic functions like I2C_Write() or I2C_Read().

I have checked many I2C releases, but I only found a I2C_Init().

I am using the DM355, but helpfull would also be the basic functions for any other TI ARM Processor, like the OMAP. As far as I know the I2C modules are pretty much the same on every TI ARM processor.

Thank you for your support,

Sebastian

  • I'm not aware of any UBL examples, but I've attached some basic I2C test code you can use as an example

     

    /*
     *  Copyright 2007 by Spectrum Digital Incorporated.
     *  All rights reserved. Property of Spectrum Digital Incorporated.
     */
    
    /*
     *  I2C implementation
     *
     */
    
    #include "evmdm365_i2c.h"
    #include "evmdm365_eeprom.h"
    
    Int32 i2c_timeout = 0x10000;
    
    /* ------------------------------------------------------------------------ *
     *                                                                          *
     *  _I2C_init( )                                                            *
     *                                                                          *
     *      Enable and initalize the I2C module                                 *
     *      The I2C clk is set to run at 100 KHz                                 *
     *                                                                          *
     * ------------------------------------------------------------------------ */
    Int16 EVMDM365_I2C_init( )
    {
        I2C_ICMDR   = 0;                // Reset I2C
        I2C_ICPSC   = 11;               // Config prescaler for 2MHz
        I2C_ICCLKL  = 5;                // Config clk LOW for 100kHz 
        I2C_ICCLKH  = 5;                // Config clk HIGH for 100kHz
        I2C_ICMDR  |= ICMDR_IRS;        // Release I2C from reset
    
        return 0;
    }
    
    /* ------------------------------------------------------------------------ *
     *                                                                          *
     *  _I2C_close( )                                                           *
     *                                                                          *
     * ------------------------------------------------------------------------ */
    Int16 EVMDM365_I2C_close( )
    {
            I2C_ICMDR = 0;                      // Reset I2C
            return 0;
    }
    
    /* ------------------------------------------------------------------------ *
     *                                                                          *
     *  _I2C_reset( )                                                           *
     *                                                                          *
     * ------------------------------------------------------------------------ */
    Int16 EVMDM365_I2C_reset( )
    {
        EVMDM365_I2C_close( );
        EVMDM365_I2C_init( );
        return 0;
    }
    
    /* ------------------------------------------------------------------------ *
     *                                                                          *
     *  _I2C_write( i2c_addr, data, len )                                       *
     *                                                                          *
     *      I2C write in Master mode                                            *
     *                                                                          *
     *      i2c_addr    <- I2C slave address                                    *
     *      data        <- I2C data ptr                                         *
     *      len         <- # of bytes to write                                  *
     *                                                                          *
     * ------------------------------------------------------------------------ */
    Int16 EVMDM365_I2C_write( Uint16 i2c_addr, Uint8* data, Uint16 len )
    {
        Int32 timeout, i;
    
    
        I2C_ICCNT = len;                    // Set length
        I2C_ICSAR = i2c_addr;               // Set I2C slave address
        I2C_ICMDR = ICMDR_STT               // Set for Master Write
                  | ICMDR_TRX
                  | ICMDR_MST
                  | ICMDR_IRS
                  | ICMDR_FREE;
    
        _wait( 10 );                        // Short delay
    
        for ( i = 0 ; i < len ; i++ )
        {
            I2C_ICDXR = data[i];            // Write
    
            timeout = i2c_timeout;
            do
            {
                if ( timeout-- < 0  )
                {
                    EVMDM365_I2C_reset( );
                    return -1;
                }
            } while ( ( I2C_ICSTR & ICSTR_ICXRDY ) == 0 );// Wait for Tx Ready
        }
    
        I2C_ICMDR |= ICMDR_STP;             // Generate STOP
    
    	while(I2C_ICSTR & 0x1000);
    	EVMDM365_waitusec(30);
    
        return 0;
    }
    
    /* ------------------------------------------------------------------------ *
     *                                                                          *
     *  _I2C_read( i2c_addr, data, len )                                        *
     *                                                                          *
     *      I2C read in Master mode                                             *
     *                                                                          *
     *      i2c_addr    <- I2C slave address                                    *
     *      data        <- I2C data ptr                                         *
     *      len         <- # of bytes to write                                  *
     *                                                                          *
     *      Returns:    0: PASS                                                 *
     *                 -1: FAIL Timeout                                         *
     *                                                                          *
     * ------------------------------------------------------------------------ */
    Int16 EVMDM365_I2C_read( Uint16 i2c_addr, Uint8* data, Uint16 len )
    {
        Int32 timeout, i;
    
        I2C_ICCNT = len;                    // Set length
        I2C_ICSAR = i2c_addr;               // Set I2C slave address
        I2C_ICMDR = ICMDR_STT               // Set for Master Read
                  | ICMDR_MST
                  | ICMDR_IRS
                  | ICMDR_FREE;
    
        _wait( 10 );                        // Short delay
    
        for ( i = 0 ; i < len ; i++ )
        {
            timeout = i2c_timeout;
    
            /* Wait for Rx Ready */
            do
            {
                if ( timeout-- < 0 )
                {
                    EVMDM365_I2C_reset( );
                    return -1;
                }
            } while ( ( I2C_ICSTR & ICSTR_ICRRDY ) == 0 );// Wait for Rx Ready
    
            data[i] = I2C_ICDRR;            // Read
        }
    
        I2C_ICMDR |= ICMDR_STP;             // Generate STOP
    
    	while(I2C_ICSTR & 0x1000);
    	EVMDM365_waitusec(30);
    
        return 0;
    }
    
    /* ------------------------------------------------------------------------ *
     *                                                                          *
     *  _I2C_read_variable( i2c_addr, data, len )                               *
     *                                                                          *
     *      I2C read in Master mode                                             *
     *                                                                          *
     *      i2c_addr <- I2C slave address                                       *
     *      data    <- I2C data ptr                                             *
     *      len     <- # of bytes to write                                      *
     *                                                                          *
     * ------------------------------------------------------------------------ */
    Int16 EVMDM365_I2C_read_variable( Uint16 i2c_addr, Uint8* data, Uint16 len )
    {
        Int32 timeout, i;
    
    
        I2C_ICCNT = len;                    // Set length
        I2C_ICSAR = i2c_addr;               // Set I2C slave address
        I2C_ICMDR = ICMDR_STT               // Set for Master Read
                  | ICMDR_MST
                  | ICMDR_IRS
                  | ICMDR_FREE;
    
        _wait( 10 );                        // Short delay
    
        for ( i = 0 ; i < len ; i++ )
        {
            if ( i == 1 )
            {
                len = data[0];
                I2C_ICCNT = len;            // Set length
            }
    
            timeout = i2c_timeout;
            do
            {
                if ( timeout-- < 0 )
                {
                    EVMDM365_I2C_reset( );
                    return -1;
                }
            } while ( ( I2C_ICSTR & ICSTR_ICRRDY ) == 0 );// Wait for Rx Ready
    
            data[i] = I2C_ICDRR;            // Read
        }
    
        I2C_ICMDR |= ICMDR_STP;             // Generate STOP
    
        return 0;
    }
    
     0172.evmdm365_i2c.h4212.evmdm365.h

  • Hi Marcus!

    The basic I2C functions you sent me work perfectly. I just had to adjust the timing parameter in the I2C_Init() to a higher prescaler clocking rate.

    Thank you for your support!

    Sebastian