| Line No. | Rev | Author | Line |
|---|---|---|---|
| 1 | 6 | kaklik | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> |
| 2 | <html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1"> |
||
| 3 | <title>Procyon AVRlib: i2c.h Source File</title> |
||
| 4 | <link href="dox.css" rel="stylesheet" type="text/css"> |
||
| 5 | </head><body> |
||
| 6 | <!-- Generated by Doxygen 1.4.2 --> |
||
| 7 | <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> |
||
| 8 | <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> |
||
| 9 | 00002 <span class="comment">//*****************************************************************************</span> |
||
| 10 | 00003 <span class="comment">//</span> |
||
| 11 | 00004 <span class="comment">// File Name : 'i2c.h'</span> |
||
| 12 | 00005 <span class="comment">// Title : I2C interface using AVR Two-Wire Interface (TWI) hardware</span> |
||
| 13 | 00006 <span class="comment">// Author : Pascal Stang - Copyright (C) 2002-2003</span> |
||
| 14 | 00007 <span class="comment">// Created : 2002.06.25</span> |
||
| 15 | 00008 <span class="comment">// Revised : 2003.03.03</span> |
||
| 16 | 00009 <span class="comment">// Version : 0.9</span> |
||
| 17 | 00010 <span class="comment">// Target MCU : Atmel AVR series</span> |
||
| 18 | 00011 <span class="comment">// Editor Tabs : 4</span> |
||
| 19 | 00012 <span class="comment">//</span><span class="comment"></span> |
||
| 20 | 00013 <span class="comment">/// \ingroup driver_avr</span> |
||
| 21 | 00014 <span class="comment">/// \defgroup i2c I2C Serial Interface Function Library (i2c.c)</span> |
||
| 22 | 00015 <span class="comment">/// \code #include "i2c.h" \endcode</span> |
||
| 23 | 00016 <span class="comment">/// \par Overview</span> |
||
| 24 | 00017 <span class="comment">/// This library provides the high-level functions needed to use the I2C</span> |
||
| 25 | 00018 <span class="comment">/// serial interface supported by the hardware of several AVR processors.</span> |
||
| 26 | 00019 <span class="comment">/// The library is functional but has not been exhaustively tested yet and is</span> |
||
| 27 | 00020 <span class="comment">/// still expanding. Thanks to the standardization of the I2C protocol and</span> |
||
| 28 | 00021 <span class="comment">/// register access, the send and receive commands are everything you need to</span> |
||
| 29 | 00022 <span class="comment">/// talk to thousands of different I2C devices including: EEPROMS, Flash memory,</span> |
||
| 30 | 00023 <span class="comment">/// MP3 players, A/D and D/A converters, electronic potentiometers, etc.</span> |
||
| 31 | 00024 <span class="comment">///</span> |
||
| 32 | 00025 <span class="comment">/// \par About I2C</span> |
||
| 33 | 00026 <span class="comment">/// I2C (pronounced "eye-squared-see") is a two-wire bidirectional</span> |
||
| 34 | 00027 <span class="comment">/// network designed for easy transfer of information between a wide variety</span> |
||
| 35 | 00028 <span class="comment">/// of intelligent devices. Many of the Atmel AVR series processors have</span> |
||
| 36 | 00029 <span class="comment">/// hardware support for transmitting and receiving using an I2C-type bus.</span> |
||
| 37 | 00030 <span class="comment">/// In addition to the AVRs, there are thousands of other parts made by</span> |
||
| 38 | 00031 <span class="comment">/// manufacturers like Philips, Maxim, National, TI, etc that use I2C as</span> |
||
| 39 | 00032 <span class="comment">/// their primary means of communication and control. Common device types</span> |
||
| 40 | 00033 <span class="comment">/// are A/D & D/A converters, temp sensors, intelligent battery monitors,</span> |
||
| 41 | 00034 <span class="comment">/// MP3 decoder chips, EEPROM chips, multiplexing switches, etc.</span> |
||
| 42 | 00035 <span class="comment">///</span> |
||
| 43 | 00036 <span class="comment">/// I2C uses only two wires (SDA and SCL) to communicate bidirectionally</span> |
||
| 44 | 00037 <span class="comment">/// between devices. I2C is a multidrop network, meaning that you can have</span> |
||
| 45 | 00038 <span class="comment">/// several devices on a single bus. Because I2C uses a 7-bit number to</span> |
||
| 46 | 00039 <span class="comment">/// identify which device it wants to talk to, you cannot have more than</span> |
||
| 47 | 00040 <span class="comment">/// 127 devices on a single bus.</span> |
||
| 48 | 00041 <span class="comment">///</span> |
||
| 49 | 00042 <span class="comment">/// I2C ordinarily requires two 4.7K pull-up resistors to power (one each on</span> |
||
| 50 | 00043 <span class="comment">/// SDA and SCL), but for small numbers of devices (maybe 1-4), it is enough</span> |
||
| 51 | 00044 <span class="comment">/// to activate the internal pull-up resistors in the AVR processor. To do</span> |
||
| 52 | 00045 <span class="comment">/// this, set the port pins, which correspond to the I2C pins SDA/SCL, high.</span> |
||
| 53 | 00046 <span class="comment">/// For example, on the mega163, sbi(PORTC, 0); sbi(PORTC, 1);.</span> |
||
| 54 | 00047 <span class="comment">///</span> |
||
| 55 | 00048 <span class="comment">/// For complete information about I2C, see the Philips Semiconductor</span> |
||
| 56 | 00049 <span class="comment">/// website. They created I2C and have the largest family of devices that</span> |
||
| 57 | 00050 <span class="comment">/// work with I2C.</span> |
||
| 58 | 00051 <span class="comment">///</span> |
||
| 59 | 00052 <span class="comment">/// \Note: Many manufacturers market I2C bus devices under a different or generic</span> |
||
| 60 | 00053 <span class="comment">/// bus name like "Two-Wire Interface". This is because Philips still holds</span> |
||
| 61 | 00054 <span class="comment">/// "I2C" as a trademark. For example, SMBus and SMBus devices are hardware</span> |
||
| 62 | 00055 <span class="comment">/// compatible and closely related to I2C. They can be directly connected</span> |
||
| 63 | 00056 <span class="comment">/// to an I2C bus along with other I2C devices are are generally accessed in</span> |
||
| 64 | 00057 <span class="comment">/// the same way as I2C devices. SMBus is often found on modern motherboards</span> |
||
| 65 | 00058 <span class="comment">/// for temp sensing and other low-level control tasks.</span> |
||
| 66 | 00059 <span class="comment"></span><span class="comment">//</span> |
||
| 67 | 00060 <span class="comment">// This code is distributed under the GNU Public License</span> |
||
| 68 | 00061 <span class="comment">// which can be found at http://www.gnu.org/licenses/gpl.txt</span> |
||
| 69 | 00062 <span class="comment">//</span> |
||
| 70 | 00063 <span class="comment">//*****************************************************************************</span> |
||
| 71 | 00064 |
||
| 72 | 00065 <span class="preprocessor">#ifndef I2C_H</span> |
||
| 73 | 00066 <span class="preprocessor"></span><span class="preprocessor">#define I2C_H</span> |
||
| 74 | 00067 <span class="preprocessor"></span> |
||
| 75 | 00068 <span class="preprocessor">#include "<a class="code" href="global_8h.html">global.h</a>"</span> |
||
| 76 | 00069 |
||
| 77 | 00070 <span class="comment">// include project-specific configuration</span> |
||
| 78 | 00071 <span class="preprocessor">#include "<a class="code" href="i2cconf_8h.html">i2cconf.h</a>"</span> |
||
| 79 | 00072 |
||
| 80 | 00073 <span class="comment">// TWSR values (not bits)</span> |
||
| 81 | 00074 <span class="comment">// (taken from avr-libc twi.h - thank you Marek Michalkiewicz)</span> |
||
| 82 | 00075 <span class="comment">// Master</span> |
||
| 83 | 00076 <span class="preprocessor">#define TW_START 0x08</span> |
||
| 84 | 00077 <span class="preprocessor"></span><span class="preprocessor">#define TW_REP_START 0x10</span> |
||
| 85 | 00078 <span class="preprocessor"></span><span class="comment">// Master Transmitter</span> |
||
| 86 | 00079 <span class="preprocessor">#define TW_MT_SLA_ACK 0x18</span> |
||
| 87 | 00080 <span class="preprocessor"></span><span class="preprocessor">#define TW_MT_SLA_NACK 0x20</span> |
||
| 88 | 00081 <span class="preprocessor"></span><span class="preprocessor">#define TW_MT_DATA_ACK 0x28</span> |
||
| 89 | 00082 <span class="preprocessor"></span><span class="preprocessor">#define TW_MT_DATA_NACK 0x30</span> |
||
| 90 | 00083 <span class="preprocessor"></span><span class="preprocessor">#define TW_MT_ARB_LOST 0x38</span> |
||
| 91 | 00084 <span class="preprocessor"></span><span class="comment">// Master Receiver</span> |
||
| 92 | 00085 <span class="preprocessor">#define TW_MR_ARB_LOST 0x38</span> |
||
| 93 | 00086 <span class="preprocessor"></span><span class="preprocessor">#define TW_MR_SLA_ACK 0x40</span> |
||
| 94 | 00087 <span class="preprocessor"></span><span class="preprocessor">#define TW_MR_SLA_NACK 0x48</span> |
||
| 95 | 00088 <span class="preprocessor"></span><span class="preprocessor">#define TW_MR_DATA_ACK 0x50</span> |
||
| 96 | 00089 <span class="preprocessor"></span><span class="preprocessor">#define TW_MR_DATA_NACK 0x58</span> |
||
| 97 | 00090 <span class="preprocessor"></span><span class="comment">// Slave Transmitter</span> |
||
| 98 | 00091 <span class="preprocessor">#define TW_ST_SLA_ACK 0xA8</span> |
||
| 99 | 00092 <span class="preprocessor"></span><span class="preprocessor">#define TW_ST_ARB_LOST_SLA_ACK 0xB0</span> |
||
| 100 | 00093 <span class="preprocessor"></span><span class="preprocessor">#define TW_ST_DATA_ACK 0xB8</span> |
||
| 101 | 00094 <span class="preprocessor"></span><span class="preprocessor">#define TW_ST_DATA_NACK 0xC0</span> |
||
| 102 | 00095 <span class="preprocessor"></span><span class="preprocessor">#define TW_ST_LAST_DATA 0xC8</span> |
||
| 103 | 00096 <span class="preprocessor"></span><span class="comment">// Slave Receiver</span> |
||
| 104 | 00097 <span class="preprocessor">#define TW_SR_SLA_ACK 0x60</span> |
||
| 105 | 00098 <span class="preprocessor"></span><span class="preprocessor">#define TW_SR_ARB_LOST_SLA_ACK 0x68</span> |
||
| 106 | 00099 <span class="preprocessor"></span><span class="preprocessor">#define TW_SR_GCALL_ACK 0x70</span> |
||
| 107 | 00100 <span class="preprocessor"></span><span class="preprocessor">#define TW_SR_ARB_LOST_GCALL_ACK 0x78</span> |
||
| 108 | 00101 <span class="preprocessor"></span><span class="preprocessor">#define TW_SR_DATA_ACK 0x80</span> |
||
| 109 | 00102 <span class="preprocessor"></span><span class="preprocessor">#define TW_SR_DATA_NACK 0x88</span> |
||
| 110 | 00103 <span class="preprocessor"></span><span class="preprocessor">#define TW_SR_GCALL_DATA_ACK 0x90</span> |
||
| 111 | 00104 <span class="preprocessor"></span><span class="preprocessor">#define TW_SR_GCALL_DATA_NACK 0x98</span> |
||
| 112 | 00105 <span class="preprocessor"></span><span class="preprocessor">#define TW_SR_STOP 0xA0</span> |
||
| 113 | 00106 <span class="preprocessor"></span><span class="comment">// Misc</span> |
||
| 114 | 00107 <span class="preprocessor">#define TW_NO_INFO 0xF8</span> |
||
| 115 | 00108 <span class="preprocessor"></span><span class="preprocessor">#define TW_BUS_ERROR 0x00</span> |
||
| 116 | 00109 <span class="preprocessor"></span> |
||
| 117 | 00110 <span class="comment">// defines and constants</span> |
||
| 118 | 00111 <span class="preprocessor">#define TWCR_CMD_MASK 0x0F</span> |
||
| 119 | 00112 <span class="preprocessor"></span><span class="preprocessor">#define TWSR_STATUS_MASK 0xF8</span> |
||
| 120 | 00113 <span class="preprocessor"></span> |
||
| 121 | 00114 <span class="comment">// return values</span> |
||
| 122 | 00115 <span class="preprocessor">#define I2C_OK 0x00</span> |
||
| 123 | 00116 <span class="preprocessor"></span><span class="preprocessor">#define I2C_ERROR_NODEV 0x01</span> |
||
| 124 | 00117 <span class="preprocessor"></span> |
||
| 125 | 00118 <span class="comment">// types</span> |
||
| 126 | 00119 <span class="keyword">typedef</span> <span class="keyword">enum</span> |
||
| 127 | 00120 { |
||
| 128 | 00121 I2C_IDLE = 0, I2C_BUSY = 1, |
||
| 129 | 00122 I2C_MASTER_TX = 2, I2C_MASTER_RX = 3, |
||
| 130 | 00123 I2C_SLAVE_TX = 4, I2C_SLAVE_RX = 5 |
||
| 131 | 00124 } eI2cStateType; |
||
| 132 | 00125 |
||
| 133 | 00126 <span class="comment">// functions</span> |
||
| 134 | 00127 <span class="comment"></span> |
||
| 135 | 00128 <span class="comment">//! Initialize I2C (TWI) interface</span> |
||
| 136 | 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>); |
||
| 137 | 00130 <span class="comment"></span> |
||
| 138 | 00131 <span class="comment">//! Set the I2C transaction bitrate (in KHz)</span> |
||
| 139 | 00132 <span class="comment"></span><span class="keywordtype">void</span> <a class="code" href="i2c_8c.html#a11">i2cSetBitrate</a>(u16 bitrateKHz); |
||
| 140 | 00133 |
||
| 141 | 00134 <span class="comment">// I2C setup and configurations commands</span><span class="comment"></span> |
||
| 142 | 00135 <span class="comment">//! Set the local (AVR processor's) I2C device address</span> |
||
| 143 | 00136 <span class="comment"></span><span class="keywordtype">void</span> <a class="code" href="i2c_8c.html#a12">i2cSetLocalDeviceAddr</a>(u08 deviceAddr, u08 genCallEn); |
||
| 144 | 00137 <span class="comment"></span> |
||
| 145 | 00138 <span class="comment">//! Set the user function which handles receiving (incoming) data as a slave</span> |
||
| 146 | 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> |
||
| 147 | 00140 <span class="comment">//! Set the user function which handles transmitting (outgoing) data as a slave</span> |
||
| 148 | 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)); |
||
| 149 | 00142 |
||
| 150 | 00143 <span class="comment">// Low-level I2C transaction commands </span><span class="comment"></span> |
||
| 151 | 00144 <span class="comment">//! Send an I2C start condition in Master mode</span> |
||
| 152 | 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> |
||
| 153 | 00146 <span class="comment">//! Send an I2C stop condition in Master mode</span> |
||
| 154 | 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> |
||
| 155 | 00148 <span class="comment">//! Wait for current I2C operation to complete</span> |
||
| 156 | 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> |
||
| 157 | 00150 <span class="comment">//! Send an (address|R/W) combination or a data byte over I2C</span> |
||
| 158 | 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> |
||
| 159 | 00152 <span class="comment">//! Receive a data byte over I2C </span> |
||
| 160 | 00153 <span class="comment"></span><span class="comment">// ackFlag = TRUE if recevied data should be ACK'ed</span> |
||
| 161 | 00154 <span class="comment">// ackFlag = FALSE if recevied data should be NACK'ed</span> |
||
| 162 | 00155 <span class="keywordtype">void</span> <a class="code" href="i2c_8c.html#a19">i2cReceiveByte</a>(u08 ackFlag);<span class="comment"></span> |
||
| 163 | 00156 <span class="comment">//! Pick up the data that was received with i2cReceiveByte()</span> |
||
| 164 | 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> |
||
| 165 | 00158 <span class="comment">//! Get current I2c bus status from TWSR</span> |
||
| 166 | 00159 <span class="comment"></span>u08 <a class="code" href="i2c_8c.html#a21">i2cGetStatus</a>(<span class="keywordtype">void</span>); |
||
| 167 | 00160 |
||
| 168 | 00161 <span class="comment">// high-level I2C transaction commands</span> |
||
| 169 | 00162 <span class="comment"></span> |
||
| 170 | 00163 <span class="comment">//! send I2C data to a device on the bus</span> |
||
| 171 | 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> |
||
| 172 | 00165 <span class="comment">//! receive I2C data from a device on the bus</span> |
||
| 173 | 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); |
||
| 174 | 00167 <span class="comment"></span> |
||
| 175 | 00168 <span class="comment">//! send I2C data to a device on the bus (non-interrupt based)</span> |
||
| 176 | 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> |
||
| 177 | 00170 <span class="comment">//! receive I2C data from a device on the bus (non-interrupt based)</span> |
||
| 178 | 00171 <span class="comment"></span>u08 <a class="code" href="i2c_8c.html#a25">i2cMasterReceiveNI</a>(u08 deviceAddr, u08 length, u08 *data); |
||
| 179 | 00172 <span class="comment"></span> |
||
| 180 | 00173 <span class="comment">//! Get the current high-level state of the I2C interface</span> |
||
| 181 | 00174 <span class="comment"></span>eI2cStateType <a class="code" href="i2c_8c.html#a27">i2cGetState</a>(<span class="keywordtype">void</span>); |
||
| 182 | 00175 |
||
| 183 | 00176 <span class="preprocessor">#endif</span> |
||
| 184 | </pre></div><hr size="1"><address style="align: right;"><small>Generated on Sun Oct 29 03:41:06 2006 for Procyon AVRlib by |
||
| 185 | <a href="http://www.doxygen.org/index.html"> |
||
| 186 | <img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.4.2 </small></address> |
||
| 187 | </body> |
||
| 188 | </html> |
Powered by WebSVN v2.8.3