Skip to content

ESP32S2: Reset PCNT on user code reload #3682

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions ports/esp32s2/peripherals/pcnt.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,20 @@
#define PCNT_UNIT_ACTIVE 1
#define PCNT_UNIT_INACTIVE 0

static uint8_t pcnt_state[4];
static uint8_t pcnt_unit_state[4];

void peripherals_pcnt_reset(void) {
for (uint8_t i = 0; i<=3; i++) {
pcnt_unit_state[i] = PCNT_UNIT_INACTIVE;
}
}

int peripherals_pcnt_init(pcnt_config_t pcnt_config) {
// Look for available pcnt unit
for (uint8_t i = 0; i<=3; i++) {
if (pcnt_state[i] == PCNT_UNIT_INACTIVE) {
if (pcnt_unit_state[i] == PCNT_UNIT_INACTIVE) {
pcnt_config.unit = (pcnt_unit_t)i;
pcnt_state[i] = PCNT_UNIT_ACTIVE;
pcnt_unit_state[i] = PCNT_UNIT_ACTIVE;
break;
} else if (i == 3) {
return -1;
Expand All @@ -46,10 +52,6 @@ int peripherals_pcnt_init(pcnt_config_t pcnt_config) {
// Initialize PCNT unit
pcnt_unit_config(&pcnt_config);

// Configure and enable the input filter
pcnt_set_filter_value(pcnt_config.unit, 100);
pcnt_filter_enable(pcnt_config.unit);

// Initialize PCNT's counter
pcnt_counter_pause(pcnt_config.unit);
pcnt_counter_clear(pcnt_config.unit);
Expand All @@ -61,6 +63,6 @@ int peripherals_pcnt_init(pcnt_config_t pcnt_config) {
}

void peripherals_pcnt_deinit(pcnt_unit_t* unit) {
pcnt_state[*unit] = PCNT_UNIT_INACTIVE;
pcnt_unit_state[*unit] = PCNT_UNIT_INACTIVE;
*unit = PCNT_UNIT_MAX;
}
1 change: 1 addition & 0 deletions ports/esp32s2/peripherals/pcnt.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,6 @@

extern int peripherals_pcnt_init(pcnt_config_t pcnt_config);
extern void peripherals_pcnt_deinit(pcnt_unit_t* unit);
extern void peripherals_pcnt_reset(void);

#endif // MICROPY_INCLUDED_ESP32S2_PERIPHERALS_PCNT_HANDLER_H
5 changes: 5 additions & 0 deletions ports/esp32s2/supervisor/port.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
#include "shared-bindings/rtc/__init__.h"

#include "peripherals/rmt.h"
#include "peripherals/pcnt.h"
#include "components/heap/include/esp_heap_caps.h"
#include "components/soc/soc/esp32s2/include/soc/cache_memory.h"

Expand Down Expand Up @@ -117,6 +118,10 @@ void reset_port(void) {
uart_reset();
#endif

#if defined(CIRCUITPY_COUNTIO) || defined(CIRCUITPY_ROTARYIO)
peripherals_pcnt_reset();
#endif

#if CIRCUITPY_RTC
rtc_reset();
#endif
Expand Down