Fork me on GitHub

I2C

This library provides an Inter-Integrated Circuit (I2C) hardware access interface.

C API Reference mgos_i2c.h

#include "mgos_i2c.h"

See on GitHub: mgos_i2c.h, mgos_i2c.c


struct mgos_i2c *mgos_i2c_create(const struct sys_config_i2c *cfg);

Initialize I2C master

bool mgos_i2c_read(struct mgos_i2c *i2c, uint16_t addr, void *data, size_t len,
                   bool stop);

Read specified number of bytes from the specified address. Address should not include the R/W bit. If addr is -1, START is not performed. If |stop| is true, then at the end of the operation bus will be released.

bool mgos_i2c_write(struct mgos_i2c *i2c, uint16_t addr, const void *data,
                    size_t len, bool stop);

Write specified number of bytes from the specified address. Address should not include the R/W bit. If addr is -1, START is not performed. If |stop| is true, then at the end of the operation bus will be released.

void mgos_i2c_stop(struct mgos_i2c *i2c);

Release the bus (when left unreleased after read or write).

int mgos_i2c_get_freq(struct mgos_i2c *i2c);

Get I2C interface frequency.

bool mgos_i2c_set_freq(struct mgos_i2c *i2c, int freq);

Set I2C interface frequency.

int mgos_i2c_read_reg_b(struct mgos_i2c *conn, uint16_t addr, uint8_t reg);

Register read/write routines. These are helpers for reading register values from a device. First a 1-byte write of a register address is performed, followed by either a byte or a word (2 bytes) data read/write. For word operations, value is big-endian: for a read, first byte read from the bus is in bits 15-8, second is in bits 7-0; for a write, bits 15-8 are put on the us first, followed by bits 7-0. Read operations return negative number on error and positive value in the respective range (0-255, 0-65535) on success.

int mgos_i2c_read_reg_w(struct mgos_i2c *conn, uint16_t addr, uint8_t reg);

bool mgos_i2c_read_reg_n(struct mgos_i2c *conn, uint16_t addr, uint8_t reg,
                         size_t n, uint8_t *buf);

bool mgos_i2c_write_reg_b(struct mgos_i2c *conn, uint16_t addr, uint8_t reg,
                          uint8_t value);

bool mgos_i2c_write_reg_w(struct mgos_i2c *conn, uint16_t addr, uint8_t reg,
                          uint16_t value);

bool mgos_i2c_write_reg_n(struct mgos_i2c *conn, uint16_t addr, uint8_t reg,
                          size_t n, const uint8_t *buf);

void mgos_i2c_close(struct mgos_i2c *conn);

Close i2c connection and free resources.

struct mgos_i2c *mgos_i2c_get_global(void);

bool mgos_i2c_init(void);

JAVASCRIPT API Reference api_i2c.js

load("api_i2c.js");


I2C.get()

Get I2C bus handle. Return value: opaque pointer.

I2C.close()

Close I2C handle. Return value: none.

I2C.write(handle, addr, buf, size, stop)

Send a byte array to I2C. If stop is true, the bus will be released at the end. Return value: success, true/false.

I2C.read(handle, addr, len, stop)

Read specified number of bytes from the specified address. If stop is true, the bus will be released at the end. Return value: null on error, string with data on success. Example:

let data = I2C.read(bus, 31, 3, true);  // Read 3 bytes
if (data) print(JSON.stringify([data.at(0), data.at(1), data.at(2)]));

I2C.stop(handle)

Set i2c Stop condition. Releases the bus. Return value: none.

I2C.readRegB(handle, addr, reg)

Read 1-byte register reg from the device at address addr.

I2C.readRegW(handle, addr, reg)

Read 2-byte register reg from the device at address addr.

I2C.writeRegB(handle, addr, reg, val)

Write val into 1-byte register reg at address addr. Return true on success, false on failure.

I2C.writeRegW(handle, addr, reg, val)

Write val into 2-byte register reg at address addr. Return true on success, false on failure.