| 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 "ulpi.h" |
| 31 |
|
| 32 |
void NICLAV_board_startup(void) { |
| 33 |
} |
| 34 |
|
| 35 |
void NICLAV_board_pmic_enable(int); |
| 36 |
|
| 37 |
void NICLAV_board_early_init(void) { |
| 38 |
HAL_InitTick(0); |
| 39 |
|
| 40 |
// Enable oscillator pin |
| 41 |
// This is enabled in the bootloader anyway. |
| 42 |
NICLAV_board_osc_enable(true); |
| 43 |
|
| 44 |
// Re/Enable PMIC rails. |
| 45 |
NICLAV_board_pmic_enable(true); |
| 46 |
|
| 47 |
#if MICROPY_HW_ENABLE_INTERNAL_FLASH_STORAGE |
| 48 |
// The Arduino/mbed bootloader uses the MPU to protect sector 1 |
| 49 |
// which is used for the flash filesystem. The following code |
| 50 |
// resets and disables all MPU regions configured in the bootloader. |
| 51 |
HAL_MPU_Disable(); |
| 52 |
MPU_Region_InitTypeDef MPU_InitStruct; |
| 53 |
MPU_InitStruct.AccessPermission = MPU_REGION_FULL_ACCESS; |
| 54 |
MPU_InitStruct.IsBufferable = MPU_ACCESS_NOT_BUFFERABLE; |
| 55 |
MPU_InitStruct.IsCacheable = MPU_ACCESS_NOT_CACHEABLE; |
| 56 |
MPU_InitStruct.IsShareable = MPU_ACCESS_NOT_SHAREABLE; |
| 57 |
MPU_InitStruct.TypeExtField = MPU_TEX_LEVEL1; |
| 58 |
MPU_InitStruct.SubRegionDisable = 0x00; |
| 59 |
MPU_InitStruct.DisableExec = MPU_INSTRUCTION_ACCESS_ENABLE; |
| 60 |
|
| 61 |
for (int i = MPU_REGION_NUMBER0; i < MPU_REGION_NUMBER15; i++) { |
| 62 |
MPU_InitStruct.Number = i; |
| 63 |
MPU_InitStruct.Enable = MPU_REGION_DISABLE; |
| 64 |
HAL_MPU_ConfigRegion(&MPU_InitStruct); |
| 65 |
} |
| 66 |
#endif |
| 67 |
|
| 68 |
#if MICROPY_HW_USB_HS_ULPI3320 |
| 69 |
// Make sure UPLI is Not in low-power mode. |
| 70 |
ulpi_leave_low_power(); |
| 71 |
#endif |
| 72 |
} |
| 73 |
|
| 74 |
void NICLAV_board_enter_bootloader(void) { |
| 75 |
RTC_HandleTypeDef RTCHandle; |
| 76 |
RTCHandle.Instance = RTC; |
| 77 |
HAL_RTCEx_BKUPWrite(&RTCHandle, RTC_BKP_DR0, 0xDF59); |
| 78 |
NVIC_SystemReset(); |
| 79 |
} |
| 80 |
|
| 81 |
void NICLAV_board_osc_enable(int enable) { |
| 82 |
mp_hal_pin_config(pyb_pin_OSCEN, MP_HAL_PIN_MODE_OUTPUT, MP_HAL_PIN_PULL_UP, 0); |
| 83 |
mp_hal_pin_config_speed(pyb_pin_OSCEN, MP_HAL_PIN_SPEED_LOW); |
| 84 |
mp_hal_pin_write(pyb_pin_OSCEN, enable); |
| 85 |
} |
| 86 |
|
| 87 |
void NICLAV_board_pmic_enable(int enable) { |
| 88 |
__HAL_RCC_GPIOF_CLK_ENABLE(); |
| 89 |
|
| 90 |
// Configure PMIC I2C GPIOs |
| 91 |
GPIO_InitTypeDef GPIO_InitStructure; |
| 92 |
GPIO_InitStructure.Pin = GPIO_PIN_0 | GPIO_PIN_1; |
| 93 |
GPIO_InitStructure.Pull = GPIO_PULLUP; |
| 94 |
GPIO_InitStructure.Speed = GPIO_SPEED_LOW; |
| 95 |
GPIO_InitStructure.Mode = GPIO_MODE_AF_OD; |
| 96 |
GPIO_InitStructure.Alternate = GPIO_AF4_I2C2; |
| 97 |
HAL_GPIO_Init(GPIOF, &GPIO_InitStructure); |
| 98 |
|
| 99 |
// Configure PMIC I2C |
| 100 |
I2C_HandleTypeDef i2c; |
| 101 |
i2c.Instance = I2C2; |
| 102 |
i2c.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT; |
| 103 |
i2c.Init.Timing = 0x20D09DE7; |
| 104 |
i2c.Init.DualAddressMode = I2C_DUALADDRESS_DISABLED; |
| 105 |
i2c.Init.GeneralCallMode = I2C_GENERALCALL_DISABLED; |
| 106 |
i2c.Init.NoStretchMode = I2C_NOSTRETCH_DISABLED; |
| 107 |
i2c.Init.OwnAddress1 = 0xFE; |
| 108 |
i2c.Init.OwnAddress2 = 0xFE; |
| 109 |
i2c.Init.OwnAddress2Masks = 0; |
| 110 |
|
| 111 |
__HAL_RCC_I2C2_CLK_ENABLE(); |
| 112 |
__HAL_RCC_I2C2_FORCE_RESET(); |
| 113 |
__HAL_RCC_I2C2_RELEASE_RESET(); |
| 114 |
HAL_I2C_Init(&i2c); |
| 115 |
|
| 116 |
// LDO1 2V8 |
| 117 |
HAL_I2C_Master_Transmit(&i2c, 0x08 << 1, (uint8_t [2]) {0x4D, (enable) ? 0x01 : 0x00}, 2, 1000); |
| 118 |
// LDO2 1V8 CAM |
| 119 |
HAL_I2C_Master_Transmit(&i2c, 0x08 << 1, (uint8_t [2]) {0x50, (enable) ? 0x01 : 0x00}, 2, 1000); |
| 120 |
// LDO3 1V8 |
| 121 |
HAL_I2C_Master_Transmit(&i2c, 0x08 << 1, (uint8_t [2]) {0x53, (enable) ? 0x01 : 0x00}, 2, 1000); |
| 122 |
// SW2 VDDIO_EXT |
| 123 |
HAL_I2C_Master_Transmit(&i2c, 0x08 << 1, (uint8_t [2]) {0x3b, (enable) ? 0x81 : 0x80}, 2, 1000); |
| 124 |
// SW3 3V3 (Leaving 3.3 on lowers ULPI current). |
| 125 |
// HAL_I2C_Master_Transmit(&i2c, 0x08<<1, (uint8_t [2]) {0x41, (enable) ? 0x81 : 0x80}, 2, 1000); |
| 126 |
// SW1 1V8 (Main supply is Never turned off) |
| 127 |
// HAL_I2C_Master_Transmit(&i2c, 0x08<<1, (uint8_t [2]) {0x35, (enable) ? 0x81 : 0x80}, 2, 1000); |
| 128 |
|
| 129 |
HAL_I2C_DeInit(&i2c); |
| 130 |
__HAL_RCC_I2C2_FORCE_RESET(); |
| 131 |
__HAL_RCC_I2C2_RELEASE_RESET(); |
| 132 |
__HAL_RCC_I2C2_CLK_DISABLE(); |
| 133 |
|
| 134 |
HAL_GPIO_DeInit(GPIOF, GPIO_PIN_0); |
| 135 |
HAL_GPIO_DeInit(GPIOF, GPIO_PIN_1); |
| 136 |
__HAL_RCC_GPIOF_CLK_DISABLE(); |
| 137 |
} |
| 138 |
|
| 139 |
static __attribute__((naked, noreturn)) void cm4_enter_standby(void) { |
| 140 |
// Clear and mask D1 EXTIs. |
| 141 |
EXTI_D1->PR1 = 0x3fffff; |
| 142 |
EXTI_D1->IMR1 &= ~(0xFFFFu); // 16 lines |
| 143 |
|
| 144 |
// Clear and mask D2 EXTIs. |
| 145 |
EXTI_D2->IMR1 &= ~(0xFFFFu); // 16 lines |
| 146 |
EXTI_D2->PR1 = 0x3fffff; |
| 147 |
|
| 148 |
EXTI->D3PMR1 = 0;///0x0238FFFFu; |
| 149 |
EXTI->D3PMR2 = 0;///0x003F020Cu; |
| 150 |
EXTI->D3PMR3 = 0;///0x01000000u; |
| 151 |
|
| 152 |
// Set D2/D2 PDDS bits. |
| 153 |
PWR->CPUCR |= (PWR_CPUCR_PDDS_D2 | PWR_CPUCR_PDDS_D3); |
| 154 |
PWR->CPU2CR |= (PWR_CPU2CR_PDDS_D2 | PWR_CPU2CR_PDDS_D3); |
| 155 |
CLEAR_BIT(PWR->CPU2CR, PWR_CPU2CR_RUN_D3); |
| 156 |
|
| 157 |
// Set SLEEPDEEP bit. |
| 158 |
SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk; |
| 159 |
|
| 160 |
// Enter CPU stop mode. |
| 161 |
__WFI(); |
| 162 |
} |
| 163 |
|
| 164 |
void NICLAV_board_low_power(int mode) { |
| 165 |
switch (mode) { |
| 166 |
case 0: // Leave stop mode. |
| 167 |
ulpi_leave_low_power(); |
| 168 |
break; |
| 169 |
case 1: // Enter stop mode. |
| 170 |
ulpi_enter_low_power(); |
| 171 |
break; |
| 172 |
case 2: // Enter standby mode. |
| 173 |
ulpi_enter_low_power(); |
| 174 |
break; |
| 175 |
} |
| 176 |
|
| 177 |
#if (MICROPY_HW_ENABLE_INTERNAL_FLASH_STORAGE == 0) |
| 178 |
// Enable QSPI deepsleep for modes 1 and 2 |
| 179 |
mp_spiflash_deepsleep(&spi_bdev.spiflash, (mode != 0)); |
| 180 |
#endif |
| 181 |
|
| 182 |
if (mode == 2) { |
| 183 |
if (RCC->GCR & RCC_GCR_BOOT_C2) { |
| 184 |
// If CM4 is booted, it should be running a firmware |
| 185 |
// that supports low-power mode entry via HSEM. |
| 186 |
__HAL_RCC_HSEM_CLK_ENABLE(); |
| 187 |
HAL_HSEM_FastTake(0); |
| 188 |
HAL_HSEM_Release(0, 0); |
| 189 |
} else { |
| 190 |
// If CM4 is not booted, rig an ISR vector, with a reset |
| 191 |
// handler that points to standby function and boot it. |
| 192 |
volatile extern char _cm4_ram_start[1024]; |
| 193 |
|
| 194 |
uint32_t *cm4_ram = (uint32_t *)_cm4_ram_start; |
| 195 |
cm4_ram[0] = (uint32_t)(_cm4_ram_start + 1024); |
| 196 |
cm4_ram[1] = ((uint32_t)&cm4_enter_standby) | 1; |
| 197 |
SCB_CleanDCache_by_Addr((uint32_t *)_cm4_ram_start, 8); |
| 198 |
|
| 199 |
HAL_PWREx_HoldCore(PWR_CORE_CPU2); |
| 200 |
HAL_SYSCFG_CM4BootAddConfig(SYSCFG_BOOT_ADDR0, (uint32_t)_cm4_ram_start); |
| 201 |
HAL_RCCEx_EnableBootCore(RCC_BOOT_C2); |
| 202 |
} |
| 203 |
|
| 204 |
// Wait for the CM4 to enter stop mode. |
| 205 |
HAL_Delay(100); |
| 206 |
|
| 207 |
// Disable all power rails, except core voltage. |
| 208 |
NICLAV_board_pmic_enable(false); |
| 209 |
|
| 210 |
// Reset all buses, peripherals, GPIO clocks etc.. |
| 211 |
RCC->AHB1RSTR = 0x0A00C023U; |
| 212 |
RCC->AHB2RSTR = 0x00000271U; |
| 213 |
RCC->AHB3RSTR = 0x00015031U; |
| 214 |
RCC->AHB4RSTR = 0x0328077FU; // LSE GPIO port is masked. |
| 215 |
|
| 216 |
RCC->APB1LRSTR = 0xE8FFC3FFU; |
| 217 |
RCC->APB1HRSTR = 0x00000136U; |
| 218 |
RCC->APB2RSTR = 0x31D73033U; |
| 219 |
RCC->APB3RSTR = 0x00000018U; |
| 220 |
RCC->APB4RSTR = 0x0020DEAAU; |
| 221 |
} |
| 222 |
} |