Linduino  1.3.0
Linear Technology Arduino-Compatible Demonstration Board

LT_I2C: Routines to communicate with ATmega328P's hardware I2C port. More...

Detailed Description

LT_I2C: Routines to communicate with ATmega328P's hardware I2C port.

Copyright 2018(c) Analog Devices, Inc.

All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  • Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  • Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  • Neither the name of Analog Devices, Inc. nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
  • The use of this software may or may not infringe the patent rights of one or more patent holders. This license does not release you from the requirement that you obtain separate licenses from these patent holders to use this software.
  • Use of the software either in source or binary form, must be run on or directly connected to an Analog Devices Inc. component.

THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Library Header File for LT_I2C: Routines to communicate with ATmega328P's hardware I2C port.

Definition in file LT_I2C.h.

Go to the source code of this file.

Functions

int8_t i2c_read_byte (uint8_t address, uint8_t *value)
 Read a byte, store in "value". More...
 
int8_t i2c_write_byte (uint8_t address, uint8_t value)
 Write "value" byte to device at "address". More...
 
int8_t i2c_read_byte_data (uint8_t address, uint8_t command, uint8_t *value)
 Read a byte of data at register specified by "command", store in "value". More...
 
int8_t i2c_write_byte_data (uint8_t address, uint8_t command, uint8_t value)
 Write a byte of data to register specified by "command". More...
 
int8_t i2c_read_word_data (uint8_t address, uint8_t command, uint16_t *value)
 Read a 16-bit word of data from register specified by "command". More...
 
int8_t i2c_write_word_data (uint8_t address, uint8_t command, uint16_t value)
 Write a 16-bit word of data to register specified by "command". More...
 
int8_t i2c_read_block_data (uint8_t address, uint8_t command, uint8_t length, uint8_t *values)
 Read a block of data, starting at register specified by "command" and ending at (command + length - 1) More...
 
int8_t i2c_read_block_data (uint8_t address, uint8_t length, uint8_t *values)
 Read a block of data, no command byte, reads length number of bytes and stores it in values. More...
 
int8_t i2c_write_block_data (uint8_t address, uint8_t command, uint8_t length, uint8_t *values)
 Write a block of data, starting at register specified by "command" and ending at (command + length - 1) More...
 
int8_t i2c_two_byte_command_read_block (uint8_t address, uint16_t command, uint8_t length, uint8_t *values)
 Write a two command bytes, then receive a block of data. More...
 
void quikeval_I2C_init (void)
 Initializes Linduino I2C port. More...
 
void quikeval_I2C_connect (void)
 Switch MUX to connect I2C pins to QuikEval connector. More...
 
void i2c_enable (void)
 i2c_enable or quikeval_I2C_init must be called before using any of the other I2C routines. More...
 
int8_t i2c_start ()
 Write start bit to the hardware I2C port. More...
 
int8_t i2c_repeated_start ()
 Write a repeat start bit to the hardware I2C port. More...
 
void i2c_stop ()
 Write stop bit to the hardware I2C port. More...
 
int8_t i2c_write (uint8_t data)
 Send a data byte to hardware I2C port. More...
 
uint8_t i2c_read (int8_t ack)
 Read a data byte from the hardware I2C port. More...
 
int8_t i2c_poll (uint8_t i2c_address)
 Poll the I2C port and look for an acknowledge. More...
 

Macros

HARDWARE I2C PRESCALER VALUES
#define HARDWARE_I2C_PRESCALER_1   0
 
#define HARDWARE_I2C_PRESCALER_4   1
 
#define HARDWARE_I2C_PRESCALER_16   2
 
#define HARDWARE_I2C_PRESCALER_64   3
 
I2C READ and WRITE BITS

Eighth bit (LSB) of I2C address indicates a "read" or "write".

(The first seven bits are the 7-bit I2C address.)

#define I2C_READ_BIT   0x01
 
