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

📄 i2c270.c

📁 dm270 source code
💻 C
字号:
/*
    DM270 ARM Evaluation Software

    (c)Texas Instruments 2003
*/

/**
    \file i2c270.c
    \brief I2C related APIs
*/
#include <i2c270.h>
#include <intc270.h>

#define I2C_NORMAL  0
#define I2C_START   1
#define I2C_STOP    2

#define I2C_WRITE   0
#define I2C_READ    1

#define I2C_SET_DEV_ADDR(devAddr, dir)  ( ( (devAddr) << 1 )  | ( (dir) & 0x1 ) )

#define I2C_ACK     0
#define I2C_NAK     1

static void   I2C_trigger();
static Uint8  I2C_waitAck();
static STATUS I2C_txData(Uint8 data);


/**
    \brief  Set I2C configuration parameters

    \param i2cConfig    I2C configuration parameters

    \return if success, \c E_PASS, else error code

    \see I2C_ConfigData
*/
STATUS I2C_setConfig( I2C_ConfigData *i2cConfig){
    STATUS status=E_PASS;

    switch(i2cConfig->speed) {
        case I2C_SPEED_100KHZ :
            I2C_FSET( SCS, CLKSEL, 0);
            break;
        case I2C_SPEED_400KHZ :
            I2C_FSET( SCS, CLKSEL, 1);
            break;
        default:
            status=E_INVALID_INPUT;
            break;
    }
    return status;
}

/**
    \brief  Send byte over I2C

    Device address must be sent by using I2C_txStart(), before calling this routine \n
    Use I2C_stop(), to stop I2C communication

    \param  byte        byte to be sent

    \return if success, \c E_PASS, else error code
*/
STATUS I2C_sendByte( Uint8 byte){
    I2C_FSET( SCS, COND, I2C_NORMAL );  // Transfer with normal condition
    return I2C_txData(byte);
}

/**
    \brief  Receive byte over I2C

    Device address must be sent by using I2C_rxStart(), before calling this routine \n
    Use I2C_recvByte( xx, TRUE ), to stop I2C communication

    \param  byte        received byte
    \param  lastByte    TRUE:data byte to be received is the last byte, \n FALSE:data byte to be received is \b NOT the last byte,

    \return if success, \c E_PASS, else error code
*/
STATUS I2C_recvByte( Uint8 *byte, BOOL lastByte){
    I2C_FSET( SCS, COND, I2C_NORMAL );  // Transfer with normal condition
    I2C_FSET( TXDATA, TXD, 0xFF );
    I2C_FSET( TXDATA, ACK, lastByte == TRUE ? I2C_NAK : I2C_ACK );
    I2C_trigger();
    I2C_waitAck();
    *byte = (Uint8)I2C_FGET( RXDATA, RXD);
    return E_PASS;
}

/**
    \brief Start I2C commnication for transmission of data

    \param devAddr      7-bit device address

    \return if success, \c E_PASS, else error code

    \see I2C_sendByte()
*/
STATUS I2C_txStart( Uint8 devAddr){
    I2C_FSET( SCS, COND, I2C_START );   // Transfer with start condition
    return I2C_txData(I2C_SET_DEV_ADDR(devAddr, I2C_WRITE));
}

/**
    \brief Start I2C commnication for receiving data

    \param devAddr      7-bit device address

    \return if success, \c E_PASS, else error code

    \see I2C_recvByte()
*/
STATUS I2C_rxStart( Uint8 devAddr){
    I2C_FSET( SCS, COND, I2C_START );   // Transfer with start condition
    return I2C_txData(I2C_SET_DEV_ADDR(devAddr, I2C_READ));
}

/**
    \brief  Stop I2C communication

    \return if success, \c E_PASS, else error code

    \see I2C_sendByte()
*/
STATUS I2C_stop(){
    I2C_FSET( SCS, COND, I2C_STOP );    // Transfer with stop condition
    I2C_trigger();
    I2C_waitAck();
    return E_PASS;
}


static void I2C_trigger() {
    while( I2C_FGET( SCS, XMIT ) )  // wait for I2C module to be idle
        ;

    // start transfer
    I2C_FSET( SCS, XMIT, 1);
}


static Uint8 I2C_waitAck() {
    //  Wait for the I2C transfer done
    while( INTC_getIntIRQStatus(INT_I2C) )
        ;
    INTC_clearIRQ(INT_I2C);

    return I2C_FGET( RXDATA, ACKS );
}

static STATUS I2C_txData(Uint8 data) {
    I2C_FSET( TXDATA, TXD, data );  // set data to be transmitted
    I2C_FSET( TXDATA, ACK, 1 );     // set ACK high
    I2C_trigger();                  // start transfer

    // wait for transfer to complete
    if( I2C_waitAck() == I2C_ACK )
        return E_PASS;
    else
        return E_DEVICE;
}


⌨️ 快捷键说明

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