samd/machine_i2c_target: Support I2C target mode.

Supporting readfrom_mem*(). writeto_mem() and a set of IRQs.  Enabled by
default for SAMD51 devices and SAMD21 devices with external flash.

Tested with ItsyBitsy M4 and ItsyBitsy M0 with both on-board SoftI2C and a
RP2 Pico as controller.

Signed-off-by: Damien George <damien@micropython.org>
Signed-off-by: robert-hh <robert@hammelrath.com>
This commit is contained in:
robert-hh
2025-06-05 20:48:25 +02:00
committed by Damien George
parent 5c78762c16
commit 79d182deb2
10 changed files with 259 additions and 26 deletions

View File

@@ -8,6 +8,7 @@
#define MICROPY_PY_MACHINE_SOFTI2C (0)
#define MICROPY_PY_MACHINE_SOFTSPI (0)
#define MICROPY_PY_MACHINE_I2C (0)
#define MICROPY_PY_MACHINE_I2C_TARGET (0)
#define MICROPY_PY_MACHINE_SPI (0)
#define MICROPY_PY_MACHINE_UART (0)
#define MICROPY_PY_MACHINE_ADC (0)

View File

@@ -29,13 +29,11 @@
#if MICROPY_PY_MACHINE_I2C
#include "py/mphal.h"
#include "py/mperrno.h"
#include "extmod/modmachine.h"
#include "samd_soc.h"
#include "pin_af.h"
#include "genhdr/pins.h"
#include "clock_config.h"
#define DEFAULT_I2C_FREQ (400000)
#define RISETIME_NS (200)
@@ -79,9 +77,9 @@ static void i2c_send_command(Sercom *i2c, uint8_t command) {
}
void common_i2c_irq_handler(int i2c_id) {
// handle Sercom I2C IRQ
// Handle Sercom I2C IRQ for controller mode.
machine_i2c_obj_t *self = MP_STATE_PORT(sercom_table[i2c_id]);
// Handle IRQ
if (self != NULL) {
Sercom *i2c = self->instance;
// For now, clear all interrupts
@@ -114,7 +112,8 @@ void common_i2c_irq_handler(int i2c_id) {
} else { // On any error, e.g. ARBLOST or BUSERROR, stop the transmission
self->len = 0;
self->state = state_buserr;
i2c->I2CM.INTFLAG.reg |= SERCOM_I2CM_INTFLAG_ERROR;
i2c->I2CM.INTFLAG.reg = SERCOM_I2CM_INTFLAG_ERROR |
SERCOM_I2CM_INTFLAG_SB | SERCOM_I2CM_INTFLAG_MB;
}
}
}
@@ -158,28 +157,19 @@ mp_obj_t machine_i2c_make_new(const mp_obj_type_t *type, size_t n_args, size_t n
// Get the peripheral object.
machine_i2c_obj_t *self = mp_obj_malloc(machine_i2c_obj_t, &machine_i2c_type);
self->id = id;
self->instance = sercom_instance[self->id];
self->instance = sercom_instance[id];
// Set SCL/SDA pins.
self->scl = mp_hal_get_pin_obj(args[ARG_scl].u_obj);
self->sda = mp_hal_get_pin_obj(args[ARG_sda].u_obj);
self->sda = pin_config_for_i2c(args[ARG_sda].u_obj, id, 0);
self->scl = pin_config_for_i2c(args[ARG_scl].u_obj, id, 1);
MP_STATE_PORT(sercom_table[id]) = self;
sercom_pad_config_t scl_pad_config = get_sercom_config(self->scl, self->id);
sercom_pad_config_t sda_pad_config = get_sercom_config(self->sda, self->id);
if (sda_pad_config.pad_nr != 0 || scl_pad_config.pad_nr != 1) {
mp_raise_ValueError(MP_ERROR_TEXT("invalid sda/scl pin"));
}
MP_STATE_PORT(sercom_table[self->id]) = self;
self->freq = args[ARG_freq].u_int;
// The unit for ARG_timeout is us, but the code uses ms.
self->timeout = args[ARG_timeout].u_int / 1000;
// Configure the Pin mux.
mp_hal_set_pin_mux(self->scl, scl_pad_config.alt_fct);
mp_hal_set_pin_mux(self->sda, sda_pad_config.alt_fct);
// Set up the clocks
enable_sercom_clock(self->id);
enable_sercom_clock(id);
// Initialise the I2C peripheral
Sercom *i2c = self->instance;
@@ -207,13 +197,13 @@ mp_obj_t machine_i2c_make_new(const mp_obj_type_t *type, size_t n_args, size_t n
i2c->I2CM.BAUD.reg = baud;
// Enable interrupts
sercom_register_irq(self->id, &common_i2c_irq_handler);
sercom_register_irq(id, &common_i2c_irq_handler);
#if defined(MCU_SAMD21)
NVIC_EnableIRQ(SERCOM0_IRQn + self->id);
NVIC_EnableIRQ(SERCOM0_IRQn + id);
#elif defined(MCU_SAMD51)
NVIC_EnableIRQ(SERCOM0_0_IRQn + 4 * self->id); // MB interrupt
NVIC_EnableIRQ(SERCOM0_0_IRQn + 4 * self->id + 1); // SB interrupt
NVIC_EnableIRQ(SERCOM0_0_IRQn + 4 * self->id + 3); // ERROR interrupt
NVIC_EnableIRQ(SERCOM0_0_IRQn + 4 * id); // MB interrupt
NVIC_EnableIRQ(SERCOM0_0_IRQn + 4 * id + 1); // SB interrupt
NVIC_EnableIRQ(SERCOM0_0_IRQn + 4 * id + 3); // ERROR interrupt
#endif
// Now enable I2C.

View File

@@ -0,0 +1,213 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2020-2021 Damien P. George
* Copyright (c) 2022-2025 Robert Hammelrath
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "py/runtime.h"
#include "samd_soc.h"
#include "pin_af.h"
#include "genhdr/pins.h"
#define TRANSMIT (1)
#define RECEIVE (0)
#define NACK_RECVD (i2c->I2CS.STATUS.bit.RXNACK == 1)
#define IRQ_AMATCH (i2c->I2CS.INTFLAG.bit.AMATCH == 1)
#define IRQ_DRDY (i2c->I2CS.INTFLAG.bit.DRDY == 1)
#define IRQ_STOP (i2c->I2CS.INTFLAG.bit.PREC == 1)
#define PREPARE_ACK i2c->I2CS.CTRLB.bit.ACKACT = 0
#define PREPARE_NACK i2c->I2CS.CTRLB.bit.ACKACT = 1
typedef struct _machine_i2c_target_obj_t {
mp_obj_base_t base;
Sercom *instance;
uint8_t id;
uint8_t scl;
uint8_t sda;
uint8_t addr;
uint8_t direction;
} machine_i2c_target_obj_t;
void common_i2c_target_irq_handler(int i2c_id) {
// Handle Sercom I2C IRQ for target memory mode.
machine_i2c_target_obj_t *self = MP_STATE_PORT(sercom_table[i2c_id]);
machine_i2c_target_data_t *data = &machine_i2c_target_data[i2c_id];
if (self != NULL) {
Sercom *i2c = self->instance;
if (IRQ_AMATCH) {
// Address match.
self->direction = i2c->I2CS.STATUS.bit.DIR;
machine_i2c_target_data_addr_match(data, self->direction);
// Send ACK
i2c->I2CS.CTRLB.bit.CMD = 3;
} else if (IRQ_DRDY) {
// Data to be handled, depending in the direction
if (self->direction == TRANSMIT) {
machine_i2c_target_data_read_request(self, data);
} else {
machine_i2c_target_data_write_request(self, data);
}
// ACK will be sent in mp_machine_i2c_target_read_bytes/mp_machine_i2c_target_write_bytes.
} else if (IRQ_STOP) {
// Stop detected. Just reset the data machine.
machine_i2c_target_data_stop(data);
i2c->I2CS.INTFLAG.reg |= SERCOM_I2CS_INTFLAG_PREC;
} else { // On any error clear the interrupts and reset the data state.
machine_i2c_target_data_stop(data);
i2c->I2CS.INTFLAG.reg = SERCOM_I2CS_INTFLAG_ERROR | SERCOM_I2CS_INTFLAG_AMATCH |
SERCOM_I2CS_INTFLAG_DRDY | SERCOM_I2CS_INTFLAG_PREC;
}
}
}
/******************************************************************************/
// I2CTarget port implementation
static inline size_t mp_machine_i2c_target_get_index(machine_i2c_target_obj_t *self) {
return self->id;
}
static void mp_machine_i2c_target_event_callback(machine_i2c_target_irq_obj_t *irq) {
mp_irq_handler(&irq->base);
}
static size_t mp_machine_i2c_target_read_bytes(machine_i2c_target_obj_t *self, size_t len, uint8_t *buf) {
Sercom *i2c = self->instance;
buf[0] = i2c->I2CS.DATA.reg;
i2c->I2CS.CTRLB.bit.CMD = 3; // send ACK
return 1;
}
static size_t mp_machine_i2c_target_write_bytes(machine_i2c_target_obj_t *self, size_t len, const uint8_t *buf) {
Sercom *i2c = self->instance;
i2c->I2CS.DATA.reg = buf[0];
i2c->I2CS.CTRLB.bit.CMD = 3; // send ACK
return 1;
}
static inline void mp_machine_i2c_target_irq_config(machine_i2c_target_obj_t *self, unsigned int trigger) {
(void)self;
(void)trigger;
}
mp_obj_t mp_machine_i2c_target_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
enum { ARG_id, ARG_addr, ARG_addrsize, ARG_mem, ARG_mem_addrsize, ARG_scl, ARG_sda };
static const mp_arg_t allowed_args[] = {
#if MICROPY_HW_DEFAULT_I2C_ID < 0
{ MP_QSTR_id, MP_ARG_REQUIRED | MP_ARG_INT, {.u_int = -1} },
#else
{ MP_QSTR_id, MP_ARG_INT, {.u_int = MICROPY_HW_DEFAULT_I2C_ID} },
#endif
{ MP_QSTR_addr, MP_ARG_REQUIRED | MP_ARG_INT },
{ MP_QSTR_addrsize, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 7} },
{ MP_QSTR_mem, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} },
{ MP_QSTR_mem_addrsize, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 8} },
#if defined(pin_SCL) && defined(pin_SDA)
{ MP_QSTR_scl, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = pin_SCL} },
{ MP_QSTR_sda, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = pin_SDA} },
#else
{ MP_QSTR_scl, MP_ARG_REQUIRED | MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} },
{ MP_QSTR_sda, MP_ARG_REQUIRED | MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} },
#endif
};
// Parse args.
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
// Get I2C bus.
int id = args[ARG_id].u_int;
if (id < 0 || id >= SERCOM_INST_NUM) {
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("I2C(%d) doesn't exist"), id);
}
// Get the peripheral object.
machine_i2c_target_obj_t *self = mp_obj_malloc_with_finaliser(machine_i2c_target_obj_t, &machine_i2c_target_type);
self->id = id;
self->instance = sercom_instance[id];
// Set SCL/SDA pins.
self->sda = pin_config_for_i2c(args[ARG_sda].u_obj, id, 0);
self->scl = pin_config_for_i2c(args[ARG_scl].u_obj, id, 1);
MP_STATE_PORT(sercom_table[id]) = self;
// Get the address and initialise data.
self->addr = args[ARG_addr].u_int;
MP_STATE_PORT(machine_i2c_target_mem_obj)[id] = args[ARG_mem].u_obj;
machine_i2c_target_data_t *data = &machine_i2c_target_data[id];
machine_i2c_target_data_init(data, args[ARG_mem].u_obj, args[ARG_mem_addrsize].u_int);
// Set up the clocks
enable_sercom_clock(id);
// Initialise the I2C peripheral
Sercom *i2c = self->instance;
// Reset the device
i2c->I2CS.CTRLA.reg = SERCOM_I2CM_CTRLA_SWRST;
while (i2c->I2CS.SYNCBUSY.bit.SWRST == 1) {
}
// Set to slave mode, enable SCl timeout, set the address
i2c->I2CS.CTRLA.reg = SERCOM_I2CS_CTRLA_MODE(0x04)
| SERCOM_I2CS_CTRLA_SEXTTOEN | SERCOM_I2CS_CTRLA_LOWTOUTEN;
i2c->I2CS.ADDR.reg = self->addr << 1;
// Enable interrupts
sercom_register_irq(id, &common_i2c_target_irq_handler);
#if defined(MCU_SAMD21)
NVIC_EnableIRQ(SERCOM0_IRQn + id);
#elif defined(MCU_SAMD51)
NVIC_EnableIRQ(SERCOM0_0_IRQn + 4 * id);
NVIC_EnableIRQ(SERCOM0_0_IRQn + 4 * id + 1);
NVIC_EnableIRQ(SERCOM0_0_IRQn + 4 * id + 2);
NVIC_EnableIRQ(SERCOM0_0_IRQn + 4 * id + 3);
#endif
i2c->I2CS.INTENSET.reg = SERCOM_I2CS_INTENSET_DRDY | SERCOM_I2CS_INTENSET_AMATCH |
SERCOM_I2CS_INTENSET_PREC | SERCOM_I2CS_INTENSET_ERROR;
// Now enable I2C.
sercom_enable(i2c, 1);
return MP_OBJ_FROM_PTR(self);
}
static void mp_machine_i2c_target_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
machine_i2c_target_obj_t *self = MP_OBJ_TO_PTR(self_in);
mp_printf(print, "I2C(%u, scl=\"%q\", sda=\"%q\", addr=%u)",
self->id, pin_find_by_id(self->scl)->name, pin_find_by_id(self->sda)->name,
self->addr);
}
// Stop the Slave transfer and free the memory objects.
static void mp_machine_i2c_target_deinit(machine_i2c_target_obj_t *self) {
// Disable I2C
sercom_enable(self->instance, 0);
MP_STATE_PORT(sercom_table[self->id]) = NULL;
}

