| 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 VDDIO3, 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 |
|
| 36 |
static void board_config_vdd(void) { |
| 37 |
// TODO: move some of the below code to a common location for all N6 boards? |
| 38 |
|
| 39 |
// Enable PWR, BSEC and SYSCFG clocks. |
| 40 |
LL_AHB4_GRP1_EnableClock(LL_AHB4_GRP1_PERIPH_PWR); |
| 41 |
LL_APB4_GRP2_EnableClock(LL_APB4_GRP2_PERIPH_BSEC); |
| 42 |
LL_APB4_GRP2_EnableClock(LL_APB4_GRP2_PERIPH_SYSCFG); |
| 43 |
|
| 44 |
// Program high speed IO optimization fuses if they aren't already set. |
| 45 |
uint32_t fuse; |
| 46 |
BSEC_HandleTypeDef hbsec = { .Instance = BSEC }; |
| 47 |
const uint32_t mask = BSEC_HWS_HSLV_VDDIO3; |
| 48 |
if (HAL_BSEC_OTP_Read(&hbsec, BSEC_HW_CONFIG_ID, &fuse) != HAL_OK) { |
| 49 |
fuse = 0; |
| 50 |
} else if ((fuse & mask) != mask) { |
| 51 |
// Program the fuse, and read back the set value. |
| 52 |
if (HAL_BSEC_OTP_Program(&hbsec, BSEC_HW_CONFIG_ID, fuse | mask, HAL_BSEC_NORMAL_PROG) != HAL_OK) { |
| 53 |
fuse = 0; |
| 54 |
} else if (HAL_BSEC_OTP_Read(&hbsec, BSEC_HW_CONFIG_ID, &fuse) != HAL_OK) { |
| 55 |
fuse = 0; |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
// Enable Vdd ADC, needed for the ADC to work. |
| 60 |
LL_PWR_EnableVddADC(); |
| 61 |
|
| 62 |
// Configure VDDIO2. |
| 63 |
LL_PWR_EnableVddIO2(); |
| 64 |
LL_PWR_SetVddIO2VoltageRange(LL_PWR_VDDIO_VOLTAGE_RANGE_3V3); |
| 65 |
SYSCFG->VDDIO2CCCR |= SYSCFG_VDDIO2CCCR_EN; // enable IO compensation |
| 66 |
|
| 67 |
// Configure VDDIO3. Only enable 1.8V mode if the fuse is set. |
| 68 |
LL_PWR_EnableVddIO3(); |
| 69 |
if (fuse & BSEC_HWS_HSLV_VDDIO3) { |
| 70 |
LL_PWR_SetVddIO3VoltageRange(LL_PWR_VDDIO_VOLTAGE_RANGE_1V8); |
| 71 |
} |
| 72 |
SYSCFG->VDDIO3CCCR |= SYSCFG_VDDIO3CCCR_EN; // enable IO compensation |
| 73 |
|
| 74 |
// Configure VDDIO4. |
| 75 |
LL_PWR_EnableVddIO4(); |
| 76 |
LL_PWR_SetVddIO4VoltageRange(LL_PWR_VDDIO_VOLTAGE_RANGE_3V3); |
| 77 |
SYSCFG->VDDIO4CCCR |= SYSCFG_VDDIO4CCCR_EN; // enable IO compensation |
| 78 |
|
| 79 |
// Enable VDD for ADC and USB. |
| 80 |
LL_PWR_EnableVddADC(); |
| 81 |
LL_PWR_EnableVddUSB(); |
| 82 |
} |
| 83 |
|
| 84 |
void mboot_board_early_init(void) { |
| 85 |
board_config_vdd(); |
| 86 |
xspi_init(); |
| 87 |
} |
| 88 |
|
| 89 |
void board_early_init(void) { |
| 90 |
#if !MICROPY_HW_RUNS_FROM_EXT_FLASH |
| 91 |
// Firmware runs directly from SRAM, so configure VDD and enable XSPI flash. |
| 92 |
board_config_vdd(); |
| 93 |
xspi_init(); |
| 94 |
#endif |
| 95 |
} |
| 96 |
|
| 97 |
void board_leave_standby(void) { |
| 98 |
// TODO: move some of the below code to a common location for all N6 boards? |
| 99 |
|
| 100 |
// Enable PWR, BSEC and SYSCFG clocks. |
| 101 |
LL_AHB4_GRP1_EnableClock(LL_AHB4_GRP1_PERIPH_PWR); |
| 102 |
LL_APB4_GRP2_EnableClock(LL_APB4_GRP2_PERIPH_BSEC); |
| 103 |
LL_APB4_GRP2_EnableClock(LL_APB4_GRP2_PERIPH_SYSCFG); |
| 104 |
|
| 105 |
// Configure VDDIO2. |
| 106 |
LL_PWR_EnableVddIO2(); |
| 107 |
LL_PWR_SetVddIO2VoltageRange(LL_PWR_VDDIO_VOLTAGE_RANGE_3V3); |
| 108 |
SYSCFG->VDDIO2CCCR |= SYSCFG_VDDIO2CCCR_EN; // enable IO compensation |
| 109 |
|
| 110 |
// Configure VDDIO3 (1.8V mode selection is retained). |
| 111 |
LL_PWR_EnableVddIO3(); |
| 112 |
SYSCFG->VDDIO3CCCR |= SYSCFG_VDDIO3CCCR_EN; // enable IO compensation |
| 113 |
|
| 114 |
// Configure VDDIO4. |
| 115 |
LL_PWR_EnableVddIO4(); |
| 116 |
LL_PWR_SetVddIO4VoltageRange(LL_PWR_VDDIO_VOLTAGE_RANGE_3V3); |
| 117 |
SYSCFG->VDDIO4CCCR |= SYSCFG_VDDIO4CCCR_EN; // enable IO compensation |
| 118 |
|
| 119 |
// Enable VDD for ADC and USB. |
| 120 |
LL_PWR_EnableVddADC(); |
| 121 |
LL_PWR_EnableVddUSB(); |
| 122 |
} |