2021-04-13 08:56:21 -04:00
|
|
|
|
|
|
|
#include "common-hal/countio/Counter.h"
|
2021-11-10 12:26:54 -05:00
|
|
|
#include "shared-bindings/countio/Counter.h"
|
2021-08-27 17:24:31 -04:00
|
|
|
|
|
|
|
#include "py/runtime.h"
|
|
|
|
|
2021-04-13 08:56:21 -04:00
|
|
|
#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;
|
|
|
|
}
|
|
|
|
|
2021-04-30 11:47:37 -04:00
|
|
|
void common_hal_countio_counter_construct(countio_counter_obj_t *self,
|
2021-12-30 21:49:52 -05:00
|
|
|
const mcu_pin_obj_t *pin, countio_edge_t edge, digitalio_pull_t pull) {
|
2021-04-13 08:56:21 -04:00
|
|
|
|
2021-12-30 21:49:52 -05:00
|
|
|
self->pin = pin->number;
|
|
|
|
_countio_objs[self->pin] = self;
|
2021-04-13 08:56:21 -04:00
|
|
|
|
|
|
|
self->count = 0;
|
|
|
|
|
2021-12-30 21:49:52 -05:00
|
|
|
nrf_gpiote_polarity_t polarity = NRF_GPIOTE_POLARITY_TOGGLE;
|
|
|
|
switch (edge) {
|
|
|
|
case EDGE_RISE:
|
|
|
|
polarity = NRF_GPIOTE_POLARITY_LOTOHI;
|
|
|
|
break;
|
|
|
|
case EDGE_FALL:
|
|
|
|
polarity = NRF_GPIOTE_POLARITY_HITOLO;
|
|
|
|
break;
|
|
|
|
case EDGE_RISE_AND_FALL:
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
nrf_gpio_pin_pull_t hal_pull = NRF_GPIO_PIN_NOPULL;
|
|
|
|
switch (pull) {
|
|
|
|
case PULL_UP:
|
|
|
|
hal_pull = NRF_GPIO_PIN_PULLUP;
|
|
|
|
break;
|
|
|
|
case PULL_DOWN:
|
|
|
|
hal_pull = NRF_GPIO_PIN_PULLDOWN;
|
|
|
|
break;
|
|
|
|
case PULL_NONE:
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-04-13 08:56:21 -04:00
|
|
|
nrfx_gpiote_in_config_t cfg = {
|
2021-12-30 21:49:52 -05:00
|
|
|
.sense = polarity,
|
|
|
|
.pull = hal_pull,
|
2021-04-13 08:56:21 -04:00
|
|
|
.is_watcher = false,
|
|
|
|
.hi_accuracy = true,
|
2021-12-30 21:49:52 -05:00
|
|
|
.skip_gpio_setup = false,
|
2021-04-13 08:56:21 -04:00
|
|
|
};
|
|
|
|
|
2021-12-30 21:49:52 -05:00
|
|
|
nrfx_err_t err = nrfx_gpiote_in_init(self->pin, &cfg, _intr_handler);
|
2021-08-27 17:24:31 -04:00
|
|
|
if (err != NRFX_SUCCESS) {
|
|
|
|
mp_raise_RuntimeError(translate("All channels in use"));
|
|
|
|
}
|
2021-12-30 21:49:52 -05:00
|
|
|
nrfx_gpiote_in_event_enable(self->pin, true);
|
2021-04-13 08:56:21 -04:00
|
|
|
|
2021-12-30 21:49:52 -05:00
|
|
|
claim_pin(pin);
|
2021-04-13 08:56:21 -04:00
|
|
|
}
|
|
|
|
|
2021-04-30 11:47:37 -04:00
|
|
|
bool common_hal_countio_counter_deinited(countio_counter_obj_t *self) {
|
2021-12-30 21:49:52 -05:00
|
|
|
return self->pin == NO_PIN;
|
2021-04-13 08:56:21 -04:00
|
|
|
}
|
|
|
|
|
2021-04-30 11:47:37 -04:00
|
|
|
void common_hal_countio_counter_deinit(countio_counter_obj_t *self) {
|
2021-04-13 08:56:21 -04:00
|
|
|
if (common_hal_countio_counter_deinited(self)) {
|
|
|
|
return;
|
|
|
|
}
|
2021-12-30 21:49:52 -05:00
|
|
|
_countio_objs[self->pin] = NULL;
|
2021-04-13 08:56:21 -04:00
|
|
|
|
2021-12-30 21:49:52 -05:00
|
|
|
nrfx_gpiote_in_event_disable(self->pin);
|
|
|
|
nrfx_gpiote_in_uninit(self->pin);
|
|
|
|
reset_pin_number(self->pin);
|
|
|
|
self->pin = NO_PIN;
|
2021-04-13 08:56:21 -04:00
|
|
|
}
|
|
|
|
|
2021-04-30 11:47:37 -04:00
|
|
|
mp_int_t common_hal_countio_counter_get_count(countio_counter_obj_t *self) {
|
2021-04-13 08:56:21 -04:00
|
|
|
return self->count;
|
|
|
|
}
|
|
|
|
|
2021-04-30 11:47:37 -04:00
|
|
|
void common_hal_countio_counter_set_count(countio_counter_obj_t *self,
|
|
|
|
mp_int_t new_count) {
|
2021-04-13 08:56:21 -04:00
|
|
|
self->count = new_count;
|
|
|
|
}
|