Debian Codegraph @

ports/stm32/boards/OPENMV_N6/board.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-2025 Damien P. George
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 "py/mphal.h"
28 #include "boardctrl.h"
29 #include "xspi.h"
30
31 // Values for OTP fuses for VDDIO2/3, to select low voltage mode (<2.5V).
32 // See RM0486, Section 5, Table 18.
33 #define BSEC_HW_CONFIG_ID (124U)
34 #define BSEC_HWS_HSLV_VDDIO3 (1U << 15)
35 #define BSEC_HWS_HSLV_VDDIO2 (1U << 16)
36
37 #define OMV_BOOT_MAGIC_ADDR (0x3401FFFCU)
38 #define OMV_BOOT_MAGIC_VALUE (0xB00710ADU)
39
40 void mboot_board_early_init(void) {
41 // TODO: move some of the below code to a common location for all N6 boards?
42
43 // Enable PWR, BSEC and SYSCFG clocks.
44 LL_AHB4_GRP1_EnableClock(LL_AHB4_GRP1_PERIPH_PWR);
45 LL_APB4_GRP2_EnableClock(LL_APB4_GRP2_PERIPH_BSEC);
46 LL_APB4_GRP2_EnableClock(LL_APB4_GRP2_PERIPH_SYSCFG);
47
48 // Program high speed IO optimization fuses if they aren't already set.
49 uint32_t fuse;
50 BSEC_HandleTypeDef hbsec = { .Instance = BSEC };
51 const uint32_t mask = BSEC_HWS_HSLV_VDDIO2 | BSEC_HWS_HSLV_VDDIO3;
52 if (HAL_BSEC_OTP_Read(&hbsec, BSEC_HW_CONFIG_ID, &fuse) != HAL_OK) {
53 fuse = 0;
54 } else if ((fuse & mask) != mask) {
55 // Program the fuse, and read back the set value.
56 if (HAL_BSEC_OTP_Program(&hbsec, BSEC_HW_CONFIG_ID, fuse | mask, HAL_BSEC_NORMAL_PROG) != HAL_OK) {
57 fuse = 0;
58 } else if (HAL_BSEC_OTP_Read(&hbsec, BSEC_HW_CONFIG_ID, &fuse) != HAL_OK) {
59 fuse = 0;
60 }
61 }
62
63 // Enable Vdd ADC, needed for the ADC to work.
64 LL_PWR_EnableVddADC();
65
66 // Configure VDDIO2. Only enable 1.8V mode if the fuse is set.
67 LL_PWR_EnableVddIO2();
68 if (fuse & BSEC_HWS_HSLV_VDDIO2) {
69 LL_PWR_SetVddIO2VoltageRange(LL_PWR_VDDIO_VOLTAGE_RANGE_1V8);
70 }
71 SYSCFG->VDDIO2CCCR |= SYSCFG_VDDIO2CCCR_EN; // enable IO compensation
72
73 // Configure VDDIO3. Only enable 1.8V mode if the fuse is set.
74 LL_PWR_EnableVddIO3();
75 if (fuse & BSEC_HWS_HSLV_VDDIO3) {
76 LL_PWR_SetVddIO3VoltageRange(LL_PWR_VDDIO_VOLTAGE_RANGE_1V8);
77 }
78 SYSCFG->VDDIO3CCCR |= SYSCFG_VDDIO3CCCR_EN; // enable IO compensation
79
80 // Configure VDDIO4.
81 LL_PWR_EnableVddIO4();
82 LL_PWR_SetVddIO4VoltageRange(LL_PWR_VDDIO_VOLTAGE_RANGE_3V3);
83 SYSCFG->VDDIO4CCCR |= SYSCFG_VDDIO4CCCR_EN; // enable IO compensation
84
85 // Enable VDD for ADC and USB.
86 LL_PWR_EnableVddADC();
87 LL_PWR_EnableVddUSB();
88
89 // Enable XSPI in memory-mapped mode.
90 xspi_init();
91 }
92
93 void board_enter_bootloader(unsigned int n_args, const void *args) {
94 // Support both OpenMV bootloader and mboot.
95 *((uint32_t *)OMV_BOOT_MAGIC_ADDR) = OMV_BOOT_MAGIC_VALUE;
96 SCB_CleanDCache();
97 boardctrl_maybe_enter_mboot(n_args, args);
98 }
99
100 void board_early_init(void) {
101 // TODO: if (HAL_PWREx_ConfigSupply(PWR_EXTERNAL_SOURCE_SUPPLY ) != HAL_OK)
102
103 LL_PWR_EnableWakeUpPin(LL_PWR_WAKEUP_PIN3 | LL_PWR_WAKEUP_PIN2);
104 LL_PWR_SetWakeUpPinPolarityLow(LL_PWR_WAKEUP_PIN3 | LL_PWR_WAKEUP_PIN2);
105 }
106
107 void board_leave_standby(void) {
108 // TODO: move some of the below code to a common location for all N6 boards?
109
110 // Enable PWR, BSEC and SYSCFG clocks.
111 LL_AHB4_GRP1_EnableClock(LL_AHB4_GRP1_PERIPH_PWR);
112 LL_APB4_GRP2_EnableClock(LL_APB4_GRP2_PERIPH_BSEC);
113 LL_APB4_GRP2_EnableClock(LL_APB4_GRP2_PERIPH_SYSCFG);
114
115 // Configure VDDIO2 (1.8V mode selection is retained).
116 LL_PWR_EnableVddIO2();
117 SYSCFG->VDDIO2CCCR |= SYSCFG_VDDIO2CCCR_EN; // enable IO compensation
118
119 // Configure VDDIO3 (1.8V mode selection is retained).
120 LL_PWR_EnableVddIO3();
121 SYSCFG->VDDIO3CCCR |= SYSCFG_VDDIO3CCCR_EN; // enable IO compensation
122
123 // Configure VDDIO4.
124 LL_PWR_EnableVddIO4();
125 LL_PWR_SetVddIO4VoltageRange(LL_PWR_VDDIO_VOLTAGE_RANGE_3V3);
126 SYSCFG->VDDIO4CCCR |= SYSCFG_VDDIO4CCCR_EN; // enable IO compensation
127
128 // Enable VDD for ADC and USB.
129 LL_PWR_EnableVddADC();
130 LL_PWR_EnableVddUSB();
131 }