Debian Codegraph @

ports/stm32/boards/ARDUINO_OPTA/board_init.c

raw
1 /*
2 * This file is part of the MicroPython project, http://micropython.org/
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (c) 2024 Arduino SA
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 */
26
27 #include <string.h>
28 #include "py/mphal.h"
29 #include "storage.h"
30 #include "sdram.h"
31
32 #define BOOTLOADER_MAGIC (0xDF59)
33
34 void OPTA_board_startup(void) {
35 }
36
37 void OPTA_board_early_init(void) {
38 HAL_InitTick(TICK_INT_PRIORITY);
39
40 // Enable oscillator pin
41 // This is enabled in the bootloader anyway.
42 OPTA_board_osc_enable(true);
43
44 #if MICROPY_HW_ENABLE_INTERNAL_FLASH_STORAGE
45 // The Arduino/mbed bootloader uses the MPU to protect sector 1
46 // which is used for the flash filesystem. The following code
47 // resets and disables all MPU regions configured in the bootloader.
48 HAL_MPU_Disable();
49 MPU_Region_InitTypeDef MPU_InitStruct;
50 MPU_InitStruct.AccessPermission = MPU_REGION_FULL_ACCESS;
51 MPU_InitStruct.IsBufferable = MPU_ACCESS_NOT_BUFFERABLE;
52 MPU_InitStruct.IsCacheable = MPU_ACCESS_NOT_CACHEABLE;
53 MPU_InitStruct.IsShareable = MPU_ACCESS_NOT_SHAREABLE;
54 MPU_InitStruct.TypeExtField = MPU_TEX_LEVEL1;
55 MPU_InitStruct.SubRegionDisable = 0x00;
56 MPU_InitStruct.DisableExec = MPU_INSTRUCTION_ACCESS_ENABLE;
57
58 for (int i = MPU_REGION_NUMBER0; i < MPU_REGION_NUMBER15; i++) {
59 MPU_InitStruct.Number = i;
60 MPU_InitStruct.Enable = MPU_REGION_DISABLE;
61 HAL_MPU_ConfigRegion(&MPU_InitStruct);
62 }
63 #endif
64 }
65
66 void OPTA_board_enter_bootloader(void) {
67 RTC_HandleTypeDef RTCHandle;
68 RTCHandle.Instance = RTC;
69 HAL_RTCEx_BKUPWrite(&RTCHandle, RTC_BKP_DR0, BOOTLOADER_MAGIC);
70 NVIC_SystemReset();
71 }
72
73 void OPTA_board_osc_enable(int enable) {
74 mp_hal_pin_config(pyb_pin_OSCEN, MP_HAL_PIN_MODE_OUTPUT, MP_HAL_PIN_PULL_UP, 0);
75 mp_hal_pin_config_speed(pyb_pin_OSCEN, MP_HAL_PIN_SPEED_LOW);
76 mp_hal_pin_write(pyb_pin_OSCEN, enable);
77 }
78
79 void OPTA_board_low_power(int mode) {
80
81 #if !MICROPY_HW_ENABLE_INTERNAL_FLASH_STORAGE
82 // Enable QSPI deepsleep for modes 1 and 2
83 mp_spiflash_deepsleep(&spi_bdev.spiflash, (mode != 0));
84 #endif
85
86 #if defined(M4_APP_ADDR)
87 // Signal Cortex-M4 to go to Standby mode.
88 if (mode == 2) {
89 __HAL_RCC_HSEM_CLK_ENABLE();
90 HAL_HSEM_FastTake(0);
91 HAL_HSEM_Release(0, 0);
92 __HAL_RCC_HSEM_CLK_DISABLE();
93 HAL_Delay(100);
94 }
95 #endif
96 }