8000 samd/machine_xyz.c: Rework the I2C, SPI, UART Pin arg handling. · micropython/micropython@8c21b89 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8c21b89

Browse files
committed
samd/machine_xyz.c: Rework the I2C, SPI, UART Pin arg handling.
- Simplify the I2C Pin check. - For UART and SPI move setting of the default Pins to the constructor. This is needed since uart.init() and spi.init() must not change the Pin assignment if not specified, but must change it if told so. Signed-off-by: robert-hh <robert@hammelrath.com>
1 parent 2c014f1 commit 8c21b89

File tree

3 files changed

+20
-25
lines changed

3 files changed

+20
-25
lines changed

ports/samd/machine_i2c.c

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -156,19 +156,11 @@ mp_obj_t machine_i2c_make_new(const mp_obj_type_t *type, size_t n_args, size_t n
156156
machine_i2c_obj_t *self = mp_obj_malloc(machine_i2c_obj_t, &machine_i2c_type);
157157
self->id = id;
158158
self->instance = sercom_instance[self->id];
159-
self->scl = 0xff;
160-
self->sda = 0xff;
161159

162160
// Set SCL/SDA pins.
163-
if (args[ARG_scl].u_obj != MP_ROM_NONE) {
164-
self->scl = mp_hal_get_pin_obj(args[ARG_scl].u_obj);
165-
}
166-
if (args[ARG_sda].u_obj != MP_ROM_NONE) {
167-
self->sda = mp_hal_get_pin_obj(args[ARG_sda].u_obj);
168-
}
169-
if (self->scl == 0xff || self->sda == 0xff) {
170-
mp_raise_ValueError(MP_ERROR_TEXT("missing scl/sda"));
171-
}
161+
self->scl = mp_hal_get_pin_obj(args[ARG_scl].u_obj);
162+
self->sda = mp_hal_get_pin_obj(args[ARG_sda].u_obj);
163+
172164
sercom_pad_config_t scl_pad_config = get_sercom_config(self->scl, self->id);
173165
sercom_pad_config_t sda_pad_config = get_sercom_config(self->sda, self->id);
174166
if (sda_pad_config.pad_nr != 0 || scl_pad_config.pad_nr != 1) {

ports/samd/machine_spi.c

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -102,15 +102,9 @@ static void machine_spi_init(mp_obj_base_t *self_in, size_t n_args, const mp_obj
102102
{ MP_QSTR_polarity, MP_ARG_INT, {.u_int = -1} },
103103
{ MP_QSTR_phase, MP_ARG_INT, {.u_int = -1} },
104104
{ MP_QSTR_firstbit, MP_ARG_INT, {.u_int = -1} },
105-
#if defined(pin_SCK) && defined(pin_MOSI) && defined(pin_MISO)
106-
{ MP_QSTR_sck, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = pin_SCK} },
107-
{ MP_QSTR_mosi, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = pin_MOSI} },
108-
{ MP_QSTR_miso, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = pin_MISO} },
109-
#else
110105
{ MP_QSTR_sck, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} },
111106
{ MP_QSTR_mosi, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} },
112-
{ MP_QSTR_miso, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} },
113-
#endif
107+
{ MP_QSTR_miso, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_INT(-1)} },
114108
};
115109

116110
machine_spi_obj_t *self = MP_OBJ_TO_PTR(self_in);
@@ -146,8 +140,8 @@ static void machine_spi_init(mp_obj_base_t *self_in, size_t n_args, const mp_obj
146140
if (args[ARG_mosi].u_obj != mp_const_none) {
147141
self->mosi = mp_hal_get_pin_obj(args[ARG_mosi].u_obj);
148142
}
149-
if (args[ARG_miso].u_obj != mp_const_none) {
150-
self->miso = mp_hal_get_pin_obj(args[ARG_miso].u_obj);
143+
if (args[ARG_miso].u_obj != MP_ROM_INT(-1)) {
144+
self->miso = args[ARG_miso].u_obj == mp_const_none ? 0xff : mp_hal_get_pin_obj(args[ARG_miso].u_obj);
151145
}
152146
// Initialise the SPI peripheral if any arguments given, or it was not initialised previously.
153147
if (n_args > 0 || kw_args->used > 0 || self->new) {
@@ -269,9 +263,16 @@ static mp_obj_t machine_spi_make_new(const mp_obj_type_t *type, size_t n_args, s
269263
self->polarity = DEFAULT_SPI_POLARITY;
270264
self->phase = DEFAULT_SPI_PHASE;
271265
self->firstbit = DEFAULT_SPI_FIRSTBIT;
266+
#if defined(pin_SCK) && defined(pin_MOSI) && defined(pin_MISO)
267+
// Initialize with the default pins
268+
self->sck = mp_hal_get_pin_obj((mp_obj_t)pin_SCK);
269+
self->mosi = mp_hal_get_pin_obj((mp_obj_t)pin_MOSI);
270+
self->miso = mp_hal_get_pin_obj((mp_obj_t)pin_MISO);
271+
#else
272272
self->mosi = 0xff; // 0xff: pin not defined (yet)
273273
self->miso = 0xff;
274274
self->sck = 0xff;
275+
#endif
275276

276277
self->new = true;
277278
MP_STATE_PORT(sercom_table[spi_id]) = self;

ports/samd/machine_uart.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -334,13 +334,8 @@ static void mp_machine_uart_init_helper(machine_uart_obj_t *self, size_t n_args,
334334
{ MP_QSTR_bits, MP_ARG_INT, {.u_int = -1} },
335335
{ MP_QSTR_parity, MP_ARG_OBJ, {.u_rom_obj = MP_ROM_INT(-1)} },
336336
{ MP_QSTR_stop, MP_ARG_INT, {.u_int = -1} },
337-
#if defined(pin_TX) && defined(pin_RX)
338-
{ MP_QSTR_tx, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = pin_TX} },
339-
{ MP_QSTR_rx, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = pin_RX} },
340-
#else
341337
{ MP_QSTR_tx, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} },
342338
{ MP_QSTR_rx, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} },
343-
#endif
344339
{ MP_QSTR_timeout, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} },
345340
{ MP_QSTR_timeout_char, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} },
346341
{ MP_QSTR_rxbuf, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} },
@@ -508,8 +503,15 @@ static mp_obj_t mp_machine_uart_make_new(const mp_obj_type_t *type, size_t n_arg
508503
self->stop = 0;
509504
self->timeout = 1;
510505
self->timeout_char = 1;
506+
#if defined(pin_TX) && defined(pin_RX)
507+
// Initialize with the default pins
508+
self->tx = mp_hal_get_pin_obj((mp_obj_t)pin_TX);
509+
self->rx = mp_hal_get_pin_obj((mp_obj_t)pin_RX);
510+
#else
511+
// Have to be defined
511512
self->tx = 0xff;
512513
self->rx = 0xff;
514+
#endif
513515
#if MICROPY_HW_UART_RTSCTS
514516
self->rts = 0xff;
515517
self->cts = 0xff;

0 commit comments

Comments
 (0)
0