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

📄 evmdm642_i2c.c

📁 ti dm642 board library, good for dm642 developer.
💻 C
字号:

/*
 *  Copyright 2005 by  Zhongxon Digital Incorporated.
 *  All rights reserved. Property of Zhongxon Digital Incorporated.
 */

/*
 *  ======== evmdm642_i2c.c ========
 *  EVMDM642 board initializion implementation.
 */

#include <csl.h>

#include <csl_i2c.h>
#include "evmdm642_i2c.h"



#define I2CDELAY(iterations) {  \
volatile Uint32 j; 				 \
for(j = 0; j < iterations; j ++); \
} 

#define DELAY_TIME 1000


I2C_Config prevIICConfig; 
I2C_Handle EVMDM642_I2C_hI2C;

/*****************************************************************/
static  I2C_Config IIC_InitConfig = {
    0x20,  /* master mode,  i2coar;   */
    0,  /* no interrupt, i2cimr;   */
    (20-5), /* scl low time, i2cclkl;  */
    (20-5), /* scl high time,i2cclkh;  */
    1,  /* configure later, i2ccnt;*/
    0,  /* configure later, i2csar;*/
    0x46a0, /* master tx mode,     */
            /* i2c runs free,      */
            /* 8-bit data + ACK    */
            /* repeat mode      */
    (75-1), /* 4MHz clock, i2cpsc  */
};    
/*--------------------------------------------------------------*/    
static  I2C_Config IIC_WriteConfig = {
    0x20,  /* master mode,  i2coar;   */
    0,  /* no interrupt, i2cimr;   */
    (20-5), /* scl low time, i2cclkl;  */
    (20-5), /* scl high time,i2cclkh;  */
    1,  /* configure later, i2ccnt;*/
    0,  /* configure later, i2csar;*/
    0x46a0, /* master tx mode,     */
            /* i2c runs free,      */
            /* 8-bit data + NACK   */
            /* repeat mode      */
    (75-1), /* 4MHz clock, i2cpsc  */
};
/*--------------------------------------------------------------*/ 
static  I2C_Config IIC_ReadConfig = {
    0x20,  /* master mode,  i2coar;   */
    0,  /* no interrupt, i2cimr;   */
    (20-5), /* scl low time, i2cclkl;  */
    (20-5), /* scl high time,i2cclkh;  */
    1,  /* configure later, i2ccnt;*/
    0,  /* configure later, i2csar;*/
    0x44a0, /* master rx mode,     */
            /* i2c runs free,      */
            /* 8-bit data + ACK    */
            /* repeat mode      */
    (75-1), /* 4MHz clock, i2cpsc  */
};

/*****************************************************************/

/*
 * ======== EVMDM642_IIC_init ========
 * This function performs  I2C bus init .
 */
void EVMDM642_IIC_init()
{ 
/* Open I2C handle */
	EVMDM642_I2C_hI2C = I2C_open(I2C_DEV0,I2C_OPEN_RESET);
/* make sure handle is valid */
	if(EVMDM642_I2C_hI2C == INV) return;
/* Wait until bus is free */
	while (I2C_bb(EVMDM642_I2C_hI2C));
	I2CDELAY(DELAY_TIME);
/* Configure I2C controller */	
	I2C_config(EVMDM642_I2C_hI2C, &IIC_InitConfig);
	
}


/*
 * ======== EVMDM642_IIC_write ========
 * This function performs write operation via I2C bus.
 */

void EVMDM642_IIC_write(I2C_Handle hI2C,
              Uint8 devAddress,
              Uint32  subAddress,
              Uint8 *data,
              Uint16  numBytes
              )
{             
    Int i;
    
    
    
    /* make sure handle is valid */
    if(hI2C == INV) {
       return;
   }
    
    /* Wait until bus is free */
    while (I2C_bb(hI2C));

    /* save old settings */
    I2C_getConfig(hI2C, &prevIICConfig);

    /* set I2C mode register */
    I2C_RSETH(hI2C, I2CMDR, IIC_WriteConfig.i2cmdr);
    
    /* set I2C imr register  */
    I2C_RSETH(hI2C, I2CIMR, IIC_WriteConfig.i2cimr);
    
    /* configure the I2C slave address register */
    I2C_RSETH(hI2C, I2CSAR, devAddress);
    
    /* set I2C count register */
    I2C_RSETH(hI2C, I2CCNT, numBytes + 1);
    
    /* write the sub address */
    I2C_writeByte(hI2C, subAddress);
    
    /* Generate start condition */
    I2C_start(hI2C);
    
    I2CDELAY(DELAY_TIME);

    /* write the data */ 
    for(i = 0; i < numBytes; i ++) {
        while(!I2C_xrdy(hI2C));
        I2C_writeByte(hI2C, *data ++);
        I2CDELAY(DELAY_TIME);
    }

    /* Generate stop condition */
    I2C_sendStop(hI2C); 
    
    I2CDELAY(DELAY_TIME);        
    /* Wait until bus is free */
    while (I2C_bb(hI2C));

    I2CDELAY(DELAY_TIME);        
    /* now restore the previous I2C settings */
    I2C_config(hI2C, &prevIICConfig);
    
    I2CDELAY(DELAY_TIME);        
} 

/*
 * ======== EVMDM642_IIC_read ========
 * This function performs read operation via I2C bus.
 */   
void EVMDM642_IIC_read(I2C_Handle hI2C,
              Uint8 devAddress,
              Uint32  subAddress,
              Uint8 *data,
              Uint16  numBytes
              )
{

    Int i;
    I2C_Config prevIICConfig; 
    
    /* make sure handle is valid */
    if(hI2C == INV) {
        return;
    }
    
    /* Wait until bus is free */
    while (I2C_bb(hI2C));

    /* save old settings */
    I2C_getConfig(hI2C, &prevIICConfig);

    /* set I2C mode register */
    I2C_RSETH(hI2C, I2CMDR, IIC_WriteConfig.i2cmdr);
    
    /* set I2C imr register  */
    I2C_RSETH(hI2C, I2CIMR, IIC_WriteConfig.i2cimr);
    
    /* configure the I2C slave address register */
    I2C_RSETH(hI2C, I2CSAR, devAddress);

    /* set I2C count register */
    I2C_RSETH(hI2C, I2CCNT, 1);
    
    /* write the sub address */
    I2C_writeByte(hI2C, subAddress);

    /* Generate start condition */
    I2C_start(hI2C);
    
    I2CDELAY(DELAY_TIME);

    /* waiting for sub-address to be transmitted */
    while(!I2C_xrdy(hI2C));

    /* now enter the master receiver mode */
    I2C_RSETH(hI2C, I2CMDR, IIC_ReadConfig.i2cmdr);

    /* set I2C count register */
    I2C_RSETH(hI2C, I2CCNT, numBytes);

    /* clear the DRR register */ 
    I2C_readByte(hI2C);
    /* Generate start condition */
    I2C_start(hI2C);
    
    I2CDELAY(DELAY_TIME);

    /* read the data */ 
    for(i = 0; i < numBytes; i ++) {
        while(!I2C_rrdy(hI2C));
        *data++ = I2C_readByte(hI2C);
        I2CDELAY(DELAY_TIME);
    }

    /* Generate stop condition */
    I2C_sendStop(hI2C); 
    
    I2CDELAY(DELAY_TIME);

    /* Wait until bus is free */
    while (I2C_bb(hI2C));

    I2CDELAY(DELAY_TIME);        
    /* now restore the previous I2C settings */
    I2C_config(hI2C, &prevIICConfig);
    
    I2CDELAY(DELAY_TIME);        
   
}




⌨️ 快捷键说明

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