summaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorJim Mussared <jim.mussared@gmail.com>2021-08-11 14:26:23 +1000
committerDamien George <damien@micropython.org>2021-08-19 22:50:32 +1000
commit71f4faac2732d932dcc03bdbc9f80434e5757edb (patch)
treee23213639f2daa95983d12980ff9b9c91291f688
parente64cda529521feb41cda3cc80d1074217816ee6c (diff)
downloadmicropython-71f4faac2732d932dcc03bdbc9f80434e5757edb.tar.gz
micropython-71f4faac2732d932dcc03bdbc9f80434e5757edb.zip
esp32: Replace esp.neopixel with machine.bitstream.
Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
-rw-r--r--ports/esp32/espneopixel.c74
-rw-r--r--ports/esp32/machine_bitstream.c88
-rw-r--r--ports/esp32/main/CMakeLists.txt2
-rw-r--r--ports/esp32/modesp.c10
-rw-r--r--ports/esp32/modmachine.c6
-rw-r--r--ports/esp32/modules/neopixel.py19
-rw-r--r--ports/esp32/mpconfigport.h1
7 files changed, 110 insertions, 90 deletions
diff --git a/ports/esp32/espneopixel.c b/ports/esp32/espneopixel.c
deleted file mode 100644
index 0b9308e2c0..0000000000
--- a/ports/esp32/espneopixel.c
+++ /dev/null
@@ -1,74 +0,0 @@
-// Original version from https://github.com/adafruit/Adafruit_NeoPixel
-// Modifications by dpgeorge to support auto-CPU-frequency detection
-
-// This is a mash-up of the Due show() code + insights from Michael Miller's
-// ESP8266 work for the NeoPixelBus library: github.com/Makuna/NeoPixelBus
-// Needs to be a separate .c file to enforce ICACHE_RAM_ATTR execution.
-
-#include "py/mpconfig.h"
-#include "py/mphal.h"
-#include "modesp.h"
-
-void IRAM_ATTR esp_neopixel_write(uint8_t pin, uint8_t *pixels, uint32_t numBytes, uint8_t timing) {
- uint8_t *p, *end, pix, mask;
- uint32_t t, time0, time1, period, c, startTime, pinMask, gpio_reg_set, gpio_reg_clear;
-
- #if !CONFIG_IDF_TARGET_ESP32C3
- if (pin >= 32) {
- pinMask = 1 << (pin - 32);
- gpio_reg_set = GPIO_OUT1_W1TS_REG;
- gpio_reg_clear = GPIO_OUT1_W1TC_REG;
- } else
- #endif
- {
- pinMask = 1 << pin;
- gpio_reg_set = GPIO_OUT_W1TS_REG;
- gpio_reg_clear = GPIO_OUT_W1TC_REG;
- }
- p = pixels;
- end = p + numBytes;
- pix = *p++;
- mask = 0x80;
- startTime = 0;
-
- uint32_t fcpu = ets_get_cpu_frequency();
-
- if (timing == 1) {
- // 800 KHz
- time0 = (fcpu * 350) / 1000; // 0.35us
- time1 = (fcpu * 800) / 1000; // 0.8us
- period = (fcpu * 1250) / 1000; // 1.25us per bit
- } else {
- // 400 KHz
- time0 = (fcpu * 500) / 1000; // 0.5us
- time1 = (fcpu * 1200) / 1000; // 1.2us
- period = (fcpu * 2500) / 1000; // 2.5us per bit
- }
-
- uint32_t irq_state = mp_hal_quiet_timing_enter();
- for (t = time0;; t = time0) {
- if (pix & mask) {
- t = time1; // Bit high duration
- }
- while (((c = mp_hal_ticks_cpu()) - startTime) < period) {
- ; // Wait for bit start
- }
- GPIO_REG_WRITE(gpio_reg_set, pinMask); // Set high
- startTime = c; // Save start time
- while (((c = mp_hal_ticks_cpu()) - startTime) < t) {
- ; // Wait high duration
- }
- GPIO_REG_WRITE(gpio_reg_clear, pinMask); // Set low
- if (!(mask >>= 1)) { // Next bit/byte
- if (p >= end) {
- break;
- }
- pix = *p++;
- mask = 0x80;
- }
- }
- while ((mp_hal_ticks_cpu() - startTime) < period) {
- ; // Wait for last bit
- }
- mp_hal_quiet_timing_exit(irq_state);
-}
diff --git a/ports/esp32/machine_bitstream.c b/ports/esp32/machine_bitstream.c
new file mode 100644
index 0000000000..a60fe1b1c5
--- /dev/null
+++ b/ports/esp32/machine_bitstream.c
@@ -0,0 +1,88 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2021 Jim Mussared
+ *
+ * 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.
+ */
+
+// This is a translation of the cycle counter implementation in ports/stm32/machine_bitstream.c.
+
+#include "py/mpconfig.h"
+#include "py/mphal.h"
+
+#if MICROPY_PY_MACHINE_BITSTREAM
+
+#define NS_TICKS_OVERHEAD (6)
+
+void IRAM_ATTR machine_bitstream_high_low(mp_hal_pin_obj_t pin, uint32_t *timing_ns, const uint8_t *buf, size_t len) {
+ uint32_t pin_mask, gpio_reg_set, gpio_reg_clear;
+ #if !CONFIG_IDF_TARGET_ESP32C3
+ if (pin >= 32) {
+ pin_mask = 1 << (pin - 32);
+ gpio_reg_set = GPIO_OUT1_W1TS_REG;
+ gpio_reg_clear = GPIO_OUT1_W1TC_REG;
+ } else
+ #endif
+ {
+ pin_mask = 1 << pin;
+ gpio_reg_set = GPIO_OUT_W1TS_REG;
+ gpio_reg_clear = GPIO_OUT_W1TC_REG;
+ }
+
+ // Convert ns to cpu ticks [high_time_0, period_0, high_time_1, period_1].
+ uint32_t fcpu_mhz = ets_get_cpu_frequency();
+ for (size_t i = 0; i < 4; ++i) {
+ timing_ns[i] = fcpu_mhz * timing_ns[i] / 1000;
+ if (timing_ns[i] > NS_TICKS_OVERHEAD) {
+ timing_ns[i] -= NS_TICKS_OVERHEAD;
+ }
+ if (i % 2 == 1) {
+ // Convert low_time to period (i.e. add high_time).
+ timing_ns[i] += timing_ns[i - 1];
+ }
+ }
+
+ uint32_t irq_state = mp_hal_quiet_timing_enter();
+
+ for (size_t i = 0; i < len; ++i) {
+ uint8_t b = buf[i];
+ for (size_t j = 0; j < 8; ++j) {
+ GPIO_REG_WRITE(gpio_reg_set, pin_mask);
+ uint32_t start_ticks = mp_hal_ticks_cpu();
+ uint32_t *t = &timing_ns[b >> 6 & 2];
+ uint32_t end_ticks = start_ticks + t[0];
+ while (mp_hal_ticks_cpu() - start_ticks < t[0]) {
+ ;
+ }
+ GPIO_REG_WRITE(gpio_reg_clear, pin_mask);
+ b <<= 1;
+ end_ticks += t[1];
+ while (mp_hal_ticks_cpu() - start_ticks < t[1]) {
+ ;
+ }
+ }
+ }
+
+ mp_hal_quiet_timing_exit(irq_state);
+}
+
+#endif // MICROPY_PY_MACHINE_BITSTREAM
diff --git a/ports/esp32/main/CMakeLists.txt b/ports/esp32/main/CMakeLists.txt
index 1ca30b0cb9..0e7d6ff250 100644
--- a/ports/esp32/main/CMakeLists.txt
+++ b/ports/esp32/main/CMakeLists.txt
@@ -53,6 +53,7 @@ set(MICROPY_SOURCE_PORT
${PROJECT_DIR}/help.c
${PROJECT_DIR}/modutime.c
${PROJECT_DIR}/moduos.c
+ ${PROJECT_DIR}/machine_bitstream.c
${PROJECT_DIR}/machine_timer.c
${PROJECT_DIR}/machine_pin.c
${PROJECT_DIR}/machine_touchpad.c
@@ -74,7 +75,6 @@ set(MICROPY_SOURCE_PORT
${PROJECT_DIR}/esp32_rmt.c
${PROJECT_DIR}/esp32_ulp.c
${PROJECT_DIR}/modesp32.c
- ${PROJECT_DIR}/espneopixel.c
${PROJECT_DIR}/machine_hw_spi.c
${PROJECT_DIR}/machine_wdt.c
${PROJECT_DIR}/mpthreadport.c
diff --git a/ports/esp32/modesp.c b/ports/esp32/modesp.c
index 59a261e8c2..8a7f025754 100644
--- a/ports/esp32/modesp.c
+++ b/ports/esp32/modesp.c
@@ -112,15 +112,6 @@ STATIC mp_obj_t esp_gpio_matrix_out(size_t n_args, const mp_obj_t *args) {
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(esp_gpio_matrix_out_obj, 4, 4, esp_gpio_matrix_out);
-STATIC mp_obj_t esp_neopixel_write_(mp_obj_t pin, mp_obj_t buf, mp_obj_t timing) {
- mp_buffer_info_t bufinfo;
- mp_get_buffer_raise(buf, &bufinfo, MP_BUFFER_READ);
- esp_neopixel_write(mp_hal_get_pin_obj(pin),
- (uint8_t *)bufinfo.buf, bufinfo.len, mp_obj_get_int(timing));
- return mp_const_none;
-}
-STATIC MP_DEFINE_CONST_FUN_OBJ_3(esp_neopixel_write_obj, esp_neopixel_write_);
-
STATIC const mp_rom_map_elem_t esp_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_esp) },
@@ -135,7 +126,6 @@ STATIC const mp_rom_map_elem_t esp_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR_gpio_matrix_in), MP_ROM_PTR(&esp_gpio_matrix_in_obj) },
{ MP_ROM_QSTR(MP_QSTR_gpio_matrix_out), MP_ROM_PTR(&esp_gpio_matrix_out_obj) },
- { MP_ROM_QSTR(MP_QSTR_neopixel_write), MP_ROM_PTR(&esp_neopixel_write_obj) },
{ MP_ROM_QSTR(MP_QSTR_dht_readinto), MP_ROM_PTR(&dht_readinto_obj) },
// Constants for second arg of osdebug()
diff --git a/ports/esp32/modmachine.c b/ports/esp32/modmachine.c
index 34fe8600ae..5f94007d49 100644
--- a/ports/esp32/modmachine.c
+++ b/ports/esp32/modmachine.c
@@ -49,6 +49,7 @@
#include "py/obj.h"
#include "py/runtime.h"
#include "shared/runtime/pyexec.h"
+#include "extmod/machine_bitstream.h"
#include "extmod/machine_mem.h"
#include "extmod/machine_signal.h"
#include "extmod/machine_pulse.h"
@@ -271,7 +272,12 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR_disable_irq), MP_ROM_PTR(&machine_disable_irq_obj) },
{ MP_ROM_QSTR(MP_QSTR_enable_irq), MP_ROM_PTR(&machine_enable_irq_obj) },
+ #if MICROPY_PY_MACHINE_BITSTREAM
+ { MP_ROM_QSTR(MP_QSTR_bitstream), MP_ROM_PTR(&machine_bitstream_obj) },
+ #endif
+ #if MICROPY_PY_MACHINE_PULSE
{ MP_ROM_QSTR(MP_QSTR_time_pulse_us), MP_ROM_PTR(&machine_time_pulse_us_obj) },
+ #endif
{ MP_ROM_QSTR(MP_QSTR_Timer), MP_ROM_PTR(&machine_timer_type) },
{ MP_ROM_QSTR(MP_QSTR_WDT), MP_ROM_PTR(&machine_wdt_type) },
diff --git a/ports/esp32/modules/neopixel.py b/ports/esp32/modules/neopixel.py
index f5c9193988..9ed5155c72 100644
--- a/ports/esp32/modules/neopixel.py
+++ b/ports/esp32/modules/neopixel.py
@@ -1,7 +1,12 @@
-# NeoPixel driver for MicroPython on ESP32
-# MIT license; Copyright (c) 2016 Damien P. George
+# NeoPixel driver for MicroPython
+# MIT license; Copyright (c) 2016 Damien P. George, 2021 Jim Mussared
-from esp import neopixel_write
+from micropython import const
+from machine import bitstream
+
+_BITSTREAM_TYPE_HIGH_LOW = const(0)
+_TIMING_WS2818_800 = (400, 850, 800, 450)
+_TIMING_WS2818_400 = (800, 1700, 1600, 900)
class NeoPixel:
@@ -13,7 +18,11 @@ class NeoPixel:
self.bpp = bpp
self.buf = bytearray(n * bpp)
self.pin.init(pin.OUT)
- self.timing = timing
+ self.timing = (
+ (_TIMING_WS2818_800 if timing else _TIMING_WS2818_400)
+ if isinstance(timing, int)
+ else timing
+ )
def __len__(self):
return self.n
@@ -32,4 +41,4 @@ class NeoPixel:
self[i] = color
def write(self):
- neopixel_write(self.pin, self.buf, self.timing)
+ bitstream(self.pin, _BITSTREAM_TYPE_HIGH_LOW, self.timing, self.buf)
diff --git a/ports/esp32/mpconfigport.h b/ports/esp32/mpconfigport.h
index a21c0d524d..057251fa49 100644
--- a/ports/esp32/mpconfigport.h
+++ b/ports/esp32/mpconfigport.h
@@ -156,6 +156,7 @@
#define MICROPY_PY_OS_DUPTERM (1)
#define MICROPY_PY_MACHINE (1)
#define MICROPY_PY_MACHINE_PIN_MAKE_NEW mp_pin_make_new
+#define MICROPY_PY_MACHINE_BITSTREAM (1)
#define MICROPY_PY_MACHINE_PULSE (1)
#define MICROPY_PY_MACHINE_I2C (1)
#define MICROPY_PY_MACHINE_SPI (1)