Skip to content

Commit 39e9c07

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

File tree

8 files changed

+97
-102
lines changed

8 files changed

+97
-102
lines changed

ports/esp8266/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,10 @@ SRC_C = \
9191
lexerstr32.c \
9292
uart.c \
9393
esppwm.c \
94-
espneopixel.c \
9594
espapa102.c \
9695
modpyb.c \
9796
modmachine.c \
97+
machine_bitstream.c \
9898
machine_pin.c \
9999
machine_pwm.c \
100100
machine_rtc.c \

ports/esp8266/espneopixel.c

-75
This file was deleted.

ports/esp8266/espneopixel.h

-6
This file was deleted.

ports/esp8266/machine_bitstream.c

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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 slightly modified from the implementation in ports/esp32/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 (8)
35+
36+
void 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 = 1 << pin;
38+
39+
// Convert ns to cpu ticks [high_time_0, period_0, high_time_1, period_1].
40+
uint32_t fcpu_mhz = system_get_cpu_freq();
41+
for (size_t i = 0; i < 4; ++i) {
42+
timing_ns[i] = fcpu_mhz * timing_ns[i] / 1000;
43+
if (timing_ns[i] > NS_TICKS_OVERHEAD) {
44+
timing_ns[i] -= NS_TICKS_OVERHEAD;
45+
}
46+
if (i % 2 == 1) {
47+
// Convert low_time to period (i.e. add high_time).
48+
timing_ns[i] += timing_ns[i - 1];
49+
}
50+
}
51+
52+
uint32_t irq_state = mp_hal_quiet_timing_enter();
53+
54+
for (size_t i = 0; i < len; ++i) {
55+
uint8_t b = buf[i];
56+
for (size_t j = 0; j < 8; ++j) {
57+
GPIO_REG_WRITE(GPIO_OUT_W1TS_ADDRESS, pin_mask);
58+
uint32_t start_ticks = mp_hal_ticks_cpu();
59+
uint32_t *t = &timing_ns[b >> 6 & 2];
60+
uint32_t end_ticks = start_ticks + t[0];
61+
while (mp_hal_ticks_cpu() - start_ticks < t[0]) {
62+
;
63+
}
64+
GPIO_REG_WRITE(GPIO_OUT_W1TC_ADDRESS, pin_mask);
65+
b <<= 1;
66+
end_ticks += t[1];
67+
while (mp_hal_ticks_cpu() - start_ticks < t[1]) {
68+
;
69+
}
70+
}
71+
}
72+
73+
mp_hal_quiet_timing_exit(irq_state);
74+
}
75+
76+
#endif // MICROPY_PY_MACHINE_BITSTREAM

ports/esp8266/modesp.c

-14
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
#include "user_interface.h"
3737
#include "mem.h"
3838
#include "ets_alt_task.h"
39-
#include "espneopixel.h"
4039
#include "espapa102.h"
4140
#include "modmachine.h"
4241

@@ -199,16 +198,6 @@ STATIC mp_obj_t esp_check_fw(void) {
199198
}
200199
STATIC MP_DEFINE_CONST_FUN_OBJ_0(esp_check_fw_obj, esp_check_fw);
201200

202-
203-
STATIC mp_obj_t esp_neopixel_write_(mp_obj_t pin, mp_obj_t buf, mp_obj_t is800k) {
204-
mp_buffer_info_t bufinfo;
205-
mp_get_buffer_raise(buf, &bufinfo, MP_BUFFER_READ);
206-
esp_neopixel_write(mp_obj_get_pin_obj(pin)->phys_port,
207-
(uint8_t *)bufinfo.buf, bufinfo.len, mp_obj_is_true(is800k));
208-
return mp_const_none;
209-
}
210-
STATIC MP_DEFINE_CONST_FUN_OBJ_3(esp_neopixel_write_obj, esp_neopixel_write_);
211-
212201
#if MICROPY_ESP8266_APA102
213202
STATIC mp_obj_t esp_apa102_write_(mp_obj_t clockPin, mp_obj_t dataPin, mp_obj_t buf) {
214203
mp_buffer_info_t bufinfo;
@@ -363,9 +352,6 @@ STATIC const mp_rom_map_elem_t esp_module_globals_table[] = {
363352
{ MP_ROM_QSTR(MP_QSTR_flash_erase), MP_ROM_PTR(&esp_flash_erase_obj) },
364353
{ MP_ROM_QSTR(MP_QSTR_flash_size), MP_ROM_PTR(&esp_flash_size_obj) },
365354
{ MP_ROM_QSTR(MP_QSTR_flash_user_start), MP_ROM_PTR(&esp_flash_user_start_obj) },
366-
#if MICROPY_ESP8266_NEOPIXEL
367-
{ MP_ROM_QSTR(MP_QSTR_neopixel_write), MP_ROM_PTR(&esp_neopixel_write_obj) },
368-
#endif
369355
#if MICROPY_ESP8266_APA102
370356
{ MP_ROM_QSTR(MP_QSTR_apa102_write), MP_ROM_PTR(&esp_apa102_write_obj) },
371357
#endif

ports/esp8266/modmachine.c

+5
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
// This needs to be set before we include the RTOS headers
3636
#define USE_US_TIMER 1
3737

38+
#include "extmod/machine_bitstream.h"
3839
#include "extmod/machine_mem.h"
3940
#include "extmod/machine_signal.h"
4041
#include "extmod/machine_pulse.h"
@@ -411,6 +412,10 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = {
411412
{ MP_ROM_QSTR(MP_QSTR_disable_irq), MP_ROM_PTR(&machine_disable_irq_obj) },
412413
{ MP_ROM_QSTR(MP_QSTR_enable_irq), MP_ROM_PTR(&machine_enable_irq_obj) },
413414

415+
#if MICROPY_PY_MACHINE_BITSTREAM
416+
{ MP_ROM_QSTR(MP_QSTR_bitstream), MP_ROM_PTR(&machine_bitstream_obj) },
417+
#endif
418+
414419
{ MP_ROM_QSTR(MP_QSTR_time_pulse_us), MP_ROM_PTR(&machine_time_pulse_us_obj) },
415420

416421
{ MP_ROM_QSTR(MP_QSTR_RTC), MP_ROM_PTR(&pyb_rtc_type) },

ports/esp8266/modules/neopixel.py

+14-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1-
# NeoPixel driver for MicroPython on ESP8266
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/esp8266/mpconfigport.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
#define MICROPY_PY_LWIP_SOCK_RAW (1)
8080
#define MICROPY_PY_MACHINE (1)
8181
#define MICROPY_PY_MACHINE_PIN_MAKE_NEW mp_pin_make_new
82+
#define MICROPY_PY_MACHINE_BITSTREAM (1)
8283
#define MICROPY_PY_MACHINE_PULSE (1)
8384
#define MICROPY_PY_MACHINE_I2C (1)
8485
#define MICROPY_PY_MACHINE_SPI (1)
@@ -102,7 +103,6 @@
102103
#define MICROPY_FATFS_MAX_SS (4096)
103104
#define MICROPY_FATFS_LFN_CODE_PAGE 437 /* 1=SFN/ANSI 437=LFN/U.S.(OEM) */
104105
#define MICROPY_ESP8266_APA102 (1)
105-
#define MICROPY_ESP8266_NEOPIXEL (1)
106106

107107
#define MICROPY_EVENT_POLL_HOOK {ets_event_poll();}
108108
#define MICROPY_VM_HOOK_COUNT (10)

0 commit comments

Comments
 (0)