Skip to content

Commit cdcc70d

Browse files
robert-hhdpgeorge
authored andcommitted
mimxrt: Enable default devices for I2C, SPI and UART.
Since all boards are configured to have a I2C(0), SPI(0) and UART(1), these can be set as default devices, allowing the instantiation of I2C(), SPI(), UART() without an id argument. Signed-off-by: robert-hh <[email protected]>
1 parent 1e7328c commit cdcc70d

File tree

4 files changed

+28
-8
lines changed

4 files changed

+28
-8
lines changed

docs/mimxrt/quickref.rst

+11-1
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,13 @@ See :ref:`machine.UART <machine.UART>`. ::
122122
uart1 = UART(1, baudrate=115200)
123123
uart1.write('hello') # write 5 bytes
124124
uart1.read(5) # read up to 5 bytes
125+
uart1 = UART(baudrate=19200) # open UART 1 at 19200 baud
125126

126127
The i.MXRT has up to eight hardware UARTs, but not every board exposes all
127128
TX and RX pins for users. For the assignment of Pins to UART signals,
128-
refer to the :ref:`UART pinout <mimxrt_uart_pinout>`.
129+
refer to the :ref:`UART pinout <mimxrt_uart_pinout>`. If the UART ID is
130+
omitted, UART(1) is selected. Then, the keyword
131+
option for baudrate must be used to change it from the default value.
129132

130133
PWM (pulse width modulation)
131134
----------------------------
@@ -305,12 +308,15 @@ rates (up to 30Mhz). Hardware SPI is accessed via the
305308
cs_pin(0)
306309
spi.write('Hello World')
307310
cs_pin(1)
311+
spi = SPI(baudrate=4_000_000) # Use SPI(0) at a baudrate of 4 MHz
308312

309313
For the assignment of Pins to SPI signals, refer to
310314
:ref:`Hardware SPI pinout <mimxrt_spi_pinout>`.
311315
The keyword option cs=n can be used to enable the cs pin 0 or 1 for an automatic cs signal. The
312316
default is cs=-1. Using cs=-1 the automatic cs signal is not created.
313317
In that case, cs has to be set by the script. Clearing that assignment requires a power cycle.
318+
If the SPI ID is omitted, SPI(0) is selected. Then, the keyword
319+
option for baudrate must be used to change it from the default value.
314320

315321
Notes:
316322

@@ -355,6 +361,10 @@ has the same methods as software SPI above::
355361

356362
i2c = I2C(0, 400_000)
357363
i2c.writeto(0x76, b"Hello World")
364+
i2c = I2C(freq=100_000) # use I2C(0) at 100kHz
365+
366+
If the I2C ID is omitted, I2C(0) is selected. Then, the keyword
367+
option for freq must be used to change the freq from the default value.
358368

359369
I2S bus
360370
-------

ports/mimxrt/machine_i2c.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include "fsl_iomuxc.h"
3535
#include "fsl_lpi2c.h"
3636

37+
#define DEFAULT_I2C_ID (0)
3738
#define DEFAULT_I2C_FREQ (400000)
3839
#define DEFAULT_I2C_DRIVE (6)
3940
#define DEFAULT_I2C_TIMEOUT (50000)
@@ -91,7 +92,7 @@ static void machine_i2c_print(const mp_print_t *print, mp_obj_t self_in, mp_prin
9192
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) {
9293
enum { ARG_id, ARG_freq, ARG_drive, ARG_timeout};
9394
static const mp_arg_t allowed_args[] = {
94-
{ MP_QSTR_id, MP_ARG_REQUIRED | MP_ARG_OBJ },
95+
{ MP_QSTR_id, MP_ARG_INT, {.u_int = DEFAULT_I2C_ID} },
9596
{ MP_QSTR_freq, MP_ARG_INT, {.u_int = DEFAULT_I2C_FREQ} },
9697
{ MP_QSTR_drive, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = DEFAULT_I2C_DRIVE} },
9798
{ MP_QSTR_timeout, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = DEFAULT_I2C_TIMEOUT} },
@@ -102,7 +103,7 @@ mp_obj_t machine_i2c_make_new(const mp_obj_type_t *type, size_t n_args, size_t n
102103
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
103104

104105
// Get I2C bus.
105-
int i2c_id = mp_obj_get_int(args[ARG_id].u_obj);
106+
int i2c_id = args[ARG_id].u_int;
106107
if (i2c_id < 0 || i2c_id >= MICROPY_HW_I2C_NUM || i2c_index_table[i2c_id] == 0) {
107108
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("I2C(%d) doesn't exist"), i2c_id);
108109
}

