nrf: protomatter port

This commit is contained in:
Jeff Epler 2020-04-01 11:57:52 -05:00
parent 09dc46a984
commit 1d8a073c05
9 changed files with 171 additions and 12 deletions

@ -1 +1 @@
Subproject commit c3a3e35731d641cb5a21ae7319634afc419f5afe
Subproject commit c411714cbdc05725e80398acb18c3c1fb6fa68a4

View File

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

View File

@ -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

View File

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

View File

@ -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++;

View File

@ -51,6 +51,9 @@ endif
# frequencyio not yet implemented
CIRCUITPY_FREQUENCYIO = 0
CIRCUITPY_PROTOMATTER = 1
CIRCUITPY_FRAMEBUFFERIO = 1
# nRF52840-specific
ifeq ($(MCU_CHIP),nrf52840)

View File

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

View File

@ -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);