Skip to content

Commit a3bbd53

Browse files
committed
esp32/machine_bitstream: Reinstate bitstream bit-bang implementation.
The bit-bang implementation was replaced with the RMT implementation in 599b61c. This commit brings back that bit-bang code, and allows it to be selected via the new static method: esp32.RMT.bitstream_channel(None) The bit-bang implementation may be useful if the RMT needs to be used for something else, or if bit-banging is more stable in certain applications. Signed-off-by: Damien George <[email protected]>
1 parent e754c2e commit a3bbd53

File tree

5 files changed

+119
-9
lines changed

5 files changed

+119
-9
lines changed

Diff for: docs/esp32/quickref.rst

+3
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,9 @@ For low-level driving of a NeoPixel::
573573
400kHz) devices by passing ``timing=0`` when constructing the
574574
``NeoPixel`` object.
575575

576+
The low-level driver uses an RMT channel by default. To configure this see
577+
`RMT.bitstream_channel`.
578+
576579
APA102 (DotStar) uses a different driver as it has an additional clock pin.
577580

578581
Capacitive touch

Diff for: docs/library/esp32.rst

+11
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,17 @@ For more details see Espressif's `ESP-IDF RMT documentation.
250250
new sequence of pulses. Looping sequences longer than 126 pulses is not
251251
supported by the hardware.
252252

253+
.. staticmethod:: RMT.bitstream_channel([value])
254+
255+
Select which RMT channel is used by the `machine.bitstream` implementation.
256+
*value* can be ``None`` or a valid RMT channel number. The default RMT
257+
channel is the highest numbered one.
258+
259+
Passing in ``None`` disables the use of RMT and instead selects a bit-banging
260+
implementation for `machine.bitstream`.
261+
262+
Passing in no argument will not change the channel. This function returns
263+
the current channel number.
253264

254265
Ultra-Low-Power co-processor
255266
----------------------------

Diff for: ports/esp32/esp32_rmt.c

+30-2
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ typedef struct _rmt_install_state_t {
6666
esp_err_t ret;
6767
} rmt_install_state_t;
6868

