Skip to content

Commit 71f3ade

Browse files
committed
ports: Support legacy soft I2C/SPI construction via id=-1 arg.
With a warning that this way of constructing software I2C/SPI is deprecated. The check and warning will be removed in a future release. This should help existing code to migrate to the new SoftI2C/SoftSPI types. Signed-off-by: Damien George <[email protected]>
1 parent 39d50d1 commit 71f3ade

File tree

9 files changed

+40
-0
lines changed

9 files changed

+40
-0
lines changed

extmod/machine_i2c.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,19 @@
2929
#include "py/obj.h"
3030
#include "py/mphal.h"
3131

32+
// Temporary support for legacy construction of SoftI2C via I2C type.
33+
#define MP_MACHINE_I2C_CHECK_FOR_LEGACY_SOFTI2C_CONSTRUCTION(n_args, n_kw, all_args) \
34+
do { \
35+
if (n_args == 0 || all_args[0] == MP_OBJ_NEW_SMALL_INT(-1)) { \
36+
mp_print_str(MICROPY_ERROR_PRINTER, "Warning: I2C(-1, ...) is deprecated, use SoftI2C(...) instead\n"); \
37+
if (n_args != 0) { \
38+
--n_args; \
39+
++all_args; \
40+
} \
41+
return mp_machine_soft_i2c_type.make_new(&mp_machine_soft_i2c_type, n_args, n_kw, all_args); \
42+
} \
43+
} while (0)
44+
3245
#define MP_MACHINE_I2C_FLAG_READ (0x01) // if not set then it's a write
3346
#define MP_MACHINE_I2C_FLAG_STOP (0x02)
3447

extmod/machine_spi.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,19 @@
3030
#include "py/mphal.h"
3131
#include "drivers/bus/spi.h"
3232

33+
// Temporary support for legacy construction of SoftSPI via SPI type.
34+
#define MP_MACHINE_SPI_CHECK_FOR_LEGACY_SOFTSPI_CONSTRUCTION(n_args, n_kw, all_args) \
35+
do { \
36+
if (n_args == 0 || all_args[0] == MP_OBJ_NEW_SMALL_INT(-1)) { \
37+
mp_print_str(MICROPY_ERROR_PRINTER, "Warning: SPI(-1, ...) is deprecated, use SoftSPI(...) instead\n"); \
38+
if (n_args != 0) { \
39+
--n_args; \
40+
++all_args; \
41+
} \
42+
return mp_machine_soft_spi_type.make_new(&mp_machine_soft_spi_type, n_args, n_kw, all_args); \
43+
} \
44+
} while (0)
45+
3346
// SPI protocol
3447
typedef struct _mp_machine_spi_p_t {
3548
void (*init)(mp_obj_base_t *obj, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args);

ports/esp32/machine_hw_spi.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,8 @@ STATIC void machine_hw_spi_init(mp_obj_base_t *self_in, size_t n_args, const mp_
351351
}
352352

353353
mp_obj_t machine_hw_spi_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
354+
MP_MACHINE_SPI_CHECK_FOR_LEGACY_SOFTSPI_CONSTRUCTION(n_args, n_kw, all_args);
355+
354356
enum { ARG_id, ARG_baudrate, ARG_polarity, ARG_phase, ARG_bits, ARG_firstbit, ARG_sck, ARG_mosi, ARG_miso };
355357
static const mp_arg_t allowed_args[] = {
356358
{ MP_QSTR_id, MP_ARG_REQUIRED | MP_ARG_INT, {.u_int = -1} },

ports/esp32/machine_i2c.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,8 @@ STATIC void machine_hw_i2c_print(const mp_print_t *print, mp_obj_t self_in, mp_p
115115
}
116116

117117
mp_obj_t machine_hw_i2c_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
118+
MP_MACHINE_I2C_CHECK_FOR_LEGACY_SOFTI2C_CONSTRUCTION(n_args, n_kw, all_args);
119+
118120
// Parse args
119121
enum { ARG_id, ARG_scl, ARG_sda, ARG_freq, ARG_timeout };
120122
static const mp_arg_t allowed_args[] = {

ports/esp8266/machine_hspi.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,8 @@ STATIC void machine_hspi_init(mp_obj_base_t *self_in, size_t n_args, const mp_ob
151151
}
152152

153153
mp_obj_t machine_hspi_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
154+
MP_MACHINE_SPI_CHECK_FOR_LEGACY_SOFTSPI_CONSTRUCTION(n_args, n_kw, args);
155+
154156
// args[0] holds the id of the peripheral
155157
if (args[0] != MP_OBJ_NEW_SMALL_INT(1)) {
156158
// FlashROM is on SPI0, so far we don't support its usage

ports/nrf/modules/machine/i2c.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ STATIC void machine_hard_i2c_print(const mp_print_t *print, mp_obj_t self_in, mp
9494
/* MicroPython bindings for machine API */
9595

9696
mp_obj_t machine_hard_i2c_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
97+
MP_MACHINE_I2C_CHECK_FOR_LEGACY_SOFTI2C_CONSTRUCTION(n_args, n_kw, all_args);
98+
9799
enum { ARG_id, ARG_scl, ARG_sda };
98100
static const mp_arg_t allowed_args[] = {
99101
{ MP_QSTR_id, MP_ARG_REQUIRED | MP_ARG_OBJ },

ports/stm32/machine_i2c.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,8 @@ STATIC void machine_hard_i2c_init(machine_hard_i2c_obj_t *self, uint32_t freq, u
193193
#endif
194194

195195
mp_obj_t machine_hard_i2c_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
196+
MP_MACHINE_I2C_CHECK_FOR_LEGACY_SOFTI2C_CONSTRUCTION(n_args, n_kw, all_args);
197+
196198
// parse args
197199
enum { ARG_id, ARG_scl, ARG_sda, ARG_freq, ARG_timeout, ARG_timingr };
198200
static const mp_arg_t allowed_args[] = {

ports/stm32/machine_spi.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ STATIC void machine_hard_spi_print(const mp_print_t *print, mp_obj_t self_in, mp
4646
}
4747

4848
mp_obj_t machine_hard_spi_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
49+
MP_MACHINE_SPI_CHECK_FOR_LEGACY_SOFTSPI_CONSTRUCTION(n_args, n_kw, all_args);
50+
4951
enum { ARG_id, ARG_baudrate, ARG_polarity, ARG_phase, ARG_bits, ARG_firstbit, ARG_sck, ARG_mosi, ARG_miso };
5052
static const mp_arg_t allowed_args[] = {
5153
{ MP_QSTR_id, MP_ARG_REQUIRED | MP_ARG_OBJ },

ports/zephyr/machine_i2c.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ STATIC void machine_hard_i2c_print(const mp_print_t *print, mp_obj_t self_in, mp
5151
}
5252

5353
mp_obj_t machine_hard_i2c_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
54+
MP_MACHINE_I2C_CHECK_FOR_LEGACY_SOFTI2C_CONSTRUCTION(n_args, n_kw, all_args);
55+
5456
enum { ARG_id, ARG_scl, ARG_sda, ARG_freq, ARG_timeout };
5557
static const mp_arg_t allowed_args[] = {
5658
{ MP_QSTR_id, MP_ARG_REQUIRED | MP_ARG_OBJ },

0 commit comments

Comments
 (0)