2018-05-25 21:39:16 -04:00
|
|
|
/*
|
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2018 Scott Shawcroft for Adafruit Industries
|
|
|
|
*
|
|
|
|
* 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-04-08 17:32:36 -04:00
|
|
|
#include "shared-module/rotaryio/IncrementalEncoder.h"
|
2018-05-25 21:39:16 -04:00
|
|
|
|
|
|
|
#include "atmel_start_pins.h"
|
|
|
|
|
2019-04-04 16:04:11 -04:00
|
|
|
#include "eic_handler.h"
|
2018-06-15 19:16:21 -04:00
|
|
|
#include "samd/external_interrupts.h"
|
2018-05-25 21:39:16 -04:00
|
|
|
#include "py/runtime.h"
|
2018-07-31 19:53:54 -04:00
|
|
|
#include "supervisor/shared/translate.h"
|
2018-05-25 21:39:16 -04:00
|
|
|
|
|
|
|
void common_hal_rotaryio_incrementalencoder_construct(rotaryio_incrementalencoder_obj_t* self,
|
2018-05-29 21:21:19 -04:00
|
|
|
const mcu_pin_obj_t* pin_a, const mcu_pin_obj_t* pin_b) {
|
2019-08-20 16:23:17 -04:00
|
|
|
if (!pin_a->has_extint || !pin_b->has_extint) {
|
2018-07-31 19:53:54 -04:00
|
|
|
mp_raise_RuntimeError(translate("Both pins must support hardware interrupts"));
|
2018-05-29 21:21:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: The SAMD51 has a peripheral dedicated to quadrature encoder debugging. Use it instead
|
|
|
|
// of the external interrupt.
|
|
|
|
|
|
|
|
if (eic_get_enable()) {
|
|
|
|
if (!eic_channel_free(pin_a->extint_channel) || !eic_channel_free(pin_b->extint_channel)) {
|
2018-07-31 19:53:54 -04:00
|
|
|
mp_raise_RuntimeError(translate("A hardware interrupt channel is already in use"));
|
2018-05-29 21:21:19 -04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
turn_on_external_interrupt_controller();
|
|
|
|
}
|
|
|
|
|
|
|
|
// These default settings apply when the EIC isn't yet enabled.
|
|
|
|
self->eic_channel_a = pin_a->extint_channel;
|
|
|
|
self->eic_channel_b = pin_b->extint_channel;
|
2018-07-31 17:01:01 -04:00
|
|
|
self->pin_a = pin_a->number;
|
|
|
|
self->pin_b = pin_b->number;
|
2018-05-29 21:21:19 -04:00
|
|
|
|
|
|
|
gpio_set_pin_function(self->pin_a, GPIO_PIN_FUNCTION_A);
|
|
|
|
gpio_set_pin_pull_mode(self->pin_a, GPIO_PULL_UP);
|
|
|
|
|
|
|
|
gpio_set_pin_function(self->pin_b, GPIO_PIN_FUNCTION_A);
|
|
|
|
gpio_set_pin_pull_mode(self->pin_b, GPIO_PULL_UP);
|
|
|
|
|
|
|
|
set_eic_channel_data(self->eic_channel_a, (void*) self);
|
|
|
|
set_eic_channel_data(self->eic_channel_b, (void*) self);
|
|
|
|
|
2018-06-01 11:00:40 -04:00
|
|
|
self->position = 0;
|
|
|
|
self->quarter_count = 0;
|
|
|
|
|
2021-04-08 17:32:36 -04:00
|
|
|
shared_module_softencoder_state_init(self,
|
2018-06-01 11:00:40 -04:00
|
|
|
((uint8_t) gpio_get_pin_level(self->pin_a) << 1) |
|
2021-04-08 17:32:36 -04:00
|
|
|
(uint8_t) gpio_get_pin_level(self->pin_b));
|
2018-05-29 21:21:19 -04:00
|
|
|
|
2018-08-31 17:46:03 -04:00
|
|
|
claim_pin(pin_a);
|
|
|
|
claim_pin(pin_b);
|
|
|
|
|
2019-04-04 16:04:11 -04:00
|
|
|
set_eic_handler(self->eic_channel_a, EIC_HANDLER_INCREMENTAL_ENCODER);
|
|
|
|
turn_on_eic_channel(self->eic_channel_a, EIC_CONFIG_SENSE0_BOTH_Val);
|
|
|
|
|
|
|
|
set_eic_handler(self->eic_channel_b, EIC_HANDLER_INCREMENTAL_ENCODER);
|
|
|
|
turn_on_eic_channel(self->eic_channel_b, EIC_CONFIG_SENSE0_BOTH_Val);
|
2018-05-25 21:39:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool common_hal_rotaryio_incrementalencoder_deinited(rotaryio_incrementalencoder_obj_t* self) {
|
2018-05-29 21:21:19 -04:00
|
|
|
return self->pin_a == NO_PIN;
|
2018-05-25 21:39:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void common_hal_rotaryio_incrementalencoder_deinit(rotaryio_incrementalencoder_obj_t* self) {
|
2018-05-29 21:21:19 -04:00
|
|
|
if (common_hal_rotaryio_incrementalencoder_deinited(self)) {
|
|
|
|
return;
|
|
|
|
}
|
2019-04-04 16:04:11 -04:00
|
|
|
|
|
|
|
set_eic_handler(self->eic_channel_a, EIC_HANDLER_NO_INTERRUPT);
|
2018-05-29 21:21:19 -04:00
|
|
|
turn_off_eic_channel(self->eic_channel_a);
|
2019-04-04 16:04:11 -04:00
|
|
|
|
|
|
|
set_eic_handler(self->eic_channel_b, EIC_HANDLER_NO_INTERRUPT);
|
2018-05-29 21:21:19 -04:00
|
|
|
turn_off_eic_channel(self->eic_channel_b);
|
2019-04-04 16:04:11 -04:00
|
|
|
|
2018-08-31 17:46:03 -04:00
|
|
|
reset_pin_number(self->pin_a);
|
2018-05-29 21:21:19 -04:00
|
|
|
self->pin_a = NO_PIN;
|
2019-04-04 16:04:11 -04:00
|
|
|
|
2018-08-31 17:46:03 -04:00
|
|
|
reset_pin_number(self->pin_b);
|
2018-05-29 21:21:19 -04:00
|
|
|
self->pin_b = NO_PIN;
|
2018-05-25 21:39:16 -04:00
|
|
|
}
|
|
|
|
|
2018-05-29 21:21:19 -04:00
|
|
|
void incrementalencoder_interrupt_handler(uint8_t channel) {
|
|
|
|
rotaryio_incrementalencoder_obj_t* self = get_eic_channel_data(channel);
|
2018-06-01 11:00:40 -04:00
|
|
|
|
2021-04-08 17:32:36 -04:00
|
|
|
uint8_t new_state =
|
2018-06-01 11:00:40 -04:00
|
|
|
((uint8_t) gpio_get_pin_level(self->pin_a) << 1) |
|
|
|
|
(uint8_t) gpio_get_pin_level(self->pin_b);
|
|
|
|
|
2021-04-08 17:32:36 -04:00
|
|
|
shared_module_softencoder_state_update(self, new_state);
|
2018-05-25 21:39:16 -04:00
|
|
|
}
|