|
| 1 | +/* |
| 2 | + * This file is part of the MicroPython project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2022 Ibrahim Abdelkader <iabdalkader@openmv.io> |
| 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 | +#include "eth.h" |
| 32 | +#if MICROPY_HW_USB_HS_ULPI3320 |
| 33 | +#include "ulpi.h" |
| 34 | +#endif |
| 35 | +#define PORTENTA_PMIC_ERRATA (1) |
| 36 | + |
| 37 | +void PORTENTA_board_startup(void) { |
| 38 | + |
| 39 | +} |
| 40 | + |
| 41 | +void PORTENTA_board_early_init(void) { |
| 42 | + |
| 43 | + HAL_InitTick(0); |
| 44 | + |
| 45 | + // Enable oscillator pin |
| 46 | + // This is enabled in the bootloader anyway. |
| 47 | + PORTENTA_board_osc_enable(true); |
| 48 | + |
| 49 | + #if (MICROPY_HW_ENABLE_INTERNAL_FLASH_STORAGE == 1) |
| 50 | + // The Arduino/mbed bootloader uses the MPU to protect sector 1 |
| 51 | + // which is used for the flash filesystem. The following code |
| 52 | + // resets and disables all MPU regions configured in the bootloader. |
| 53 | + HAL_MPU_Disable(); |
| 54 | + MPU_Region_InitTypeDef MPU_InitStruct; |
| 55 | + MPU_InitStruct.AccessPermission = MPU_REGION_FULL_ACCESS; |
| 56 | + MPU_InitStruct.IsBufferable = MPU_ACCESS_NOT_BUFFERABLE; |
| 57 | + MPU_InitStruct.IsCacheable = MPU_ACCESS_NOT_CACHEABLE; |
| 58 | + MPU_InitStruct.IsShareable = MPU_ACCESS_NOT_SHAREABLE; |
| 59 | + MPU_InitStruct.TypeExtField = MPU_TEX_LEVEL1; |
| 60 | + MPU_InitStruct.SubRegionDisable = 0x00; |
| 61 | + MPU_InitStruct.DisableExec = MPU_INSTRUCTION_ACCESS_ENABLE; |
| 62 | + |
| 63 | + for (int i=MPU_REGION_NUMBER0; i<MPU_REGION_NUMBER15; i++) { |
| 64 | + MPU_InitStruct.Number = i; |
| 65 | + MPU_InitStruct.Enable = MPU_REGION_DISABLE; |
| 66 | + HAL_MPU_ConfigRegion(&MPU_InitStruct); |
| 67 | + } |
| 68 | + #endif |
| 69 | + |
| 70 | + #if PORTENTA_PMIC_ERRATA |
| 71 | + // PMIC SW1 current limit fix |
| 72 | + __HAL_RCC_GPIOB_CLK_ENABLE(); |
| 73 | + |
| 74 | + // Configure PMIC I2C GPIOs |
| 75 | + GPIO_InitTypeDef GPIO_InitStructure; |
| 76 | + GPIO_InitStructure.Pin = GPIO_PIN_6 | GPIO_PIN_7; |
| 77 | + GPIO_InitStructure.Pull = GPIO_NOPULL; |
| 78 | + GPIO_InitStructure.Speed = GPIO_SPEED_LOW; |
| 79 | + GPIO_InitStructure.Mode = GPIO_MODE_AF_OD; |
| 80 | + GPIO_InitStructure.Alternate = GPIO_AF4_I2C1; |
| 81 | + HAL_GPIO_Init(GPIOB, &GPIO_InitStructure); |
| 82 | + |
| 83 | + // Configure PMIC I2C |
| 84 | + I2C_HandleTypeDef i2c; |
| 85 | + i2c.Instance = I2C1; |
| 86 | + i2c.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT; |
| 87 | + i2c.Init.Timing = 0x20D09DE7; |
| 88 | + i2c.Init.DualAddressMode = I2C_DUALADDRESS_DISABLED; |
| 89 | + i2c.Init.GeneralCallMode = I2C_GENERALCALL_DISABLED; |
| 90 | + i2c.Init.NoStretchMode = I2C_NOSTRETCH_DISABLED; |
| 91 | + i2c.Init.OwnAddress1 = 0xFE; |
| 92 | + i2c.Init.OwnAddress2 = 0xFE; |
| 93 | + i2c.Init.OwnAddress2Masks = 0; |
| 94 | + |
| 95 | + __HAL_RCC_I2C1_CLK_ENABLE(); |
| 96 | + __HAL_RCC_I2C1_FORCE_RESET(); |
| 97 | + __HAL_RCC_I2C1_RELEASE_RESET(); |
| 98 | + HAL_I2C_Init(&i2c); |
| 99 | + |
| 100 | + // Set SW1 current limit to 1.5A |
| 101 | + // Fixes power glitches with Eth and SDRAM. |
| 102 | + uint8_t buf[] = {0x36, 0x02}; |
| 103 | + HAL_I2C_Master_Transmit(&i2c, 0x08<<1, buf, 2, 1000); |
| 104 | + |
| 105 | + HAL_I2C_DeInit(&i2c); |
| 106 | + __HAL_RCC_I2C1_FORCE_RESET(); |
| 107 | + __HAL_RCC_I2C1_RELEASE_RESET(); |
| 108 | + __HAL_RCC_I2C1_CLK_DISABLE(); |
| 109 | + |
| 110 | + HAL_GPIO_DeInit(GPIOB, GPIO_PIN_6); |
| 111 | + HAL_GPIO_DeInit(GPIOB, GPIO_PIN_7); |
| 112 | + __HAL_RCC_GPIOB_CLK_DISABLE(); |
| 113 | + #endif // PORTENTA_PMIC_ERRATA |
| 114 | + |
| 115 | + // Reset ETH Phy |
| 116 | + __HAL_RCC_GPIOJ_CLK_ENABLE(); |
| 117 | + GPIO_InitTypeDef gpio_eth_rst_init_structure; |
| 118 | + gpio_eth_rst_init_structure.Pin = GPIO_PIN_15; |
| 119 | + gpio_eth_rst_init_structure.Mode = GPIO_MODE_OUTPUT_PP; |
| 120 | + gpio_eth_rst_init_structure.Pull = GPIO_PULLUP; |
| 121 | + gpio_eth_rst_init_structure.Speed = GPIO_SPEED_FREQ_LOW; |
| 122 | + HAL_GPIO_Init(GPIOJ, &gpio_eth_rst_init_structure); |
| 123 | + HAL_GPIO_WritePin(GPIOJ, GPIO_PIN_15, 0); |
| 124 | + HAL_Delay(100); |
| 125 | + HAL_GPIO_WritePin(GPIOJ, GPIO_PIN_15, 1); |
| 126 | + |
| 127 | + // Put Eth in low-power mode |
| 128 | + eth_init(ð_instance, MP_HAL_MAC_ETH0); |
| 129 | + eth_low_power_mode(ð_instance, true); |
| 130 | + |
| 131 | + #if MICROPY_HW_USB_HS_ULPI3320 |
| 132 | + // Make sure UPLI is Not in low-power mode. |
| 133 | + ulpi_leave_low_power(); |
| 134 | + #endif |
| 135 | +} |
| 136 | + |
| 137 | +void PORTENTA_reboot_to_bootloader(void) { |
| 138 | + RTC_HandleTypeDef RTCHandle; |
| 139 | + RTCHandle.Instance = RTC; |
| 140 | + HAL_RTCEx_BKUPWrite(&RTCHandle, RTC_BKP_DR0, 0xDF59); |
| 141 | + NVIC_SystemReset(); |
| 142 | +} |
| 143 | + |
| 144 | +void PORTENTA_board_osc_enable(int enable) { |
| 145 | + __HAL_RCC_GPIOH_CLK_ENABLE(); |
| 146 | + GPIO_InitTypeDef gpio_osc_init_structure; |
| 147 | + gpio_osc_init_structure.Pin = GPIO_PIN_1; |
| 148 | + gpio_osc_init_structure.Mode = GPIO_MODE_OUTPUT_PP; |
| 149 | + gpio_osc_init_structure.Pull = GPIO_PULLUP; |
| 150 | + gpio_osc_init_structure.Speed = GPIO_SPEED_FREQ_LOW; |
| 151 | + HAL_GPIO_Init(GPIOH, &gpio_osc_init_structure); |
| 152 | + HAL_GPIO_WritePin(GPIOH, GPIO_PIN_1, enable); |
| 153 | +} |
| 154 | + |
| 155 | +void PORTENTA_board_low_power(int mode) |
| 156 | +{ |
| 157 | + switch (mode) { |
| 158 | + case 0: // Leave stop mode. |
| 159 | + #if MICROPY_HW_USB_HS_ULPI3320 |
| 160 | + ulpi_leave_low_power(); |
| 161 | + #endif |
| 162 | + eth_low_power_mode(NULL, false); |
| 163 | + sdram_leave_low_power(); |
| 164 | + break; |
| 165 | + case 1: // Enter stop mode. |
| 166 | + #if MICROPY_HW_USB_HS_ULPI3320 |
| 167 | + ulpi_enter_low_power(); |
| 168 | + #endif |
| 169 | + eth_low_power_mode(NULL, true); |
| 170 | + sdram_enter_low_power(); |
| 171 | + break; |
| 172 | + case 2: // Enter standby mode. |
| 173 | + #if MICROPY_HW_USB_HS_ULPI3320 |
| 174 | + ulpi_enter_low_power(); |
| 175 | + #endif |
| 176 | + eth_low_power_mode(NULL, true); |
| 177 | + sdram_enter_power_down(); |
| 178 | + break; |
| 179 | + } |
| 180 | + |
| 181 | + #if (MICROPY_HW_ENABLE_INTERNAL_FLASH_STORAGE == 0) |
| 182 | + // Enable QSPI deepsleep for modes 1 and 2 |
| 183 | + mp_spiflash_deepsleep(&spi_bdev.spiflash, (mode != 0)); |
| 184 | + #endif |
| 185 | + |
| 186 | + #if defined(M4_APP_ADDR) |
| 187 | + // Signal Cortex-M4 to go to Standby mode. |
| 188 | + if (mode == 2) { |
| 189 | + __HAL_RCC_HSEM_CLK_ENABLE(); |
| 190 | + HAL_HSEM_FastTake(0); |
| 191 | + HAL_HSEM_Release(0, 0); |
| 192 | + __HAL_RCC_HSEM_CLK_DISABLE(); |
| 193 | + HAL_Delay(100); |
| 194 | + } |
| 195 | + #endif |
| 196 | +} |
0 commit comments