blob: 42c657d5ea1b5f02668ddeda560847e5bd72cb70 [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#define LED_PORT GPIOB
35#define LED_PIN GPIO_PIN_2
36#define LED_STATE_ON 1
37
38#define BUTTON_PORT GPIOA
39#define BUTTON_PIN GPIO_PIN_0
40#define BUTTON_STATE_ACTIVE 1
41
42#define UART_DEV USART2
43#define UART_CLK_EN __HAL_RCC_USART2_CLK_ENABLE
44#define UART_GPIO_PORT GPIOD
45#define UART_GPIO_AF GPIO_AF7_USART2
46#define UART_TX_PIN GPIO_PIN_5
47#define UART_RX_PIN GPIO_PIN_6
48
49//--------------------------------------------------------------------+
50// RCC Clock
51//--------------------------------------------------------------------+
52
53/**
54 * @brief System Clock Configuration
55 * The system Clock is configured as follow :
56 *
57 * If define USB_USE_LSE_MSI_CLOCK enabled:
58 * System Clock source = PLL (MSI)
59 * SYSCLK(Hz) = 80000000
60 * HCLK(Hz) = 80000000
61 * AHB Prescaler = 1
62 * APB1 Prescaler = 1
63 * APB2 Prescaler = 2
64 * MSI Frequency(Hz) = 4800000
65 * LSE Frequency(Hz) = 32768
66 * PLL_M = 6
67 * PLL_N = 40
68 * PLL_P = 7
69 * PLL_Q = 4
70 * PLL_R = 4
71 * Flash Latency(WS) = 4
72 * @param None
73 * @retval None
74 */
75static inline void board_clock_init(void)
76{
77 RCC_ClkInitTypeDef RCC_ClkInitStruct;
78 RCC_OscInitTypeDef RCC_OscInitStruct;
79 RCC_PeriphCLKInitTypeDef PeriphClkInitStruct;
80
81 /* Enable the LSE Oscillator */
82 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSE;
83 RCC_OscInitStruct.LSEState = RCC_LSE_ON;
84 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
85 HAL_RCC_OscConfig(&RCC_OscInitStruct);
86
87 /* Enable the CSS interrupt in case LSE signal is corrupted or not present */
88 HAL_RCCEx_DisableLSECSS();
89
90 /* Set tick interrupt priority, default HAL value is intentionally invalid
91 and that prevents PLL initialization in HAL_RCC_OscConfig() */
92 HAL_InitTick((1UL << __NVIC_PRIO_BITS) - 1UL);
93
94 /* Enable MSI Oscillator and activate PLL with MSI as source */
95 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_MSI;
96 RCC_OscInitStruct.MSIState = RCC_MSI_ON;
97 RCC_OscInitStruct.MSICalibrationValue = RCC_MSICALIBRATION_DEFAULT;
98 RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_11;
99 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
100 RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_MSI;
101 RCC_OscInitStruct.PLL.PLLM = 6;
102 RCC_OscInitStruct.PLL.PLLN = 40;
103 RCC_OscInitStruct.PLL.PLLP = 7;
104 RCC_OscInitStruct.PLL.PLLQ = 4;
105 RCC_OscInitStruct.PLL.PLLR = 4;
106 HAL_RCC_OscConfig(&RCC_OscInitStruct);
107
108 /* Enable MSI Auto-calibration through LSE */
109 HAL_RCCEx_EnableMSIPLLMode();
110
111 /* Select MSI output as USB clock source */
112 PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_USB;
113 PeriphClkInitStruct.UsbClockSelection = RCC_USBCLKSOURCE_MSI;
114 HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct);
115
116 /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2
117 clocks dividers */
118 RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
119 RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
120 RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
121 RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
122 RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
123 HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4);
124}
125
126static inline void board_vbus_sense_init(void)
127{
128 // L476Disco use general GPIO PC11 for VBUS sensing instead of dedicated PA9 as others
129 // Disable VBUS Sense and force device mode
130 USB_OTG_FS->GCCFG &= ~USB_OTG_GCCFG_VBDEN;
131
132 USB_OTG_FS->GOTGCTL |= USB_OTG_GOTGCTL_BVALOEN | USB_OTG_GOTGCTL_BVALOVAL;
133}
134
135#ifdef __cplusplus
136 }
137#endif
138
139#endif /* BOARD_H_ */