blob: 84976b4e44548ec2c6029cf1936b471a62cbb3e0 [file] [log] [blame]
Austin Schuh41baf202022-01-01 14:33:40 -08001/*
2 * The MIT License (MIT)
3 *
4 * Copyright (c) 2019
5 * William D. Jones (thor0505@comcast.net),
6 * Ha Thach (tinyusb.org)
7 * Uwe Bonnes (bon@elektron.ikp.physik.tu-darmstadt.de
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a copy
10 * of this software and associated documentation files (the "Software"), to deal
11 * in the Software without restriction, including without limitation the rights
12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 * copies of the Software, and to permit persons to whom the Software is
14 * furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 * THE SOFTWARE.
26 *
27 * This file is part of the TinyUSB stack.
28 */
29
30#include "stm32h7xx_hal.h"
31#include "bsp/board.h"
32#include "board.h"
33
34//--------------------------------------------------------------------+
35// Forward USB interrupt events to TinyUSB IRQ Handler
36//--------------------------------------------------------------------+
37
38// Despite being call USB2_OTG
39// OTG_FS is marked as RHPort0 by TinyUSB to be consistent across stm32 port
40void OTG_FS_IRQHandler(void)
41{
42 tud_int_handler(0);
43}
44
45// Despite being call USB2_OTG
46// OTG_HS is marked as RHPort1 by TinyUSB to be consistent across stm32 port
47void OTG_HS_IRQHandler(void)
48{
49 tud_int_handler(1);
50}
51
52
53//--------------------------------------------------------------------+
54// MACRO TYPEDEF CONSTANT ENUM
55//--------------------------------------------------------------------+
56
57UART_HandleTypeDef UartHandle;
58
59void board_init(void)
60{
61 board_stm32h7_clock_init();
62
63 // Enable All GPIOs clocks
64 __HAL_RCC_GPIOA_CLK_ENABLE();
65 __HAL_RCC_GPIOB_CLK_ENABLE(); // USB ULPI NXT
66 __HAL_RCC_GPIOC_CLK_ENABLE(); // USB ULPI NXT
67 __HAL_RCC_GPIOD_CLK_ENABLE();
68 __HAL_RCC_GPIOE_CLK_ENABLE();
69 __HAL_RCC_GPIOE_CLK_ENABLE();
70 __HAL_RCC_GPIOG_CLK_ENABLE();
71 __HAL_RCC_GPIOH_CLK_ENABLE(); // USB ULPI NXT
72 __HAL_RCC_GPIOI_CLK_ENABLE(); // USB ULPI NXT
73 __HAL_RCC_GPIOJ_CLK_ENABLE();
74
75 // Enable UART Clock
76 UART_CLK_EN();
77
78#if CFG_TUSB_OS == OPT_OS_NONE
79 // 1ms tick timer
80 SysTick_Config(SystemCoreClock / 1000);
81
82#elif CFG_TUSB_OS == OPT_OS_FREERTOS
83 // Explicitly disable systick to prevent its ISR runs before scheduler start
84 SysTick->CTRL &= ~1U;
85
86 // If freeRTOS is used, IRQ priority is limit by max syscall ( smaller is higher )
87 NVIC_SetPriority(OTG_FS_IRQn, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY );
88 NVIC_SetPriority(OTG_HS_IRQn, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY );
89#endif
90
91 GPIO_InitTypeDef GPIO_InitStruct;
92
93 // LED
94 GPIO_InitStruct.Pin = LED_PIN;
95 GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
96 GPIO_InitStruct.Pull = GPIO_PULLUP;
97 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
98 HAL_GPIO_Init(LED_PORT, &GPIO_InitStruct);
99
100 // Button
101 GPIO_InitStruct.Pin = BUTTON_PIN;
102 GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
103 GPIO_InitStruct.Pull = GPIO_NOPULL;
104 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
105 HAL_GPIO_Init(BUTTON_PORT, &GPIO_InitStruct);
106
107 // Uart
108 GPIO_InitStruct.Pin = UART_TX_PIN | UART_RX_PIN;
109 GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
110 GPIO_InitStruct.Pull = GPIO_PULLUP;
111 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
112 GPIO_InitStruct.Alternate = UART_GPIO_AF;
113 HAL_GPIO_Init(UART_GPIO_PORT, &GPIO_InitStruct);
114
115 UartHandle.Instance = UART_DEV;
116 UartHandle.Init.BaudRate = CFG_BOARD_UART_BAUDRATE;
117 UartHandle.Init.WordLength = UART_WORDLENGTH_8B;
118 UartHandle.Init.StopBits = UART_STOPBITS_1;
119 UartHandle.Init.Parity = UART_PARITY_NONE;
120 UartHandle.Init.HwFlowCtl = UART_HWCONTROL_NONE;
121 UartHandle.Init.Mode = UART_MODE_TX_RX;
122 UartHandle.Init.OverSampling = UART_OVERSAMPLING_16;
123 HAL_UART_Init(&UartHandle);
124
125#if BOARD_DEVICE_RHPORT_NUM == 0
126 // Despite being call USB2_OTG
127 // OTG_FS is marked as RHPort0 by TinyUSB to be consistent across stm32 port
128 // PA9 VUSB, PA10 ID, PA11 DM, PA12 DP
129
130 // Configure DM DP Pins
131 GPIO_InitStruct.Pin = GPIO_PIN_11 | GPIO_PIN_12;
132 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
133 GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
134 GPIO_InitStruct.Pull = GPIO_NOPULL;
135 GPIO_InitStruct.Alternate = GPIO_AF10_OTG2_HS;
136 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
137
138 // This for ID line debug
139 GPIO_InitStruct.Pin = GPIO_PIN_10;
140 GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
141 GPIO_InitStruct.Pull = GPIO_PULLUP;
142 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
143 GPIO_InitStruct.Alternate = GPIO_AF10_OTG2_HS;
144 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
145
146 // https://community.st.com/s/question/0D50X00009XkYZLSA3/stm32h7-nucleo-usb-fs-cdc
147 // TODO: Board init actually works fine without this line.
148 HAL_PWREx_EnableUSBVoltageDetector();
149 __HAL_RCC_USB2_OTG_FS_CLK_ENABLE();
150
151#if OTG_FS_VBUS_SENSE
152 // Configure VBUS Pin
153 GPIO_InitStruct.Pin = GPIO_PIN_9;
154 GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
155 GPIO_InitStruct.Pull = GPIO_NOPULL;
156 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
157
158 // Enable VBUS sense (B device) via pin PA9
159 USB_OTG_FS->GCCFG |= USB_OTG_GCCFG_VBDEN;
160#else
161 // Disable VBUS sense (B device) via pin PA9
162 USB_OTG_FS->GCCFG &= ~USB_OTG_GCCFG_VBDEN;
163
164 // B-peripheral session valid override enable
165 USB_OTG_FS->GOTGCTL |= USB_OTG_GOTGCTL_BVALOEN;
166 USB_OTG_FS->GOTGCTL |= USB_OTG_GOTGCTL_BVALOVAL;
167#endif // vbus sense
168
169#elif BOARD_DEVICE_RHPORT_NUM == 1
170 // Despite being call USB2_OTG
171 // OTG_HS is marked as RHPort1 by TinyUSB to be consistent across stm32 port
172
173 struct {
174 GPIO_TypeDef* port;
175 uint32_t pin;
176 } const ulpi_pins[] =
177 {
178 ULPI_PINS
179 };
180
181 for (uint8_t i=0; i < sizeof(ulpi_pins)/sizeof(ulpi_pins[0]); i++)
182 {
183 GPIO_InitStruct.Pin = ulpi_pins[i].pin;
184 GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
185 GPIO_InitStruct.Pull = GPIO_NOPULL;
186 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
187 GPIO_InitStruct.Alternate = GPIO_AF10_OTG2_HS;
188 HAL_GPIO_Init(ulpi_pins[i].port, &GPIO_InitStruct);
189 }
190
191 // Enable USB HS & ULPI Clocks
192 __HAL_RCC_USB1_OTG_HS_ULPI_CLK_ENABLE();
193 __HAL_RCC_USB1_OTG_HS_CLK_ENABLE();
194
195#if OTG_HS_VBUS_SENSE
196 #error OTG HS VBUS Sense enabled is not implemented
197#else
198 // No VBUS sense
199 USB_OTG_HS->GCCFG &= ~USB_OTG_GCCFG_VBDEN;
200
201 // B-peripheral session valid override enable
202 USB_OTG_HS->GOTGCTL |= USB_OTG_GOTGCTL_BVALOEN;
203 USB_OTG_HS->GOTGCTL |= USB_OTG_GOTGCTL_BVALOVAL;
204#endif
205
206 // Force device mode
207 USB_OTG_HS->GUSBCFG &= ~USB_OTG_GUSBCFG_FHMOD;
208 USB_OTG_HS->GUSBCFG |= USB_OTG_GUSBCFG_FDMOD;
209
210 HAL_PWREx_EnableUSBVoltageDetector();
211
212 // For waveshare openh743 ULPI PHY reset walkaround
213 board_stm32h7_post_init();
214#endif // rhport = 1
215
216}
217
218//--------------------------------------------------------------------+
219// Board porting API
220//--------------------------------------------------------------------+
221
222void board_led_write(bool state)
223{
224 HAL_GPIO_WritePin(LED_PORT, LED_PIN, state ? LED_STATE_ON : (1-LED_STATE_ON));
225}
226
227uint32_t board_button_read(void)
228{
229 return (BUTTON_STATE_ACTIVE == HAL_GPIO_ReadPin(BUTTON_PORT, BUTTON_PIN)) ? 1 : 0;
230}
231
232int board_uart_read(uint8_t* buf, int len)
233{
234 (void) buf; (void) len;
235 return 0;
236}
237
238int board_uart_write(void const * buf, int len)
239{
240 HAL_UART_Transmit(&UartHandle, (uint8_t*)(uintptr_t) buf, len, 0xffff);
241 return len;
242}
243
244
245#if CFG_TUSB_OS == OPT_OS_NONE
246volatile uint32_t system_ticks = 0;
247void SysTick_Handler(void)
248{
249 system_ticks++;
250}
251
252uint32_t board_millis(void)
253{
254 return system_ticks;
255}
256#endif
257
258void HardFault_Handler(void)
259{
260 asm("bkpt");
261}
262
263// Required by __libc_init_array in startup code if we are compiling using
264// -nostdlib/-nostartfiles.
265void _init(void)
266{
267
268}