2019-11-02 11:52:26 -04:00
|
|
|
/*
|
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
2020-01-08 23:32:45 -05:00
|
|
|
* Copyright (c) 2020 Scott Shawcroft for Adafruit Industries
|
|
|
|
* Copyright (c) 2020 Artur Pacholec
|
2019-11-02 11:52:26 -04:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
2020-01-08 23:32:45 -05:00
|
|
|
/*
|
|
|
|
* Copyright 2018 NXP
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
*/
|
2019-11-02 11:52:26 -04:00
|
|
|
|
2020-12-02 21:05:29 -05:00
|
|
|
#include "supervisor/board.h"
|
2019-11-02 11:52:26 -04:00
|
|
|
#include "supervisor/port.h"
|
|
|
|
|
|
|
|
#include "fsl_device_registers.h"
|
|
|
|
|
|
|
|
#include "common-hal/microcontroller/Pin.h"
|
2020-08-18 16:08:33 -04:00
|
|
|
#include "common-hal/pwmio/PWMOut.h"
|
2019-11-02 11:52:26 -04:00
|
|
|
#include "common-hal/rtc/RTC.h"
|
2020-05-01 10:06:40 -04:00
|
|
|
#include "common-hal/busio/SPI.h"
|
2021-08-19 15:18:13 -04:00
|
|
|
#include "shared-bindings/microcontroller/__init__.h"
|
2019-11-02 11:52:26 -04:00
|
|
|
|
|
|
|
#include "reset.h"
|
|
|
|
|
2021-08-19 15:18:13 -04:00
|
|
|
#include "supervisor/background_callback.h"
|
2019-11-02 11:52:26 -04:00
|
|
|
|
|
|
|
#if CIRCUITPY_GAMEPADSHIFT
|
|
|
|
#include "shared-module/gamepadshift/__init__.h"
|
|
|
|
#endif
|
2020-06-21 09:31:26 -04:00
|
|
|
#if CIRCUITPY_PEW
|
2019-11-02 11:52:26 -04:00
|
|
|
#include "shared-module/_pew/PewPew.h"
|
2020-06-21 09:31:26 -04:00
|
|
|
#endif
|
2020-04-07 17:16:07 -04:00
|
|
|
#include "supervisor/shared/tick.h"
|
2019-11-02 11:52:26 -04:00
|
|
|
|
|
|
|
#include "clocks.h"
|
|
|
|
|
|
|
|
#include "fsl_gpio.h"
|
|
|
|
#include "fsl_lpuart.h"
|
|
|
|
|
2020-01-08 23:32:45 -05:00
|
|
|
// Device memories must be accessed in order.
|
|
|
|
#define DEVICE 2
|
|
|
|
// Normal memory can have accesses reorder and prefetched.
|
|
|
|
#define NORMAL 0
|
|
|
|
|
|
|
|
// Prevents instruction access.
|
|
|
|
#define NO_EXECUTION 1
|
|
|
|
#define EXECUTION 0
|
|
|
|
|
2020-09-28 22:21:17 -04:00
|
|
|
// Shareable if the memory system manages coherency. This means shared between memory bus masters,
|
|
|
|
// not just CPUs.
|
2020-01-08 23:32:45 -05:00
|
|
|
#define NOT_SHAREABLE 0
|
|
|
|
#define SHAREABLE 1
|
|
|
|
|
|
|
|
//
|
|
|
|
#define NOT_CACHEABLE 0
|
|
|
|
#define CACHEABLE 1
|
|
|
|
|
|
|
|
#define NOT_BUFFERABLE 0
|
|
|
|
#define BUFFERABLE 1
|
|
|
|
|
|
|
|
#define NO_SUBREGIONS 0
|
|
|
|
|
|
|
|
extern uint32_t _ld_flash_size;
|
2020-01-18 14:54:01 -05:00
|
|
|
extern uint32_t _ld_stack_top;
|
|
|
|
|
|
|
|
extern uint32_t __isr_vector[];
|
|
|
|
|
|
|
|
extern uint32_t _ld_ocram_bss_start;
|
|
|
|
extern uint32_t _ld_ocram_bss_size;
|
|
|
|
extern uint32_t _ld_ocram_data_destination;
|
|
|
|
extern uint32_t _ld_ocram_data_size;
|
|
|
|
extern uint32_t _ld_ocram_data_flash_copy;
|
|
|
|
extern uint32_t _ld_dtcm_bss_start;
|
|
|
|
extern uint32_t _ld_dtcm_bss_size;
|
|
|
|
extern uint32_t _ld_dtcm_data_destination;
|
|
|
|
extern uint32_t _ld_dtcm_data_size;
|
|
|
|
extern uint32_t _ld_dtcm_data_flash_copy;
|
|
|
|
extern uint32_t _ld_itcm_destination;
|
|
|
|
extern uint32_t _ld_itcm_size;
|
|
|
|
extern uint32_t _ld_itcm_flash_copy;
|
|
|
|
|
|
|
|
extern void main(void);
|
|
|
|
|
|
|
|
// This replaces the Reset_Handler in startup_*.S and SystemInit in system_*.c.
|
2021-11-12 20:30:24 -05:00
|
|
|
void Reset_Handler(void);
|
2020-01-18 14:54:01 -05:00
|
|
|
__attribute__((used, naked)) void Reset_Handler(void) {
|
|
|
|
__disable_irq();
|
2021-03-15 09:57:36 -04:00
|
|
|
SCB->VTOR = (uint32_t)&__isr_vector;
|
|
|
|
__set_MSP((uint32_t)&_ld_stack_top);
|
2020-01-08 23:32:45 -05:00
|
|
|
|
|
|
|
/* Disable I cache and D cache */
|
2020-01-18 14:54:01 -05:00
|
|
|
SCB_DisableICache();
|
|
|
|
SCB_DisableDCache();
|
|
|
|
|
|
|
|
// Changing the FlexRAM must happen here where the stack is empty. If it is in a function call,
|
|
|
|
// then the return will jump to an invalid address.
|
2020-01-08 23:32:45 -05:00
|
|
|
// Configure FlexRAM. The e is one block of ITCM (0b11) and DTCM (0b10). The rest is two OCRAM
|
|
|
|
// (0b01). We shift in zeroes for all unimplemented banks.
|
|
|
|
IOMUXC_GPR->GPR17 = (0xe5555555) >> (32 - 2 * FSL_FEATURE_FLEXRAM_INTERNAL_RAM_TOTAL_BANK_NUMBERS);
|
|
|
|
|
|
|
|
// Switch from FlexRAM fuse config to the IOMUXC values.
|
|
|
|
IOMUXC_GPR->GPR16 |= IOMUXC_GPR_GPR16_FLEXRAM_BANK_CFG_SEL(1);
|
|
|
|
|
|
|
|
// Let the core know the TCM sizes changed.
|
|
|
|
uint32_t current_gpr14 = IOMUXC_GPR->GPR14;
|
|
|
|
current_gpr14 &= ~IOMUXC_GPR_GPR14_CM7_CFGDTCMSZ_MASK;
|
|
|
|
current_gpr14 |= IOMUXC_GPR_GPR14_CM7_CFGDTCMSZ(0x6);
|
|
|
|
current_gpr14 &= ~IOMUXC_GPR_GPR14_CM7_CFGITCMSZ_MASK;
|
|
|
|
current_gpr14 |= IOMUXC_GPR_GPR14_CM7_CFGITCMSZ(0x6);
|
|
|
|
IOMUXC_GPR->GPR14 = current_gpr14;
|
|
|
|
|
2020-01-18 14:54:01 -05:00
|
|
|
#if ((__FPU_PRESENT == 1) && (__FPU_USED == 1))
|
2021-03-15 09:57:36 -04:00
|
|
|
SCB->CPACR |= ((3UL << 10 * 2) | (3UL << 11 * 2)); /* set CP10, CP11 Full Access */
|
2020-01-18 14:54:01 -05:00
|
|
|
#endif /* ((__FPU_PRESENT == 1) && (__FPU_USED == 1)) */
|
2020-01-08 23:32:45 -05:00
|
|
|
|
2020-01-18 14:54:01 -05:00
|
|
|
/* Disable Watchdog Power Down Counter */
|
|
|
|
WDOG1->WMCR &= ~WDOG_WMCR_PDE_MASK;
|
|
|
|
WDOG2->WMCR &= ~WDOG_WMCR_PDE_MASK;
|
2020-01-08 23:32:45 -05:00
|
|
|
|
2020-01-18 14:54:01 -05:00
|
|
|
/* Watchdog disable */
|
|
|
|
WDOG1->WCR &= ~WDOG_WCR_WDE_MASK;
|
|
|
|
WDOG2->WCR &= ~WDOG_WCR_WDE_MASK;
|
|
|
|
RTWDOG->CNT = 0xD928C520U; /* 0xD928C520U is the update key */
|
|
|
|
RTWDOG->TOVAL = 0xFFFF;
|
2021-03-15 09:57:36 -04:00
|
|
|
RTWDOG->CS = (uint32_t)((RTWDOG->CS) & ~RTWDOG_CS_EN_MASK) | RTWDOG_CS_UPDATE_MASK;
|
2020-01-08 23:32:45 -05:00
|
|
|
|
2020-01-18 14:54:01 -05:00
|
|
|
/* Disable Systick which might be enabled by bootrom */
|
2021-03-15 09:57:36 -04:00
|
|
|
if (SysTick->CTRL & SysTick_CTRL_ENABLE_Msk) {
|
2020-01-18 14:54:01 -05:00
|
|
|
SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Disable MPU */
|
|
|
|
ARM_MPU_Disable();
|
|
|
|
|
2020-04-02 11:47:16 -04:00
|
|
|
// Copy all of the itcm code to run from ITCM. Do this while the MPU is disabled because we write
|
2020-01-18 14:54:01 -05:00
|
|
|
// protect it.
|
2021-03-15 09:57:36 -04:00
|
|
|
for (uint32_t i = 0; i < ((size_t)&_ld_itcm_size) / 4; i++) {
|
2020-01-18 14:54:01 -05:00
|
|
|
(&_ld_itcm_destination)[i] = (&_ld_itcm_flash_copy)[i];
|
|
|
|
}
|
2020-01-08 23:32:45 -05:00
|
|
|
|
|
|
|
// The first number in RBAR is the region number. When searching for a policy, the region with
|
|
|
|
// the highest number wins. If none match, then the default policy set at enable applies.
|
2019-11-02 11:52:26 -04:00
|
|
|
|
2020-01-08 23:32:45 -05:00
|
|
|
// This is an undocumented region and is likely more registers.
|
|
|
|
MPU->RBAR = ARM_MPU_RBAR(8, 0xC0000000U);
|
|
|
|
MPU->RASR = ARM_MPU_RASR(EXECUTION, ARM_MPU_AP_FULL, DEVICE, NOT_SHAREABLE, NOT_CACHEABLE, NOT_BUFFERABLE, NO_SUBREGIONS, ARM_MPU_REGION_SIZE_512MB);
|
|
|
|
|
|
|
|
// This is the SEMC region where external RAM and 8+ flash would live. Disable for now, even though the EVKs have stuff here.
|
|
|
|
MPU->RBAR = ARM_MPU_RBAR(9, 0x80000000U);
|
|
|
|
MPU->RASR = ARM_MPU_RASR(NO_EXECUTION, ARM_MPU_AP_NONE, DEVICE, NOT_SHAREABLE, NOT_CACHEABLE, NOT_BUFFERABLE, NO_SUBREGIONS, ARM_MPU_REGION_SIZE_1GB);
|
2019-11-02 11:52:26 -04:00
|
|
|
|
2020-01-08 23:32:45 -05:00
|
|
|
// FlexSPI2 is 0x70000000
|
|
|
|
|
|
|
|
// This the first 1MB of flash is the bootloader and CircuitPython read-only data.
|
|
|
|
MPU->RBAR = ARM_MPU_RBAR(10, 0x60000000U);
|
|
|
|
MPU->RASR = ARM_MPU_RASR(EXECUTION, ARM_MPU_AP_FULL, NORMAL, NOT_SHAREABLE, CACHEABLE, BUFFERABLE, NO_SUBREGIONS, ARM_MPU_REGION_SIZE_1MB);
|
|
|
|
|
|
|
|
// The remainder of flash is the fat filesystem which could have code on it too. Make sure that
|
|
|
|
// we set the region to the minimal size so that bad data doesn't get speculatively fetched.
|
|
|
|
// Thanks to Damien for the tip!
|
|
|
|
uint32_t region_size = ARM_MPU_REGION_SIZE_32B;
|
|
|
|
uint32_t filesystem_size = &_ld_filesystem_end - &_ld_filesystem_start;
|
|
|
|
while (filesystem_size > (1u << (region_size + 1))) {
|
|
|
|
region_size += 1;
|
|
|
|
}
|
|
|
|
// Mask out as much of the remainder as we can. For example on an 8MB flash, 7MB are for the
|
|
|
|
// filesystem. The region_size here must be a power of 2 so it is 8MB. Using the subregion mask
|
|
|
|
// we can ignore 1/8th size chunks. So, we ignore the last 1MB using the subregion.
|
|
|
|
uint32_t remainder = (1u << (region_size + 1)) - filesystem_size;
|
|
|
|
uint32_t subregion_size = (1u << (region_size + 1)) / 8;
|
2020-01-17 21:35:09 -05:00
|
|
|
uint8_t subregion_mask = (0xff00 >> (remainder / subregion_size)) & 0xff;
|
2020-01-08 23:32:45 -05:00
|
|
|
|
|
|
|
MPU->RBAR = ARM_MPU_RBAR(11, 0x60100000U);
|
2020-01-17 21:35:09 -05:00
|
|
|
MPU->RASR = ARM_MPU_RASR(EXECUTION, ARM_MPU_AP_FULL, NORMAL, NOT_SHAREABLE, CACHEABLE, BUFFERABLE, subregion_mask, region_size);
|
2020-01-08 23:32:45 -05:00
|
|
|
|
|
|
|
// This the ITCM. Set it to read-only because we've loaded everything already and it's easy to
|
|
|
|
// accidentally write the wrong value to 0x00000000 (aka NULL).
|
|
|
|
MPU->RBAR = ARM_MPU_RBAR(12, 0x00000000U);
|
|
|
|
MPU->RASR = ARM_MPU_RASR(EXECUTION, ARM_MPU_AP_RO, NORMAL, NOT_SHAREABLE, CACHEABLE, BUFFERABLE, NO_SUBREGIONS, ARM_MPU_REGION_SIZE_32KB);
|
|
|
|
|
|
|
|
// This the DTCM.
|
|
|
|
MPU->RBAR = ARM_MPU_RBAR(13, 0x20000000U);
|
|
|
|
MPU->RASR = ARM_MPU_RASR(EXECUTION, ARM_MPU_AP_FULL, NORMAL, NOT_SHAREABLE, CACHEABLE, BUFFERABLE, NO_SUBREGIONS, ARM_MPU_REGION_SIZE_32KB);
|
|
|
|
|
2020-09-28 22:21:17 -04:00
|
|
|
// This is OCRAM. We mark it as shareable so that it isn't cached. This makes USB work at the
|
|
|
|
// cost of 1/4 speed OCRAM accesses. It will leave more room for caching data from the flash
|
|
|
|
// too which might be a net win.
|
2020-01-08 23:32:45 -05:00
|
|
|
MPU->RBAR = ARM_MPU_RBAR(14, 0x20200000U);
|
2020-09-28 22:21:17 -04:00
|
|
|
MPU->RASR = ARM_MPU_RASR(EXECUTION, ARM_MPU_AP_FULL, NORMAL, SHAREABLE, CACHEABLE, BUFFERABLE, NO_SUBREGIONS, ARM_MPU_REGION_SIZE_512KB);
|
2020-01-08 23:32:45 -05:00
|
|
|
|
|
|
|
// We steal 64k from FlexRAM for ITCM and DTCM so disable those memory regions here.
|
|
|
|
MPU->RBAR = ARM_MPU_RBAR(15, 0x20280000U);
|
|
|
|
MPU->RASR = ARM_MPU_RASR(EXECUTION, ARM_MPU_AP_FULL, NORMAL, NOT_SHAREABLE, CACHEABLE, BUFFERABLE, 0x80, ARM_MPU_REGION_SIZE_512KB);
|
|
|
|
|
|
|
|
/* Enable MPU */
|
|
|
|
ARM_MPU_Enable(MPU_CTRL_PRIVDEFENA_Msk);
|
|
|
|
|
2020-01-18 14:54:01 -05:00
|
|
|
/* We're done mucking with memory so enable I cache and D cache */
|
2020-01-08 23:32:45 -05:00
|
|
|
SCB_EnableDCache();
|
|
|
|
SCB_EnableICache();
|
2020-01-18 14:54:01 -05:00
|
|
|
|
|
|
|
// Copy all of the data to run from DTCM.
|
2021-03-15 09:57:36 -04:00
|
|
|
for (uint32_t i = 0; i < ((size_t)&_ld_dtcm_data_size) / 4; i++) {
|
2020-01-18 14:54:01 -05:00
|
|
|
(&_ld_dtcm_data_destination)[i] = (&_ld_dtcm_data_flash_copy)[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clear DTCM bss.
|
2021-03-15 09:57:36 -04:00
|
|
|
for (uint32_t i = 0; i < ((size_t)&_ld_dtcm_bss_size) / 4; i++) {
|
2020-01-18 14:54:01 -05:00
|
|
|
(&_ld_dtcm_bss_start)[i] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy all of the data to run from OCRAM.
|
2021-03-15 09:57:36 -04:00
|
|
|
for (uint32_t i = 0; i < ((size_t)&_ld_ocram_data_size) / 4; i++) {
|
2020-01-18 14:54:01 -05:00
|
|
|
(&_ld_ocram_data_destination)[i] = (&_ld_ocram_data_flash_copy)[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clear OCRAM bss.
|
2021-03-15 09:57:36 -04:00
|
|
|
for (uint32_t i = 0; i < ((size_t)&_ld_ocram_bss_size) / 4; i++) {
|
2020-01-18 14:54:01 -05:00
|
|
|
(&_ld_ocram_bss_start)[i] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
__enable_irq();
|
|
|
|
main();
|
2019-11-02 11:52:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
safe_mode_t port_init(void) {
|
2020-04-07 17:16:07 -04:00
|
|
|
CLOCK_SetMode(kCLOCK_ModeRun);
|
|
|
|
|
2019-11-02 11:52:26 -04:00
|
|
|
clocks_init();
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
#if CIRCUITPY_RTC
|
2019-11-02 11:52:26 -04:00
|
|
|
rtc_init();
|
2021-03-15 09:57:36 -04:00
|
|
|
#endif
|
2019-11-02 11:52:26 -04:00
|
|
|
|
2020-04-07 20:15:18 -04:00
|
|
|
// Always enable the SNVS interrupt. The GPC won't wake us up unless at least one interrupt is
|
|
|
|
// enabled. It won't occur very often so it'll be low overhead.
|
|
|
|
NVIC_EnableIRQ(SNVS_HP_WRAPPER_IRQn);
|
|
|
|
|
2020-05-30 05:44:13 -04:00
|
|
|
// Note that `reset_port` CANNOT GO HERE, unlike other ports, because `board_init` hasn't been
|
2020-05-29 12:19:37 -04:00
|
|
|
// run yet, which uses `never_reset` to protect critical pins from being reset by `reset_port`.
|
2019-11-02 11:52:26 -04:00
|
|
|
|
|
|
|
if (board_requests_safe_mode()) {
|
|
|
|
return USER_SAFE_MODE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NO_SAFE_MODE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void reset_port(void) {
|
2020-05-01 10:06:40 -04:00
|
|
|
spi_reset();
|
2019-11-02 11:52:26 -04:00
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
#if CIRCUITPY_AUDIOIO
|
2019-11-02 11:52:26 -04:00
|
|
|
audio_dma_reset();
|
|
|
|
audioout_reset();
|
2021-03-15 09:57:36 -04:00
|
|
|
#endif
|
|
|
|
#if CIRCUITPY_AUDIOBUSIO
|
2019-11-02 11:52:26 -04:00
|
|
|
i2sout_reset();
|
2021-03-15 09:57:36 -04:00
|
|
|
// pdmin_reset();
|
|
|
|
#endif
|
2019-11-02 11:52:26 -04:00
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
#if CIRCUITPY_TOUCHIO && CIRCUITPY_TOUCHIO_USE_NATIVE
|
2019-11-02 11:52:26 -04:00
|
|
|
touchin_reset();
|
2021-03-15 09:57:36 -04:00
|
|
|
#endif
|
2019-11-02 11:52:26 -04:00
|
|
|
|
|
|
|
// eic_reset();
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
#if CIRCUITPY_PWMIO
|
2019-11-02 11:52:26 -04:00
|
|
|
pwmout_reset();
|
2021-03-15 09:57:36 -04:00
|
|
|
#endif
|
2019-11-02 11:52:26 -04:00
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
#if CIRCUITPY_RTC
|
2019-11-02 11:52:26 -04:00
|
|
|
rtc_reset();
|
2021-03-15 09:57:36 -04:00
|
|
|
#endif
|
2019-11-02 11:52:26 -04:00
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
#if CIRCUITPY_GAMEPADSHIFT
|
2019-11-02 11:52:26 -04:00
|
|
|
gamepadshift_reset();
|
2021-03-15 09:57:36 -04:00
|
|
|
#endif
|
|
|
|
#if CIRCUITPY_PEW
|
2019-11-02 11:52:26 -04:00
|
|
|
pew_reset();
|
2021-03-15 09:57:36 -04:00
|
|
|
#endif
|
2019-11-02 11:52:26 -04:00
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
// reset_event_system();
|
2019-11-02 11:52:26 -04:00
|
|
|
|
|
|
|
reset_all_pins();
|
|
|
|
}
|
|
|
|
|
|
|
|
void reset_to_bootloader(void) {
|
2020-01-08 23:32:45 -05:00
|
|
|
SNVS->LPGPR[0] = DBL_TAP_MAGIC;
|
2019-11-02 11:52:26 -04:00
|
|
|
reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
void reset_cpu(void) {
|
|
|
|
reset();
|
|
|
|
}
|
|
|
|
|
2020-01-08 23:32:45 -05:00
|
|
|
extern uint32_t _ld_heap_start, _ld_heap_end, _ld_stack_top, _ld_stack_bottom;
|
2019-11-02 11:52:26 -04:00
|
|
|
uint32_t *port_stack_get_limit(void) {
|
2020-09-28 22:21:17 -04:00
|
|
|
return &_ld_stack_bottom;
|
2019-11-02 11:52:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t *port_stack_get_top(void) {
|
2020-01-08 23:32:45 -05:00
|
|
|
return &_ld_stack_top;
|
|
|
|
}
|
|
|
|
|
2020-10-11 08:59:33 -04:00
|
|
|
bool port_has_fixed_stack(void) {
|
|
|
|
return true;
|
2020-09-28 22:21:17 -04:00
|
|
|
}
|
|
|
|
|
2020-01-08 23:32:45 -05:00
|
|
|
uint32_t *port_heap_get_bottom(void) {
|
|
|
|
return &_ld_heap_start;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get heap top address
|
|
|
|
uint32_t *port_heap_get_top(void) {
|
|
|
|
return &_ld_heap_end;
|
2019-11-02 11:52:26 -04:00
|
|
|
}
|
|
|
|
|
2020-04-07 16:07:29 -04:00
|
|
|
// Place the word into the low power section of the SNVS.
|
2019-11-02 11:52:26 -04:00
|
|
|
void port_set_saved_word(uint32_t value) {
|
2020-01-08 23:32:45 -05:00
|
|
|
SNVS->LPGPR[1] = value;
|
2019-11-02 11:52:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t port_get_saved_word(void) {
|
2020-01-08 23:32:45 -05:00
|
|
|
return SNVS->LPGPR[1];
|
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
uint64_t port_get_raw_ticks(uint8_t *subticks) {
|
2020-03-23 21:20:58 -04:00
|
|
|
uint64_t ticks = 0;
|
|
|
|
uint64_t next_ticks = 1;
|
|
|
|
while (ticks != next_ticks) {
|
|
|
|
ticks = next_ticks;
|
2021-03-15 09:57:36 -04:00
|
|
|
next_ticks = ((uint64_t)SNVS->HPRTCMR) << 32 | SNVS->HPRTCLR;
|
2020-03-23 21:20:58 -04:00
|
|
|
}
|
|
|
|
if (subticks != NULL) {
|
|
|
|
*subticks = ticks % 32;
|
|
|
|
}
|
|
|
|
return ticks / 32;
|
|
|
|
}
|
|
|
|
|
2021-11-12 20:30:24 -05:00
|
|
|
void SNVS_HP_WRAPPER_IRQHandler(void);
|
|
|
|
__attribute__((used))
|
2020-04-07 17:16:07 -04:00
|
|
|
void SNVS_HP_WRAPPER_IRQHandler(void) {
|
|
|
|
if ((SNVS->HPSR & SNVS_HPSR_PI_MASK) != 0) {
|
|
|
|
supervisor_tick();
|
|
|
|
SNVS->HPSR = SNVS_HPSR_PI_MASK;
|
|
|
|
}
|
|
|
|
if ((SNVS->HPSR & SNVS_HPSR_HPTA_MASK) != 0) {
|
|
|
|
SNVS->HPSR = SNVS_HPSR_HPTA_MASK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-23 21:20:58 -04:00
|
|
|
// Enable 1/1024 second tick.
|
|
|
|
void port_enable_tick(void) {
|
|
|
|
uint32_t hpcr = SNVS->HPCR;
|
|
|
|
hpcr &= ~SNVS_HPCR_PI_FREQ_MASK;
|
|
|
|
SNVS->HPCR = hpcr | SNVS_HPCR_PI_FREQ(5) | SNVS_HPCR_PI_EN_MASK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Disable 1/1024 second tick.
|
|
|
|
void port_disable_tick(void) {
|
|
|
|
SNVS->HPCR &= ~SNVS_HPCR_PI_EN_MASK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void port_interrupt_after_ticks(uint32_t ticks) {
|
|
|
|
uint8_t subticks;
|
|
|
|
uint64_t current_ticks = port_get_raw_ticks(&subticks);
|
|
|
|
current_ticks += ticks;
|
2020-04-07 16:07:29 -04:00
|
|
|
SNVS->HPCR &= ~SNVS_HPCR_HPTA_EN_MASK;
|
|
|
|
// Wait for the alarm to be disabled.
|
2021-03-15 09:57:36 -04:00
|
|
|
while ((SNVS->HPCR & SNVS_HPCR_HPTA_EN_MASK) != 0) {
|
|
|
|
}
|
2020-03-23 21:20:58 -04:00
|
|
|
SNVS->HPTAMR = current_ticks >> (32 - 5);
|
2020-04-07 16:07:29 -04:00
|
|
|
SNVS->HPTALR = current_ticks << 5 | subticks;
|
2020-03-23 21:20:58 -04:00
|
|
|
SNVS->HPCR |= SNVS_HPCR_HPTA_EN_MASK;
|
|
|
|
}
|
|
|
|
|
2020-12-02 21:05:29 -05:00
|
|
|
void port_idle_until_interrupt(void) {
|
2020-03-23 21:20:58 -04:00
|
|
|
// App note here: https://www.nxp.com/docs/en/application-note/AN12085.pdf
|
2022-04-16 12:00:45 -04:00
|
|
|
// Currently I have disabled the setting into wait mode as this impacts lots of differnt
|
|
|
|
// subsystems and it is unclear if you can or should set it generically without having
|
|
|
|
// a better understanding of user intent. For example by default it will kill PWM
|
|
|
|
// when in this mode, unless PWM_CTRL2_WAITEN_MASK is set, and even with this set
|
|
|
|
// it may not work properly if the same timer/subtimer is trying to PWM on multiple channels.
|
|
|
|
// Maybe at later date, revisit after we have a better understanding on things like which
|
|
|
|
// timers it impacts and how each subsystem is configured.
|
2020-03-23 21:20:58 -04:00
|
|
|
|
|
|
|
// Clear the FPU interrupt because it can prevent us from sleeping.
|
2021-03-15 09:57:36 -04:00
|
|
|
if (__get_FPSCR() & ~(0x9f)) {
|
|
|
|
__set_FPSCR(__get_FPSCR() & ~(0x9f));
|
|
|
|
(void)__get_FPSCR();
|
2020-03-23 21:20:58 -04:00
|
|
|
}
|
2021-08-19 15:18:13 -04:00
|
|
|
|
|
|
|
common_hal_mcu_disable_interrupts();
|
|
|
|
if (!background_callback_pending()) {
|
|
|
|
NVIC_ClearPendingIRQ(SNVS_HP_WRAPPER_IRQn);
|
2022-03-08 20:17:07 -05:00
|
|
|
// Don't down clock on debug builds because it prevents the DAP from
|
|
|
|
// reading memory
|
2022-04-16 12:00:45 -04:00
|
|
|
#if 0
|
2022-03-08 20:17:07 -05:00
|
|
|
#if CIRCUITPY_DEBUG == 0
|
2021-08-19 15:18:13 -04:00
|
|
|
CLOCK_SetMode(kCLOCK_ModeWait);
|
2022-03-08 20:17:07 -05:00
|
|
|
#endif
|
2021-08-19 15:18:13 -04:00
|
|
|
__WFI();
|
|
|
|
CLOCK_SetMode(kCLOCK_ModeRun);
|
2022-04-16 12:00:45 -04:00
|
|
|
#else
|
|
|
|
__WFI();
|
|
|
|
#endif
|
2021-08-19 15:18:13 -04:00
|
|
|
}
|
|
|
|
common_hal_mcu_enable_interrupts();
|
2020-03-23 21:20:58 -04:00
|
|
|
}
|
|
|
|
|
2020-01-08 23:32:45 -05:00
|
|
|
/**
|
|
|
|
* \brief Default interrupt handler for unused IRQs.
|
|
|
|
*/
|
2021-11-12 20:30:24 -05:00
|
|
|
void MemManage_Handler(void);
|
2021-03-15 09:57:36 -04:00
|
|
|
__attribute__((used)) void MemManage_Handler(void) {
|
2020-01-08 23:32:45 -05:00
|
|
|
reset_into_safe_mode(MEM_MANAGE);
|
|
|
|
while (true) {
|
2021-03-15 09:57:36 -04:00
|
|
|
asm ("nop;");
|
2020-01-08 23:32:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Default interrupt handler for unused IRQs.
|
|
|
|
*/
|
2021-11-12 20:30:24 -05:00
|
|
|
void BusFault_Handler(void);
|
2021-03-15 09:57:36 -04:00
|
|
|
__attribute__((used)) void BusFault_Handler(void) {
|
2020-01-08 23:32:45 -05:00
|
|
|
reset_into_safe_mode(MEM_MANAGE);
|
|
|
|
while (true) {
|
2021-03-15 09:57:36 -04:00
|
|
|
asm ("nop;");
|
2020-01-08 23:32:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Default interrupt handler for unused IRQs.
|
|
|
|
*/
|
2021-11-12 20:30:24 -05:00
|
|
|
void UsageFault_Handler(void);
|
2021-03-15 09:57:36 -04:00
|
|
|
__attribute__((used)) void UsageFault_Handler(void) {
|
2020-01-08 23:32:45 -05:00
|
|
|
reset_into_safe_mode(MEM_MANAGE);
|
|
|
|
while (true) {
|
2021-03-15 09:57:36 -04:00
|
|
|
asm ("nop;");
|
2020-01-08 23:32:45 -05:00
|
|
|
}
|
2019-11-02 11:52:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Default interrupt handler for unused IRQs.
|
|
|
|
*/
|
2021-11-12 20:30:24 -05:00
|
|
|
void HardFault_Handler(void);
|
2021-03-15 09:57:36 -04:00
|
|
|
__attribute__((used)) void HardFault_Handler(void) {
|
2019-11-02 11:52:26 -04:00
|
|
|
reset_into_safe_mode(HARD_CRASH);
|
|
|
|
while (true) {
|
2021-03-15 09:57:36 -04:00
|
|
|
asm ("nop;");
|
2019-11-02 11:52:26 -04:00
|
|
|
}
|
|
|
|
}
|