?? i2c.c
字號:
/******************************************************************************* Copyright (C) 2002 by Ingenient Technologies, Inc.* All Rights Reserved.** Use of this software is restricted to the terms and* conditions of Ingenient's software license agreement.** www.ingenient.com********************************************************************************* Filename:** i2c.c** File Description:** This file contains the I2C driver functions*******************************************************************************/#include "i2c.h"/* ************************* Global Variables ************************* */static UINT16 i2c_scl,i2c_sda;static volatile UINT16 *dir_scl, *bitset_scl, *bitclr_scl;static volatile UINT16 *dir_sda, *bitset_sda, *bitclr_sda;/* *********************** Function Prototypes ************************ */static void makeSclLow(void);static void makeSclHigh(void);static void makeSdaLow(void);static void makeSdaHigh(void);static BOOL readSda(void);static void makeSdaInput(void);static void makeSdaOutput(void);static void i2cStart(void);static void i2cStop(void);static BOOL i2cReadBit(void);static void i2cWriteBit(BOOL bit);static BOOL i2cGetAck(void);static void i2cSendAck(void);static void i2cSendNack(void);static UINT8 i2cReadByte(void);static void i2cWriteByte(UINT8 byte);static void i2cWait(void);/* **************************** BEGIN CODE **************************** *//* main initialization function */void I2C_init(INT16 scl_gio, INT16 sda_gio){ /* configure pointers based on GIO usage */ if (scl_gio >= 32) { dir_scl = DIR2; bitset_scl = BITSET2; bitclr_scl = BITCLR2; i2c_scl = 1 << (scl_gio-32); } else if (scl_gio >= 16) { dir_scl = DIR1; bitset_scl = BITSET1; bitclr_scl = BITCLR1; i2c_scl = 1 << (scl_gio-16); } else { dir_scl = DIR0; bitset_scl = BITSET0; bitclr_scl = BITCLR0; i2c_scl = 1 << scl_gio; } if (sda_gio >= 32) { dir_sda = DIR2; bitset_sda = BITSET2; bitclr_sda = BITCLR2; i2c_sda = 1 << (sda_gio-32); } else if (sda_gio >= 16) { dir_sda = DIR1; bitset_sda = BITSET1; bitclr_sda = BITCLR1; i2c_sda = 1 << (sda_gio-16); } else { dir_sda = DIR0; bitset_sda = BITSET0; bitclr_sda = BITCLR0; i2c_sda = 1 << sda_gio; } /* configure the 2 GIO lines as outputs */ *dir_scl &= ~i2c_scl; *dir_sda &= ~i2c_sda; /* put bus in stop state */ i2cStop();}void I2C_writeReg(UINT8 slaveAddr, UINT8 subAddr, UINT8 val, UINT8 *successFlag){ UINT8 flag=1; i2cStart(); i2cWriteByte(slaveAddr & 0xfe); /* LSb = 0 for write */ flag &= i2cGetAck(); i2cWriteByte(subAddr); flag &= i2cGetAck(); i2cWriteByte(val); flag &= i2cGetAck(); i2cStop(); *successFlag = (flag) ? I2C_SUCCESS : I2C_ERROR;}void I2C_writeRegs(UINT8 slaveAddr, UINT8 subAddr, UINT8 *val, UINT32 count, UINT8 *successFlag){ UINT8 i, flag=1; i2cStart(); i2cWriteByte(slaveAddr & 0xfe); /* LSb = 0 for write */ flag &= i2cGetAck(); i2cWriteByte(subAddr); flag &= i2cGetAck(); for (i = 0; i < count; i++) { i2cWriteByte(val[i]); flag &= i2cGetAck(); } i2cStop(); *successFlag = (flag) ? I2C_SUCCESS : I2C_ERROR;}unsigned char I2C_readReg(UINT8 slaveAddr, UINT8 subAddr, UINT8 *successFlag){ UINT8 val, flag=1; i2cStart(); i2cWriteByte(slaveAddr & 0xfe); /* LSb = 0 for write */ flag &= i2cGetAck(); i2cWriteByte(subAddr); flag &= i2cGetAck(); i2cStart(); i2cWriteByte(slaveAddr | 0x01); /* LSb = 1 for read */ flag &= i2cGetAck(); makeSdaInput(); i2cWait(); val = i2cReadByte(); makeSdaOutput(); i2cWait(); i2cSendNack(); i2cStop(); *successFlag = (flag) ? I2C_SUCCESS : I2C_ERROR; return val;}void I2C_readRegs(UINT8 slaveAddr, UINT8 subAddr, UINT8 *val, UINT32 count, UINT8 *successFlag){ UINT32 i, flag=1; i2cStart(); i2cWriteByte(0xfe&slaveAddr); // "write" must be even number flag &= i2cGetAck(); i2cWriteByte(subAddr); flag &= i2cGetAck(); i2cStart(); i2cWriteByte((0xfe&slaveAddr)+1); flag &= i2cGetAck(); for(i=0; i<count; i++) { val[i] = i2cReadByte(); if (i == count-1) i2cSendNack(); else i2cSendAck(); } i2cStop();}/* ********************** Local Helper Functions ********************** *//* write a '0' to SCL */static void makeSclLow(void){ *bitclr_scl = i2c_scl;}/* write a '1' to SCL */static void makeSclHigh(void){ *bitset_scl = i2c_scl;}/* write a '0' to SDA */static void makeSdaLow(void){ *bitclr_sda = i2c_sda;}/* write a '1' to SDA */static void makeSdaHigh(void){ *bitset_sda = i2c_sda;}/* read SDA */static BOOL readSda(void){ UINT16 usTemp; usTemp = *bitset_sda; if ((usTemp & i2c_sda) != 0) return (BOOL)1; else return (BOOL)0;}/* set SDA as input */static void makeSdaInput(void){ *dir_sda |= i2c_sda;}/* set SDA as output */static void makeSdaOutput(void){ makeSdaHigh(); /* was pulled up, keep same state */ *dir_sda &= ~i2c_sda;}static void i2cStart(void){ makeSdaHigh(); makeSclHigh(); i2cWait(); makeSdaLow(); i2cWait(); makeSclLow(); i2cWait();}static void i2cStop(void){ makeSdaLow(); makeSclHigh(); i2cWait(); makeSdaHigh(); i2cWait();}static BOOL i2cReadBit(void){ BOOL bit; makeSclHigh(); i2cWait();// i2cWait();// i2cWait();// i2cWait();// i2cWait();// i2cWait(); bit = readSda(); makeSclLow(); i2cWait(); return bit;}static void i2cWriteBit(BOOL bit){ if (bit) makeSdaHigh(); else makeSdaLow(); makeSclHigh(); i2cWait(); makeSclLow();}static BOOL i2cGetAck(void){ BOOL ackVal; makeSdaInput(); i2cWait(); ackVal = (BOOL)(!i2cReadBit()); /* 0 - Ack, 1 - NACK */ makeSdaOutput(); i2cWait(); return ackVal;}static void i2cSendNack(void){ i2cWriteBit((BOOL)1);}static void i2cSendAck(void){ i2cWriteBit((BOOL)0);}static UINT8 i2cReadByte(void){ UINT8 i, mask=0x80, retVal=0; for (i = 0; i < 8; i++) { if (i2cReadBit()) retVal |= mask; mask >>= 1; } return retVal;}static void i2cWriteByte(UINT8 byte){ UINT8 i, mask=0x80; for (i = 0; i < 8; i++) { if (byte & mask) i2cWriteBit((BOOL)1); else i2cWriteBit((BOOL)0); mask >>= 1; }}static void i2cWait(void){ volatile int i; for (i = 0; i < 100; i++) ;}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -