?lang_form? ?lang_select? ?lang_submit? ?lang_endform?
{HEADER END}
{BLAME START}

library

?curdirlinks? -

Blame information for rev 6

Line No. Rev Author Line
1 6 kaklik /*! \file i2c.h \brief I2C interface using AVR Two-Wire Interface (TWI) hardware. */
2 //*****************************************************************************
3 //
4 // File Name : 'i2c.h'
5 // Title : I2C interface using AVR Two-Wire Interface (TWI) hardware
6 // Author : Pascal Stang - Copyright (C) 2002-2003
7 // Created : 2002.06.25
8 // Revised : 2003.03.03
9 // Version : 0.9
10 // Target MCU : Atmel AVR series
11 // Editor Tabs : 4
12 //
13 /// \ingroup driver_avr
14 /// \defgroup i2c I2C Serial Interface Function Library (i2c.c)
15 /// \code #include "i2c.h" \endcode
16 /// \par Overview
17 /// This library provides the high-level functions needed to use the I2C
18 /// serial interface supported by the hardware of several AVR processors.
19 /// The library is functional but has not been exhaustively tested yet and is
20 /// still expanding.  Thanks to the standardization of the I2C protocol and
21 /// register access, the send and receive commands are everything you need to
22 /// talk to thousands of different I2C devices including: EEPROMS, Flash memory,
23 /// MP3 players, A/D and D/A converters, electronic potentiometers, etc.
24 ///
25 /// \par About I2C
26 /// I2C (pronounced "eye-squared-see") is a two-wire bidirectional
27 /// network designed for easy transfer of information between a wide variety
28 /// of intelligent devices. Many of the Atmel AVR series processors have
29 /// hardware support for transmitting and receiving using an I2C-type bus.
30 /// In addition to the AVRs, there are thousands of other parts made by
31 /// manufacturers like Philips, Maxim, National, TI, etc that use I2C as
32 /// their primary means of communication and control. Common device types
33 /// are A/D & D/A converters, temp sensors, intelligent battery monitors,
34 /// MP3 decoder chips, EEPROM chips, multiplexing switches, etc.
35 ///
36 /// I2C uses only two wires (SDA and SCL) to communicate bidirectionally
37 /// between devices. I2C is a multidrop network, meaning that you can have
38 /// several devices on a single bus. Because I2C uses a 7-bit number to
39 /// identify which device it wants to talk to, you cannot have more than
40 /// 127 devices on a single bus.
41 ///
42 /// I2C ordinarily requires two 4.7K pull-up resistors to power (one each on
43 /// SDA and SCL), but for small numbers of devices (maybe 1-4), it is enough
44 /// to activate the internal pull-up resistors in the AVR processor. To do
45 /// this, set the port pins, which correspond to the I2C pins SDA/SCL, high.
46 /// For example, on the mega163, sbi(PORTC, 0); sbi(PORTC, 1);.
47 ///
48 /// For complete information about I2C, see the Philips Semiconductor
49 /// website. They created I2C and have the largest family of devices that
50 /// work with I2C.
51 ///
52 /// \Note: Many manufacturers market I2C bus devices under a different or generic
53 /// bus name like "Two-Wire Interface". This is because Philips still holds
54 /// "I2C" as a trademark. For example, SMBus and SMBus devices are hardware
55 /// compatible and closely related to I2C. They can be directly connected
56 /// to an I2C bus along with other I2C devices are are generally accessed in
57 /// the same way as I2C devices. SMBus is often found on modern motherboards
58 /// for temp sensing and other low-level control tasks.
59 //
60 // This code is distributed under the GNU Public License
61 // which can be found at http://www.gnu.org/licenses/gpl.txt
62 //
63 //*****************************************************************************
64  
65 #ifndef I2C_H
66 #define I2C_H
67  
68 #include "global.h"
69  
70 // include project-specific configuration
71 #include "i2cconf.h"
72  
73 // TWSR values (not bits)
74 // (taken from avr-libc twi.h - thank you Marek Michalkiewicz)
75 // Master
76 #define TW_START 0x08
77 #define TW_REP_START 0x10
78 // Master Transmitter
79 #define TW_MT_SLA_ACK 0x18
80 #define TW_MT_SLA_NACK 0x20
81 #define TW_MT_DATA_ACK 0x28
82 #define TW_MT_DATA_NACK 0x30
83 #define TW_MT_ARB_LOST 0x38
84 // Master Receiver
85 #define TW_MR_ARB_LOST 0x38
86 #define TW_MR_SLA_ACK 0x40
87 #define TW_MR_SLA_NACK 0x48
88 #define TW_MR_DATA_ACK 0x50
89 #define TW_MR_DATA_NACK 0x58
90 // Slave Transmitter
91 #define TW_ST_SLA_ACK 0xA8
92 #define TW_ST_ARB_LOST_SLA_ACK 0xB0
93 #define TW_ST_DATA_ACK 0xB8
94 #define TW_ST_DATA_NACK 0xC0
95 #define TW_ST_LAST_DATA 0xC8
96 // Slave Receiver
97 #define TW_SR_SLA_ACK 0x60
98 #define TW_SR_ARB_LOST_SLA_ACK 0x68
99 #define TW_SR_GCALL_ACK 0x70
100 #define TW_SR_ARB_LOST_GCALL_ACK 0x78
101 #define TW_SR_DATA_ACK 0x80
102 #define TW_SR_DATA_NACK 0x88
103 #define TW_SR_GCALL_DATA_ACK 0x90
104 #define TW_SR_GCALL_DATA_NACK 0x98
105 #define TW_SR_STOP 0xA0
106 // Misc
107 #define TW_NO_INFO 0xF8
108 #define TW_BUS_ERROR 0x00
109  
110 // defines and constants
111 #define TWCR_CMD_MASK 0x0F
112 #define TWSR_STATUS_MASK 0xF8
113  
114 // return values
115 #define I2C_OK 0x00
116 #define I2C_ERROR_NODEV 0x01
117  
118 // types
119 typedef enum
120 {
121 I2C_IDLE = 0, I2C_BUSY = 1,
122 I2C_MASTER_TX = 2, I2C_MASTER_RX = 3,
123 I2C_SLAVE_TX = 4, I2C_SLAVE_RX = 5
124 } eI2cStateType;
125  
126 // functions
127  
128 //! Initialize I2C (TWI) interface
129 void i2cInit(void);
130  
131 //! Set the I2C transaction bitrate (in KHz)
132 void i2cSetBitrate(u16 bitrateKHz);
133  
134 // I2C setup and configurations commands
135 //! Set the local (AVR processor's) I2C device address
136 void i2cSetLocalDeviceAddr(u08 deviceAddr, u08 genCallEn);
137  
138 //! Set the user function which handles receiving (incoming) data as a slave
139 void i2cSetSlaveReceiveHandler(void (*i2cSlaveRx_func)(u08 receiveDataLength, u08* recieveData));
140 //! Set the user function which handles transmitting (outgoing) data as a slave
141 void i2cSetSlaveTransmitHandler(u08 (*i2cSlaveTx_func)(u08 transmitDataLengthMax, u08* transmitData));
142  
143 // Low-level I2C transaction commands
144 //! Send an I2C start condition in Master mode
145 void i2cSendStart(void);
146 //! Send an I2C stop condition in Master mode
147 void i2cSendStop(void);
148 //! Wait for current I2C operation to complete
149 void i2cWaitForComplete(void);
150 //! Send an (address|R/W) combination or a data byte over I2C
151 void i2cSendByte(u08 data);
152 //! Receive a data byte over I2C
153 // ackFlag = TRUE if recevied data should be ACK'ed
154 // ackFlag = FALSE if recevied data should be NACK'ed
155 void i2cReceiveByte(u08 ackFlag);
156 //! Pick up the data that was received with i2cReceiveByte()
157 u08 i2cGetReceivedByte(void);
158 //! Get current I2c bus status from TWSR
159 u08 i2cGetStatus(void);
160  
161 // high-level I2C transaction commands
162  
163 //! send I2C data to a device on the bus
164 void i2cMasterSend(u08 deviceAddr, u08 length, u08 *data);
165 //! receive I2C data from a device on the bus
166 void i2cMasterReceive(u08 deviceAddr, u08 length, u08* data);
167  
168 //! send I2C data to a device on the bus (non-interrupt based)
169 u08 i2cMasterSendNI(u08 deviceAddr, u08 length, u08* data);
170 //! receive I2C data from a device on the bus (non-interrupt based)
171 u08 i2cMasterReceiveNI(u08 deviceAddr, u08 length, u08 *data);
172  
173 //! Get the current high-level state of the I2C interface
174 eI2cStateType i2cGetState(void);
175  
176 #endif
{BLAME END}
{FOOTER START}

Powered by WebSVN v2.8.3