Skip to content

countio: add selectable rise and fall detection, pulls #5803

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions locale/circuitpython.pot
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@ msgstr ""
msgid "%q must be of type %q"
msgstr ""

#: shared-bindings/digitalio/Pull.c
msgid "%q must be of type %q or None"
msgstr ""

#: ports/atmel-samd/common-hal/busio/UART.c
msgid "%q must be power of 2"
msgstr ""
Expand Down Expand Up @@ -1596,7 +1600,7 @@ msgstr ""
msgid "No DMA pacing timer found"
msgstr ""

#: shared-module/adafruit_bus_device/I2CDevice.c
#: shared-module/adafruit_bus_device/i2c_device/I2CDevice.c
#, c-format
msgid "No I2C device at address: %x"
msgstr ""
Expand Down Expand Up @@ -1896,7 +1900,7 @@ msgstr ""
msgid "Pin interrupt already in use"
msgstr ""

#: shared-bindings/adafruit_bus_device/SPIDevice.c
#: shared-bindings/adafruit_bus_device/spi_device/SPIDevice.c
#: shared-bindings/digitalio/DigitalInOut.c
msgid "Pin is input only"
msgstr ""
Expand Down Expand Up @@ -1978,6 +1982,10 @@ msgstr ""
msgid "RAISE mode is not implemented"
msgstr ""

#: ports/raspberrypi/common-hal/countio/Counter.c
msgid "RISE_AND_FALL not available on this chip"
msgstr ""

#: ports/stm/common-hal/os/__init__.c
msgid "RNG DeInit Error"
msgstr ""
Expand Down Expand Up @@ -2447,10 +2455,6 @@ msgstr ""
msgid "Unsupported operation"
msgstr ""

#: shared-bindings/digitalio/DigitalInOut.c
msgid "Unsupported pull value."
msgstr ""

#: ports/espressif/common-hal/dualbank/__init__.c
msgid "Update Failed"
msgstr ""
Expand Down
72 changes: 45 additions & 27 deletions ports/atmel-samd/common-hal/countio/Counter.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,56 +10,78 @@
#include "supervisor/shared/translate.h"

