Skip to content

Commit

Permalink
BAD
Browse files Browse the repository at this point in the history
  • Loading branch information
IhorNehrutsa committed Jul 28, 2023
1 parent 863a887 commit ce1d19b
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 26 deletions.
36 changes: 18 additions & 18 deletions ports/esp32/machine_encoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,33 +57,30 @@ 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

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
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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);
Expand All @@ -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;
}
Expand Down Expand Up @@ -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);
/*
Expand Down
14 changes: 8 additions & 6 deletions ports/esp32/machine_encoder.h
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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;
Expand Down
6 changes: 4 additions & 2 deletions ports/esp32/machine_pwm.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}
}
Expand Down Expand Up @@ -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) {
Expand All @@ -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:
```
Expand Down

0 comments on commit ce1d19b

Please sign in to comment.