Browse Source

nrf: protomatter port

crypto-aes
Jeff Epler 2 years ago
parent
commit
1d8a073c05
  1. 2
      lib/protomatter
  2. 66
      ports/nrf/common-hal/_protomatter/Protomatter.c
  3. 35
      ports/nrf/common-hal/_protomatter/Protomatter.h
  4. 0
      ports/nrf/common-hal/_protomatter/__init__.c
  5. 12
      ports/nrf/common-hal/microcontroller/Pin.c
  6. 5
      ports/nrf/common-hal/pulseio/PulseOut.c
  7. 3
      ports/nrf/mpconfigport.mk
  8. 55
      ports/nrf/peripherals/nrf/timers.c
  9. 5
      ports/nrf/peripherals/nrf/timers.h

2
lib/protomatter

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

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

35
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

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

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

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

3
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)

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

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

Loading…
Cancel
Save