#define I2C_WRITE_BIT   0x00
 
STATUS BITS
#define STATUS_START   0x08
 
#define STATUS_REPEATED_START   0x10
 
#define STATUS_ADDRESS_WRITE_ACK   0x18
 
#define STATUS_ADDRESS_WRITE_NACK   0x20
 
#define STATUS_WRITE_ACK   0x28
 
#define STATUS_WRITE_NACK   0x30
 
#define STATUS_ARBITRATION_LOST   0x38
 
#define STATUS_ADDRESS_READ_ACK   0x40
 
#define STATUS_ADDRESS_READ_NACK   0x48
 
#define STATUS_READ_ACK   0x50
 
#define STATUS_READ_NACK   0x58
 
TIMEOUT AND DELAY IN US
#define HW_I2C_DELAY   1
 
#define HW_I2C_TIMEOUT   20000
 
ACK OR NACK PARAMETER PASSED TO I2C_READ
#define WITH_ACK   0
 Use with i2c_read(WITH_ACK) to read with an acknowledge. More...
 
#define WITH_NACK   1
 Use with i2c_read(WITH_NACK) to read without an acknowledge. More...
 
OPTIONAL I2C Address MACRO
#define I2C_8ADDR(address)   (address >> 1)
 Use to convert an 8-bit I2C address to 7 bits. More...
 

Function Documentation

◆ i2c_enable()

void i2c_enable ( void  )

i2c_enable or quikeval_I2C_init must be called before using any of the other I2C routines.

Definition at line 414 of file LT_I2C.cpp.

◆ i2c_poll()

int8_t i2c_poll ( uint8_t  i2c_address)

Poll the I2C port and look for an acknowledge.

Returns
Returns 0 if successful, 1 if not successful
Parameters
i2c_addressi2c_address is the address of the slave being polled.

Definition at line 526 of file LT_I2C.cpp.

◆ i2c_read()

uint8_t i2c_read ( int8_t  ack)

Read a data byte from the hardware I2C port.

If ack is 0 then an acknowledge (ACK) is generated, else a NACK is generated.

Returns
the data byte read.
Parameters
ackIf ACK is 0 then an acknowledge is generated, else a NACK is generated.

Definition at line 491 of file LT_I2C.cpp.

◆ i2c_read_block_data() [1/2]

int8_t i2c_read_block_data ( uint8_t  address,
uint8_t  command,
uint8_t  length,
uint8_t *  values 
)

Read a block of data, starting at register specified by "command" and ending at (command + length - 1)

Returns
0 on success, 1 on failure
Parameters
address7-bit I2C address
commandCommand byte
lengthLength of array
valuesByte array to be read

Definition at line 244 of file LT_I2C.cpp.

◆ i2c_read_block_data() [2/2]

int8_t i2c_read_block_data ( uint8_t  address,
uint8_t  length,
uint8_t *  values 
)

Read a block of data, no command byte, reads length number of bytes and stores it in values.

Returns
0 on success, 1 on failure
Parameters
address7-bit I2C address
lengthLength of array
valuesByte array to be read

Definition at line 291 of file LT_I2C.cpp.

◆ i2c_read_byte()

int8_t i2c_read_byte ( uint8_t  address,
uint8_t *  value 
)

Read a byte, store in "value".

Returns
0 on success, 1 on failure

Read a byte, store in "value".

  Wire.endTransmission() returns 0, 1, 2, 3, or 4. To maintain consistency with our Legacy LT_I2C library,
  a zero(false) or a non-zero(true) is tested.

  0 .. success
  1 .. length to long for buffer
  2 .. address send, NACK received
  3 .. data send, NACK received
  4 .. other twi error (lost bus arbitration, bus error, ..)

Copyright 2018(c) Analog Devices, Inc.

