circuitpython/ports/stm/common-hal/microcontroller/__init__.c

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

114 lines
3.6 KiB
C
Raw Normal View History

2020-03-11 18:13:06 -04:00
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2016 Scott Shawcroft for Adafruit Industries
* Copyright (c) 2019 Lucian Copeland 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 "py/mphal.h"
#include "py/obj.h"
#include "py/runtime.h"
#include "common-hal/microcontroller/Pin.h"
#include "common-hal/microcontroller/Processor.h"
2020-03-22 05:22:59 -04:00
#include "shared-bindings/nvm/ByteArray.h"
2020-03-11 18:13:06 -04:00
#include "shared-bindings/microcontroller/__init__.h"
#include "shared-bindings/microcontroller/Pin.h"
#include "shared-bindings/microcontroller/Processor.h"
#include "supervisor/port.h"
2020-03-11 18:13:06 -04:00
#include "supervisor/filesystem.h"
#include "supervisor/shared/safe_mode.h"
void common_hal_mcu_delay_us(uint32_t delay) {
uint32_t ticks_per_us = HAL_RCC_GetSysClockFreq() / 1000000UL;
2020-03-23 19:09:50 -04:00
delay *= ticks_per_us;
SysTick->VAL = 0UL;
2020-03-23 19:09:50 -04:00
SysTick->LOAD = delay;
SysTick->CTRL = SysTick_CTRL_CLKSOURCE_Msk | SysTick_CTRL_ENABLE_Msk;
2021-03-15 09:57:36 -04:00
while ((SysTick->CTRL & SysTick_CTRL_COUNTFLAG_Msk) == 0) {
}
SysTick->CTRL = 0UL;
2020-03-11 18:13:06 -04:00
}
volatile uint32_t nesting_count = 0;
void common_hal_mcu_disable_interrupts(void) {
__disable_irq();
__DMB();
nesting_count++;
}
void common_hal_mcu_enable_interrupts(void) {
if (nesting_count == 0) {
2023-02-11 23:50:20 -05:00
// This is very very bad because it means there was mismatched disable/enables.
reset_into_safe_mode(SAFE_MODE_INTERRUPT_ERROR);
2020-03-11 18:13:06 -04:00
}
nesting_count--;
if (nesting_count > 0) {
return;
}
__DMB();
__enable_irq();
}
static bool next_reset_to_bootloader = false;
2020-03-11 18:13:06 -04:00
void common_hal_mcu_on_next_reset(mcu_runmode_t runmode) {
2021-03-15 09:57:36 -04:00
if (runmode == RUNMODE_SAFE_MODE) {
2023-02-11 23:50:20 -05:00
safe_mode_on_next_reset(SAFE_MODE_PROGRAMMATIC);
2021-03-15 09:57:36 -04:00
}
if (runmode == RUNMODE_BOOTLOADER) {
next_reset_to_bootloader = true;
}
2020-03-11 18:13:06 -04:00
}
void common_hal_mcu_reset(void) {
2021-03-15 09:57:36 -04:00
filesystem_flush(); // TODO: implement as part of flash improvements
if (next_reset_to_bootloader) {
reset_to_bootloader();
} else {
NVIC_SystemReset();
}
2020-03-11 18:13:06 -04:00
}
// The singleton microcontroller.Processor object, bound to microcontroller.cpu
// It currently only has properties, and no state.
const mcu_processor_obj_t common_hal_mcu_processor_obj = {
.base = {
.type = &mcu_processor_type,
},
};
2020-03-22 05:22:59 -04:00
#if CIRCUITPY_INTERNAL_NVM_SIZE > 0
// The singleton nvm.ByteArray object.
const nvm_bytearray_obj_t common_hal_mcu_nvm_obj = {
.base = {
.type = &nvm_bytearray_type,
},
2020-03-22 18:20:58 -04:00
.len = NVM_BYTEARRAY_BUFFER_SIZE,
2021-03-15 09:57:36 -04:00
.start_address = (uint8_t *)(CIRCUITPY_INTERNAL_NVM_START_ADDR)
2020-03-22 05:22:59 -04:00
};
#endif