I2C not yet integrated
Tested working with single and differential voltages.
Connect SCL (pin 11) to D15 Connect SDA (pin 10) to D14 Connect pin 16 to +5v Connect pin 9 to gnd
LT_I2C.h@0:1473318f27b6, 2016-11-16 (annotated)
- Committer:
- lrdawg99
- Date:
- Wed Nov 16 15:54:08 2016 +0000
- Revision:
- 0:1473318f27b6
- Child:
- 2:c9e727dcd00e
Ready to integrate I2C into LT_I2C
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
lrdawg99 | 0:1473318f27b6 | 1 | |
lrdawg99 | 0:1473318f27b6 | 2 | /*! |
lrdawg99 | 0:1473318f27b6 | 3 | LT_I2C: Routines to communicate with ATmega328P's hardware I2C port. |
lrdawg99 | 0:1473318f27b6 | 4 | |
lrdawg99 | 0:1473318f27b6 | 5 | REVISION HISTORY |
lrdawg99 | 0:1473318f27b6 | 6 | $Revision: 3659 $ |
lrdawg99 | 0:1473318f27b6 | 7 | $Date: 2015-07-01 10:19:20 -0700 (Wed, 01 Jul 2015) $ |
lrdawg99 | 0:1473318f27b6 | 8 | |
lrdawg99 | 0:1473318f27b6 | 9 | Copyright (c) 2013, Linear Technology Corp.(LTC) |
lrdawg99 | 0:1473318f27b6 | 10 | All rights reserved. |
lrdawg99 | 0:1473318f27b6 | 11 | |
lrdawg99 | 0:1473318f27b6 | 12 | Redistribution and use in source and binary forms, with or without |
lrdawg99 | 0:1473318f27b6 | 13 | modification, are permitted provided that the following conditions are met: |
lrdawg99 | 0:1473318f27b6 | 14 | |
lrdawg99 | 0:1473318f27b6 | 15 | 1. Redistributions of source code must retain the above copyright notice, this |
lrdawg99 | 0:1473318f27b6 | 16 | list of conditions and the following disclaimer. |
lrdawg99 | 0:1473318f27b6 | 17 | 2. Redistributions in binary form must reproduce the above copyright notice, |
lrdawg99 | 0:1473318f27b6 | 18 | this list of conditions and the following disclaimer in the documentation |
lrdawg99 | 0:1473318f27b6 | 19 | and/or other materials provided with the distribution. |
lrdawg99 | 0:1473318f27b6 | 20 | |
lrdawg99 | 0:1473318f27b6 | 21 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
lrdawg99 | 0:1473318f27b6 | 22 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
lrdawg99 | 0:1473318f27b6 | 23 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
lrdawg99 | 0:1473318f27b6 | 24 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR |
lrdawg99 | 0:1473318f27b6 | 25 | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
lrdawg99 | 0:1473318f27b6 | 26 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
lrdawg99 | 0:1473318f27b6 | 27 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
lrdawg99 | 0:1473318f27b6 | 28 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
lrdawg99 | 0:1473318f27b6 | 29 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
lrdawg99 | 0:1473318f27b6 | 30 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
lrdawg99 | 0:1473318f27b6 | 31 | |
lrdawg99 | 0:1473318f27b6 | 32 | The views and conclusions contained in the software and documentation are those |
lrdawg99 | 0:1473318f27b6 | 33 | of the authors and should not be interpreted as representing official policies, |
lrdawg99 | 0:1473318f27b6 | 34 | either expressed or implied, of Linear Technology Corp. |
lrdawg99 | 0:1473318f27b6 | 35 | |
lrdawg99 | 0:1473318f27b6 | 36 | The Linear Technology Linduino is not affiliated with the official Arduino team. |
lrdawg99 | 0:1473318f27b6 | 37 | However, the Linduino is only possible because of the Arduino team's commitment |
lrdawg99 | 0:1473318f27b6 | 38 | to the open-source community. Please, visit http://www.arduino.cc and |
lrdawg99 | 0:1473318f27b6 | 39 | http://store.arduino.cc , and consider a purchase that will help fund their |
lrdawg99 | 0:1473318f27b6 | 40 | ongoing work. |
lrdawg99 | 0:1473318f27b6 | 41 | */ |
lrdawg99 | 0:1473318f27b6 | 42 | |
lrdawg99 | 0:1473318f27b6 | 43 | /*! @file |
lrdawg99 | 0:1473318f27b6 | 44 | @ingroup LT_I2C |
lrdawg99 | 0:1473318f27b6 | 45 | Library Header File for LT_I2C: Routines to communicate with ATmega328P's hardware I2C port. |
lrdawg99 | 0:1473318f27b6 | 46 | */ |
lrdawg99 | 0:1473318f27b6 | 47 | |
lrdawg99 | 0:1473318f27b6 | 48 | #ifndef LT_I2C_H |
lrdawg99 | 0:1473318f27b6 | 49 | #define LT_I2C_H |
lrdawg99 | 0:1473318f27b6 | 50 | |
lrdawg99 | 0:1473318f27b6 | 51 | #include <stdint.h> |
lrdawg99 | 0:1473318f27b6 | 52 | //#include <Wire.h> |
lrdawg99 | 0:1473318f27b6 | 53 | |
lrdawg99 | 0:1473318f27b6 | 54 | //! @name HARDWARE I2C PRESCALER VALUES |
lrdawg99 | 0:1473318f27b6 | 55 | //! @{ |
lrdawg99 | 0:1473318f27b6 | 56 | #define HARDWARE_I2C_PRESCALER_1 0 |
lrdawg99 | 0:1473318f27b6 | 57 | #define HARDWARE_I2C_PRESCALER_4 1 |
lrdawg99 | 0:1473318f27b6 | 58 | #define HARDWARE_I2C_PRESCALER_16 2 |
lrdawg99 | 0:1473318f27b6 | 59 | #define HARDWARE_I2C_PRESCALER_64 3 |
lrdawg99 | 0:1473318f27b6 | 60 | //! @} |
lrdawg99 | 0:1473318f27b6 | 61 | |
lrdawg99 | 0:1473318f27b6 | 62 | //! @name I2C READ and WRITE BITS |
lrdawg99 | 0:1473318f27b6 | 63 | //! @{ |
lrdawg99 | 0:1473318f27b6 | 64 | //! Eighth bit (LSB) of I2C address indicates a "read" or "write". |
lrdawg99 | 0:1473318f27b6 | 65 | //! (The first seven bits are the 7-bit I2C address.) |
lrdawg99 | 0:1473318f27b6 | 66 | #define I2C_READ_BIT 0x01 |
lrdawg99 | 0:1473318f27b6 | 67 | #define I2C_WRITE_BIT 0x00 |
lrdawg99 | 0:1473318f27b6 | 68 | //! @} |
lrdawg99 | 0:1473318f27b6 | 69 | |
lrdawg99 | 0:1473318f27b6 | 70 | //! @name STATUS BITS |
lrdawg99 | 0:1473318f27b6 | 71 | //! @{ |
lrdawg99 | 0:1473318f27b6 | 72 | #define STATUS_START 0x08 |
lrdawg99 | 0:1473318f27b6 | 73 | #define STATUS_REPEATED_START 0x10 |
lrdawg99 | 0:1473318f27b6 | 74 | #define STATUS_ADDRESS_WRITE_ACK 0x18 |
lrdawg99 | 0:1473318f27b6 | 75 | #define STATUS_ADDRESS_WRITE_NACK 0x20 |
lrdawg99 | 0:1473318f27b6 | 76 | #define STATUS_WRITE_ACK 0x28 |
lrdawg99 | 0:1473318f27b6 | 77 | #define STATUS_WRITE_NACK 0x30 |
lrdawg99 | 0:1473318f27b6 | 78 | #define STATUS_ARBITRATION_LOST 0x38 |
lrdawg99 | 0:1473318f27b6 | 79 | #define STATUS_ADDRESS_READ_ACK 0x40 |
lrdawg99 | 0:1473318f27b6 | 80 | #define STATUS_ADDRESS_READ_NACK 0x48 |
lrdawg99 | 0:1473318f27b6 | 81 | #define STATUS_READ_ACK 0x50 |
lrdawg99 | 0:1473318f27b6 | 82 | #define STATUS_READ_NACK 0x58 |
lrdawg99 | 0:1473318f27b6 | 83 | //! @} |
lrdawg99 | 0:1473318f27b6 | 84 | |
lrdawg99 | 0:1473318f27b6 | 85 | //! @name TIMEOUT AND DELAY IN US |
lrdawg99 | 0:1473318f27b6 | 86 | //! @{ |
lrdawg99 | 0:1473318f27b6 | 87 | #define HW_I2C_DELAY 1 |
lrdawg99 | 0:1473318f27b6 | 88 | #define HW_I2C_TIMEOUT 20000 |
lrdawg99 | 0:1473318f27b6 | 89 | //! @} |
lrdawg99 | 0:1473318f27b6 | 90 | |
lrdawg99 | 0:1473318f27b6 | 91 | //! @name ACK OR NACK PARAMETER PASSED TO I2C_READ |
lrdawg99 | 0:1473318f27b6 | 92 | //! @{ |
lrdawg99 | 0:1473318f27b6 | 93 | #define WITH_ACK 0 //!< Use with i2c_read(WITH_ACK) to read with an acknowledge |
lrdawg99 | 0:1473318f27b6 | 94 | #define WITH_NACK 1 //!< Use with i2c_read(WITH_NACK) to read without an acknowledge. Normally used after the last byte of a multi-byte read. |
lrdawg99 | 0:1473318f27b6 | 95 | //! @} |
lrdawg99 | 0:1473318f27b6 | 96 | |
lrdawg99 | 0:1473318f27b6 | 97 | //! @name OPTIONAL I2C Address MACRO |
lrdawg99 | 0:1473318f27b6 | 98 | //! @{ |
lrdawg99 | 0:1473318f27b6 | 99 | #define I2C_8ADDR(address) (address >> 1) //!< Use to convert an 8-bit I2C address to 7 bits. |
lrdawg99 | 0:1473318f27b6 | 100 | //! @} |
lrdawg99 | 0:1473318f27b6 | 101 | |
lrdawg99 | 0:1473318f27b6 | 102 | //! Read a byte, store in "value". |
lrdawg99 | 0:1473318f27b6 | 103 | //! @return 0 on success, 1 on failure |
lrdawg99 | 0:1473318f27b6 | 104 | int8_t i2c_read_byte(uint8_t address, //!< 7-bit I2C address |
lrdawg99 | 0:1473318f27b6 | 105 | uint8_t *value //!< Byte to be read |
lrdawg99 | 0:1473318f27b6 | 106 | ); |
lrdawg99 | 0:1473318f27b6 | 107 | |
lrdawg99 | 0:1473318f27b6 | 108 | //! Write "value" byte to device at "address" |
lrdawg99 | 0:1473318f27b6 | 109 | //! @return 0 on success, 1 on failure |
lrdawg99 | 0:1473318f27b6 | 110 | int8_t i2c_write_byte(uint8_t address, //!< 7-bit I2C address |
lrdawg99 | 0:1473318f27b6 | 111 | uint8_t value //!< Byte to be written |
lrdawg99 | 0:1473318f27b6 | 112 | ); |
lrdawg99 | 0:1473318f27b6 | 113 | |
lrdawg99 | 0:1473318f27b6 | 114 | //! Read a byte of data at register specified by "command", store in "value" |
lrdawg99 | 0:1473318f27b6 | 115 | //! @return 0 on success, 1 on failure |
lrdawg99 | 0:1473318f27b6 | 116 | int8_t i2c_read_byte_data(uint8_t address, //!< 7-bit I2C address |
lrdawg99 | 0:1473318f27b6 | 117 | uint8_t command, //!< Command byte |
lrdawg99 | 0:1473318f27b6 | 118 | uint8_t *value //!< Byte to be read |
lrdawg99 | 0:1473318f27b6 | 119 | ); |
lrdawg99 | 0:1473318f27b6 | 120 | |
lrdawg99 | 0:1473318f27b6 | 121 | //! Write a byte of data to register specified by "command" |
lrdawg99 | 0:1473318f27b6 | 122 | //! @return 0 on success, 1 on failure |
lrdawg99 | 0:1473318f27b6 | 123 | int8_t i2c_write_byte_data(uint8_t address, //!< 7-bit I2C address |
lrdawg99 | 0:1473318f27b6 | 124 | uint8_t command, //!< Command byte |
lrdawg99 | 0:1473318f27b6 | 125 | uint8_t value //!< Byte to be written |
lrdawg99 | 0:1473318f27b6 | 126 | ); |
lrdawg99 | 0:1473318f27b6 | 127 | |
lrdawg99 | 0:1473318f27b6 | 128 | //! Read a 16-bit word of data from register specified by "command" |
lrdawg99 | 0:1473318f27b6 | 129 | //! @return 0 on success, 1 on failure |
lrdawg99 | 0:1473318f27b6 | 130 | int8_t i2c_read_word_data(uint8_t address, //!< 7-bit I2C address |
lrdawg99 | 0:1473318f27b6 | 131 | uint8_t command, //!< Command byte |
lrdawg99 | 0:1473318f27b6 | 132 | uint16_t *value //!< Word to be read |
lrdawg99 | 0:1473318f27b6 | 133 | ); |
lrdawg99 | 0:1473318f27b6 | 134 | |
lrdawg99 | 0:1473318f27b6 | 135 | //! Write a 16-bit word of data to register specified by "command" |
lrdawg99 | 0:1473318f27b6 | 136 | //! @return 0 on success, 1 on failure |
lrdawg99 | 0:1473318f27b6 | 137 | int8_t i2c_write_word_data(uint8_t address, //!< 7-bit I2C address |
lrdawg99 | 0:1473318f27b6 | 138 | uint8_t command, //!< Command byte |
lrdawg99 | 0:1473318f27b6 | 139 | uint16_t value //!< Word to be written |
lrdawg99 | 0:1473318f27b6 | 140 | ); |
lrdawg99 | 0:1473318f27b6 | 141 | |
lrdawg99 | 0:1473318f27b6 | 142 | //! Read a block of data, starting at register specified by "command" and ending at (command + length - 1) |
lrdawg99 | 0:1473318f27b6 | 143 | //! @return 0 on success, 1 on failure |
lrdawg99 | 0:1473318f27b6 | 144 | int8_t i2c_read_block_data(uint8_t address, //!< 7-bit I2C address |
lrdawg99 | 0:1473318f27b6 | 145 | uint8_t command, //!< Command byte |
lrdawg99 | 0:1473318f27b6 | 146 | uint8_t length, //!< Length of array |
lrdawg99 | 0:1473318f27b6 | 147 | uint8_t *values //!< Byte array to be read |
lrdawg99 | 0:1473318f27b6 | 148 | ); |
lrdawg99 | 0:1473318f27b6 | 149 | |
lrdawg99 | 0:1473318f27b6 | 150 | //! Read a block of data, no command byte, reads length number of bytes and stores it in values. |
lrdawg99 | 0:1473318f27b6 | 151 | //! @return 0 on success, 1 on failure |
lrdawg99 | 0:1473318f27b6 | 152 | int8_t i2c_read_block_data(uint8_t address, //!< 7-bit I2C address |
lrdawg99 | 0:1473318f27b6 | 153 | uint8_t length, //!< Length of array |
lrdawg99 | 0:1473318f27b6 | 154 | uint8_t *values //!< Byte array to be read |
lrdawg99 | 0:1473318f27b6 | 155 | ); |
lrdawg99 | 0:1473318f27b6 | 156 | |
lrdawg99 | 0:1473318f27b6 | 157 | |
lrdawg99 | 0:1473318f27b6 | 158 | //! Write a block of data, starting at register specified by "command" and ending at (command + length - 1) |
lrdawg99 | 0:1473318f27b6 | 159 | //! @return 0 on success, 1 on failure |
lrdawg99 | 0:1473318f27b6 | 160 | int8_t i2c_write_block_data(uint8_t address, //!< 7-bit I2C address |
lrdawg99 | 0:1473318f27b6 | 161 | uint8_t command, //!< Command byte |
lrdawg99 | 0:1473318f27b6 | 162 | uint8_t length, //!< Length of array |
lrdawg99 | 0:1473318f27b6 | 163 | uint8_t *values //!< Byte array to be written |
lrdawg99 | 0:1473318f27b6 | 164 | ); |
lrdawg99 | 0:1473318f27b6 | 165 | |
lrdawg99 | 0:1473318f27b6 | 166 | //! Write a two command bytes, then receive a block of data |
lrdawg99 | 0:1473318f27b6 | 167 | //! @return 0 on success, 1 on failure |
lrdawg99 | 0:1473318f27b6 | 168 | int8_t i2c_two_byte_command_read_block(uint8_t address, //!< 7-bit I2C address |
lrdawg99 | 0:1473318f27b6 | 169 | uint16_t command, //!< Command word |
lrdawg99 | 0:1473318f27b6 | 170 | uint8_t length, //!< Length of array |
lrdawg99 | 0:1473318f27b6 | 171 | uint8_t *values //!< Byte array to be read |
lrdawg99 | 0:1473318f27b6 | 172 | ); |
lrdawg99 | 0:1473318f27b6 | 173 | |
lrdawg99 | 0:1473318f27b6 | 174 | //! Initializes Linduino I2C port. |
lrdawg99 | 0:1473318f27b6 | 175 | //! Before communicating to the I2C port through the QuikEval connector, you must also run |
lrdawg99 | 0:1473318f27b6 | 176 | //! quikeval_I2C_connect to connect the I2C port to the QuikEval connector through the |
lrdawg99 | 0:1473318f27b6 | 177 | //! QuikEval MUX (and disconnect SPI). |
lrdawg99 | 0:1473318f27b6 | 178 | void quikeval_I2C_init(void); |
lrdawg99 | 0:1473318f27b6 | 179 | |
lrdawg99 | 0:1473318f27b6 | 180 | //! Switch MUX to connect I2C pins to QuikEval connector. |
lrdawg99 | 0:1473318f27b6 | 181 | //! This will disconnect SPI pins. |
lrdawg99 | 0:1473318f27b6 | 182 | void quikeval_I2C_connect(void); |
lrdawg99 | 0:1473318f27b6 | 183 | |
lrdawg99 | 0:1473318f27b6 | 184 | //! i2c_enable or quikeval_I2C_init must be called before using any of the other I2C routines. |
lrdawg99 | 0:1473318f27b6 | 185 | void i2c_enable(void); |
lrdawg99 | 0:1473318f27b6 | 186 | |
lrdawg99 | 0:1473318f27b6 | 187 | //! Write start bit to the hardware I2C port |
lrdawg99 | 0:1473318f27b6 | 188 | //! @return 0 if successful, 1 if not successful |
lrdawg99 | 0:1473318f27b6 | 189 | int8_t i2c_start(); |
lrdawg99 | 0:1473318f27b6 | 190 | |
lrdawg99 | 0:1473318f27b6 | 191 | //! Write a repeat start bit to the hardware I2C port |
lrdawg99 | 0:1473318f27b6 | 192 | //! @return 0 if successful, 1 if not successful |
lrdawg99 | 0:1473318f27b6 | 193 | int8_t i2c_repeated_start(); |
lrdawg99 | 0:1473318f27b6 | 194 | |
lrdawg99 | 0:1473318f27b6 | 195 | //! Write stop bit to the hardware I2C port |
lrdawg99 | 0:1473318f27b6 | 196 | void i2c_stop(); |
lrdawg99 | 0:1473318f27b6 | 197 | |
lrdawg99 | 0:1473318f27b6 | 198 | //! Send a data byte to hardware I2C port |
lrdawg99 | 0:1473318f27b6 | 199 | //! @return 0 if successful, 1 if not successful |
lrdawg99 | 0:1473318f27b6 | 200 | int8_t i2c_write(uint8_t data //!< byte that will be written to hardware I2C port. |
lrdawg99 | 0:1473318f27b6 | 201 | ); |
lrdawg99 | 0:1473318f27b6 | 202 | |
lrdawg99 | 0:1473318f27b6 | 203 | //! Read a data byte from the hardware I2C port. |
lrdawg99 | 0:1473318f27b6 | 204 | //! If ack is 0 then an acknowledge (ACK) is generated, else a NACK is generated. |
lrdawg99 | 0:1473318f27b6 | 205 | //! @return the data byte read. |
lrdawg99 | 0:1473318f27b6 | 206 | uint8_t i2c_read(int8_t ack //!< If ACK is 0 then an acknowledge is generated, else a NACK is generated. |
lrdawg99 | 0:1473318f27b6 | 207 | ); |
lrdawg99 | 0:1473318f27b6 | 208 | |
lrdawg99 | 0:1473318f27b6 | 209 | //! Poll the I2C port and look for an acknowledge |
lrdawg99 | 0:1473318f27b6 | 210 | //! @return Returns 0 if successful, 1 if not successful |
lrdawg99 | 0:1473318f27b6 | 211 | int8_t i2c_poll(uint8_t i2c_address //!< i2c_address is the address of the slave being polled. |
lrdawg99 | 0:1473318f27b6 | 212 | ); |
lrdawg99 | 0:1473318f27b6 | 213 | |
lrdawg99 | 0:1473318f27b6 | 214 | |
lrdawg99 | 0:1473318f27b6 | 215 | // //! Read a byte, store in "value". |
lrdawg99 | 0:1473318f27b6 | 216 | // //! @return -1 if failed or value if it succeeds |
lrdawg99 | 0:1473318f27b6 | 217 | // int32_t i2c_read_byte(uint8_t address, //!< 7-bit I2C address |
lrdawg99 | 0:1473318f27b6 | 218 | // uint8_t *value //!< Byte to be read |
lrdawg99 | 0:1473318f27b6 | 219 | // ); |
lrdawg99 | 0:1473318f27b6 | 220 | // |
lrdawg99 | 0:1473318f27b6 | 221 | // //! Write "value" byte to device at "address" |
lrdawg99 | 0:1473318f27b6 | 222 | // //! @return -1 if failed or 0 if it succeeds |
lrdawg99 | 0:1473318f27b6 | 223 | // int32_t i2c_write_byte(uint8_t address, //!< 7-bit I2C address |
lrdawg99 | 0:1473318f27b6 | 224 | // uint8_t value //!< Byte to be written |
lrdawg99 | 0:1473318f27b6 | 225 | // ); |
lrdawg99 | 0:1473318f27b6 | 226 | // |
lrdawg99 | 0:1473318f27b6 | 227 | // //! Read a byte of data at register specified by "command", store in "value" |
lrdawg99 | 0:1473318f27b6 | 228 | // //! @return -1 if failed or value if it succeeds |
lrdawg99 | 0:1473318f27b6 | 229 | // int32_t i2c_read_byte_data(uint8_t address, //!< 7-bit I2C address |
lrdawg99 | 0:1473318f27b6 | 230 | // uint8_t command, //!< Command byte |
lrdawg99 | 0:1473318f27b6 | 231 | // uint8_t *value //!< Byte to be read |
lrdawg99 | 0:1473318f27b6 | 232 | // ); |
lrdawg99 | 0:1473318f27b6 | 233 | // |
lrdawg99 | 0:1473318f27b6 | 234 | // //! Write a byte of data to register specified by "command" |
lrdawg99 | 0:1473318f27b6 | 235 | // //! @return -1 if failed or 0 if it succeeds |
lrdawg99 | 0:1473318f27b6 | 236 | // int32_t i2c_write_byte_data(uint8_t address, //!< 7-bit I2C address |
lrdawg99 | 0:1473318f27b6 | 237 | // uint8_t command, //!< Command byte |
lrdawg99 | 0:1473318f27b6 | 238 | // uint8_t value //!< Byte to be written |
lrdawg99 | 0:1473318f27b6 | 239 | // ); |
lrdawg99 | 0:1473318f27b6 | 240 | // |
lrdawg99 | 0:1473318f27b6 | 241 | // //! Read a 16-bit word of data from register specified by "command" |
lrdawg99 | 0:1473318f27b6 | 242 | // //! @return -1 if failed or value if it succeeds |
lrdawg99 | 0:1473318f27b6 | 243 | // int32_t i2c_read_word_data(uint8_t address, //!< 7-bit I2C address |
lrdawg99 | 0:1473318f27b6 | 244 | // uint8_t command, //!< Command byte |
lrdawg99 | 0:1473318f27b6 | 245 | // uint16_t *value //!< Word to be read |
lrdawg99 | 0:1473318f27b6 | 246 | // ); |
lrdawg99 | 0:1473318f27b6 | 247 | // |
lrdawg99 | 0:1473318f27b6 | 248 | // //! Write a 16-bit word of data to register specified by "command" |
lrdawg99 | 0:1473318f27b6 | 249 | // //! @return -1 if failed or 0 if it succeeds |
lrdawg99 | 0:1473318f27b6 | 250 | // int32_t i2c_write_word_data(uint8_t address, //!< 7-bit I2C address |
lrdawg99 | 0:1473318f27b6 | 251 | // uint8_t command, //!< Command byte |
lrdawg99 | 0:1473318f27b6 | 252 | // uint16_t value //!< Word to be written |
lrdawg99 | 0:1473318f27b6 | 253 | // ); |
lrdawg99 | 0:1473318f27b6 | 254 | // |
lrdawg99 | 0:1473318f27b6 | 255 | // |
lrdawg99 | 0:1473318f27b6 | 256 | // //! Read a block of data, starting at register specified by "command" and ending at (command + length - 1) |
lrdawg99 | 0:1473318f27b6 | 257 | // //! @return Byte count |
lrdawg99 | 0:1473318f27b6 | 258 | // int32_t i2c_read_block_data(uint8_t address, //!< 7-bit I2C address |
lrdawg99 | 0:1473318f27b6 | 259 | // uint8_t command, //!< Command byte |
lrdawg99 | 0:1473318f27b6 | 260 | // uint8_t length, //!< Length of array |
lrdawg99 | 0:1473318f27b6 | 261 | // uint8_t *values //!< Byte array to be read |
lrdawg99 | 0:1473318f27b6 | 262 | // ); |
lrdawg99 | 0:1473318f27b6 | 263 | // |
lrdawg99 | 0:1473318f27b6 | 264 | // //! Write a block of data, starting at register specified by "command" and ending at (command + length - 1) |
lrdawg99 | 0:1473318f27b6 | 265 | // //! @return Byte count |
lrdawg99 | 0:1473318f27b6 | 266 | // int32_t i2c_write_block_data(uint8_t address, //!< 7-bit I2C address |
lrdawg99 | 0:1473318f27b6 | 267 | // uint8_t command, //!< Command byte |
lrdawg99 | 0:1473318f27b6 | 268 | // uint8_t length, //!< Length of array |
lrdawg99 | 0:1473318f27b6 | 269 | // uint8_t *values //!< Byte array to be written |
lrdawg99 | 0:1473318f27b6 | 270 | // ); |
lrdawg99 | 0:1473318f27b6 | 271 | // |
lrdawg99 | 0:1473318f27b6 | 272 | // //! Write a two command bytes, then receive a block of data |
lrdawg99 | 0:1473318f27b6 | 273 | // //! @return Byte count |
lrdawg99 | 0:1473318f27b6 | 274 | // int32_t i2c_two_byte_command_read_block(uint8_t address, //!< 7-bit I2C address |
lrdawg99 | 0:1473318f27b6 | 275 | // uint16_t command, //!< Command word |
lrdawg99 | 0:1473318f27b6 | 276 | // uint8_t length, //!< Length of array |
lrdawg99 | 0:1473318f27b6 | 277 | // uint8_t *values //!< Byte array to be read |
lrdawg99 | 0:1473318f27b6 | 278 | // ); |
lrdawg99 | 0:1473318f27b6 | 279 | // |
lrdawg99 | 0:1473318f27b6 | 280 | // //! Initializes Linduino I2C port. |
lrdawg99 | 0:1473318f27b6 | 281 | // //! Before communicating to the I2C port through the QuikEval connector, you must also run |
lrdawg99 | 0:1473318f27b6 | 282 | // //! quikeval_I2C_connect to connect the I2C port to the QuikEval connector through the |
lrdawg99 | 0:1473318f27b6 | 283 | // //! QuikEval MUX (and disconnect SPI). |
lrdawg99 | 0:1473318f27b6 | 284 | // void quikeval_I2C_init(void); |
lrdawg99 | 0:1473318f27b6 | 285 | // |
lrdawg99 | 0:1473318f27b6 | 286 | // //! Switch MUX to connect I2C pins to QuikEval connector |
lrdawg99 | 0:1473318f27b6 | 287 | // //! This will disconnect SPI pins. |
lrdawg99 | 0:1473318f27b6 | 288 | // void quikeval_I2C_connect(void); |
lrdawg99 | 0:1473318f27b6 | 289 | // |
lrdawg99 | 0:1473318f27b6 | 290 | // //! Setup and enable the hardware I2C interface. |
lrdawg99 | 0:1473318f27b6 | 291 | // //! Must be called before using any of the other routines. |
lrdawg99 | 0:1473318f27b6 | 292 | // // 100kHz example: |
lrdawg99 | 0:1473318f27b6 | 293 | // // uint8 bit_rate = 18; |
lrdawg99 | 0:1473318f27b6 | 294 | // // hw_i2c_enable(bit_rate, HARDWARE_I2C_PRESCALER_4); |
lrdawg99 | 0:1473318f27b6 | 295 | // void i2c_enable(uint8_t bit_rate, //!< I2C Bit Rate |
lrdawg99 | 0:1473318f27b6 | 296 | // uint8_t prescaler //!< I2C prescaler |
lrdawg99 | 0:1473318f27b6 | 297 | // ); |
lrdawg99 | 0:1473318f27b6 | 298 | |
lrdawg99 | 0:1473318f27b6 | 299 | |
lrdawg99 | 0:1473318f27b6 | 300 | // LT I2C functions to emulate Linux SMBus functions for E-Z porting. There does not appear to be |
lrdawg99 | 0:1473318f27b6 | 301 | // a single source of the "correct" way to implement these functions. |
lrdawg99 | 0:1473318f27b6 | 302 | |
lrdawg99 | 0:1473318f27b6 | 303 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
lrdawg99 | 0:1473318f27b6 | 304 | //// From http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/Documentation/i2c/dev-interface |
lrdawg99 | 0:1473318f27b6 | 305 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
lrdawg99 | 0:1473318f27b6 | 306 | //// You can do plain I2C transactions by using read(2) and write(2) calls. |
lrdawg99 | 0:1473318f27b6 | 307 | //// You do not need to pass the address byte; instead, set it through |
lrdawg99 | 0:1473318f27b6 | 308 | //// ioctl I2C_SLAVE before you try to access the device. |
lrdawg99 | 0:1473318f27b6 | 309 | //// |
lrdawg99 | 0:1473318f27b6 | 310 | //// You can do SMBus level transactions (see documentation file smbus-protocol |
lrdawg99 | 0:1473318f27b6 | 311 | //// for details) through the following functions: |
lrdawg99 | 0:1473318f27b6 | 312 | //// __s32 i2c_smbus_write_quick(int file, __u8 value); |
lrdawg99 | 0:1473318f27b6 | 313 | //// __s32 i2c_smbus_read_byte(int file); |
lrdawg99 | 0:1473318f27b6 | 314 | //// __s32 i2c_smbus_write_byte(int file, __u8 value); |
lrdawg99 | 0:1473318f27b6 | 315 | //// __s32 i2c_smbus_read_byte_data(int file, __u8 command); |
lrdawg99 | 0:1473318f27b6 | 316 | //// __s32 i2c_smbus_write_byte_data(int file, __u8 command, __u8 value); |
lrdawg99 | 0:1473318f27b6 | 317 | //// __s32 i2c_smbus_read_word_data(int file, __u8 command); |
lrdawg99 | 0:1473318f27b6 | 318 | //// __s32 i2c_smbus_write_word_data(int file, __u8 command, __u16 value); |
lrdawg99 | 0:1473318f27b6 | 319 | //// __s32 i2c_smbus_process_call(int file, __u8 command, __u16 value); |
lrdawg99 | 0:1473318f27b6 | 320 | //// __s32 i2c_smbus_read_block_data(int file, __u8 command, __u8 *values); |
lrdawg99 | 0:1473318f27b6 | 321 | //// __s32 i2c_smbus_write_block_data(int file, __u8 command, __u8 length, |
lrdawg99 | 0:1473318f27b6 | 322 | //// // __u8 *values); |
lrdawg99 | 0:1473318f27b6 | 323 | //// All these transactions return -1 on failure; you can read errno to see |
lrdawg99 | 0:1473318f27b6 | 324 | //// what happened. The 'write' transactions return 0 on success; the |
lrdawg99 | 0:1473318f27b6 | 325 | //// 'read' transactions return the read value, except for read_block, which |
lrdawg99 | 0:1473318f27b6 | 326 | //// returns the number of values read. The block buffers need not be longer |
lrdawg99 | 0:1473318f27b6 | 327 | //// than 32 bytes. |
lrdawg99 | 0:1473318f27b6 | 328 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
lrdawg99 | 0:1473318f27b6 | 329 | |
lrdawg99 | 0:1473318f27b6 | 330 | |
lrdawg99 | 0:1473318f27b6 | 331 | /* |
lrdawg99 | 0:1473318f27b6 | 332 | |
lrdawg99 | 0:1473318f27b6 | 333 | // Begin "LT-ified" versions of these functions: |
lrdawg99 | 0:1473318f27b6 | 334 | |
lrdawg99 | 0:1473318f27b6 | 335 | // Original Linux prototype: __s32 i2c_smbus_write_quick(int file, __u8 value); |
lrdawg99 | 0:1473318f27b6 | 336 | // Write a single bit in the R/W bit of the address. Note - we are using 8-bit addresses, |
lrdawg99 | 0:1473318f27b6 | 337 | // we should probably AND with 0xFE before appending the "value" bit. Also, should we obligate the user |
lrdawg99 | 0:1473318f27b6 | 338 | // to send a value with a "1" in the LSB, or any nonzero value to assert a 1? |
lrdawg99 | 0:1473318f27b6 | 339 | int8_t LT_i2c_write_quick(uint8_t address, uint8_t value); |
lrdawg99 | 0:1473318f27b6 | 340 | |
lrdawg99 | 0:1473318f27b6 | 341 | // Original Linux prototype: __s32 i2c_smbus_read_byte(int file); |
lrdawg99 | 0:1473318f27b6 | 342 | // Read a byte, store in "value". |
lrdawg99 | 0:1473318f27b6 | 343 | int8_t LT_i2c_read_byte(uint8_t address, uint8_t *value); |
lrdawg99 | 0:1473318f27b6 | 344 | |
lrdawg99 | 0:1473318f27b6 | 345 | // Original Linux prototype: __s32 i2c_smbus_write_byte(int file, __u8 value); |
lrdawg99 | 0:1473318f27b6 | 346 | // Write "value" byte to device at "address" |
lrdawg99 | 0:1473318f27b6 | 347 | int8_t LT_i2c_write_byte(uint8_t address, uint8_t value); |
lrdawg99 | 0:1473318f27b6 | 348 | |
lrdawg99 | 0:1473318f27b6 | 349 | // Original Linux prototype: __s32 i2c_smbus_read_byte_data(int file, __u8 command); |
lrdawg99 | 0:1473318f27b6 | 350 | // Read a byte of data at register specified by "command", store in "value" |
lrdawg99 | 0:1473318f27b6 | 351 | int8_t LT_i2c_read_byte_data(uint8_t address, uint8_t command, uint8_t *value); |
lrdawg99 | 0:1473318f27b6 | 352 | |
lrdawg99 | 0:1473318f27b6 | 353 | // Original Linux prototype: __s32 i2c_smbus_write_byte_data(int file, __u8 command, __u8 value); |
lrdawg99 | 0:1473318f27b6 | 354 | // Write a byte of data to register specified by "command" |
lrdawg99 | 0:1473318f27b6 | 355 | int8_t LT_i2c_write_byte_data(uint8_t address, uint8_t command, uint8_t value); |
lrdawg99 | 0:1473318f27b6 | 356 | |
lrdawg99 | 0:1473318f27b6 | 357 | // Original Linux prototype: __s32 i2c_smbus_read_word_data(int file, __u8 command); |
lrdawg99 | 0:1473318f27b6 | 358 | // Read a 16-bit word of data from register specified by "command" |
lrdawg99 | 0:1473318f27b6 | 359 | int8_t LT_i2c_read_word_data(uint8_t address, uint8_t command, uint16_t *value); |
lrdawg99 | 0:1473318f27b6 | 360 | |
lrdawg99 | 0:1473318f27b6 | 361 | // Original Linux prototype: __s32 i2c_smbus_write_word_data(int file, __u8 command, __u16 value); |
lrdawg99 | 0:1473318f27b6 | 362 | // Write a 16-bit word of data to register specified by "command" |
lrdawg99 | 0:1473318f27b6 | 363 | int8_t LT_i2c_write_word_data(uint8_t address, uint8_t command, uint16_t value); |
lrdawg99 | 0:1473318f27b6 | 364 | |
lrdawg99 | 0:1473318f27b6 | 365 | // Original Linux prototype: __s32 i2c_smbus_process_call(int file, __u8 command, __u16 value); |
lrdawg99 | 0:1473318f27b6 | 366 | int8_t LT_i2c_process_call(uint8_t address, uint8_t command, uint16_t value); |
lrdawg99 | 0:1473318f27b6 | 367 | |
lrdawg99 | 0:1473318f27b6 | 368 | // For block read / write, this explains the lack of length parameter in some versions: |
lrdawg99 | 0:1473318f27b6 | 369 | // "Until kernel 2.6.22, the length is hard coded to 32 bytes. If you |
lrdawg99 | 0:1473318f27b6 | 370 | // ask for less than 32 bytes, your code will only work with kernels |
lrdawg99 | 0:1473318f27b6 | 371 | // 2.6.23 and later." |
lrdawg99 | 0:1473318f27b6 | 372 | |
lrdawg99 | 0:1473318f27b6 | 373 | // Original Linux prototype: __s32 i2c_smbus_read_block_data(int file, __u8 command, __u8 *values); |
lrdawg99 | 0:1473318f27b6 | 374 | // Read a block of data, starting at register specified by "command" and ending at (command + length - 1) |
lrdawg99 | 0:1473318f27b6 | 375 | int8_t LT_i2c_read_block_data(uint8_t address, uint8_t command, uint8_t length, uint8_t *values); |
lrdawg99 | 0:1473318f27b6 | 376 | |
lrdawg99 | 0:1473318f27b6 | 377 | // Original Linux prototype: __s32 i2c_smbus_write_block_data(int file, __u8 command, __u8 length, __u8 *values); |
lrdawg99 | 0:1473318f27b6 | 378 | // Write a block of data, starting at register specified by "command" and ending at (command + length - 1) |
lrdawg99 | 0:1473318f27b6 | 379 | int8_t LT_i2c_write_block_data(uint8_t address, uint8_t command, uint8_t length, uint8_t *values); |
lrdawg99 | 0:1473318f27b6 | 380 | |
lrdawg99 | 0:1473318f27b6 | 381 | // Proposed additional function to handle cases like the LTC2495 / 97 / 99 family, which require |
lrdawg99 | 0:1473318f27b6 | 382 | // a 16-bit command, and can return either 3 or 4 bytes of data. |
lrdawg99 | 0:1473318f27b6 | 383 | // Write a two command bytes, then receive a block of data |
lrdawg99 | 0:1473318f27b6 | 384 | int8_t LT_i2c_two_byte_command_read_block(uint8_t address, uint16_t command, uint8_t length, uint8_t *values); |
lrdawg99 | 0:1473318f27b6 | 385 | |
lrdawg99 | 0:1473318f27b6 | 386 | */ |
lrdawg99 | 0:1473318f27b6 | 387 | |
lrdawg99 | 0:1473318f27b6 | 388 | #endif // LT_I2C_H |