From ee21cc163f7a6ae07239f13bdb1cd479b4180400 Mon Sep 17 00:00:00 2001 From: Nick Moore Date: Tue, 5 Feb 2019 13:26:36 +1100 Subject: [PATCH 1/5] Start on rotaryio.IncrementalEncoder adafruit/circuitpython#1045 (does nothing yet!) --- ports/nrf/Makefile | 2 + .../common-hal/rotaryio/IncrementalEncoder.c | 62 +++++++++++++++++++ .../common-hal/rotaryio/IncrementalEncoder.h | 44 +++++++++++++ ports/nrf/common-hal/rotaryio/__init__.c | 1 + ports/nrf/mpconfigport.h | 2 + 5 files changed, 111 insertions(+) create mode 100644 ports/nrf/common-hal/rotaryio/IncrementalEncoder.c create mode 100644 ports/nrf/common-hal/rotaryio/IncrementalEncoder.h create mode 100644 ports/nrf/common-hal/rotaryio/__init__.c diff --git a/ports/nrf/Makefile b/ports/nrf/Makefile index 2f762e2a3a..ef11baddd2 100755 --- a/ports/nrf/Makefile +++ b/ports/nrf/Makefile @@ -166,6 +166,8 @@ SRC_COMMON_HAL += \ pulseio/PulseIn.c \ pulseio/PulseOut.c \ pulseio/__init__.c \ + rotaryio/__init__.c \ + rotaryio/IncrementalEncoder.c \ supervisor/Runtime.c \ supervisor/__init__.c \ time/__init__.c \ diff --git a/ports/nrf/common-hal/rotaryio/IncrementalEncoder.c b/ports/nrf/common-hal/rotaryio/IncrementalEncoder.c new file mode 100644 index 0000000000..2818db805b --- /dev/null +++ b/ports/nrf/common-hal/rotaryio/IncrementalEncoder.c @@ -0,0 +1,62 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2019 Nick Moore 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" + +#include "py/runtime.h" + +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) { + + claim_pin(pin_a); + claim_pin(pin_b); +} + +bool common_hal_rotaryio_incrementalencoder_deinited(rotaryio_incrementalencoder_obj_t* self) { + return self->pin_a == NO_PIN; +} + +void common_hal_rotaryio_incrementalencoder_deinit(rotaryio_incrementalencoder_obj_t* self) { + if (common_hal_rotaryio_incrementalencoder_deinited(self)) { + return; + } + reset_pin_number(self->pin_a); + self->pin_a = NO_PIN; + reset_pin_number(self->pin_b); + self->pin_b = NO_PIN; +} + +mp_int_t common_hal_rotaryio_incrementalencoder_get_position(rotaryio_incrementalencoder_obj_t* self) { + return self->position; +} + +void common_hal_rotaryio_incrementalencoder_set_position(rotaryio_incrementalencoder_obj_t* self, + mp_int_t new_position) { + self->position = new_position; +} + +void incrementalencoder_interrupt_handler(uint8_t channel) { +} diff --git a/ports/nrf/common-hal/rotaryio/IncrementalEncoder.h b/ports/nrf/common-hal/rotaryio/IncrementalEncoder.h new file mode 100644 index 0000000000..73723bc0c4 --- /dev/null +++ b/ports/nrf/common-hal/rotaryio/IncrementalEncoder.h @@ -0,0 +1,44 @@ +/* + * 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. + */ + +#ifndef MICROPY_INCLUDED_NRF_COMMON_HAL_ROTARYIO_INCREMENTALENCODER_H +#define MICROPY_INCLUDED_NRF_COMMON_HAL_ROTARYIO_INCREMENTALENCODER_H + +#include "common-hal/microcontroller/Pin.h" + +#include "py/obj.h" + +typedef struct { + mp_obj_base_t base; + uint8_t pin_a; + uint8_t pin_b; + mp_int_t position; +} rotaryio_incrementalencoder_obj_t; + + +void incrementalencoder_interrupt_handler(uint8_t channel); + +#endif // MICROPY_INCLUDED_NRF_COMMON_HAL_ROTARYIO_INCREMENTALENCODER_H diff --git a/ports/nrf/common-hal/rotaryio/__init__.c b/ports/nrf/common-hal/rotaryio/__init__.c new file mode 100644 index 0000000000..0aae79c26a --- /dev/null +++ b/ports/nrf/common-hal/rotaryio/__init__.c @@ -0,0 +1 @@ +// No rotaryio module functions. diff --git a/ports/nrf/mpconfigport.h b/ports/nrf/mpconfigport.h index f632651291..17f7f574d7 100644 --- a/ports/nrf/mpconfigport.h +++ b/ports/nrf/mpconfigport.h @@ -169,6 +169,7 @@ extern const struct _mp_obj_module_t busio_module; extern const struct _mp_obj_module_t board_module; extern const struct _mp_obj_module_t os_module; extern const struct _mp_obj_module_t random_module; +extern const struct _mp_obj_module_t rotaryio_module; extern const struct _mp_obj_module_t storage_module; extern const struct _mp_obj_module_t struct_module; extern const struct _mp_obj_module_t time_module; @@ -205,6 +206,7 @@ extern const struct _mp_obj_module_t touchio_module; { MP_OBJ_NEW_QSTR (MP_QSTR_bitbangio ), (mp_obj_t)&bitbangio_module }, \ { MP_OBJ_NEW_QSTR (MP_QSTR_os ), (mp_obj_t)&os_module }, \ { MP_OBJ_NEW_QSTR (MP_QSTR_random ), (mp_obj_t)&random_module }, \ + { MP_OBJ_NEW_QSTR (MP_QSTR_rotaryio ), (mp_obj_t)&rotaryio_module }, \ { MP_OBJ_NEW_QSTR (MP_QSTR_storage ), (mp_obj_t)&storage_module }, \ { MP_OBJ_NEW_QSTR (MP_QSTR_struct ), (mp_obj_t)&struct_module }, \ { MP_OBJ_NEW_QSTR (MP_QSTR_supervisor ), (mp_obj_t)&supervisor_module }, \ From 21eb7e8e649e216664bdadd24a4e4e4cae6467b0 Mon Sep 17 00:00:00 2001 From: Nick Moore Date: Tue, 5 Feb 2019 14:13:16 +1100 Subject: [PATCH 2/5] GPIOTE handlers for rotaryio.IncrementalEncoder adafruit/circuitpython#1045 --- .../common-hal/rotaryio/IncrementalEncoder.c | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/ports/nrf/common-hal/rotaryio/IncrementalEncoder.c b/ports/nrf/common-hal/rotaryio/IncrementalEncoder.c index 2818db805b..b55ce75d6b 100644 --- a/ports/nrf/common-hal/rotaryio/IncrementalEncoder.c +++ b/ports/nrf/common-hal/rotaryio/IncrementalEncoder.c @@ -25,12 +25,41 @@ */ #include "common-hal/rotaryio/IncrementalEncoder.h" +#include "nrfx_gpiote.h" #include "py/runtime.h" +// obj array to map pin -> self since nrfx hide the mapping +static rotaryio_incrementalencoder_obj_t *_objs[NUMBER_OF_PINS]; + +static void _intr_handler(nrfx_gpiote_pin_t pin, nrf_gpiote_polarity_t action) { + rotaryio_incrementalencoder_obj_t *self = _objs[pin]; + if (!self) return; + + self->position++; +} + 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) { + self->pin_a = pin_a->number; + self->pin_b = pin_b->number; + + _objs[self->pin_a] = self; + _objs[self->pin_b] = self; + + nrfx_gpiote_in_config_t cfg = { + .sense = NRF_GPIOTE_POLARITY_TOGGLE, + .pull = NRF_GPIO_PIN_NOPULL, + .is_watcher = false, + .hi_accuracy = true, + .skip_gpio_setup = false + }; + nrfx_gpiote_in_init(self->pin_a, &cfg, _intr_handler); + nrfx_gpiote_in_init(self->pin_b, &cfg, _intr_handler); + nrfx_gpiote_in_event_enable(self->pin_a, true); + nrfx_gpiote_in_event_enable(self->pin_b, true); + claim_pin(pin_a); claim_pin(pin_b); } @@ -43,6 +72,9 @@ void common_hal_rotaryio_incrementalencoder_deinit(rotaryio_incrementalencoder_o if (common_hal_rotaryio_incrementalencoder_deinited(self)) { return; } + _objs[self->pin_a] = NULL; + _objs[self->pin_b] = NULL; + reset_pin_number(self->pin_a); self->pin_a = NO_PIN; reset_pin_number(self->pin_b); From 95454ecde0af2772b9bb009acac444bfa682088d Mon Sep 17 00:00:00 2001 From: Nick Moore Date: Tue, 5 Feb 2019 15:50:35 +1100 Subject: [PATCH 3/5] useful output from rotaryio adafruit/circuitpython#1045 --- .../common-hal/rotaryio/IncrementalEncoder.c | 24 ++++++++++++++----- .../common-hal/rotaryio/IncrementalEncoder.h | 1 + 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/ports/nrf/common-hal/rotaryio/IncrementalEncoder.c b/ports/nrf/common-hal/rotaryio/IncrementalEncoder.c index b55ce75d6b..a4cf60c878 100644 --- a/ports/nrf/common-hal/rotaryio/IncrementalEncoder.c +++ b/ports/nrf/common-hal/rotaryio/IncrementalEncoder.c @@ -29,14 +29,25 @@ #include "py/runtime.h" -// obj array to map pin -> self since nrfx hide the mapping +#include + +// obj array to map pin number -> self since nrfx hide the mapping static rotaryio_incrementalencoder_obj_t *_objs[NUMBER_OF_PINS]; static void _intr_handler(nrfx_gpiote_pin_t pin, nrf_gpiote_polarity_t action) { rotaryio_incrementalencoder_obj_t *self = _objs[pin]; if (!self) return; - self->position++; + // reads a state 0 .. 3 *in order*. + uint8_t new_state = nrf_gpio_pin_read(self->pin_a); + new_state = (new_state << 1) + (new_state ^ nrf_gpio_pin_read(self->pin_b)); + + uint8_t change = (new_state - self->state) & 0x03; + if (change == 1) self->position++; + else if (change == 3) self->position--; + // ignore other state transitions + + self->state = new_state; } void common_hal_rotaryio_incrementalencoder_construct(rotaryio_incrementalencoder_obj_t* self, @@ -75,9 +86,13 @@ void common_hal_rotaryio_incrementalencoder_deinit(rotaryio_incrementalencoder_o _objs[self->pin_a] = NULL; _objs[self->pin_b] = NULL; + nrfx_gpiote_in_event_disable(self->pin_a); + nrfx_gpiote_in_event_disable(self->pin_b); + nrfx_gpiote_in_uninit(self->pin_a); + nrfx_gpiote_in_uninit(self->pin_b); reset_pin_number(self->pin_a); - self->pin_a = NO_PIN; reset_pin_number(self->pin_b); + self->pin_a = NO_PIN; self->pin_b = NO_PIN; } @@ -89,6 +104,3 @@ void common_hal_rotaryio_incrementalencoder_set_position(rotaryio_incrementalenc mp_int_t new_position) { self->position = new_position; } - -void incrementalencoder_interrupt_handler(uint8_t channel) { -} diff --git a/ports/nrf/common-hal/rotaryio/IncrementalEncoder.h b/ports/nrf/common-hal/rotaryio/IncrementalEncoder.h index 73723bc0c4..eba857ac77 100644 --- a/ports/nrf/common-hal/rotaryio/IncrementalEncoder.h +++ b/ports/nrf/common-hal/rotaryio/IncrementalEncoder.h @@ -35,6 +35,7 @@ typedef struct { mp_obj_base_t base; uint8_t pin_a; uint8_t pin_b; + uint8_t state; mp_int_t position; } rotaryio_incrementalencoder_obj_t; From a7c349bc6ec04c250895b62ddce871134b81761c Mon Sep 17 00:00:00 2001 From: Nick Moore Date: Tue, 5 Feb 2019 16:40:39 +1100 Subject: [PATCH 4/5] Add quarter-click logic to adafruit/circuitpython#1045 --- ports/nrf/common-hal/rotaryio/IncrementalEncoder.c | 14 ++++++++++++-- ports/nrf/common-hal/rotaryio/IncrementalEncoder.h | 1 + 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/ports/nrf/common-hal/rotaryio/IncrementalEncoder.c b/ports/nrf/common-hal/rotaryio/IncrementalEncoder.c index a4cf60c878..20673c340c 100644 --- a/ports/nrf/common-hal/rotaryio/IncrementalEncoder.c +++ b/ports/nrf/common-hal/rotaryio/IncrementalEncoder.c @@ -43,11 +43,21 @@ static void _intr_handler(nrfx_gpiote_pin_t pin, nrf_gpiote_polarity_t action) { new_state = (new_state << 1) + (new_state ^ nrf_gpio_pin_read(self->pin_b)); uint8_t change = (new_state - self->state) & 0x03; - if (change == 1) self->position++; - else if (change == 3) self->position--; + if (change == 1) self->quarter++; + else if (change == 3) self->quarter--; // ignore other state transitions self->state = new_state; + + // logic from the atmel-samd port: provides some damping and scales movement + // down by 4:1. + if (self->quarter >= 4) { + self->position++; + self->quarter = 0; + } else if (self->quarter <= -4) { + self->position--; + self->quarter = 0; + } } void common_hal_rotaryio_incrementalencoder_construct(rotaryio_incrementalencoder_obj_t* self, diff --git a/ports/nrf/common-hal/rotaryio/IncrementalEncoder.h b/ports/nrf/common-hal/rotaryio/IncrementalEncoder.h index eba857ac77..1d0fe41839 100644 --- a/ports/nrf/common-hal/rotaryio/IncrementalEncoder.h +++ b/ports/nrf/common-hal/rotaryio/IncrementalEncoder.h @@ -36,6 +36,7 @@ typedef struct { uint8_t pin_a; uint8_t pin_b; uint8_t state; + int8_t quarter; mp_int_t position; } rotaryio_incrementalencoder_obj_t; From b9db977a2d9da0bf3f97a67aa1eeaeaebea910e4 Mon Sep 17 00:00:00 2001 From: Nick Moore Date: Thu, 7 Feb 2019 09:47:56 +1100 Subject: [PATCH 5/5] Change pin mode to pullup for adafruit/circuitpython#1045 --- ports/nrf/common-hal/rotaryio/IncrementalEncoder.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ports/nrf/common-hal/rotaryio/IncrementalEncoder.c b/ports/nrf/common-hal/rotaryio/IncrementalEncoder.c index 20673c340c..a0cfd48d56 100644 --- a/ports/nrf/common-hal/rotaryio/IncrementalEncoder.c +++ b/ports/nrf/common-hal/rotaryio/IncrementalEncoder.c @@ -71,7 +71,7 @@ void common_hal_rotaryio_incrementalencoder_construct(rotaryio_incrementalencode nrfx_gpiote_in_config_t cfg = { .sense = NRF_GPIOTE_POLARITY_TOGGLE, - .pull = NRF_GPIO_PIN_NOPULL, + .pull = NRF_GPIO_PIN_PULLUP, .is_watcher = false, .hi_accuracy = true, .skip_gpio_setup = false