From 0f003ac5b8312fafb120e86e05eefd2431014d8c Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Mon, 8 Apr 2019 16:58:50 -0700 Subject: Reorganize board busses into shared-bindings and shared-module. --- shared-bindings/board/__init__.c | 75 ++++++++++++++++++++++++++++++++++++ shared-bindings/board/__init__.h | 12 ++++++ shared-bindings/displayio/FourWire.h | 1 - 3 files changed, 87 insertions(+), 1 deletion(-) (limited to 'shared-bindings') diff --git a/shared-bindings/board/__init__.c b/shared-bindings/board/__init__.c index 06c2f218f..1092d40a0 100644 --- a/shared-bindings/board/__init__.c +++ b/shared-bindings/board/__init__.c @@ -37,6 +37,81 @@ //| //| Common container for board base pin names. These will vary from board to //| board so don't expect portability when using this module. +//| +//| .. warning:: The board module varies by board. The APIs documented here may or may not be +//| available on a specific board. + +//| .. method:: I2C() +//| +//| Returns the `busio.I2C` object for the board designated SDA and SCL pins. It is a singleton. +//| + +#if BOARD_I2C +mp_obj_t board_i2c(void) { + mp_obj_t singleton = common_hal_board_get_i2c(); + if (singleton != NULL) { + return singleton; + } + assert_pin_free(DEFAULT_I2C_BUS_SDA); + assert_pin_free(DEFAULT_I2C_BUS_SCL); + return common_hal_board_create_i2c(); +} +#else +mp_obj_t board_i2c(void) { + mp_raise_NotImplementedError_varg(translate("No default %q bus"), MP_QSTR_I2C); + return NULL; +} +#endif +MP_DEFINE_CONST_FUN_OBJ_0(board_i2c_obj, board_i2c); + + +//| .. method:: SPI() +//| +//| Returns the `busio.SPI` object for the board designated SCK, MOSI and MISO pins. It is a +//| singleton. +//| +#if BOARD_SPI +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 board_spi(void) { + mp_raise_NotImplementedError_varg(translate("No default %q bus"), MP_QSTR_SPI); + return NULL; +} +#endif +MP_DEFINE_CONST_FUN_OBJ_0(board_spi_obj, board_spi); + +//| .. method:: UART() +//| +//| Returns the `busio.UART` object for the board designated TX and RX pins. It is a singleton. +//| +#if BOARD_UART +mp_obj_t board_uart(void) { + mp_obj_t singleton = common_hal_board_get_uart(); + if (singleton != NULL) { + return singleton; + } + + assert_pin_free(DEFAULT_UART_BUS_RX); + assert_pin_free(DEFAULT_UART_BUS_TX); + + return common_hal_board_create_uart(); +} +#else +mp_obj_t board_uart(void) { + mp_raise_NotImplementedError_varg(translate("No default %q bus"), MP_QSTR_SPI); + return NULL; +} +#endif +MP_DEFINE_CONST_FUN_OBJ_0(board_uart_obj, board_uart); const mp_obj_module_t board_module = { .base = { &mp_type_module }, diff --git a/shared-bindings/board/__init__.h b/shared-bindings/board/__init__.h index 2730e5f51..a9b652ba8 100644 --- a/shared-bindings/board/__init__.h +++ b/shared-bindings/board/__init__.h @@ -33,4 +33,16 @@ extern const mp_obj_dict_t board_module_globals; +mp_obj_t common_hal_board_get_i2c(void); +mp_obj_t common_hal_board_create_i2c(void); +MP_DECLARE_CONST_FUN_OBJ_0(board_i2c_obj); + +mp_obj_t common_hal_board_get_spi(void); +mp_obj_t common_hal_board_create_spi(void); +MP_DECLARE_CONST_FUN_OBJ_0(board_spi_obj); + +mp_obj_t common_hal_board_get_uart(void); +mp_obj_t common_hal_board_create_uart(void); +MP_DECLARE_CONST_FUN_OBJ_0(board_uart_obj); + #endif // MICROPY_INCLUDED_SHARED_BINDINGS_BOARD___INIT___H diff --git a/shared-bindings/displayio/FourWire.h b/shared-bindings/displayio/FourWire.h index b8b00372c..b65b1b5b7 100644 --- a/shared-bindings/displayio/FourWire.h +++ b/shared-bindings/displayio/FourWire.h @@ -31,7 +31,6 @@ #include "common-hal/microcontroller/Pin.h" #include "shared-module/displayio/Group.h" -#include "supervisor/shared/board_busses.h" extern const mp_obj_type_t displayio_fourwire_type; -- cgit v1.2.3