countio implementation for esp32s2
This commit is contained in:
parent
557a58b244
commit
5e3bfa1956
|
@ -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);
|
||||
}
|
|
@ -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
|
|
@ -0,0 +1 @@
|
|||
//No countio module functions
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue