blob: 73c5f83b9c6c0309ffb347994151b40eed0a58c9 [file] [log] [blame]
Austin Schuh41baf202022-01-01 14:33:40 -08001/*
2 * The MIT License (MIT)
3 *
4 * Copyright (c) 2020, 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// LED
35#define LED_PORT GPIOB
36#define LED_PIN GPIO_PIN_14
37#define LED_STATE_ON 0
38
39// Button
40#define BUTTON_PORT GPIOC
41#define BUTTON_PIN GPIO_PIN_13
42#define BUTTON_STATE_ACTIVE 1
43
44// UART Enable for STLink VCOM
45#define UART_DEV USART3
46#define UART_GPIO_PORT GPIOD
47#define UART_GPIO_AF GPIO_AF7_USART3
48#define UART_TX_PIN GPIO_PIN_8
49#define UART_RX_PIN GPIO_PIN_9
50
51//--------------------------------------------------------------------+
52// RCC Clock
53//--------------------------------------------------------------------+
54static inline void board_clock_init(void)
55{
56 RCC_ClkInitTypeDef RCC_ClkInitStruct;
57 RCC_OscInitTypeDef RCC_OscInitStruct;
58 RCC_PeriphCLKInitTypeDef PeriphClkInitStruct;
59
60 /* Enable Power Control clock */
61 __HAL_RCC_PWR_CLK_ENABLE();
62
63 /* The voltage scaling allows optimizing the power consumption when the
64 * device is clocked below the maximum system frequency, to update the
65 * voltage scaling value regarding system frequency refer to product
66 * datasheet. */
67 __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
68
69 /* Enable HSE Oscillator and activate PLL with HSE as source */
70 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
71 RCC_OscInitStruct.HSEState = RCC_HSE_ON;
72 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
73 RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
74 RCC_OscInitStruct.PLL.PLLM = HSE_VALUE/1000000;
75 RCC_OscInitStruct.PLL.PLLN = 200;
76 RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
77 RCC_OscInitStruct.PLL.PLLQ = 7;
78 RCC_OscInitStruct.PLL.PLLR = 2;
79 HAL_RCC_OscConfig(&RCC_OscInitStruct);
80
81 /* Select PLLSAI output as USB clock source */
82 PeriphClkInitStruct.PLLI2S.PLLI2SM = 8;
83 PeriphClkInitStruct.PLLI2S.PLLI2SQ = 4;
84 PeriphClkInitStruct.PLLI2S.PLLI2SN = 192;
85 PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_CK48;
86 PeriphClkInitStruct.Clk48ClockSelection = RCC_CK48CLKSOURCE_PLLI2SQ;
87 PeriphClkInitStruct.PLLI2SSelection = RCC_PLLI2SCLKSOURCE_PLLSRC;
88 PeriphClkInitStruct.PLLI2S.PLLI2SR = 7;
89 HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct);
90
91 /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2
92 * clocks dividers */
93 RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK |
94 RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
95
96 RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
97 RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
98 RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
99 RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
100 HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_3);
101
102 // Enable clocks for LED, Button, Uart
103 __HAL_RCC_GPIOB_CLK_ENABLE();
104 __HAL_RCC_GPIOC_CLK_ENABLE();
105 __HAL_RCC_GPIOD_CLK_ENABLE();
106 __HAL_RCC_USART3_CLK_ENABLE();
107}
108
109static inline void board_vbus_sense_init(void)
110{
111 // Enable VBUS sense (B device) via pin PA9
112 USB_OTG_FS->GCCFG |= USB_OTG_GCCFG_VBDEN;
113}
114
115#ifdef __cplusplus
116 }
117#endif
118
119#endif /* BOARD_H_ */