Skip to content

Commit 346740e

Browse files
authored
Merge pull request #4609 from Neradoc/add_nrf_countio
NRF: implement countio
2 parents 8f73270 + 223027f commit 346740e

File tree

5 files changed

+84
-1
lines changed

5 files changed

+84
-1
lines changed

ports/nrf/boards/simmel/mpconfigboard.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ CIRCUITPY_AUDIOMP3 = 0
1515
CIRCUITPY_BITMAPTOOLS = 0
1616
CIRCUITPY_BUSDEVICE = 0
1717
CIRCUITPY_BUSIO = 1
18+
CIRCUITPY_COUNTIO = 0
1819
CIRCUITPY_DISPLAYIO = 0
1920
CIRCUITPY_ERRNO = 0
2021
CIRCUITPY_FRAMEBUFFERIO = 0
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
2+
#include "common-hal/countio/Counter.h"
3+
#include "nrfx_gpiote.h"
4+
5+
// obj array to map pin number -> self since nrfx hide the mapping
6+
static countio_counter_obj_t *_countio_objs[NUMBER_OF_PINS];
7+
8+
static void _intr_handler(nrfx_gpiote_pin_t pin, nrf_gpiote_polarity_t action) {
9+
countio_counter_obj_t *self = _countio_objs[pin];
10+
if (!self) {
11+
return;
12+
}
13+
self->count += 1;
14+
}
15+
16+
void common_hal_countio_counter_construct(countio_counter_obj_t* self,
17+
const mcu_pin_obj_t* pin_a) {
18+
19+
self->pin_a = pin_a->number;
20+
_countio_objs[self->pin_a] = self;
21+
22+
self->count = 0;
23+
24+
nrfx_gpiote_in_config_t cfg = {
25+
.sense = NRF_GPIOTE_POLARITY_HITOLO,
26+
.pull = NRF_GPIO_PIN_PULLUP,
27+
.is_watcher = false,
28+
.hi_accuracy = true,
29+
.skip_gpio_setup = false
30+
};
31+
32+
nrfx_gpiote_in_init(self->pin_a, &cfg, _intr_handler);
33+
nrfx_gpiote_in_event_enable(self->pin_a, true);
34+
35+
claim_pin(pin_a);
36+
37+
}
38+
39+
bool common_hal_countio_counter_deinited(countio_counter_obj_t* self) {
40+
return self->pin_a == NO_PIN;
41+
}
42+
43+
void common_hal_countio_counter_deinit(countio_counter_obj_t* self) {
44+
if (common_hal_countio_counter_deinited(self)) {
45+
return;
46+
}
47+
_countio_objs[self->pin_a] = NULL;
48+
49+
nrfx_gpiote_in_event_disable(self->pin_a);
50+
nrfx_gpiote_in_uninit(self->pin_a);
51+
reset_pin_number(self->pin_a);
52+
self->pin_a = NO_PIN;
53+
}
54+
55+
mp_int_t common_hal_countio_counter_get_count(countio_counter_obj_t* self) {
56+
return self->count;
57+
}
58+
59+
void common_hal_countio_counter_set_count(countio_counter_obj_t* self,
60+
mp_int_t new_count) {
61+
self->count = new_count;
62+
}
63+
64+
void common_hal_countio_counter_reset(countio_counter_obj_t* self){
65+
self->count = 0;
66+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
#ifndef MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_COUNTIO_COUNTER_H
3+
#define MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_COUNTIO_COUNTER_H
4+
5+
#include "common-hal/microcontroller/Pin.h"
6+
7+
#include "py/obj.h"
8+
9+
typedef struct {
10+
mp_obj_base_t base;
11+
uint8_t pin_a;
12+
mp_int_t count;
13+
} countio_counter_obj_t;
14+
15+
#endif // MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_COUNTIO_COUNT_H
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
//No countio module functions

ports/nrf/mpconfigport.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ CIRCUITPY_RGBMATRIX ?= 1
4343
CIRCUITPY_ROTARYIO_SOFTENCODER = 1
4444
CIRCUITPY_FRAMEBUFFERIO ?= 1
4545

46-
CIRCUITPY_COUNTIO = 0
46+
CIRCUITPY_COUNTIO ?= 1
4747
CIRCUITPY_WATCHDOG ?= 1
4848

4949
# nRF52840-specific

0 commit comments

Comments
 (0)