summaryrefslogtreecommitdiff
path: root/ports/stm32/i2c.c
diff options
context:
space:
mode:
Diffstat (limited to 'ports/stm32/i2c.c')
-rw-r--r--ports/stm32/i2c.c50
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