summaryrefslogtreecommitdiff
path: root/shared-module
diff options
context:
space:
mode:
authorDan Halbert <halbert@halwitz.org>2019-04-10 14:09:26 -0400
committerDan Halbert <halbert@halwitz.org>2019-04-10 14:09:26 -0400
commit3618461f9b7d4dbf179cb02b671919b621022a2f (patch)
treee933837e02ff1473b88935526068f8e5af3affa5 /shared-module
parent18908c21f7998b13493fe75d0020bd9ad1bd1b60 (diff)
parentf115ec219353cb43027548d900be740522035bff (diff)
Merge remote-tracking branch 'adafruit/master' into circuitpython-nickzoic-1042-nrf-nvm-bytearray-2
Diffstat (limited to 'shared-module')
-rw-r--r--shared-module/board/__init__.c121
-rw-r--r--shared-module/board/__init__.h32
-rw-r--r--shared-module/displayio/FourWire.c5
-rw-r--r--shared-module/displayio/__init__.c12
4 files changed, 167 insertions, 3 deletions
diff --git a/shared-module/board/__init__.c b/shared-module/board/__init__.c
new file mode 100644
index 000000000..ac4de2fe5
--- /dev/null
+++ b/shared-module/board/__init__.c
@@ -0,0 +1,121 @@
+/*
+ * 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/busio/I2C.h"
+#include "shared-bindings/busio/SPI.h"
+#include "shared-bindings/busio/UART.h"
+
+#include "shared-bindings/microcontroller/Pin.h"
+#include "supervisor/shared/translate.h"
+#include "mpconfigboard.h"
+#include "py/runtime.h"
+
+#ifdef CIRCUITPY_DISPLAYIO
+#include "shared-module/displayio/__init__.h"
+#endif
+
+#if BOARD_I2C
+mp_obj_t common_hal_board_get_i2c(void) {
+ return MP_STATE_VM(shared_i2c_bus);
+}
+
+mp_obj_t common_hal_board_create_i2c(void) {
+ busio_i2c_obj_t *self = m_new_ll_obj(busio_i2c_obj_t);
+ self->base.type = &busio_i2c_type;
+
+ common_hal_busio_i2c_construct(self, DEFAULT_I2C_BUS_SCL, DEFAULT_I2C_BUS_SDA, 400000, 0);
+ MP_STATE_VM(shared_i2c_bus) = MP_OBJ_FROM_PTR(self);
+ return MP_STATE_VM(shared_i2c_bus);
+}
+#endif
+
+
+#if BOARD_SPI
+// 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().
+STATIC busio_spi_obj_t spi_obj;
+STATIC mp_obj_t spi_singleton = NULL;
+
+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;
+
+ 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;
+}
+#endif
+
+#if BOARD_UART
+mp_obj_t common_hal_board_get_uart(void) {
+ return MP_STATE_VM(shared_uart_bus);
+}
+
+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;
+
+ 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);
+
+ common_hal_busio_uart_construct(self, tx, rx, 9600, 8, PARITY_NONE, 1, 1000, 64);
+ MP_STATE_VM(shared_uart_bus) = MP_OBJ_FROM_PTR(self);
+ return MP_STATE_VM(shared_uart_bus);
+}
+#endif
+
+void reset_board_busses(void) {
+#if BOARD_I2C
+ MP_STATE_VM(shared_i2c_bus) = NULL;
+#endif
+#if BOARD_SPI
+ bool display_using_spi = false;
+ #ifdef CIRCUITPY_DISPLAYIO
+ for (uint8_t i = 0; i < CIRCUITPY_DISPLAY_LIMIT; i++) {
+ if (displays[i].fourwire_bus.bus == spi_singleton) {
+ display_using_spi = true;
+ break;
+ }
+ }
+ #endif
+ if (!display_using_spi) {
+ spi_singleton = NULL;
+ }
+#endif
+#if BOARD_UART
+ MP_STATE_VM(shared_uart_bus) = NULL;
+#endif
+}
diff --git a/shared-module/board/__init__.h b/shared-module/board/__init__.h
new file mode 100644
index 000000000..f7eecd417
--- /dev/null
+++ b/shared-module/board/__init__.h
@@ -0,0 +1,32 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2019 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.
+ */
+
+#ifndef MICROPY_INCLUDED_SHARED_MODULE_BOARD__INIT__H
+#define MICROPY_INCLUDED_SHARED_MODULE_BOARD__INIT__H
+
+void reset_board_busses(void);
+
+#endif // MICROPY_INCLUDED_SHARED_MODULE_BOARD__INIT__H
diff --git a/shared-module/displayio/FourWire.c b/shared-module/displayio/FourWire.c
index c87d2be95..b01456bba 100644
--- a/shared-module/displayio/FourWire.c
+++ b/shared-module/displayio/FourWire.c
@@ -28,6 +28,7 @@
#include <stdint.h>
+#include "py/gc.h"
#include "shared-bindings/busio/SPI.h"
#include "shared-bindings/digitalio/DigitalInOut.h"
#include "shared-bindings/time/__init__.h"
@@ -40,6 +41,10 @@ void common_hal_displayio_fourwire_construct(displayio_fourwire_obj_t* self,
self->bus = spi;
common_hal_busio_spi_never_reset(self->bus);
+ // Our object is statically allocated off the heap so make sure the bus object lives to the end
+ // of the heap as well.
+ gc_never_free(self->bus);
+
self->frequency = common_hal_busio_spi_get_frequency(spi);
self->polarity = common_hal_busio_spi_get_polarity(spi);
self->phase = common_hal_busio_spi_get_phase(spi);
diff --git a/shared-module/displayio/__init__.c b/shared-module/displayio/__init__.c
index ced5b0fef..156640440 100644
--- a/shared-module/displayio/__init__.c
+++ b/shared-module/displayio/__init__.c
@@ -5,6 +5,7 @@
#include "lib/utils/interrupt_char.h"
#include "py/reload.h"
#include "py/runtime.h"
+#include "shared-bindings/board/__init__.h"
#include "shared-bindings/displayio/Bitmap.h"
#include "shared-bindings/displayio/Display.h"
#include "shared-bindings/displayio/Group.h"
@@ -190,9 +191,14 @@ void reset_displays(void) {
if (((uint32_t) fourwire->bus) < ((uint32_t) &displays) ||
((uint32_t) fourwire->bus) > ((uint32_t) &displays + CIRCUITPY_DISPLAY_LIMIT)) {
busio_spi_obj_t* original_spi = fourwire->bus;
- if (original_spi == board_spi()) {
- continue;
- }
+ #if BOARD_SPI
+ // We don't need to move original_spi if it is the board.SPI object because it is
+ // statically allocated already. (Doing so would also make it impossible to reference in
+ // a subsequent VM run.)
+ if (original_spi == common_hal_board_get_spi()) {
+ continue;
+ }
+ #endif
memcpy(&fourwire->inline_bus, original_spi, sizeof(busio_spi_obj_t));
fourwire->bus = &fourwire->inline_bus;
// Check for other displays that use the same spi bus and swap them too.