Skip to content

Commit 6fda662

Browse files
robert-hhdpgeorge
authored andcommitted
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: robert-hh <[email protected]>
1 parent af01fc8 commit 6fda662

File tree

9 files changed

+261
-26
lines changed

9 files changed

+261
-26
lines changed

ports/samd/boards/ADAFRUIT_NEOKEY_TRINKEY/mpconfigboard.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#define MICROPY_PY_MACHINE_SOFTI2C (0)
99
#define MICROPY_PY_MACHINE_SOFTSPI (0)
1010
#define MICROPY_PY_MACHINE_I2C (0)
11+
#define MICROPY_PY_MACHINE_I2C_TARGET_MEMORY (0)
1112
#define MICROPY_PY_MACHINE_SPI (0)
1213
#define MICROPY_PY_MACHINE_UART (0)
1314
#define MICROPY_PY_MACHINE_ADC (0)

ports/samd/machine_i2c.c

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,11 @@
2929

3030
#if MICROPY_PY_MACHINE_I2C
3131

32-
#include "py/mphal.h"
3332
#include "py/mperrno.h"
3433
#include "extmod/modmachine.h"
3534
#include "samd_soc.h"
3635
#include "pin_af.h"
3736
#include "genhdr/pins.h"
38-
#include "clock_config.h"
3937

4038
#define DEFAULT_I2C_FREQ (400000)
4139
#define RISETIME_NS (200)
@@ -72,16 +70,18 @@ typedef struct _machine_i2c_obj_t {
7270
uint8_t *buf;
7371
} machine_i2c_obj_t;
7472

73+
uint8_t machine_i2c_pin_config(mp_obj_t pin_obj, uint8_t id, uint8_t pad_nr);
74+
7575
static void i2c_send_command(Sercom *i2c, uint8_t command) {
7676
i2c->I2CM.CTRLB.bit.CMD = command;
7777
while (i2c->I2CM.SYNCBUSY.bit.SYSOP) {
7878
}
7979
}
8080

