From 122d901ef1b624c26771532cb4725eaa8f85e0f4 Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 2 Sep 2021 12:37:00 +1000 Subject: [PATCH] extmod/machine_i2c: Make SoftI2C configurable via macro option. The zephyr port doesn't support SoftI2C so it's not enabled, and the legacy I2C constructor check can be removed. Signed-off-by: Damien George --- extmod/machine_i2c.c | 12 ++++++++++-- ports/esp32/mpconfigport.h | 1 + ports/esp8266/mpconfigport.h | 1 + ports/mimxrt/mpconfigport.h | 1 + ports/nrf/mpconfigport.h | 1 + ports/rp2/mpconfigport.h | 1 + ports/stm32/mpconfigport.h | 1 + ports/zephyr/machine_i2c.c | 2 -- py/mpconfig.h | 5 +++++ 9 files changed, 21 insertions(+), 4 deletions(-) diff --git a/extmod/machine_i2c.c b/extmod/machine_i2c.c index 44161fbbb9..b2e39c534d 100644 --- a/extmod/machine_i2c.c +++ b/extmod/machine_i2c.c @@ -33,7 +33,7 @@ #include "py/runtime.h" #include "extmod/machine_i2c.h" -#if MICROPY_PY_MACHINE_I2C +#if MICROPY_PY_MACHINE_SOFTI2C typedef mp_machine_soft_i2c_obj_t machine_i2c_obj_t; @@ -240,9 +240,13 @@ int mp_machine_soft_i2c_transfer(mp_obj_base_t *self_in, uint16_t addr, size_t n return transfer_ret; } +#endif // MICROPY_PY_MACHINE_SOFTI2C + /******************************************************************************/ // Generic helper functions +#if MICROPY_PY_MACHINE_I2C || MICROPY_PY_MACHINE_SOFTI2C + // For use by ports that require a single buffer of data for a read/write transfer int mp_machine_i2c_transfer_adaptor(mp_obj_base_t *self, uint16_t addr, size_t n, mp_machine_i2c_buf_t *bufs, unsigned int flags) { size_t len; @@ -628,9 +632,13 @@ STATIC const mp_rom_map_elem_t machine_i2c_locals_dict_table[] = { }; MP_DEFINE_CONST_DICT(mp_machine_i2c_locals_dict, machine_i2c_locals_dict_table); +#endif // MICROPY_PY_MACHINE_I2C || MICROPY_PY_MACHINE_SOFTI2C + /******************************************************************************/ // Implementation of soft I2C +#if MICROPY_PY_MACHINE_SOFTI2C + STATIC void mp_machine_soft_i2c_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { mp_machine_soft_i2c_obj_t *self = MP_OBJ_TO_PTR(self_in); mp_printf(print, "SoftI2C(scl=" MP_HAL_PIN_FMT ", sda=" MP_HAL_PIN_FMT ", freq=%u)", @@ -711,4 +719,4 @@ const mp_obj_type_t mp_machine_soft_i2c_type = { .locals_dict = (mp_obj_dict_t *)&mp_machine_i2c_locals_dict, }; -#endif // MICROPY_PY_MACHINE_I2C +#endif // MICROPY_PY_MACHINE_SOFTI2C diff --git a/ports/esp32/mpconfigport.h b/ports/esp32/mpconfigport.h index 057251fa49..b9e1337186 100644 --- a/ports/esp32/mpconfigport.h +++ b/ports/esp32/mpconfigport.h @@ -159,6 +159,7 @@ #define MICROPY_PY_MACHINE_BITSTREAM (1) #define MICROPY_PY_MACHINE_PULSE (1) #define MICROPY_PY_MACHINE_I2C (1) +#define MICROPY_PY_MACHINE_SOFTI2C (1) #define MICROPY_PY_MACHINE_SPI (1) #define MICROPY_PY_MACHINE_SPI_MSB (0) #define MICROPY_PY_MACHINE_SPI_LSB (1) diff --git a/ports/esp8266/mpconfigport.h b/ports/esp8266/mpconfigport.h index c7e2f4a44b..4e234683b9 100644 --- a/ports/esp8266/mpconfigport.h +++ b/ports/esp8266/mpconfigport.h @@ -82,6 +82,7 @@ #define MICROPY_PY_MACHINE_BITSTREAM (1) #define MICROPY_PY_MACHINE_PULSE (1) #define MICROPY_PY_MACHINE_I2C (1) +#define MICROPY_PY_MACHINE_SOFTI2C (1) #define MICROPY_PY_MACHINE_SPI (1) #define MICROPY_PY_UWEBSOCKET (1) #define MICROPY_PY_WEBREPL (1) diff --git a/ports/mimxrt/mpconfigport.h b/ports/mimxrt/mpconfigport.h index 1066b93e16..76716f705a 100644 --- a/ports/mimxrt/mpconfigport.h +++ b/ports/mimxrt/mpconfigport.h @@ -126,6 +126,7 @@ uint32_t trng_random_u32(void); #define MICROPY_PY_MACHINE_PIN_MAKE_NEW mp_pin_make_new #define MICROPY_PY_MACHINE_PULSE (1) #define MICROPY_PY_MACHINE_I2C (1) +#define MICROPY_PY_MACHINE_SOFTI2C (1) #define MICROPY_PY_MACHINE_SPI (1) #define MICROPY_PY_FRAMEBUF (1) diff --git a/ports/nrf/mpconfigport.h b/ports/nrf/mpconfigport.h index 8a622d811c..6290f85e87 100644 --- a/ports/nrf/mpconfigport.h +++ b/ports/nrf/mpconfigport.h @@ -136,6 +136,7 @@ #define MICROPY_PY_UTIME_MP_HAL (1) #define MICROPY_PY_MACHINE (1) #define MICROPY_PY_MACHINE_PULSE (0) +#define MICROPY_PY_MACHINE_SOFTI2C (MICROPY_PY_MACHINE_I2C) #define MICROPY_PY_MACHINE_SPI (0) #define MICROPY_PY_MACHINE_SPI_MIN_DELAY (0) #define MICROPY_PY_FRAMEBUF (0) diff --git a/ports/rp2/mpconfigport.h b/ports/rp2/mpconfigport.h index 8d053ac1cd..04a2e48161 100644 --- a/ports/rp2/mpconfigport.h +++ b/ports/rp2/mpconfigport.h @@ -132,6 +132,7 @@ #define MICROPY_PY_MACHINE_PIN_MAKE_NEW mp_pin_make_new #define MICROPY_PY_MACHINE_PULSE (1) #define MICROPY_PY_MACHINE_I2C (1) +#define MICROPY_PY_MACHINE_SOFTI2C (1) #define MICROPY_PY_MACHINE_SPI (1) #define MICROPY_PY_MACHINE_SPI_MSB (SPI_MSB_FIRST) #define MICROPY_PY_MACHINE_SPI_LSB (SPI_LSB_FIRST) diff --git a/ports/stm32/mpconfigport.h b/ports/stm32/mpconfigport.h index 6f97377d9b..e2863f192c 100644 --- a/ports/stm32/mpconfigport.h +++ b/ports/stm32/mpconfigport.h @@ -201,6 +201,7 @@ #define MICROPY_PY_MACHINE_PULSE (1) #define MICROPY_PY_MACHINE_PIN_MAKE_NEW mp_pin_make_new #define MICROPY_PY_MACHINE_I2C (1) +#define MICROPY_PY_MACHINE_SOFTI2C (1) #define MICROPY_PY_MACHINE_SPI (1) #define MICROPY_PY_MACHINE_SPI_MSB (SPI_FIRSTBIT_MSB) #define MICROPY_PY_MACHINE_SPI_LSB (SPI_FIRSTBIT_LSB) diff --git a/ports/zephyr/machine_i2c.c b/ports/zephyr/machine_i2c.c index aa8823392d..810c3e5a9e 100644 --- a/ports/zephyr/machine_i2c.c +++ b/ports/zephyr/machine_i2c.c @@ -53,8 +53,6 @@ STATIC void machine_hard_i2c_print(const mp_print_t *print, mp_obj_t self_in, mp } 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) { - MP_MACHINE_I2C_CHECK_FOR_LEGACY_SOFTI2C_CONSTRUCTION(n_args, n_kw, all_args); - enum { ARG_id, ARG_scl, ARG_sda, ARG_freq, ARG_timeout }; static const mp_arg_t allowed_args[] = { { MP_QSTR_id, MP_ARG_REQUIRED | MP_ARG_OBJ }, diff --git a/py/mpconfig.h b/py/mpconfig.h index 71440da394..cc302c3cb7 100644 --- a/py/mpconfig.h +++ b/py/mpconfig.h @@ -1485,6 +1485,11 @@ typedef double mp_float_t; #define MICROPY_PY_MACHINE_I2C (0) #endif +// Whether to provide the "machine.SoftI2C" class +#ifndef MICROPY_PY_MACHINE_SOFTI2C +#define MICROPY_PY_MACHINE_SOFTI2C (0) +#endif + #ifndef MICROPY_PY_MACHINE_SPI #define MICROPY_PY_MACHINE_SPI (0) #endif