⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 davincievm_i2c.c

📁 TI的DM6446的硬件平台搭建的相关例子
💻 C
📖 第 1 页 / 共 2 页
字号:
/*
 *  Copyright 2005 by Spectrum Digital Incorporated.
 *  All rights reserved. Property of Spectrum Digital Incorporated.
 *
 *  Not for distribution.
 */

/*
 *  I2C implementation
 *
 */

#include "davincievm_i2c.h"

/* ------------------------------------------------------------------------ *
 *                                                                          *
 *  DAVINCIEVM_I2C_init( )                                                  *
 *                                                                          *
 *      Enable and initalize the I2C module                                 *
 *                                                                          *
 *      The I2C clk is set to run at 20 KHz                                 *
 *                                                                          *
 * ------------------------------------------------------------------------ */
Int16 DAVINCIEVM_I2C_init( )
{
        CSL_Status status;
        CSL_I2cClkSetup i2c_clksetup;
        CSL_I2cHwSetup  i2c_hwsetup;
        CSL_I2cParam    i2c_param;

        CSL_i2cInit( 0 );
        i2c_handle = CSL_i2cOpen( &i2c_obj, 0, &i2c_param, &status );

        i2c_clksetup.prescalar      = 26;
        i2c_clksetup.clklowdiv      = 20;
        i2c_clksetup.clkhighdiv     = 20;

        i2c_hwsetup.mode            = 1;    // 0: Slave mode        1: Master mode
        i2c_hwsetup.dir             = 0;    // 0: Rx mode           1: Tx mode
        i2c_hwsetup.addrMode        = 0;    // 0: 7-bit mode        1: 10-bit mode
        i2c_hwsetup.sttbyteen       = 0;    // 0: Normal mode       1: Start byte mode
        i2c_hwsetup.ownaddr         = 0;    // #: Own address
        i2c_hwsetup.ackMode         = 0;    // 0: ACK mode          1: NACK mode
        i2c_hwsetup.runMode         = 1;    // 0: No Free run mode  1: Free run mode
        i2c_hwsetup.repeatMode      = 0;    // 0: No repeat mode    1: Repeat mode
        i2c_hwsetup.loopBackMode    = 0;    // 0: No loopback       1: Loopback mode
        i2c_hwsetup.freeDataFormat  = 0;    // 0: No Free data fmt  1: Free data fmt
        i2c_hwsetup.resetMode       = 0;    // 0: Reset             1: Out of reset
        i2c_hwsetup.bcm             = 0;    // 0: Not compatible    1: Compatible
        i2c_hwsetup.inten           = 0;    // #: Intr enable mask
        i2c_hwsetup.clksetup        = &i2c_clksetup;

        status = CSL_i2cHwSetup( i2c_handle, &i2c_hwsetup );

        CSL_i2cHwControl( i2c_handle, CSL_I2C_CMD_OUTOFRESET, 0 );
        return 0;
}

/* ------------------------------------------------------------------------ *
 *                                                                          *
 *  DAVINCIEVM_I2C_close( )                                                 *
 *                                                                          *
 * ------------------------------------------------------------------------ */
Int16 DAVINCIEVM_I2C_close( )
{

        CSL_i2cHwControl( i2c_handle, CSL_I2C_CMD_RESET, 0 );
        CSL_i2cClose( i2c_handle );
        return 0;

}

/* ------------------------------------------------------------------------ *
 *                                                                          *
 *  DAVINCIEVM_I2C_reset( )                                                 *
 *                                                                          *
 *                                                                          *
 *                                                                          *
 * ------------------------------------------------------------------------ */
Int16 DAVINCIEVM_I2C_reset( )
{
    DAVINCIEVM_I2C_close( );
    DAVINCIEVM_I2C_init( );
    return 0;
}

/* ------------------------------------------------------------------------ *
 *                                                                          *
 *  DAVINCIEVM_I2C_write( i2caddr, data, len )                              *
 *                                                                          *
 *      I2C write in Master mode                                            *
 *                                                                          *
 *      i2caddr <- I2C slave address                                        *
 *      data    <- I2C data ptr                                             *
 *      len     <- # of bytes to write                                      *
 *                                                                          *
 * ------------------------------------------------------------------------ */
