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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "shared-bindings/microcontroller/Pin.h"
|
2018-08-16 16:34:12 -04:00
|
|
|
#include "supervisor/shared/translate.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
|
|
|
|
|
2019-04-09 14:36:10 -04:00
|
|
|
#if 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().
|
|
|
|
STATIC busio_i2c_obj_t i2c_obj;
|
|
|
|
STATIC mp_obj_t i2c_singleton = NULL;
|
|
|
|
|
2019-04-08 19:58:50 -04:00
|
|
|
mp_obj_t common_hal_board_get_i2c(void) {
|
2019-08-14 18:53:58 -04:00
|
|
|
return i2c_singleton;
|
2019-04-08 19:58:50 -04:00
|
|
|
}
|
2018-05-15 14:27:05 -04:00
|
|
|
|
2019-04-08 19:58:50 -04:00
|
|
|
mp_obj_t common_hal_board_create_i2c(void) {
|
2020-10-24 11:50:11 -04:00
|
|
|
// All callers have either already verified this or come so early that it can't be otherwise.
|
|
|
|
assert(i2c_singleton == NULL || common_hal_busio_i2c_deinited(i2c_singleton));
|
2019-08-14 18:53:58 -04:00
|
|
|
busio_i2c_obj_t *self = &i2c_obj;
|
2019-04-08 19:58:50 -04:00
|
|
|
self->base.type = &busio_i2c_type;
|
2018-05-15 14:27:05 -04:00
|
|
|
|
2020-09-25 14:07:22 -04:00
|
|
|
common_hal_busio_i2c_construct(self, DEFAULT_I2C_BUS_SCL, DEFAULT_I2C_BUS_SDA, 100000, 0);
|
2019-08-14 18:53:58 -04:00
|
|
|
i2c_singleton = (mp_obj_t)self;
|
|
|
|
return i2c_singleton;
|
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
|
|
|
|
2019-04-09 14:36:10 -04:00
|
|
|
|
|
|
|
#if 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().
|
2019-01-16 15:04:42 -05:00
|
|
|
STATIC busio_spi_obj_t spi_obj;
|
2018-09-12 17:19:43 -04:00
|
|
|
STATIC mp_obj_t spi_singleton = NULL;
|
|
|
|
|
2019-04-05 22:06:37 -04:00
|
|
|
mp_obj_t common_hal_board_get_spi(void) {
|
|
|
|
return spi_singleton;
|
|
|
|
}
|
|
|
|
|
|
|
|
mp_obj_t common_hal_board_create_spi(void) {
|
2020-10-24 11:50:11 -04:00
|
|
|
// All callers have either already verified this or come so early that it can't be otherwise.
|
|
|
|
assert(spi_singleton == NULL || common_hal_busio_spi_deinited(spi_singleton));
|
2019-04-05 22:06:37 -04:00
|
|
|
busio_spi_obj_t *self = &spi_obj;
|
|
|
|
self->base.type = &busio_spi_type;
|
2019-04-08 19:58:50 -04:00
|
|
|
|
2019-04-05 22:06:37 -04:00
|
|
|
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;
|
2018-09-12 17:19:43 -04:00
|
|
|
return spi_singleton;
|
|
|
|
}
|
2019-04-09 14:36:10 -04:00
|
|
|
#endif
|
2019-04-05 22:06:37 -04:00
|
|
|
|
2019-04-09 14:36:10 -04:00
|
|
|
#if BOARD_UART
|
2019-04-08 19:58:50 -04:00
|
|
|
mp_obj_t common_hal_board_get_uart(void) {
|
|
|
|
return MP_STATE_VM(shared_uart_bus);
|
2018-09-12 17:19:43 -04:00
|
|
|
}
|
2018-05-17 12:14:51 -04:00
|
|
|
|
2019-04-08 19:58:50 -04:00
|
|
|
mp_obj_t common_hal_board_create_uart(void) {
|
|
|
|
busio_uart_obj_t *self = m_new_ll_obj(busio_uart_obj_t);
|
|
|
|
self->base.type = &busio_uart_type;
|
2018-05-17 12:14:51 -04:00
|
|
|
|
2019-04-08 19:58:50 -04:00
|
|
|
const mcu_pin_obj_t* rx = MP_OBJ_TO_PTR(DEFAULT_UART_BUS_RX);
|
|
|
|
const mcu_pin_obj_t* tx = MP_OBJ_TO_PTR(DEFAULT_UART_BUS_TX);
|
2020-02-15 19:23:43 -05:00
|
|
|
#ifdef DEFAULT_UART_BUS_RTS
|
|
|
|
const mcu_pin_obj_t* rts = MP_OBJ_TO_PTR(DEFAULT_UART_BUS_RTS);
|
|
|
|
#else
|
2020-02-28 23:32:24 -05:00
|
|
|
const mcu_pin_obj_t* rts = NULL;
|
2020-02-15 19:23:43 -05:00
|
|
|
#endif
|
|
|
|
#ifdef DEFAULT_UART_BUS_CTS
|
|
|
|
const mcu_pin_obj_t* cts = MP_OBJ_TO_PTR(DEFAULT_UART_BUS_CTS);
|
|
|
|
#else
|
2020-02-28 23:32:24 -05:00
|
|
|
const mcu_pin_obj_t* cts = NULL;
|
2020-02-15 19:23:43 -05:00
|
|
|
#endif
|
|
|
|
#ifdef DEFAULT_UART_IS_RS485
|
2020-02-17 19:09:35 -05:00
|
|
|
const mcu_pin_obj_t* rs485_dir = MP_OBJ_TO_PTR(DEFAULT_UART_BUS_RS485DIR);
|
|
|
|
#ifdef DEFAULT_UART_RS485_INVERT
|
|
|
|
const bool rs485_invert = true;
|
2020-02-23 06:16:24 -05:00
|
|
|
#else
|
|
|
|
const bool rs485_invert = false;
|
2020-02-17 19:09:35 -05:00
|
|
|
#endif
|
2020-02-15 19:23:43 -05:00
|
|
|
#else
|
2020-02-28 23:32:24 -05:00
|
|
|
const mcu_pin_obj_t* rs485_dir = NULL;
|
2020-02-23 06:16:24 -05:00
|
|
|
const bool rs485_invert = false;
|
2020-02-15 19:23:43 -05:00
|
|
|
#endif
|
2018-05-17 12:14:51 -04:00
|
|
|
|
2020-02-17 19:09:35 -05:00
|
|
|
common_hal_busio_uart_construct(self, tx, rx, rts, cts, rs485_dir, rs485_invert,
|
2020-06-09 21:28:02 -04:00
|
|
|
9600, 8, BUSIO_UART_PARITY_NONE, 1, 1.0f, 64, NULL, false);
|
2019-04-08 19:58:50 -04:00
|
|
|
MP_STATE_VM(shared_uart_bus) = MP_OBJ_FROM_PTR(self);
|
|
|
|
return MP_STATE_VM(shared_uart_bus);
|
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
|
|
|
|
|
|
|
void reset_board_busses(void) {
|
|
|
|
#if BOARD_I2C
|
2019-08-14 18:53:58 -04:00
|
|
|
bool display_using_i2c = false;
|
|
|
|
#if CIRCUITPY_DISPLAYIO
|
|
|
|
for (uint8_t i = 0; i < CIRCUITPY_DISPLAY_LIMIT; i++) {
|
2020-10-24 11:13:10 -04:00
|
|
|
if (displays[i].bus_base.type == &displayio_i2cdisplay_type && displays[i].i2cdisplay_bus.bus == i2c_singleton) {
|
2019-08-14 18:53:58 -04:00
|
|
|
display_using_i2c = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2020-10-24 11:23:17 -04:00
|
|
|
if (i2c_singleton != NULL) {
|
|
|
|
if (!display_using_i2c) {
|
|
|
|
common_hal_busio_i2c_deinit(i2c_singleton);
|
|
|
|
i2c_singleton = NULL;
|
|
|
|
}
|
2019-08-14 18:53:58 -04:00
|
|
|
}
|
2018-09-12 17:19:43 -04:00
|
|
|
#endif
|
|
|
|
#if BOARD_SPI
|
2019-01-17 21:19:07 -05:00
|
|
|
bool display_using_spi = false;
|
2019-06-06 17:49:32 -04:00
|
|
|
#if CIRCUITPY_DISPLAYIO
|
2019-01-17 21:19:07 -05:00
|
|
|
for (uint8_t i = 0; i < CIRCUITPY_DISPLAY_LIMIT; i++) {
|
2020-08-18 10:31:03 -04:00
|
|
|
mp_const_obj_t bus_type = displays[i].bus_base.type;
|
|
|
|
if (bus_type == &displayio_fourwire_type && displays[i].fourwire_bus.bus == spi_singleton) {
|
2019-01-17 21:19:07 -05:00
|
|
|
display_using_spi = true;
|
|
|
|
break;
|
|
|
|
}
|
2020-08-18 10:31:03 -04:00
|
|
|
#if CIRCUITPY_SHARPDISPLAY
|
|
|
|
if (displays[i].bus_base.type == &sharpdisplay_framebuffer_type && displays[i].sharpdisplay.bus == spi_singleton) {
|
|
|
|
display_using_spi = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
2020-08-18 11:03:45 -04:00
|
|
|
#endif
|
2020-09-11 11:54:46 -04:00
|
|
|
// make sure SPI lock is not held over a soft reset
|
2020-09-29 16:04:39 -04:00
|
|
|
if (spi_singleton != NULL) {
|
|
|
|
common_hal_busio_spi_unlock(spi_singleton);
|
2020-10-24 11:23:17 -04:00
|
|
|
if (!display_using_spi) {
|
|
|
|
common_hal_busio_spi_deinit(spi_singleton);
|
|
|
|
spi_singleton = NULL;
|
|
|
|
}
|
2019-01-17 21:19:07 -05:00
|
|
|
}
|
2018-09-12 17:19:43 -04:00
|
|
|
#endif
|
|
|
|
#if BOARD_UART
|
2019-04-08 19:58:50 -04:00
|
|
|
MP_STATE_VM(shared_uart_bus) = NULL;
|
2018-09-12 17:19:43 -04:00
|
|
|
#endif
|
|
|
|
}
|