|
3 | 3 | *
|
4 | 4 | * The MIT License (MIT)
|
5 | 5 | *
|
6 |
| - * Copyright (c) 2018 Scott Shawcroft for Adafruit Industries |
| 6 | + * Copyright (c) 2020 microDev |
7 | 7 | *
|
8 | 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
9 | 9 | * of this software and associated documentation files (the "Software"), to deal
|
|
25 | 25 | */
|
26 | 26 |
|
27 | 27 | #include "common-hal/rotaryio/IncrementalEncoder.h"
|
| 28 | +#include "common-hal/microcontroller/Pin.h" |
28 | 29 |
|
29 | 30 | #include "py/runtime.h"
|
30 | 31 | #include "supervisor/shared/translate.h"
|
31 | 32 |
|
32 |
| -#include "driver/pcnt.h" |
33 |
| - |
34 |
| -static void pcnt_reset(int unit) { |
35 |
| - // Initialize PCNT's counter |
36 |
| - pcnt_counter_pause(unit); |
37 |
| - pcnt_counter_clear(unit); |
38 |
| - |
39 |
| - // Everything is set up, now go to counting |
40 |
| - pcnt_counter_resume(unit); |
41 |
| -} |
| 33 | +void common_hal_rotaryio_incrementalencoder_construct(rotaryio_incrementalencoder_obj_t* self, |
| 34 | + const mcu_pin_obj_t* pin_a, const mcu_pin_obj_t* pin_b) { |
| 35 | + claim_pin(pin_a); |
| 36 | + claim_pin(pin_b); |
42 | 37 |
|
43 |
| -static void pcnt_init(int unit, rotaryio_incrementalencoder_obj_t* self) { |
44 | 38 | // Prepare configuration for the PCNT unit
|
45 |
| - pcnt_config_t pcnt_config = { |
| 39 | + const pcnt_config_t pcnt_config = { |
46 | 40 | // Set PCNT input signal and control GPIOs
|
47 |
| - .pulse_gpio_num = self->pin_a->number, |
48 |
| - .ctrl_gpio_num = self->pin_b->number, |
| 41 | + .pulse_gpio_num = pin_a->number, |
| 42 | + .ctrl_gpio_num = pin_b->number, |
49 | 43 | .channel = PCNT_CHANNEL_0,
|
50 |
| - .unit = unit, |
51 | 44 | // What to do on the positive / negative edge of pulse input?
|
52 | 45 | .pos_mode = PCNT_COUNT_DEC, // Count up on the positive edge
|
53 | 46 | .neg_mode = PCNT_COUNT_INC, // Keep the counter value on the negative edge
|
54 | 47 | // What to do when control input is low or high?
|
55 | 48 | .lctrl_mode = PCNT_MODE_REVERSE, // Reverse counting direction if low
|
56 | 49 | .hctrl_mode = PCNT_MODE_KEEP, // Keep the primary counter mode if high
|
57 | 50 | };
|
58 |
| - // Initialize PCNT unit |
59 |
| - pcnt_unit_config(&pcnt_config); |
60 |
| - |
61 |
| - // Configure channel 1 |
62 |
| - pcnt_config.pulse_gpio_num = self->pin_b->number; |
63 |
| - pcnt_config.ctrl_gpio_num = self->pin_a->number; |
64 |
| - pcnt_config.channel = PCNT_CHANNEL_1; |
65 |
| - pcnt_config.pos_mode = PCNT_COUNT_INC; |
66 |
| - pcnt_config.neg_mode = PCNT_COUNT_DEC; |
67 |
| - pcnt_unit_config(&pcnt_config); |
68 |
| - |
69 |
| - // Configure and enable the input filter |
70 |
| - pcnt_set_filter_value(unit, 100); |
71 |
| - pcnt_filter_enable(unit); |
72 |
| - |
73 |
| - pcnt_reset(unit); |
74 |
| -} |
75 |
| - |
76 |
| -void common_hal_rotaryio_incrementalencoder_construct(rotaryio_incrementalencoder_obj_t* self, |
77 |
| - const mcu_pin_obj_t* pin_a, const mcu_pin_obj_t* pin_b) { |
78 |
| - claim_pin(pin_a); |
79 |
| - claim_pin(pin_b); |
80 |
| - |
81 |
| - self->pin_a = pin_a; |
82 |
| - self->pin_b = pin_b; |
83 | 51 |
|
84 |
| - self->position = 0; |
| 52 | + // Initialize PCNT unit |
| 53 | + const int8_t unit = peripherals_pcnt_init(pcnt_config); |
| 54 | + if (unit == -1) { |
| 55 | + mp_raise_RuntimeError(translate("All PCNT units in use")); |
| 56 | + } |
85 | 57 |
|
86 |
| - pcnt_init(PCNT_UNIT_0, self); |
| 58 | + self->pin_a = pin_a->number; |
| 59 | + self->pin_b = pin_b->number; |
| 60 | + self->unit = (pcnt_unit_t)unit; |
87 | 61 | }
|
88 | 62 |
|
89 | 63 | bool common_hal_rotaryio_incrementalencoder_deinited(rotaryio_incrementalencoder_obj_t* self) {
|
90 |
| - return self->pin_a == NULL; |
| 64 | + return self->unit == PCNT_UNIT_MAX; |
91 | 65 | }
|
92 | 66 |
|
93 | 67 | void common_hal_rotaryio_incrementalencoder_deinit(rotaryio_incrementalencoder_obj_t* self) {
|
94 | 68 | if (common_hal_rotaryio_incrementalencoder_deinited(self)) {
|
95 | 69 | return;
|
96 | 70 | }
|
97 |
| - |
98 |
| - reset_pin_number(self->pin_a->number); |
99 |
| - self->pin_a = NULL; |
100 |
| - |
101 |
| - reset_pin_number(self->pin_b->number); |
102 |
| - self->pin_b = NULL; |
| 71 | + reset_pin_number(self->pin_a); |
| 72 | + reset_pin_number(self->pin_b); |
| 73 | + peripherals_pcnt_deinit(&self->unit); |
103 | 74 | }
|
104 | 75 |
|
105 | 76 | mp_int_t common_hal_rotaryio_incrementalencoder_get_position(rotaryio_incrementalencoder_obj_t* self) {
|
106 |
| - int16_t count = 0; |
107 |
| - pcnt_get_counter_value(PCNT_UNIT_0, &count); |
108 |
| - return self->position+count; |
| 77 | + int16_t count; |
| 78 | + pcnt_get_counter_value(self->unit, &count); |
| 79 | + return (count/2)+self->position; |
109 | 80 | }
|
110 | 81 |
|
111 | 82 | void common_hal_rotaryio_incrementalencoder_set_position(rotaryio_incrementalencoder_obj_t* self,
|
112 | 83 | mp_int_t new_position) {
|
113 | 84 | self->position = new_position;
|
114 |
| - pcnt_reset(PCNT_UNIT_0); |
| 85 | + pcnt_counter_clear(self->unit); |
115 | 86 | }
|
0 commit comments