mirror of
https://github.com/micropython/micropython.git
synced 2026-01-06 12:10:13 +01:00
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>
This commit is contained in:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user