8000 stm32: Allow to build a board without any hardware I2C ports defined. · neverhover/micropython@c73360b · GitHub
[go: up one dir, main page]

Skip to content

Commit c73360b

Browse files
committed
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.
1 parent 82dc5c1 commit c73360b

File tree

5 files changed

+24
-1
lines changed

5 files changed

+24
-1
lines changed

ports/stm32/i2c.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
#include "dma.h"
3737
#include "i2c.h"
3838

39+
#if MICROPY_HW_ENABLE_HW_I2C
40+
3941
/// \moduleref pyb
4042
/// \class I2C - a two-wire serial protocol
4143
///
@@ -1033,3 +1035,5 @@ const mp_obj_type_t pyb_i2c_type = {
10331035
.make_new = pyb_i2c_make_new,
10341036
.locals_dict = (mp_obj_dict_t*)&pyb_i2c_locals_dict,
10351037
};
1038+
1039+
#endif // MICROPY_HW_ENABLE_HW_I2C

ports/stm32/machine_i2c.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
#include "genhdr/pins.h"
3535
#include "i2c.h"
3636

37+
#if MICROPY_HW_ENABLE_HW_I2C
38+
3739
STATIC const mp_obj_type_t machine_hard_i2c_type;
3840

3941
#if defined(MCU_SERIES_F4)
@@ -548,3 +550,5 @@ STATIC const mp_obj_type_t machine_hard_i2c_type = {
548550
.protocol = &machine_hard_i2c_p,
549551
.locals_dict = (mp_obj_dict_t*)&mp_machine_soft_i2c_locals_dict,
550552
};
553+
554+
#endif // MICROPY_HW_ENABLE_HW_I2C

ports/stm32/main.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,10 @@ int main(void) {
556556
rng_init0();
557557
#endif
558558

559+
#if MICROPY_HW_ENABLE_HW_I2C
559560
i2c_init0();
561+
#endif
562+
560563
spi_init0();
561564
pyb_usb_init0();
562565

ports/stm32/modpyb.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,9 @@ STATIC const mp_rom_map_elem_t pyb_module_globals_table[] = {
203203
#if defined(MICROPY_HW_LED1)
204204
{ MP_ROM_QSTR(MP_QSTR_LED), MP_ROM_PTR(&pyb_led_type) },
205205
#endif
206+
#if MICROPY_HW_ENABLE_HW_I2C
206207
{ MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&pyb_i2c_type) },
208+
#endif
207209
{ MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&pyb_spi_type) },
208210
{ MP_ROM_QSTR(MP_QSTR_UART), MP_ROM_PTR(&pyb_uart_type) },
209211
#if MICROPY_HW_ENABLE_CAN

ports/stm32/mpconfigport.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,6 @@
131131
#define MICROPY_PY_MACHINE_PULSE (1)
132132
#define MICROPY_PY_MACHINE_PIN_MAKE_NEW mp_pin_make_new
133133
#define MICROPY_PY_MACHINE_I2C (1)
134-
#define MICROPY_PY_MACHINE_I2C_MAKE_NEW machine_hard_i2c_make_new
135134
#define MICROPY_PY_MACHINE_SPI (1)
136135
#define MICROPY_PY_MACHINE_SPI_MSB (SPI_FIRSTBIT_MSB)
137136
#define MICROPY_PY_MACHINE_SPI_LSB (SPI_FIRSTBIT_LSB)
@@ -245,6 +244,17 @@ extern const struct _mp_obj_module_t mp_module_onewire;
245244
#define MICROPY_HW_MAX_UART (6)
246245
#endif
247246

247+
// enable hardware I2C if there are any peripherals defined
248+
#define MICROPY_HW_ENABLE_HW_I2C ( \
249+
defined(MICROPY_HW_I2C1_SCL) \
250+
|| defined(MICROPY_HW_I2C2_SCL) \
251+
|| defined(MICROPY_HW_I2C3_SCL) \
252+
|| defined(MICROPY_HW_I2C4_SCL) \
253+
)
254+
#if MICROPY_HW_ENABLE_HW_I2C
255+
#define MICROPY_PY_MACHINE_I2C_MAKE_NEW machine_hard_i2c_make_new
256+
#endif
257+
248258
#define MP_STATE_PORT MP_STATE_VM
249259

250260
#define MICROPY_PORT_ROOT_POINTERS \

0 commit comments

Comments
 (0)
0