circuitpython/ports/stm/supervisor/port.c

373 lines
10 KiB
C
Raw Normal View History

2019-06-28 15:36:08 -04:00
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2017 Scott Shawcroft for Adafruit Industries
2019-07-24 14:21:27 -04:00
* Copyright (c) 2019 Lucian Copeland for Adafruit Industries
2019-06-28 15:36:08 -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.
*/
#include <stdint.h>
#include "supervisor/port.h"
#include "supervisor/board.h"
#include "lib/timeutils/timeutils.h"
2019-07-18 17:55:57 -04:00
2019-09-12 13:47:01 -04:00
#include "common-hal/microcontroller/Pin.h"
2020-04-13 12:03:05 -04:00
#if CIRCUITPY_BUSIO
#include "common-hal/busio/I2C.h"
2019-09-27 17:59:55 -04:00
#include "common-hal/busio/SPI.h"
2019-10-04 14:37:18 -04:00
#include "common-hal/busio/UART.h"
2020-04-13 12:03:05 -04:00
#endif
#if CIRCUITPY_PULSEIO
#include "common-hal/pulseio/PulseOut.h"
2020-03-10 17:16:31 -04:00
#include "common-hal/pulseio/PulseIn.h"
#endif
#if CIRCUITPY_PWMIO
#include "common-hal/pwmio/PWMOut.h"
#endif
#if CIRCUITPY_PULSEIO || CIRCUITPY_PWMIO
#include "peripherals/timers.h"
#endif
#if CIRCUITPY_SDIOIO
#include "common-hal/sdioio/SDCard.h"
#endif
#if CIRCUITPY_PULSEIO || CIRCUITPY_ALARM
#include "peripherals/exti.h"
#endif
2019-07-18 17:55:57 -04:00
#include "peripherals/clocks.h"
#include "peripherals/gpio.h"
#include "peripherals/rtc.h"
2019-07-18 17:55:57 -04:00
#include STM32_HAL_H
Add some NORETURN attributes I have a function where it should be impossible to reach the end, so I put in a safe-mode reset at the bottom: ``` int find_unused_slot(void) { // precondition: you already verified that a slot was available for (int i=0; i<NUM_SLOTS; i++) { if( slot_free(i)) { return i; } } safe_mode_reset(MICROPY_FATAL_ERROR); } ``` However, the compiler still gave a diagnostic, because safe_mode_reset was not declared NORETURN. So I started by teaching the compiler that reset_into_safe_mode never returned. This leads at least one level deeper due to reset_cpu needing to be a NORETURN function. Each port is a little different in this area. I also marked reset_to_bootloader as NORETURN. Additional notes: * stm32's reset_to_bootloader was not implemented, but now does a bare reset. Most stm32s are not fitted with uf2 bootloaders anyway. * ditto cxd56 * esp32s2 did not implement reset_cpu at all. I used esp_restart(). (not tested) * litex did not implement reset_cpu at all. I used reboot_ctrl_write. But notably this is what reset_to_bootloader already did, so one or the other must be incorrect (not tested). reboot_ctrl_write cannot be declared NORETURN, as it returns unless the special value 0xac is written), so a new unreachable forever-loop is added. * cxd56's reset is via a boardctl() call which can't generically be declared NORETURN, so a new unreacahble "for(;;)" forever-loop is added. * In several places, NVIC_SystemReset is redeclared with NORETURN applied. This is accepted just fine by gcc. I chose this as preferable to editing the multiple copies of CMSIS headers where it is normally declared. * the stub safe_mode reset simply aborts. This is used in mpy-cross.
2020-09-24 12:20:32 -04:00
void NVIC_SystemReset(void) NORETURN;
2020-06-09 18:01:52 -04:00
#if (CPY_STM32H7) || (CPY_STM32F7)
// 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
// Shareable if the memory system manages coherency.
#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_stack_top;
extern uint32_t _ld_d1_ram_bss_start;
extern uint32_t _ld_d1_ram_bss_size;
extern uint32_t _ld_d1_ram_data_destination;
extern uint32_t _ld_d1_ram_data_size;
extern uint32_t _ld_d1_ram_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);
extern void SystemInit(void);
2020-06-09 18:01:52 -04:00
// This replaces the Reset_Handler in gcc/startup_*.s, calls SystemInit from system_*.c
__attribute__((used, naked)) void Reset_Handler(void) {
__disable_irq();
2021-03-15 09:57:36 -04:00
__set_MSP((uint32_t)&_ld_stack_top);
/* Disable MPU */
ARM_MPU_Disable();
// Copy all of the itcm code to run from ITCM. Do this while the MPU is disabled because we write
// protect it.
2021-03-15 09:57:36 -04:00
for (uint32_t i = 0; i < ((size_t)&_ld_itcm_size) / 4; i++) {
(&_ld_itcm_destination)[i] = (&_ld_itcm_flash_copy)[i];
}
// 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.
// Mark all the flash the same until instructed otherwise.
MPU->RBAR = ARM_MPU_RBAR(11, 0x08000000U);
2020-06-09 18:01:52 -04:00
MPU->RASR = ARM_MPU_RASR(EXECUTION, ARM_MPU_AP_FULL, NORMAL, NOT_SHAREABLE, CACHEABLE, BUFFERABLE, NO_SUBREGIONS, CPY_FLASH_REGION_SIZE);
// 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);
2020-06-09 18:01:52 -04:00
MPU->RASR = ARM_MPU_RASR(EXECUTION, ARM_MPU_AP_RO, NORMAL, NOT_SHAREABLE, CACHEABLE, BUFFERABLE, NO_SUBREGIONS, CPY_ITCM_REGION_SIZE);
// This the DTCM.
MPU->RBAR = ARM_MPU_RBAR(14, 0x20000000U);
2020-06-09 18:01:52 -04:00
MPU->RASR = ARM_MPU_RASR(EXECUTION, ARM_MPU_AP_FULL, NORMAL, NOT_SHAREABLE, CACHEABLE, BUFFERABLE, NO_SUBREGIONS, CPY_DTCM_REGION_SIZE);
// This is AXI SRAM (D1).
2020-06-09 18:01:52 -04:00
MPU->RBAR = ARM_MPU_RBAR(15, CPY_SRAM_START_ADDR);
MPU->RASR = ARM_MPU_RASR(EXECUTION, ARM_MPU_AP_FULL, NORMAL, NOT_SHAREABLE, CACHEABLE, BUFFERABLE, CPY_SRAM_SUBMASK, CPY_SRAM_REGION_SIZE);
/* Enable MPU */
ARM_MPU_Enable(MPU_CTRL_PRIVDEFENA_Msk);
2019-09-12 13:47:01 -04: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++) {
(&_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++) {
(&_ld_dtcm_bss_start)[i] = 0;
}
2019-07-18 17:55:57 -04:00
// Copy all of the data to run from D1 RAM.
2021-03-15 09:57:36 -04:00
for (uint32_t i = 0; i < ((size_t)&_ld_d1_ram_data_size) / 4; i++) {
(&_ld_d1_ram_data_destination)[i] = (&_ld_d1_ram_data_flash_copy)[i];
}
// Clear D1 RAM bss.
2021-03-15 09:57:36 -04:00
for (uint32_t i = 0; i < ((size_t)&_ld_d1_ram_bss_size) / 4; i++) {
(&_ld_d1_ram_bss_start)[i] = 0;
}
SystemInit();
__enable_irq();
main();
}
2021-03-15 09:57:36 -04:00
#endif // end H7 specific code
2019-07-18 17:55:57 -04:00
2020-06-05 11:42:34 -04:00
// Low power clock variables
static volatile uint32_t systick_ms;
2020-03-23 17:46:25 -04:00
2019-06-28 15:36:08 -04:00
safe_mode_t port_init(void) {
2020-06-05 11:42:34 -04:00
HAL_Init(); // Turns on SysTick
2019-09-18 16:49:15 -04:00
__HAL_RCC_SYSCFG_CLK_ENABLE();
2019-06-28 15:36:08 -04:00
#if (CPY_STM32F4)
2021-03-15 09:57:36 -04:00
__HAL_RCC_PWR_CLK_ENABLE();
#endif
2019-06-28 15:36:08 -04:00
__HAL_RCC_BACKUPRESET_FORCE();
__HAL_RCC_BACKUPRESET_RELEASE();
stm32_peripherals_clocks_init();
stm32_peripherals_gpio_init();
stm32_peripherals_rtc_init();
__HAL_PWR_CLEAR_FLAG(PWR_FLAG_WU);
stm32_peripherals_reset_alarms();
2020-06-05 11:42:34 -04:00
// Turn off SysTick
SysTick->CTRL = 0;
2019-06-28 15:36:08 -04:00
return NO_SAFE_MODE;
}
2020-06-05 11:42:34 -04:00
void HAL_Delay(uint32_t delay_ms) {
if (SysTick->CTRL != 0) {
// SysTick is on, so use it
uint32_t tickstart = systick_ms;
while (systick_ms - tickstart < delay_ms) {
}
} else {
mp_hal_delay_ms(delay_ms);
}
}
uint32_t HAL_GetTick() {
if (SysTick->CTRL != 0) {
return systick_ms;
} else {
uint8_t subticks;
uint32_t result = (uint32_t)port_get_raw_ticks(&subticks);
return result;
}
}
void SysTick_Handler(void) {
2020-06-05 11:42:34 -04:00
systick_ms += 1;
// Read the CTRL register to clear the SysTick interrupt.
SysTick->CTRL;
}
2019-06-28 15:36:08 -04:00
void reset_port(void) {
2019-11-12 11:26:14 -05:00
reset_all_pins();
2021-03-15 09:57:36 -04:00
#if CIRCUITPY_BUSIO
i2c_reset();
2019-09-27 17:59:55 -04:00
spi_reset();
2019-10-04 14:37:18 -04:00
uart_reset();
2021-03-15 09:57:36 -04:00
#endif
#if CIRCUITPY_SDIOIO
sdioio_reset();
2021-03-15 09:57:36 -04:00
#endif
#if CIRCUITPY_PULSEIO || CIRCUITPY_PWMIO
timers_reset();
2021-03-15 09:57:36 -04:00
#endif
#if CIRCUITPY_PULSEIO
pulseout_reset();
2020-03-10 17:16:31 -04:00
pulsein_reset();
2021-03-15 09:57:36 -04:00
#endif
#if CIRCUITPY_PWMIO
pwmout_reset();
2021-03-15 09:57:36 -04:00
#endif
#if CIRCUITPY_PULSEIO || CIRCUITPY_ALARM
exti_reset();
#endif
// TEMP: set up interrupt logging pins
GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Pin = GPIO_PIN_6 | GPIO_PIN_2 | GPIO_PIN_3 | GPIO_PIN_7;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
GPIO_InitStruct.Pin = GPIO_PIN_8 | GPIO_PIN_9;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
// TEMP: ping port init
HAL_GPIO_WritePin(GPIOB,GPIO_PIN_8,1);
HAL_GPIO_WritePin(GPIOB,GPIO_PIN_8,0);
2019-06-28 15:36:08 -04:00
}
void reset_to_bootloader(void) {
Add some NORETURN attributes I have a function where it should be impossible to reach the end, so I put in a safe-mode reset at the bottom: ``` int find_unused_slot(void) { // precondition: you already verified that a slot was available for (int i=0; i<NUM_SLOTS; i++) { if( slot_free(i)) { return i; } } safe_mode_reset(MICROPY_FATAL_ERROR); } ``` However, the compiler still gave a diagnostic, because safe_mode_reset was not declared NORETURN. So I started by teaching the compiler that reset_into_safe_mode never returned. This leads at least one level deeper due to reset_cpu needing to be a NORETURN function. Each port is a little different in this area. I also marked reset_to_bootloader as NORETURN. Additional notes: * stm32's reset_to_bootloader was not implemented, but now does a bare reset. Most stm32s are not fitted with uf2 bootloaders anyway. * ditto cxd56 * esp32s2 did not implement reset_cpu at all. I used esp_restart(). (not tested) * litex did not implement reset_cpu at all. I used reboot_ctrl_write. But notably this is what reset_to_bootloader already did, so one or the other must be incorrect (not tested). reboot_ctrl_write cannot be declared NORETURN, as it returns unless the special value 0xac is written), so a new unreachable forever-loop is added. * cxd56's reset is via a boardctl() call which can't generically be declared NORETURN, so a new unreacahble "for(;;)" forever-loop is added. * In several places, NVIC_SystemReset is redeclared with NORETURN applied. This is accepted just fine by gcc. I chose this as preferable to editing the multiple copies of CMSIS headers where it is normally declared. * the stub safe_mode reset simply aborts. This is used in mpy-cross.
2020-09-24 12:20:32 -04:00
NVIC_SystemReset();
2019-06-28 15:36:08 -04:00
}
void reset_cpu(void) {
2019-11-12 11:26:14 -05:00
NVIC_SystemReset();
2019-06-28 15:36:08 -04:00
}
extern uint32_t _ld_heap_start, _ld_heap_end, _ld_stack_top, _ld_stack_bottom;
uint32_t *port_heap_get_bottom(void) {
return &_ld_heap_start;
}
uint32_t *port_heap_get_top(void) {
return &_ld_heap_end;
}
bool port_has_fixed_stack(void) {
return false;
2020-05-15 19:22:33 -04:00
}
uint32_t *port_stack_get_limit(void) {
return &_ld_stack_bottom;
}
uint32_t *port_stack_get_top(void) {
return &_ld_stack_top;
}
2019-06-28 15:36:08 -04:00
extern uint32_t _ebss;
2020-04-15 10:18:09 -04:00
2019-06-28 15:36:08 -04:00
// Place the word to save just after our BSS section that gets blanked.
void port_set_saved_word(uint32_t value) {
_ebss = value;
}
uint32_t port_get_saved_word(void) {
return _ebss;
}
2021-03-15 09:57:36 -04:00
__attribute__((used)) void MemManage_Handler(void) {
reset_into_safe_mode(MEM_MANAGE);
while (true) {
2021-03-15 09:57:36 -04:00
asm ("nop;");
}
}
2021-03-15 09:57:36 -04:00
__attribute__((used)) void BusFault_Handler(void) {
reset_into_safe_mode(MEM_MANAGE);
while (true) {
2021-03-15 09:57:36 -04:00
asm ("nop;");
}
}
2021-03-15 09:57:36 -04:00
__attribute__((used)) void UsageFault_Handler(void) {
reset_into_safe_mode(MEM_MANAGE);
while (true) {
2021-03-15 09:57:36 -04:00
asm ("nop;");
}
}
2021-03-15 09:57:36 -04:00
__attribute__((used)) void HardFault_Handler(void) {
2019-11-12 11:26:14 -05:00
reset_into_safe_mode(HARD_CRASH);
2019-09-12 13:47:01 -04:00
while (true) {
2021-03-15 09:57:36 -04:00
asm ("nop;");
2019-09-12 13:47:01 -04:00
}
2019-08-14 13:14:42 -04:00
}
uint64_t port_get_raw_ticks(uint8_t* subticks) {
return stm32_peripherals_rtc_raw_ticks(subticks);
}
// Enable 1/1024 second tick.
void port_enable_tick(void) {
stm32_peripherals_rtc_set_wakeup_mode_tick();
stm32_peripherals_rtc_assign_wkup_callback(supervisor_tick);
stm32_peripherals_rtc_enable_wakeup_timer();
}
// TODO: what is this? can I get rid of it?
extern volatile uint32_t autoreload_delay_ms;
// Disable 1/1024 second tick.
void port_disable_tick(void) {
stm32_peripherals_rtc_disable_wakeup_timer();
}
void port_interrupt_after_ticks(uint32_t ticks) {
stm32_peripherals_rtc_set_alarm(PERIPHERALS_ALARM_A, ticks);
}
void port_idle_until_interrupt(void) {
// 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();
}
// The alarm might have triggered before we even reach the WFI
if (stm32_peripherals_rtc_alarm_triggered(PERIPHERALS_ALARM_A)) {
return;
}
__WFI();
}
2020-04-16 14:53:52 -04:00
// Required by __libc_init_array in startup code if we are compiling using
// -nostdlib/-nostartfiles.
2021-03-15 09:57:36 -04:00
void _init(void) {
2020-04-16 14:53:52 -04:00
}
2021-03-27 13:16:12 -04:00
#if CIRCUITPY_ALARM
// in case boards/xxx/board.c does not provide board_deinit()
MP_WEAK void board_deinit(void) {
}
#endif