8181
void common_i2c_irq_handler(int i2c_id) {
82-
// handle Sercom I2C IRQ
82+
// Handle Sercom I2C IRQ for controller mode.
8383
machine_i2c_obj_t *self = MP_STATE_PORT(sercom_table[i2c_id]);
84-
// Handle IRQ
84+
8585
if (self != NULL) {
8686
Sercom *i2c = self->instance;
8787
// For now, clear all interrupts
@@ -114,7 +114,8 @@ void common_i2c_irq_handler(int i2c_id) {
114114
} else { // On any error, e.g. ARBLOST or BUSERROR, stop the transmission
115115
self->len = 0;
116116
self->state = state_buserr;
117-
i2c->I2CM.INTFLAG.reg |= SERCOM_I2CM_INTFLAG_ERROR;
117+
i2c->I2CM.INTFLAG.reg = SERCOM_I2CM_INTFLAG_ERROR |
118+
SERCOM_I2CM_INTFLAG_SB | SERCOM_I2CM_INTFLAG_MB;
118119
}
119120
}
120121
}
@@ -158,28 +159,19 @@ mp_obj_t machine_i2c_make_new(const mp_obj_type_t *type, size_t n_args, size_t n
158159
// Get the peripheral object.
159160
machine_i2c_obj_t *self = mp_obj_malloc(machine_i2c_obj_t, &machine_i2c_type);
160161
self->id = id;
161-
self->instance = sercom_instance[self->id];
162+
self->instance = sercom_instance[id];
162163

163164
// Set SCL/SDA pins.
164-
self->scl = mp_hal_get_pin_obj(args[ARG_scl].u_obj);
165-
self->sda = mp_hal_get_pin_obj(args[ARG_sda].u_obj);
165+
self->sda = machine_i2c_pin_config(args[ARG_sda].u_obj, id, 0);
166+
self->scl = machine_i2c_pin_config(args[ARG_scl].u_obj, id, 1);
167+
MP_STATE_PORT(sercom_table[id]) = self;
166168

167-
sercom_pad_config_t scl_pad_config = get_sercom_config(self->scl, self->id);
168-
sercom_pad_config_t sda_pad_config = get_sercom_config(self->sda, self->id);
169-
if (sda_pad_config.pad_nr != 0 || scl_pad_config.pad_nr != 1) {
170-
mp_raise_ValueError(MP_ERROR_TEXT("invalid sda/scl pin"));
171-
}
172-
MP_STATE_PORT(sercom_table[self->id]) = self;
173169
self->freq = args[ARG_freq].u_int;
174170
// The unit for ARG_timeout is us, but the code uses ms.
175171
self->timeout = args[ARG_timeout].u_int / 1000;
176172

177-
// Configure the Pin mux.
178-
mp_hal_set_pin_mux(self->scl, scl_pad_config.alt_fct);
179-
mp_hal_set_pin_mux(self->sda, sda_pad_config.alt_fct);
180-
181173
// Set up the clocks
182-
enable_sercom_clock(self->id);
174+
enable_sercom_clock(id);
183175

184176
// Initialise the I2C peripheral
185177
Sercom *i2c = self->instance;
@@ -207,13 +199,13 @@ mp_obj_t machine_i2c_make_new(const mp_obj_type_t *type, size_t n_args, size_t n
207199
i2c->I2CM.BAUD.reg = baud;
208200

209201
// Enable interrupts
210-
sercom_register_irq(self->id, &common_i2c_irq_handler);
202+
sercom_register_irq(id, &common_i2c_irq_handler);
211203
#if defined(MCU_SAMD21)
212-
NVIC_EnableIRQ(SERCOM0_IRQn + self->id);
204+
NVIC_EnableIRQ(SERCOM0_IRQn + id);
213205
#elif defined(MCU_SAMD51)
214-
NVIC_EnableIRQ(SERCOM0_0_IRQn + 4 * self->id); // MB interrupt
215-
NVIC_EnableIRQ(SERCOM0_0_IRQn + 4 * self->id + 1); // SB interrupt
216-
NVIC_EnableIRQ(SERCOM0_0_IRQn + 4 * self->id + 3); // ERROR interrupt
206+
NVIC_EnableIRQ(SERCOM0_0_IRQn + 4 * id); // MB interrupt
207+
NVIC_EnableIRQ(SERCOM0_0_IRQn + 4 * id + 1); // SB interrupt
208+
NVIC_EnableIRQ(SERCOM0_0_IRQn + 4 * id + 3); // ERROR interrupt
217209
#endif
218210

219211
// Now enable I2C.

ports/samd/machine_i2c_target.c

Lines changed: 215 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,215 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2020-2021 Damien P. George
7+
* Copyright (c) 2022-2025 Robert Hammelrath
8+
*
9+
* Permission is hereby granted, free of charge, to any person obtaining a copy
10+
* of this software and associated documentation files (the "Software"), to deal
11+
* in the Software without restriction, including without limitation the rights
12+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13+
* copies of the Software, and to permit persons to whom the Software is
14+
* furnished to do so, subject to the following conditions:
15+
*
16+
* The above copyright notice and this permission notice shall be included in
17+
* all copies or substantial portions of the Software.
18+
*
19+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25+
* THE SOFTWARE.
26+
*/
27+
28+
#include "py/runtime.h"
29+
#include "samd_soc.h"
30+
#include "pin_af.h"
31+
#include "genhdr/pins.h"
32+
33+
#define TRANSMIT (1)
34+
#define RECEIVE (0)
35+
#define NACK_RECVD (i2c->I2CS.STATUS.bit.RXNACK == 1)
36+
#define IRQ_AMATCH (i2c->I2CS.INTFLAG.bit.AMATCH == 1)
37+
#define IRQ_DRDY (i2c->I2CS.INTFLAG.bit.DRDY == 1)
38+
#define IRQ_STOP (i2c->I2CS.INTFLAG.bit.PREC == 1)
39+
40+
#define PREPARE_ACK i2c->I2CS.CTRLB.bit.ACKACT = 0
41+
#define PREPARE_NACK i2c->I2CS.CTRLB.bit.ACKACT = 1
42+
43+
typedef struct _machine_i2c_target_obj_t {
44+
mp_obj_base_t base;
45+
Sercom *instance;
46+
uint8_t id;
47+
uint8_t scl;
48+
uint8_t sda;
49+
uint8_t addr;
50+
uint8_t direction;
51+
} machine_i2c_target_obj_t;
52+
53+
uint8_t machine_i2c_pin_config(mp_obj_t pin_obj, uint8_t id, uint8_t pad_nr);
54+
55+
static machine_i2c_target_data_t i2c_target_data[SERCOM_INST_NUM];
56+
MP_REGISTER_ROOT_POINTER(mp_obj_t pyb_i2cslave_mem[SERCOM_INST_NUM]);
57+
58+
void common_i2c_target_irq_handler(int i2c_id) {
59+
// Handle Sercom I2C IRQ for target memory mode.
60+
machine_i2c_target_obj_t *self = MP_STATE_PORT(sercom_table[i2c_id]);
61+
machine_i2c_target_data_t *data = &i2c_target_data[i2c_id];
62+
63+
if (self != NULL) {
64+
Sercom *i2c = self->instance;
65+
66+
if (IRQ_AMATCH) {
67+
// Address match.
68+
self->direction = i2c->I2CS.STATUS.bit.DIR;
69+
machine_i2c_target_data_addr_match(data, !self->direction);
70+
// Send ACK
71+
i2c->I2CS.CTRLB.bit.CMD = 3;
72+
73+
} else if (IRQ_DRDY) {
74+
// Data to be handled, depending in the direction
75+
if (self->direction == TRANSMIT) {
76+
machine_i2c_target_data_read_request(self, data);
77+
} else {
78+
machine_i2c_target_data_write_request(self, data);
79+
}
80+
// Send ACK
81+
i2c->I2CS.CTRLB.bit.CMD = 3;
82+
} else if (IRQ_STOP) {
83+
// Stop detected. Just reset the data machine.
84+
machine_i2c_target_data_stop(data);
85+
i2c->I2CS.INTFLAG.reg |= SERCOM_I2CS_INTFLAG_PREC;
86+
87+
} else { // On any error clear the interrupts and reset the data state.
88+
machine_i2c_target_data_stop(data);
89+
i2c->I2CS.INTFLAG.reg = SERCOM_I2CS_INTFLAG_ERROR | SERCOM_I2CS_INTFLAG_AMATCH |
90+
SERCOM_I2CS_INTFLAG_DRDY | SERCOM_I2CS_INTFLAG_PREC;
91+
}
92+
}
93+
}
94+
95+
/******************************************************************************/
96+
// I2CTarget port implementation
97+
98+
static void mp_machine_i2c_target_deinit_all_port(void) {
99+
}
100+
101+
static void mp_machine_i2c_target_event_callback(machine_i2c_target_irq_obj_t *irq) {
102+
mp_irq_handler(&irq->base);
103+
}
104+
105+
static size_t mp_machine_i2c_target_read_bytes(machine_i2c_target_obj_t *self, size_t len, uint8_t *buf) {
106+
Sercom *i2c = self->instance;
107+
buf[0] = i2c->I2CS.DATA.reg;
108+
return 1;
109+
}
110+
111+
static size_t mp_machine_i2c_target_write_bytes(machine_i2c_target_obj_t *self, size_t len, const uint8_t *buf) {
112+
Sercom *i2c = self->instance;
113+
i2c->I2CS.DATA.reg = buf[0];
114+
return 1;
115+
}
116+
117+
static void mp_machine_i2c_target_irq_config(machine_i2c_target_obj_t *self, unsigned int trigger) {
118+
}
119+
120+
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) {
121+
enum { ARG_id, ARG_scl, ARG_sda, ARG_addr, ARG_addrsize, ARG_mem, ARG_mem_addrsize };
122+
static const mp_arg_t allowed_args[] = {
123+
#if MICROPY_HW_DEFAULT_I2C_ID < 0
124+
{ MP_QSTR_id, MP_ARG_REQUIRED | MP_ARG_INT, {.u_int = -1} },
125+
#else
126+
{ MP_QSTR_id, MP_ARG_INT, {.u_int = MICROPY_HW_DEFAULT_I2C_ID} },
127+
#endif
128+
#if defined(pin_SCL) && defined(pin_SDA)
129+
{ MP_QSTR_scl, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = pin_SCL} },
130+
{ MP_QSTR_sda, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = pin_SDA} },
131+
#else
132+
{ MP_QSTR_scl, MP_ARG_REQUIRED | MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} },
133+
{ MP_QSTR_sda, MP_ARG_REQUIRED | MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} },
134+
#endif
135+
{ MP_QSTR_addr, MP_ARG_REQUIRED | MP_ARG_INT },
136+
{ MP_QSTR_addrsize, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 7} },
137+
{ MP_QSTR_mem, MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} },
138+
{ MP_QSTR_mem_addrsize, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 8} },
139+
};
140+
141+
// Parse args.
142+
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
143+
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
144+
145+
// Get I2C bus.
146+
int id = args[ARG_id].u_int;
147+
if (id < 0 || id >= SERCOM_INST_NUM) {
148+
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("I2C(%d) doesn't exist"), id);
149+
}
150+
151+
// Get the peripheral object.
152+
machine_i2c_target_obj_t *self = mp_obj_malloc(machine_i2c_target_obj_t, &machine_i2c_target_type);
153+
self->id = id;
154+
self->instance = sercom_instance[id];
155+
156+
// Set SCL/SDA pins.
157+
self->sda = machine_i2c_pin_config(args[ARG_sda].u_obj, id, 0);
158+
self->scl = machine_i2c_pin_config(args[ARG_scl].u_obj, id, 1);
159+
160+
MP_STATE_PORT(sercom_table[id]) = self;
161+
162+
// Get the address and initialise data.
163+
self->addr = args[ARG_addr].u_int;
164+
MP_STATE_PORT(pyb_i2cslave_mem)[id] = args[ARG_mem].u_obj;
165+
machine_i2c_target_data_t *data = &i2c_target_data[id];
166+
machine_i2c_target_data_init(data, args[ARG_mem].u_obj, args[ARG_mem_addrsize].u_int);
167+
168+
// Set up the clocks
169+
enable_sercom_clock(id);
170+
171+
// Initialise the I2C peripheral
172+
Sercom *i2c = self->instance;
173+
// Reset the device
174+
i2c->I2CS.CTRLA.reg = SERCOM_I2CM_CTRLA_SWRST;
175+
while (i2c->I2CS.SYNCBUSY.bit.SWRST == 1) {
176+
}
177+
178+
// Set to slave mode, enable SCl timeout, set the address
179+
i2c->I2CS.CTRLA.reg = SERCOM_I2CS_CTRLA_MODE(0x04)
180+
| SERCOM_I2CS_CTRLA_SEXTTOEN | SERCOM_I2CS_CTRLA_LOWTOUTEN;
181+
i2c->I2CS.ADDR.reg = self->addr << 1;
182+
183+
// Enable interrupts
184+
sercom_register_irq(id, &common_i2c_target_irq_handler);
185+
#if defined(MCU_SAMD21)
186+
NVIC_EnableIRQ(SERCOM0_IRQn + id);
187+
#elif defined(MCU_SAMD51)
188+
NVIC_EnableIRQ(SERCOM0_0_IRQn + 4 * id);
189+
NVIC_EnableIRQ(SERCOM0_0_IRQn + 4 * id + 1);
190+
NVIC_EnableIRQ(SERCOM0_0_IRQn + 4 * id + 2);
191+
NVIC_EnableIRQ(SERCOM0_0_IRQn + 4 * id + 3);
192+
#endif
193+
i2c->I2CS.INTENSET.reg = SERCOM_I2CS_INTENSET_DRDY | SERCOM_I2CS_INTENSET_AMATCH |
194+
SERCOM_I2CS_INTENSET_PREC | SERCOM_I2CS_INTENSET_ERROR;
195+
196+
// Now enable I2C.
197+
sercom_enable(i2c, 1);
198+
199+
return MP_OBJ_FROM_PTR(self);
200+
}
201+
202+
static void mp_machine_i2c_target_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
203+
machine_i2c_target_obj_t *self = MP_OBJ_TO_PTR(self_in);
204+
mp_printf(print, "I2C(%u, scl=\"%q\", sda=\"%q\", addr=%u)",
205+
self->id, pin_find_by_id(self->scl)->name, pin_find_by_id(self->sda)->name,
206+
self->addr);
207+
}
208+
209+
// Stop the Slave transfer and free the memory objects.
210+
static void mp_machine_i2c_target_deinit(machine_i2c_target_obj_t *self) {
211+
// Disable I2C
212+
sercom_enable(self->instance, 0);
213+
MP_STATE_PORT(pyb_i2cslave_mem)[self->id] = NULL;
214+
MP_STATE_PORT(sercom_table[self->id]) = NULL;
215+
}

