update peripherals_pcnt_init()
This commit is contained in:
parent
d8ef9a127b
commit
ac8a0faa0d
@ -27,12 +27,15 @@
|
|||||||
#include "common-hal/countio/Counter.h"
|
#include "common-hal/countio/Counter.h"
|
||||||
#include "common-hal/microcontroller/Pin.h"
|
#include "common-hal/microcontroller/Pin.h"
|
||||||
|
|
||||||
|
#include "py/runtime.h"
|
||||||
|
#include "supervisor/shared/translate.h"
|
||||||
|
|
||||||
void common_hal_countio_counter_construct(countio_counter_obj_t* self,
|
void common_hal_countio_counter_construct(countio_counter_obj_t* self,
|
||||||
const mcu_pin_obj_t* pin) {
|
const mcu_pin_obj_t* pin) {
|
||||||
claim_pin(pin);
|
claim_pin(pin);
|
||||||
|
|
||||||
// Prepare configuration for the PCNT unit
|
// Prepare configuration for the PCNT unit
|
||||||
pcnt_config_t pcnt_config = {
|
const pcnt_config_t pcnt_config = {
|
||||||
// Set PCNT input signal and control GPIOs
|
// Set PCNT input signal and control GPIOs
|
||||||
.pulse_gpio_num = pin->number,
|
.pulse_gpio_num = pin->number,
|
||||||
.ctrl_gpio_num = PCNT_PIN_NOT_USED,
|
.ctrl_gpio_num = PCNT_PIN_NOT_USED,
|
||||||
@ -41,12 +44,15 @@ void common_hal_countio_counter_construct(countio_counter_obj_t* self,
|
|||||||
.pos_mode = PCNT_COUNT_INC, // Count up on the positive edge
|
.pos_mode = PCNT_COUNT_INC, // Count up on the positive edge
|
||||||
.neg_mode = PCNT_COUNT_DIS, // Keep the counter value on the negative edge
|
.neg_mode = PCNT_COUNT_DIS, // Keep the counter value on the negative edge
|
||||||
};
|
};
|
||||||
|
|
||||||
// Initialize PCNT unit
|
// Initialize PCNT unit
|
||||||
// This also sets pcnt_config.unit
|
const int8_t unit = peripherals_pcnt_init(pcnt_config);
|
||||||
peripherals_pcnt_init(&pcnt_config);
|
if (unit == -1) {
|
||||||
|
mp_raise_RuntimeError(translate("All PCNT units in use"));
|
||||||
|
}
|
||||||
|
|
||||||
self->pin = pin->number;
|
self->pin = pin->number;
|
||||||
self->unit = pcnt_config.unit;
|
self->unit = (pcnt_unit_t)unit;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool common_hal_countio_counter_deinited(countio_counter_obj_t* self) {
|
bool common_hal_countio_counter_deinited(countio_counter_obj_t* self) {
|
||||||
|
@ -26,39 +26,38 @@
|
|||||||
|
|
||||||
#include "peripherals/pcnt.h"
|
#include "peripherals/pcnt.h"
|
||||||
|
|
||||||
#include "py/runtime.h"
|
|
||||||
#include "supervisor/shared/translate.h"
|
|
||||||
|
|
||||||
#define PCNT_UNIT_ACTIVE 1
|
#define PCNT_UNIT_ACTIVE 1
|
||||||
#define PCNT_UNIT_INACTIVE 0
|
#define PCNT_UNIT_INACTIVE 0
|
||||||
|
|
||||||
static uint8_t pcnt_state[4];
|
static uint8_t pcnt_state[4];
|
||||||
|
|
||||||
void peripherals_pcnt_init(pcnt_config_t* pcnt_config) {
|
int peripherals_pcnt_init(pcnt_config_t pcnt_config) {
|
||||||
// Look for available pcnt unit
|
// Look for available pcnt unit
|
||||||
for (uint8_t i = 0; i<=3; i++) {
|
for (uint8_t i = 0; i<=3; i++) {
|
||||||
if (pcnt_state[i] == PCNT_UNIT_INACTIVE) {
|
if (pcnt_state[i] == PCNT_UNIT_INACTIVE) {
|
||||||
pcnt_config->unit = (pcnt_unit_t)i;
|
pcnt_config.unit = (pcnt_unit_t)i;
|
||||||
pcnt_state[i] = PCNT_UNIT_ACTIVE;
|
pcnt_state[i] = PCNT_UNIT_ACTIVE;
|
||||||
break;
|
break;
|
||||||
} else if (i == 3) {
|
} else if (i == 3) {
|
||||||
mp_raise_RuntimeError(translate("All PCNT units in use"));
|
return -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Initialize PCNT unit
|
// Initialize PCNT unit
|
||||||
pcnt_unit_config(pcnt_config);
|
pcnt_unit_config(&pcnt_config);
|
||||||
|
|
||||||
// Configure and enable the input filter
|
// Configure and enable the input filter
|
||||||
pcnt_set_filter_value(pcnt_config->unit, 100);
|
pcnt_set_filter_value(pcnt_config.unit, 100);
|
||||||
pcnt_filter_enable(pcnt_config->unit);
|
pcnt_filter_enable(pcnt_config.unit);
|
||||||
|
|
||||||
// Initialize PCNT's counter
|
// Initialize PCNT's counter
|
||||||
pcnt_counter_pause(pcnt_config->unit);
|
pcnt_counter_pause(pcnt_config.unit);
|
||||||
pcnt_counter_clear(pcnt_config->unit);
|
pcnt_counter_clear(pcnt_config.unit);
|
||||||
|
|
||||||
// Everything is set up, now go to counting
|
// Everything is set up, now go to counting
|
||||||
pcnt_counter_resume(pcnt_config->unit);
|
pcnt_counter_resume(pcnt_config.unit);
|
||||||
|
|
||||||
|
return pcnt_config.unit;
|
||||||
}
|
}
|
||||||
|
|
||||||
void peripherals_pcnt_deinit(pcnt_unit_t* unit) {
|
void peripherals_pcnt_deinit(pcnt_unit_t* unit) {
|
||||||
|
@ -29,7 +29,7 @@
|
|||||||
|
|
||||||
#include "driver/pcnt.h"
|
#include "driver/pcnt.h"
|
||||||
|
|
||||||
extern void peripherals_pcnt_init(pcnt_config_t* pcnt_config);
|
extern int peripherals_pcnt_init(pcnt_config_t pcnt_config);
|
||||||
extern void peripherals_pcnt_deinit(pcnt_unit_t* unit);
|
extern void peripherals_pcnt_deinit(pcnt_unit_t* unit);
|
||||||
|
|
||||||
#endif // MICROPY_INCLUDED_ESP32S2_PERIPHERALS_PCNT_HANDLER_H
|
#endif // MICROPY_INCLUDED_ESP32S2_PERIPHERALS_PCNT_HANDLER_H
|
||||||
|
Loading…
Reference in New Issue
Block a user