2017-12-21 07:49:14 -05:00
|
|
|
/*
|
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2017 Scott Shawcroft 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 <stdint.h>
|
|
|
|
#include "supervisor/port.h"
|
2020-12-02 21:05:29 -05:00
|
|
|
#include "supervisor/board.h"
|
2021-02-17 06:53:18 -05:00
|
|
|
#ifdef MY_DEBUGUART
|
|
|
|
#include "supervisor/serial.h" // dbg_printf()
|
|
|
|
extern void _debug_uart_init(void);
|
|
|
|
#endif
|
2017-12-21 07:49:14 -05:00
|
|
|
|
2020-03-05 20:14:54 -05:00
|
|
|
#include "nrfx/hal/nrf_clock.h"
|
2018-12-06 17:24:20 -05:00
|
|
|
#include "nrfx/hal/nrf_power.h"
|
|
|
|
#include "nrfx/drivers/include/nrfx_power.h"
|
2020-03-05 20:14:54 -05:00
|
|
|
#include "nrfx/drivers/include/nrfx_rtc.h"
|
2018-12-06 17:24:20 -05:00
|
|
|
|
2018-09-09 15:01:28 -04:00
|
|
|
#include "nrf/cache.h"
|
|
|
|
#include "nrf/clocks.h"
|
|
|
|
#include "nrf/power.h"
|
2018-10-16 11:05:02 -04:00
|
|
|
#include "nrf/timers.h"
|
2018-09-09 15:01:28 -04:00
|
|
|
|
2018-02-03 11:08:51 -05:00
|
|
|
#include "shared-module/gamepad/__init__.h"
|
2017-12-21 07:49:14 -05:00
|
|
|
#include "common-hal/microcontroller/Pin.h"
|
2019-08-29 18:44:27 -04:00
|
|
|
#include "common-hal/_bleio/__init__.h"
|
2019-10-12 15:42:15 -04:00
|
|
|
#include "common-hal/analogio/AnalogIn.h"
|
2018-09-25 15:12:10 -04:00
|
|
|
#include "common-hal/busio/I2C.h"
|
|
|
|
#include "common-hal/busio/SPI.h"
|
2019-02-12 22:34:05 -05:00
|
|
|
#include "common-hal/busio/UART.h"
|
2018-10-16 11:05:02 -04:00
|
|
|
#include "common-hal/pulseio/PulseOut.h"
|
2018-12-18 10:05:17 -05:00
|
|
|
#include "common-hal/pulseio/PulseIn.h"
|
2020-08-18 16:08:33 -04:00
|
|
|
#include "common-hal/pwmio/PWMOut.h"
|
2019-02-02 02:38:10 -05:00
|
|
|
#include "common-hal/rtc/RTC.h"
|
2020-01-08 15:15:27 -05:00
|
|
|
#include "common-hal/neopixel_write/__init__.h"
|
2020-05-22 06:51:54 -04:00
|
|
|
#include "common-hal/watchdog/WatchDogTimer.h"
|
2017-12-21 07:49:14 -05:00
|
|
|
|
2020-05-08 00:38:23 -04:00
|
|
|
#include "shared-bindings/microcontroller/__init__.h"
|
2019-02-02 02:38:10 -05:00
|
|
|
#include "shared-bindings/rtc/__init__.h"
|
|
|
|
|
2020-05-08 00:38:23 -04:00
|
|
|
#include "lib/tinyusb/src/device/usbd.h"
|
|
|
|
|
2019-09-08 14:08:15 -04:00
|
|
|
#ifdef CIRCUITPY_AUDIOBUSIO
|
|
|
|
#include "common-hal/audiobusio/I2SOut.h"
|
|
|
|
#endif
|
|
|
|
|
2019-07-15 20:56:21 -04:00
|
|
|
#ifdef CIRCUITPY_AUDIOPWMIO
|
2019-07-29 18:39:00 -04:00
|
|
|
#include "common-hal/audiopwmio/PWMAudioOut.h"
|
2019-07-15 20:56:21 -04:00
|
|
|
#endif
|
|
|
|
|
2020-08-07 04:23:42 -04:00
|
|
|
#if defined(MICROPY_QSPI_CS)
|
2020-08-05 21:56:05 -04:00
|
|
|
extern void qspi_disable(void);
|
|
|
|
#endif
|
|
|
|
|
2018-12-06 17:24:20 -05:00
|
|
|
static void power_warning_handler(void) {
|
|
|
|
reset_into_safe_mode(BROWNOUT);
|
|
|
|
}
|
2018-09-09 15:01:28 -04:00
|
|
|
|
2021-02-20 19:55:10 -05:00
|
|
|
#ifdef MY_DEBUGUART
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include "nrfx.h"
|
|
|
|
#include "nrf_uart.h"
|
|
|
|
#include "nrfx_uart.h"
|
|
|
|
const nrfx_uarte_t _dbg_uart_inst = NRFX_UARTE_INSTANCE(1);
|
|
|
|
static int _dbg_uart_initialized = 0;
|
|
|
|
#define DBG_PBUF_LEN 80
|
|
|
|
static char _dbg_pbuf[DBG_PBUF_LEN+1];
|
|
|
|
|
|
|
|
void _debug_uart_init(void) {
|
|
|
|
//if (_dbg_uart_initialized) return;
|
|
|
|
nrfx_uarte_config_t config = {
|
|
|
|
.pseltxd = 26,
|
|
|
|
.pselrxd = 15,
|
|
|
|
.pselcts = NRF_UARTE_PSEL_DISCONNECTED,
|
|
|
|
.pselrts = NRF_UARTE_PSEL_DISCONNECTED,
|
|
|
|
.p_context = NULL,
|
|
|
|
.baudrate = NRF_UART_BAUDRATE_115200,
|
|
|
|
.interrupt_priority = 7,
|
|
|
|
.hal_cfg = {
|
|
|
|
.hwfc = NRF_UARTE_HWFC_DISABLED,
|
|
|
|
.parity = NRF_UARTE_PARITY_EXCLUDED
|
|
|
|
}
|
|
|
|
};
|
|
|
|
nrfx_uarte_init(&_dbg_uart_inst, &config, NULL);
|
|
|
|
// drive config
|
|
|
|
nrf_gpio_cfg(config.pseltxd,
|
|
|
|
NRF_GPIO_PIN_DIR_OUTPUT,
|
|
|
|
NRF_GPIO_PIN_INPUT_DISCONNECT,
|
|
|
|
NRF_GPIO_PIN_PULLUP, // orig=NOPULL
|
|
|
|
NRF_GPIO_PIN_H0H1, // orig=S0S1
|
|
|
|
NRF_GPIO_PIN_NOSENSE);
|
|
|
|
_dbg_uart_initialized = 1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
void _debug_print_substr(const char* text, uint32_t length) {
|
|
|
|
char* data = (char*)text;
|
|
|
|
int siz;
|
|
|
|
while(length != 0) {
|
|
|
|
if (length <= DBG_PBUF_LEN) {
|
|
|
|
siz = length;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
siz = DBG_PBUF_LEN;
|
|
|
|
}
|
|
|
|
memcpy(_dbg_pbuf, data, siz);
|
|
|
|
_dbg_pbuf[siz] = 0;
|
|
|
|
nrfx_uarte_tx(&_dbg_uart_inst, (uint8_t const*)_dbg_pbuf, siz);
|
|
|
|
data += siz;
|
|
|
|
length -= siz;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void _debug_uart_deinit(void) {
|
|
|
|
nrfx_uarte_uninit(&_dbg_uart_inst);
|
|
|
|
}
|
|
|
|
|
|
|
|
int dbg_printf(const char *fmt, ...) {
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
|
|
|
int ret = vprintf(fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
extern void _debug_led_init(void);
|
|
|
|
extern void _debug_led_set(int v);
|
|
|
|
#else /*!MY_DEBUGUART*/
|
|
|
|
int dbg_printf(const char *fmt, ...) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-03-05 20:14:54 -05:00
|
|
|
const nrfx_rtc_t rtc_instance = NRFX_RTC_INSTANCE(2);
|
|
|
|
|
|
|
|
const nrfx_rtc_config_t rtc_config = {
|
2020-03-06 21:15:16 -05:00
|
|
|
.prescaler = RTC_FREQ_TO_PRESCALER(0x8000),
|
2020-03-05 20:14:54 -05:00
|
|
|
.reliable = 0,
|
|
|
|
.tick_latency = 0,
|
|
|
|
.interrupt_priority = 6
|
|
|
|
};
|
|
|
|
|
2020-05-22 07:36:05 -04:00
|
|
|
#define OVERFLOW_CHECK_PREFIX 0x2cad564f
|
|
|
|
#define OVERFLOW_CHECK_SUFFIX 0x11343ef7
|
|
|
|
static volatile struct {
|
|
|
|
uint32_t prefix;
|
|
|
|
uint64_t overflowed_ticks;
|
|
|
|
uint32_t suffix;
|
|
|
|
} overflow_tracker __attribute__((section(".uninitialized")));
|
2020-03-05 20:14:54 -05:00
|
|
|
|
2021-02-17 06:53:18 -05:00
|
|
|
uint32_t reset_reason_saved = 0;
|
|
|
|
volatile int rtc_woke_up_counter = 0;
|
|
|
|
|
2020-03-05 20:14:54 -05:00
|
|
|
void rtc_handler(nrfx_rtc_int_type_t int_type) {
|
|
|
|
if (int_type == NRFX_RTC_INT_OVERFLOW) {
|
2020-03-13 02:05:12 -04:00
|
|
|
// Our RTC is 24 bits and we're clocking it at 32.768khz which is 32 (2 ** 5) subticks per
|
|
|
|
// tick.
|
2020-05-22 07:36:05 -04:00
|
|
|
overflow_tracker.overflowed_ticks += (1L<< (24 - 5));
|
2020-03-06 21:15:16 -05:00
|
|
|
} else if (int_type == NRFX_RTC_INT_TICK && nrfx_rtc_counter_get(&rtc_instance) % 32 == 0) {
|
|
|
|
// Do things common to all ports when the tick occurs
|
2020-03-05 20:14:54 -05:00
|
|
|
supervisor_tick();
|
2020-03-06 21:15:16 -05:00
|
|
|
} else if (int_type == NRFX_RTC_INT_COMPARE0) {
|
|
|
|
nrfx_rtc_cc_set(&rtc_instance, 0, 0, false);
|
2021-02-17 06:53:18 -05:00
|
|
|
} else if (int_type == NRFX_RTC_INT_COMPARE1) {
|
|
|
|
// used in light sleep
|
|
|
|
++rtc_woke_up_counter;
|
|
|
|
nrfx_rtc_cc_set(&rtc_instance, 1, 0, false);
|
2020-03-05 20:14:54 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-20 10:47:07 -05:00
|
|
|
#ifdef MY_DEBUGUART
|
|
|
|
void dbg_dumpRTC(void) {
|
2021-02-17 06:53:18 -05:00
|
|
|
dbg_printf("\r\nRTC2\r\n");
|
|
|
|
NRF_RTC_Type *r = rtc_instance.p_reg;
|
|
|
|
dbg_printf("PRESCALER=%08X, ", (int)r->PRESCALER);
|
|
|
|
dbg_printf("COUNTER=%08X ", (int)r->COUNTER);
|
|
|
|
dbg_printf("INTENSET=%08X ", (int)r->INTENSET);
|
|
|
|
dbg_printf("EVTENSET=%08X\r\n", (int)r->EVTENSET);
|
|
|
|
dbg_printf("EVENTS_COMPARE[0..3]=%X,%X,%X,%X ", (int)r->EVENTS_COMPARE[0], (int)r->EVENTS_COMPARE[1], (int)r->EVENTS_COMPARE[2], (int)r->EVENTS_COMPARE[3]);
|
|
|
|
dbg_printf("CC[0..3]=%08X,%08X,%08X,%08X\r\n", (int)r->CC[0], (int)r->CC[1], (int)r->CC[2], (int)r->CC[3]);
|
|
|
|
dbg_printf("woke_up=%d\r\n", rtc_woke_up_counter);
|
|
|
|
}
|
2021-02-20 10:47:07 -05:00
|
|
|
#endif
|
2021-02-17 06:53:18 -05:00
|
|
|
|
2020-03-05 20:14:54 -05:00
|
|
|
void tick_init(void) {
|
|
|
|
if (!nrf_clock_lf_is_running(NRF_CLOCK)) {
|
|
|
|
nrf_clock_task_trigger(NRF_CLOCK, NRF_CLOCK_TASK_LFCLKSTART);
|
|
|
|
}
|
|
|
|
nrfx_rtc_counter_clear(&rtc_instance);
|
|
|
|
nrfx_rtc_init(&rtc_instance, &rtc_config, rtc_handler);
|
|
|
|
nrfx_rtc_enable(&rtc_instance);
|
|
|
|
nrfx_rtc_overflow_enable(&rtc_instance, true);
|
2020-05-22 07:36:05 -04:00
|
|
|
|
|
|
|
// If the check prefix and suffix aren't correct, then the structure
|
|
|
|
// in memory isn't correct and the clock will be wildly wrong. Initialize
|
|
|
|
// the prefix and suffix so that we know the value is correct, and reset
|
|
|
|
// the time to 0.
|
|
|
|
if (overflow_tracker.prefix != OVERFLOW_CHECK_PREFIX ||
|
|
|
|
overflow_tracker.suffix != OVERFLOW_CHECK_SUFFIX) {
|
|
|
|
overflow_tracker.prefix = OVERFLOW_CHECK_PREFIX;
|
|
|
|
overflow_tracker.suffix = OVERFLOW_CHECK_SUFFIX;
|
|
|
|
overflow_tracker.overflowed_ticks = 0;
|
|
|
|
}
|
2020-03-05 20:14:54 -05:00
|
|
|
}
|
|
|
|
|
2018-12-06 17:24:20 -05:00
|
|
|
safe_mode_t port_init(void) {
|
2018-09-09 15:01:28 -04:00
|
|
|
nrf_peripherals_clocks_init();
|
|
|
|
|
|
|
|
// If GPIO voltage is set wrong in UICR, this will fix it, and
|
|
|
|
// will also do a reset to make the change take effect.
|
|
|
|
nrf_peripherals_power_init();
|
|
|
|
|
2018-12-06 17:24:20 -05:00
|
|
|
nrfx_power_pofwarn_config_t power_failure_config;
|
|
|
|
power_failure_config.handler = power_warning_handler;
|
|
|
|
power_failure_config.thr = NRF_POWER_POFTHR_V27;
|
2018-12-06 19:38:14 -05:00
|
|
|
#if NRF_POWER_HAS_VDDH
|
2018-12-06 17:24:20 -05:00
|
|
|
power_failure_config.thrvddh = NRF_POWER_POFTHRVDDH_V27;
|
2018-12-06 19:38:14 -05:00
|
|
|
#endif
|
2018-12-06 17:24:20 -05:00
|
|
|
nrfx_power_pof_init(&power_failure_config);
|
|
|
|
nrfx_power_pof_enable(&power_failure_config);
|
|
|
|
|
2018-09-09 15:01:28 -04:00
|
|
|
nrf_peripherals_enable_cache();
|
2017-12-21 07:49:14 -05:00
|
|
|
|
|
|
|
// Configure millisecond timer initialization.
|
|
|
|
tick_init();
|
2019-04-01 22:23:55 -04:00
|
|
|
|
2020-05-17 23:50:04 -04:00
|
|
|
#if CIRCUITPY_RTC
|
|
|
|
common_hal_rtc_init();
|
|
|
|
#endif
|
|
|
|
|
2019-10-14 23:38:41 -04:00
|
|
|
#if CIRCUITPY_ANALOGIO
|
|
|
|
analogin_init();
|
|
|
|
#endif
|
|
|
|
|
2021-02-17 06:53:18 -05:00
|
|
|
reset_reason_saved = NRF_POWER->RESETREAS;
|
2021-02-20 10:47:07 -05:00
|
|
|
// clear all RESET reason bits
|
|
|
|
NRF_POWER->RESETREAS = reset_reason_saved;
|
2021-02-17 06:53:18 -05:00
|
|
|
|
2020-05-22 00:56:49 -04:00
|
|
|
// If the board was reset by the WatchDogTimer, we may
|
|
|
|
// need to boot into safe mode. Reset the RESETREAS bit
|
|
|
|
// for the WatchDogTimer so we don't encounter this the
|
|
|
|
// next time we reboot.
|
2021-02-20 10:47:07 -05:00
|
|
|
if (reset_reason_saved & POWER_RESETREAS_DOG_Msk) {
|
2020-05-22 00:56:49 -04:00
|
|
|
NRF_POWER->RESETREAS = POWER_RESETREAS_DOG_Msk;
|
|
|
|
uint32_t usb_reg = NRF_POWER->USBREGSTATUS;
|
|
|
|
|
|
|
|
// If USB is connected, then the user might be editing `code.py`,
|
|
|
|
// in which case we should reboot into Safe Mode.
|
|
|
|
if (usb_reg & POWER_USBREGSTATUS_VBUSDETECT_Msk) {
|
|
|
|
return WATCHDOG_RESET;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-21 07:49:14 -05:00
|
|
|
return NO_SAFE_MODE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void reset_port(void) {
|
2018-09-25 15:12:10 -04:00
|
|
|
#ifdef CIRCUITPY_GAMEPAD_TICKS
|
|
|
|
gamepad_reset();
|
|
|
|
#endif
|
2018-02-03 11:08:51 -05:00
|
|
|
|
2020-04-19 01:53:51 -04:00
|
|
|
#if CIRCUITPY_BUSIO
|
2018-09-25 15:12:10 -04:00
|
|
|
i2c_reset();
|
|
|
|
spi_reset();
|
2019-02-12 22:34:05 -05:00
|
|
|
uart_reset();
|
2020-04-19 01:53:51 -04:00
|
|
|
#endif
|
2020-04-16 22:36:26 -04:00
|
|
|
|
|
|
|
#if CIRCUITPY_NEOPIXEL_WRITE
|
2020-01-08 15:15:27 -05:00
|
|
|
neopixel_write_reset();
|
2020-04-16 22:36:26 -04:00
|
|
|
#endif
|
2019-06-06 17:49:32 -04:00
|
|
|
|
2019-10-12 15:42:15 -04:00
|
|
|
#if CIRCUITPY_AUDIOBUSIO
|
2019-09-08 14:08:15 -04:00
|
|
|
i2s_reset();
|
|
|
|
#endif
|
|
|
|
|
2019-10-12 15:42:15 -04:00
|
|
|
#if CIRCUITPY_AUDIOPWMIO
|
2019-07-15 20:56:21 -04:00
|
|
|
audiopwmout_reset();
|
|
|
|
#endif
|
|
|
|
|
2019-09-08 14:08:15 -04:00
|
|
|
|
2019-06-06 17:49:32 -04:00
|
|
|
#if CIRCUITPY_PULSEIO
|
2018-10-16 11:05:02 -04:00
|
|
|
pulseout_reset();
|
2018-12-18 10:05:17 -05:00
|
|
|
pulsein_reset();
|
2019-06-06 17:49:32 -04:00
|
|
|
#endif
|
|
|
|
|
2020-08-18 16:08:33 -04:00
|
|
|
#if CIRCUITPY_PWMIO
|
|
|
|
pwmout_reset();
|
|
|
|
#endif
|
|
|
|
|
2020-05-20 04:44:18 -04:00
|
|
|
#if CIRCUITPY_RTC
|
|
|
|
rtc_reset();
|
|
|
|
#endif
|
|
|
|
|
2018-10-16 11:05:02 -04:00
|
|
|
timers_reset();
|
2019-04-01 22:23:55 -04:00
|
|
|
|
2019-06-06 17:49:32 -04:00
|
|
|
#if CIRCUITPY_BLEIO
|
2018-12-28 22:55:29 -05:00
|
|
|
bleio_reset();
|
2019-06-06 17:49:32 -04:00
|
|
|
#endif
|
2018-12-28 22:55:29 -05:00
|
|
|
|
2020-05-22 06:51:54 -04:00
|
|
|
#if CIRCUITPY_WATCHDOG
|
|
|
|
watchdog_reset();
|
|
|
|
#endif
|
|
|
|
|
2017-12-21 07:49:14 -05:00
|
|
|
reset_all_pins();
|
2021-02-17 06:53:18 -05:00
|
|
|
|
|
|
|
#ifdef MY_DEBUGUART
|
|
|
|
_debug_uart_init();
|
|
|
|
#endif
|
2017-12-21 07:49:14 -05:00
|
|
|
}
|
|
|
|
|
2018-10-19 21:46:22 -04:00
|
|
|
void reset_to_bootloader(void) {
|
|
|
|
enum { DFU_MAGIC_SERIAL = 0x4e };
|
|
|
|
|
|
|
|
NRF_POWER->GPREGRET = DFU_MAGIC_SERIAL;
|
2018-12-06 17:24:20 -05:00
|
|
|
reset_cpu();
|
|
|
|
}
|
|
|
|
|
|
|
|
void reset_cpu(void) {
|
2020-05-22 07:36:05 -04:00
|
|
|
// We're getting ready to reset, so save the counter off.
|
|
|
|
// This counter will get reset to zero during the reboot.
|
|
|
|
uint32_t ticks = nrfx_rtc_counter_get(&rtc_instance);
|
2020-05-22 23:26:27 -04:00
|
|
|
overflow_tracker.overflowed_ticks += ticks / 32;
|
2018-10-19 21:46:22 -04:00
|
|
|
NVIC_SystemReset();
|
2020-09-24 12:20:32 -04:00
|
|
|
for (;;) {
|
|
|
|
}
|
2018-10-19 21:46:22 -04:00
|
|
|
}
|
|
|
|
|
2020-05-17 23:45:35 -04:00
|
|
|
// The uninitialized data section is placed directly after BSS, under the theory
|
|
|
|
// that Circuit Python has a lot more .data and .bss than the bootloader. As a
|
|
|
|
// result, this section is less likely to be tampered with by the bootloader.
|
|
|
|
extern uint32_t _euninitialized;
|
|
|
|
|
2020-01-18 21:06:56 -05:00
|
|
|
uint32_t *port_heap_get_bottom(void) {
|
2020-05-17 23:45:35 -04:00
|
|
|
return &_euninitialized;
|
2020-01-18 21:06:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t *port_heap_get_top(void) {
|
|
|
|
return port_stack_get_top();
|
|
|
|
}
|
|
|
|
|
2020-10-11 08:59:33 -04:00
|
|
|
bool port_has_fixed_stack(void) {
|
|
|
|
return false;
|
2020-05-15 19:22:33 -04:00
|
|
|
}
|
|
|
|
|
2019-10-18 05:00:09 -04:00
|
|
|
uint32_t *port_stack_get_limit(void) {
|
2020-05-17 23:45:35 -04:00
|
|
|
return &_euninitialized;
|
2019-10-18 05:00:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t *port_stack_get_top(void) {
|
|
|
|
return &_estack;
|
|
|
|
}
|
|
|
|
|
2020-05-17 23:46:38 -04:00
|
|
|
// Place the word in the uninitialized section so it won't get overwritten.
|
|
|
|
__attribute__((section(".uninitialized"))) uint32_t _saved_word;
|
2018-12-06 17:24:20 -05:00
|
|
|
void port_set_saved_word(uint32_t value) {
|
2020-05-17 23:46:38 -04:00
|
|
|
_saved_word = value;
|
2018-12-06 17:24:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t port_get_saved_word(void) {
|
2020-05-17 23:46:38 -04:00
|
|
|
return _saved_word;
|
2018-12-06 17:24:20 -05:00
|
|
|
}
|
2017-12-21 07:49:14 -05:00
|
|
|
|
2020-03-06 21:15:16 -05:00
|
|
|
uint64_t port_get_raw_ticks(uint8_t* subticks) {
|
2020-10-12 21:36:18 -04:00
|
|
|
common_hal_mcu_disable_interrupts();
|
2020-03-06 21:15:16 -05:00
|
|
|
uint32_t rtc = nrfx_rtc_counter_get(&rtc_instance);
|
2021-01-06 11:09:06 -05:00
|
|
|
uint64_t overflow_count = overflow_tracker.overflowed_ticks;
|
2020-10-12 21:36:18 -04:00
|
|
|
common_hal_mcu_enable_interrupts();
|
|
|
|
|
2020-03-06 21:15:16 -05:00
|
|
|
if (subticks != NULL) {
|
|
|
|
*subticks = (rtc % 32);
|
|
|
|
}
|
2020-10-12 21:36:18 -04:00
|
|
|
return overflow_count + rtc / 32;
|
2020-03-05 20:14:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Enable 1/1024 second tick.
|
|
|
|
void port_enable_tick(void) {
|
|
|
|
nrfx_rtc_tick_enable(&rtc_instance, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Disable 1/1024 second tick.
|
|
|
|
void port_disable_tick(void) {
|
|
|
|
nrfx_rtc_tick_disable(&rtc_instance);
|
|
|
|
}
|
|
|
|
|
2021-02-17 06:53:18 -05:00
|
|
|
void port_interrupt_after_ticks_ch(uint32_t channel, uint32_t ticks) {
|
2020-03-05 20:14:54 -05:00
|
|
|
uint32_t current_ticks = nrfx_rtc_counter_get(&rtc_instance);
|
|
|
|
uint32_t diff = 3;
|
|
|
|
if (ticks > diff) {
|
2020-03-06 21:15:16 -05:00
|
|
|
diff = ticks * 32;
|
|
|
|
}
|
|
|
|
if (diff > 0xffffff) {
|
|
|
|
diff = 0xffffff;
|
2020-03-05 20:14:54 -05:00
|
|
|
}
|
2021-02-17 06:53:18 -05:00
|
|
|
nrfx_rtc_cc_set(&rtc_instance, channel, current_ticks + diff, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void port_disable_interrupt_after_ticks_ch(uint32_t channel) {
|
|
|
|
nrfx_rtc_cc_disable(&rtc_instance, channel);
|
|
|
|
}
|
|
|
|
|
|
|
|
void port_interrupt_after_ticks(uint32_t ticks) {
|
|
|
|
port_interrupt_after_ticks_ch(0, ticks);
|
2020-03-05 20:14:54 -05:00
|
|
|
}
|
|
|
|
|
2020-12-02 21:05:29 -05:00
|
|
|
void port_idle_until_interrupt(void) {
|
2020-08-07 04:23:42 -04:00
|
|
|
#if defined(MICROPY_QSPI_CS)
|
2020-08-05 21:56:05 -04:00
|
|
|
qspi_disable();
|
2020-08-04 13:10:58 -04:00
|
|
|
#endif
|
|
|
|
|
2020-03-05 20:14:54 -05:00
|
|
|
// Clear the FPU interrupt because it can prevent us from sleeping.
|
|
|
|
if (NVIC_GetPendingIRQ(FPU_IRQn)) {
|
|
|
|
__set_FPSCR(__get_FPSCR() & ~(0x9f));
|
|
|
|
(void) __get_FPSCR();
|
|
|
|
NVIC_ClearPendingIRQ(FPU_IRQn);
|
|
|
|
}
|
2020-03-06 21:15:16 -05:00
|
|
|
uint8_t sd_enabled;
|
|
|
|
|
|
|
|
sd_softdevice_is_enabled(&sd_enabled);
|
|
|
|
if (sd_enabled) {
|
|
|
|
sd_app_evt_wait();
|
|
|
|
} else {
|
|
|
|
// Call wait for interrupt ourselves if the SD isn't enabled.
|
2020-05-08 00:38:23 -04:00
|
|
|
// Note that `wfi` should be called with interrupts disabled,
|
|
|
|
// to ensure that the queue is properly drained. The `wfi`
|
|
|
|
// instruction will returned as long as an interrupt is
|
|
|
|
// available, even though the actual handler won't fire until
|
|
|
|
// we re-enable interrupts.
|
nRF: Always use sd_nvic_critical_region calls
The motivation for doing this is so that we can allow
common_hal_mcu_disable_interrupts in IRQ context, something that works
on other ports, but not on nRF with SD enabled. This is because
when SD is enabled, calling sd_softdevice_is_enabled in the context
of an interrupt with priority 2 or 3 causes a HardFault. We have chosen
to give the USB interrupt priority 2 on nRF, the highest priority that
is compatible with SD.
Since at least SoftDevice s130 v2.0.1, sd_nvic_critical_region_enter/exit
have been implemented as inline functions and are safe to call even if
softdevice is not enabled. Reference kindly provided by danh:
https://devzone.nordicsemi.com/f/nordic-q-a/29553/sd_nvic_critical_region_enter-exit-missing-in-s130-v2
Switching to these as the default/only way to enable/disable interrupts
simplifies things, and fixes several problems and potential problems:
* Interrupts at priority 2 or 3 could not call common_hal_mcu_disable_interrupts
because the call to sd_softdevice_is_enabled would HardFault
* Hypothetically, the state of sd_softdevice_is_enabled
could change from the disable to the enable call, meaning the calls
would not match (__disable_irq() could be balanced with
sd_nvic_critical_region_exit).
This also fixes a problem I believe would exist if disable() were called
twice when SD is enabled. There is a single "is_nested_critical_region"
flag, and the second call would set it to 1. Both of the enable()
calls that followed would call critical_region_exit(1), and interrupts
would not properly be reenabled. In the new version of the code,
we use our own nesting_count value to track the intended state, so
now nested disable()s only call critical_region_enter() once, only
updating is_nested_critical_region once; and only the second enable()
call will call critical_region_exit, with the right value of i_n_c_r.
Finally, in port_sleep_until_interrupt, if !sd_enabled, we really do
need to __disable_irq, rather than using the common_hal_mcu routines;
the reason why is documented in a comment.
2020-07-14 18:34:45 -04:00
|
|
|
//
|
|
|
|
// We do not use common_hal_mcu_disable_interrupts here because
|
|
|
|
// we truly require that interrupts be disabled, while
|
|
|
|
// common_hal_mcu_disable_interrupts actually just masks the
|
|
|
|
// interrupts that are not required to allow the softdevice to
|
|
|
|
// function (whether or not SD is enabled)
|
|
|
|
int nested = __get_PRIMASK();
|
|
|
|
__disable_irq();
|
2020-05-21 09:46:37 -04:00
|
|
|
if (!tud_task_event_ready()) {
|
|
|
|
__DSB();
|
2020-05-08 00:38:23 -04:00
|
|
|
__WFI();
|
2020-05-21 09:46:37 -04:00
|
|
|
}
|
nRF: Always use sd_nvic_critical_region calls
The motivation for doing this is so that we can allow
common_hal_mcu_disable_interrupts in IRQ context, something that works
on other ports, but not on nRF with SD enabled. This is because
when SD is enabled, calling sd_softdevice_is_enabled in the context
of an interrupt with priority 2 or 3 causes a HardFault. We have chosen
to give the USB interrupt priority 2 on nRF, the highest priority that
is compatible with SD.
Since at least SoftDevice s130 v2.0.1, sd_nvic_critical_region_enter/exit
have been implemented as inline functions and are safe to call even if
softdevice is not enabled. Reference kindly provided by danh:
https://devzone.nordicsemi.com/f/nordic-q-a/29553/sd_nvic_critical_region_enter-exit-missing-in-s130-v2
Switching to these as the default/only way to enable/disable interrupts
simplifies things, and fixes several problems and potential problems:
* Interrupts at priority 2 or 3 could not call common_hal_mcu_disable_interrupts
because the call to sd_softdevice_is_enabled would HardFault
* Hypothetically, the state of sd_softdevice_is_enabled
could change from the disable to the enable call, meaning the calls
would not match (__disable_irq() could be balanced with
sd_nvic_critical_region_exit).
This also fixes a problem I believe would exist if disable() were called
twice when SD is enabled. There is a single "is_nested_critical_region"
flag, and the second call would set it to 1. Both of the enable()
calls that followed would call critical_region_exit(1), and interrupts
would not properly be reenabled. In the new version of the code,
we use our own nesting_count value to track the intended state, so
now nested disable()s only call critical_region_enter() once, only
updating is_nested_critical_region once; and only the second enable()
call will call critical_region_exit, with the right value of i_n_c_r.
Finally, in port_sleep_until_interrupt, if !sd_enabled, we really do
need to __disable_irq, rather than using the common_hal_mcu routines;
the reason why is documented in a comment.
2020-07-14 18:34:45 -04:00
|
|
|
if (!nested) {
|
|
|
|
__enable_irq();
|
|
|
|
}
|
2020-03-06 21:15:16 -05:00
|
|
|
}
|
2020-03-05 20:14:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-12-06 17:24:20 -05:00
|
|
|
void HardFault_Handler(void) {
|
|
|
|
reset_into_safe_mode(HARD_CRASH);
|
2018-10-16 11:05:02 -04:00
|
|
|
while (true) {
|
2018-12-06 17:24:20 -05:00
|
|
|
asm("nop;");
|
2018-10-16 11:05:02 -04:00
|
|
|
}
|
2017-12-21 07:49:14 -05:00
|
|
|
}
|
2021-02-21 02:27:21 -05:00
|
|
|
|
|
|
|
#if CIRCUITPY_ALARM
|
|
|
|
// in case boards/xxx/board.c does not provide board_deinit()
|
|
|
|
MP_WEAK void board_deinit(void) {
|
|
|
|
}
|
|
|
|
#endif
|