diff options
author | Damien George <damien@micropython.org> | 2020-12-03 12:58:48 +1100 |
---|---|---|
committer | Damien George <damien@micropython.org> | 2020-12-07 17:21:36 +1100 |
commit | 4ce6427bd71129b1ab3b9eeaf2c1182d623cd7b9 (patch) | |
tree | 52f3191197cfb0b11ad0fd38f9287016518246e9 /ports/stm32/i2c.c | |
parent | 7dc2f4ed384f45eb943e0b7dc39a539af7058b80 (diff) |
stm32/i2c: Factor I2C finding code to i2c_find_peripheral function.
Signed-off-by: Damien George <damien@micropython.org>
Diffstat (limited to 'ports/stm32/i2c.c')
-rw-r--r-- | ports/stm32/i2c.c | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/ports/stm32/i2c.c b/ports/stm32/i2c.c index 5981df11c..d67201b71 100644 --- a/ports/stm32/i2c.c +++ b/ports/stm32/i2c.c @@ -26,6 +26,7 @@ #include "py/mperrno.h" #include "py/mphal.h" +#include "py/runtime.h" #include "i2c.h" #if MICROPY_HW_ENABLE_HW_I2C @@ -487,4 +488,53 @@ int i2c_writeto(i2c_t *i2c, uint16_t addr, const uint8_t *src, size_t len, bool #endif +STATIC const uint8_t i2c_available = + 0 + #if defined(MICROPY_HW_I2C1_SCL) + | 1 << 1 + #endif + #if defined(MICROPY_HW_I2C2_SCL) + | 1 << 2 + #endif + #if defined(MICROPY_HW_I2C3_SCL) + | 1 << 3 + #endif + #if defined(MICROPY_HW_I2C4_SCL) + | 1 << 4 + #endif +; + +int i2c_find_peripheral(mp_obj_t id) { + int i2c_id = 0; + if (mp_obj_is_str(id)) { + const char *port = mp_obj_str_get_str(id); + if (0) { + #ifdef MICROPY_HW_I2C1_NAME + } else if (strcmp(port, MICROPY_HW_I2C1_NAME) == 0) { + i2c_id = 1; + #endif + #ifdef MICROPY_HW_I2C2_NAME + } else if (strcmp(port, MICROPY_HW_I2C2_NAME) == 0) { + i2c_id = 2; + #endif + #ifdef MICROPY_HW_I2C3_NAME + } else if (strcmp(port, MICROPY_HW_I2C3_NAME) == 0) { + i2c_id = 3; + #endif + #ifdef MICROPY_HW_I2C4_NAME + } else if (strcmp(port, MICROPY_HW_I2C4_NAME) == 0) { + i2c_id = 4; + #endif + } else { + mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("I2C(%s) doesn't exist"), port); + } + } else { + i2c_id = mp_obj_get_int(id); + if (i2c_id < 1 || i2c_id >= 8 * sizeof(i2c_available) || !(i2c_available & (1 << i2c_id))) { + mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("I2C(%d) doesn't exist"), i2c_id); + } + } + return i2c_id; +} + #endif // MICROPY_HW_ENABLE_HW_I2C |