Module i2c

AtomVM I2c interface.

Description

This module provides and interface into the AtomVM I2C driver.

Use this module to communicate with devices connected to your ESP32 device via the 2-wire I2C interface.

Using this interface, you can read or write data to an I2C device at a given I2C address. In addition, you may read from or write to specific registers on the I2C device.

Data Types

address()


address() = non_neg_integer()

freq_hz()


freq_hz() = non_neg_integer()

i2c()


i2c() = pid() | {$i2c, term(), reference()}

param()


param() = {scl, pin()} | {sda, pin()} | {clock_speed_hz, freq_hz()} | {peripheral, peripheral()} | {use_nif, boolean()} | {send_timeout_ms, non_neg_integer()}

params()


params() = [param()]

peripheral()


peripheral() = string() | binary()

pin()


pin() = non_neg_integer()

register()


register() = non_neg_integer()

Function Index

begin_transmission/2 Begin a transmission of I2C commands.
close/1 Closes the connection to the I2C driver.
end_transmission/1 End a transmission of I2C commands.
open/1 Open a connection to the I2C driver.
read_bytes/3 Read a block of bytes from the I2C device.
read_bytes/4 Read a block of bytes from the I2C device starting at a specified register address.
write_byte/2 Write a byte to the device.
write_bytes/2 Write a sequence of bytes to the device.
write_bytes/3 Write a block of bytes to the I2C device.
write_bytes/4 Write a block of bytes to the I2C device starting at a specified register address.

Function Details

begin_transmission/2


begin_transmission(I2C::i2c(), Address::address()) -> ok | {error, Reason::term()}

I2C: I2C instance created via open/1
Address: I2C Address of the device (typically fixed for the device type)

returns: ok or {error, Reason}

Begin a transmission of I2C commands

This command is typically followed by one or more calls to write_byte/2 and then a call to end_transmission/1

close/1


close(I2C::i2c()) -> ok | {error, Reason::term()}

I2C: I2C instance created via open/1

returns: ok atom

Closes the connection to the I2C driver

This function will close the connection to the I2C driver and free any resources in use by it.

end_transmission/1


end_transmission(I2C::i2c()) -> ok | {error, Reason::term()}

I2C: I2C instance created via open/1

returns: ok or {error, Reason}

End a transmission of I2C commands

This command is typically preceded by a call to begin_transmission/2 and one or more calls to write_byte/2.

open/1


open(Params::params()) -> i2c()

Params: Initialization parameters

returns: process id of the driver.

Open a connection to the I2C driver

This function will open a connection to the I2C driver.

read_bytes/3


read_bytes(I2C::i2c(), Address::address(), Count::non_neg_integer()) -> {ok, Data::binary()} | {error, Reason::term()}

I2C: I2C instance created via open/1
Address: I2C Address of the device (typically fixed for the device type)
Count: The number of bytes to read

returns: {ok, Data} which includes the read binary data or {error, Reason}

Read a block of bytes from the I2C device.

This command is not wrapped in a begin_transmission/2 and end_transmission/1 call.

read_bytes/4


read_bytes(I2C::i2c(), Address::address(), Register::register(), Count::non_neg_integer()) -> {ok, binary()} | {error, Reason::term()}

I2C: I2C instance created via open/1
Address: I2C Address of the device (typically fixed for the device type)
Register: The register address in the device from which to read data
Count: The number of bytes to read

returns: {ok, Data} which includes the read binary data or {error, Reason}

Read a block of bytes from the I2C device starting at a specified register address

This command is not wrapped in a begin_transmission/2 and end_transmission/1 call.

write_byte/2


write_byte(I2C::i2c(), Byte::byte()) -> ok | {error, Reason::term()}

I2C: I2C instance created via open/1
Byte: value to write

returns: ok or {error, Reason}

Write a byte to the device.

This command must be wrapped in a begin_transmission/2 and end_transmission/1 call.

write_bytes/2


write_bytes(I2C::i2c(), Bytes::binary()) -> ok | {error, Reason::term()}

I2C: I2C instance created via open/1
Bytes: value to write

returns: ok or {error, Reason}

Write a sequence of bytes to the device.

This command must be wrapped in a begin_transmission/2 and end_transmission/1 call.

write_bytes/3


write_bytes(I2C::i2c(), Address::address(), BinOrInt::binary() | byte()) -> ok | {error, Reason::term()}

I2C: I2C instance created via open/1
Address: I2C Address of the device (typically fixed for the device type)
BinOrInt: The binary or byte value to write

returns: ok or {error, Reason}

Write a block of bytes to the I2C device.

This command is not wrapped in a begin_transmission/2 and end_transmission/1 call.

write_bytes/4


write_bytes(I2C::i2c(), Address::address(), Register::register(), BinOrInt::binary() | integer()) -> ok | {error, Reason::term()}

I2C: I2C instance created via open/1
Address: I2C Address of the device (typically fixed for the device type)
Register: The register address in the device to which to write data
BinOrInt: The binary or byte value to write

returns: ok or {error, Reason}

Write a block of bytes to the I2C device starting at a specified register address.

This command is not wrapped in a begin_transmission/2 and end_transmission/1 call.