Debian Codegraph @

ports/stm32/boards/ARDUINO_PORTENTA_H7/board_init.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) 2022 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 "sdram.h"
31 #include "eth.h"
32 #if MICROPY_HW_USB_HS_ULPI3320
33 #include "ulpi.h"
34 #endif
35
36 #define PORTENTA_PMIC_ERRATA (1)
37
38 void PORTENTA_board_startup(void) {
39 }
40
41 void PORTENTA_board_early_init(void) {
42 HAL_InitTick(0);
43
44 // Enable oscillator pin
45 // This is enabled in the bootloader anyway.
46 PORTENTA_board_osc_enable(true);
47
48 #if MICROPY_HW_ENABLE_INTERNAL_FLASH_STORAGE
49 // The Arduino/mbed bootloader uses the MPU to protect sector 1
50 // which is used for the flash filesystem. The following code
51 // resets and disables all MPU regions configured in the bootloader.
52 HAL_MPU_Disable();
53 MPU_Region_InitTypeDef MPU_InitStruct;
54 MPU_InitStruct.AccessPermission = MPU_REGION_FULL_ACCESS;
55 MPU_InitStruct.IsBufferable = MPU_ACCESS_NOT_BUFFERABLE;
56 MPU_InitStruct.IsCacheable = MPU_ACCESS_NOT_CACHEABLE;
57 MPU_InitStruct.IsShareable = MPU_ACCESS_NOT_SHAREABLE;
58 MPU_InitStruct.TypeExtField = MPU_TEX_LEVEL1;
59 MPU_InitStruct.SubRegionDisable = 0x00;
60 MPU_InitStruct.DisableExec = MPU_INSTRUCTION_ACCESS_ENABLE;
61
62 for (int i = MPU_REGION_NUMBER0; i < MPU_REGION_NUMBER15; i++) {
63 MPU_InitStruct.Number = i;
64 MPU_InitStruct.Enable = MPU_REGION_DISABLE;
65 HAL_MPU_ConfigRegion(&MPU_InitStruct);
66 }
67 #endif
68
69 #if PORTENTA_PMIC_ERRATA
70 // PMIC SW1 current limit fix
71 // Configure PMIC I2C GPIOs
72 mp_hal_pin_config(pyb_pin_I2C1_SCL, MP_HAL_PIN_MODE_ALT_OPEN_DRAIN, MP_HAL_PIN_PULL_NONE, GPIO_AF4_I2C1);
73 mp_hal_pin_config_speed(pyb_pin_I2C1_SCL, MP_HAL_PIN_SPEED_LOW);
74
75 mp_hal_pin_config(pyb_pin_I2C1_SDA, MP_HAL_PIN_MODE_ALT_OPEN_DRAIN, MP_HAL_PIN_PULL_NONE, GPIO_AF4_I2C1);
76 mp_hal_pin_config_speed(pyb_pin_I2C1_SDA, MP_HAL_PIN_SPEED_LOW);
77
78 // Configure PMIC I2C
79 I2C_HandleTypeDef i2c;
80 i2c.Instance = I2C1;
81 i2c.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
82 i2c.Init.Timing = 0x20D09DE7;
83 i2c.Init.DualAddressMode = I2C_DUALADDRESS_DISABLED;
84 i2c.Init.GeneralCallMode = I2C_GENERALCALL_DISABLED;
85 i2c.Init.NoStretchMode = I2C_NOSTRETCH_DISABLED;
86 i2c.Init.OwnAddress1 = 0xFE;
87 i2c.Init.OwnAddress2 = 0xFE;
88 i2c.Init.OwnAddress2Masks = 0;
89
90 __HAL_RCC_I2C1_CLK_ENABLE();
91 __HAL_RCC_I2C1_FORCE_RESET();
92 __HAL_RCC_I2C1_RELEASE_RESET();
93 HAL_I2C_Init(&i2c);
94
95 // Set SW1 current limit to 1.5A
96 // Fixes power glitches with Eth and SDRAM.
97 uint8_t buf[] = {0x36, 0x02};
98 HAL_I2C_Master_Transmit(&i2c, 0x08 << 1, buf, 2, 1000);
99
100 HAL_I2C_DeInit(&i2c);
101 __HAL_RCC_I2C1_FORCE_RESET();
102 __HAL_RCC_I2C1_RELEASE_RESET();
103 __HAL_RCC_I2C1_CLK_DISABLE();
104
105 HAL_GPIO_DeInit(GPIOB, GPIO_PIN_6);
106 HAL_GPIO_DeInit(GPIOB, GPIO_PIN_7);
107 __HAL_RCC_GPIOB_CLK_DISABLE();
108 #endif // PORTENTA_PMIC_ERRATA
109
110 // Reset ETH Phy
111 mp_hal_pin_config(pyb_pin_ETH_RST, MP_HAL_PIN_MODE_OUTPUT, MP_HAL_PIN_PULL_UP, 0);
112 mp_hal_pin_config_speed(pyb_pin_ETH_RST, MP_HAL_PIN_SPEED_LOW);
113 mp_hal_pin_write(pyb_pin_ETH_RST, 0);
114 HAL_Delay(100);
115 mp_hal_pin_write(pyb_pin_ETH_RST, 1);
116
117 // Put Eth in low-power mode
118 eth_init(&eth_instance, MP_HAL_MAC_ETH0, 0, ETH_PHY_LAN8742);
119 eth_low_power_mode(&eth_instance, true);
120
121 #if MICROPY_HW_USB_HS_ULPI3320
122 // Make sure UPLI is Not in low-power mode.
123 ulpi_leave_low_power();
124 #endif
125 }
126
127 void PORTENTA_board_enter_bootloader(void) {
128 RTC_HandleTypeDef RTCHandle;
129 RTCHandle.Instance = RTC;
130 HAL_RTCEx_BKUPWrite(&RTCHandle, RTC_BKP_DR0, 0xDF59);
131 NVIC_SystemReset();
132 }
133
134 void PORTENTA_board_osc_enable(int enable) {
135 mp_hal_pin_config(pyb_pin_OSCEN, MP_HAL_PIN_MODE_OUTPUT, MP_HAL_PIN_PULL_UP, 0);
136 mp_hal_pin_config_speed(pyb_pin_OSCEN, MP_HAL_PIN_SPEED_LOW);
137 mp_hal_pin_write(pyb_pin_OSCEN, enable);
138 }
139
140 void PORTENTA_board_low_power(int mode) {
141 switch (mode) {
142 case 0: // Leave stop mode.
143 #if MICROPY_HW_USB_HS_ULPI3320
144 ulpi_leave_low_power();
145 #endif
146 eth_low_power_mode(NULL, false);
147 sdram_leave_low_power();
148 break;
149 case 1: // Enter stop mode.
150 #if MICROPY_HW_USB_HS_ULPI3320
151 ulpi_enter_low_power();
152 #endif
153 eth_low_power_mode(NULL, true);
154 sdram_enter_low_power();
155 break;
156 case 2: // Enter standby mode.
157 #if MICROPY_HW_USB_HS_ULPI3320
158 ulpi_enter_low_power();
159 #endif
160 eth_low_power_mode(NULL, true);
161 sdram_enter_power_down();
162 break;
163 }
164
165 #if !MICROPY_HW_ENABLE_INTERNAL_FLASH_STORAGE
166 // Enable QSPI deepsleep for modes 1 and 2
167 mp_spiflash_deepsleep(&spi_bdev.spiflash, (mode != 0));
168 #endif
169
170 #if defined(M4_APP_ADDR)
171 // Signal Cortex-M4 to go to Standby mode.
172 if (mode == 2) {
173 __HAL_RCC_HSEM_CLK_ENABLE();
174 HAL_HSEM_FastTake(0);
175 HAL_HSEM_Release(0, 0);
176 __HAL_RCC_HSEM_CLK_DISABLE();
177 HAL_Delay(100);
178 }
179 #endif
180 }