summaryrefslogtreecommitdiff
path: root/ports
diff options
context:
space:
mode:
authorJeff Epler <jepler@gmail.com>2020-04-01 11:57:52 -0500
committerJeff Epler <jepler@gmail.com>2020-04-14 18:24:58 -0500
commit1d8a073c05c36133e02d839ed187b9d7abf24d79 (patch)
treeeb19c30925e97a96790220da552ec8068ac7c252 /ports
parent09dc46a98480350b157636ffff8b89470f595f6e (diff)
nrf: protomatter port
Diffstat (limited to 'ports')
-rw-r--r--ports/nrf/common-hal/_protomatter/Protomatter.c66
-rw-r--r--ports/nrf/common-hal/_protomatter/Protomatter.h35
-rw-r--r--ports/nrf/common-hal/_protomatter/__init__.c0
-rw-r--r--ports/nrf/common-hal/microcontroller/Pin.c12
-rw-r--r--ports/nrf/common-hal/pulseio/PulseOut.c5
-rw-r--r--ports/nrf/mpconfigport.mk3
-rw-r--r--ports/nrf/peripherals/nrf/timers.c55
-rw-r--r--ports/nrf/peripherals/nrf/timers.h5
8 files changed, 170 insertions, 11 deletions
diff --git a/ports/nrf/common-hal/_protomatter/Protomatter.c b/ports/nrf/common-hal/_protomatter/Protomatter.c
new file mode 100644
index 000000000..b6e55bbd4
--- /dev/null
+++ b/ports/nrf/common-hal/_protomatter/Protomatter.c
@@ -0,0 +1,66 @@
+/*
+ * This file is part of the Micro Python project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2020 Jeff Epler for Adafruit Industries
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#include <stddef.h>
+
+#include "common-hal/_protomatter/Protomatter.h"
+
+#include "peripherals/nrf/timers.h"
+
+extern void _PM_IRQ_HANDLER(void);
+
+void *common_hal_protomatter_timer_allocate() {
+ nrfx_timer_t *timer = nrf_peripherals_allocate_timer_or_throw();
+ nrf_peripherals_timer_never_reset(timer);
+ return timer->p_reg;
+}
+
+
+static void protomatter_event_handler(nrf_timer_event_t event_type, void *p_context) {
+ _PM_IRQ_HANDLER();
+}
+
+void common_hal_protomatter_timer_enable(void* ptr) {
+ nrfx_timer_t *timer = nrf_peripherals_timer_from_reg(ptr);
+ static const nrfx_timer_config_t timer_config = {
+ .frequency = NRF_TIMER_FREQ_16MHz,
+ .mode = NRF_TIMER_MODE_TIMER,
+ .bit_width = NRF_TIMER_BIT_WIDTH_16,
+ .interrupt_priority = NRFX_TIMER_DEFAULT_CONFIG_IRQ_PRIORITY,
+ .p_context = NULL,
+ };
+ nrfx_timer_init(timer, &timer_config, &protomatter_event_handler);
+}
+
+void common_hal_protomatter_timer_disable(void* ptr) {
+ nrfx_timer_t *timer = nrf_peripherals_timer_from_reg(ptr);
+ nrfx_timer_uninit(timer);
+}
+
+void common_hal_protomatter_timer_free(void* ptr) {
+ nrfx_timer_t *timer = nrf_peripherals_timer_from_reg(ptr);
+ nrf_peripherals_free_timer(timer);
+}
diff --git a/ports/nrf/common-hal/_protomatter/Protomatter.h b/ports/nrf/common-hal/_protomatter/Protomatter.h
new file mode 100644
index 000000000..8185bed0e
--- /dev/null
+++ b/ports/nrf/common-hal/_protomatter/Protomatter.h
@@ -0,0 +1,35 @@
+/*
+ * This file is part of the Micro Python project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2020 Jeff Epler for Adafruit Industries
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#ifndef MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_PROTOMATTER_PROTOMATTER_H
+#define MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_PROTOMATTER_PROTOMATTER_H
+
+void *common_hal_protomatter_timer_allocate(void);
+void common_hal_protomatter_timer_enable(void*);
+void common_hal_protomatter_timer_disable(void*);
+void common_hal_protomatter_timer_free(void*);
+
+#endif
diff --git a/ports/nrf/common-hal/_protomatter/__init__.c b/ports/nrf/common-hal/_protomatter/__init__.c
new file mode 100644
index 000000000..e69de29bb
--- /dev/null
+++ b/ports/nrf/common-hal/_protomatter/__init__.c
diff --git a/ports/nrf/common-hal/microcontroller/Pin.c b/ports/nrf/common-hal/microcontroller/Pin.c
index b7931a2e1..3132c7631 100644
--- a/ports/nrf/common-hal/microcontroller/Pin.c
+++ b/ports/nrf/common-hal/microcontroller/Pin.c
@@ -196,3 +196,15 @@ bool common_hal_mcu_pin_is_free(const mcu_pin_obj_t *pin) {
return pin_number_is_free(pin->number);
}
+
+uint8_t common_hal_mcu_pin_number(const mcu_pin_obj_t* pin) {
+ return pin->number;
+}
+
+void common_hal_mcu_pin_claim(const mcu_pin_obj_t* pin) {
+ claim_pin(pin);
+}
+
+void common_hal_mcu_pin_reset_number(uint8_t pin_no) {
+ reset_pin_number(pin_no);
+}
diff --git a/ports/nrf/common-hal/pulseio/PulseOut.c b/ports/nrf/common-hal/pulseio/PulseOut.c
index d0433247a..270cb45d6 100644
--- a/ports/nrf/common-hal/pulseio/PulseOut.c
+++ b/ports/nrf/common-hal/pulseio/PulseOut.c
@@ -102,10 +102,7 @@ void pulseout_reset() {
void common_hal_pulseio_pulseout_construct(pulseio_pulseout_obj_t* self,
const pulseio_pwmout_obj_t* carrier) {
if (refcount == 0) {
- timer = nrf_peripherals_allocate_timer();
- if (timer == NULL) {
- mp_raise_RuntimeError(translate("All timers in use"));
- }
+ timer = nrf_peripherals_allocate_timer_or_throw();
}
refcount++;
diff --git a/ports/nrf/mpconfigport.mk b/ports/nrf/mpconfigport.mk
index fcc59c484..5ca1f0f35 100644
--- a/ports/nrf/mpconfigport.mk
+++ b/ports/nrf/mpconfigport.mk
@@ -51,6 +51,9 @@ endif
# frequencyio not yet implemented
CIRCUITPY_FREQUENCYIO = 0
+CIRCUITPY_PROTOMATTER = 1
+CIRCUITPY_FRAMEBUFFERIO = 1
+
# nRF52840-specific
ifeq ($(MCU_CHIP),nrf52840)
diff --git a/ports/nrf/peripherals/nrf/timers.c b/ports/nrf/peripherals/nrf/timers.c
index 88f3dd468..fd814968d 100644
--- a/ports/nrf/peripherals/nrf/timers.c
+++ b/ports/nrf/peripherals/nrf/timers.c
@@ -25,6 +25,7 @@
*/
#include "common-hal/pulseio/PulseOut.h"
+#include "peripherals/nrf/timers.h"
#include <stdint.h>
@@ -54,14 +55,47 @@ STATIC nrfx_timer_t nrfx_timers[] = {
};
static bool nrfx_timer_allocated[ARRAY_SIZE(nrfx_timers)];
+static bool nrfx_timer_never_reset[ARRAY_SIZE(nrfx_timers)];
void timers_reset(void) {
for (size_t i = 0; i < ARRAY_SIZE(nrfx_timers); i ++) {
+ if (nrfx_timer_never_reset[i]) {
+ continue;
+ }
nrfx_timer_uninit(&nrfx_timers[i]);
nrfx_timer_allocated[i] = false;
}
}
+void nrf_peripherals_timer_never_reset(nrfx_timer_t* timer) {
+ int idx = nrf_peripherals_timer_idx_from_timer(timer);
+ nrfx_timer_never_reset[idx] = true;
+}
+
+void nrf_peripherals_timer_reset_ok(nrfx_timer_t* timer) {
+ int idx = nrf_peripherals_timer_idx_from_timer(timer);
+ nrfx_timer_never_reset[idx] = false;
+}
+
+nrfx_timer_t* nrf_peripherals_timer_from_reg(NRF_TIMER_Type* ptr) {
+ for (size_t i = 0; i < ARRAY_SIZE(nrfx_timers); i ++) {
+ if (nrfx_timers[i].p_reg == ptr) {
+ return &nrfx_timers[i];
+ }
+ }
+ return NULL;
+}
+
+size_t nrf_peripherals_timer_idx_from_timer(nrfx_timer_t* ptr) {
+ for (size_t i = 0; i < ARRAY_SIZE(nrfx_timers); i ++) {
+ if (&nrfx_timers[i] == ptr) {
+ return i;
+ }
+ }
+ return ~(size_t)0;
+}
+
+
// Returns a free nrfx_timer instance, and marks it as allocated.
// The caller should init as with the desired config.
// Returns NULL if no timer is available.
@@ -75,14 +109,21 @@ nrfx_timer_t* nrf_peripherals_allocate_timer(void) {
return NULL;
}
+nrfx_timer_t* nrf_peripherals_allocate_timer_or_throw(void) {
+ nrfx_timer_t* result = nrf_peripherals_allocate_timer();
+ if (!result) {
+ mp_raise_RuntimeError(translate("All timers in use"));
+ }
+ return result;
+}
+
// Free a timer, which may or may not have been initialized.
void nrf_peripherals_free_timer(nrfx_timer_t* timer) {
- for (size_t i = 0; i < ARRAY_SIZE(nrfx_timers); i ++) {
- if (timer == &nrfx_timers[i]) {
- nrfx_timer_allocated[i] = false;
- // Safe to call even if not initialized.
- nrfx_timer_uninit(timer);
- return;
- }
+ size_t idx = nrf_peripherals_timer_idx_from_timer(timer);
+ if (idx != ~(size_t)0) {
+ nrfx_timer_allocated[idx] = false;
+ nrfx_timer_never_reset[idx] = false;
+ // Safe to call even if not initialized.
+ nrfx_timer_uninit(timer);
}
}
diff --git a/ports/nrf/peripherals/nrf/timers.h b/ports/nrf/peripherals/nrf/timers.h
index 7d3815579..cf96c6273 100644
--- a/ports/nrf/peripherals/nrf/timers.h
+++ b/ports/nrf/peripherals/nrf/timers.h
@@ -29,4 +29,9 @@
void timers_reset(void);
nrfx_timer_t* nrf_peripherals_allocate_timer(void);
+nrfx_timer_t* nrf_peripherals_allocate_timer_or_throw(void);
void nrf_peripherals_free_timer(nrfx_timer_t* timer);
+void nrf_peripherals_timer_never_reset(nrfx_timer_t* timer);
+void nrf_peripherals_timer_reset_ok(nrfx_timer_t* timer);
+nrfx_timer_t* nrf_peripherals_timer_from_reg(NRF_TIMER_Type* ptr);
+size_t nrf_peripherals_timer_idx_from_timer(nrfx_timer_t* ptr);