Skip to content

Commit 0b6b746

Browse files
authored
Merge pull request adafruit#9461 from vickash/9.1.x
espressif: enable PCNT accumulator for Counter, FrequencyIn, Incremental Encoder
2 parents c0a2e21 + 3e44957 commit 0b6b746

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,15 +20,19 @@ 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));
3131

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);
35+
3236
self->pin = pin->number;
3337
pcnt_chan_config_t channel_config = {
3438
.edge_gpio_num = self->pin,

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ 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
@@ -42,6 +42,10 @@ static esp_err_t init_pcnt(frequencyio_frequencyin_obj_t *self) {
4242
return result;
4343
}
4444

45+
// Set watchpoints at limis, to auto-accumulate overflows.
46+
pcnt_unit_add_watch_point(self->internal_data->unit, INT16_MIN);
47+
pcnt_unit_add_watch_point(self->internal_data->unit, INT16_MAX);
48+
4549
pcnt_chan_config_t channel_config = {
4650
.edge_gpio_num = self->pin,
4751
.level_gpio_num = -1

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)