Int16 DAVINCIEVM_I2C_write( Uint16 i2caddr, Uint8* data, Uint16 len )
{
    Uint16 i;
    Int32 timeout   = 0x20000;
    Int32 timecount = 0;

        Uint16 response;

        CSL_i2cHwControl( i2c_handle, CSL_I2C_CMD_SET_DATA_COUNT, &len );
        CSL_i2cHwControl( i2c_handle, CSL_I2C_CMD_SET_SLAVE_ADDR, &i2caddr );
        CSL_i2cHwControl( i2c_handle, CSL_I2C_CMD_DIR_TRANSMIT, 0 );
        CSL_i2cHwControl( i2c_handle, CSL_I2C_CMD_START, 0 );

        DAVINCIEVM_wait( 10 );

        for ( i = 0 ; i < len ; i++ )
        {
            CSL_i2cWrite( i2c_handle, &data[i] );

            timecount = 0;
            do
            {
                CSL_i2cGetHwStatus( i2c_handle, CSL_I2C_QUERY_TX_RDY, &response );

                timecount++;
                if ( timecount >= timeout )
                {
                    DAVINCIEVM_I2C_reset( );
                    return 1000;
                }

            } while( response == 0 );
        }

      //CSL_i2cHwControl( i2c_handle, CSL_I2C_CMD_STOP, 0 );

        return 0;

}

/* ------------------------------------------------------------------------ *
 *                                                                          *
 *  DAVINCIEVM_I2C_read( i2caddr, data, len )                               *
 *                                                                          *
 *      I2C read in Master mode                                             *
 *                                                                          *
 *      i2caddr <- I2C slave address                                        *
 *      data    <- I2C data ptr                                             *
 *      len     <- # of bytes to write                                      *
 *                                                                          *
 * ------------------------------------------------------------------------ */
Int16 DAVINCIEVM_I2C_read( Uint16 i2caddr, Uint8* data, Uint16 len )
{
    Uint16 i;
    Int32 timeout   = 0x20000;
    Int32 timecount = 0;

        Uint16 response;

        CSL_i2cHwControl( i2c_handle, CSL_I2C_CMD_SET_DATA_COUNT, &len );
        CSL_i2cHwControl( i2c_handle, CSL_I2C_CMD_SET_SLAVE_ADDR, &i2caddr );
        CSL_i2cHwControl( i2c_handle, CSL_I2C_CMD_DIR_RECEIVE, 0 );
        CSL_i2cHwControl( i2c_handle, CSL_I2C_CMD_START, 0 );

        DAVINCIEVM_wait( 10 );

        for ( i = 0 ; i < len ; i++ )
        {
            timecount = 0;
            do
            {
                CSL_i2cGetHwStatus( i2c_handle, CSL_I2C_QUERY_RX_RDY, &response );

                timecount++;
                if ( timecount >= timeout )
                {
                    DAVINCIEVM_I2C_reset( );
                    return 1000;
                }

            } while( response == 0 );

            CSL_i2cRead( i2c_handle, &data[i] );
        }

      //CSL_i2cHwControl( i2c_handle, CSL_I2C_CMD_STOP, 0 );

        return 0;

}

/* ------------------------------------------------------------------------ *
 *                                                                          *
 *  DAVINCIEVM_I2C_read_variable( i2caddr, data, len )                      *
 *                                                                          *
 *      I2C read in Master mode                                             *
 *                                                                          *
 *      i2caddr <- I2C slave address                                        *
 *      data    <- I2C data ptr                                             *
 *      len     <- # of bytes to write                                      *
 *                                                                          *
 * ------------------------------------------------------------------------ */
Int16 DAVINCIEVM_I2C_read_variable( Uint16 i2caddr, Uint8* data, Uint16 len )
{
    Uint16 i;
    Int32 timeout   = 0x20000;
    Int32 timecount = 0;

        Uint16 response;

        CSL_i2cHwControl( i2c_handle, CSL_I2C_CMD_SET_DATA_COUNT, &len );
        CSL_i2cHwControl( i2c_handle, CSL_I2C_CMD_SET_SLAVE_ADDR, &i2caddr );
        CSL_i2cHwControl( i2c_handle, CSL_I2C_CMD_DIR_RECEIVE, 0 );
        CSL_i2cHwControl( i2c_handle, CSL_I2C_CMD_START, 0 );

        DAVINCIEVM_wait( 10 );

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -