diff --git a/ports/esp32/machine_encoder.c b/ports/esp32/machine_encoder.c index dd21f484405d..22b111ec613c 100644 --- a/ports/esp32/machine_encoder.c +++ b/ports/esp32/machine_encoder.c @@ -57,17 +57,20 @@ See also #include "driver/pulse_cnt.h" #include "soc/pcnt_struct.h" #include "esp_err.h" +#include "esp_log.h" #include "machine_encoder.h" -/**/ +/* #include "hal/ledc_hal.h" #include "driver/ledc.h" #include "hal/gpio_hal.h" -/**/ +*/ #include "py/mpprint.h" -// #define PWM_DBG(...) -#define PWM_DBG(...) mp_printf(&mp_plat_print, __VA_ARGS__); mp_printf(&mp_plat_print, "\n"); +#define PWM_DBG(...) +//#define PWM_DBG(...) mp_printf(&mp_plat_print, __VA_ARGS__); mp_printf(&mp_plat_print, "\n"); +//const static char *TAG = "Encoder"; +//#define PWM_DBG(...) ESP_LOGE(TAG, __VA_ARGS__) #define GET_INT mp_obj_get_int_truncated // #define GET_INT mp_obj_get_ll_int // need PR: py\obj.c: Get 64-bit integer arg. #80896 @@ -75,15 +78,9 @@ See also STATIC pcnt_isr_handle_t pcnt_isr_handle = NULL; STATIC mp_pcnt_obj_t *pcnts[PCNT_UNIT_MAX] = {}; -#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 1, 0) #define EVT_THRES_0 PCNT_EVT_THRES_0 #define EVT_THRES_1 PCNT_EVT_THRES_1 #define EVT_ZERO PCNT_EVT_ZERO -#else -#define EVT_THRES_0 (1 << PCNT_EVT_THRES_0) -#define EVT_THRES_1 (1 << PCNT_EVT_THRES_1) -#define EVT_ZERO (1 << PCNT_EVT_ZERO) -#endif /* Decode what PCNT's unit originated an interrupt * and pass this information together with the event type @@ -113,6 +110,10 @@ STATIC void IRAM_ATTR pcnt_intr_handler(void *arg) { self->counter -= INT16_ROLL; } + PWM_DBG("counter=%ld", self->counter); + PWM_DBG("counter_match1=%ld, counter_match2=%ld", self->counter_match1, self->counter_match2); + PWM_DBG("match1=%ld, match2=%ld", self->match1, self->match2); + self->status = 0; if (PCNT.status_unit[id].THRES1_LAT) { if (self->counter == self->counter_match1) { @@ -231,9 +232,7 @@ void machine_encoder_deinit_all(void) { pcnt_deinit(pcnts[id]); } if (pcnt_isr_handle != NULL) { - #if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 2, 0) check_esp_err(pcnt_isr_unregister(pcnt_isr_handle)); - #endif pcnt_isr_handle = NULL; } } @@ -355,17 +354,22 @@ STATIC mp_obj_t machine_PCNT_irq(size_t n_pos_args, const mp_obj_t *pos_args, mp if (args[ARG_value].u_obj != MP_OBJ_NULL) { self->match1 = GET_INT(args[ARG_value].u_obj); self->counter_match1 = self->match1 % INT16_ROLL; + PWM_DBG("self->match1=%ld, self->counter_match1=%ld", self->match1, self->counter_match1); check_esp_err(pcnt_set_event_value(self->unit, EVT_THRES_1, (int16_t)self->counter_match1)); self->counter_match1 = self->match1 - self->counter_match1; + PWM_DBG("self->match1=%ld, self->counter_match1=%ld", self->match1, self->counter_match1); } self->handler_match1 = handler; pcnt_event_enable(self->unit, EVT_THRES_1); - } else if (trigger & EVT_THRES_0) { + } + if (trigger & EVT_THRES_0) { if (args[ARG_value].u_obj != MP_OBJ_NULL) { self->match2 = GET_INT(args[ARG_value].u_obj); self->counter_match2 = self->match2 % INT16_ROLL; + PWM_DBG("self->match2=%ld, self->counter_match2=%ld", self->match2, self->counter_match2); check_esp_err(pcnt_set_event_value(self->unit, EVT_THRES_0, (int16_t)self->counter_match2)); self->counter_match2 = self->match2 - self->counter_match2; + PWM_DBG("self->match2=%ld, self->counter_match2=%ld", self->match2, self->counter_match2); } self->handler_match2 = handler; pcnt_event_enable(self->unit, EVT_THRES_0); @@ -374,10 +378,6 @@ STATIC mp_obj_t machine_PCNT_irq(size_t n_pos_args, const mp_obj_t *pos_args, mp self->handler_zero = handler; pcnt_event_enable(self->unit, EVT_ZERO); } - /* - check_esp_err(pcnt_counter_clear(self->unit)); - self->counter = 0; - */ } return mp_const_none; } @@ -466,7 +466,7 @@ STATIC void mp_machine_Counter_init_helper(mp_pcnt_obj_t *self, size_t n_args, c check_esp_err(pcnt_unit_config(&r_enc_config)); -#if 1 +#if 0 // reconfigure for PWM esp_rom_gpio_pad_select_gpio(r_enc_config.pulse_gpio_num); /* diff --git a/ports/esp32/machine_encoder.h b/ports/esp32/machine_encoder.h index 79c44d720f54..36838efd3739 100644 --- a/ports/esp32/machine_encoder.h +++ b/ports/esp32/machine_encoder.h @@ -1,7 +1,9 @@ #ifndef MICROPY_INCLUDED_MACHINE_ENCODER_H #define MICROPY_INCLUDED_MACHINE_ENCODER_H -#define INT16_ROLL 32767 +typedef int32_t counter_t; // int64_t + +#define INT16_ROLL ((counter_t)32767) #define FILTER_MAX 1023 @@ -20,12 +22,12 @@ typedef struct _mp_pcnt_obj_t { int aPinNumber; int bPinNumber; - volatile int64_t counter; + volatile counter_t counter; - int64_t match1; - int64_t match2; - int64_t counter_match1; - int64_t counter_match2; + counter_t match1; + counter_t match2; + counter_t counter_match1; + counter_t counter_match2; mp_obj_t handler_match1; mp_obj_t handler_match2; mp_obj_t handler_zero; diff --git a/ports/esp32/machine_pwm.c b/ports/esp32/machine_pwm.c index 5ecdb01b9fdf..845f7f090832 100644 --- a/ports/esp32/machine_pwm.c +++ b/ports/esp32/machine_pwm.c @@ -180,6 +180,7 @@ STATIC void pwm_deinit(int mode, int channel) { } } + /* int pin = chans[mode][channel].pin; if (pin >= 0) { // Mark it unused, and tell the hardware to stop routing @@ -195,6 +196,7 @@ STATIC void pwm_deinit(int mode, int channel) { // reconfigure as GPIO //gpio_set_direction(pin, GPIO_MODE_INPUT_OUTPUT); } + */ unregister_channel(mode, channel); } } @@ -229,7 +231,7 @@ STATIC void configure_channel(machine_pwm_obj_t *self) { PWM_DBG("cfg.duty=%d, cfg.flags.output_invert=%d", cfg.duty, cfg.flags.output_invert); } check_esp_err(ledc_channel_config(&cfg)); -/* + // reconfigure PWM output for Counter input gpio_set_direction(self->pin, GPIO_MODE_INPUT_OUTPUT); if (self->mode == LEDC_LOW_SPEED_MODE) { @@ -240,7 +242,7 @@ STATIC void configure_channel(machine_pwm_obj_t *self) { PWM_DBG("self->pin=%d, LEDC_HS_SIG_OUT0_IDX + self->channel=%d", self->pin, LEDC_HS_SIG_OUT0_IDX + self->channel); #endif } -*/ + /* test.py: ```