69+
// Current channel used for machine.bitstream, in the machine_bitstream_high_low_rmt
70+
// implementation. A value of -1 means do not use RMT.
71+
int8_t esp32_rmt_bitstream_channel_id = RMT_CHANNEL_MAX - 1;
72+
6973
STATIC void rmt_install_task(void *pvParameter) {
7074
rmt_install_state_t *state = pvParameter;
7175
state->ret = rmt_driver_install(state->channel_id, 0, 0);
@@ -104,8 +108,8 @@ STATIC mp_obj_t esp32_rmt_make_new(const mp_obj_type_t *type, size_t n_args, siz
104108
mp_uint_t idle_level = args[3].u_bool;
105109
mp_obj_t tx_carrier_obj = args[4].u_obj;
106110

107-
if (channel_id == MICROPY_HW_ESP32_RMT_CHANNEL_BITSTREAM) {
108-
mp_raise_ValueError(MP_ERROR_TEXT("reserved channel id"));
111+
if (esp32_rmt_bitstream_channel_id >= 0 && channel_id == esp32_rmt_bitstream_channel_id) {
112+
mp_raise_ValueError(MP_ERROR_TEXT("channel used by bitstream"));
109113
}
110114

111115
if (clock_div < 1 || clock_div > 255) {
@@ -314,6 +318,27 @@ STATIC mp_obj_t esp32_rmt_write_pulses(size_t n_args, const mp_obj_t *args) {
314318
}
315319
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(esp32_rmt_write_pulses_obj, 2, 3, esp32_rmt_write_pulses);
316320

321+
STATIC mp_obj_t esp32_rmt_bitstream_channel(size_t n_args, const mp_obj_t *args) {
322+
if (n_args > 0) {
323+
if (args[0] == mp_const_none) {
324+
esp32_rmt_bitstream_channel_id = -1;
325+
} else {
326+
mp_int_t channel_id = mp_obj_get_int(args[0]);
327+
if (channel_id < 0 || channel_id >= RMT_CHANNEL_MAX) {
328+
mp_raise_ValueError(MP_ERROR_TEXT("invalid channel"));
329+
}
330+
esp32_rmt_bitstream_channel_id = channel_id;
331+
}
332+
}
333+
if (esp32_rmt_bitstream_channel_id < 0) {
334+
return mp_const_none;
335+
} else {
336+
return MP_OBJ_NEW_SMALL_INT(esp32_rmt_bitstream_channel_id);
337+
}
338+
}
339+
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(esp32_rmt_bitstream_channel_fun_obj, 0, 1, esp32_rmt_bitstream_channel);
340+
STATIC MP_DEFINE_CONST_STATICMETHOD_OBJ(esp32_rmt_bitstream_channel_obj, MP_ROM_PTR(&esp32_rmt_bitstream_channel_fun_obj));
341+
317342
STATIC const mp_rom_map_elem_t esp32_rmt_locals_dict_table[] = {
318343
{ MP_ROM_QSTR(MP_QSTR___del__), MP_ROM_PTR(&esp32_rmt_deinit_obj) },
319344
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&esp32_rmt_deinit_obj) },
@@ -322,6 +347,9 @@ STATIC const mp_rom_map_elem_t esp32_rmt_locals_dict_table[] = {
322347
{ MP_ROM_QSTR(MP_QSTR_wait_done), MP_ROM_PTR(&esp32_rmt_wait_done_obj) },
323348
{ MP_ROM_QSTR(MP_QSTR_loop), MP_ROM_PTR(&esp32_rmt_loop_obj) },
324349
{ MP_ROM_QSTR(MP_QSTR_write_pulses), MP_ROM_PTR(&esp32_rmt_write_pulses_obj) },
350+
351+
// Static methods
352+
{ MP_ROM_QSTR(MP_QSTR_bitstream_channel), MP_ROM_PTR(&esp32_rmt_bitstream_channel_obj) },
325353
};
326354
STATIC MP_DEFINE_CONST_DICT(esp32_rmt_locals_dict, esp32_rmt_locals_dict_table);
327355

Diff for: ports/esp32/machine_bitstream.c

+73-4
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,70 @@
2626

2727
#include "py/mpconfig.h"
2828
#include "py/mphal.h"
29+
#include "modesp32.h"
2930

3031
#if MICROPY_PY_MACHINE_BITSTREAM
3132

32-
#include "driver/rmt.h"
33+
/******************************************************************************/
34+
// Bit-bang implementation
3335

34-
#include "modesp32.h"
36+
#define NS_TICKS_OVERHEAD (6)
37+
38+
// This is a translation of the cycle counter implementation in ports/stm32/machine_bitstream.c.
39+
STATIC void IRAM_ATTR machine_bitstream_high_low_bitbang(mp_hal_pin_obj_t pin, uint32_t *timing_ns, const uint8_t *buf, size_t len) {
40+
uint32_t pin_mask, gpio_reg_set, gpio_reg_clear;
41+
#if !CONFIG_IDF_TARGET_ESP32C3
42+
if (pin >= 32) {
43+
pin_mask = 1 << (pin - 32);
44+
gpio_reg_set = GPIO_OUT1_W1TS_REG;
45+
gpio_reg_clear = GPIO_OUT1_W1TC_REG;
46+
} else
47+
#endif
48+
{
49+
pin_mask = 1 << pin;
50+
gpio_reg_set = GPIO_OUT_W1TS_REG;
51+
gpio_reg_clear = GPIO_OUT_W1TC_REG;
52+
}
53+
54+
// Convert ns to cpu ticks [high_time_0, period_0, high_time_1, period_1].
55+
uint32_t fcpu_mhz = ets_get_cpu_frequency();
56+
for (size_t i = 0; i < 4; ++i) {
57+
timing_ns[i] = fcpu_mhz * timing_ns[i] / 1000;
58+
if (timing_ns[i] > NS_TICKS_OVERHEAD) {
59+
timing_ns[i] -= NS_TICKS_OVERHEAD;
60+
}
61+
if (i % 2 == 1) {
62+
// Convert low_time to period (i.e. add high_time).
63+
timing_ns[i] += timing_ns[i - 1];
64+
}
65+
}
66+
67+
uint32_t irq_state = mp_hal_quiet_timing_enter();
68+
69+
for (size_t i = 0; i < len; ++i) {
70+
uint8_t b = buf[i];
71+
for (size_t j = 0; j < 8; ++j) {
72+
GPIO_REG_WRITE(gpio_reg_set, pin_mask);
73+
uint32_t start_ticks = mp_hal_ticks_cpu();
74+
uint32_t *t = &timing_ns[b >> 6 & 2];
75+
while (mp_hal_ticks_cpu() - start_ticks < t[0]) {
76+
;
77+
}
78+
GPIO_REG_WRITE(gpio_reg_clear, pin_mask);
79+
b <<= 1;
80+
while (mp_hal_ticks_cpu() - start_ticks < t[1]) {
81+
;
82+
}
83+
}
84+
}
85+
86+
mp_hal_quiet_timing_exit(irq_state);
87+
}
88+
89+
/******************************************************************************/
90+
// RMT implementation
91+
92+
#include "driver/rmt.h"
3593

3694
#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(4, 1, 0)
3795
// This convenience macro was not available in earlier IDF versions.
@@ -93,8 +151,8 @@ STATIC void IRAM_ATTR bitstream_high_low_rmt_adapter(const void *src, rmt_item32
93151
}
94152

95153
// Use the reserved RMT channel to stream high/low data on the specified pin.
96-
void machine_bitstream_high_low(mp_hal_pin_obj_t pin, uint32_t *timing_ns, const uint8_t *buf, size_t len) {
97-
rmt_config_t config = RMT_DEFAULT_CONFIG_TX(pin, MICROPY_HW_ESP32_RMT_CHANNEL_BITSTREAM);
154+
STATIC void machine_bitstream_high_low_rmt(mp_hal_pin_obj_t pin, uint32_t *timing_ns, const uint8_t *buf, size_t len, uint8_t channel_id) {
155+
rmt_config_t config = RMT_DEFAULT_CONFIG_TX(pin, channel_id);
98156

99157
// Use 40MHz clock (although 2MHz would probably be sufficient).
100158
config.clk_div = 2;
@@ -138,4 +196,15 @@ void machine_bitstream_high_low(mp_hal_pin_obj_t pin, uint32_t *timing_ns, const
138196
gpio_matrix_out(pin, SIG_GPIO_OUT_IDX, false, false);
139197
}
140198

199+
/******************************************************************************/
200+
// Interface to machine.bitstream
201+
202+
void machine_bitstream_high_low(mp_hal_pin_obj_t pin, uint32_t *timing_ns, const uint8_t *buf, size_t len) {
203+
if (esp32_rmt_bitstream_channel_id < 0) {
204+
machine_bitstream_high_low_bitbang(pin, timing_ns, buf, len);
205+
} else {
206+
machine_bitstream_high_low_rmt(pin, timing_ns, buf, len, esp32_rmt_bitstream_channel_id);
207+
}
208+
}
209+
141210
#endif // MICROPY_PY_MACHINE_BITSTREAM

Diff for: ports/esp32/modesp32.h

+2-3
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,13 @@
2626
#define RTC_LAST_EXT_PIN 39
2727
#define RTC_IS_VALID_EXT_PIN(pin_id) ((1ll << (pin_id)) & RTC_VALID_EXT_PINS)
2828

29+
extern int8_t esp32_rmt_bitstream_channel_id;
30+
2931
extern const mp_obj_type_t esp32_nvs_type;
3032
extern const mp_obj_type_t esp32_partition_type;
3133
extern const mp_obj_type_t esp32_rmt_type;
3234
extern const mp_obj_type_t esp32_ulp_type;
3335

34-
// Reserve the last channel for machine.bitstream.
35-
#define MICROPY_HW_ESP32_RMT_CHANNEL_BITSTREAM (RMT_CHANNEL_MAX - 1)
36-
3736
esp_err_t rmt_driver_install_core1(uint8_t channel_id);
3837

3938
#endif // MICROPY_INCLUDED_ESP32_MODESP32_H

0 commit comments

Comments
 (0)