All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  • Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  • Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  • Neither the name of Analog Devices, Inc. nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
  • The use of this software may or may not infringe the patent rights of one or more patent holders. This license does not release you from the requirement that you obtain separate licenses from these patent holders to use this software.
  • Use of the software either in source or binary form, must be run on or directly connected to an Analog Devices Inc. component.

THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Parameters
address7-bit I2C address
valueByte to be read

Definition at line 93 of file LT_I2C.cpp.

◆ i2c_read_byte_data()

int8_t i2c_read_byte_data ( uint8_t  address,
uint8_t  command,
uint8_t *  value 
)

Read a byte of data at register specified by "command", store in "value".

Returns
0 on success, 1 on failure
Parameters
address7-bit I2C address
commandCommand byte
valueByte to be read

Definition at line 124 of file LT_I2C.cpp.

◆ i2c_read_word_data()

int8_t i2c_read_word_data ( uint8_t  address,
uint8_t  command,
uint16_t *  value 
)

Read a 16-bit word of data from register specified by "command".

Returns
0 on success, 1 on failure
Parameters
address7-bit I2C address
commandCommand byte
valueWord to be read

Definition at line 172 of file LT_I2C.cpp.

◆ i2c_repeated_start()

int8_t i2c_repeated_start ( )

Write a repeat start bit to the hardware I2C port.

Returns
0 if successful, 1 if not successful

Definition at line 444 of file LT_I2C.cpp.

◆ i2c_start()

int8_t i2c_start ( )

Write start bit to the hardware I2C port.

Returns
0 if successful, 1 if not successful

Definition at line 425 of file LT_I2C.cpp.

◆ i2c_stop()

void i2c_stop ( )

Write stop bit to the hardware I2C port.

Definition at line 462 of file LT_I2C.cpp.

◆ i2c_two_byte_command_read_block()

int8_t i2c_two_byte_command_read_block ( uint8_t  address,
uint16_t  command,
uint8_t  length,
uint8_t *  values 
)

Write a two command bytes, then receive a block of data.

Returns
0 on success, 1 on failure
Parameters
address7-bit I2C address
commandCommand word
lengthLength of array
valuesByte array to be read

Definition at line 345 of file LT_I2C.cpp.

◆ i2c_write()

int8_t i2c_write ( uint8_t  data)

Send a data byte to hardware I2C port.

Returns
0 if successful, 1 if not successful
Parameters
databyte that will be written to hardware I2C port.

Definition at line 470 of file LT_I2C.cpp.

◆ i2c_write_block_data()

int8_t i2c_write_block_data ( uint8_t  address,
uint8_t  command,
uint8_t  length,
uint8_t *  values 
)

Write a block of data, starting at register specified by "command" and ending at (command + length - 1)

Returns
0 on success, 1 on failure
Parameters
address7-bit I2C address
commandCommand byte
lengthLength of array
valuesByte array to be written

Definition at line 321 of file LT_I2C.cpp.

◆ i2c_write_byte()

int8_t i2c_write_byte ( uint8_t  address,
uint8_t  value 
)

Write "value" byte to device at "address".

Returns
0 on success, 1 on failure
Parameters
address7-bit I2C address
valueByte to be written

Definition at line 109 of file LT_I2C.cpp.

◆ i2c_write_byte_data()

int8_t i2c_write_byte_data ( uint8_t  address,
uint8_t  command,
uint8_t  value 
)

Write a byte of data to register specified by "command".

Returns
0 on success, 1 on failure
Parameters
address7-bit I2C address
commandCommand byte
valueByte to be written

Definition at line 155 of file LT_I2C.cpp.

◆ i2c_write_word_data()

int8_t i2c_write_word_data ( uint8_t  address,
uint8_t  command,
uint16_t  value 
)

Write a 16-bit word of data to register specified by "command".

Returns
0 on success, 1 on failure
Parameters
address7-bit I2C address
commandCommand byte
valueWord to be written

Definition at line 216 of file LT_I2C.cpp.

◆ quikeval_I2C_connect()

void quikeval_I2C_connect ( void  )

