Skip to content

Commit d166834

Browse files
authored
Merge pull request #5803 from dhalbert/countio-enhancements
countio: add selectable rise and fall detection, pulls
2 parents 8a94d9a + 9d2a32d commit d166834

File tree

18 files changed

+304
-135
lines changed

18 files changed

+304
-135
lines changed

locale/circuitpython.pot

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,10 @@ msgstr ""
144144
msgid "%q must be of type %q"
145145
msgstr ""
146146

147+
#: shared-bindings/digitalio/Pull.c
148+
msgid "%q must be of type %q or None"
149+
msgstr ""
150+
147151
#: ports/atmel-samd/common-hal/busio/UART.c
148152
msgid "%q must be power of 2"
149153
msgstr ""
@@ -1596,7 +1600,7 @@ msgstr ""
15961600
msgid "No DMA pacing timer found"
15971601
msgstr ""
15981602

1599-
#: shared-module/adafruit_bus_device/I2CDevice.c
1603+
#: shared-module/adafruit_bus_device/i2c_device/I2CDevice.c
16001604
#, c-format
16011605
msgid "No I2C device at address: %x"
16021606
msgstr ""
@@ -1896,7 +1900,7 @@ msgstr ""
18961900
msgid "Pin interrupt already in use"
18971901
msgstr ""
18981902

1899-
#: shared-bindings/adafruit_bus_device/SPIDevice.c
1903+
#: shared-bindings/adafruit_bus_device/spi_device/SPIDevice.c
19001904
#: shared-bindings/digitalio/DigitalInOut.c
19011905
msgid "Pin is input only"
19021906
msgstr ""
@@ -1978,6 +1982,10 @@ msgstr ""
19781982
msgid "RAISE mode is not implemented"
19791983
msgstr ""
19801984

1985+
#: ports/raspberrypi/common-hal/countio/Counter.c
1986+
msgid "RISE_AND_FALL not available on this chip"
1987+
msgstr ""
1988+
19811989
#: ports/stm/common-hal/os/__init__.c
19821990
msgid "RNG DeInit Error"
19831991
msgstr ""
@@ -2447,10 +2455,6 @@ msgstr ""
24472455
msgid "Unsupported operation"
24482456
msgstr ""
24492457

2450-
#: shared-bindings/digitalio/DigitalInOut.c
2451-
msgid "Unsupported pull value."
2452-
msgstr ""
2453-
24542458
#: ports/espressif/common-hal/dualbank/__init__.c
24552459
msgid "Update Failed"
24562460
msgstr ""

ports/atmel-samd/common-hal/countio/Counter.c

Lines changed: 45 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,56 +10,78 @@
1010
#include "supervisor/shared/translate.h"
1111

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

1818

1919
if (eic_get_enable()) {
20-
if (!eic_channel_free(pin_a->extint_channel)) {
20+
if (!eic_channel_free(pin->extint_channel)) {
2121
mp_raise_RuntimeError(translate("A hardware interrupt channel is already in use"));
2222
}
2323
} else {
2424
turn_on_external_interrupt_controller();
2525
}
2626

2727
// These default settings apply when the EIC isn't yet enabled.
28-
self->eic_channel_a = pin_a->extint_channel;
29-
30-
self->pin_a = pin_a->number;
31-
32-
gpio_set_pin_function(self->pin_a, GPIO_PIN_FUNCTION_A);
33-
gpio_set_pin_pull_mode(self->pin_a, GPIO_PULL_UP);
28+
self->eic_channel = pin->extint_channel;
29+
30+
self->pin = pin->number;
31+
32+
gpio_set_pin_function(self->pin, GPIO_PIN_FUNCTION_A);
33+
34+
enum gpio_pull_mode asf_pull = GPIO_PULL_OFF;
35+
switch (pull) {
36+
case PULL_UP:
37+
asf_pull = GPIO_PULL_UP;
38+
break;
39+
case PULL_DOWN:
40+
asf_pull = GPIO_PULL_DOWN;
41+
break;
42+
case PULL_NONE:
43+
default:
44+
break;
45+
}
46+
gpio_set_pin_pull_mode(self->pin, asf_pull);
3447

35-
set_eic_channel_data(self->eic_channel_a, (void *)self);
48+
set_eic_channel_data(self->eic_channel, (void *)self);
3649

3750
self->count = 0;
38-
39-
40-
claim_pin(pin_a);
41-
42-
43-
set_eic_handler(self->eic_channel_a, EIC_HANDLER_COUNTER);
44-
turn_on_eic_channel(self->eic_channel_a, EIC_CONFIG_SENSE0_FALL_Val);
45-
51+
claim_pin(pin);
52+
53+
set_eic_handler(self->eic_channel, EIC_HANDLER_COUNTER);
54+
55+
uint32_t sense_setting = EIC_CONFIG_SENSE0_BOTH_Val;
56+
switch (edge) {
57+
case EDGE_RISE:
58+
sense_setting = EIC_CONFIG_SENSE0_RISE_Val;
59+
break;
60+
case EDGE_FALL:
61+
sense_setting = EIC_CONFIG_SENSE0_FALL_Val;
62+
break;
63+
case EDGE_RISE_AND_FALL:
64+
default:
65+
break;
66+
}
67+
turn_on_eic_channel(self->eic_channel, sense_setting);
4668
}
4769

