rjw | 1f88458 | 2022-01-06 17:20:42 +0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (c) 2015 Travis Geiselbrecht |
| 3 | * |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining |
| 5 | * a copy of this software and associated documentation files |
| 6 | * (the "Software"), to deal in the Software without restriction, |
| 7 | * including without limitation the rights to use, copy, modify, merge, |
| 8 | * publish, distribute, sublicense, and/or sell copies of the Software, |
| 9 | * and to permit persons to whom the Software is furnished to do so, |
| 10 | * subject to the following conditions: |
| 11 | * |
| 12 | * The above copyright notice and this permission notice shall be |
| 13 | * included in all copies or substantial portions of the Software. |
| 14 | * |
| 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 18 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 19 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 20 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 21 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 22 | */ |
| 23 | #include <err.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <debug.h> |
| 26 | #include <trace.h> |
| 27 | #include <target.h> |
| 28 | #include <compiler.h> |
| 29 | #include <reg.h> |
| 30 | #include <dev/gpio.h> |
| 31 | #include <dev/usb.h> |
| 32 | #include <platform/stm32.h> |
| 33 | #include <platform/sdram.h> |
| 34 | #include <platform/gpio.h> |
| 35 | #include <platform/eth.h> |
| 36 | #include <platform/qspi.h> |
| 37 | #include <platform/n25q128a.h> |
| 38 | #include <target/debugconfig.h> |
| 39 | #include <target/gpioconfig.h> |
| 40 | #include <kernel/novm.h> |
| 41 | |
| 42 | #if WITH_LIB_MINIP |
| 43 | #include <lib/minip.h> |
| 44 | #endif |
| 45 | |
| 46 | extern uint8_t BSP_LCD_Init(void); |
| 47 | extern void target_usb_setup(void); |
| 48 | |
| 49 | const sdram_config_t target_sdram_config = { |
| 50 | .bus_width = SDRAM_BUS_WIDTH_16, |
| 51 | .cas_latency = SDRAM_CAS_LATENCY_2, |
| 52 | .col_bits_num = SDRAM_COLUMN_BITS_8 |
| 53 | }; |
| 54 | |
| 55 | void target_early_init(void) |
| 56 | { |
| 57 | #if DEBUG_UART == 1 |
| 58 | /* configure usart 1 pins */ |
| 59 | gpio_config(GPIO_USART1_TX, GPIO_STM32_AF | GPIO_STM32_AFn(GPIO_AF7_USART1) | GPIO_PULLUP); |
| 60 | gpio_config(GPIO_USART1_RX, GPIO_STM32_AF | GPIO_STM32_AFn(GPIO_AF7_USART1) | GPIO_PULLUP); |
| 61 | #else |
| 62 | #error need to configure gpio pins for debug uart |
| 63 | #endif |
| 64 | |
| 65 | /* now that the uart gpios are configured, enable the debug uart */ |
| 66 | stm32_debug_early_init(); |
| 67 | |
| 68 | /* start the lcd */ |
| 69 | BSP_LCD_Init(); |
| 70 | } |
| 71 | |
| 72 | void target_init(void) |
| 73 | { |
| 74 | stm32_debug_init(); |
| 75 | |
| 76 | qspi_flash_init(N25Q128A_FLASH_SIZE); |
| 77 | |
| 78 | #if WITH_LIB_MINIP |
| 79 | uint8_t mac_addr[6]; |
| 80 | gen_random_mac_address(mac_addr); |
| 81 | eth_init(mac_addr, PHY_LAN8742A); |
| 82 | |
| 83 | /* start minip */ |
| 84 | minip_set_macaddr(mac_addr); |
| 85 | |
| 86 | uint32_t ip_addr = IPV4(192, 168, 0, 98); |
| 87 | uint32_t ip_mask = IPV4(255, 255, 255, 0); |
| 88 | uint32_t ip_gateway = IPV4_NONE; |
| 89 | |
| 90 | minip_init(stm32_eth_send_minip_pkt, NULL, ip_addr, ip_mask, ip_gateway); |
| 91 | #endif |
| 92 | |
| 93 | // start usb |
| 94 | target_usb_setup(); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * @brief Initializes SDRAM GPIO. |
| 99 | * called back from stm32_sdram_init |
| 100 | */ |
| 101 | void stm_sdram_GPIO_init(void) |
| 102 | { |
| 103 | GPIO_InitTypeDef gpio_init_structure; |
| 104 | |
| 105 | /* Enable GPIOs clock */ |
| 106 | __HAL_RCC_GPIOC_CLK_ENABLE(); |
| 107 | __HAL_RCC_GPIOD_CLK_ENABLE(); |
| 108 | __HAL_RCC_GPIOE_CLK_ENABLE(); |
| 109 | __HAL_RCC_GPIOF_CLK_ENABLE(); |
| 110 | __HAL_RCC_GPIOG_CLK_ENABLE(); |
| 111 | __HAL_RCC_GPIOH_CLK_ENABLE(); |
| 112 | |
| 113 | /* Common GPIO configuration */ |
| 114 | gpio_init_structure.Mode = GPIO_MODE_AF_PP; |
| 115 | gpio_init_structure.Pull = GPIO_PULLUP; |
| 116 | gpio_init_structure.Speed = GPIO_SPEED_FAST; |
| 117 | gpio_init_structure.Alternate = GPIO_AF12_FMC; |
| 118 | |
| 119 | /* GPIOC configuration */ |
| 120 | gpio_init_structure.Pin = GPIO_PIN_3; |
| 121 | HAL_GPIO_Init(GPIOC, &gpio_init_structure); |
| 122 | |
| 123 | /* GPIOD configuration */ |
| 124 | gpio_init_structure.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_3 | GPIO_PIN_8| GPIO_PIN_9 | GPIO_PIN_10 |\ |
| 125 | GPIO_PIN_14 | GPIO_PIN_15; |
| 126 | HAL_GPIO_Init(GPIOD, &gpio_init_structure); |
| 127 | |
| 128 | /* GPIOE configuration */ |
| 129 | gpio_init_structure.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_7| GPIO_PIN_8 | GPIO_PIN_9 |\ |
| 130 | GPIO_PIN_10 | GPIO_PIN_11 | GPIO_PIN_12 | GPIO_PIN_13 | GPIO_PIN_14 |\ |
| 131 | GPIO_PIN_15; |
| 132 | HAL_GPIO_Init(GPIOE, &gpio_init_structure); |
| 133 | |
| 134 | /* GPIOF configuration */ |
| 135 | gpio_init_structure.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2| GPIO_PIN_3 | GPIO_PIN_4 |\ |
| 136 | GPIO_PIN_5 | GPIO_PIN_11 | GPIO_PIN_12 | GPIO_PIN_13 | GPIO_PIN_14 |\ |
| 137 | GPIO_PIN_15; |
| 138 | HAL_GPIO_Init(GPIOF, &gpio_init_structure); |
| 139 | |
| 140 | /* GPIOG configuration */ |
| 141 | gpio_init_structure.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_4| GPIO_PIN_5 | GPIO_PIN_8 |\ |
| 142 | GPIO_PIN_15; |
| 143 | HAL_GPIO_Init(GPIOG, &gpio_init_structure); |
| 144 | |
| 145 | /* GPIOH configuration */ |
| 146 | gpio_init_structure.Pin = GPIO_PIN_3 | GPIO_PIN_5; |
| 147 | HAL_GPIO_Init(GPIOH, &gpio_init_structure); |
| 148 | } |
| 149 | |
| 150 | |
| 151 | /** |
| 152 | * @brief Initializes the ETH MSP. |
| 153 | * @param heth: ETH handle |
| 154 | * @retval None |
| 155 | */ |
| 156 | /* called back from the HAL_ETH_Init routine */ |
| 157 | void HAL_ETH_MspInit(ETH_HandleTypeDef *heth) |
| 158 | { |
| 159 | GPIO_InitTypeDef GPIO_InitStructure; |
| 160 | |
| 161 | /* Enable GPIOs clocks */ |
| 162 | __HAL_RCC_GPIOA_CLK_ENABLE(); |
| 163 | __HAL_RCC_GPIOC_CLK_ENABLE(); |
| 164 | __HAL_RCC_GPIOG_CLK_ENABLE(); |
| 165 | |
| 166 | /* Ethernet pins configuration ************************************************/ |
| 167 | /* |
| 168 | RMII_REF_CLK ----------------------> PA1 |
| 169 | RMII_MDIO -------------------------> PA2 |
| 170 | RMII_MDC --------------------------> PC1 |
| 171 | RMII_MII_CRS_DV -------------------> PA7 |
| 172 | RMII_MII_RXD0 ---------------------> PC4 |
| 173 | RMII_MII_RXD1 ---------------------> PC5 |
| 174 | RMII_MII_TX_EN --------------------> PG11 |
| 175 | RMII_MII_TXD0 ---------------------> PG13 |
| 176 | RMII_MII_TXD1 ---------------------> PG14 |
| 177 | */ |
| 178 | |
| 179 | /* Configure PA1, PA2 and PA7 */ |
| 180 | GPIO_InitStructure.Speed = GPIO_SPEED_HIGH; |
| 181 | GPIO_InitStructure.Mode = GPIO_MODE_AF_PP; |
| 182 | GPIO_InitStructure.Pull = GPIO_NOPULL; |
| 183 | GPIO_InitStructure.Alternate = GPIO_AF11_ETH; |
| 184 | GPIO_InitStructure.Pin = GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_7; |
| 185 | HAL_GPIO_Init(GPIOA, &GPIO_InitStructure); |
| 186 | |
| 187 | /* Configure PC1, PC4 and PC5 */ |
| 188 | GPIO_InitStructure.Pin = GPIO_PIN_1 | GPIO_PIN_4 | GPIO_PIN_5; |
| 189 | HAL_GPIO_Init(GPIOC, &GPIO_InitStructure); |
| 190 | |
| 191 | /* Configure PG2, PG11, PG13 and PG14 */ |
| 192 | GPIO_InitStructure.Pin = GPIO_PIN_2 | GPIO_PIN_11 | GPIO_PIN_13 | GPIO_PIN_14; |
| 193 | HAL_GPIO_Init(GPIOG, &GPIO_InitStructure); |
| 194 | } |
| 195 | |
| 196 | void HAL_QSPI_MspInit(QSPI_HandleTypeDef *hqspi) |
| 197 | { |
| 198 | GPIO_InitTypeDef GPIO_InitStruct; |
| 199 | |
| 200 | /*##-1- Enable peripherals and GPIO Clocks #################################*/ |
| 201 | /* Enable GPIO clocks */ |
| 202 | __HAL_RCC_GPIOB_CLK_ENABLE(); |
| 203 | __HAL_RCC_GPIOD_CLK_ENABLE(); |
| 204 | __HAL_RCC_GPIOE_CLK_ENABLE(); |
| 205 | |
| 206 | /*##-2- Configure peripheral GPIO ##########################################*/ |
| 207 | /* QSPI CS GPIO pin configuration */ |
| 208 | GPIO_InitStruct.Pin = GPIO_PIN_6; |
| 209 | GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; |
| 210 | GPIO_InitStruct.Pull = GPIO_PULLUP; |
| 211 | GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; |
| 212 | GPIO_InitStruct.Alternate = GPIO_AF10_QUADSPI; |
| 213 | HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); |
| 214 | |
| 215 | /* QSPI CLK GPIO pin configuration */ |
| 216 | GPIO_InitStruct.Pin = GPIO_PIN_2; |
| 217 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
| 218 | GPIO_InitStruct.Alternate = GPIO_AF9_QUADSPI; |
| 219 | HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); |
| 220 | |
| 221 | /* QSPI D0 GPIO pin configuration */ |
| 222 | GPIO_InitStruct.Pin = GPIO_PIN_11; |
| 223 | GPIO_InitStruct.Alternate = GPIO_AF9_QUADSPI; |
| 224 | HAL_GPIO_Init(GPIOD, &GPIO_InitStruct); |
| 225 | |
| 226 | /* QSPI D1 GPIO pin configuration */ |
| 227 | GPIO_InitStruct.Pin = GPIO_PIN_12; |
| 228 | GPIO_InitStruct.Alternate = GPIO_AF9_QUADSPI; |
| 229 | HAL_GPIO_Init(GPIOD, &GPIO_InitStruct); |
| 230 | |
| 231 | /* QSPI D2 GPIO pin configuration */ |
| 232 | GPIO_InitStruct.Pin = GPIO_PIN_2; |
| 233 | GPIO_InitStruct.Alternate = GPIO_AF9_QUADSPI; |
| 234 | HAL_GPIO_Init(GPIOE, &GPIO_InitStruct); |
| 235 | |
| 236 | /* QSPI D3 GPIO pin configuration */ |
| 237 | GPIO_InitStruct.Pin = GPIO_PIN_13; |
| 238 | GPIO_InitStruct.Alternate = GPIO_AF9_QUADSPI; |
| 239 | HAL_GPIO_Init(GPIOD, &GPIO_InitStruct); |
| 240 | } |
| 241 | |
| 242 | /** |
| 243 | * @brief Initializes the PCD MSP. |
| 244 | * @param hpcd: PCD handle |
| 245 | * @retval None |
| 246 | */ |
| 247 | void HAL_PCD_MspInit(PCD_HandleTypeDef *hpcd) |
| 248 | { |
| 249 | GPIO_InitTypeDef GPIO_InitStruct; |
| 250 | |
| 251 | if (hpcd->Instance == USB_OTG_FS) { |
| 252 | /* Configure USB FS GPIOs */ |
| 253 | __HAL_RCC_GPIOA_CLK_ENABLE(); |
| 254 | |
| 255 | /* Configure DM DP Pins */ |
| 256 | GPIO_InitStruct.Pin = (GPIO_PIN_11 | GPIO_PIN_12); |
| 257 | GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; |
| 258 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
| 259 | GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; |
| 260 | GPIO_InitStruct.Alternate = GPIO_AF10_OTG_FS; |
| 261 | HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); |
| 262 | |
| 263 | /* Enable USB FS Clock */ |
| 264 | __HAL_RCC_USB_OTG_FS_CLK_ENABLE(); |
| 265 | |
| 266 | /* Set USBFS Interrupt priority */ |
| 267 | HAL_NVIC_SetPriority(OTG_FS_IRQn, 5, 0); |
| 268 | |
| 269 | /* Enable USBFS Interrupt */ |
| 270 | HAL_NVIC_EnableIRQ(OTG_FS_IRQn); |
| 271 | |
| 272 | if (hpcd->Init.low_power_enable == 1) { |
| 273 | /* Enable EXTI Line 18 for USB wakeup*/ |
| 274 | __HAL_USB_OTG_FS_WAKEUP_EXTI_CLEAR_FLAG(); |
| 275 | __HAL_USB_OTG_FS_WAKEUP_EXTI_ENABLE_RISING_EDGE(); |
| 276 | __HAL_USB_OTG_FS_WAKEUP_EXTI_ENABLE_IT(); |
| 277 | |
| 278 | /* Set EXTI Wakeup Interrupt priority*/ |
| 279 | HAL_NVIC_SetPriority(OTG_FS_WKUP_IRQn, 0, 0); |
| 280 | |
| 281 | /* Enable EXTI Interrupt */ |
| 282 | HAL_NVIC_EnableIRQ(OTG_FS_WKUP_IRQn); |
| 283 | } |
| 284 | } else if (hpcd->Instance == USB_OTG_HS) { |
| 285 | /* Configure USB FS GPIOs */ |
| 286 | __HAL_RCC_GPIOA_CLK_ENABLE(); |
| 287 | __HAL_RCC_GPIOB_CLK_ENABLE(); |
| 288 | __HAL_RCC_GPIOC_CLK_ENABLE(); |
| 289 | __HAL_RCC_GPIOH_CLK_ENABLE(); |
| 290 | |
| 291 | /* CLK */ |
| 292 | GPIO_InitStruct.Pin = GPIO_PIN_5; |
| 293 | GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; |
| 294 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
| 295 | GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; |
| 296 | GPIO_InitStruct.Alternate = GPIO_AF10_OTG_HS; |
| 297 | HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); |
| 298 | |
| 299 | /* D0 */ |
| 300 | GPIO_InitStruct.Pin = GPIO_PIN_3; |
| 301 | GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; |
| 302 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
| 303 | GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; |
| 304 | GPIO_InitStruct.Alternate = GPIO_AF10_OTG_HS; |
| 305 | HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); |
| 306 | |
| 307 | /* D1 D2 D3 D4 D5 D6 D7 */ |
| 308 | GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_5 |\ |
| 309 | GPIO_PIN_10 | GPIO_PIN_11 | GPIO_PIN_12 | GPIO_PIN_13; |
| 310 | GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; |
| 311 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
| 312 | GPIO_InitStruct.Alternate = GPIO_AF10_OTG_HS; |
| 313 | HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); |
| 314 | |
| 315 | /* STP */ |
| 316 | GPIO_InitStruct.Pin = GPIO_PIN_0; |
| 317 | GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; |
| 318 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
| 319 | GPIO_InitStruct.Alternate = GPIO_AF10_OTG_HS; |
| 320 | HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); |
| 321 | |
| 322 | /* NXT */ |
| 323 | GPIO_InitStruct.Pin = GPIO_PIN_4; |
| 324 | GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; |
| 325 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
| 326 | GPIO_InitStruct.Alternate = GPIO_AF10_OTG_HS; |
| 327 | HAL_GPIO_Init(GPIOH, &GPIO_InitStruct); |
| 328 | |
| 329 | /* DIR */ |
| 330 | GPIO_InitStruct.Pin = GPIO_PIN_2; |
| 331 | GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; |
| 332 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
| 333 | GPIO_InitStruct.Alternate = GPIO_AF10_OTG_HS; |
| 334 | HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); |
| 335 | |
| 336 | __HAL_RCC_USB_OTG_HS_ULPI_CLK_ENABLE(); |
| 337 | |
| 338 | /* Enable USB HS Clocks */ |
| 339 | __HAL_RCC_USB_OTG_HS_CLK_ENABLE(); |
| 340 | |
| 341 | /* Set USBHS Interrupt to the lowest priority */ |
| 342 | HAL_NVIC_SetPriority(OTG_HS_IRQn, 5, 0); |
| 343 | |
| 344 | /* Enable USBHS Interrupt */ |
| 345 | HAL_NVIC_EnableIRQ(OTG_HS_IRQn); |
| 346 | } |
| 347 | } |
| 348 | |