maix.peripheral.i2c

maix.peripheral.i2c module

You can use maix.peripheral.i2c to access this module with MaixPy
This module is generated from MaixPy and MaixCDK

Module

No module

Enum

AddrSize

Address size enum

item describe
values SEVEN_BIT: 7-bit address mode
TEN_BIT: 10-bit address mode

C++ defination code:

enum AddrSize
    {
        SEVEN_BIT = 7,   // 7-bit address mode
        TEN_BIT   = 10   // 10-bit address mode
    }

Mode

I2C mode enum

item describe
values MASTER: master mode
SLAVE: slave mode

C++ defination code:

enum Mode
    {
        MASTER = 0x00, // master mode
        SLAVE = 0x01   // slave mode
    }

Variable

Function

list_devices

def list_devices() -> list[int]

Get supported i2c bus devices.

item description
return i2c bus devices list, int type, is the i2c bus id.

C++ defination code:

std::vector<int> list_devices()

Class

I2C

Peripheral i2c class

C++ defination code:

class I2C

__init__

def __init__(self, id: int, mode: Mode, freq: int = 100000, addr_size: AddrSize = ...) -> None

I2C Device constructor\nthis constructor will be export to MaixPy as _maix.example.Example.init

item description
type func
param id: direction [in], i2c bus id, int type, e.g. 0, 1, 2
freq: direction [in], i2c clock, int type, default is 100000(100kbit/s), will auto set fast mode if freq > 100000.
mode: direction [in], mode of i2c, i2c.Mode.SLAVE or i2c.Mode.MASTER.
addr_size: direction [in], address length of i2c, i2c.AddrSize.SEVEN_BIT or i2c.AddrSize.TEN_BIT.
throw err::Exception if open i2c device failed.
static False

C++ defination code:

I2C(int id, i2c::Mode mode, int freq = 100000, i2c::AddrSize addr_size = i2c::AddrSize::SEVEN_BIT)

scan

def scan(self, addr: int = -1) -> list[int]

scan all i2c salve address on the bus

item description
type func
param addr: If -1, only scan this addr, or scan from 0x08~0x77, default -1.
return the list of i2c slave address, int list type.
static False

C++ defination code:

std::vector<int> scan(int addr = -1)

writeto

def writeto(self, addr: int, data: maix.Bytes(bytes)) -> int

write data to i2c slave

item description
type func
param addr: direction [in], i2c slave address, int type
data: direction [in], data to write, bytes type.
Note: The range of value should be in [0,255].
return if success, return the length of written data, error occurred will return -err::Err.
static False

C++ defination code:

int writeto(int addr, const Bytes &data)

readfrom

def readfrom(*args, **kwargs)

read data from i2c slave

item description
type func
param addr: direction [in], i2c slave address, int type
len: direction [in], data length to read, int type
return the list of data read from i2c slave, bytes type, you should delete it after use in C++.
If read failed, return nullptr in C++, None in MaixPy.
static False

C++ defination code:

Bytes* readfrom(int addr, int len)

writeto_mem

def writeto_mem(self, addr: int, mem_addr: int, data: maix.Bytes(bytes), mem_addr_size: int = 8, mem_addr_le: bool = False) -> int

write data to i2c slave's memory address

item description
type func
param addr: direction [in], i2c slave address, int type
mem_addr: direction [in], memory address want to write, int type.
data: direction [in], data to write, bytes type.
mem_addr_size: direction [in], memory address size, default is 8.
mem_addr_le: direction [in], memory address little endian, default is false, that is send high byte first.
return data length written if success, error occurred will return -err::Err.
static False

C++ defination code:

int writeto_mem(int addr, int mem_addr, const Bytes &data, int mem_addr_size = 8, bool mem_addr_le = false)

readfrom_mem

def readfrom_mem(*args, **kwargs)

read data from i2c slave

item description
type func
param addr: direction [in], i2c slave address, int type
mem_addr: direction [in], memory address want to read, int type.
len: direction [in], data length to read, int type
mem_addr_size: direction [in], memory address size, default is 8.
mem_addr_le: direction [in], memory address little endian, default is false, that is send high byte first.
return the list of data read from i2c slave, bytes type, you should delete it after use in C++.
If read failed, return nullptr in C++, None in MaixPy.
static False

C++ defination code:

Bytes* readfrom_mem(int addr, int mem_addr, int len, int mem_addr_size = 8, bool mem_addr_le = false)