8000 samd/machine_i2c: Support default instance and SCL/SDA pin values. · sparkfun/micropython@213f1c1 · GitHub
[go: up one dir, main page]

Skip to content

Commit 213f1c1

Browse files
robert-hhdpgeorge
authored andcommitted
samd/machine_i2c: Support default instance and SCL/SDA pin values.
If a board configures a default I2C instance and/or SCL/SDA pins, then these no longer need to be given in the constructor. This allows the user to easily construct the default I2C instance via `machine.I2C()` and that will work on the default pins as designated on the board silkscreen. Signed-off-by: robert-hh <robert@hammelrath.com>
1 parent eb45d97 commit 213f1c1

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

ports/samd/machine_i2c.c

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include "extmod/modmachine.h"
3535
#include "samd_soc.h"
3636
#include "pin_af.h"
37+
#include "genhdr/pins.h"
3738
#include "clock_config.h"
3839

3940
#define DEFAULT_I2C_FREQ (400000)
@@ -119,25 +120,34 @@ void common_i2c_irq_handler(int i2c_id) {
119120

120121
static void machine_i2c_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
121122
machine_i2c_obj_t *self = MP_OBJ_TO_PTR(self_in);
122-
mp_printf(print, "I2C(%u, freq=%u, scl=%u, sda=%u)",
123-
self->id, self->freq, self->scl, self->sda);
123+
mp_printf(print, "I2C(%u, freq=%u, scl=\"%q\", sda=\"%q\")",
124+
self->id, self->freq, pin_find_by_id(self->scl)->name, pin_find_by_id(self->sda)->name);
124125
}
125126

126127
mp_obj_t machine_i2c_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
127128
enum { ARG_id, ARG_freq, ARG_scl, ARG_sda };
128129
static const mp_arg_t allowed_args[] = {
129-
{ MP_QSTR_id, MP_ARG_REQUIRED | MP_ARG_OBJ },
130+
#if MICROPY_HW_DEFAULT_I2C_ID < 0
131+
{ MP_QSTR_id, MP_ARG_REQUIRED | MP_ARG_INT, {.u_int = -1} },
132+
10000 #else
133+
{ MP_QSTR_id, MP_ARG_INT, {.u_int = MICROPY_HW_DEFAULT_I2C_ID} },
134+
#endif
130135
{ MP_QSTR_freq, MP_ARG_INT, {.u_int = DEFAULT_I2C_FREQ} },
136+
#if defined(pin_SCL) && defined(pin_SDA)
137+
{ MP_QSTR_scl, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = pin_SCL} },
138+
{ MP_QSTR_sda, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = pin_SDA} },
139+
#else
131140
{ MP_QSTR_scl, MP_ARG_REQUIRED | MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} },
132141
{ MP_QSTR_sda, MP_ARG_REQUIRED | MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} },
142+
#endif
133143
};
134144

135145
// Parse args.
136146
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
137147
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
138148

139149
// Get I2C bus.
140-
int id = mp_obj_get_int(args[ARG_id].u_obj);
150+
int id = args[ARG_id].u_int;
141151
if (id < 0 || id >= SERCOM_INST_NUM) {
142152
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("I2C(%d) doesn't exist"), id);
143153
}
@@ -148,15 +158,13 @@ mp_obj_t machine_i2c_make_new(const mp_obj_type_t *type, size_t n_args, size_t n
148158
self->instance = sercom_instance[self->id];
149159

150160
// Set SCL/SDA pins.
151-
sercom_pad_config_t scl_pad_config;
152161
self->scl = mp_hal_get_pin_obj(args[ARG_scl].u_obj);
153-
scl_pad_config = get_sercom_config(self->scl, self->id);
154-
155-
sercom_pad_config_t sda_pad_config;
156162
self->sda = mp_hal_get_pin_obj(args[ARG_sda].u_obj);
157-
sda_pad_config = get_sercom_config(self->sda, self->id);
163+
164+
sercom_pad_config_t scl_pad_config = get_sercom_config(self->scl, self->id);
165+
sercom_pad_config_t sda_pad_config = get_sercom_config(self->sda, self->id);
158166
if (sda_pad_config.pad_nr != 0 || scl_pad_config.pad_nr != 1) {
159-
mp_raise_ValueError(MP_ERROR_TEXT("invalid pin for sda or scl"));
167+
mp_raise_ValueError(MP_ERROR_TEXT("invalid sda/scl pin"));
160168
}
161169
MP_STATE_PORT(sercom_table[self->id]) = self;
162170
self->freq = args[ARG_freq].u_int;

0 commit comments

Comments
 (0)
0