Skip to content

Commit 5e3bfa1

Browse files
committed
countio implementation for esp32s2
1 parent 557a58b commit 5e3bfa1

File tree

4 files changed

+86
-1
lines changed

4 files changed

+86
-1
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#include "common-hal/countio/Counter.h"
2+
3+
#include "py/runtime.h"
4+
#include "supervisor/shared/translate.h"
5+
6+
#include "driver/pcnt.h"
7+
8+
static void pcnt_init(countio_counter_obj_t* self) {
9+
int unit = PCNT_UNIT_0;
10+
// Prepare configuration for the PCNT unit
11+
pcnt_config_t pcnt_config = {
12+
// Set PCNT input signal and control GPIOs
13+
.pulse_gpio_num = self->pin->number,
14+
.ctrl_gpio_num = PCNT_PIN_NOT_USED,
15+
.channel = PCNT_CHANNEL_0,
16+
.unit = unit,
17+
// What to do on the positive / negative edge of pulse input?
18+
.pos_mode = PCNT_COUNT_INC, // Count up on the positive edge
19+
.neg_mode = PCNT_COUNT_DIS, // Keep the counter value on the negative edge
20+
};
21+
// Initialize PCNT unit
22+
pcnt_unit_config(&pcnt_config);
23+
24+
// Configure and enable the input filter
25+
pcnt_set_filter_value(unit, 100);
26+
pcnt_filter_enable(unit);
27+
28+
// Initialize PCNT's counter
29+
pcnt_counter_pause(unit);
30+
pcnt_counter_clear(unit);
31+
32+
// Everything is set up, now go to counting
33+
pcnt_counter_resume(unit);
34+
}
35+
36+
void common_hal_countio_counter_construct(countio_counter_obj_t* self,
37+
const mcu_pin_obj_t* pin) {
38+
claim_pin(pin);
39+
self->pin = pin;
40+
pcnt_init(self);
41+
}
42+
43+
bool common_hal_countio_counter_deinited(countio_counter_obj_t* self) {
44+
return self->pin == NULL;
45+
}
46+
47+
void common_hal_countio_counter_deinit(countio_counter_obj_t* self) {
48+
if (common_hal_countio_counter_deinited(self)) {
49+
return;
50+
}
51+
reset_pin_number(self->pin->number);
52+
self->pin = NULL;
53+
}
54+
55+
mp_int_t common_hal_countio_counter_get_count(countio_counter_obj_t* self) {
56+
int16_t count;
57+
pcnt_get_counter_value(PCNT_UNIT_0, &count);
58+
return count+self->count;
59+
}
60+
61+
void common_hal_countio_counter_set_count(countio_counter_obj_t* self,
62+
mp_int_t new_count) {
63+
self->count = new_count;
64+
pcnt_counter_clear(PCNT_UNIT_0);
65+
}
66+
67+
void common_hal_countio_counter_reset(countio_counter_obj_t* self) {
68+
common_hal_countio_counter_set_count(self, 0);
69+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
#ifndef MICROPY_INCLUDED_ESP32S2_COMMON_HAL_COUNTIO_COUNTER_H
3+
#define MICROPY_INCLUDED_ESP32S2_COMMON_HAL_COUNTIO_COUNTER_H
4+
5+
#include "common-hal/microcontroller/Pin.h"
6+
7+
#include "py/obj.h"
8+
9+
typedef struct {
10+
mp_obj_base_t base;
11+
const mcu_pin_obj_t * pin;
12+
mp_int_t count;
13+
} countio_counter_obj_t;
14+
15+
#endif // MICROPY_INCLUDED_ESP32S2_COMMON_HAL_COUNTIO_COUNT_H
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
//No countio module functions

ports/esp32s2/mpconfigport.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ CIRCUITPY_FULL_BUILD = 1
1717
CIRCUITPY_AUDIOBUSIO = 0
1818
CIRCUITPY_AUDIOIO = 0
1919
CIRCUITPY_CANIO = 1
20-
CIRCUITPY_COUNTIO = 0
20+
CIRCUITPY_COUNTIO = 1
2121
CIRCUITPY_FREQUENCYIO = 0
2222
CIRCUITPY_I2CPERIPHERAL = 0
2323
CIRCUITPY_ROTARYIO = 0

0 commit comments

Comments
 (0)