machine.I2C
I2C bus protocol, simply use two lines (SCL, SDA) to control multiple slaves (master mode).
- Support master mode and slave mode
- 7-bit/10-bit addressing mode
- Standard mode <=100Kb/s
- Fast mode <=400Kb/s
- Super fast mode <=1000Kb/s
- High-speed mode 3.4Mb/s
1. Constructor#
Create a new I2C object with the specified parameters
1.1. Parameters#
id
: I2C ID, [0~2] (I2C.I2C0~I2C.I2C2) [3~5] (I2C.I2C3~I2C.I2C5, I2C_SOFT) is the number of soft analog I2Cmode
: Mode, master (I2C.MODE_MASTER
) and slave (I2C.MODE_SLAVE
) modesscl
: SCL pin, just pass the pin number directly, the value range: [0,47]. It is not necessary to set, but use fm to manage pin mapping in a unified manner.sda
: SDA pin, just pass the pin number directly, the value range: [0,47]. It is not necessary to set, but use fm to manage pin mapping in a unified manner.gscl
: GPIOHS corresponding to SCL, only need to be passed when using software to simulate I2C, the default is the same asscl
.gsda
: GPIOHS corresponding to SDA, only need to be passed when using software to simulate I2C, the default is the same assda
.freq
: I2C communication frequency, supports standard 100Kb/s, fast 400Kb/s, and higher rates (hardware supports ultra-fast mode 1000Kb/s, and high-speed mode 3.4Mb/s)timeout
: timeout time, currently this parameter is reserved, the setting is invalidaddr
: slave address, if it is in master mode, don’t need to set, slave mode means slave (local) addressaddr_size
: address length, supports 7-bit addressing and 10-bit addressing, the value is7
or10
on_recieve
: Receive callback function in slave modeon_transmit
: send callback function in slave modeon_event
: event function in slave mode (start event and end event)
2. Method#
2.1. init#
Similar constructor
2.1.1. Parameters#
Same as constructor
2.1.2. return value#
no
2.2. scan#
Scan the slave on the I2C bus
2.2.1. Parameters#
no
2.2.2. return value#
list object, contains all scanned slave addresses
2.3. readfrom#
Read data from the bus
2.3.1. Parameters#
addr
: slave addresslen
: data lengthstop
: Whether to generate a stop signal, keep it, currently only the default value Ture can be used
2.3.2. return value#
The read data, bytes
type
2.4. readfrom_into#
Read the data and put it in the specified variable
2.4.1. Parameters#
addr
: slave addressbuf
:bytearray
type, the length is defined, the data read is stored herestop
: Whether to generate a stop signal, keep it, currently only the default value Ture can be used
2.4.2. return value#
no
2.5. writeto#
Send data to slave
2.5.1. Parameters#
addr
: slave addressbuf
: The data to be sentstop
: Whether to generate a stop signal, keep it, currently only the default value Ture can be used
2.5.2. return value#
Number of bytes sent successfully
2.6. readfrom_mem#
Read slave register
2.6.1. Parameters#
addr
: slave addressmemaddr
: slave register addressnbytes
: the length to be readmem_size
: register width, the default is 8 bits
2.6.2. return value#
Returns the read data of bytes
type
2.7. readfrom_mem_into#
Read the slave register value into the specified variable
2.7.1. Parameters#
addr
: slave addressmemaddr
: slave register addressbuf
:bytearray
type, the length is defined, the data read is stored heremem_size
: register width, the default is 8 bits
2.7.2. return value#
no
2.8. writeto_mem#
Write data to slave register
2.8.1. Parameters#
addr
: slave addressmemaddr
: slave register addressbuf
: the data to be writtenmem_size
: register width, the default is 8 bits
2.8.2. return value#
no
2.9. deinit/__del__#
Log off the I2C hardware, release the occupied resources, and turn off the I2C clock
2.9.1. Parameters#
no
2.9.2. return value#
no
2.9.3. Examples#
or
3. Constant#
I2C0
: I2C 0I2C1
: I2C 1I2C2
: I2C 2MODE_MASTER
: as the master modeMODE_SLAVE
: as a slave modeI2C_EV_START
: Event type, start signalI2C_EV_RESTART
: Event type, restart signalI2C_EV_STOP
: Event type, end signal
Related Issues not found
Please login GitHub to create issue