1 // Copyright (C) 2009 Ubixum, Inc.
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, see <http://www.gnu.org/licenses/>.
17 * Utilities for reading and writing to i2c devices and as eeproms.
26 * i2c_write and i2c_read set this to FALSE at the beginning of a
27 * transaction. If for some reason, the read/write is taking too
28 * long or not returning, firmware can set this to TRUE to cause the
29 * routine to abort. (Usually done via an interrupt).
31 extern volatile __xdata BOOL cancel_i2c_trans;
34 * \brief write data to i2c bus.
36 * Writes data from addr buffer 1st, then data buffer.
37 * Either buffer can be NULL (as long as you set lenN to 0).
39 * Two buffers allows writing data all in one i2c write command without
40 * having to write a hardware address and a data byte with each
43 * \param addr i2c address
44 * \param len1 length of addr data
45 * \param addr_buf addr data
46 * \param len2 length of data
47 * \param data_buf data bytes
49 BOOL i2c_write ( BYTE addr, WORD len1, BYTE* addr_buf, WORD len2, BYTE* data_buf );
52 * \brief read data on the i2c bus.
54 * \param addr i2c address
55 * \param len number of bytes to read
56 * \param buf buffer to store data
58 BOOL i2c_read ( BYTE addr, WORD len, BYTE* buf);
61 * \brief read data from an attached eeprom.
63 * Writes the address of the data to read then reads len bytes into buf.
64 * This function checks the I2CS register to determine if a one or two
65 * byte address eepom was detected on the i2c bus. Reading from proms
66 * at non-standard addresses my require using the i2c_read/write commands
69 * \param prom_addr eeprom i2c address
70 * \param addr address of bytes to start reading
71 * \param len number of bytes to read
72 * \param buf data buffer
74 BOOL eeprom_read( BYTE prom_addr, WORD addr, WORD len, BYTE* buf);
77 * \brief write data to the eeprom
79 * This function checks the I2CS register to determin if a one or two
80 * two byte eeprom is detected. If the prom is not detected at boot time
81 * or is connected to alternate addresses, the i2c_read/write commands should
82 * be used explicitly insread of using this function.
84 * For each byte in buf, the address is written and then the data byte. Many
85 * proms support writing multiple bytes at the same time. For these, it is
86 * also better to use i2c_read/write explicitly. This function is rather slow
89 * \param prom_addr eeprom i2c address
90 * \param addr address of bytes to start writing
91 * \param len number of bytes to write
92 * \param buf data buffer
94 BOOL eeprom_write( BYTE prom_addr, WORD addr, WORD len, BYTE* buf);