|
| 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 | + // Make sure PC2 and PC3 and PC2_C and PC3_C pads are connected |
| 69 | + // through the analog switch for ULPI NXT and DIR pins. |
| 70 | + HAL_SYSCFG_AnalogSwitchConfig(SYSCFG_SWITCH_PC2, SYSCFG_SWITCH_PC2_CLOSE); |
| 71 | + HAL_SYSCFG_AnalogSwitchConfig(SYSCFG_SWITCH_PC3, SYSCFG_SWITCH_PC3_CLOSE); |
| 72 | + |
| 73 | + #if MICROPY_HW_USB_HS_ULPI3320 |
| 74 | + // Make sure UPLI is Not in low-power mode. |
| 75 | + ulpi_leave_low_power(); |
| 76 | + #endif |
| 77 | +} |
| 78 | + |
| 79 | +void NICLAV_board_enter_bootloader(void) { |
| 80 | + RTC_HandleTypeDef RTCHandle; |
| 81 | + RTCHandle.Instance = RTC; |
| 82 | + HAL_RTCEx_BKUPWrite(&RTCHandle, RTC_BKP_DR0, 0xDF59); |
| 83 | + NVIC_SystemReset(); |
| 84 | +} |
| 85 | + |
| 86 | +void NICLAV_board_osc_enable(int enable) { |
| 87 | + mp_hal_pin_config(pyb_pin_OSCEN, MP_HAL_PIN_MODE_OUTPUT, MP_HAL_PIN_PULL_UP, 0); |
| 88 | + mp_hal_pin_config_speed(pyb_pin_OSCEN, MP_HAL_PIN_SPEED_LOW); |
| 89 | + mp_hal_pin_write(pyb_pin_OSCEN, enable); |
| 90 | +} |
| 91 | + |
| 92 | +void NICLAV_board_pmic_enable(int enable) { |
| 93 | + __HAL_RCC_GPIOF_CLK_ENABLE(); |
| 94 | + |
| 95 | + // Configure PMIC I2C GPIOs |
| 96 | + GPIO_InitTypeDef GPIO_InitStructure; |
| 97 | + GPIO_InitStructure.Pin = GPIO_PIN_0 | GPIO_PIN_1; |
| 98 | + GPIO_InitStructure.Pull = GPIO_PULLUP; |
| 99 | + GPIO_InitStructure.Speed = GPIO_SPEED_LOW; |
| 100 | + GPIO_InitStructure.Mode = GPIO_MODE_AF_OD; |
| 101 | + GPIO_InitStructure.Alternate = GPIO_AF4_I2C2; |
| 102 | + HAL_GPIO_Init(GPIOF, &GPIO_InitStructure); |
| 103 | + |
| 104 | + // Configure PMIC I2C |
| 105 | + I2C_HandleTypeDef i2c; |
| 106 | + i2c.Instance = I2C2; |
| 107 | + i2c.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT; |
| 108 | + i2c.Init.Timing = 0x20D09DE7; |
| 109 | + i2c.Init.DualAddressMode = I2C_DUALADDRESS_DISABLED; |
| 110 | + i2c.Init.GeneralCallMode = I2C_GENERALCALL_DISABLED; |
| 111 | + i2c.Init.NoStretchMode = I2C_NOSTRETCH_DISABLED; |
| 112 | + i2c.Init.OwnAddress1 = 0xFE; |
| 113 | + i2c.Init.OwnAddress2 = 0xFE; |
| 114 | + i2c.Init.OwnAddress2Masks = 0; |
| 115 | + |
| 116 | + __HAL_RCC_I2C2_CLK_ENABLE(); |
| 117 | + __HAL_RCC_I2C2_FORCE_RESET(); |
| 118 | + __HAL_RCC_I2C2_RELEASE_RESET(); |
| 119 | + HAL_I2C_Init(&i2c); |
| 120 | + |
| 121 | + // LDO1 2V8 |
| 122 | + HAL_I2C_Master_Transmit(&i2c, 0x08 << 1, (uint8_t [2]) {0x4D, (enable) ? 0x01 : 0x00}, 2, 1000); |
| 123 | + // LDO2 1V8 CAM |
| 124 | + HAL_I2C_Master_Transmit(&i2c, 0x08 << 1, (uint8_t [2]) {0x50, (enable) ? 0x01 : 0x00}, 2, 1000); |
| 125 | + // LDO3 1V8 |
| 126 | + HAL_I2C_Master_Transmit(&i2c, 0x08 << 1, (uint8_t [2]) {0x53, (enable) ? 0x01 : 0x00}, 2, 1000); |
| 127 | + // SW2 VDDIO_EXT |
| 128 | + HAL_I2C_Master_Transmit(&i2c, 0x08 << 1, (uint8_t [2]) {0x3b, (enable) ? 0x81 : 0x80}, 2, 1000); |
| 129 | + // SW3 3V3 (Leaving 3.3 on lowers ULPI current). |
| 130 | + // HAL_I2C_Master_Transmit(&i2c, 0x08<<1, (uint8_t [2]) {0x41, (enable) ? 0x81 : 0x80}, 2, 1000); |
| 131 | + // SW1 1V8 (Main supply is Never turned off) |
| 132 | + // HAL_I2C_Master_Transmit(&i2c, 0x08<<1, (uint8_t [2]) {0x35, (enable) ? 0x81 : 0x80}, 2, 1000); |
| 133 | + |
| 134 | + HAL_I2C_DeInit(&i2c); |
| 135 | + __HAL_RCC_I2C2_FORCE_RESET(); |
| 136 | + __HAL_RCC_I2C2_RELEASE_RESET(); |
| 137 | + __HAL_RCC_I2C2_CLK_DISABLE(); |
| 138 | + |
| 139 | + HAL_GPIO_DeInit(GPIOF, GPIO_PIN_0); |
| 140 | + HAL_GPIO_DeInit(GPIOF, GPIO_PIN_1); |
| 141 | + __HAL_RCC_GPIOF_CLK_DISABLE(); |
| 142 | +} |
| 143 | + |
| 144 | +static __attribute__((naked, noreturn)) void cm4_enter_standby(void) { |
| 145 | + // Clear and mask D1 EXTIs. |
| 146 | + EXTI_D1->PR1 = 0x3fffff; |
| 147 | + EXTI_D1->IMR1 &= ~(0xFFFFu); // 16 lines |
| 148 | + |
| 149 | + // Clear and mask D2 EXTIs. |
| 150 | + EXTI_D2->IMR1 &= ~(0xFFFFu); // 16 lines |
| 151 | + EXTI_D2->PR1 = 0x3fffff; |
| 152 | + |
| 153 | + EXTI->D3PMR1 = 0;///0x0238FFFFu; |
| 154 | + EXTI->D3PMR2 = 0;///0x003F020Cu; |
| 155 | + EXTI->D3PMR3 = 0;///0x01000000u; |
| 156 | + |
| 157 | + // Set D2/D2 PDDS bits. |
| 158 | + PWR->CPUCR |= (PWR_CPUCR_PDDS_D2 | PWR_CPUCR_PDDS_D3); |
| 159 | + PWR->CPU2CR |= (PWR_CPU2CR_PDDS_D2 | PWR_CPU2CR_PDDS_D3); |
| 160 | + CLEAR_BIT(PWR->CPU2CR, PWR_CPU2CR_RUN_D3); |
| 161 | + |
| 162 | + // Set SLEEPDEEP bit. |
| 163 | + SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk; |
| 164 | + |
| 165 | + // Enter CPU stop mode. |
| 166 | + __WFI(); |
| 167 | +} |
| 168 | + |
| 169 | +void NICLAV_board_low_power(int mode) { |
| 170 | + switch (mode) { |
| 171 | + case 0: // Leave stop mode. |
| 172 | + ulpi_leave_low_power(); |
| 173 | + break; |
| 174 | + case 1: // Enter stop mode. |
| 175 | + ulpi_enter_low_power(); |
| 176 | + break; |
| 177 | + case 2: // Enter standby mode. |
| 178 | + ulpi_enter_low_power(); |
| 179 | + break; |
| 180 | + } |
| 181 | + |
| 182 | + #if (MICROPY_HW_ENABLE_INTERNAL_FLASH_STORAGE == 0) |
| 183 | + // Enable QSPI deepsleep for modes 1 and 2 |
| 184 | + mp_spiflash_deepsleep(&spi_bdev.spiflash, (mode != 0)); |
| 185 | + #endif |
| 186 | + |
| 187 | + if (mode == 2) { |
| 188 | + if (RCC->GCR & RCC_GCR_BOOT_C2) { |
| 189 | + // If CM4 is booted, it should be running a firmware |
| 190 | + // that supports low-power mode entry via HSEM. |
| 191 | + __HAL_RCC_HSEM_CLK_ENABLE(); |
| 192 | + HAL_HSEM_FastTake(0); |
| 193 | + HAL_HSEM_Release(0, 0); |
| 194 | + } else { |
| 195 | + // If CM4 is not booted, rig an ISR vector, with a reset |
| 196 | + // handler that points to standby function and boot it. |
| 197 | + volatile extern char _cm4_ram_start[1024]; |
| 198 | + |
| 199 | + uint32_t *cm4_ram = (uint32_t *)_cm4_ram_start; |
| 200 | + cm4_ram[0] = (uint32_t)(_cm4_ram_start + 1024); |
| 201 | + cm4_ram[1] = ((uint32_t)&cm4_enter_standby) | 1; |
| 202 | + SCB_CleanDCache_by_Addr((uint32_t *)_cm4_ram_start, 8); |
| 203 | + |
| 204 | + HAL_PWREx_HoldCore(PWR_CORE_CPU2); |
| 205 | + HAL_SYSCFG_CM4BootAddConfig(SYSCFG_BOOT_ADDR0, (uint32_t)_cm4_ram_start); |
| 206 | + HAL_RCCEx_EnableBootCore(RCC_BOOT_C2); |
| 207 | + } |
| 208 | + |
| 209 | + // Wait for the CM4 to enter stop mode. |
| 210 | + HAL_Delay(100); |
| 211 | + |
| 212 | + // Disable all power rails, except core voltage. |
| 213 | + NICLAV_board_pmic_enable(false); |
| 214 | + |
| 215 | + // Reset all busses, peripherals, GPIO clocks etc.. |
| 216 | + RCC->AHB1RSTR = 0x0A00C023U; |
| 217 | + RCC->AHB2RSTR = 0x00000271U; |
| 218 | + RCC->AHB3RSTR = 0x00015031U; |
| 219 | + RCC->AHB4RSTR = 0x0328077FU; // LSE GPIO port is masked. |
| 220 | + |
| 221 | + RCC->APB1LRSTR = 0xE8FFC3FFU; |
| 222 | + RCC->APB1HRSTR = 0x00000136U; |
| 223 | + RCC->APB2RSTR = 0x31D73033U; |
| 224 | + RCC->APB3RSTR = 0x00000018U; |
| 225 | + RCC->APB4RSTR = 0x0020DEAAU; |
| 226 | + } |
| 227 | +} |
0 commit comments