View File

@@ -30,6 +30,7 @@
#include "py/mperrno.h"
#include "py/mphal.h"
#include "py/stackctrl.h"
#include "extmod/modmachine.h"
#include "shared/readline/readline.h"
#include "shared/runtime/gchelper.h"
#include "shared/runtime/pyexec.h"
@@ -101,7 +102,7 @@ void samd_main(void) {
mp_usbd_deinit();
#endif
gc_sweep_all();
#if MICROPY_PY_MACHINE_I2C || MICROPY_PY_MACHINE_SPI || MICROPY_PY_MACHINE_UART
#if MICROPY_PY_MACHINE_I2C || MICROPY_PY_MACHINE_I2C_TARGET || MICROPY_PY_MACHINE_SPI || MICROPY_PY_MACHINE_UART
sercom_deinit_all();
#endif
mp_deinit();

View File

@@ -78,6 +78,9 @@ unsigned long trng_random_u32(int delay);
#ifndef MICROPY_PY_ONEWIRE
#define MICROPY_PY_ONEWIRE (SAMD21_EXTRA_FEATURES)
#endif
#ifndef MICROPY_PY_MACHINE_I2C_TARGET
#define MICROPY_PY_MACHINE_I2C_TARGET (SAMD21_EXTRA_FEATURES)
#endif
#ifndef MICROPY_PY_MACHINE_PIN_BOARD_CPU
#define MICROPY_PY_MACHINE_PIN_BOARD_CPU (1)

View File

@@ -16,6 +16,9 @@
#define MICROPY_PY_RANDOM_SEED_INIT_FUNC (trng_random_u32())
unsigned long trng_random_u32(void);
#define MICROPY_PY_MACHINE_UART_IRQ (1)
#ifndef MICROPY_PY_MACHINE_I2C_TARGET
#define MICROPY_PY_MACHINE_I2C_TARGET (1)
#endif
// fatfs configuration used in ffconf.h
#define MICROPY_FATFS_ENABLE_LFN (1)

View File

@@ -127,6 +127,10 @@
#define MICROPY_PY_MACHINE_WDT_INCLUDEFILE "ports/samd/machine_wdt.c"
#define MICROPY_PY_MACHINE_WDT_TIMEOUT_MS (1)
#define MICROPY_PLATFORM_VERSION "ASF4"
#define MICROPY_PY_MACHINE_I2C_TARGET_INCLUDEFILE "ports/samd/machine_i2c_target.c"
#define MICROPY_PY_MACHINE_I2C_TARGET_MAX (SERCOM_INST_NUM)
#define MICROPY_PY_MACHINE_I2C_TARGET_HARD_IRQ (1)
#define MICROPY_PY_MACHINE_I2C_TARGET_FINALISER (1)
#define MP_STATE_PORT MP_STATE_VM

View File

@@ -161,3 +161,19 @@ pwm_config_t get_pwm_config(int pin_id, int wanted_dev, uint8_t device_status[])
}
#endif
#if MICROPY_PY_MACHINE_I2C || MICROPY_PY_MACHINE_I2C_TARGET
// Configure a I2C pin. Used by machine_i2c.c and machine_i2c_target.c.
uint8_t pin_config_for_i2c(mp_obj_t pin_obj, uint8_t id, uint8_t pad_nr) {
uint8_t pin = mp_hal_get_pin_obj(pin_obj);
sercom_pad_config_t pad_config = get_sercom_config(pin, id);
if (pad_config.pad_nr != pad_nr) {
mp_raise_ValueError(MP_ERROR_TEXT("invalid sda/scl pin"));
}
// Configure the Pin mux.
mp_hal_set_pin_mux(pin, pad_config.alt_fct);
return pin;
}
#endif

View File

@@ -100,3 +100,5 @@ adc_config_t get_adc_config(int pin_id, int32_t flag);
pwm_config_t get_pwm_config(int pin_id, int wanted_dev, uint8_t used_dev[]);
const machine_pin_obj_t *pin_find_by_id(int pin_id);
const machine_pin_obj_t *pin_find(mp_obj_t pin);
uint8_t pin_config_for_i2c(mp_obj_t pin_obj, uint8_t id, uint8_t pad_nr);

View File

@@ -122,7 +122,7 @@ void samd_init(void) {
machine_rtc_start(false);
}
#if MICROPY_PY_MACHINE_I2C || MICROPY_PY_MACHINE_SPI || MICROPY_PY_MACHINE_UART
#if MICROPY_PY_MACHINE_I2C || MICROPY_PY_MACHINE_I2C_TARGET || MICROPY_PY_MACHINE_SPI || MICROPY_PY_MACHINE_UART
Sercom *sercom_instance[] = SERCOM_INSTS;
MP_REGISTER_ROOT_POINTER(void *sercom_table[SERCOM_INST_NUM]);