update peripherals_pcnt_init()

This commit is contained in:
microDev 2020-11-06 01:42:20 +05:30
parent d8ef9a127b
commit ac8a0faa0d
No known key found for this signature in database
GPG Key ID: 2C0867BE60967730
3 changed files with 22 additions and 17 deletions

View File

@ -27,12 +27,15 @@
#include "common-hal/countio/Counter.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,
const mcu_pin_obj_t* pin) {
claim_pin(pin);
// Prepare configuration for the PCNT unit
pcnt_config_t pcnt_config = {
const pcnt_config_t pcnt_config = {
// Set PCNT input signal and control GPIOs
.pulse_gpio_num = pin->number,
.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
.neg_mode = PCNT_COUNT_DIS, // Keep the counter value on the negative edge
};
// Initialize PCNT unit
// This also sets pcnt_config.unit
peripherals_pcnt_init(&pcnt_config);
const int8_t unit = peripherals_pcnt_init(pcnt_config);
if (unit == -1) {
mp_raise_RuntimeError(translate("All PCNT units in use"));
}
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) {

View File

@ -26,39 +26,38 @@
#include "peripherals/pcnt.h"
#include "py/runtime.h"
#include "supervisor/shared/translate.h"
#define PCNT_UNIT_ACTIVE 1
#define PCNT_UNIT_INACTIVE 0
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
for (uint8_t i = 0; i<=3; i++) {
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;
break;
} else if (i == 3) {
mp_raise_RuntimeError(translate("All PCNT units in use"));
return -1;
}
}
// Initialize PCNT unit
pcnt_unit_config(pcnt_config);
pcnt_unit_config(&pcnt_config);
// Configure and enable the input filter
pcnt_set_filter_value(pcnt_config->unit, 100);
pcnt_filter_enable(pcnt_config->unit);
pcnt_set_filter_value(pcnt_config.unit, 100);
pcnt_filter_enable(pcnt_config.unit);
// Initialize PCNT's counter
pcnt_counter_pause(pcnt_config->unit);
pcnt_counter_clear(pcnt_config->unit);
pcnt_counter_pause(pcnt_config.unit);
pcnt_counter_clear(pcnt_config.unit);
// 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) {

View File

@ -29,7 +29,7 @@
#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);
#endif // MICROPY_INCLUDED_ESP32S2_PERIPHERALS_PCNT_HANDLER_H