4870
bool common_hal_countio_counter_deinited(countio_counter_obj_t *self) {
49-
return self->pin_a == NO_PIN;
71+
return self->pin == NO_PIN;
5072
}
5173

5274
void common_hal_countio_counter_deinit(countio_counter_obj_t *self) {
5375
if (common_hal_countio_counter_deinited(self)) {
5476
return;
5577
}
5678

57-
set_eic_handler(self->eic_channel_a, EIC_HANDLER_NO_INTERRUPT);
58-
turn_off_eic_channel(self->eic_channel_a);
79+
set_eic_handler(self->eic_channel, EIC_HANDLER_NO_INTERRUPT);
80+
turn_off_eic_channel(self->eic_channel);
5981

6082

61-
reset_pin_number(self->pin_a);
62-
self->pin_a = NO_PIN;
83+
reset_pin_number(self->pin);
84+
self->pin = NO_PIN;
6385

6486
}
6587

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

75-
void common_hal_countio_counter_reset(countio_counter_obj_t *self) {
76-
self->count = 0;
77-
}
78-
7997
void counter_interrupt_handler(uint8_t channel) {
8098
countio_counter_obj_t *self = get_eic_channel_data(channel);
8199

ports/atmel-samd/common-hal/countio/Counter.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88

99
typedef struct {
1010
mp_obj_base_t base;
11-
uint8_t pin_a;
12-
uint8_t eic_channel_a : 4;
11+
uint8_t pin;
12+
uint8_t eic_channel : 4;
1313
mp_int_t count;
1414
} countio_counter_obj_t;
1515

ports/espressif/common-hal/countio/Counter.c

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
#include "supervisor/shared/translate.h"
3333

3434
void common_hal_countio_counter_construct(countio_counter_obj_t *self,
35-
const mcu_pin_obj_t *pin) {
35+
const mcu_pin_obj_t *pin, countio_edge_t edge, digitalio_pull_t pull) {
3636
claim_pin(pin);
3737

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

4950
// Initialize PCNT unit
@@ -53,6 +54,15 @@ void common_hal_countio_counter_construct(countio_counter_obj_t *self,
5354
}
5455

5556
self->pin = pin->number;
57+
58+
gpio_pullup_dis(pin->number);
59+
gpio_pulldown_dis(pin->number);
60+
if (pull == PULL_UP) {
61+
gpio_pullup_en(pin->number);
62+
} else if (pull == PULL_DOWN) {
63+
gpio_pulldown_en(pin->number);
64+
}
65+
5666
self->unit = (pcnt_unit_t)unit;
5767
}
5868

@@ -79,7 +89,3 @@ void common_hal_countio_counter_set_count(countio_counter_obj_t *self,
7989
self->count = new_count;
8090
pcnt_counter_clear(self->unit);
8191
}
82-
83-
void common_hal_countio_counter_reset(countio_counter_obj_t *self) {
84-
common_hal_countio_counter_set_count(self, 0);
85-
}

ports/nrf/common-hal/countio/Counter.c

Lines changed: 41 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,45 +18,70 @@ static void _intr_handler(nrfx_gpiote_pin_t pin, nrf_gpiote_polarity_t action) {
1818
}
1919

2020
void common_hal_countio_counter_construct(countio_counter_obj_t *self,
21-
const mcu_pin_obj_t *pin_a) {
21+
const mcu_pin_obj_t *pin, countio_edge_t edge, digitalio_pull_t pull) {
2222

23-
self->pin_a = pin_a->number;
24-
_countio_objs[self->pin_a] = self;
23+
self->pin = pin->number;
24+
_countio_objs[self->pin] = self;
2525

2626
self->count = 0;
2727

28+
nrf_gpiote_polarity_t polarity = NRF_GPIOTE_POLARITY_TOGGLE;
29+
switch (edge) {
30+
case EDGE_RISE:
31+
polarity = NRF_GPIOTE_POLARITY_LOTOHI;
32+
break;
33+
case EDGE_FALL:
34+
polarity = NRF_GPIOTE_POLARITY_HITOLO;
35+
break;
36+
case EDGE_RISE_AND_FALL:
37+
default:
38+
break;
39+
}
40+
41+
nrf_gpio_pin_pull_t hal_pull = NRF_GPIO_PIN_NOPULL;
42+
switch (pull) {
43+
case PULL_UP:
44+
hal_pull = NRF_GPIO_PIN_PULLUP;
45+
break;
46+
case PULL_DOWN:
47+
hal_pull = NRF_GPIO_PIN_PULLDOWN;
48+
break;
49+
case PULL_NONE:
50+
default:
51+
break;
52+
}
53+
2854
nrfx_gpiote_in_config_t cfg = {
29-
.sense = NRF_GPIOTE_POLARITY_HITOLO,
30-
.pull = NRF_GPIO_PIN_PULLUP,
55+
.sense = polarity,
56+
.pull = hal_pull,
3157
.is_watcher = false,
3258
.hi_accuracy = true,
33-
.skip_gpio_setup = false
59+
.skip_gpio_setup = false,
3460
};
3561

36-
37-
nrfx_err_t err = nrfx_gpiote_in_init(self->pin_a, &cfg, _intr_handler);
62+
nrfx_err_t err = nrfx_gpiote_in_init(self->pin, &cfg, _intr_handler);
3863
if (err != NRFX_SUCCESS) {
3964
mp_raise_RuntimeError(translate("All channels in use"));
4065
}
41-
nrfx_gpiote_in_event_enable(self->pin_a, true);
66+
nrfx_gpiote_in_event_enable(self->pin, true);
4267

43-
claim_pin(pin_a);
68+
claim_pin(pin);
4469
}
4570

4671
bool common_hal_countio_counter_deinited(countio_counter_obj_t *self) {
47-
return self->pin_a == NO_PIN;
72+
return self->pin == NO_PIN;
4873
}
4974

5075
void common_hal_countio_counter_deinit(countio_counter_obj_t *self) {
5176
if (common_hal_countio_counter_deinited(self)) {
5277
return;
5378
}
54-
_countio_objs[self->pin_a] = NULL;
79+
_countio_objs[self->pin] = NULL;
5580

56-
nrfx_gpiote_in_event_disable(self->pin_a);
57-
nrfx_gpiote_in_uninit(self->pin_a);
58-
reset_pin_number(self->pin_a);
59-
self->pin_a = NO_PIN;
81+
nrfx_gpiote_in_event_disable(self->pin);
82+
nrfx_gpiote_in_uninit(self->pin);
83+
reset_pin_number(self->pin);
84+
self->pin = NO_PIN;
6085
}
6186

6287
mp_int_t common_hal_countio_counter_get_count(countio_counter_obj_t *self) {
@@ -67,7 +92,3 @@ void common_hal_countio_counter_set_count(countio_counter_obj_t *self,
6792
mp_int_t new_count) {
6893
self->count = new_count;
6994
}
70-
71-
void common_hal_countio_counter_reset(countio_counter_obj_t *self) {
72-
self->count = 0;
73-
}

ports/nrf/common-hal/countio/Counter.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
typedef struct {
1010
mp_obj_base_t base;
11-
uint8_t pin_a;
11+
uint8_t pin;
1212
mp_int_t count;
1313
} countio_counter_obj_t;
1414

0 commit comments

Comments
 (0)