2018-05-15 13:15:24 -04:00
|
|
|
/*
|
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2018 Scott Shawcroft for Adafruit Industries
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2021-11-10 09:42:21 -05:00
|
|
|
#include "shared-bindings/board/__init__.h"
|
2018-05-15 13:15:24 -04:00
|
|
|
#include "shared-bindings/microcontroller/Pin.h"
|
2021-11-10 09:42:21 -05:00
|
|
|
#include "shared-module/board/__init__.h"
|
2018-05-15 13:15:24 -04:00
|
|
|
#include "mpconfigboard.h"
|
2018-05-15 14:27:05 -04:00
|
|
|
#include "py/runtime.h"
|
2018-05-15 13:15:24 -04:00
|
|
|
|
2019-07-22 12:58:28 -04:00
|
|
|
#if CIRCUITPY_BUSIO
|
|
|
|
#include "shared-bindings/busio/I2C.h"
|
|
|
|
#include "shared-bindings/busio/SPI.h"
|
|
|
|
#include "shared-bindings/busio/UART.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if CIRCUITPY_DISPLAYIO
|
|
|
|
#include "shared-module/displayio/__init__.h"
|
|
|
|
#endif
|
2019-01-17 21:19:07 -05:00
|
|
|
|
2020-08-18 10:31:03 -04:00
|
|
|
#if CIRCUITPY_SHARPDISPLAY
|
|
|
|
#include "shared-bindings/sharpdisplay/SharpMemoryFramebuffer.h"
|
|
|
|
#include "shared-module/sharpdisplay/SharpMemoryFramebuffer.h"
|
|
|
|
#endif
|
|
|
|
|
2021-12-11 11:03:12 -05:00
|
|
|
#if CIRCUITPY_BOARD_I2C
|
2019-08-14 18:53:58 -04:00
|
|
|
// Statically allocate the I2C object so it can live past the end of the heap and into the next VM.
|
|
|
|
// That way it can be used by built-in I2CDisplay displays and be accessible through board.I2C().
|
|
|
|
|
2021-12-11 11:03:12 -05:00
|
|
|
typedef struct {
|
|
|
|
const mcu_pin_obj_t *scl;
|
|
|
|
const mcu_pin_obj_t *sda;
|
|
|
|
} board_i2c_pin_t;
|
|
|
|
|
|
|
|
static const board_i2c_pin_t i2c_pin[CIRCUITPY_BOARD_I2C] = CIRCUITPY_BOARD_I2C_PIN;
|
|
|
|
static busio_i2c_obj_t i2c_obj[CIRCUITPY_BOARD_I2C];
|
2022-01-30 23:12:49 -05:00
|
|
|
static bool i2c_obj_created[CIRCUITPY_BOARD_I2C];
|
2021-12-11 11:03:12 -05:00
|
|
|
|
|
|
|
bool common_hal_board_is_i2c(mp_obj_t obj) {
|
|
|
|
for (uint8_t instance = 0; instance < CIRCUITPY_BOARD_I2C; instance++) {
|
|
|
|
if (obj == &i2c_obj[instance]) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
mp_obj_t common_hal_board_get_i2c(const mp_int_t instance) {
|
2022-01-30 23:12:49 -05:00
|
|
|
return i2c_obj_created[instance] ? &i2c_obj[instance] : NULL;
|
2019-04-08 19:58:50 -04:00
|
|
|
}
|
2018-05-15 14:27:05 -04:00
|
|
|
|
2021-12-11 11:03:12 -05:00
|
|
|
mp_obj_t common_hal_board_create_i2c(const mp_int_t instance) {
|
2022-01-20 09:30:02 -05:00
|
|
|
const mp_obj_t singleton = common_hal_board_get_i2c(instance);
|
|
|
|
if (singleton != NULL && !common_hal_busio_i2c_deinited(singleton)) {
|
|
|
|
return singleton;
|
|
|
|
}
|
|
|
|
|
2021-12-11 11:03:12 -05:00
|
|
|
busio_i2c_obj_t *self = &i2c_obj[instance];
|
2019-04-08 19:58:50 -04:00
|
|
|
self->base.type = &busio_i2c_type;
|
2018-05-15 14:27:05 -04:00
|
|
|
|
2021-12-11 11:03:12 -05:00
|
|
|
assert_pin_free(i2c_pin[instance].scl);
|
|
|
|
assert_pin_free(i2c_pin[instance].sda);
|
|
|
|
|
|
|
|
common_hal_busio_i2c_construct(self, i2c_pin[instance].scl, i2c_pin[instance].sda, 100000, 255);
|
|
|
|
|
2022-01-30 23:12:49 -05:00
|
|
|
i2c_obj_created[instance] = true;
|
2021-12-11 11:03:12 -05:00
|
|
|
return &i2c_obj[instance];
|
2018-09-12 17:19:43 -04:00
|
|
|
}
|
2019-04-09 14:36:10 -04:00
|
|
|
#endif
|
2018-05-16 14:58:57 -04:00
|
|
|
|
2021-12-11 11:03:12 -05:00
|
|
|
#if CIRCUITPY_BOARD_SPI
|
2019-01-18 19:37:06 -05:00
|
|
|
// Statically allocate the SPI object so it can live past the end of the heap and into the next VM.
|
|
|
|
// That way it can be used by built-in FourWire displays and be accessible through board.SPI().
|
2018-09-12 17:19:43 -04:00
|
|
|
|
2021-12-11 11:03:12 -05:00
|
|
|
typedef struct {
|
|
|
|
const mcu_pin_obj_t *clock;
|
|
|
|
const mcu_pin_obj_t *mosi;
|
|
|
|
const mcu_pin_obj_t *miso;
|
|
|
|
} board_spi_pin_t;
|
|
|
|
|
|
|
|
static const board_spi_pin_t spi_pin[CIRCUITPY_BOARD_SPI] = CIRCUITPY_BOARD_SPI_PIN;
|
|
|
|
static busio_spi_obj_t spi_obj[CIRCUITPY_BOARD_SPI];
|
2022-01-30 23:12:49 -05:00
|
|
|
static bool spi_obj_created[CIRCUITPY_BOARD_SPI];
|
2021-12-11 11:03:12 -05:00
|
|
|
|
|
|
|
bool common_hal_board_is_spi(mp_obj_t obj) {
|
|
|
|
for (uint8_t instance = 0; instance < CIRCUITPY_BOARD_SPI; instance++) {
|
|
|
|
if (obj == &spi_obj[instance]) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
mp_obj_t common_hal_board_get_spi(const mp_int_t instance) {
|
2022-01-30 23:12:49 -05:00
|
|
|
return spi_obj_created[instance] ? &spi_obj[instance] : NULL;
|
2019-04-05 22:06:37 -04:00
|
|
|
}
|
|
|
|
|
2021-12-11 11:03:12 -05:00
|
|
|
mp_obj_t common_hal_board_create_spi(const mp_int_t instance) {
|
2022-01-20 09:30:02 -05:00
|
|
|
const mp_obj_t singleton = common_hal_board_get_spi(instance);
|
|
|
|
if (singleton != NULL && !common_hal_busio_spi_deinited(singleton)) {
|
|
|
|
return singleton;
|
|
|
|
}
|
|
|
|
|
2021-12-11 11:03:12 -05:00
|
|
|
busio_spi_obj_t *self = &spi_obj[instance];
|
2019-04-05 22:06:37 -04:00
|
|
|
self->base.type = &busio_spi_type;
|
2019-04-08 19:58:50 -04:00
|
|
|
|
2021-12-11 11:03:12 -05:00
|
|
|
assert_pin_free(spi_pin[instance].clock);
|
|
|
|
assert_pin_free(spi_pin[instance].mosi);
|
|
|
|
assert_pin_free(spi_pin[instance].miso);
|
|
|
|
|
2022-02-08 21:37:38 -05:00
|
|
|
common_hal_busio_spi_construct(self, spi_pin[instance].clock, spi_pin[instance].mosi, spi_pin[instance].miso, false);
|
2021-12-11 11:03:12 -05:00
|
|
|
|
2022-01-30 23:12:49 -05:00
|
|
|
spi_obj_created[instance] = true;
|
2021-12-11 11:03:12 -05:00
|
|
|
return &spi_obj[instance];
|
2018-09-12 17:19:43 -04:00
|
|
|
}
|
2019-04-09 14:36:10 -04:00
|
|
|
#endif
|
2019-04-05 22:06:37 -04:00
|
|
|
|
2021-12-11 11:03:12 -05:00
|
|
|
#if CIRCUITPY_BOARD_UART
|
|
|
|
|
2023-09-28 16:22:10 -04:00
|
|
|
|
|
|
|
MP_REGISTER_ROOT_POINTER(mp_obj_t board_uart_bus);
|
|
|
|
|
2021-12-11 11:03:12 -05:00
|
|
|
typedef struct {
|
|
|
|
const mcu_pin_obj_t *tx;
|
|
|
|
const mcu_pin_obj_t *rx;
|
|
|
|
} board_uart_pin_t;
|
|
|
|
|
|
|
|
static const board_uart_pin_t uart_pin[CIRCUITPY_BOARD_UART] = CIRCUITPY_BOARD_UART_PIN;
|
|
|
|
static busio_uart_obj_t uart_obj[CIRCUITPY_BOARD_UART];
|
2022-01-30 23:12:49 -05:00
|
|
|
static bool uart_obj_created[CIRCUITPY_BOARD_UART];
|
2021-12-11 11:03:12 -05:00
|
|
|
|
|
|
|
bool common_hal_board_is_uart(mp_obj_t obj) {
|
|
|
|
for (uint8_t instance = 0; instance < CIRCUITPY_BOARD_UART; instance++) {
|
|
|
|
if (obj == &uart_obj[instance]) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
mp_obj_t common_hal_board_get_uart(const mp_int_t instance) {
|
2022-01-30 23:12:49 -05:00
|
|
|
return uart_obj_created[instance] ? &uart_obj[instance] : NULL;
|
2018-09-12 17:19:43 -04:00
|
|
|
}
|
2018-05-17 12:14:51 -04:00
|
|
|
|
2021-12-11 11:03:12 -05:00
|
|
|
mp_obj_t common_hal_board_create_uart(const mp_int_t instance) {
|
2022-01-20 09:30:02 -05:00
|
|
|
const mp_obj_t singleton = common_hal_board_get_uart(instance);
|
|
|
|
if (singleton != NULL && !common_hal_busio_uart_deinited(singleton)) {
|
|
|
|
return singleton;
|
|
|
|
}
|
|
|
|
|
2021-12-11 11:03:12 -05:00
|
|
|
busio_uart_obj_t *self = &uart_obj[instance];
|
2019-04-08 19:58:50 -04:00
|
|
|
self->base.type = &busio_uart_type;
|
2018-05-17 12:14:51 -04:00
|
|
|
|
2021-12-11 11:03:12 -05:00
|
|
|
MP_STATE_VM(board_uart_bus) = &uart_obj;
|
2018-05-17 12:14:51 -04:00
|
|
|
|
2021-12-11 11:03:12 -05:00
|
|
|
assert_pin_free(uart_pin[instance].tx);
|
|
|
|
assert_pin_free(uart_pin[instance].rx);
|
|
|
|
|
|
|
|
common_hal_busio_uart_construct(self, uart_pin[instance].tx, uart_pin[instance].rx,
|
|
|
|
NULL, NULL, NULL, false, 9600, 8, BUSIO_UART_PARITY_NONE, 1, 1.0f, 64, NULL, false);
|
|
|
|
|
2022-01-30 23:12:49 -05:00
|
|
|
uart_obj_created[instance] = true;
|
2021-12-11 11:03:12 -05:00
|
|
|
return &uart_obj[instance];
|
2018-09-12 17:19:43 -04:00
|
|
|
}
|
2019-04-09 14:36:10 -04:00
|
|
|
#endif
|
2018-09-12 17:19:43 -04:00
|
|
|
|
2021-12-11 11:03:12 -05:00
|
|
|
void reset_board_buses(void) {
|
|
|
|
#if CIRCUITPY_BOARD_I2C
|
|
|
|
for (uint8_t instance = 0; instance < CIRCUITPY_BOARD_I2C; instance++) {
|
|
|
|
bool display_using_i2c = false;
|
2023-10-17 19:09:47 -04:00
|
|
|
#if CIRCUITPY_I2CDISPLAYBUS
|
2021-12-11 11:03:12 -05:00
|
|
|
for (uint8_t i = 0; i < CIRCUITPY_DISPLAY_LIMIT; i++) {
|
2023-10-17 19:09:47 -04:00
|
|
|
if (display_buses[i].bus_base.type == &i2cdisplaybus_i2cdisplaybus_type && display_buses[i].i2cdisplay_bus.bus == &i2c_obj[instance]) {
|
2021-12-11 11:03:12 -05:00
|
|
|
display_using_i2c = true;
|
|
|
|
break;
|
|
|
|
}
|
2019-08-14 18:53:58 -04:00
|
|
|
}
|
2021-12-11 11:03:12 -05:00
|
|
|
#endif
|
2022-01-30 23:12:49 -05:00
|
|
|
if (i2c_obj_created[instance]) {
|
2021-12-11 11:03:12 -05:00
|
|
|
// make sure I2C lock is not held over a soft reset
|
|
|
|
common_hal_busio_i2c_unlock(&i2c_obj[instance]);
|
|
|
|
if (!display_using_i2c) {
|
|
|
|
common_hal_busio_i2c_deinit(&i2c_obj[instance]);
|
2022-01-30 23:12:49 -05:00
|
|
|
i2c_obj_created[instance] = false;
|
2021-12-11 11:03:12 -05:00
|
|
|
}
|
2020-10-24 11:23:17 -04:00
|
|
|
}
|
2019-08-14 18:53:58 -04:00
|
|
|
}
|
2021-03-15 09:57:36 -04:00
|
|
|
#endif
|
2021-12-11 11:03:12 -05:00
|
|
|
#if CIRCUITPY_BOARD_SPI
|
|
|
|
for (uint8_t instance = 0; instance < CIRCUITPY_BOARD_SPI; instance++) {
|
|
|
|
bool display_using_spi = false;
|
2023-10-17 19:09:47 -04:00
|
|
|
#if CIRCUITPY_FOURWIRE || CIRCUITPY_SHARPDISPLAY
|
2021-12-11 11:03:12 -05:00
|
|
|
for (uint8_t i = 0; i < CIRCUITPY_DISPLAY_LIMIT; i++) {
|
2023-04-20 19:15:06 -04:00
|
|
|
mp_const_obj_t bus_type = display_buses[i].bus_base.type;
|
2023-10-17 19:09:47 -04:00
|
|
|
#if CIRCUITPY_FOURWIRE
|
|
|
|
if (bus_type == &fourwire_fourwire_type && display_buses[i].fourwire_bus.bus == &spi_obj[instance]) {
|
2021-12-11 11:03:12 -05:00
|
|
|
display_using_spi = true;
|
|
|
|
break;
|
|
|
|
}
|
2023-10-17 19:09:47 -04:00
|
|
|
#endif
|
2021-12-11 11:03:12 -05:00
|
|
|
#if CIRCUITPY_SHARPDISPLAY
|
2023-04-20 19:15:06 -04:00
|
|
|
if (bus_type == &sharpdisplay_framebuffer_type && display_buses[i].sharpdisplay.bus == &spi_obj[instance]) {
|
2021-12-11 11:03:12 -05:00
|
|
|
display_using_spi = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif
|
2020-08-18 10:31:03 -04:00
|
|
|
}
|
|
|
|
#endif
|
2022-01-30 23:12:49 -05:00
|
|
|
if (spi_obj_created[instance]) {
|
2021-12-11 11:03:12 -05:00
|
|
|
// make sure SPI lock is not held over a soft reset
|
|
|
|
common_hal_busio_spi_unlock(&spi_obj[instance]);
|
|
|
|
if (!display_using_spi) {
|
|
|
|
common_hal_busio_spi_deinit(&spi_obj[instance]);
|
2022-01-30 23:12:49 -05:00
|
|
|
spi_obj_created[instance] = false;
|
2021-12-11 11:03:12 -05:00
|
|
|
}
|
2020-10-24 11:23:17 -04:00
|
|
|
}
|
2019-01-17 21:19:07 -05:00
|
|
|
}
|
2021-03-15 09:57:36 -04:00
|
|
|
#endif
|
2021-12-11 11:03:12 -05:00
|
|
|
#if CIRCUITPY_BOARD_UART
|
|
|
|
for (uint8_t instance = 0; instance < CIRCUITPY_BOARD_UART; instance++) {
|
2022-01-30 23:12:49 -05:00
|
|
|
if (uart_obj_created[instance]) {
|
|
|
|
common_hal_busio_uart_deinit(&uart_obj[instance]);
|
|
|
|
uart_obj_created[instance] = false;
|
|
|
|
}
|
2021-12-11 11:03:12 -05:00
|
|
|
}
|
2021-03-15 09:57:36 -04:00
|
|
|
#endif
|
2018-09-12 17:19:43 -04:00
|
|
|
}
|