From c73360bfdbecb0e2143147a7e7223c8161938456 Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 22 Dec 2017 15:20:42 +1100 Subject: [PATCH] stm32: Allow to build a board without any hardware I2C ports defined. This patch adds in internal config value MICROPY_HW_ENABLE_HW_I2C that is automatically configured, and enabled only if one or more hardware I2C ports are defined in the mpconfigboard.h file. If none are defined then the pyb.I2C class is excluded from the build, along with all supporting code. The machine.I2C class will still be available for software I2C. Disabling all hardware I2C on an F4 board saves around 10,000 bytes of code and 200 bytes of RAM. --- ports/stm32/i2c.c | 4 ++++ ports/stm32/machine_i2c.c | 4 ++++ ports/stm32/main.c | 3 +++ ports/stm32/modpyb.c | 2 ++ ports/stm32/mpconfigport.h | 12 +++++++++++- 5 files changed, 24 insertions(+), 1 deletion(-) diff --git a/ports/stm32/i2c.c b/ports/stm32/i2c.c index b22787cab5..e255cbc6b2 100644 --- a/ports/stm32/i2c.c +++ b/ports/stm32/i2c.c @@ -36,6 +36,8 @@ #include "dma.h" #include "i2c.h" +#if MICROPY_HW_ENABLE_HW_I2C + /// \moduleref pyb /// \class I2C - a two-wire serial protocol /// @@ -1033,3 +1035,5 @@ const mp_obj_type_t pyb_i2c_type = { .make_new = pyb_i2c_make_new, .locals_dict = (mp_obj_dict_t*)&pyb_i2c_locals_dict, }; + +#endif // MICROPY_HW_ENABLE_HW_I2C diff --git a/ports/stm32/machine_i2c.c b/ports/stm32/machine_i2c.c index 1be2151e3b..1018a9b1a5 100644 --- a/ports/stm32/machine_i2c.c +++ b/ports/stm32/machine_i2c.c @@ -34,6 +34,8 @@ #include "genhdr/pins.h" #include "i2c.h" +#if MICROPY_HW_ENABLE_HW_I2C + STATIC const mp_obj_type_t machine_hard_i2c_type; #if defined(MCU_SERIES_F4) @@ -548,3 +550,5 @@ STATIC const mp_obj_type_t machine_hard_i2c_type = { .protocol = &machine_hard_i2c_p, .locals_dict = (mp_obj_dict_t*)&mp_machine_soft_i2c_locals_dict, }; + +#endif // MICROPY_HW_ENABLE_HW_I2C diff --git a/ports/stm32/main.c b/ports/stm32/main.c index 9a83f9f363..352c09bcd4 100644 --- a/ports/stm32/main.c +++ b/ports/stm32/main.c @@ -556,7 +556,10 @@ soft_reset: rng_init0(); #endif + #if MICROPY_HW_ENABLE_HW_I2C i2c_init0(); + #endif + spi_init0(); pyb_usb_init0(); diff --git a/ports/stm32/modpyb.c b/ports/stm32/modpyb.c index 81cbdcc191..4d186e2787 100644 --- a/ports/stm32/modpyb.c +++ b/ports/stm32/modpyb.c @@ -203,7 +203,9 @@ STATIC const mp_rom_map_elem_t pyb_module_globals_table[] = { #if defined(MICROPY_HW_LED1) { MP_ROM_QSTR(MP_QSTR_LED), MP_ROM_PTR(&pyb_led_type) }, #endif + #if MICROPY_HW_ENABLE_HW_I2C { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&pyb_i2c_type) }, + #endif { MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&pyb_spi_type) }, { MP_ROM_QSTR(MP_QSTR_UART), MP_ROM_PTR(&pyb_uart_type) }, #if MICROPY_HW_ENABLE_CAN diff --git a/ports/stm32/mpconfigport.h b/ports/stm32/mpconfigport.h index 51d4425616..6fa286b262 100644 --- a/ports/stm32/mpconfigport.h +++ b/ports/stm32/mpconfigport.h @@ -131,7 +131,6 @@ #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_I2C_MAKE_NEW machine_hard_i2c_make_new #define MICROPY_PY_MACHINE_SPI (1) #define MICROPY_PY_MACHINE_SPI_MSB (SPI_FIRSTBIT_MSB) #define MICROPY_PY_MACHINE_SPI_LSB (SPI_FIRSTBIT_LSB) @@ -245,6 +244,17 @@ extern const struct _mp_obj_module_t mp_module_onewire; #define MICROPY_HW_MAX_UART (6) #endif +// enable hardware I2C if there are any peripherals defined +#define MICROPY_HW_ENABLE_HW_I2C ( \ + defined(MICROPY_HW_I2C1_SCL) \ + || defined(MICROPY_HW_I2C2_SCL) \ + || defined(MICROPY_HW_I2C3_SCL) \ + || defined(MICROPY_HW_I2C4_SCL) \ +) +#if MICROPY_HW_ENABLE_HW_I2C +#define MICROPY_PY_MACHINE_I2C_MAKE_NEW machine_hard_i2c_make_new +#endif + #define MP_STATE_PORT MP_STATE_VM #define MICROPY_PORT_ROOT_POINTERS \