summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ports/esp32s2/common-hal/countio/Counter.c69
-rw-r--r--ports/esp32s2/common-hal/countio/Counter.h15
-rw-r--r--ports/esp32s2/common-hal/countio/__init__.c1
-rw-r--r--ports/esp32s2/mpconfigport.mk2
4 files changed, 86 insertions, 1 deletions
diff --git a/ports/esp32s2/common-hal/countio/Counter.c b/ports/esp32s2/common-hal/countio/Counter.c
new file mode 100644
index 000000000..6a76a163d
--- /dev/null
+++ b/ports/esp32s2/common-hal/countio/Counter.c
@@ -0,0 +1,69 @@
+#include "common-hal/countio/Counter.h"
+
+#include "py/runtime.h"
+#include "supervisor/shared/translate.h"
+
+#include "driver/pcnt.h"
+
+static void pcnt_init(countio_counter_obj_t* self) {
+ int unit = PCNT_UNIT_0;
+ // Prepare configuration for the PCNT unit
+ pcnt_config_t pcnt_config = {
+ // Set PCNT input signal and control GPIOs
+ .pulse_gpio_num = self->pin->number,
+ .ctrl_gpio_num = PCNT_PIN_NOT_USED,
+ .channel = PCNT_CHANNEL_0,
+ .unit = unit,
+ // What to do on the positive / negative edge of pulse input?
+ .pos_mode = PCNT_COUNT_INC, // Count up on the positive edge
+ .neg_mode = PCNT_COUNT_DIS, // Keep the counter value on the negative edge
+ };
+ // Initialize PCNT unit
+ pcnt_unit_config(&pcnt_config);
+
+ // Configure and enable the input filter
+ pcnt_set_filter_value(unit, 100);
+ pcnt_filter_enable(unit);
+
+ // Initialize PCNT's counter
+ pcnt_counter_pause(unit);
+ pcnt_counter_clear(unit);
+
+ // Everything is set up, now go to counting
+ pcnt_counter_resume(unit);
+}
+
+void common_hal_countio_counter_construct(countio_counter_obj_t* self,
+ const mcu_pin_obj_t* pin) {
+ claim_pin(pin);
+ self->pin = pin;
+ pcnt_init(self);
+}
+
+bool common_hal_countio_counter_deinited(countio_counter_obj_t* self) {
+ return self->pin == NULL;
+}
+
+void common_hal_countio_counter_deinit(countio_counter_obj_t* self) {
+ if (common_hal_countio_counter_deinited(self)) {
+ return;
+ }
+ reset_pin_number(self->pin->number);
+ self->pin = NULL;
+}
+
+mp_int_t common_hal_countio_counter_get_count(countio_counter_obj_t* self) {
+ int16_t count;
+ pcnt_get_counter_value(PCNT_UNIT_0, &count);
+ return count+self->count;
+}
+
+void common_hal_countio_counter_set_count(countio_counter_obj_t* self,
+ mp_int_t new_count) {
+ self->count = new_count;
+ pcnt_counter_clear(PCNT_UNIT_0);
+}
+
+void common_hal_countio_counter_reset(countio_counter_obj_t* self) {
+ common_hal_countio_counter_set_count(self, 0);
+}
diff --git a/ports/esp32s2/common-hal/countio/Counter.h b/ports/esp32s2/common-hal/countio/Counter.h
new file mode 100644
index 000000000..3406f9daa
--- /dev/null
+++ b/ports/esp32s2/common-hal/countio/Counter.h
@@ -0,0 +1,15 @@
+
+#ifndef MICROPY_INCLUDED_ESP32S2_COMMON_HAL_COUNTIO_COUNTER_H
+#define MICROPY_INCLUDED_ESP32S2_COMMON_HAL_COUNTIO_COUNTER_H
+
+#include "common-hal/microcontroller/Pin.h"
+
+#include "py/obj.h"
+
+typedef struct {
+ mp_obj_base_t base;
+ const mcu_pin_obj_t * pin;
+ mp_int_t count;
+} countio_counter_obj_t;
+
+#endif // MICROPY_INCLUDED_ESP32S2_COMMON_HAL_COUNTIO_COUNT_H
diff --git a/ports/esp32s2/common-hal/countio/__init__.c b/ports/esp32s2/common-hal/countio/__init__.c
new file mode 100644
index 000000000..b95b20d15
--- /dev/null
+++ b/ports/esp32s2/common-hal/countio/__init__.c
@@ -0,0 +1 @@
+//No countio module functions
diff --git a/ports/esp32s2/mpconfigport.mk b/ports/esp32s2/mpconfigport.mk
index c06c89c90..bdb25a87b 100644
--- a/ports/esp32s2/mpconfigport.mk
+++ b/ports/esp32s2/mpconfigport.mk
@@ -17,7 +17,7 @@ CIRCUITPY_FULL_BUILD = 1
CIRCUITPY_AUDIOBUSIO = 0
CIRCUITPY_AUDIOIO = 0
CIRCUITPY_CANIO = 1
-CIRCUITPY_COUNTIO = 0
+CIRCUITPY_COUNTIO = 1
CIRCUITPY_FREQUENCYIO = 0
CIRCUITPY_I2CPERIPHERAL = 0
CIRCUITPY_ROTARYIO = 0