From 11288c1c28f2101d979fa7b214e7cc18f3e0bb08 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Thu, 13 May 2021 11:19:55 -0500 Subject: [PATCH] esp32s2: Add rgbmatrix support --- lib/protomatter | 2 +- ports/esp32s2/Makefile | 2 + .../esp32s2/common-hal/rgbmatrix/RGBMatrix.c | 75 +++++++++++++++++++ .../esp32s2/common-hal/rgbmatrix/RGBMatrix.h | 37 +++++++++ ports/esp32s2/common-hal/rgbmatrix/__init__.c | 0 ports/esp32s2/common-hal/rgbmatrix/__init__.h | 0 ports/esp32s2/mpconfigport.mk | 2 + ports/esp32s2/peripherals/timer.c | 22 +++++- ports/esp32s2/peripherals/timer.h | 3 +- shared-module/rgbmatrix/RGBMatrix.c | 8 +- 10 files changed, 143 insertions(+), 8 deletions(-) create mode 100644 ports/esp32s2/common-hal/rgbmatrix/RGBMatrix.c create mode 100644 ports/esp32s2/common-hal/rgbmatrix/RGBMatrix.h create mode 100644 ports/esp32s2/common-hal/rgbmatrix/__init__.c create mode 100644 ports/esp32s2/common-hal/rgbmatrix/__init__.h diff --git a/lib/protomatter b/lib/protomatter index 98017c5734..7fe6406aff 160000 --- a/lib/protomatter +++ b/lib/protomatter @@ -1 +1 @@ -Subproject commit 98017c57349e259fab70c6a7830436b19a55f6f4 +Subproject commit 7fe6406affb1376193102cf76ded06e61316d7e6 diff --git a/ports/esp32s2/Makefile b/ports/esp32s2/Makefile index 85d8e04332..201989e0c9 100644 --- a/ports/esp32s2/Makefile +++ b/ports/esp32s2/Makefile @@ -222,6 +222,8 @@ OBJ += $(addprefix $(BUILD)/, $(SRC_MOD:.c=.o)) $(BUILD)/$(FATFS_DIR)/ff.o: COPT += -Os $(filter $(PY_BUILD)/../extmod/vfs_fat_%.o, $(PY_O)): COPT += -Os +$(BUILD)/lib/protomatter/src/core.o: CFLAGS += -DESP32 + # List of sources for qstr extraction SRC_QSTR += $(SRC_C) $(SRC_SUPERVISOR) $(SRC_MOD) $(SRC_COMMON_HAL_EXPANDED) $(SRC_SHARED_MODULE_EXPANDED) # Sources that only hold QSTRs after pre-processing. diff --git a/ports/esp32s2/common-hal/rgbmatrix/RGBMatrix.c b/ports/esp32s2/common-hal/rgbmatrix/RGBMatrix.c new file mode 100644 index 0000000000..d42829985f --- /dev/null +++ b/ports/esp32s2/common-hal/rgbmatrix/RGBMatrix.c @@ -0,0 +1,75 @@ +/* + * This file is part of the Micro Python project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2021 Jeff Epler 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 + +#include "common-hal/rgbmatrix/RGBMatrix.h" + +#include "peripherals/timer.h" + +void *common_hal_rgbmatrix_timer_allocate(rgbmatrix_rgbmatrix_obj_t *self) { + const timer_config_t config = { + .alarm_en = false, + .counter_en = false, + .intr_type = TIMER_INTR_LEVEL, + .counter_dir = TIMER_COUNT_UP, + .auto_reload = true, + .divider = 2 // 40MHz + }; + + timer_index_t *timer = malloc(sizeof(timer_index_t)); + bool res = peripherals_timer_init(&config, timer); + if (!res) { + return NULL; + } + peripherals_timer_never_reset(timer); + return timer; +} + +extern bool _PM_esp32timerCallback(void *arg); + +void common_hal_rgbmatrix_timer_enable(void *ptr) { +} + +void common_hal_rgbmatrix_timer_disable(void *ptr) { + timer_index_t *timer = (timer_index_t *)ptr; + if (timer->idx == TIMER_MAX) { + return; + } + timer_pause(timer->group, timer->idx); + timer_disable_intr(timer->group, timer->idx); + timer_isr_callback_remove(timer->group, timer->idx); +} + +void common_hal_rgbmatrix_timer_free(void *ptr) { + timer_index_t *timer = (timer_index_t *)ptr; + if (timer->idx == TIMER_MAX) { + return; + } + common_hal_rgbmatrix_timer_disable(ptr); + peripherals_timer_deinit(timer); + timer->idx = TIMER_MAX; +} diff --git a/ports/esp32s2/common-hal/rgbmatrix/RGBMatrix.h b/ports/esp32s2/common-hal/rgbmatrix/RGBMatrix.h new file mode 100644 index 0000000000..d14cd9b083 --- /dev/null +++ b/ports/esp32s2/common-hal/rgbmatrix/RGBMatrix.h @@ -0,0 +1,37 @@ +/* + * This file is part of the Micro Python project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2020 Jeff Epler 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_ATMEL_SAMD_COMMON_HAL_RGBMATRIX_RGBMATRIX_H +#define MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_RGBMATRIX_RGBMATRIX_H + +#include "shared-module/rgbmatrix/RGBMatrix.h" + +void *common_hal_rgbmatrix_timer_allocate(rgbmatrix_rgbmatrix_obj_t *self); +void common_hal_rgbmatrix_timer_enable(void *); +void common_hal_rgbmatrix_timer_disable(void *); +void common_hal_rgbmatrix_timer_free(void *); + +#endif diff --git a/ports/esp32s2/common-hal/rgbmatrix/__init__.c b/ports/esp32s2/common-hal/rgbmatrix/__init__.c new file mode 100644 index 0000000000..e69de29bb2 diff --git a/ports/esp32s2/common-hal/rgbmatrix/__init__.h b/ports/esp32s2/common-hal/rgbmatrix/__init__.h new file mode 100644 index 0000000000..e69de29bb2 diff --git a/ports/esp32s2/mpconfigport.mk b/ports/esp32s2/mpconfigport.mk index a8dded05de..1f5021bc15 100644 --- a/ports/esp32s2/mpconfigport.mk +++ b/ports/esp32s2/mpconfigport.mk @@ -21,8 +21,10 @@ CIRCUITPY_AUDIOIO = 0 CIRCUITPY_CANIO = 1 CIRCUITPY_COUNTIO = 1 CIRCUITPY_DUALBANK = 1 +CIRCUITPY_FRAMEBUFFERIO = 1 CIRCUITPY_FREQUENCYIO = 1 CIRCUITPY_I2CPERIPHERAL = 0 +CIRCUITPY_RGBMATRIX = 1 CIRCUITPY_ROTARYIO = 1 CIRCUITPY_NVM = 1 CIRCUITPY_PS2IO ?= 1 diff --git a/ports/esp32s2/peripherals/timer.c b/ports/esp32s2/peripherals/timer.c index c280cba3ef..8e516756b6 100644 --- a/ports/esp32s2/peripherals/timer.c +++ b/ports/esp32s2/peripherals/timer.c @@ -26,6 +26,7 @@ #include "peripherals/timer.h" +#define TIMER_NEVER_RESET 2 #define TIMER_FREE 1 #define TIMER_BUSY 0 @@ -45,7 +46,7 @@ void peripherals_timer_reset(void) { } } -void peripherals_timer_init(const timer_config_t *config, timer_index_t *timer) { +bool peripherals_timer_init(const timer_config_t *config, timer_index_t *timer) { bool break_loop = false; // get free timer @@ -60,7 +61,7 @@ void peripherals_timer_init(const timer_config_t *config, timer_index_t *timer) } else if (i == 1 && j == 1) { timer->idx = TIMER_MAX; timer->group = TIMER_GROUP_MAX; - return; + return false; } } if (break_loop) { @@ -73,8 +74,25 @@ void peripherals_timer_init(const timer_config_t *config, timer_index_t *timer) // initialize timer module timer_init(timer->group, timer->idx, config); timer_set_counter_value(timer->group, timer->idx, 0); + + return true; } void peripherals_timer_deinit(timer_index_t *timer) { + if (timer->group == TIMER_GROUP_MAX || timer->idx == TIMER_MAX) { + return; + } timer_deinit(timer->group, timer->idx); + int i = timer->group; + int j = timer->idx; + timer->group = TIMER_GROUP_MAX; + timer->idx = TIMER_MAX; + timer_state[i][j] = TIMER_FREE; +} + +void peripherals_timer_never_reset(timer_index_t *timer) { + timer_deinit(timer->group, timer->idx); + int i = timer->group; + int j = timer->idx; + timer_state[i][j] = TIMER_NEVER_RESET; } diff --git a/ports/esp32s2/peripherals/timer.h b/ports/esp32s2/peripherals/timer.h index ad88976ec3..6869bd4352 100644 --- a/ports/esp32s2/peripherals/timer.h +++ b/ports/esp32s2/peripherals/timer.h @@ -35,8 +35,9 @@ typedef struct { timer_group_t group; } timer_index_t; -extern void peripherals_timer_init(const timer_config_t *config, timer_index_t *timer); +extern bool peripherals_timer_init(const timer_config_t *config, timer_index_t *timer); extern void peripherals_timer_deinit(timer_index_t *timer); extern void peripherals_timer_reset(void); +extern void peripherals_timer_never_reset(timer_index_t *timer); #endif // MICROPY_INCLUDED_ESP32S2_PERIPHERALS_TIMER_HANDLER_H diff --git a/shared-module/rgbmatrix/RGBMatrix.c b/shared-module/rgbmatrix/RGBMatrix.c index a0dc783b55..8346bbbeff 100644 --- a/shared-module/rgbmatrix/RGBMatrix.c +++ b/shared-module/rgbmatrix/RGBMatrix.c @@ -104,17 +104,17 @@ void common_hal_rgbmatrix_rgbmatrix_reconstruct(rgbmatrix_rgbmatrix_obj_t *self, if (stat == PROTOMATTER_OK) { _PM_protoPtr = &self->protomatter; - common_hal_mcu_disable_interrupts(); + // common_hal_mcu_disable_interrupts(); common_hal_rgbmatrix_timer_enable(self->timer); stat = _PM_begin(&self->protomatter); if (stat == PROTOMATTER_OK) { _PM_convert_565(&self->protomatter, self->bufinfo.buf, self->width); - } - common_hal_mcu_enable_interrupts(); - if (stat == PROTOMATTER_OK) { _PM_swapbuffer_maybe(&self->protomatter); } + // common_hal_mcu_enable_interrupts(); + // if (stat == PROTOMATTER_OK) { + // } } if (stat != PROTOMATTER_OK) {