ports/samd/main.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "py/mperrno.h"
3131
#include "py/mphal.h"
3232
#include "py/stackctrl.h"
33+
#include "extmod/modmachine.h"
3334
#include "shared/readline/readline.h"
3435
#include "shared/runtime/gchelper.h"
3536
#include "shared/runtime/pyexec.h"
@@ -96,12 +97,15 @@ void samd_main(void) {
9697
#if MICROPY_PY_MACHINE_PWM
9798
pwm_deinit_all();
9899
#endif
100+
#if MICROPY_PY_MACHINE_I2C_TARGET
101+
mp_machine_i2c_target_deinit_all();
102+
#endif
99103
soft_timer_deinit();
100104
#if MICROPY_HW_ENABLE_USB_RUNTIME_DEVICE
101105
mp_usbd_deinit();
102106
#endif
103107
gc_sweep_all();
104-
#if MICROPY_PY_MACHINE_I2C || MICROPY_PY_MACHINE_SPI || MICROPY_PY_MACHINE_UART
108+
#if MICROPY_PY_MACHINE_I2C || MICROPY_PY_MACHINE_I2C_TARGET || MICROPY_PY_MACHINE_SPI || MICROPY_PY_MACHINE_UART
105109
sercom_deinit_all();
106110
#endif
107111
mp_deinit();

ports/samd/mcu/samd21/mpconfigmcu.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ unsigned long trng_random_u32(int delay);
7878
#ifndef MICROPY_PY_ONEWIRE
7979
#define MICROPY_PY_ONEWIRE (SAMD21_EXTRA_FEATURES)
8080
#endif
81+
#ifndef MICROPY_PY_MACHINE_I2C_TARGET
82+
#define MICROPY_PY_MACHINE_I2C_TARGET (SAMD21_EXTRA_FEATURES)
83+
#endif
8184

8285
#ifndef MICROPY_PY_MACHINE_PIN_BOARD_CPU
8386
#define MICROPY_PY_MACHINE_PIN_BOARD_CPU (1)

ports/samd/mcu/samd51/mpconfigmcu.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
#define MICROPY_PY_RANDOM_SEED_INIT_FUNC (trng_random_u32())
1717
unsigned long trng_random_u32(void);
1818
#define MICROPY_PY_MACHINE_UART_IRQ (1)
19+
#ifndef MICROPY_PY_MACHINE_I2C_TARGET
20+
#define MICROPY_PY_MACHINE_I2C_TARGET (1)
21+
#endif
1922

2023
// fatfs configuration used in ffconf.h
2124
#define MICROPY_FATFS_ENABLE_LFN (1)

ports/samd/mpconfigport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@
127127
#define MICROPY_PY_MACHINE_WDT_INCLUDEFILE "ports/samd/machine_wdt.c"
128128
#define MICROPY_PY_MACHINE_WDT_TIMEOUT_MS (1)
129129
#define MICROPY_PLATFORM_VERSION "ASF4"
130+
#define MICROPY_PY_MACHINE_I2C_TARGET_INCLUDEFILE "ports/samd/machine_i2c_target.c"
130131

131132
#define MP_STATE_PORT MP_STATE_VM
132133

ports/samd/pin_af.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,3 +161,19 @@ pwm_config_t get_pwm_config(int pin_id, int wanted_dev, uint8_t device_status[])
161161
}
162162

163163
#endif
164+
165+
#if MICROPY_PY_MACHINE_I2C || MICROPY_PY_MACHINE_I2C_TARGET_MEMORY
166+
167+
// Configure a I2C pin. Used by machine_i2c.c and machine_i2c_target.c.
168+
uint8_t machine_i2c_pin_config(mp_obj_t pin_obj, uint8_t id, uint8_t pad_nr) {
169+
uint8_t pin = mp_hal_get_pin_obj(pin_obj);
170+
sercom_pad_config_t pad_config = get_sercom_config(pin, id);
171+
if (pad_config.pad_nr != pad_nr) {
172+
mp_raise_ValueError(MP_ERROR_TEXT("invalid sda/scl pin"));
173+
}
174+
// Configure the Pin mux.
175+
mp_hal_set_pin_mux(pin, pad_config.alt_fct);
176+
return pin;
177+
}
178+
179+
#endif

0 commit comments

Comments
 (0)