Skip to content

Commit 70446f4

Browse files
committed
stmhal: Allow to name SPI busses, and give them names for pyboard.
1 parent 0e6f5e0 commit 70446f4

3 files changed

Lines changed: 37 additions & 8 deletions

File tree

stmhal/boards/PYBV10/mpconfigboard.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@
5454
#define MICROPY_HW_I2C2_SCL (pin_B10)
5555
#define MICROPY_HW_I2C2_SDA (pin_B11)
5656

57+
// SPI busses
58+
#define MICROPY_HW_SPI1_NAME "X"
59+
#define MICROPY_HW_SPI2_NAME "Y"
60+
5761
// CAN busses
5862
#define MICROPY_HW_CAN1_NAME "YA" // CAN1 on RX,TX = Y3,Y4 = PB8,PB9
5963
#define MICROPY_HW_CAN2_NAME "YB" // CAN2 on RX,TX = Y5,Y6 = PB12,PB13

stmhal/boards/PYBV4/mpconfigboard.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@
5454
#define MICROPY_HW_I2C2_SCL (pin_B10)
5555
#define MICROPY_HW_I2C2_SDA (pin_B11)
5656

57+
// SPI busses
58+
#define MICROPY_HW_SPI1_NAME "X"
59+
#define MICROPY_HW_SPI2_NAME "Y"
60+
5761
// CAN busses
5862
#define MICROPY_HW_CAN1_NAME "YA" // CAN1 on RX,TX = Y3,Y4 = PB8,PB9
5963
#define MICROPY_HW_CAN2_NAME "YB" // CAN2 on RX,TX = Y5,Y6 = PB12,PB13

stmhal/spi.c

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,6 @@ STATIC const pyb_spi_obj_t pyb_spi_obj[] = {
325325
{{&pyb_spi_type}, NULL},
326326
#endif
327327
};
328-
#define PYB_NUM_SPI MP_ARRAY_SIZE(pyb_spi_obj)
329328

330329
SPI_HandleTypeDef *spi_get_handle(mp_obj_t o) {
331330
if (!MP_OBJ_IS_TYPE(o, &pyb_spi_type)) {
@@ -462,16 +461,38 @@ STATIC mp_obj_t pyb_spi_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n
462461
// check arguments
463462
mp_arg_check_num(n_args, n_kw, 1, MP_OBJ_FUN_ARGS_MAX, true);
464463

465-
// get SPI number
466-
mp_int_t spi_id = mp_obj_get_int(args[0]) - 1;
467-
468-
// check SPI number
469-
if (!(0 <= spi_id && spi_id < PYB_NUM_SPI && pyb_spi_obj[spi_id].spi != NULL)) {
470-
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "SPI bus %d does not exist", spi_id + 1));
464+
// work out SPI bus
465+
int spi_id = 0;
466+
if (MP_OBJ_IS_STR(args[0])) {
467+
const char *port = mp_obj_str_get_str(args[0]);
468+
if (0) {
469+
#ifdef MICROPY_HW_SPI1_NAME
470+
} else if (strcmp(port, MICROPY_HW_SPI1_NAME) == 0) {
471+
spi_id = 1;
472+
#endif
473+
#ifdef MICROPY_HW_SPI2_NAME
474+
} else if (strcmp(port, MICROPY_HW_SPI2_NAME) == 0) {
475+
spi_id = 2;
476+
#endif
477+
#ifdef MICROPY_HW_SPI3_NAME
478+
} else if (strcmp(port, MICROPY_HW_SPI3_NAME) == 0) {
479+
spi_id = 3;
480+
#endif
481+
} else {
482+
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
483+
"SPI(%s) does not exist", port));
484+
}
485+
} else {
486+
spi_id = mp_obj_get_int(args[0]);
487+
if (spi_id < 1 || spi_id > MP_ARRAY_SIZE(pyb_spi_obj)
488+
|| pyb_spi_obj[spi_id].spi == NULL) {
489+
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
490+
"SPI(%d) does not exist", spi_id));
491+
}
471492
}
472493

473494
// get SPI object
474-
const pyb_spi_obj_t *spi_obj = &pyb_spi_obj[spi_id];
495+
const pyb_spi_obj_t *spi_obj = &pyb_spi_obj[spi_id - 1];
475496

476497
if (n_args > 1 || n_kw > 0) {
477498
// start the peripheral

0 commit comments

Comments
 (0)