Skip to content

Commit 186fa35

Browse files
committed
nrf: protomatter port
1 parent 2ca1f7c commit 186fa35

File tree

9 files changed

+171
-12
lines changed

9 files changed

+171
-12
lines changed

lib/protomatter

Submodule protomatter updated 1 file
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* This file is part of the Micro Python project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2020 Jeff Epler for Adafruit Industries
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include <stddef.h>
28+
29+
#include "common-hal/_protomatter/Protomatter.h"
30+
31+
#include "peripherals/nrf/timers.h"
32+
33+
extern void _PM_IRQ_HANDLER(void);
34+
35+
void *common_hal_protomatter_timer_allocate() {
36+
nrfx_timer_t *timer = nrf_peripherals_allocate_timer_or_throw();
37+
nrf_peripherals_timer_never_reset(timer);
38+
return timer->p_reg;
39+
}
40+
41+
42+
static void protomatter_event_handler(nrf_timer_event_t event_type, void *p_context) {
43+
_PM_IRQ_HANDLER();
44+
}
45+
46+
void common_hal_protomatter_timer_enable(void* ptr) {
47+
nrfx_timer_t *timer = nrf_peripherals_timer_from_reg(ptr);
48+
static const nrfx_timer_config_t timer_config = {
49+
.frequency = NRF_TIMER_FREQ_16MHz,
50+
.mode = NRF_TIMER_MODE_TIMER,
51+
.bit_width = NRF_TIMER_BIT_WIDTH_16,
52+
.interrupt_priority = NRFX_TIMER_DEFAULT_CONFIG_IRQ_PRIORITY,
53+
.p_context = NULL,
54+
};
55+
nrfx_timer_init(timer, &timer_config, &protomatter_event_handler);
56+
}
57+
58+
void common_hal_protomatter_timer_disable(void* ptr) {
59+
nrfx_timer_t *timer = nrf_peripherals_timer_from_reg(ptr);
60+
nrfx_timer_uninit(timer);
61+
}
62+
63+
void common_hal_protomatter_timer_free(void* ptr) {
64+
nrfx_timer_t *timer = nrf_peripherals_timer_from_reg(ptr);
65+
nrf_peripherals_free_timer(timer);
66+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* This file is part of the Micro Python project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2020 Jeff Epler for Adafruit Industries
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#ifndef MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_PROTOMATTER_PROTOMATTER_H
28+
#define MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_PROTOMATTER_PROTOMATTER_H
29+
30+
void *common_hal_protomatter_timer_allocate(void);
31+
void common_hal_protomatter_timer_enable(void*);
32+
void common_hal_protomatter_timer_disable(void*);
33+
void common_hal_protomatter_timer_free(void*);
34+
35+
#endif

ports/nrf/common-hal/_protomatter/__init__.c

Whitespace-only changes.

ports/nrf/common-hal/microcontroller/Pin.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,3 +196,15 @@ bool common_hal_mcu_pin_is_free(const mcu_pin_obj_t *pin) {
196196
return pin_number_is_free(pin->number);
197197

198198
}
199+
200+
uint8_t common_hal_mcu_pin_number(const mcu_pin_obj_t* pin) {
201+
return pin->number;
202+
}
203+
204+
void common_hal_mcu_pin_claim(const mcu_pin_obj_t* pin) {
205+
claim_pin(pin);
206+
}
207+
208+
void common_hal_mcu_pin_reset_number(uint8_t pin_no) {
209+
reset_pin_number(pin_no);
210+
}

ports/nrf/common-hal/pulseio/PulseOut.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,7 @@ void pulseout_reset() {
102102
void common_hal_pulseio_pulseout_construct(pulseio_pulseout_obj_t* self,
103103
const pulseio_pwmout_obj_t* carrier) {
104104
if (refcount == 0) {
105-
timer = nrf_peripherals_allocate_timer();
106-
if (timer == NULL) {
107-
mp_raise_RuntimeError(translate("All timers in use"));
108-
}
105+
timer = nrf_peripherals_allocate_timer_or_throw();
109106
}
110107
refcount++;
111108

ports/nrf/mpconfigport.mk

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ endif
5151
# frequencyio not yet implemented
5252
CIRCUITPY_FREQUENCYIO = 0
5353

54+
CIRCUITPY_PROTOMATTER = 1
55+
CIRCUITPY_FRAMEBUFFERIO = 1
56+
5457
# nRF52840-specific
5558

5659
ifeq ($(MCU_CHIP),nrf52840)

ports/nrf/peripherals/nrf/timers.c

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
*/
2626

2727
#include "common-hal/pulseio/PulseOut.h"
28+
#include "peripherals/nrf/timers.h"
2829

2930
#include <stdint.h>
3031

@@ -54,14 +55,47 @@ STATIC nrfx_timer_t nrfx_timers[] = {
5455
};
5556

5657
static bool nrfx_timer_allocated[ARRAY_SIZE(nrfx_timers)];
58+
static bool nrfx_timer_never_reset[ARRAY_SIZE(nrfx_timers)];
5759

5860
void timers_reset(void) {
5961
for (size_t i = 0; i < ARRAY_SIZE(nrfx_timers); i ++) {
62+
if (nrfx_timer_never_reset[i]) {
63+
continue;
64+
}
6065
nrfx_timer_uninit(&nrfx_timers[i]);
6166
nrfx_timer_allocated[i] = false;
6267
}
6368
}
6469

70+
void nrf_peripherals_timer_never_reset(nrfx_timer_t* timer) {
71+
int idx = nrf_peripherals_timer_idx_from_timer(timer);
72+
nrfx_timer_never_reset[idx] = true;
73+
}
74+
75+
void nrf_peripherals_timer_reset_ok(nrfx_timer_t* timer) {
76+
int idx = nrf_peripherals_timer_idx_from_timer(timer);
77+
nrfx_timer_never_reset[idx] = false;
78+
}
79+
80+
nrfx_timer_t* nrf_peripherals_timer_from_reg(NRF_TIMER_Type* ptr) {
81+
for (size_t i = 0; i < ARRAY_SIZE(nrfx_timers); i ++) {
82+
if (nrfx_timers[i].p_reg == ptr) {
83+
return &nrfx_timers[i];
84+
}
85+
}
86+
return NULL;
87+
}
88+
89+
size_t nrf_peripherals_timer_idx_from_timer(nrfx_timer_t* ptr) {
90+
for (size_t i = 0; i < ARRAY_SIZE(nrfx_timers); i ++) {
91+
if (&nrfx_timers[i] == ptr) {
92+
return i;
93+
}
94+
}
95+
return ~(size_t)0;
96+
}
97+
98+
6599
// Returns a free nrfx_timer instance, and marks it as allocated.
66100
// The caller should init as with the desired config.
67101
// Returns NULL if no timer is available.
@@ -75,14 +109,21 @@ nrfx_timer_t* nrf_peripherals_allocate_timer(void) {
75109
return NULL;
76110
}
77111

112+
nrfx_timer_t* nrf_peripherals_allocate_timer_or_throw(void) {
113+
nrfx_timer_t* result = nrf_peripherals_allocate_timer();
114+
if (!result) {
115+
mp_raise_RuntimeError(translate("All timers in use"));
116+
}
117+
return result;
118+
}
119+
78120
// Free a timer, which may or may not have been initialized.
79121
void nrf_peripherals_free_timer(nrfx_timer_t* timer) {
80-
for (size_t i = 0; i < ARRAY_SIZE(nrfx_timers); i ++) {
81-
if (timer == &nrfx_timers[i]) {
82-
nrfx_timer_allocated[i] = false;
83-
// Safe to call even if not initialized.
84-
nrfx_timer_uninit(timer);
85-
return;
86-
}
122+
size_t idx = nrf_peripherals_timer_idx_from_timer(timer);
123+
if (idx != ~(size_t)0) {
124+
nrfx_timer_allocated[idx] = false;
125+
nrfx_timer_never_reset[idx] = false;
126+
// Safe to call even if not initialized.
127+
nrfx_timer_uninit(timer);
87128
}
88129
}

ports/nrf/peripherals/nrf/timers.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,9 @@
2929

3030
void timers_reset(void);
3131
nrfx_timer_t* nrf_peripherals_allocate_timer(void);
32+
nrfx_timer_t* nrf_peripherals_allocate_timer_or_throw(void);
3233
void nrf_peripherals_free_timer(nrfx_timer_t* timer);
34+
void nrf_peripherals_timer_never_reset(nrfx_timer_t* timer);
35+
void nrf_peripherals_timer_reset_ok(nrfx_timer_t* timer);
36+
nrfx_timer_t* nrf_peripherals_timer_from_reg(NRF_TIMER_Type* ptr);
37+
size_t nrf_peripherals_timer_idx_from_timer(nrfx_timer_t* ptr);

0 commit comments

Comments
 (0)