From 0d743224f2c63d6f61fdf0756a1c917f863c2565 Mon Sep 17 00:00:00 2001 From: Neradoc Date: Tue, 13 Apr 2021 14:56:21 +0200 Subject: [PATCH 1/2] add countio to nrf port --- ports/nrf/common-hal/countio/Counter.c | 66 +++++++++++++++++++++++++ ports/nrf/common-hal/countio/Counter.h | 15 ++++++ ports/nrf/common-hal/countio/__init__.c | 1 + ports/nrf/mpconfigport.mk | 2 +- 4 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 ports/nrf/common-hal/countio/Counter.c create mode 100644 ports/nrf/common-hal/countio/Counter.h create mode 100644 ports/nrf/common-hal/countio/__init__.c diff --git a/ports/nrf/common-hal/countio/Counter.c b/ports/nrf/common-hal/countio/Counter.c new file mode 100644 index 0000000000..94fadae7c3 --- /dev/null +++ b/ports/nrf/common-hal/countio/Counter.c @@ -0,0 +1,66 @@ + +#include "common-hal/countio/Counter.h" +#include "nrfx_gpiote.h" + +// obj array to map pin number -> self since nrfx hide the mapping +static countio_counter_obj_t *_countio_objs[NUMBER_OF_PINS]; + +static void _intr_handler(nrfx_gpiote_pin_t pin, nrf_gpiote_polarity_t action) { + countio_counter_obj_t *self = _countio_objs[pin]; + if (!self) { + return; + } + self->count += 1; +} + +void common_hal_countio_counter_construct(countio_counter_obj_t* self, + const mcu_pin_obj_t* pin_a) { + + self->pin_a = pin_a->number; + _countio_objs[self->pin_a] = self; + + self->count = 0; + + nrfx_gpiote_in_config_t cfg = { + .sense = NRF_GPIOTE_POLARITY_HITOLO, + .pull = NRF_GPIO_PIN_PULLUP, + .is_watcher = false, + .hi_accuracy = true, + .skip_gpio_setup = false + }; + + nrfx_gpiote_in_init(self->pin_a, &cfg, _intr_handler); + nrfx_gpiote_in_event_enable(self->pin_a, true); + + claim_pin(pin_a); + +} + +bool common_hal_countio_counter_deinited(countio_counter_obj_t* self) { + return self->pin_a == NO_PIN; +} + +void common_hal_countio_counter_deinit(countio_counter_obj_t* self) { + if (common_hal_countio_counter_deinited(self)) { + return; + } + _countio_objs[self->pin_a] = NULL; + + nrfx_gpiote_in_event_disable(self->pin_a); + nrfx_gpiote_in_uninit(self->pin_a); + reset_pin_number(self->pin_a); + self->pin_a = NO_PIN; +} + +mp_int_t common_hal_countio_counter_get_count(countio_counter_obj_t* self) { + return self->count; +} + +void common_hal_countio_counter_set_count(countio_counter_obj_t* self, + mp_int_t new_count) { + self->count = new_count; +} + +void common_hal_countio_counter_reset(countio_counter_obj_t* self){ + self->count = 0; +} diff --git a/ports/nrf/common-hal/countio/Counter.h b/ports/nrf/common-hal/countio/Counter.h new file mode 100644 index 0000000000..cf40a63a02 --- /dev/null +++ b/ports/nrf/common-hal/countio/Counter.h @@ -0,0 +1,15 @@ + +#ifndef MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_COUNTIO_COUNTER_H +#define MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_COUNTIO_COUNTER_H + +#include "common-hal/microcontroller/Pin.h" + +#include "py/obj.h" + +typedef struct { + mp_obj_base_t base; + uint8_t pin_a; + mp_int_t count; +} countio_counter_obj_t; + +#endif // MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_COUNTIO_COUNT_H diff --git a/ports/nrf/common-hal/countio/__init__.c b/ports/nrf/common-hal/countio/__init__.c new file mode 100644 index 0000000000..b95b20d153 --- /dev/null +++ b/ports/nrf/common-hal/countio/__init__.c @@ -0,0 +1 @@ +//No countio module functions diff --git a/ports/nrf/mpconfigport.mk b/ports/nrf/mpconfigport.mk index 544be70be4..f349b3665f 100644 --- a/ports/nrf/mpconfigport.mk +++ b/ports/nrf/mpconfigport.mk @@ -43,7 +43,7 @@ CIRCUITPY_RGBMATRIX ?= 1 CIRCUITPY_ROTARYIO_SOFTENCODER = 1 CIRCUITPY_FRAMEBUFFERIO ?= 1 -CIRCUITPY_COUNTIO = 0 +CIRCUITPY_COUNTIO ?= 1 CIRCUITPY_WATCHDOG ?= 1 # nRF52840-specific From 223027fe9e04514c88eec7dedd1971ce20f25188 Mon Sep 17 00:00:00 2001 From: Neradoc Date: Thu, 15 Apr 2021 01:43:02 +0200 Subject: [PATCH 2/2] shrink simmel --- ports/nrf/boards/simmel/mpconfigboard.mk | 1 + 1 file changed, 1 insertion(+) diff --git a/ports/nrf/boards/simmel/mpconfigboard.mk b/ports/nrf/boards/simmel/mpconfigboard.mk index 005ec8af2f..b4a1417a53 100644 --- a/ports/nrf/boards/simmel/mpconfigboard.mk +++ b/ports/nrf/boards/simmel/mpconfigboard.mk @@ -15,6 +15,7 @@ CIRCUITPY_AUDIOMP3 = 0 CIRCUITPY_BITMAPTOOLS = 0 CIRCUITPY_BUSDEVICE = 0 CIRCUITPY_BUSIO = 1 +CIRCUITPY_COUNTIO = 0 CIRCUITPY_DISPLAYIO = 0 CIRCUITPY_ERRNO = 0 CIRCUITPY_FRAMEBUFFERIO = 0