Skip to content

Commit 1b578fe

Browse files
committed
extmod/machine_i2c_target: Add new machine.I2CTarget class.
This commit implements a generic I2C target/peripheral/"slave" device, called `machine.I2CTarget`. It can work in two separate modes: - A general device with interrupts/events/callbacks for low-level I2C operations like address match, read request and stop. - A memory device that allows reading/writing a specific region of memory (or "registers") on the target I2C device. To make a memory device is very simple: from machine import I2CTarget mem = bytearray(8) i2c = I2CTarget(addr=67, mem=mem) That's all that's needed to start the I2C target. From then on it will respond to any I2C controller on the bus, allowing reads and writes to the mem bytearray. It's also possible to register to receive events. For example to be notified when the memory is read/written: from machine import I2CTarget def irq_handler(i2c_target): flags = i2c_target.irq().flags() if flags & I2CTarget.IRQ_END_READ: print("controller read target at addr", i2c_target.memaddr) if flags & I2CTarget.IRQ_END_WRITE: print("controller wrote target at addr", i2c_target.memaddr) mem = bytearray(8) i2c = I2CTarget(addr=67, mem=mem) i2c.irq(irq_handler) Instead of a memory device, an arbitrary I2C device can be implemented using all the events (see docs). This is based on the discussion in micropython#3935. Signed-off-by: Damien George <[email protected]>
1 parent ab7c5a1 commit 1b578fe

File tree

5 files changed

+435
-0
lines changed

5 files changed

+435
-0
lines changed

extmod/extmod.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ set(MICROPY_SOURCE_EXTMOD
1111
${MICROPY_EXTMOD_DIR}/machine_adc_block.c
1212
${MICROPY_EXTMOD_DIR}/machine_bitstream.c
1313
${MICROPY_EXTMOD_DIR}/machine_i2c.c
14+
${MICROPY_EXTMOD_DIR}/machine_i2c_target.c
1415
${MICROPY_EXTMOD_DIR}/machine_i2s.c
1516
${MICROPY_EXTMOD_DIR}/machine_mem.c
1617
${MICROPY_EXTMOD_DIR}/machine_pulse.c

extmod/extmod.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ SRC_EXTMOD_C += \
66
extmod/machine_adc_block.c \
77
extmod/machine_bitstream.c \
88
extmod/machine_i2c.c \
9+
extmod/machine_i2c_target.c \
910
extmod/machine_i2s.c \
1011
extmod/machine_mem.c \
1112
extmod/machine_pinbase.c \

0 commit comments

Comments
 (0)