Skip to content

Commit 0e6f5e0

Browse files
committed
stmhal: Allow to name I2C busses, and give them names for pyboard.
1 parent 1775b65 commit 0e6f5e0

3 files changed

Lines changed: 41 additions & 13 deletions

File tree

stmhal/boards/PYBV10/mpconfigboard.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,11 @@
4646
#define MICROPY_HW_UART6_PORT (GPIOC)
4747
#define MICROPY_HW_UART6_PINS (GPIO_PIN_6 | GPIO_PIN_7)
4848

49-
// X-skin: X9=PB6=SCL, X10=PB7=SDA
49+
// I2C busses
50+
#define MICROPY_HW_I2C1_NAME "X"
5051
#define MICROPY_HW_I2C1_SCL (pin_B6)
5152
#define MICROPY_HW_I2C1_SDA (pin_B7)
52-
53-
// Y-skin: Y9=PB10=SCL, Y10=PB11=SDA
53+
#define MICROPY_HW_I2C2_NAME "Y"
5454
#define MICROPY_HW_I2C2_SCL (pin_B10)
5555
#define MICROPY_HW_I2C2_SDA (pin_B11)
5656

stmhal/boards/PYBV4/mpconfigboard.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,11 @@
4646
#define MICROPY_HW_UART6_PORT (GPIOC)
4747
#define MICROPY_HW_UART6_PINS (GPIO_PIN_6 | GPIO_PIN_7)
4848

49-
// X-skin: X9=PB6=SCL, X10=PB7=SDA
49+
// I2C busses
50+
#define MICROPY_HW_I2C1_NAME "X"
5051
#define MICROPY_HW_I2C1_SCL (pin_B6)
5152
#define MICROPY_HW_I2C1_SDA (pin_B7)
52-
53-
// Y-skin: Y9=PB10=SCL, Y10=PB11=SDA
53+
#define MICROPY_HW_I2C2_NAME "Y"
5454
#define MICROPY_HW_I2C2_SCL (pin_B10)
5555
#define MICROPY_HW_I2C2_SDA (pin_B11)
5656

stmhal/i2c.c

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -201,12 +201,18 @@ STATIC inline bool in_master_mode(pyb_i2c_obj_t *self) { return self->i2c->Init.
201201
STATIC const pyb_i2c_obj_t pyb_i2c_obj[] = {
202202
#if defined(MICROPY_HW_I2C1_SCL)
203203
{{&pyb_i2c_type}, &I2CHandle1},
204+
#else
205+
{{&pyb_i2c_type}, NULL},
204206
#endif
205207
#if defined(MICROPY_HW_I2C2_SCL)
206208
{{&pyb_i2c_type}, &I2CHandle2},
209+
#else
210+
{{&pyb_i2c_type}, NULL},
207211
#endif
208212
#if defined(MICROPY_HW_I2C3_SCL)
209213
{{&pyb_i2c_type}, &I2CHandle3},
214+
#else
215+
{{&pyb_i2c_type}, NULL},
210216
#endif
211217
};
212218

@@ -297,16 +303,38 @@ STATIC mp_obj_t pyb_i2c_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n
297303
// check arguments
298304
mp_arg_check_num(n_args, n_kw, 1, MP_OBJ_FUN_ARGS_MAX, true);
299305

300-
// get i2c number
301-
mp_int_t i2c_id = mp_obj_get_int(args[0]) - 1;
302-
303-
// check i2c number
304-
if (!(0 <= i2c_id && i2c_id < MP_ARRAY_SIZE(pyb_i2c_obj) && pyb_i2c_obj[i2c_id].i2c != NULL)) {
305-
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "I2C bus %d does not exist", i2c_id + 1));
306+
// work out i2c bus
307+
int i2c_id = 0;
308+
if (MP_OBJ_IS_STR(args[0])) {
309+
const char *port = mp_obj_str_get_str(args[0]);
310+
if (0) {
311+
#ifdef MICROPY_HW_I2C1_NAME
312+
} else if (strcmp(port, MICROPY_HW_I2C1_NAME) == 0) {
313+
i2c_id = 1;
314+
#endif
315+
#ifdef MICROPY_HW_I2C2_NAME
316+
} else if (strcmp(port, MICROPY_HW_I2C2_NAME) == 0) {
317+
i2c_id = 2;
318+
#endif
319+
#ifdef MICROPY_HW_I2C3_NAME
320+
} else if (strcmp(port, MICROPY_HW_I2C3_NAME) == 0) {
321+
i2c_id = 3;
322+
#endif
323+
} else {
324+
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
325+
"I2C(%s) does not exist", port));
326+
}
327+
} else {
328+
i2c_id = mp_obj_get_int(args[0]);
329+
if (i2c_id < 1 || i2c_id > MP_ARRAY_SIZE(pyb_i2c_obj)
330+
|| pyb_i2c_obj[i2c_id].i2c == NULL) {
331+
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
332+
"I2C(%d) does not exist", i2c_id));
333+
}
306334
}
307335

308336
// get I2C object
309-
const pyb_i2c_obj_t *i2c_obj = &pyb_i2c_obj[i2c_id];
337+
const pyb_i2c_obj_t *i2c_obj = &pyb_i2c_obj[i2c_id - 1];
310338

311339
if (n_args > 1 || n_kw > 0) {
312340
// start the peripheral

0 commit comments

Comments
 (0)