<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1">
<title>Procyon AVRlib: i2c.h Source File</title>
<link href="dox.css" rel="stylesheet" type="text/css">
</head><body>
<!-- Generated by Doxygen 1.4.2 -->
<div class="qindex"><a class="qindex" href="main.html">Main Page</a> | <a class="qindex" href="modules.html">Modules</a> | <a class="qindex" href="annotated.html">Data Structures</a> | <a class="qindex" href="dirs.html">Directories</a> | <a class="qindex" href="files.html">File List</a> | <a class="qindex" href="functions.html">Data Fields</a> | <a class="qindex" href="globals.html">Globals</a> | <a class="qindex" href="pages.html">Related Pages</a></div>
<h1>i2c.h</h1><a href="i2c_8h.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment">00001 <span class="comment">/*! \file i2c.h \brief I2C interface using AVR Two-Wire Interface (TWI) hardware. */</span>
00002 <span class="comment">//*****************************************************************************</span>
00003 <span class="comment">//</span>
00004 <span class="comment">// File Name : 'i2c.h'</span>
00005 <span class="comment">// Title : I2C interface using AVR Two-Wire Interface (TWI) hardware</span>
00006 <span class="comment">// Author : Pascal Stang - Copyright (C) 2002-2003</span>
00007 <span class="comment">// Created : 2002.06.25</span>
00008 <span class="comment">// Revised : 2003.03.03</span>
00009 <span class="comment">// Version : 0.9</span>
00010 <span class="comment">// Target MCU : Atmel AVR series</span>
00011 <span class="comment">// Editor Tabs : 4</span>
00012 <span class="comment">//</span><span class="comment"></span>
00013 <span class="comment">/// \ingroup driver_avr</span>
00014 <span class="comment">/// \defgroup i2c I2C Serial Interface Function Library (i2c.c)</span>
00015 <span class="comment">/// \code #include "i2c.h" \endcode</span>
00016 <span class="comment">/// \par Overview</span>
00017 <span class="comment">/// This library provides the high-level functions needed to use the I2C</span>
00018 <span class="comment">/// serial interface supported by the hardware of several AVR processors.</span>
00019 <span class="comment">/// The library is functional but has not been exhaustively tested yet and is</span>
00020 <span class="comment">/// still expanding. Thanks to the standardization of the I2C protocol and</span>
00021 <span class="comment">/// register access, the send and receive commands are everything you need to</span>
00022 <span class="comment">/// talk to thousands of different I2C devices including: EEPROMS, Flash memory,</span>
00023 <span class="comment">/// MP3 players, A/D and D/A converters, electronic potentiometers, etc.</span>
00024 <span class="comment">///</span>
00025 <span class="comment">/// \par About I2C</span>
00026 <span class="comment">/// I2C (pronounced "eye-squared-see") is a two-wire bidirectional</span>
00027 <span class="comment">/// network designed for easy transfer of information between a wide variety</span>
00028 <span class="comment">/// of intelligent devices. Many of the Atmel AVR series processors have</span>
00029 <span class="comment">/// hardware support for transmitting and receiving using an I2C-type bus.</span>
00030 <span class="comment">/// In addition to the AVRs, there are thousands of other parts made by</span>
00031 <span class="comment">/// manufacturers like Philips, Maxim, National, TI, etc that use I2C as</span>
00032 <span class="comment">/// their primary means of communication and control. Common device types</span>
00033 <span class="comment">/// are A/D & D/A converters, temp sensors, intelligent battery monitors,</span>
00034 <span class="comment">/// MP3 decoder chips, EEPROM chips, multiplexing switches, etc.</span>
00035 <span class="comment">///</span>
00036 <span class="comment">/// I2C uses only two wires (SDA and SCL) to communicate bidirectionally</span>
00037 <span class="comment">/// between devices. I2C is a multidrop network, meaning that you can have</span>
00038 <span class="comment">/// several devices on a single bus. Because I2C uses a 7-bit number to</span>
00039 <span class="comment">/// identify which device it wants to talk to, you cannot have more than</span>
00040 <span class="comment">/// 127 devices on a single bus.</span>
00041 <span class="comment">///</span>
00042 <span class="comment">/// I2C ordinarily requires two 4.7K pull-up resistors to power (one each on</span>
00043 <span class="comment">/// SDA and SCL), but for small numbers of devices (maybe 1-4), it is enough</span>
00044 <span class="comment">/// to activate the internal pull-up resistors in the AVR processor. To do</span>
00045 <span class="comment">/// this, set the port pins, which correspond to the I2C pins SDA/SCL, high.</span>
00046 <span class="comment">/// For example, on the mega163, sbi(PORTC, 0); sbi(PORTC, 1);.</span>
00047 <span class="comment">///</span>
00048 <span class="comment">/// For complete information about I2C, see the Philips Semiconductor</span>
00049 <span class="comment">/// website. They created I2C and have the largest family of devices that</span>
00050 <span class="comment">/// work with I2C.</span>
00051 <span class="comment">///</span>
00052 <span class="comment">/// \Note: Many manufacturers market I2C bus devices under a different or generic</span>
00053 <span class="comment">/// bus name like "Two-Wire Interface". This is because Philips still holds</span>
00054 <span class="comment">/// "I2C" as a trademark. For example, SMBus and SMBus devices are hardware</span>
00055 <span class="comment">/// compatible and closely related to I2C. They can be directly connected</span>
00056 <span class="comment">/// to an I2C bus along with other I2C devices are are generally accessed in</span>
00057 <span class="comment">/// the same way as I2C devices. SMBus is often found on modern motherboards</span>
00058 <span class="comment">/// for temp sensing and other low-level control tasks.</span>
00059 <span class="comment"></span><span class="comment">//</span>
00060 <span class="comment">// This code is distributed under the GNU Public License</span>
00061 <span class="comment">// which can be found at http://www.gnu.org/licenses/gpl.txt</span>
00062 <span class="comment">//</span>
00063 <span class="comment">//*****************************************************************************</span>
00064
00065 <span class="preprocessor">#ifndef I2C_H</span>
00066 <span class="preprocessor"></span><span class="preprocessor">#define I2C_H</span>
00067 <span class="preprocessor"></span>
00068 <span class="preprocessor">#include "<a class="code" href="global_8h.html">global.h</a>"</span>
00069
00070 <span class="comment">// include project-specific configuration</span>
00071 <span class="preprocessor">#include "<a class="code" href="i2cconf_8h.html">i2cconf.h</a>"</span>
00072
00073 <span class="comment">// TWSR values (not bits)</span>
00074 <span class="comment">// (taken from avr-libc twi.h - thank you Marek Michalkiewicz)</span>
00075 <span class="comment">// Master</span>
00076 <span class="preprocessor">#define TW_START 0x08</span>
00077 <span class="preprocessor"></span><span class="preprocessor">#define TW_REP_START 0x10</span>
00078 <span class="preprocessor"></span><span class="comment">// Master Transmitter</span>
00079 <span class="preprocessor">#define TW_MT_SLA_ACK 0x18</span>
00080 <span class="preprocessor"></span><span class="preprocessor">#define TW_MT_SLA_NACK 0x20</span>
00081 <span class="preprocessor"></span><span class="preprocessor">#define TW_MT_DATA_ACK 0x28</span>
00082 <span class="preprocessor"></span><span class="preprocessor">#define TW_MT_DATA_NACK 0x30</span>
00083 <span class="preprocessor"></span><span class="preprocessor">#define TW_MT_ARB_LOST 0x38</span>
00084 <span class="preprocessor"></span><span class="comment">// Master Receiver</span>
00085 <span class="preprocessor">#define TW_MR_ARB_LOST 0x38</span>
00086 <span class="preprocessor"></span><span class="preprocessor">#define TW_MR_SLA_ACK 0x40</span>
00087 <span class="preprocessor"></span><span class="preprocessor">#define TW_MR_SLA_NACK 0x48</span>
00088 <span class="preprocessor"></span><span class="preprocessor">#define TW_MR_DATA_ACK 0x50</span>
00089 <span class="preprocessor"></span><span class="preprocessor">#define TW_MR_DATA_NACK 0x58</span>
00090 <span class="preprocessor"></span><span class="comment">// Slave Transmitter</span>
00091 <span class="preprocessor">#define TW_ST_SLA_ACK 0xA8</span>
00092 <span class="preprocessor"></span><span class="preprocessor">#define TW_ST_ARB_LOST_SLA_ACK 0xB0</span>
00093 <span class="preprocessor"></span><span class="preprocessor">#define TW_ST_DATA_ACK 0xB8</span>
00094 <span class="preprocessor"></span><span class="preprocessor">#define TW_ST_DATA_NACK 0xC0</span>
00095 <span class="preprocessor"></span><span class="preprocessor">#define TW_ST_LAST_DATA 0xC8</span>
00096 <span class="preprocessor"></span><span class="comment">// Slave Receiver</span>
00097 <span class="preprocessor">#define TW_SR_SLA_ACK 0x60</span>
00098 <span class="preprocessor"></span><span class="preprocessor">#define TW_SR_ARB_LOST_SLA_ACK 0x68</span>
00099 <span class="preprocessor"></span><span class="preprocessor">#define TW_SR_GCALL_ACK 0x70</span>
00100 <span class="preprocessor"></span><span class="preprocessor">#define TW_SR_ARB_LOST_GCALL_ACK 0x78</span>
00101 <span class="preprocessor"></span><span class="preprocessor">#define TW_SR_DATA_ACK 0x80</span>
00102 <span class="preprocessor"></span><span class="preprocessor">#define TW_SR_DATA_NACK 0x88</span>
00103 <span class="preprocessor"></span><span class="preprocessor">#define TW_SR_GCALL_DATA_ACK 0x90</span>
00104 <span class="preprocessor"></span><span class="preprocessor">#define TW_SR_GCALL_DATA_NACK 0x98</span>
00105 <span class="preprocessor"></span><span class="preprocessor">#define TW_SR_STOP 0xA0</span>
00106 <span class="preprocessor"></span><span class="comment">// Misc</span>
00107 <span class="preprocessor">#define TW_NO_INFO 0xF8</span>
00108 <span class="preprocessor"></span><span class="preprocessor">#define TW_BUS_ERROR 0x00</span>
00109 <span class="preprocessor"></span>
00110 <span class="comment">// defines and constants</span>
00111 <span class="preprocessor">#define TWCR_CMD_MASK 0x0F</span>
00112 <span class="preprocessor"></span><span class="preprocessor">#define TWSR_STATUS_MASK 0xF8</span>
00113 <span class="preprocessor"></span>
00114 <span class="comment">// return values</span>
00115 <span class="preprocessor">#define I2C_OK 0x00</span>
00116 <span class="preprocessor"></span><span class="preprocessor">#define I2C_ERROR_NODEV 0x01</span>
00117 <span class="preprocessor"></span>
00118 <span class="comment">// types</span>
00119 <span class="keyword">typedef</span> <span class="keyword">enum</span>
00120 {
00121 I2C_IDLE = 0, I2C_BUSY = 1,
00122 I2C_MASTER_TX = 2, I2C_MASTER_RX = 3,
00123 I2C_SLAVE_TX = 4, I2C_SLAVE_RX = 5
00124 } eI2cStateType;
00125
00126 <span class="comment">// functions</span>
00127 <span class="comment"></span>
00128 <span class="comment">//! Initialize I2C (TWI) interface</span>
00129 <span class="comment"></span><span class="keywordtype">void</span> <a class="code" href="i2c_8c.html#a10">i2cInit</a>(<span class="keywordtype">void</span>);
00130 <span class="comment"></span>
00131 <span class="comment">//! Set the I2C transaction bitrate (in KHz)</span>
00132 <span class="comment"></span><span class="keywordtype">void</span> <a class="code" href="i2c_8c.html#a11">i2cSetBitrate</a>(u16 bitrateKHz);
00133
00134 <span class="comment">// I2C setup and configurations commands</span><span class="comment"></span>
00135 <span class="comment">//! Set the local (AVR processor's) I2C device address</span>
00136 <span class="comment"></span><span class="keywordtype">void</span> <a class="code" href="i2c_8c.html#a12">i2cSetLocalDeviceAddr</a>(u08 deviceAddr, u08 genCallEn);
00137 <span class="comment"></span>
00138 <span class="comment">//! Set the user function which handles receiving (incoming) data as a slave</span>
00139 <span class="comment"></span><span class="keywordtype">void</span> <a class="code" href="i2c_8c.html#a13">i2cSetSlaveReceiveHandler</a>(<span class="keywordtype">void</span> (*i2cSlaveRx_func)(u08 receiveDataLength, u08* recieveData));<span class="comment"></span>
00140 <span class="comment">//! Set the user function which handles transmitting (outgoing) data as a slave</span>
00141 <span class="comment"></span><span class="keywordtype">void</span> <a class="code" href="i2c_8c.html#a14">i2cSetSlaveTransmitHandler</a>(u08 (*i2cSlaveTx_func)(u08 transmitDataLengthMax, u08* transmitData));
00142
00143 <span class="comment">// Low-level I2C transaction commands </span><span class="comment"></span>
00144 <span class="comment">//! Send an I2C start condition in Master mode</span>
00145 <span class="comment"></span><span class="keywordtype">void</span> <a class="code" href="i2c_8c.html#a15">i2cSendStart</a>(<span class="keywordtype">void</span>);<span class="comment"></span>
00146 <span class="comment">//! Send an I2C stop condition in Master mode</span>
00147 <span class="comment"></span><span class="keywordtype">void</span> <a class="code" href="i2c_8c.html#a16">i2cSendStop</a>(<span class="keywordtype">void</span>);<span class="comment"></span>
00148 <span class="comment">//! Wait for current I2C operation to complete</span>
00149 <span class="comment"></span><span class="keywordtype">void</span> <a class="code" href="i2c_8c.html#a17">i2cWaitForComplete</a>(<span class="keywordtype">void</span>);<span class="comment"></span>
00150 <span class="comment">//! Send an (address|R/W) combination or a data byte over I2C</span>
00151 <span class="comment"></span><span class="keywordtype">void</span> <a class="code" href="i2c_8c.html#a18">i2cSendByte</a>(u08 data);<span class="comment"></span>
00152 <span class="comment">//! Receive a data byte over I2C </span>
00153 <span class="comment"></span><span class="comment">// ackFlag = TRUE if recevied data should be ACK'ed</span>
00154 <span class="comment">// ackFlag = FALSE if recevied data should be NACK'ed</span>
00155 <span class="keywordtype">void</span> <a class="code" href="i2c_8c.html#a19">i2cReceiveByte</a>(u08 ackFlag);<span class="comment"></span>
00156 <span class="comment">//! Pick up the data that was received with i2cReceiveByte()</span>
00157 <span class="comment"></span>u08 <a class="code" href="i2c_8c.html#a20">i2cGetReceivedByte</a>(<span class="keywordtype">void</span>);<span class="comment"></span>
00158 <span class="comment">//! Get current I2c bus status from TWSR</span>
00159 <span class="comment"></span>u08 <a class="code" href="i2c_8c.html#a21">i2cGetStatus</a>(<span class="keywordtype">void</span>);
00160
00161 <span class="comment">// high-level I2C transaction commands</span>
00162 <span class="comment"></span>
00163 <span class="comment">//! send I2C data to a device on the bus</span>
00164 <span class="comment"></span><span class="keywordtype">void</span> <a class="code" href="i2c_8c.html#a22">i2cMasterSend</a>(u08 deviceAddr, u08 length, u08 *data);<span class="comment"></span>
00165 <span class="comment">//! receive I2C data from a device on the bus</span>
00166 <span class="comment"></span><span class="keywordtype">void</span> <a class="code" href="i2c_8c.html#a23">i2cMasterReceive</a>(u08 deviceAddr, u08 length, u08* data);
00167 <span class="comment"></span>
00168 <span class="comment">//! send I2C data to a device on the bus (non-interrupt based)</span>
00169 <span class="comment"></span>u08 <a class="code" href="i2c_8c.html#a24">i2cMasterSendNI</a>(u08 deviceAddr, u08 length, u08* data);<span class="comment"></span>
00170 <span class="comment">//! receive I2C data from a device on the bus (non-interrupt based)</span>
00171 <span class="comment"></span>u08 <a class="code" href="i2c_8c.html#a25">i2cMasterReceiveNI</a>(u08 deviceAddr, u08 length, u08 *data);
00172 <span class="comment"></span>
00173 <span class="comment">//! Get the current high-level state of the I2C interface</span>
00174 <span class="comment"></span>eI2cStateType <a class="code" href="i2c_8c.html#a27">i2cGetState</a>(<span class="keywordtype">void</span>);
00175
00176 <span class="preprocessor">#endif</span>
</pre></div><hr size="1"><address style="align: right;"><small>Generated on Sun Oct 29 03:41:06 2006 for Procyon AVRlib by
<a href="http://www.doxygen.org/index.html">
<img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.4.2 </small></address>
</body>
</html>
|