Skip to content

Commit ac8a0fa

Browse files
committed
update peripherals_pcnt_init()
1 parent d8ef9a1 commit ac8a0fa

File tree

3 files changed

+22
-17
lines changed

3 files changed

+22
-17
lines changed

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,15 @@
2727
#include "common-hal/countio/Counter.h"
2828
#include "common-hal/microcontroller/Pin.h"
2929

30+
#include "py/runtime.h"
31+
#include "supervisor/shared/translate.h"
32+
3033
void common_hal_countio_counter_construct(countio_counter_obj_t* self,
3134
const mcu_pin_obj_t* pin) {
3235
claim_pin(pin);
3336

3437
// Prepare configuration for the PCNT unit
35-
pcnt_config_t pcnt_config = {
38+
const pcnt_config_t pcnt_config = {
3639
// Set PCNT input signal and control GPIOs
3740
.pulse_gpio_num = pin->number,
3841
.ctrl_gpio_num = PCNT_PIN_NOT_USED,
@@ -41,12 +44,15 @@ void common_hal_countio_counter_construct(countio_counter_obj_t* self,
4144
.pos_mode = PCNT_COUNT_INC, // Count up on the positive edge
4245
.neg_mode = PCNT_COUNT_DIS, // Keep the counter value on the negative edge
4346
};
47+
4448
// Initialize PCNT unit
45-
// This also sets pcnt_config.unit
46-
peripherals_pcnt_init(&pcnt_config);
49+
const int8_t unit = peripherals_pcnt_init(pcnt_config);
50+
if (unit == -1) {
51+
mp_raise_RuntimeError(translate("All PCNT units in use"));
52+
}
4753

4854
self->pin = pin->number;
49-
self->unit = pcnt_config.unit;
55+
self->unit = (pcnt_unit_t)unit;
5056
}
5157

5258
bool common_hal_countio_counter_deinited(countio_counter_obj_t* self) {

ports/esp32s2/peripherals/pcnt.c

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,39 +26,38 @@
2626

2727
#include "peripherals/pcnt.h"
2828

29-
#include "py/runtime.h"
30-
#include "supervisor/shared/translate.h"
31-
3229
#define PCNT_UNIT_ACTIVE 1
3330
#define PCNT_UNIT_INACTIVE 0
3431

3532
static uint8_t pcnt_state[4];
3633

37-
void peripherals_pcnt_init(pcnt_config_t* pcnt_config) {
34+
int peripherals_pcnt_init(pcnt_config_t pcnt_config) {
3835
// Look for available pcnt unit
3936
for (uint8_t i = 0; i<=3; i++) {
4037
if (pcnt_state[i] == PCNT_UNIT_INACTIVE) {
41-
pcnt_config->unit = (pcnt_unit_t)i;
38+
pcnt_config.unit = (pcnt_unit_t)i;
4239
pcnt_state[i] = PCNT_UNIT_ACTIVE;
4340
break;
4441
} else if (i == 3) {
45-
mp_raise_RuntimeError(translate("All PCNT units in use"));
42+
return -1;
4643
}
4744
}
4845

4946
// Initialize PCNT unit
50-
pcnt_unit_config(pcnt_config);
47+
pcnt_unit_config(&pcnt_config);
5148

5249
// Configure and enable the input filter
53-
pcnt_set_filter_value(pcnt_config->unit, 100);
54-
pcnt_filter_enable(pcnt_config->unit);
50+
pcnt_set_filter_value(pcnt_config.unit, 100);
51+
pcnt_filter_enable(pcnt_config.unit);
5552

5653
// Initialize PCNT's counter
57-
pcnt_counter_pause(pcnt_config->unit);
58-
pcnt_counter_clear(pcnt_config->unit);
54+
pcnt_counter_pause(pcnt_config.unit);
55+
pcnt_counter_clear(pcnt_config.unit);
5956

6057
// Everything is set up, now go to counting
61-
pcnt_counter_resume(pcnt_config->unit);
58+
pcnt_counter_resume(pcnt_config.unit);
59+
60+
return pcnt_config.unit;
6261
}
6362

6463
void peripherals_pcnt_deinit(pcnt_unit_t* unit) {

ports/esp32s2/peripherals/pcnt.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030
#include "driver/pcnt.h"
3131

32-
extern void peripherals_pcnt_init(pcnt_config_t* pcnt_config);
32+
extern int peripherals_pcnt_init(pcnt_config_t pcnt_config);
3333
extern void peripherals_pcnt_deinit(pcnt_unit_t* unit);
3434

3535
#endif // MICROPY_INCLUDED_ESP32S2_PERIPHERALS_PCNT_HANDLER_H

0 commit comments

Comments
 (0)