summaryrefslogtreecommitdiff
path: root/extmod/modmachine.h
diff options
context:
space:
mode:
authorDamien George <damien@micropython.org>2025-05-27 12:21:57 +1000
committerDamien George <damien@micropython.org>2025-08-01 23:03:17 +1000
commit1b578fe2c04402b1fe85b49fb5a89c23ae961f89 (patch)
tree87fa73e08bfa89e14c99d2d0eb5c298083dbd05d /extmod/modmachine.h
parentab7c5a1733b394af43035bdcf844b91fd7756dd9 (diff)
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 #3935. Signed-off-by: Damien George <damien@micropython.org>
Diffstat (limited to 'extmod/modmachine.h')
-rw-r--r--extmod/modmachine.h6
1 files changed, 6 insertions, 0 deletions
diff --git a/extmod/modmachine.h b/extmod/modmachine.h
index 26010be8e..ef507aca7 100644
--- a/extmod/modmachine.h
+++ b/extmod/modmachine.h
@@ -129,6 +129,7 @@
// A port must provide these types, but they are otherwise opaque.
typedef struct _machine_adc_obj_t machine_adc_obj_t;
typedef struct _machine_adc_block_obj_t machine_adc_block_obj_t;
+typedef struct _machine_i2c_target_obj_t machine_i2c_target_obj_t;
typedef struct _machine_i2s_obj_t machine_i2s_obj_t;
typedef struct _machine_pwm_obj_t machine_pwm_obj_t;
typedef struct _machine_uart_obj_t machine_uart_obj_t;
@@ -203,6 +204,7 @@ extern const machine_mem_obj_t machine_mem32_obj;
extern const mp_obj_type_t machine_adc_type;
extern const mp_obj_type_t machine_adc_block_type;
extern const mp_obj_type_t machine_i2c_type;
+extern const mp_obj_type_t machine_i2c_target_type;
extern const mp_obj_type_t machine_i2s_type;
extern const mp_obj_type_t machine_mem_type;
extern const mp_obj_type_t machine_pin_type;
@@ -261,6 +263,10 @@ int mp_machine_i2c_transfer_adaptor(mp_obj_base_t *self, uint16_t addr, size_t n
int mp_machine_soft_i2c_transfer(mp_obj_base_t *self, uint16_t addr, size_t n, mp_machine_i2c_buf_t *bufs, unsigned int flags);
#endif
+#if MICROPY_PY_MACHINE_I2C_TARGET
+void mp_machine_i2c_target_deinit_all(void);
+#endif
+
#if MICROPY_PY_MACHINE_SPI
mp_obj_t mp_machine_spi_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args);
MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(mp_machine_spi_read_obj);