|
28 | 28 | #include "py/runtime.h"
|
29 | 29 |
|
30 | 30 | #include "shared-bindings/board/__init__.h"
|
31 |
| -#if BOARD_I2C |
| 31 | +#if CIRCUITPY_BOARD_I2C |
32 | 32 | #include "shared-bindings/busio/I2C.h"
|
33 | 33 | #endif
|
34 |
| -#if BOARD_SPI |
| 34 | +#if CIRCUITPY_BOARD_SPI |
35 | 35 | #include "shared-bindings/busio/SPI.h"
|
36 | 36 | #endif
|
| 37 | +#if CIRCUITPY_BOARD_UART |
| 38 | +#include "shared-bindings/busio/UART.h" |
| 39 | +#endif |
37 | 40 |
|
38 | 41 | //| """Board specific pin names
|
39 | 42 | //|
|
|
47 | 50 | //| """Board ID string. The unique identifier for the board model in
|
48 | 51 | //| circuitpython, as well as on circuitpython.org.
|
49 | 52 | //| Example: "hallowing_m0_express"."""
|
50 |
| -//| |
51 | 53 |
|
52 |
| -//| def I2C() -> busio.I2C: |
53 |
| -//| """Returns the `busio.I2C` object for the board designated SDA and SCL pins. It is a singleton.""" |
| 54 | +#if CIRCUITPY_BOARD_I2C || CIRCUITPY_BOARD_SPI || CIRCUITPY_BOARD_UART |
| 55 | +STATIC mp_int_t board_get_instance(size_t n_args, const mp_obj_t *args, const mp_int_t bus_in) { |
| 56 | + if (n_args == 0) { |
| 57 | + return 0; |
| 58 | + } |
| 59 | + const mp_int_t instance = mp_obj_get_int(args[0]); |
| 60 | + if (instance >= bus_in || instance < 0) { |
| 61 | + mp_raise_ValueError_varg(translate("No default %q bus"), MP_QSTR_UART); |
| 62 | + } |
| 63 | + return instance; |
| 64 | +} |
| 65 | +#endif |
| 66 | + |
| 67 | +//| def I2C(instance: Optional[int] = 0) -> busio.I2C: |
| 68 | +//| """Returns the `busio.I2C` object for the board's designated I2C bus(es). It is a singleton. |
| 69 | +//| The object created uses the default parameter values for `busio.I2C`.""" |
54 | 70 | //| ...
|
55 | 71 | //|
|
56 |
| - |
57 |
| -#if BOARD_I2C |
58 |
| -mp_obj_t board_i2c(void) { |
59 |
| - mp_obj_t singleton = common_hal_board_get_i2c(); |
| 72 | +#if CIRCUITPY_BOARD_I2C |
| 73 | +mp_obj_t board_i2c(size_t n_args, const mp_obj_t *args) { |
| 74 | + const mp_int_t instance = board_get_instance(n_args, args, CIRCUITPY_BOARD_I2C); |
| 75 | + const mp_obj_t singleton = common_hal_board_get_i2c(instance); |
60 | 76 | if (singleton != NULL && !common_hal_busio_i2c_deinited(singleton)) {
|
61 | 77 | return singleton;
|
62 | 78 | }
|
63 |
| - assert_pin_free(DEFAULT_I2C_BUS_SDA); |
64 |
| - assert_pin_free(DEFAULT_I2C_BUS_SCL); |
65 |
| - return common_hal_board_create_i2c(); |
| 79 | + return common_hal_board_create_i2c(instance); |
66 | 80 | }
|
67 | 81 | #else
|
68 |
| -mp_obj_t board_i2c(void) { |
| 82 | +mp_obj_t board_i2c(size_t n_args, const mp_obj_t *args) { |
69 | 83 | mp_raise_NotImplementedError_varg(translate("No default %q bus"), MP_QSTR_I2C);
|
70 | 84 | return NULL;
|
71 | 85 | }
|
72 | 86 | #endif
|
73 |
| -MP_DEFINE_CONST_FUN_OBJ_0(board_i2c_obj, board_i2c); |
74 |
| - |
| 87 | +MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(board_i2c_obj, 0, 1, board_i2c); |
75 | 88 |
|
76 |
| -//| def SPI() -> busio.SPI: |
77 |
| -//| """Returns the `busio.SPI` object for the board designated SCK, MOSI and MISO pins. It is a |
78 |
| -//| singleton.""" |
| 89 | +//| def SPI(instance: Optional[int] = 0) -> busio.SPI: |
| 90 | +//| """Returns the `busio.SPI` object for the board's designated SPI bus(es). It is a singleton. |
| 91 | +//| The object created uses the default parameter values for `busio.SPI`.""" |
79 | 92 | //| ...
|
80 | 93 | //|
|
81 |
| -#if BOARD_SPI |
82 |
| -mp_obj_t board_spi(void) { |
83 |
| - mp_obj_t singleton = common_hal_board_get_spi(); |
| 94 | +#if CIRCUITPY_BOARD_SPI |
| 95 | +mp_obj_t board_spi(size_t n_args, const mp_obj_t *args) { |
| 96 | + const mp_int_t instance = board_get_instance(n_args, args, CIRCUITPY_BOARD_SPI); |
| 97 | + const mp_obj_t singleton = common_hal_board_get_spi(instance); |
84 | 98 | if (singleton != NULL && !common_hal_busio_spi_deinited(singleton)) {
|
85 | 99 | return singleton;
|
86 | 100 | }
|
87 |
| - assert_pin_free(DEFAULT_SPI_BUS_SCK); |
88 |
| - assert_pin_free(DEFAULT_SPI_BUS_MOSI); |
89 |
| - assert_pin_free(DEFAULT_SPI_BUS_MISO); |
90 |
| - return common_hal_board_create_spi(); |
| 101 | + return common_hal_board_create_spi(instance); |
91 | 102 | }
|
92 | 103 | #else
|
93 |
| -mp_obj_t board_spi(void) { |
| 104 | +mp_obj_t board_spi(size_t n_args, const mp_obj_t *args) { |
94 | 105 | mp_raise_NotImplementedError_varg(translate("No default %q bus"), MP_QSTR_SPI);
|
95 | 106 | return NULL;
|
96 | 107 | }
|
97 | 108 | #endif
|
98 |
| -MP_DEFINE_CONST_FUN_OBJ_0(board_spi_obj, board_spi); |
| 109 | +MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(board_spi_obj, 0, 1, board_spi); |
99 | 110 |
|
100 |
| -//| def UART() -> busio.UART: |
101 |
| -//| """Returns the `busio.UART` object for the board designated TX and RX pins. It is a singleton. |
102 |
| -//| |
103 |
| -//| The object created uses the default parameter values for `busio.UART`. If you need to set |
104 |
| -//| parameters that are not changeable after creation, such as ``receiver_buffer_size``, |
105 |
| -//| do not use `board.UART()`; instead create a `busio.UART` object explicitly with the |
106 |
| -//| desired parameters.""" |
| 111 | +//| def UART(instance: Optional[int] = 0) -> busio.UART: |
| 112 | +//| """Returns the `busio.UART` object for the board's designated UART bus(es). It is a singleton. |
| 113 | +//| The object created uses the default parameter values for `busio.UART`.""" |
107 | 114 | //| ...
|
108 | 115 | //|
|
109 |
| -#if BOARD_UART |
110 |
| -mp_obj_t board_uart(void) { |
111 |
| - mp_obj_t singleton = common_hal_board_get_uart(); |
112 |
| - if (singleton != NULL) { |
| 116 | +#if CIRCUITPY_BOARD_UART |
| 117 | +mp_obj_t board_uart(size_t n_args, const mp_obj_t *args) { |
| 118 | + const mp_int_t instance = board_get_instance(n_args, args, CIRCUITPY_BOARD_UART); |
| 119 | + const mp_obj_t singleton = common_hal_board_get_uart(instance); |
| 120 | + if (singleton != NULL && !common_hal_busio_uart_deinited(singleton)) { |
113 | 121 | return singleton;
|
114 | 122 | }
|
115 |
| - |
116 |
| - assert_pin_free(DEFAULT_UART_BUS_RX); |
117 |
| - assert_pin_free(DEFAULT_UART_BUS_TX); |
118 |
| - |
119 |
| - return common_hal_board_create_uart(); |
| 123 | + return common_hal_board_create_uart(instance); |
120 | 124 | }
|
121 | 125 | #else
|
122 |
| -mp_obj_t board_uart(void) { |
| 126 | +mp_obj_t board_uart(size_t n_args, const mp_obj_t *args) { |
123 | 127 | mp_raise_NotImplementedError_varg(translate("No default %q bus"), MP_QSTR_UART);
|
124 | 128 | return NULL;
|
125 | 129 | }
|
126 | 130 | #endif
|
127 |
| -MP_DEFINE_CONST_FUN_OBJ_0(board_uart_obj, board_uart); |
| 131 | +MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(board_uart_obj, 0, 1, board_uart); |
128 | 132 |
|
129 | 133 | const mp_obj_module_t board_module = {
|
130 | 134 | .base = { &mp_type_module },
|
|
0 commit comments