blob: 06148c875138a2fce76c81025a09c20df8bf5ab2 [file] [log] [blame]
Austin Schuh41baf202022-01-01 14:33:40 -08001/*
2 * The MIT License (MIT)
3 *
4 * Copyright (c) 2021, Ha Thach (tinyusb.org)
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 *
24 * This file is part of the TinyUSB stack.
25 */
26
27#ifndef BOARD_H_
28#define BOARD_H_
29
30#ifdef __cplusplus
31 extern "C" {
32#endif
33
34#define LED_PORT GPIOB
35#define LED_PIN GPIO_PIN_0
36#define LED_STATE_ON 1
37
38#define BUTTON_PORT GPIOC
39#define BUTTON_PIN GPIO_PIN_13
40#define BUTTON_STATE_ACTIVE 1
41
42#define UART_DEV USART3
43#define UART_CLK_EN __HAL_RCC_USART3_CLK_ENABLE
44#define UART_GPIO_PORT GPIOD
45#define UART_GPIO_AF GPIO_AF7_USART3
46#define UART_TX_PIN GPIO_PIN_8
47#define UART_RX_PIN GPIO_PIN_9
48
49// VBUS Sense detection
50#define OTG_FS_VBUS_SENSE 1
51#define OTG_HS_VBUS_SENSE 0
52
53//--------------------------------------------------------------------+
54// RCC Clock
55//--------------------------------------------------------------------+
56static inline void board_stm32h7_clock_init(void)
57{
58 RCC_ClkInitTypeDef RCC_ClkInitStruct;
59 RCC_OscInitTypeDef RCC_OscInitStruct;
60
61 /* The PWR block is always enabled on the H7 series- there is no clock
62 enable. For now, use the default VOS3 scale mode (lowest) and limit clock
63 frequencies to avoid potential current draw problems from bus
64 power when using the max clock speeds throughout the chip. */
65
66 /* Enable HSE Oscillator and activate PLL1 with HSE as source */
67 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
68 RCC_OscInitStruct.HSEState = RCC_HSE_ON;
69 RCC_OscInitStruct.HSIState = RCC_HSI_OFF;
70 RCC_OscInitStruct.CSIState = RCC_CSI_OFF;
71 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
72 RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
73 RCC_OscInitStruct.PLL.PLLM = HSE_VALUE/1000000;
74 RCC_OscInitStruct.PLL.PLLN = 336;
75 RCC_OscInitStruct.PLL.PLLP = 2;
76 RCC_OscInitStruct.PLL.PLLQ = 7;
77 RCC_OscInitStruct.PLL.PLLR = 2; /* Unused */
78 RCC_OscInitStruct.PLL.PLLRGE = RCC_PLL1VCIRANGE_0;
79 RCC_OscInitStruct.PLL.PLLVCOSEL = RCC_PLL1VCOMEDIUM;
80 RCC_OscInitStruct.PLL.PLLFRACN = 0;
81 HAL_RCC_OscConfig(&RCC_OscInitStruct);
82
83 RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | \
84 RCC_CLOCKTYPE_D1PCLK1 | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2 | \
85 RCC_CLOCKTYPE_D3PCLK1);
86 RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
87 RCC_ClkInitStruct.SYSCLKDivider = RCC_SYSCLK_DIV1;
88 RCC_ClkInitStruct.AHBCLKDivider = RCC_HCLK_DIV1;
89
90 /* Unlike on the STM32F4 family, it appears the maximum APB frequencies are
91 device-dependent- 120 MHz for this board according to Figure 2 of
92 the datasheet. Dividing by half will be safe for now. */
93 RCC_ClkInitStruct.APB3CLKDivider = RCC_APB3_DIV2;
94 RCC_ClkInitStruct.APB1CLKDivider = RCC_APB1_DIV2;
95 RCC_ClkInitStruct.APB2CLKDivider = RCC_APB2_DIV2;
96 RCC_ClkInitStruct.APB4CLKDivider = RCC_APB4_DIV2;
97
98 /* 4 wait states required for 168MHz and VOS3. */
99 HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4);
100
101 /* Like on F4, on H7, USB's actual peripheral clock and bus clock are
102 separate. However, the main system PLL (PLL1) doesn't have a direct
103 connection to the USB peripheral clock to generate 48 MHz, so we do this
104 dance. This will connect PLL1's Q output to the USB peripheral clock. */
105 RCC_PeriphCLKInitTypeDef RCC_PeriphCLKInitStruct;
106
107 RCC_PeriphCLKInitStruct.PeriphClockSelection = RCC_PERIPHCLK_USB;
108 RCC_PeriphCLKInitStruct.UsbClockSelection = RCC_USBCLKSOURCE_PLL;
109 HAL_RCCEx_PeriphCLKConfig(&RCC_PeriphCLKInitStruct);
110}
111
112static inline void board_stm32h7_post_init(void)
113{
114 // For this board does nothing
115}
116
117
118#ifdef __cplusplus
119 }
120#endif
121
122#endif