10000 Merge pull request #6430 from dhalbert/7.3.x-fix-espressif-rotaryio · tannewt/circuitpython@a66489e · GitHub
[go: up one dir, main page]

Skip to content

Commit a66489e

Browse files
authored
Merge pull request micropython#6430 from dhalbert/7.3.x-fix-espressif-rotaryio
Espressif: fix allocation of multiple Incremental Encoders
2 parents eb4ed30 + b143314 commit a66489e

File tree

5 files changed

+49
-51
lines changed

5 files changed

+49
-51
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ void common_hal_countio_counter_construct(countio_counter_obj_t *self,
3636
claim_pin(pin);
3737

3838
// Prepare configuration for the PCNT unit
39-
const pcnt_config_t pcnt_config = {
39+
pcnt_config_t pcnt_config = {
4040
// Set PCNT input signal and control GPIOs
4141
.pulse_gpio_num = pin->number,
4242
.ctrl_gpio_num = PCNT_PIN_NOT_USED,
@@ -48,7 +48,7 @@ void common_hal_countio_counter_construct(countio_counter_obj_t *self,
4848
};
4949

5050
// Initialize PCNT unit
51-
const int8_t unit = peripherals_pcnt_init(pcnt_config);
51+
const int8_t unit = peripherals_pcnt_init(&pcnt_config);
5252
if (unit == -1) {
5353
mp_raise_RuntimeError(translate("All PCNT units in use"));
5454
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ static void IRAM_ATTR timer_interrupt_handler(void *self_in) {
6969

7070
static void init_pcnt(frequencyio_frequencyin_obj_t *self) {
7171
// Prepare configuration for the PCNT unit
72-
const pcnt_config_t pcnt_config = {
72+
pcnt_config_t pcnt_config = {
7373
// Set PCNT input signal and control GPIOs
7474
.pulse_gpio_num = self->pin,
7575
.ctrl_gpio_num = PCNT_PIN_NOT_USED,
@@ -83,7 +83,7 @@ static void init_pcnt(frequencyio_frequencyin_obj_t *self) {
8383
};
8484

8585
// initialize PCNT
86-
const int8_t unit = peripherals_pcnt_init(pcnt_config);
86+
const int8_t unit = peripherals_pcnt_init(&pcnt_config);
8787
if (unit == -1) {
8888
mp_raise_RuntimeError(translate("All PCNT units in use"));
8989
}

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

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -36,45 +36,41 @@ void common_hal_rotaryio_incrementalencoder_construct(rotaryio_incrementalencode
3636
claim_pin(pin_b);
3737

3838
// Prepare configuration for the PCNT unit
39-
pcnt_config_t pcnt_config = {
39+
pcnt_config_t pcnt_config_channel_0 = {
4040
// Set PCNT input signal and control GPIOs
4141
.pulse_gpio_num = pin_a->number,
4242
.ctrl_gpio_num = pin_b->number,
4343
.channel = PCNT_CHANNEL_0,
4444
// What to do on the positive / negative edge of pulse input?
45-
.pos_mode = PCNT_COUNT_DEC, // Count up on the positive edge
46-
.neg_mode = PCNT_COUNT_INC, // Keep the counter value on the negative edge
45+
.pos_mode = PCNT_COUNT_DEC, // Count up on the positive edge
46+
.neg_mode = PCNT_COUNT_INC, // Keep the counter value on the negative edge
4747
// What to do when control input is low or high?
4848
.lctrl_mode = PCNT_MODE_REVERSE, // Reverse counting direction if low
4949
.hctrl_mode = PCNT_MODE_KEEP, // Keep the primary counter mode if high
5050
};
5151

52-
// Initialize PCNT unit
53-
const int8_t unit = peripherals_pcnt_get_unit(pcnt_config);
52+
// Allocate and initialize PCNT unit, CHANNEL_0.
53+
const int8_t unit = peripherals_pcnt_init(&pcnt_config_channel_0);
5454
if (unit == -1) {
5555
mp_raise_RuntimeError(translate("All PCNT units in use"));
5656
}
5757

58-
pcnt_unit_config(&pcnt_config);
59-
60-
pcnt_config.pulse_gpio_num = pin_b->number; // What was control is now signal
61-
pcnt_config.ctrl_gpio_num = pin_a->number; // What was signal is now control
62-
pcnt_config.channel = PCNT_CHANNEL_1;
63-
// What to do on the positive / negative edge of pulse input?
64-
pcnt_config.pos_mode = PCNT_COUNT_DEC; // Count up on the positive edge
65-
pcnt_config.neg_mode = PCNT_COUNT_INC; // Keep the counter value on the negative edge
66-
// What to do when control input is low or high?
67-
pcnt_config.lctrl_mode = PCNT_MODE_KEEP; // Keep the primary counter mode if low
68-
pcnt_config.hctrl_mode = PCNT_MODE_REVERSE; // Reverse counting direction if high
69-
70-
pcnt_unit_config(&pcnt_config);
71-
72-
// Initialize PCNT's counter
73-
pcnt_counter_pause(pcnt_config.unit);
74-
pcnt_counter_clear(pcnt_config.unit);
58+
pcnt_config_t pcnt_config_channel_1 = {
59+
// Set PCNT input signal and control GPIOs
60+
.pulse_gpio_num = pin_b->number, // Pins are reversed from above
61+
.ctrl_gpio_num = pin_a->number,
62+
.channel = PCNT_CHANNEL_1,
63+
// What to do on the positive / negative edge of pulse input?
64+
.pos_mode = PCNT_COUNT_DEC, // Count up on the positive edge
65+
.neg_mode = PCNT_COUNT_INC, // Keep the counter value on the negative edge
66+
// What to do when control input is low or high?
67+
.lctrl_mode = PCNT_MODE_KEEP, // Keep the primary counter mode if low
68+
.hctrl_mode = PCNT_MODE_REVERSE, // Reverse counting direction if high
69+
.unit = unit,
70+
};
7571

76-
// Everything is set up, now go to counting
77-
pcnt_counter_resume(pcnt_config.unit);
72+
// Reinitalize same unit, CHANNEL_1 with different parameters.
73+
peripherals_pcnt_reinit(&pcnt_config_channel_1);
7874

7975
self->pin_a = pin_a->number;
8076
self->pin_b = pin_b->number;

ports/espressif/peripherals/pcnt.c

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -29,48 +29,50 @@
2929
#define PCNT_UNIT_ACTIVE 1
3030
#define PCNT_UNIT_INACTIVE 0
3131

32-
static uint8_t pcnt_unit_state[4];
32+
static uint8_t pcnt_unit_state[PCNT_UNIT_MAX];
3333

3434
void peripherals_pcnt_reset(void) {
35-
for (uint8_t i = 0; i <= 3; i++) {
35+
for (uint8_t i = 0; i < PCNT_UNIT_MAX; i++) {
3636
pcnt_unit_state[i] = PCNT_UNIT_INACTIVE;
3737
}
3838
}
3939

40-
int peripherals_pcnt_get_unit(pcnt_config_t pcnt_config) {
40+
static int peripherals_pcnt_get_unit(pcnt_config_t *pcnt_config) {
4141
// Look for available pcnt unit
42-
for (uint8_t i = 0; i <= 3; i++) {
42+
for (uint8_t i = 0; i < PCNT_UNIT_MAX; i++) {
4343
if (pcnt_unit_state[i] == PCNT_UNIT_INACTIVE) {
44-
pcnt_config.unit = (pcnt_unit_t)i;
44+
pcnt_config->unit = (pcnt_unit_t)i;
4545
pcnt_unit_state[i] = PCNT_UNIT_ACTIVE;
46-
break;
47-
} else if (i == 3) {
48-
return -1;
46+
return i;
4947
}
5048
}
5149

52-
return pcnt_config.unit;
50+
return -1;
5351
}
5452

55-
int peripherals_pcnt_init(pcnt_config_t pcnt_config) {
56-
// Look for available pcnt unit
57-
58-
const int8_t unit = peripherals_pcnt_get_unit(pcnt_config);
59-
if (unit == -1) {
60-
return -1;
61-
}
53+
void peripherals_pcnt_reinit(pcnt_config_t *pcnt_config) {
54+
// Reinitialize a pcnt unit that has already been allocated.
6255

6356
// Initialize PCNT unit
64-
pcnt_unit_config(&pcnt_config);
57+
pcnt_unit_config(pcnt_config);
6558

6659
// Initialize PCNT's counter
67-
pcnt_counter_pause(pcnt_config.unit);
68-
pcnt_counter_clear(pcnt_config.unit);
60+
pcnt_counter_pause(pcnt_config->unit);
61+
pcnt_counter_clear(pcnt_config->unit);
6962

7063
// Everything is set up, now go to counting
71-
pcnt_counter_resume(pcnt_config.unit);
64+
pcnt_counter_resume(pcnt_config->unit);
65+
}
66+
67+
int peripherals_pcnt_init(pcnt_config_t *pcnt_config) {
68+
const int8_t unit = peripherals_pcnt_get_unit(pcnt_config);
69+
if (unit == -1) {
70+
return -1;
71+
}
72+
73+
peripherals_pcnt_reinit(pcnt_config);
7274

73-
return pcnt_config.unit;
75+
return pcnt_config->unit;
7476
}
7577

7678
void peripherals_pcnt_deinit(pcnt_unit_t *unit) {

ports/espressif/peripherals/pcnt.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@
3030
#include "driver/pcnt.h"
3131
#include "soc/pcnt_struct.h"
3232

33-
extern int peripherals_pcnt_init(pcnt_config_t pcnt_config);
34-
extern int peripherals_pcnt_get_unit(pcnt_config_t pcnt_config);
33+
extern int peripherals_pcnt_init(pcnt_config_t *pcnt_config);
34+
extern void peripherals_pcnt_reinit(pcnt_config_t *pcnt_config);
3535
extern void peripherals_pcnt_deinit(pcnt_unit_t *unit);
3636
extern void peripherals_pcnt_reset(void);
3737

0 commit comments

Comments
 (0)
0