Debian Codegraph @

ports/stm32/boards/ARDUINO_GIGA/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) 2023 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 void GIGA_board_startup(void) {
33 }
34
35 void GIGA_board_early_init(void) {
36 HAL_InitTick(0);
37
38 // The following pins need to be configured at this point to fix an
39 // issue with some boards that have an older QSPI flash part revision.
40 mp_hal_pin_config(pin_D13, MP_HAL_PIN_MODE_ANALOG, MP_HAL_PIN_PULL_NONE, 0);
41 __HAL_RCC_GPIOD_CLK_DISABLE();
42
43 mp_hal_pin_config(pin_F7, MP_HAL_PIN_MODE_ANALOG, MP_HAL_PIN_PULL_NONE, 0);
44 __HAL_RCC_GPIOF_CLK_DISABLE();
45
46 #if MICROPY_HW_ENABLE_INTERNAL_FLASH_STORAGE == 1
47 // The Arduino/mbed bootloader uses the MPU to protect sector 1
48 // which is used for the flash filesystem. The following code
49 // resets and disables all MPU regions configured in the bootloader.
50 HAL_MPU_Disable();
51 MPU_Region_InitTypeDef MPU_InitStruct;
52 MPU_InitStruct.AccessPermission = MPU_REGION_FULL_ACCESS;
53 MPU_InitStruct.IsBufferable = MPU_ACCESS_NOT_BUFFERABLE;
54 MPU_InitStruct.IsCacheable = MPU_ACCESS_NOT_CACHEABLE;
55 MPU_InitStruct.IsShareable = MPU_ACCESS_NOT_SHAREABLE;
56 MPU_InitStruct.TypeExtField = MPU_TEX_LEVEL1;
57 MPU_InitStruct.SubRegionDisable = 0x00;
58 MPU_InitStruct.DisableExec = MPU_INSTRUCTION_ACCESS_ENABLE;
59
60 for (int i = MPU_REGION_NUMBER0; i < MPU_REGION_NUMBER15; i++) {
61 MPU_InitStruct.Number = i;
62 MPU_InitStruct.Enable = MPU_REGION_DISABLE;
63 HAL_MPU_ConfigRegion(&MPU_InitStruct);
64 }
65 #endif
66 }
67
68 void GIGA_board_enter_bootloader(void) {
69 RTC_HandleTypeDef RTCHandle;
70 RTCHandle.Instance = RTC;
71 HAL_RTCEx_BKUPWrite(&RTCHandle, RTC_BKP_DR0, 0xDF59);
72 NVIC_SystemReset();
73 }
74
75 void GIGA_board_low_power(int mode) {
76 switch (mode) {
77 case 0: // Leave stop mode.
78 sdram_leave_low_power();
79 break;
80 case 1: // Enter stop mode.
81 sdram_enter_low_power();
82 break;
83 case 2: // Enter standby mode.
84 sdram_enter_power_down();
85 break;
86 }
87
88 #if MICROPY_HW_ENABLE_INTERNAL_FLASH_STORAGE == 0
89 // Enable QSPI deepsleep for modes 1 and 2
90 mp_spiflash_deepsleep(&spi_bdev.spiflash, (mode != 0));
91 #endif
92
93 #if defined(M4_APP_ADDR)
94 // Signal Cortex-M4 to go to Standby mode.
95 if (mode == 2) {
96 __HAL_RCC_HSEM_CLK_ENABLE();
97 HAL_HSEM_FastTake(0);
98 HAL_HSEM_Release(0, 0);
99 __HAL_RCC_HSEM_CLK_DISABLE();
100 HAL_Delay(100);
101 }
102 #endif
103 }