Skip to content

Commit 49d952c

Browse files
authored
Merge pull request #8909 from tannewt/replace_legacy_esp_drivers
Remove old pcnt, rmt and timer group drivers
2 parents 574900a + 9096487 commit 49d952c

39 files changed

+503
-915
lines changed

locale/circuitpython.pot

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -476,12 +476,6 @@ msgstr ""
476476
msgid "All I2C peripherals are in use"
477477
msgstr ""
478478

479-
#: ports/espressif/common-hal/countio/Counter.c
480-
#: ports/espressif/common-hal/frequencyio/FrequencyIn.c
481-
#: ports/espressif/common-hal/rotaryio/IncrementalEncoder.c
482-
msgid "All PCNT units in use"
483-
msgstr ""
484-
485479
#: ports/atmel-samd/common-hal/canio/Listener.c
486480
#: ports/espressif/common-hal/canio/Listener.c
487481
#: ports/stm/common-hal/canio/Listener.c
@@ -529,10 +523,6 @@ msgstr ""
529523
#: ports/atmel-samd/common-hal/pulseio/PulseIn.c
530524
#: ports/atmel-samd/common-hal/pulseio/PulseOut.c
531525
#: ports/cxd56/common-hal/pulseio/PulseOut.c
532-
#: ports/espressif/common-hal/frequencyio/FrequencyIn.c
533-
#: ports/espressif/common-hal/neopixel_write/__init__.c
534-
#: ports/espressif/common-hal/pulseio/PulseIn.c
535-
#: ports/espressif/common-hal/pulseio/PulseOut.c
536526
#: ports/nrf/common-hal/audiopwmio/PWMAudioOut.c
537527
#: ports/nrf/common-hal/pulseio/PulseIn.c ports/nrf/peripherals/nrf/timers.c
538528
#: ports/raspberrypi/common-hal/audiopwmio/PWMAudioOut.c
@@ -735,7 +725,7 @@ msgstr ""
735725

736726
#: shared-bindings/storage/__init__.c shared-bindings/usb_cdc/__init__.c
737727
#: shared-bindings/usb_hid/__init__.c shared-bindings/usb_midi/__init__.c
738-
#: shared-bindings/uvc/__init__.c
728+
#: shared-bindings/usb_video/__init__.c
739729
msgid "Cannot change USB devices now"
740730
msgstr ""
741731

@@ -827,10 +817,6 @@ msgstr ""
827817
msgid "Coordinate arrays types have different sizes"
828818
msgstr ""
829819

830-
#: ports/espressif/common-hal/neopixel_write/__init__.c
831-
msgid "Could not retrieve clock"
832-
msgstr ""
833-
834820
#: shared-bindings/_bleio/Adapter.c
835821
msgid "Could not set address"
836822
msgstr ""
@@ -1134,7 +1120,7 @@ msgstr ""
11341120
msgid "Input taking too long"
11351121
msgstr ""
11361122

1137-
#: ports/espressif/common-hal/neopixel_write/__init__.c py/moderrno.c
1123+
#: py/moderrno.c
11381124
msgid "Input/output error"
11391125
msgstr ""
11401126

ports/espressif/Makefile

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,13 @@ INC += \
7979
-isystem esp-idf/components/driver/deprecated \
8080
-isystem esp-idf/components/driver/dac/include \
8181
-isystem esp-idf/components/driver/gpio/include \
82+
-isystem esp-idf/components/driver/gptimer/include \
8283
-isystem esp-idf/components/driver/i2c/include \
8384
-isystem esp-idf/components/driver/i2s/include \
8485
-isystem esp-idf/components/driver/$(IDF_TARGET)/include \
8586
-isystem esp-idf/components/driver/ledc/include \
87+
-isystem esp-idf/components/driver/pcnt/include \
88+
-isystem esp-idf/components/driver/rmt/include \
8689
-isystem esp-idf/components/driver/spi/include \
8790
-isystem esp-idf/components/driver/temperature_sensor/include \
8891
-isystem esp-idf/components/driver/touch_sensor/include \
@@ -280,8 +283,6 @@ SRC_C += \
280283
boards/$(BOARD)/pins.c \
281284
shared/netutils/netutils.c \
282285
peripherals/i2c.c \
283-
peripherals/rmt.c \
284-
peripherals/timer.c \
285286
peripherals/$(IDF_TARGET)/pins.c
286287