ports/mimxrt/machine_spi.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#include "fsl_lpspi.h"
3838
#include "fsl_lpspi_edma.h"
3939

40+
#define DEFAULT_SPI_ID (0)
4041
#define DEFAULT_SPI_BAUDRATE (1000000)
4142
#define DEFAULT_SPI_POLARITY (0)
4243
#define DEFAULT_SPI_PHASE (0)
@@ -130,7 +131,7 @@ static void machine_spi_print(const mp_print_t *print, mp_obj_t self_in, mp_prin
130131
mp_obj_t machine_spi_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
131132
enum { ARG_id, ARG_baudrate, ARG_polarity, ARG_phase, ARG_bits, ARG_firstbit, ARG_gap_ns, ARG_drive, ARG_cs };
132133
static const mp_arg_t allowed_args[] = {
133-
{ MP_QSTR_id, MP_ARG_REQUIRED | MP_ARG_OBJ },
134+
{ MP_QSTR_id, MP_ARG_INT, {.u_int = DEFAULT_SPI_ID} },
134135
{ MP_QSTR_baudrate, MP_ARG_INT, {.u_int = DEFAULT_SPI_BAUDRATE} },
135136
{ MP_QSTR_polarity, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = DEFAULT_SPI_POLARITY} },
136137
{ MP_QSTR_phase, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = DEFAULT_SPI_PHASE} },
@@ -146,7 +147,7 @@ mp_obj_t machine_spi_make_new(const mp_obj_type_t *type, size_t n_args, size_t n
146147
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
147148

148149
// Get the SPI bus id.
149-
int spi_id = mp_obj_get_int(args[ARG_id].u_obj);
150+
int spi_id = args[ARG_id].u_int;
150151
if (spi_id < 0 || spi_id >= MP_ARRAY_SIZE(spi_index_table) || spi_index_table[spi_id] == 0) {
151152
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("SPI(%d) doesn't exist"), spi_id);
152153
}

ports/mimxrt/machine_uart.c

+11-3
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#include "modmachine.h"
3838
#include "pin.h"
3939

40+
#define DEFAULT_UART_ID (1)
4041
#define DEFAULT_UART_BAUDRATE (115200)
4142
#define DEFAULT_BUFFER_SIZE (256)
4243
#define MIN_BUFFER_SIZE (32)
@@ -377,10 +378,17 @@ static void mp_machine_uart_init_helper(machine_uart_obj_t *self, size_t n_args,
377378
}
378379

379380
static mp_obj_t mp_machine_uart_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
380-
mp_arg_check_num(n_args, n_kw, 1, MP_OBJ_FUN_ARGS_MAX, true);
381+
mp_arg_check_num(n_args, n_kw, 0, MP_OBJ_FUN_ARGS_MAX, true);
381382

382383
// Get UART bus.
383-
int uart_id = mp_obj_get_int(args[0]);
384+
int uart_id;
385+
if (n_args > 0) {
386+
uart_id = mp_obj_get_int(args[0]);
387+
n_args--;
388+
args++;
389+
} else {
390+
uart_id = DEFAULT_UART_ID;
391+
}
384392
if (uart_id < 0 || uart_id > MICROPY_HW_UART_NUM || uart_index_table[uart_id] == 0) {
385393
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("UART(%d) doesn't exist"), uart_id);
386394
}
@@ -409,7 +417,7 @@ static mp_obj_t mp_machine_uart_make_new(const mp_obj_type_t *type, size_t n_arg
409417
if (uart_present) {
410418
mp_map_t kw_args;
411419
mp_map_init_fixed_table(&kw_args, n_kw, args + n_args);
412-
mp_machine_uart_init_helper(self, n_args - 1, args + 1, &kw_args);
420+
mp_machine_uart_init_helper(self, n_args, args, &kw_args);
413421
return MP_OBJ_FROM_PTR(self);
414422
} else {
415423
return mp_const_none;

0 commit comments

Comments
 (0)