2020-10-27 14:42:13 -04:00
|
|
|
/*
|
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
2020-11-08 00:42:32 -05:00
|
|
|
* Copyright (c) 2020 microDev
|
2020-10-27 14:42:13 -04:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "common-hal/rotaryio/IncrementalEncoder.h"
|
2021-11-10 12:07:45 -05:00
|
|
|
#include "shared-bindings/rotaryio/IncrementalEncoder.h"
|
2020-11-08 00:42:32 -05:00
|
|
|
#include "common-hal/microcontroller/Pin.h"
|
2020-10-27 14:42:13 -04:00
|
|
|
|
|
|
|
#include "py/runtime.h"
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
void common_hal_rotaryio_incrementalencoder_construct(rotaryio_incrementalencoder_obj_t *self,
|
|
|
|
const mcu_pin_obj_t *pin_a, const mcu_pin_obj_t *pin_b) {
|
2020-11-08 00:42:32 -05:00
|
|
|
claim_pin(pin_a);
|
|
|
|
claim_pin(pin_b);
|
2020-10-27 14:42:13 -04:00
|
|
|
|
|
|
|
// Prepare configuration for the PCNT unit
|
2022-05-24 22:32:17 -04:00
|
|
|
pcnt_config_t pcnt_config_channel_0 = {
|
2020-10-27 14:42:13 -04:00
|
|
|
// Set PCNT input signal and control GPIOs
|
2020-11-08 00:42:32 -05:00
|
|
|
.pulse_gpio_num = pin_a->number,
|
|
|
|
.ctrl_gpio_num = pin_b->number,
|
2020-10-27 14:42:13 -04:00
|
|
|
.channel = PCNT_CHANNEL_0,
|
|
|
|
// What to do on the positive / negative edge of pulse input?
|
2022-05-24 22:32:17 -04:00
|
|
|
.pos_mode = PCNT_COUNT_DEC, // Count up on the positive edge
|
|
|
|
.neg_mode = PCNT_COUNT_INC, // Keep the counter value on the negative edge
|
2020-10-27 14:42:13 -04:00
|
|
|
// What to do when control input is low or high?
|
|
|
|
.lctrl_mode = PCNT_MODE_REVERSE, // Reverse counting direction if low
|
|
|
|
.hctrl_mode = PCNT_MODE_KEEP, // Keep the primary counter mode if high
|
|
|
|
};
|
|
|
|
|
2022-05-24 22:32:17 -04:00
|
|
|
// Allocate and initialize PCNT unit, CHANNEL_0.
|
|
|
|
const int8_t unit = peripherals_pcnt_init(&pcnt_config_channel_0);
|
2020-11-08 00:42:32 -05:00
|
|
|
if (unit == -1) {
|
|
|
|
mp_raise_RuntimeError(translate("All PCNT units in use"));
|
|
|
|
}
|
2020-10-27 14:42:13 -04:00
|
|
|
|
2022-05-24 22:32:17 -04:00
|
|
|
pcnt_config_t pcnt_config_channel_1 = {
|
|
|
|
// Set PCNT input signal and control GPIOs
|
|
|
|
.pulse_gpio_num = pin_b->number, // Pins are reversed from above
|
|
|
|
.ctrl_gpio_num = pin_a->number,
|
|
|
|
.channel = PCNT_CHANNEL_1,
|
|
|
|
// What to do on the positive / negative edge of pulse input?
|
|
|
|
.pos_mode = PCNT_COUNT_DEC, // Count up on the positive edge
|
|
|
|
.neg_mode = PCNT_COUNT_INC, // Keep the counter value on the negative edge
|
|
|
|
// What to do when control input is low or high?
|
|
|
|
.lctrl_mode = PCNT_MODE_KEEP, // Keep the primary counter mode if low
|
|
|
|
.hctrl_mode = PCNT_MODE_REVERSE, // Reverse counting direction if high
|
|
|
|
.unit = unit,
|
|
|
|
};
|
2022-03-28 03:55:04 -04:00
|
|
|
|
2022-05-24 22:32:17 -04:00
|
|
|
// Reinitalize same unit, CHANNEL_1 with different parameters.
|
|
|
|
peripherals_pcnt_reinit(&pcnt_config_channel_1);
|
2022-03-28 03:55:04 -04:00
|
|
|
|
2020-11-08 00:42:32 -05:00
|
|
|
self->pin_a = pin_a->number;
|
|
|
|
self->pin_b = pin_b->number;
|
|
|
|
self->unit = (pcnt_unit_t)unit;
|
2020-10-27 14:42:13 -04:00
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
bool common_hal_rotaryio_incrementalencoder_deinited(rotaryio_incrementalencoder_obj_t *self) {
|
2020-11-08 00:42:32 -05:00
|
|
|
return self->unit == PCNT_UNIT_MAX;
|
2020-10-27 14:42:13 -04:00
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
void common_hal_rotaryio_incrementalencoder_deinit(rotaryio_incrementalencoder_obj_t *self) {
|
2020-10-27 14:42:13 -04:00
|
|
|
if (common_hal_rotaryio_incrementalencoder_deinited(self)) {
|
|
|
|
return;
|
|
|
|
}
|
2020-11-08 00:42:32 -05:00
|
|
|
reset_pin_number(self->pin_a);
|
|
|
|
reset_pin_number(self->pin_b);
|
|
|
|
peripherals_pcnt_deinit(&self->unit);
|
2020-10-27 14:42:13 -04:00
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
mp_int_t common_hal_rotaryio_incrementalencoder_get_position(rotaryio_incrementalencoder_obj_t *self) {
|
2020-11-08 00:42:32 -05:00
|
|
|
int16_t count;
|
|
|
|
pcnt_get_counter_value(self->unit, &count);
|
2022-03-28 03:55:04 -04:00
|
|
|
|
2022-04-06 13:13:59 -04:00
|
|
|
return (count + self->position) / self->divisor;
|
2020-10-27 14:42:13 -04:00
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
void common_hal_rotaryio_incrementalencoder_set_position(rotaryio_incrementalencoder_obj_t *self,
|
|
|
|
mp_int_t new_position) {
|
2022-04-06 13:13:59 -04:00
|
|
|
self->position = new_position * self->divisor;
|
2020-11-08 00:42:32 -05:00
|
|
|
pcnt_counter_clear(self->unit);
|
2020-10-27 14:42:13 -04:00
|
|
|
}
|
2021-10-15 12:47:13 -04:00
|
|
|
|
|
|
|
mp_int_t common_hal_rotaryio_incrementalencoder_get_divisor(rotaryio_incrementalencoder_obj_t *self) {
|
2022-03-28 03:55:04 -04:00
|
|
|
return self->divisor;
|
2021-10-15 12:47:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void common_hal_rotaryio_incrementalencoder_set_divisor(rotaryio_incrementalencoder_obj_t *self, mp_int_t divisor) {
|
2022-03-28 03:55:04 -04:00
|
|
|
self->divisor = divisor;
|
2021-10-15 12:47:13 -04:00
|
|
|
}
|