Austin Schuh | 41baf20 | 2022-01-01 14:33:40 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * The MIT License (MIT) |
| 3 | * |
| 4 | * Copyright (c) 2019 William D. Jones (thor0505@comcast.net), |
| 5 | * Uwe Bonnes (bon@elektron.ikp.physik.tu-darmstadt.de), |
| 6 | * Ha Thach (tinyusb.org) |
| 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 | * This file is part of the TinyUSB stack. |
| 27 | */ |
| 28 | |
| 29 | #include "stm32f7xx_hal.h" |
| 30 | #include "bsp/board.h" |
| 31 | #include "board.h" |
| 32 | |
| 33 | //--------------------------------------------------------------------+ |
| 34 | // Forward USB interrupt events to TinyUSB IRQ Handler |
| 35 | //--------------------------------------------------------------------+ |
| 36 | void OTG_FS_IRQHandler(void) |
| 37 | { |
| 38 | tud_int_handler(0); |
| 39 | } |
| 40 | |
| 41 | // Despite being call USB2_OTG |
| 42 | // OTG_HS is marked as RHPort1 by TinyUSB to be consistent across stm32 port |
| 43 | void OTG_HS_IRQHandler(void) |
| 44 | { |
| 45 | tud_int_handler(1); |
| 46 | } |
| 47 | |
| 48 | //--------------------------------------------------------------------+ |
| 49 | // MACRO TYPEDEF CONSTANT ENUM |
| 50 | //--------------------------------------------------------------------+ |
| 51 | |
| 52 | UART_HandleTypeDef UartHandle; |
| 53 | |
| 54 | void board_init(void) |
| 55 | { |
| 56 | board_clock_init(); |
| 57 | |
| 58 | // Enable All GPIOs clocks |
| 59 | __HAL_RCC_GPIOA_CLK_ENABLE(); |
| 60 | __HAL_RCC_GPIOB_CLK_ENABLE(); |
| 61 | __HAL_RCC_GPIOC_CLK_ENABLE(); |
| 62 | __HAL_RCC_GPIOD_CLK_ENABLE(); |
| 63 | __HAL_RCC_GPIOG_CLK_ENABLE(); |
| 64 | __HAL_RCC_GPIOH_CLK_ENABLE(); // ULPI NXT |
| 65 | __HAL_RCC_GPIOI_CLK_ENABLE(); // ULPI NXT |
| 66 | |
| 67 | #ifdef __HAL_RCC_GPIOJ_CLK_ENABLE |
| 68 | __HAL_RCC_GPIOJ_CLK_ENABLE(); |
| 69 | #endif |
| 70 | |
| 71 | UART_CLK_EN(); |
| 72 | |
| 73 | #if CFG_TUSB_OS == OPT_OS_NONE |
| 74 | // 1ms tick timer |
| 75 | SysTick_Config(SystemCoreClock / 1000); |
| 76 | |
| 77 | #elif CFG_TUSB_OS == OPT_OS_FREERTOS |
| 78 | // Explicitly disable systick to prevent its ISR runs before scheduler start |
| 79 | SysTick->CTRL &= ~1U; |
| 80 | |
| 81 | // If freeRTOS is used, IRQ priority is limit by max syscall ( smaller is higher ) |
| 82 | NVIC_SetPriority(OTG_FS_IRQn, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY ); |
| 83 | NVIC_SetPriority(OTG_HS_IRQn, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY ); |
| 84 | #endif |
| 85 | |
| 86 | GPIO_InitTypeDef GPIO_InitStruct; |
| 87 | |
| 88 | // LED |
| 89 | GPIO_InitStruct.Pin = LED_PIN; |
| 90 | GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; |
| 91 | GPIO_InitStruct.Pull = GPIO_PULLUP; |
| 92 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; |
| 93 | HAL_GPIO_Init(LED_PORT, &GPIO_InitStruct); |
| 94 | |
| 95 | // Button |
| 96 | GPIO_InitStruct.Pin = BUTTON_PIN; |
| 97 | GPIO_InitStruct.Mode = GPIO_MODE_INPUT; |
| 98 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
| 99 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; |
| 100 | HAL_GPIO_Init(BUTTON_PORT, &GPIO_InitStruct); |
| 101 | |
| 102 | // Uart TX |
| 103 | GPIO_InitStruct.Pin = UART_TX_PIN; |
| 104 | GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; |
| 105 | GPIO_InitStruct.Pull = GPIO_PULLUP; |
| 106 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; |
| 107 | GPIO_InitStruct.Alternate = UART_GPIO_AF; |
| 108 | HAL_GPIO_Init(UART_TX_PORT, &GPIO_InitStruct); |
| 109 | |
| 110 | // Uart RX |
| 111 | GPIO_InitStruct.Pin = UART_RX_PIN; |
| 112 | GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; |
| 113 | GPIO_InitStruct.Pull = GPIO_PULLUP; |
| 114 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; |
| 115 | GPIO_InitStruct.Alternate = UART_GPIO_AF; |
| 116 | HAL_GPIO_Init(UART_RX_PORT, &GPIO_InitStruct); |
| 117 | |
| 118 | UartHandle.Instance = UART_DEV; |
| 119 | UartHandle.Init.BaudRate = CFG_BOARD_UART_BAUDRATE; |
| 120 | UartHandle.Init.WordLength = UART_WORDLENGTH_8B; |
| 121 | UartHandle.Init.StopBits = UART_STOPBITS_1; |
| 122 | UartHandle.Init.Parity = UART_PARITY_NONE; |
| 123 | UartHandle.Init.HwFlowCtl = UART_HWCONTROL_NONE; |
| 124 | UartHandle.Init.Mode = UART_MODE_TX_RX; |
| 125 | UartHandle.Init.OverSampling = UART_OVERSAMPLING_16; |
| 126 | HAL_UART_Init(&UartHandle); |
| 127 | |
| 128 | #if BOARD_DEVICE_RHPORT_NUM == 0 |
| 129 | // OTG_FS |
| 130 | |
| 131 | /* Configure DM DP Pins */ |
| 132 | GPIO_InitStruct.Pin = (GPIO_PIN_11 | GPIO_PIN_12); |
| 133 | GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; |
| 134 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
| 135 | GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; |
| 136 | GPIO_InitStruct.Alternate = GPIO_AF10_OTG_FS; |
| 137 | HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); |
| 138 | |
| 139 | /* Configure OTG-FS ID pin */ |
| 140 | GPIO_InitStruct.Pin = GPIO_PIN_10; |
| 141 | GPIO_InitStruct.Mode = GPIO_MODE_AF_OD; |
| 142 | GPIO_InitStruct.Pull = GPIO_PULLUP; |
| 143 | GPIO_InitStruct.Alternate = GPIO_AF10_OTG_FS; |
| 144 | HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); |
| 145 | |
| 146 | /* Enable USB FS Clocks */ |
| 147 | __HAL_RCC_USB_OTG_FS_CLK_ENABLE(); |
| 148 | |
| 149 | #if OTG_FS_VBUS_SENSE |
| 150 | /* Configure VBUS Pin */ |
| 151 | GPIO_InitStruct.Pin = GPIO_PIN_9; |
| 152 | GPIO_InitStruct.Mode = GPIO_MODE_INPUT; |
| 153 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
| 154 | HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); |
| 155 | |
| 156 | // Enable VBUS sense (B device) via pin PA9 |
| 157 | USB_OTG_FS->GCCFG |= USB_OTG_GCCFG_VBDEN; |
| 158 | #else |
| 159 | // Disable VBUS sense (B device) via pin PA9 |
| 160 | USB_OTG_FS->GCCFG &= ~USB_OTG_GCCFG_VBDEN; |
| 161 | |
| 162 | // B-peripheral session valid override enable |
| 163 | USB_OTG_FS->GOTGCTL |= USB_OTG_GOTGCTL_BVALOEN; |
| 164 | USB_OTG_FS->GOTGCTL |= USB_OTG_GOTGCTL_BVALOVAL; |
| 165 | #endif // vbus sense |
| 166 | |
| 167 | #else |
| 168 | // OTG_HS |
| 169 | |
| 170 | #ifdef USB_HS_PHYC |
| 171 | // MCU with built-in HS PHY such as F723, F733, F730 |
| 172 | |
| 173 | /* Configure DM DP Pins */ |
| 174 | GPIO_InitStruct.Pin = (GPIO_PIN_14 | GPIO_PIN_15); |
| 175 | GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; |
| 176 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
| 177 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; |
| 178 | GPIO_InitStruct.Alternate = GPIO_AF10_OTG_HS; |
| 179 | HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); |
| 180 | |
| 181 | // Enable HS VBUS sense (B device) via pin PB13 |
| 182 | USB_OTG_HS->GCCFG |= USB_OTG_GCCFG_VBDEN; |
| 183 | |
| 184 | /* Configure OTG-HS ID pin */ |
| 185 | GPIO_InitStruct.Pin = GPIO_PIN_13; |
| 186 | GPIO_InitStruct.Mode = GPIO_MODE_AF_OD; |
| 187 | GPIO_InitStruct.Pull = GPIO_PULLUP; |
| 188 | GPIO_InitStruct.Alternate = GPIO_AF10_OTG_HS; |
| 189 | HAL_GPIO_Init(GPIOD, &GPIO_InitStruct); |
| 190 | |
| 191 | /* Enable PHYC Clocks */ |
| 192 | __HAL_RCC_OTGPHYC_CLK_ENABLE(); |
| 193 | |
| 194 | #else |
| 195 | // MUC with external ULPI PHY |
| 196 | |
| 197 | /* ULPI CLK */ |
| 198 | GPIO_InitStruct.Pin = GPIO_PIN_5; |
| 199 | GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; |
| 200 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
| 201 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; |
| 202 | GPIO_InitStruct.Alternate = GPIO_AF10_OTG_HS; |
| 203 | HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); |
| 204 | |
| 205 | /* ULPI D0 */ |
| 206 | GPIO_InitStruct.Pin = GPIO_PIN_3; |
| 207 | GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; |
| 208 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
| 209 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; |
| 210 | GPIO_InitStruct.Alternate = GPIO_AF10_OTG_HS; |
| 211 | HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); |
| 212 | |
| 213 | /* ULPI D1 D2 D3 D4 D5 D6 D7 */ |
| 214 | GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_10 | GPIO_PIN_11 | GPIO_PIN_12 | GPIO_PIN_13 | GPIO_PIN_5; |
| 215 | GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; |
| 216 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
| 217 | GPIO_InitStruct.Alternate = GPIO_AF10_OTG_HS; |
| 218 | HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); |
| 219 | |
| 220 | /* ULPI STP */ |
| 221 | GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_2; |
| 222 | GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; |
| 223 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
| 224 | GPIO_InitStruct.Alternate = GPIO_AF10_OTG_HS; |
| 225 | HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); |
| 226 | |
| 227 | /* NXT */ |
| 228 | GPIO_InitStruct.Pin = GPIO_PIN_4; |
| 229 | GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; |
| 230 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
| 231 | GPIO_InitStruct.Alternate = GPIO_AF10_OTG_HS; |
| 232 | HAL_GPIO_Init(GPIOH, &GPIO_InitStruct); |
| 233 | |
| 234 | /* ULPI DIR */ |
| 235 | GPIO_InitStruct.Pin = GPIO_PIN_11; |
| 236 | GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; |
| 237 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
| 238 | GPIO_InitStruct.Alternate = GPIO_AF10_OTG_HS; |
| 239 | HAL_GPIO_Init(GPIOI, &GPIO_InitStruct); |
| 240 | #endif // USB_HS_PHYC |
| 241 | |
| 242 | // Enable USB HS & ULPI Clocks |
| 243 | __HAL_RCC_USB_OTG_HS_ULPI_CLK_ENABLE(); |
| 244 | __HAL_RCC_USB_OTG_HS_CLK_ENABLE(); |
| 245 | |
| 246 | #if OTG_HS_VBUS_SENSE |
| 247 | #error OTG HS VBUS Sense enabled is not implemented |
| 248 | #else |
| 249 | // No VBUS sense |
| 250 | USB_OTG_HS->GCCFG &= ~USB_OTG_GCCFG_VBDEN; |
| 251 | |
| 252 | // B-peripheral session valid override enable |
| 253 | USB_OTG_HS->GOTGCTL |= USB_OTG_GOTGCTL_BVALOEN; |
| 254 | USB_OTG_HS->GOTGCTL |= USB_OTG_GOTGCTL_BVALOVAL; |
| 255 | #endif |
| 256 | |
| 257 | // Force device mode |
| 258 | USB_OTG_HS->GUSBCFG &= ~USB_OTG_GUSBCFG_FHMOD; |
| 259 | USB_OTG_HS->GUSBCFG |= USB_OTG_GUSBCFG_FDMOD; |
| 260 | |
| 261 | #endif // BOARD_DEVICE_RHPORT_NUM |
| 262 | |
| 263 | } |
| 264 | |
| 265 | //--------------------------------------------------------------------+ |
| 266 | // Board porting API |
| 267 | //--------------------------------------------------------------------+ |
| 268 | |
| 269 | void board_led_write(bool state) |
| 270 | { |
| 271 | HAL_GPIO_WritePin(LED_PORT, LED_PIN, state ? LED_STATE_ON : (1-LED_STATE_ON)); |
| 272 | } |
| 273 | |
| 274 | uint32_t board_button_read(void) |
| 275 | { |
| 276 | return HAL_GPIO_ReadPin(BUTTON_PORT, BUTTON_PIN); |
| 277 | } |
| 278 | |
| 279 | int board_uart_read(uint8_t* buf, int len) |
| 280 | { |
| 281 | (void) buf; (void) len; |
| 282 | return 0; |
| 283 | } |
| 284 | |
| 285 | int board_uart_write(void const * buf, int len) |
| 286 | { |
| 287 | HAL_UART_Transmit(&UartHandle, (uint8_t*)(uintptr_t) buf, len, 0xffff); |
| 288 | return len; |
| 289 | } |
| 290 | |
| 291 | #if CFG_TUSB_OS == OPT_OS_NONE |
| 292 | volatile uint32_t system_ticks = 0; |
| 293 | void SysTick_Handler (void) |
| 294 | { |
| 295 | system_ticks++; |
| 296 | } |
| 297 | |
| 298 | uint32_t board_millis(void) |
| 299 | { |
| 300 | return system_ticks; |
| 301 | } |
| 302 | #endif |
| 303 | |
| 304 | void HardFault_Handler (void) |
| 305 | { |
| 306 | asm("bkpt"); |
| 307 | } |
| 308 | |
| 309 | // Required by __libc_init_array in startup code if we are compiling using |
| 310 | // -nostdlib/-nostartfiles. |
| 311 | void _init(void) |
| 312 | { |
| 313 | |
| 314 | } |