void common_hal_countio_counter_construct(countio_counter_obj_t *self,
const mcu_pin_obj_t *pin_a) {
if (!pin_a->has_extint) {
const mcu_pin_obj_t *pin, countio_edge_t edge, digitalio_pull_t pull) {
if (!pin->has_extint) {
mp_raise_RuntimeError(translate("Pin must support hardware interrupts"));
}


if (eic_get_enable()) {
if (!eic_channel_free(pin_a->extint_channel)) {
if (!eic_channel_free(pin->extint_channel)) {
mp_raise_RuntimeError(translate("A hardware interrupt channel is already in use"));
}
} 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->pin_a = pin_a->number;

gpio_set_pin_function(self->pin_a, GPIO_PIN_FUNCTION_A);
gpio_set_pin_pull_mode(self->pin_a, GPIO_PULL_UP);
self->eic_channel = pin->extint_channel;

self->pin = pin->number;

gpio_set_pin_function(self->pin, GPIO_PIN_FUNCTION_A);

enum gpio_pull_mode asf_pull = GPIO_PULL_OFF;
switch (pull) {
case PULL_UP:
asf_pull = GPIO_PULL_UP;
break;
case PULL_DOWN:
asf_pull = GPIO_PULL_DOWN;
break;
case PULL_NONE:
default:
break;
}
gpio_set_pin_pull_mode(self->pin, asf_pull);

set_eic_channel_data(self->eic_channel_a, (void *)self);
set_eic_channel_data(self->eic_channel, (void *)self);

self->count = 0;


claim_pin(pin_a);


set_eic_handler(self->eic_channel_a, EIC_HANDLER_COUNTER);
turn_on_eic_channel(self->eic_channel_a, EIC_CONFIG_SENSE0_FALL_Val);

claim_pin(pin);

set_eic_handler(self->eic_channel, EIC_HANDLER_COUNTER);

uint32_t sense_setting = EIC_CONFIG_SENSE0_BOTH_Val;
switch (edge) {
case EDGE_RISE:
sense_setting = EIC_CONFIG_SENSE0_RISE_Val;
break;
case EDGE_FALL:
sense_setting = EIC_CONFIG_SENSE0_FALL_Val;
break;
case EDGE_RISE_AND_FALL:
default:
break;
}
turn_on_eic_channel(self->eic_channel, sense_setting);
}

bool common_hal_countio_counter_deinited(countio_counter_obj_t *self) {
return self->pin_a == NO_PIN;
return self->pin == NO_PIN;
}

void common_hal_countio_counter_deinit(countio_counter_obj_t *self) {
if (common_hal_countio_counter_deinited(self)) {
return;
}

set_eic_handler(self->eic_channel_a, EIC_HANDLER_NO_INTERRUPT);
turn_off_eic_channel(self->eic_channel_a);
set_eic_handler(self->eic_channel, EIC_HANDLER_NO_INTERRUPT);
turn_off_eic_channel(self->eic_channel);


reset_pin_number(self->pin_a);
self->pin_a = NO_PIN;
reset_pin_number(self->pin);
self->pin = NO_PIN;

}

Expand All @@ -72,10 +94,6 @@ void common_hal_countio_counter_set_count(countio_counter_obj_t *self,
self->count = new_count;
}

void common_hal_countio_counter_reset(countio_counter_obj_t *self) {
self->count = 0;
}

void counter_interrupt_handler(uint8_t channel) {
countio_counter_obj_t *self = get_eic_channel_data(channel);

Expand Down
4 changes: 2 additions & 2 deletions ports/atmel-samd/common-hal/countio/Counter.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

typedef struct {
mp_obj_base_t base;
uint8_t pin_a;
uint8_t eic_channel_a : 4;
uint8_t pin;
uint8_t eic_channel : 4;
mp_int_t count;
} countio_counter_obj_t;

Expand Down
22 changes: 14 additions & 8 deletions ports/espressif/common-hal/countio/Counter.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
#include "supervisor/shared/translate.h"

void common_hal_countio_counter_construct(countio_counter_obj_t *self,
const mcu_pin_obj_t *pin) {
const mcu_pin_obj_t *pin, countio_edge_t edge, digitalio_pull_t pull) {
claim_pin(pin);

// Prepare configuration for the PCNT unit
Expand All @@ -41,9 +41,10 @@ void common_hal_countio_counter_construct(countio_counter_obj_t *self,
.pulse_gpio_num = pin->number,
.ctrl_gpio_num = PCNT_PIN_NOT_USED,
.channel = PCNT_CHANNEL_0,
// What to do on the positive / negative edge of pulse input?
.pos_mode = PCNT_COUNT_INC, // Count up on the positive edge
.neg_mode = PCNT_COUNT_DIS, // Keep the counter value on the negative edge
// What to do on the rising / falling edge of pulse input?
// If EDGE_RISE_AND_FALL, both modeswill do PCNT_COUNT_INC.
.pos_mode = (edge == EDGE_FALL) ? PCNT_COUNT_DIS : PCNT_COUNT_INC, // Count up unless only fall
.neg_mode = (edge == EDGE_RISE) ? PCNT_COUNT_DIS : PCNT_COUNT_INC, // Count up unless only rise
};

// Initialize PCNT unit
Expand All @@ -53,6 +54,15 @@ void common_hal_countio_counter_construct(countio_counter_obj_t *self,
}

self->pin = pin->number;

gpio_pullup_dis(pin->number);
gpio_pulldown_dis(pin->number);
if (pull == PULL_UP) {
gpio_pullup_en(pin->number);
} else if (pull == PULL_DOWN) {
gpio_pulldown_en(pin->number);
}

self->unit = (pcnt_unit_t)unit;
}

Expand All @@ -79,7 +89,3 @@ void common_hal_countio_counter_set_count(countio_counter_obj_t *self,
self->count = new_count;
pcnt_counter_clear(self->unit);
}

void common_hal_countio_counter_reset(countio_counter_obj_t *self) {
common_hal_countio_counter_set_count(self, 0);
}
61 changes: 41 additions & 20 deletions ports/nrf/common-hal/countio/Counter.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,45 +18,70 @@ static void _intr_handler(nrfx_gpiote_pin_t pin, nrf_gpiote_polarity_t action) {
}

void common_hal_countio_counter_construct(countio_counter_obj_t *self,
const mcu_pin_obj_t *pin_a) {
const mcu_pin_obj_t *pin, countio_edge_t edge, digitalio_pull_t pull) {

self->pin_a = pin_a->number;
_countio_objs[self->pin_a] = self;
self->pin = pin->number;
_countio_objs[self->pin] = self;

self->count = 0;

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;
}

nrfx_gpiote_in_config_t cfg = {
.sense = NRF_GPIOTE_POLARITY_HITOLO,
.pull = NRF_GPIO_PIN_PULLUP,
.sense = polarity,
.pull = hal_pull,
.is_watcher = false,
.hi_accuracy = true,
.skip_gpio_setup = false
.skip_gpio_setup = false,
};


nrfx_err_t err = nrfx_gpiote_in_init(self->pin_a, &cfg, _intr_handler);
nrfx_err_t err = nrfx_gpiote_in_init(self->pin, &cfg, _intr_handler);
if (err != NRFX_SUCCESS) {
mp_raise_RuntimeError(translate("All channels in use"));
}
nrfx_gpiote_in_event_enable(self->pin_a, true);
nrfx_gpiote_in_event_enable(self->pin, true);

claim_pin(pin_a);
claim_pin(pin);
}

bool common_hal_countio_counter_deinited(countio_counter_obj_t *self) {
return self->pin_a == NO_PIN;
return self->pin == 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;
_countio_objs[self->pin] = 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;
nrfx_gpiote_in_event_disable(self->pin);
nrfx_gpiote_in_uninit(self->pin);
reset_pin_number(self->pin);
self->pin = NO_PIN;
}

mp_int_t common_hal_countio_counter_get_count(countio_counter_obj_t *self) {
Expand All @@ -67,7 +92,3 @@ 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;
}
2 changes: 1 addition & 1 deletion ports/nrf/common-hal/countio/Counter.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

typedef struct {
mp_obj_base_t base;
uint8_t pin_a;
uint8_t pin;
mp_int_t count;
} countio_counter_obj_t;

Expand Down
Loading