Skip to content

Commit 71f4faa

Browse files
jimmodpgeorge
authored andcommitted
esp32: Replace esp.neopixel with machine.bitstream.
Signed-off-by: Jim Mussared <[email protected]>
1 parent e64cda5 commit 71f4faa

File tree

7 files changed

+110
-90
lines changed

7 files changed

+110
-90
lines changed

ports/esp32/espneopixel.c

-74
This file was deleted.

ports/esp32/machine_bitstream.c

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2021 Jim Mussared
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
// This is a translation of the cycle counter implementation in ports/stm32/machine_bitstream.c.
28+
29+
#include "py/mpconfig.h"
30+
#include "py/mphal.h"
31+
32+
#if MICROPY_PY_MACHINE_BITSTREAM
33+
34+
#define NS_TICKS_OVERHEAD (6)
35+
36+
void IRAM_ATTR machine_bitstream_high_low(mp_hal_pin_obj_t pin, uint32_t *timing_ns, const uint8_t *buf, size_t len) {
37+
uint32_t pin_mask, gpio_reg_set, gpio_reg_clear;
38+
#if !CONFIG_IDF_TARGET_ESP32C3
39+
if (pin >= 32) {
40+
pin_mask = 1 << (pin - 32);
41+
gpio_reg_set = GPIO_OUT1_W1TS_REG;
42+
gpio_reg_clear = GPIO_OUT1_W1TC_REG;
43+
} else
44+
#endif
45+
{
46+
pin_mask = 1 << pin;
47+
gpio_reg_set = GPIO_OUT_W1TS_REG;
48+
gpio_reg_clear = GPIO_OUT_W1TC_REG;
49+
}
50+
51+
// Convert ns to cpu ticks [high_time_0, period_0, high_time_1, period_1].
52+
uint32_t fcpu_mhz = ets_get_cpu_frequency();
53+
for (size_t i = 0; i < 4; ++i) {
54+
timing_ns[i] = fcpu_mhz * timing_ns[i] / 1000;
55+
if (timing_ns[i] > NS_TICKS_OVERHEAD) {
56+
timing_ns[i] -= NS_TICKS_OVERHEAD;
57+
}
58+
if (i % 2 == 1) {
59+
// Convert low_time to period (i.e. add high_time).
60+
timing_ns[i] += timing_ns[i - 1];
61+
}
62+
}
63+
64+
uint32_t irq_state = mp_hal_quiet_timing_enter();
65+
66+
for (size_t i = 0; i < len; ++i) {
67+
uint8_t b = buf[i];
68+
for (size_t j = 0; j < 8; ++j) {
69+
GPIO_REG_WRITE(gpio_reg_set, pin_mask);
70+
uint32_t start_ticks = mp_hal_ticks_cpu();
71+
uint32_t *t = &timing_ns[b >> 6 & 2];
72+
uint32_t end_ticks = start_ticks + t[0];
73+
while (mp_hal_ticks_cpu() - start_ticks < t[0]) {
74+
;
75+
}
76+
GPIO_REG_WRITE(gpio_reg_clear, pin_mask);
77+
b <<= 1;
78+
end_ticks += t[1];
79+
while (mp_hal_ticks_cpu() - start_ticks < t[1]) {
80+
;
81+
}
82+
}
83+
}
84+
85+
mp_hal_quiet_timing_exit(irq_state);
86+
}
87+
88+
#endif // MICROPY_PY_MACHINE_BITSTREAM

