From afe0634c989d81f0c887308a7bc152061cb5d319 Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 2 Sep 2021 12:39:28 +1000 Subject: [PATCH] extmod/machine_spi: Make SoftSPI configurable via macro option. Signed-off-by: Damien George --- extmod/machine_spi.c | 11 +++++++---- ports/esp32/mpconfigport.h | 1 + ports/esp8266/mpconfigport.h | 1 + ports/mimxrt/mpconfigport.h | 1 + ports/rp2/mpconfigport.h | 1 + ports/stm32/mpconfigport.h | 1 + py/mpconfig.h | 5 +++++ 7 files changed, 17 insertions(+), 4 deletions(-) diff --git a/extmod/machine_spi.c b/extmod/machine_spi.c index c951a5137c..ae5e6677d2 100644 --- a/extmod/machine_spi.c +++ b/extmod/machine_spi.c @@ -30,8 +30,6 @@ #include "py/runtime.h" #include "extmod/machine_spi.h" -#if MICROPY_PY_MACHINE_SPI - // if a port didn't define MSB/LSB constants then provide them #ifndef MICROPY_PY_MACHINE_SPI_MSB #define MICROPY_PY_MACHINE_SPI_MSB (0) @@ -41,6 +39,8 @@ /******************************************************************************/ // MicroPython bindings for generic machine.SPI +#if MICROPY_PY_MACHINE_SPI || MICROPY_PY_MACHINE_SOFTSPI + STATIC mp_obj_t machine_spi_init(size_t n_args, const mp_obj_t *args, mp_map_t *kw_args) { mp_obj_base_t *s = (mp_obj_base_t *)MP_OBJ_TO_PTR(args[0]); mp_machine_spi_p_t *spi_p = (mp_machine_spi_p_t *)s->type->protocol; @@ -115,12 +115,15 @@ STATIC const mp_rom_map_elem_t machine_spi_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_MSB), MP_ROM_INT(MICROPY_PY_MACHINE_SPI_MSB) }, { MP_ROM_QSTR(MP_QSTR_LSB), MP_ROM_INT(MICROPY_PY_MACHINE_SPI_LSB) }, }; - MP_DEFINE_CONST_DICT(mp_machine_spi_locals_dict, machine_spi_locals_dict_table); +#endif // MICROPY_PY_MACHINE_SPI || MICROPY_PY_MACHINE_SOFTSPI + /******************************************************************************/ // Implementation of soft SPI +#if MICROPY_PY_MACHINE_SOFTSPI + STATIC uint32_t baudrate_from_delay_half(uint32_t delay_half) { #ifdef MICROPY_HW_SOFTSPI_MIN_DELAY if (delay_half == MICROPY_HW_SOFTSPI_MIN_DELAY) { @@ -258,4 +261,4 @@ const mp_obj_type_t mp_machine_soft_spi_type = { .locals_dict = (mp_obj_dict_t *)&mp_machine_spi_locals_dict, }; -#endif // MICROPY_PY_MACHINE_SPI +#endif // MICROPY_PY_MACHINE_SOFTSPI diff --git a/ports/esp32/mpconfigport.h b/ports/esp32/mpconfigport.h index b9e1337186..f8b26bfe84 100644 --- a/ports/esp32/mpconfigport.h +++ b/ports/esp32/mpconfigport.h @@ -163,6 +163,7 @@ #define MICROPY_PY_MACHINE_SPI (1) #define MICROPY_PY_MACHINE_SPI_MSB (0) #define MICROPY_PY_MACHINE_SPI_LSB (1) +#define MICROPY_PY_MACHINE_SOFTSPI (1) #ifndef MICROPY_PY_MACHINE_DAC #define MICROPY_PY_MACHINE_DAC (1) #endif diff --git a/ports/esp8266/mpconfigport.h b/ports/esp8266/mpconfigport.h index 4e234683b9..5364dd58b5 100644 --- a/ports/esp8266/mpconfigport.h +++ b/ports/esp8266/mpconfigport.h @@ -84,6 +84,7 @@ #define MICROPY_PY_MACHINE_I2C (1) #define MICROPY_PY_MACHINE_SOFTI2C (1) #define MICROPY_PY_MACHINE_SPI (1) +#define MICROPY_PY_MACHINE_SOFTSPI (1) #define MICROPY_PY_UWEBSOCKET (1) #define MICROPY_PY_WEBREPL (1) #define MICROPY_PY_WEBREPL_DELAY (20) diff --git a/ports/mimxrt/mpconfigport.h b/ports/mimxrt/mpconfigport.h index 76716f705a..88e1872cfc 100644 --- a/ports/mimxrt/mpconfigport.h +++ b/ports/mimxrt/mpconfigport.h @@ -128,6 +128,7 @@ uint32_t trng_random_u32(void); #define MICROPY_PY_MACHINE_I2C (1) #define MICROPY_PY_MACHINE_SOFTI2C (1) #define MICROPY_PY_MACHINE_SPI (1) +#define MICROPY_PY_MACHINE_SOFTSPI (1) #define MICROPY_PY_FRAMEBUF (1) // Use VfsLfs2's types for fileio/textio diff --git a/ports/rp2/mpconfigport.h b/ports/rp2/mpconfigport.h index 04a2e48161..ad3de396b0 100644 --- a/ports/rp2/mpconfigport.h +++ b/ports/rp2/mpconfigport.h @@ -136,6 +136,7 @@ #define MICROPY_PY_MACHINE_SPI (1) #define MICROPY_PY_MACHINE_SPI_MSB (SPI_MSB_FIRST) #define MICROPY_PY_MACHINE_SPI_LSB (SPI_LSB_FIRST) +#define MICROPY_PY_MACHINE_SOFTSPI (1) #define MICROPY_PY_FRAMEBUF (1) #define MICROPY_VFS (1) #define MICROPY_VFS_LFS2 (1) diff --git a/ports/stm32/mpconfigport.h b/ports/stm32/mpconfigport.h index e2863f192c..eff90cb25f 100644 --- a/ports/stm32/mpconfigport.h +++ b/ports/stm32/mpconfigport.h @@ -205,6 +205,7 @@ #define MICROPY_PY_MACHINE_SPI (1) #define MICROPY_PY_MACHINE_SPI_MSB (SPI_FIRSTBIT_MSB) #define MICROPY_PY_MACHINE_SPI_LSB (SPI_FIRSTBIT_LSB) +#define MICROPY_PY_MACHINE_SOFTSPI (1) #endif #define MICROPY_HW_SOFTSPI_MIN_DELAY (0) #define MICROPY_HW_SOFTSPI_MAX_BAUDRATE (HAL_RCC_GetSysClockFreq() / 48) diff --git a/py/mpconfig.h b/py/mpconfig.h index cc302c3cb7..288b105375 100644 --- a/py/mpconfig.h +++ b/py/mpconfig.h @@ -1494,6 +1494,11 @@ typedef double mp_float_t; #define MICROPY_PY_MACHINE_SPI (0) #endif +// Whether to provide the "machine.SoftSPI" class +#ifndef MICROPY_PY_MACHINE_SOFTSPI +#define MICROPY_PY_MACHINE_SOFTSPI (0) +#endif + #ifndef MICROPY_PY_USSL #define MICROPY_PY_USSL (0) // Whether to add finaliser code to ussl objects