Skip to content

Commit a60ad7b

Browse files
committed
espressif: enable PCNT accumulator for Counter, FrequencyIn, IncrementalEncoder
1 parent c0a2e21 commit a60ad7b

File tree

3 files changed

+18
-6
lines changed

3 files changed

+18
-6
lines changed

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,18 @@ void common_hal_countio_counter_construct(countio_counter_obj_t *self,
2020

2121
pcnt_unit_config_t unit_config = {
2222
// Set counter limit
23-
.low_limit = -1,
23+
.low_limit = INT16_MIN,
2424
.high_limit = INT16_MAX
2525
};
26-
// The pulse count driver automatically counts roll overs.
26+
// Enable PCNT internal accumulator to count overflows.
2727
unit_config.flags.accum_count = true;
2828

2929
// initialize PCNT
3030
CHECK_ESP_RESULT(pcnt_new_unit(&unit_config, &self->unit));
31+
32+
// Set watchpoints at limis, to auto-accumulate overflows.
33+
pcnt_unit_add_watch_point(self->unit, INT16_MIN);
34+
pcnt_unit_add_watch_point(self->unit, INT16_MAX);
3135

3236
self->pin = pin->number;
3337
pcnt_chan_config_t channel_config = {

ports/espressif/common-hal/frequencyio/FrequencyIn.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,21 @@ static IRAM_ATTR bool timer_interrupt_handler(gptimer_handle_t timer,
3030
static esp_err_t init_pcnt(frequencyio_frequencyin_obj_t *self) {
3131
pcnt_unit_config_t unit_config = {
3232
// Set counter limit
33-
.low_limit = -INT16_MAX + 1,
33+
.low_limit = INT16_MIN,
3434
.high_limit = INT16_MAX
3535
};
36-
// The pulse count driver automatically counts roll overs.
36+
// Enable PCNT internal accumulator to count overflows.
3737
unit_config.flags.accum_count = true;
3838

3939
// initialize PCNT
4040
esp_err_t result = pcnt_new_unit(&unit_config, &self->internal_data->unit);
4141
if (result != ESP_OK) {
4242
return result;
4343
}
44+
45+
// Set watchpoints at limis, to auto-accumulate overflows.
46+
pcnt_unit_add_watch_point(self->unit, INT16_MIN);
47+
pcnt_unit_add_watch_point(self->unit, INT16_MAX);
4448

4549
pcnt_chan_config_t channel_config = {
4650
.edge_gpio_num = self->pin,

ports/espressif/common-hal/rotaryio/IncrementalEncoder.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,19 @@ void common_hal_rotaryio_incrementalencoder_construct(rotaryio_incrementalencode
2323
// in CircuitPython.
2424
pcnt_unit_config_t unit_config = {
2525
// Set counter limit
26-
.low_limit = -INT16_MAX,
26+
.low_limit = INT16_MIN,
2727
.high_limit = INT16_MAX
2828
};
29-
// The pulse count driver automatically counts roll overs.
29+
// Enable PCNT internal accumulator to count overflows.
3030
unit_config.flags.accum_count = true;
3131

3232
// initialize PCNT
3333
CHECK_ESP_RESULT(pcnt_new_unit(&unit_config, &self->unit));
3434

35+
// Set watchpoints at limits, to auto-accumulate overflows.
36+
pcnt_unit_add_watch_point(self->unit, INT16_MIN);
37+
pcnt_unit_add_watch_point(self->unit, INT16_MAX);
38+
3539
pcnt_chan_config_t channel_a_config = {
3640
.edge_gpio_num = pin_a->number,
3741
.level_gpio_num = pin_b->number

0 commit comments

Comments
 (0)