Skip to content

Commit 1c1df05

Browse files
committed
Add neopixel support
1 parent 7be9837 commit 1c1df05

File tree

10 files changed

+217
-5
lines changed

10 files changed

+217
-5
lines changed

ports/esp32s2/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ SRC_C += \
171171
lib/utils/stdout_helpers.c \
172172
lib/utils/sys_stdio_mphal.c \
173173
peripherals/pins.c \
174+
peripherals/rmt.c \
174175
supervisor/shared/memory.c
175176

176177
ifneq ($(USB),FALSE)

ports/esp32s2/boards/espressif_saola_1_wrover/mpconfigboard.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,6 @@
2929
#define MICROPY_HW_BOARD_NAME "Saola 1 w/Wrover"
3030
#define MICROPY_HW_MCU_NAME "ESP32S2"
3131

32+
#define MICROPY_HW_NEOPIXEL (&pin_GPIO18)
33+
3234
#define AUTORESET_DELAY_MS 500

ports/esp32s2/boards/espressif_saola_1_wrover/pins.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,7 @@ STATIC const mp_rom_map_elem_t board_global_dict_table[] = {
4242
{ MP_ROM_QSTR(MP_QSTR_IO44), MP_ROM_PTR(&pin_GPIO44) },
4343
{ MP_ROM_QSTR(MP_QSTR_IO45), MP_ROM_PTR(&pin_GPIO45) },
4444
{ MP_ROM_QSTR(MP_QSTR_IO46), MP_ROM_PTR(&pin_GPIO46) },
45+
46+
{ MP_ROM_QSTR(MP_QSTR_NEOPIXEL), MP_ROM_PTR(&pin_GPIO18) },
4547
};
4648
MP_DEFINE_CONST_DICT(board_module_globals, board_global_dict_table);

ports/esp32s2/common-hal/microcontroller/Pin.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,18 @@
2626
*/
2727

2828
#include "shared-bindings/microcontroller/Pin.h"
29+
#include "shared-bindings/digitalio/DigitalInOut.h"
30+
#include "supervisor/shared/rgb_led_status.h"
2931

3032
#include "py/mphal.h"
3133

3234
#include "esp-idf/components/driver/include/driver/gpio.h"
3335
#include "esp-idf/components/soc/include/hal/gpio_hal.h"
3436

37+
#ifdef MICROPY_HW_NEOPIXEL
38+
bool neopixel_in_use;
39+
#endif
40+
3541
STATIC uint32_t never_reset_pins[2];
3642
STATIC uint32_t in_use[2];
3743

