diff options
| author | Scott Shawcroft <scott@tannewt.org> | 2019-04-05 19:06:37 -0700 |
|---|---|---|
| committer | Scott Shawcroft <scott@tannewt.org> | 2019-04-05 19:06:37 -0700 |
| commit | 7686f93ef456aa46f538da3159d0e12cae9fa737 (patch) | |
| tree | bdd9d98d82e09d9ea725284e9bb2eee2d71c2f41 /supervisor/shared/board_busses.c | |
| parent | 8b9e93329d5b9c732954f0d8f1ba2c97819ad774 (diff) | |
Fix crash when getting board.SPI outside the VM
If one of the default pins was already in use it would crash.
The internal API has been refined to allow us to get the value
without causing an init of the singleton.
Fixes #1753
Diffstat (limited to 'supervisor/shared/board_busses.c')
| -rw-r--r-- | supervisor/shared/board_busses.c | 45 |
1 files changed, 32 insertions, 13 deletions
diff --git a/supervisor/shared/board_busses.c b/supervisor/shared/board_busses.c index d77c4f331..b2d04207d 100644 --- a/supervisor/shared/board_busses.c +++ b/supervisor/shared/board_busses.c @@ -71,23 +71,42 @@ MP_DEFINE_CONST_FUN_OBJ_0(board_i2c_obj, board_i2c); STATIC busio_spi_obj_t spi_obj; STATIC mp_obj_t spi_singleton = NULL; -mp_obj_t board_spi(void) { - if (spi_singleton == NULL) { - busio_spi_obj_t *self = &spi_obj; - self->base.type = &busio_spi_type; - assert_pin_free(DEFAULT_SPI_BUS_SCK); - assert_pin_free(DEFAULT_SPI_BUS_MOSI); - assert_pin_free(DEFAULT_SPI_BUS_MISO); - const mcu_pin_obj_t* clock = MP_OBJ_TO_PTR(DEFAULT_SPI_BUS_SCK); - const mcu_pin_obj_t* mosi = MP_OBJ_TO_PTR(DEFAULT_SPI_BUS_MOSI); - const mcu_pin_obj_t* miso = MP_OBJ_TO_PTR(DEFAULT_SPI_BUS_MISO); - common_hal_busio_spi_construct(self, clock, mosi, miso); - spi_singleton = (mp_obj_t)self; +// TODO(tannewt): Move this to shared-bindings/board/__init__.c and corresponding shared-module. +mp_obj_t common_hal_board_get_spi(void) { + return spi_singleton; +} + +mp_obj_t common_hal_board_create_spi(void) { + if (spi_singleton != NULL) { + return spi_singleton; + } + busio_spi_obj_t *self = &spi_obj; + self->base.type = &busio_spi_type; + if (!common_hal_mcu_pin_is_free(DEFAULT_SPI_BUS_SCK) || + !common_hal_mcu_pin_is_free(DEFAULT_SPI_BUS_MOSI) || + !common_hal_mcu_pin_is_free(DEFAULT_SPI_BUS_MISO)) { + return NULL; } + const mcu_pin_obj_t* clock = MP_OBJ_TO_PTR(DEFAULT_SPI_BUS_SCK); + const mcu_pin_obj_t* mosi = MP_OBJ_TO_PTR(DEFAULT_SPI_BUS_MOSI); + const mcu_pin_obj_t* miso = MP_OBJ_TO_PTR(DEFAULT_SPI_BUS_MISO); + common_hal_busio_spi_construct(self, clock, mosi, miso); + spi_singleton = (mp_obj_t)self; return spi_singleton; } -#else + mp_obj_t board_spi(void) { + mp_obj_t singleton = common_hal_board_get_spi(); + if (singleton != NULL) { + return singleton; + } + assert_pin_free(DEFAULT_SPI_BUS_SCK); + assert_pin_free(DEFAULT_SPI_BUS_MOSI); + assert_pin_free(DEFAULT_SPI_BUS_MISO); + return common_hal_board_create_spi(); +} +#else +mp_obj_t common_hal_board_spi(void) { mp_raise_NotImplementedError(translate("No default SPI bus")); return NULL; } |