287288
SRC_C += $(wildcard common-hal/espidf/*.c)
@@ -290,10 +291,6 @@ ifneq ($(CIRCUITPY_ESP_USB_SERIAL_JTAG),0)
290291
SRC_C += supervisor/usb_serial_jtag.c
291292
endif
292293

293-
ifneq ($(CIRCUITPY_COUNTIO),0)
294-
SRC_C += peripherals/pcnt.c
295-
endif
296-
297294
ifneq ($(CIRCUITPY_TOUCHIO_USE_NATIVE),0)
298295
SRC_C += peripherals/touch.c
299296
endif

ports/espressif/background.c

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,9 @@
3232
#include "freertos/FreeRTOS.h"
3333
#include "freertos/task.h"
3434

35-
#if CIRCUITPY_DISPLAYIO
36-
#include "shared-module/displayio/__init__.h"
37-
#endif
38-
39-
#if CIRCUITPY_PULSEIO
40-
#include "common-hal/pulseio/PulseIn.h"
41-
#endif
42-
4335
void port_background_tick(void) {
4436
// Zero delay in case FreeRTOS wants to switch to something else.
4537
vTaskDelay(0);
46-
#if CIRCUITPY_PULSEIO
47-
pulsein_background();
48-
#endif
4938
}
5039

5140
void port_background_task(void) {

ports/espressif/boards/deneyap_kart_1a_v2/mpconfigboard.mk

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ CIRCUITPY_ESP_PSRAM_FREQ = 80m
1616

1717
CIRCUITPY_ESPCAMERA = 0
1818
CIRCUITPY_BITMAPFILTER = 0
19+
CIRCUITPY_BLEIO = 0
20+
CIRCUITPY_CODEOP=0
1921
CIRCUITPY_PARALLELDISPLAYBUS = 0
2022

2123
OPTIMIZATION_FLAGS = -Os

ports/espressif/boards/lolin_s3_mini/mpconfigboard.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ CIRCUITPY_ESP_PSRAM_FREQ = 80m
1616
OPTIMIZATION_FLAGS = -Os
1717
CIRCUITPY_ESPCAMERA = 0
1818
CIRCUITPY_BITMAPFILTER = 0
19+
CIRCUITPY_BLEIO = 0
1920
CIRCUITPY_CODEOP=0
2021
CIRCUITPY_PARALLELDISPLAYBUS = 0
2122

ports/espressif/boards/waveshare_esp32_s3_zero/mpconfigboard.mk

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ CIRCUITPY_ESP_PSRAM_FREQ = 80m
1616
OPTIMIZATION_FLAGS = -Os
1717
CIRCUITPY_ESPCAMERA = 0
1818
CIRCUITPY_BITMAPFILTER = 0
19+
CIRCUITPY_BLEIO = 0
20+
CIRCUITPY_CODEOP=0
1921
CIRCUITPY_PARALLELDISPLAYBUS = 0
2022

2123
# Include these Python libraries in firmware.

ports/espressif/common-hal/audiobusio/__init__.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,9 @@ void port_i2s_allocate_init(i2s_t *self, bool left_justified) {
140140
if (err == ESP_ERR_NOT_FOUND) {
141141
mp_raise_RuntimeError(MP_ERROR_TEXT("Peripheral in use"));
142142
}
143+
self->playing = false;
144+
self->paused = false;
145+
self->stopping = false;
143146

144147
i2s_event_callbacks_t callbacks = {
145148
.on_recv = NULL,
@@ -157,6 +160,8 @@ void port_i2s_deinit(i2s_t *self) {
157160
}
158161

159162
void port_i2s_play(i2s_t *self, mp_obj_t sample, bool loop) {
163+
// Pause to disable the I2S channel so we can adjust the clock.
164+
port_i2s_pause(self);
160165
self->sample = sample;
161166
self->loop = loop;
162167
self->bytes_per_sample = audiosample_bits_per_sample(sample) / 8;
@@ -204,6 +209,9 @@ void port_i2s_play(i2s_t *self, mp_obj_t sample, bool loop) {
204209
}
205210

206211
bool port_i2s_playing(i2s_t *self) {
212+
// TODO: Reason about stopping. This check leads to cases where the DMA is
213+
// "playing" but the common-hal thinks it isn't and skips pausing. Probably
214+
// best to move this functionality into I2SOut directly.
207215
return self->playing && !self->stopping;
208216
}
209217

@@ -219,14 +227,14 @@ void port_i2s_stop(i2s_t *self) {
219227
}
220228

221229
void port_i2s_pause(i2s_t *self) {
222-
if (!self->paused) {
230+
if (self->playing && !self->paused) {
223231
self->paused = true;
224232
CHECK_ESP_RESULT(i2s_channel_disable(self->handle));
225233
}
226234
}
227235

228236
void port_i2s_resume(i2s_t *self) {
229-
if (self->paused) {
237+
if (self->playing && self->paused) {
230238
self->paused = false;
231239
CHECK_ESP_RESULT(i2s_channel_enable(self->handle));
232240
}

ports/espressif/common-hal/audiobusio/__init__.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ typedef struct {
3636
mp_obj_t *sample;
3737
bool left_justified;
3838
bool loop;
39-
bool paused;
40-
bool playing;
39+
bool paused; // True when the I2S channel is configured but disabled.
40+
bool playing; // True when the I2S channel is configured.
4141
bool stopping;
4242
bool samples_signed;
4343
int8_t bytes_per_sample;

ports/espressif/common-hal/countio/Counter.c

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
#include "shared-bindings/countio/Counter.h"
2929
#include "common-hal/microcontroller/Pin.h"
3030

31+
#include "bindings/espidf/__init__.h"
32+
3133
#include "py/runtime.h"
3234

3335
#include "driver/gpio.h"
@@ -36,25 +38,26 @@ void common_hal_countio_counter_construct(countio_counter_obj_t *self,
3638
const mcu_pin_obj_t *pin, countio_edge_t edge, digitalio_pull_t pull) {
3739
claim_pin(pin);
3840

39-
// Prepare configuration for the PCNT unit
40-
pcnt_config_t pcnt_config = {
41-
// Set PCNT input signal and control GPIOs
42-
.pulse_gpio_num = pin->number,
43-
.ctrl_gpio_num = PCNT_PIN_NOT_USED,
44-
.channel = PCNT_CHANNEL_0,
45-
// What to do on the rising / falling edge of pulse input?
46-
// If EDGE_RISE_AND_FALL, both modeswill do PCNT_COUNT_INC.
47-
.pos_mode = (edge == EDGE_FALL) ? PCNT_COUNT_DIS : PCNT_COUNT_INC, // Count up unless only fall
48-
.neg_mode = (edge == EDGE_RISE) ? PCNT_COUNT_DIS : PCNT_COUNT_INC, // Count up unless only rise
41+
pcnt_unit_config_t unit_config = {
42+
// Set counter limit
43+
.low_limit = -1,
44+
.high_limit = INT16_MAX
4945
};
46+
// The pulse count driver automatically counts roll overs.
47+
unit_config.flags.accum_count = true;
5048

51-
// Initialize PCNT unit
52-
const int8_t unit = peripherals_pcnt_init(&pcnt_config);
53-
if (unit == -1) {
54-
mp_raise_RuntimeError(MP_ERROR_TEXT("All PCNT units in use"));
55-
}
49+
// initialize PCNT
50+
CHECK_ESP_RESULT(pcnt_new_unit(&unit_config, &self->unit));
5651

5752
self->pin = pin->number;
53+
pcnt_chan_config_t channel_config = {
54+
.edge_gpio_num = self->pin,
55+
.level_gpio_num = -1
56+
};
57+
CHECK_ESP_RESULT(pcnt_new_channel(self->unit, &channel_config, &self->channel));
58+
pcnt_channel_edge_action_t pos = (edge == EDGE_RISE || edge == EDGE_RISE_AND_FALL) ? PCNT_CHANNEL_EDGE_ACTION_INCREASE : PCNT_CHANNEL_EDGE_ACTION_HOLD;
59+
pcnt_channel_edge_action_t neg = (edge == EDGE_FALL || edge == EDGE_RISE_AND_FALL) ? PCNT_CHANNEL_EDGE_ACTION_INCREASE : PCNT_CHANNEL_EDGE_ACTION_HOLD;
60+
pcnt_channel_set_edge_action(self->channel, pos, neg);
5861

5962
gpio_pullup_dis(pin->number);
6063
gpio_pulldown_dis(pin->number);
@@ -64,29 +67,34 @@ void common_hal_countio_counter_construct(countio_counter_obj_t *self,
6467
gpio_pulldown_en(pin->number);
6568
}
6669

67-
self->unit = (pcnt_unit_t)unit;
70+
71+
pcnt_unit_enable(self->unit);
72+
pcnt_unit_start(self->unit);
6873
}
6974

7075
bool common_hal_countio_counter_deinited(countio_counter_obj_t *self) {
71-
return self->unit == PCNT_UNIT_MAX;
76+
return self->unit == NULL;
7277
}
7378

7479
void common_hal_countio_counter_deinit(countio_counter_obj_t *self) {
7580
if (common_hal_countio_counter_deinited(self)) {
7681
return;
7782
}
83+
pcnt_unit_disable(self->unit);
84+
pcnt_del_channel(self->channel);
7885
reset_pin_number(self->pin);
79-
peripherals_pcnt_deinit(&self->unit);
86+
pcnt_del_unit(self->unit);
87+
self->unit = NULL;
8088
}
8189

8290
mp_int_t common_hal_countio_counter_get_count(countio_counter_obj_t *self) {
83-
int16_t count;
84-
pcnt_get_counter_value(self->unit, &count);
91+
int count;
92+
pcnt_unit_get_count(self->unit, &count);
8593
return count + self->count;
8694
}
8795

8896
void common_hal_countio_counter_set_count(countio_counter_obj_t *self,
8997
mp_int_t new_count) {
9098
self->count = new_count;
91-
pcnt_counter_clear(self->unit);
99+
pcnt_unit_clear_count(self->unit);
92100
}

ports/espressif/common-hal/countio/Counter.h

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,15 @@
2424
* THE SOFTWARE.
2525
*/
2626

27-
#ifndef MICROPY_INCLUDED_ESPRESSIF_COMMON_HAL_COUNTIO_COUNTER_H
28-
#define MICROPY_INCLUDED_ESPRESSIF_COMMON_HAL_COUNTIO_COUNTER_H
27+
#pragma once
2928

3029
#include "py/obj.h"
31-
#include "peripherals/pcnt.h"
30+
#include "driver/pulse_cnt.h"
3231

3332
typedef struct {
3433
mp_obj_base_t base;
35-
uint8_t pin;
34+
pcnt_unit_handle_t unit;
35+
pcnt_channel_handle_t channel;
3636
mp_int_t count;
37-
pcnt_unit_t unit;
37+
uint8_t pin;
3838
} countio_counter_obj_t;
39-
40-
#endif // MICROPY_INCLUDED_ESPRESSIF_COMMON_HAL_COUNTIO_COUNT_H

0 commit comments

Comments
 (0)