@@ -50,6 +56,14 @@ void common_hal_never_reset_pin(const mcu_pin_obj_t* pin) {
5056
void reset_pin_number(gpio_num_t pin_number) {
5157
never_reset_pins[pin_number / 32] &= ~(1 << pin_number % 32);
5258
in_use[pin_number / 32] &= ~(1 << pin_number % 32);
59+
60+
#ifdef MICROPY_HW_NEOPIXEL
61+
if (pin_number == MICROPY_HW_NEOPIXEL->number) {
62+
neopixel_in_use = false;
63+
rgb_led_status_init();
64+
return;
65+
}
66+
#endif
5367
}
5468

5569
void common_hal_reset_pin(const mcu_pin_obj_t* pin) {
@@ -69,13 +83,28 @@ void reset_all_pins(void) {
6983
}
7084
in_use[0] = 0;
7185
in_use[1] = 0;
86+
87+
#ifdef MICROPY_HW_NEOPIXEL
88+
neopixel_in_use = false;
89+
#endif
7290
}
7391

7492
void claim_pin(const mcu_pin_obj_t* pin) {
7593
in_use[pin->number / 32] |= (1 << pin->number % 32);
94+
#ifdef MICROPY_HW_NEOPIXEL
95+
if (pin == MICROPY_HW_NEOPIXEL) {
96+
neopixel_in_use = true;
97+
}
98+
#endif
7699
}
77100

78101
bool pin_number_is_free(gpio_num_t pin_number) {
102+
#ifdef MICROPY_HW_NEOPIXEL
103+
if (pin_number == MICROPY_HW_NEOPIXEL->number) {
104+
return !neopixel_in_use;
105+
}
106+
#endif
107+
79108
uint8_t offset = pin_number / 32;
80109
uint8_t mask = 1 << pin_number % 32;
81110
return (never_reset_pins[offset] & mask) == 0 && (in_use[offset] & mask) == 0;

ports/esp32s2/common-hal/microcontroller/Pin.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@
3434
extern bool apa102_mosi_in_use;
3535
extern bool apa102_sck_in_use;
3636

37+
#ifdef MICROPY_HW_NEOPIXEL
38+
extern bool neopixel_in_use;
39+
#endif
40+
3741
void reset_all_pins(void);
3842
// reset_pin_number takes the pin number instead of the pointer so that objects don't
3943
// need to store a full pointer.

ports/esp32s2/common-hal/neopixel_write/__init__.c

Lines changed: 94 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* The MIT License (MIT)
55
*
6-
* Copyright (c) 2018 hathach for Adafruit Industries
6+
* Copyright (c) 2020 Lucian Copeland for Adafruit Industries
77
*
88
* Permission is hereby granted, free of charge, to any person obtaining a copy
99
* of this software and associated documentation files (the "Software"), to deal
@@ -24,10 +24,101 @@
2424
* THE SOFTWARE.
2525
*/
2626

27+
/* Uses code from Espressif RGB LED Strip demo and drivers
28+
* Copyright 2015-2020 Espressif Systems (Shanghai) PTE LTD
29+
*
30+
* Licensed under the Apache License, Version 2.0 (the "License");
31+
* you may not use this file except in compliance with the License.
32+
* You may obtain a copy of the License at
33+
*
34+
* http://www.apache.org/licenses/LICENSE-2.0
35+
*
36+
* Unless required by applicable law or agreed to in writing, software
37+
* distributed under the License is distributed on an "AS IS" BASIS,
38+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
39+
* See the License for the specific language governing permissions and
40+
* limitations under the License.
41+
*/
42+
2743
#include "py/mphal.h"
2844
#include "shared-bindings/neopixel_write/__init__.h"
45+
#include "driver/rmt.h"
46+
#include "rmt.h"
47+
48+
#define WS2812_T0H_NS (350)
49+
#define WS2812_T0L_NS (1000)
50+
#define WS2812_T1H_NS (1000)
51+
#define WS2812_T1L_NS (350)
52+
#define WS2812_RESET_US (280)
53+
54+
static uint32_t ws2812_t0h_ticks = 0;
55+
static uint32_t ws2812_t1h_ticks = 0;
56+
static uint32_t ws2812_t0l_ticks = 0;
57+
static uint32_t ws2812_t1l_ticks = 0;
58+
59+
static void IRAM_ATTR ws2812_rmt_adapter(const void *src, rmt_item32_t *dest, size_t src_size,
60+
size_t wanted_num, size_t *translated_size, size_t *item_num)
61+
{
62+
if (src == NULL || dest == NULL) {
63+
*translated_size = 0;
64+
*item_num = 0;
65+
return;
66+
}
67+
const rmt_item32_t bit0 = {{{ ws2812_t0h_ticks, 1, ws2812_t0l_ticks, 0 }}}; //Logical 0
68+
const rmt_item32_t bit1 = {{{ ws2812_t1h_ticks, 1, ws2812_t1l_ticks, 0 }}}; //Logical 1
69+
size_t size = 0;
70+
size_t num = 0;
71+
uint8_t *psrc = (uint8_t *)src;
72+
rmt_item32_t *pdest = dest;
73+
while (size < src_size && num < wanted_num) {
74+
for (int i = 0; i < 8; i++) {
75+
// MSB first
76+
if (*psrc & (1 << (7 - i))) {
77+
pdest->val = bit1.val;
78+
} else {
79+
pdest->val = bit0.val;
80+
}
81+
num++;
82+
pdest++;
83+
}
84+
size++;
85+
psrc++;
86+
}
87+
*translated_size = size;
88+
*item_num = num;
89+
}
2990

3091
void common_hal_neopixel_write (const digitalio_digitalinout_obj_t* digitalinout, uint8_t *pixels, uint32_t numBytes) {
31-
(void)digitalinout;
32-
(void)numBytes;
92+
// Reserve channel
93+
uint8_t number = digitalinout->pin->number;
94+
rmt_channel_t channel = esp32s2_peripherals_find_and_reserve_rmt();
95+
96+
// Configure Channel
97+
rmt_config_t config = RMT_DEFAULT_CONFIG_TX(number, channel);
98+
config.clk_div = 2; // set counter clock to 40MHz
99+
rmt_config(&config);
100+
rmt_driver_install(config.channel, 0, 0);
101+
102+
// Convert NS timings to ticks
103+
uint32_t counter_clk_hz = 0;
104+
if (rmt_get_counter_clock(config.channel, &counter_clk_hz) != ESP_OK) {
105+
mp_raise_ValueError(translate("Could not retrieve clock"));
106+
}
107+
float ratio = (float)counter_clk_hz / 1e9;
108+
ws2812_t0h_ticks = (uint32_t)(ratio * WS2812_T0H_NS);
109+
ws2812_t0l_ticks = (uint32_t)(ratio * WS2812_T0L_NS);
110+
ws2812_t1h_ticks = (uint32_t)(ratio * WS2812_T1H_NS);
111+
ws2812_t1l_ticks = (uint32_t)(ratio * WS2812_T1L_NS);
112+
113+
// Initialize automatic timing translator
114+
rmt_translator_init(config.channel, ws2812_rmt_adapter);
115+
116+
// Write and wait to finish
117+
if(rmt_write_sample(config.channel, pixels, (size_t)numBytes, true) != ESP_OK) {
118+
mp_raise_ValueError(translate("Input/output error"));
119+
}
120+
rmt_wait_tx_done(config.channel, pdMS_TO_TICKS(100));
121+
122+
// Free channel again
123+
esp32s2_peripherals_free_rmt(config.channel);
33124
}

ports/esp32s2/common-hal/pulseio/PWMOut.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
*
44
* The MIT License (MIT)
55
*
6-
* Copyright (c) 2019 Lucian Copeland for Adafruit Industries
7-
* Uses code from Micropython, Copyright (c) 2013-2016 Damien P. George
6+
* Copyright (c) 2020 Lucian Copeland for Adafruit Industries
87
*
98
* Permission is hereby granted, free of charge, to any person obtaining a copy
109
* of this software and associated documentation files (the "Software"), to deal

ports/esp32s2/mpconfigport.mk

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ USB_SERIAL_NUMBER_LENGTH = 12
1212
# Longints can be implemented as mpz, as longlong, or not
1313
LONGINT_IMPL = MPZ
1414

15+
CIRCUITPY_NEOPIXEL_WRITE = 1
16+
1517
CIRCUITPY_FULL_BUILD = 0
1618
CIRCUITPY_ANALOGIO = 0
1719
CIRCUITPY_AUDIOBUSIO = 0

ports/esp32s2/peripherals/rmt.c

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* This file is part of the Micro Python project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2020 Lucian Copeland for Adafruit Industries
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+
#include "rmt.h"
28+
#include "py/runtime.h"
29+
30+
bool rmt_reserved_channels[RMT_CHANNEL_MAX];
31+
32+
rmt_channel_t esp32s2_peripherals_find_and_reserve_rmt(void) {
33+
for (size_t i = 0; i < RMT_CHANNEL_MAX; i++) {
34+
if (!rmt_reserved_channels[i]) {
35+
rmt_reserved_channels[i] = true;
36+
return i;
37+
}
38+
}
39+
mp_raise_RuntimeError(translate("All timers in use"));
40+
return false;
41+
}
42+
43+
void esp32s2_peripherals_free_rmt(rmt_channel_t chan) {
44+
rmt_reserved_channels[chan] = false;
45+
}

ports/esp32s2/peripherals/rmt.h

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* This file is part of the Micro Python project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2020 Lucian Copeland for Adafruit Industries
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+
#ifndef MICROPY_INCLUDED_ESP32S2_PERIPHERALS_RMT_H
28+
#define MICROPY_INCLUDED_ESP32S2_PERIPHERALS_RMT_H
29+
30+
#include "py/mphal.h"
31+
#include "driver/rmt.h"
32+
#include <stdint.h>
33+
34+
rmt_channel_t esp32s2_peripherals_find_and_reserve_rmt(void);
35+
void esp32s2_peripherals_free_rmt(rmt_channel_t chan);
36+
37+
#endif

0 commit comments

Comments
 (0)