ports/esp32/main/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ set(MICROPY_SOURCE_PORT
5353
${PROJECT_DIR}/help.c
5454
${PROJECT_DIR}/modutime.c
5555
${PROJECT_DIR}/moduos.c
56+
${PROJECT_DIR}/machine_bitstream.c
5657
${PROJECT_DIR}/machine_timer.c
5758
${PROJECT_DIR}/machine_pin.c
5859
${PROJECT_DIR}/machine_touchpad.c
@@ -74,7 +75,6 @@ set(MICROPY_SOURCE_PORT
7475
${PROJECT_DIR}/esp32_rmt.c
7576
${PROJECT_DIR}/esp32_ulp.c
7677
${PROJECT_DIR}/modesp32.c
77-
${PROJECT_DIR}/espneopixel.c
7878
${PROJECT_DIR}/machine_hw_spi.c
7979
${PROJECT_DIR}/machine_wdt.c
8080
${PROJECT_DIR}/mpthreadport.c

ports/esp32/modesp.c

-10
Original file line numberDiff line numberDiff line change
@@ -112,15 +112,6 @@ STATIC mp_obj_t esp_gpio_matrix_out(size_t n_args, const mp_obj_t *args) {
112112
}
113113
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(esp_gpio_matrix_out_obj, 4, 4, esp_gpio_matrix_out);
114114

115-
STATIC mp_obj_t esp_neopixel_write_(mp_obj_t pin, mp_obj_t buf, mp_obj_t timing) {
116-
mp_buffer_info_t bufinfo;
117-
mp_get_buffer_raise(buf, &bufinfo, MP_BUFFER_READ);
118-
esp_neopixel_write(mp_hal_get_pin_obj(pin),
119-
(uint8_t *)bufinfo.buf, bufinfo.len, mp_obj_get_int(timing));
120-
return mp_const_none;
121-
}
122-
STATIC MP_DEFINE_CONST_FUN_OBJ_3(esp_neopixel_write_obj, esp_neopixel_write_);
123-
124115
STATIC const mp_rom_map_elem_t esp_module_globals_table[] = {
125116
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_esp) },
126117

@@ -135,7 +126,6 @@ STATIC const mp_rom_map_elem_t esp_module_globals_table[] = {
135126
{ MP_ROM_QSTR(MP_QSTR_gpio_matrix_in), MP_ROM_PTR(&esp_gpio_matrix_in_obj) },
136127
{ MP_ROM_QSTR(MP_QSTR_gpio_matrix_out), MP_ROM_PTR(&esp_gpio_matrix_out_obj) },
137128

138-
{ MP_ROM_QSTR(MP_QSTR_neopixel_write), MP_ROM_PTR(&esp_neopixel_write_obj) },
139129
{ MP_ROM_QSTR(MP_QSTR_dht_readinto), MP_ROM_PTR(&dht_readinto_obj) },
140130

141131
// Constants for second arg of osdebug()

ports/esp32/modmachine.c

+6
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
#include "py/obj.h"
5050
#include "py/runtime.h"
5151
#include "shared/runtime/pyexec.h"
52+
#include "extmod/machine_bitstream.h"
5253
#include "extmod/machine_mem.h"
5354
#include "extmod/machine_signal.h"
5455
#include "extmod/machine_pulse.h"
@@ -271,7 +272,12 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = {
271272
{ MP_ROM_QSTR(MP_QSTR_disable_irq), MP_ROM_PTR(&machine_disable_irq_obj) },
272273
{ MP_ROM_QSTR(MP_QSTR_enable_irq), MP_ROM_PTR(&machine_enable_irq_obj) },
273274

275+
#if MICROPY_PY_MACHINE_BITSTREAM
276+
{ MP_ROM_QSTR(MP_QSTR_bitstream), MP_ROM_PTR(&machine_bitstream_obj) },
277+
#endif
278+
#if MICROPY_PY_MACHINE_PULSE
274279
{ MP_ROM_QSTR(MP_QSTR_time_pulse_us), MP_ROM_PTR(&machine_time_pulse_us_obj) },
280+
#endif
275281

276282
{ MP_ROM_QSTR(MP_QSTR_Timer), MP_ROM_PTR(&machine_timer_type) },
277283
{ MP_ROM_QSTR(MP_QSTR_WDT), MP_ROM_PTR(&machine_wdt_type) },

ports/esp32/modules/neopixel.py

+14-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1-
# NeoPixel driver for MicroPython on ESP32
2-
# MIT license; Copyright (c) 2016 Damien P. George
1+
# NeoPixel driver for MicroPython
2+
# MIT license; Copyright (c) 2016 Damien P. George, 2021 Jim Mussared
33

4-
from esp import neopixel_write
4+
from micropython import const
5+
from machine import bitstream
6+
7+
_BITSTREAM_TYPE_HIGH_LOW = const(0)
8+
_TIMING_WS2818_800 = (400, 850, 800, 450)
9+
_TIMING_WS2818_400 = (800, 1700, 1600, 900)
510

611

712
class NeoPixel:
@@ -13,7 +18,11 @@ def __init__(self, pin, n, bpp=3, timing=1):
1318
self.bpp = bpp
1419
self.buf = bytearray(n * bpp)
1520
self.pin.init(pin.OUT)
16-
self.timing = timing
21+
self.timing = (
22+
(_TIMING_WS2818_800 if timing else _TIMING_WS2818_400)
23+
if isinstance(timing, int)
24+
else timing
25+
)
1726

1827
def __len__(self):
1928
return self.n
@@ -32,4 +41,4 @@ def fill(self, color):
3241
self[i] = color
3342

3443
def write(self):
35-
neopixel_write(self.pin, self.buf, self.timing)
44+
bitstream(self.pin, _BITSTREAM_TYPE_HIGH_LOW, self.timing, self.buf)

ports/esp32/mpconfigport.h

+1
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@
156156
#define MICROPY_PY_OS_DUPTERM (1)
157157
#define MICROPY_PY_MACHINE (1)
158158
#define MICROPY_PY_MACHINE_PIN_MAKE_NEW mp_pin_make_new
159+
#define MICROPY_PY_MACHINE_BITSTREAM (1)
159160
#define MICROPY_PY_MACHINE_PULSE (1)
160161
#define MICROPY_PY_MACHINE_I2C (1)
161162
#define MICROPY_PY_MACHINE_SPI (1)

0 commit comments

Comments
 (0)