Switch MUX to connect I2C pins to QuikEval connector.

This will disconnect SPI pins.

Definition at line 401 of file LT_I2C.cpp.

◆ quikeval_I2C_init()

void quikeval_I2C_init ( void  )

Initializes Linduino I2C port.

Before communicating to the I2C port through the QuikEval connector, you must also run quikeval_I2C_connect to connect the I2C port to the QuikEval connector through the QuikEval MUX (and disconnect SPI).

Definition at line 394 of file LT_I2C.cpp.

Macro Definition Documentation

◆ HARDWARE_I2C_PRESCALER_1

#define HARDWARE_I2C_PRESCALER_1   0

Definition at line 53 of file LT_I2C.h.

◆ HARDWARE_I2C_PRESCALER_16

#define HARDWARE_I2C_PRESCALER_16   2

Definition at line 55 of file LT_I2C.h.

◆ HARDWARE_I2C_PRESCALER_4

#define HARDWARE_I2C_PRESCALER_4   1

Definition at line 54 of file LT_I2C.h.

◆ HARDWARE_I2C_PRESCALER_64

#define HARDWARE_I2C_PRESCALER_64   3

Definition at line 56 of file LT_I2C.h.

◆ HW_I2C_DELAY

#define HW_I2C_DELAY   1

Definition at line 84 of file LT_I2C.h.

◆ HW_I2C_TIMEOUT

#define HW_I2C_TIMEOUT   20000

Definition at line 85 of file LT_I2C.h.

◆ I2C_8ADDR

#define I2C_8ADDR (   address)    (address >> 1)

Use to convert an 8-bit I2C address to 7 bits.

Definition at line 96 of file LT_I2C.h.

◆ I2C_READ_BIT

#define I2C_READ_BIT   0x01

Definition at line 63 of file LT_I2C.h.

◆ I2C_WRITE_BIT

#define I2C_WRITE_BIT   0x00

Definition at line 64 of file LT_I2C.h.

◆ STATUS_ADDRESS_READ_ACK

#define STATUS_ADDRESS_READ_ACK   0x40

Definition at line 76 of file LT_I2C.h.

◆ STATUS_ADDRESS_READ_NACK

#define STATUS_ADDRESS_READ_NACK   0x48

Definition at line 77 of file LT_I2C.h.

◆ STATUS_ADDRESS_WRITE_ACK

#define STATUS_ADDRESS_WRITE_ACK   0x18

Definition at line 71 of file LT_I2C.h.

◆ STATUS_ADDRESS_WRITE_NACK

#define STATUS_ADDRESS_WRITE_NACK   0x20

Definition at line 72 of file LT_I2C.h.

◆ STATUS_ARBITRATION_LOST

#define STATUS_ARBITRATION_LOST   0x38

Definition at line 75 of file LT_I2C.h.

◆ STATUS_READ_ACK

#define STATUS_READ_ACK   0x50

Definition at line 78 of file LT_I2C.h.

◆ STATUS_READ_NACK

#define STATUS_READ_NACK   0x58

Definition at line 79 of file LT_I2C.h.

◆ STATUS_REPEATED_START

#define STATUS_REPEATED_START   0x10

Definition at line 70 of file LT_I2C.h.

◆ STATUS_START

#define STATUS_START   0x08

Definition at line 69 of file LT_I2C.h.

◆ STATUS_WRITE_ACK

#define STATUS_WRITE_ACK   0x28

Definition at line 73 of file LT_I2C.h.

◆ STATUS_WRITE_NACK

#define STATUS_WRITE_NACK   0x30

Definition at line 74 of file LT_I2C.h.

◆ WITH_ACK

#define WITH_ACK   0

Use with i2c_read(WITH_ACK) to read with an acknowledge.

Definition at line 90 of file LT_I2C.h.

◆ WITH_NACK

#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.

Definition at line 91 of file LT_I2C.h.