blob: 33552bc07c5c9d31a637882c4b65861d30494b45 [file] [log] [blame]
Austin Schuh41baf202022-01-01 14:33:40 -08001/*
2 * The MIT License (MIT)
3 *
4 * Copyright (c) 2019 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#include "../board.h"
28#include "stm32f3xx_hal.h"
29
30//--------------------------------------------------------------------+
31// Forward USB interrupt events to TinyUSB IRQ Handler
32//--------------------------------------------------------------------+
33
34// USB defaults to using interrupts 19, 20 and 42, however, this BSP sets the
35// SYSCFG_CFGR1.USB_IT_RMP bit remapping interrupts to 74, 75 and 76.
36
37// FIXME: Do all three need to be handled, or just the LP one?
38// USB high-priority interrupt (Channel 74): Triggered only by a correct
39// transfer event for isochronous and double-buffer bulk transfer to reach
40// the highest possible transfer rate.
41void USB_HP_IRQHandler(void)
42{
43 tud_int_handler(0);
44}
45
46// USB low-priority interrupt (Channel 75): Triggered by all USB events
47// (Correct transfer, USB reset, etc.). The firmware has to check the
48// interrupt source before serving the interrupt.
49void USB_LP_IRQHandler(void)
50{
51 tud_int_handler(0);
52}
53
54// USB wakeup interrupt (Channel 76): Triggered by the wakeup event from the USB
55// Suspend mode.
56void USBWakeUp_RMP_IRQHandler(void)
57{
58 tud_int_handler(0);
59}
60
61//--------------------------------------------------------------------+
62// MACRO TYPEDEF CONSTANT ENUM
63//--------------------------------------------------------------------+
64
65#define LED_PORT GPIOE
66#define LED_PIN GPIO_PIN_9
67#define LED_STATE_ON 1
68
69#define BUTTON_PORT GPIOA
70#define BUTTON_PIN GPIO_PIN_0
71#define BUTTON_STATE_ACTIVE 1
72
73
74/**
75 * @brief System Clock Configuration
76 * The system Clock is configured as follow :
77 * System Clock source = PLL (HSE)
78 * SYSCLK(Hz) = 72000000
79 * HCLK(Hz) = 72000000
80 * AHB Prescaler = 1
81 * APB1 Prescaler = 2
82 * APB2 Prescaler = 1
83 * HSE Frequency(Hz) = 8000000
84 * HSE PREDIV = 1
85 * PLLMUL = RCC_PLL_MUL9 (9)
86 * Flash Latency(WS) = 2
87 * @param None
88 * @retval None
89 */
90static void SystemClock_Config(void)
91{
92 RCC_ClkInitTypeDef RCC_ClkInitStruct;
93 RCC_OscInitTypeDef RCC_OscInitStruct;
94 RCC_PeriphCLKInitTypeDef RCC_PeriphClkInit;
95
96 /* Enable HSE Oscillator and activate PLL with HSE as source */
97 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
98 RCC_OscInitStruct.HSEState = RCC_HSE_ON;
99 RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
100 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
101 RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
102 RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
103 HAL_RCC_OscConfig(&RCC_OscInitStruct);
104
105 /* Configures the USB clock */
106 HAL_RCCEx_GetPeriphCLKConfig(&RCC_PeriphClkInit);
107 RCC_PeriphClkInit.USBClockSelection = RCC_USBCLKSOURCE_PLL_DIV1_5;
108 HAL_RCCEx_PeriphCLKConfig(&RCC_PeriphClkInit);
109
110 /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2
111 clocks dividers */
112 RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
113 RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
114 RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
115 RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
116 RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
117 HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2);
118
119 /* Enable Power Clock */
120 __HAL_RCC_PWR_CLK_ENABLE();
121}
122
123void board_init(void)
124{
125 SystemClock_Config();
126
127 #if CFG_TUSB_OS == OPT_OS_NONE
128 // 1ms tick timer
129 SysTick_Config(SystemCoreClock / 1000);
130 #endif
131
132 // Remap the USB interrupts
133 __HAL_RCC_SYSCFG_CLK_ENABLE();
134 __HAL_REMAPINTERRUPT_USB_ENABLE();
135
136 // LED
137 __HAL_RCC_GPIOE_CLK_ENABLE();
138 GPIO_InitTypeDef GPIO_InitStruct;
139 GPIO_InitStruct.Pin = LED_PIN;
140 GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
141 GPIO_InitStruct.Pull = GPIO_PULLUP;
142 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
143 HAL_GPIO_Init(LED_PORT, &GPIO_InitStruct);
144
145 // Button
146 __HAL_RCC_GPIOA_CLK_ENABLE();
147 GPIO_InitStruct.Pin = BUTTON_PIN;
148 GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
149 GPIO_InitStruct.Pull = GPIO_PULLDOWN;
150 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
151 HAL_GPIO_Init(BUTTON_PORT, &GPIO_InitStruct);
152
153 /* Configure USB DM and DP pins */
154 __HAL_RCC_GPIOA_CLK_ENABLE();
155 GPIO_InitStruct.Pin = (GPIO_PIN_11 | GPIO_PIN_12);
156 GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
157 GPIO_InitStruct.Pull = GPIO_NOPULL;
158 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
159 GPIO_InitStruct.Alternate = GPIO_AF14_USB;
160 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
161
162 // Enable USB clock
163 __HAL_RCC_USB_CLK_ENABLE();
164}
165
166//--------------------------------------------------------------------+
167// Board porting API
168//--------------------------------------------------------------------+
169
170void board_led_write(bool state)
171{
172 HAL_GPIO_WritePin(LED_PORT, LED_PIN, state ? LED_STATE_ON : (1-LED_STATE_ON));
173}
174
175uint32_t board_button_read(void)
176{
177 return BUTTON_STATE_ACTIVE == HAL_GPIO_ReadPin(BUTTON_PORT, BUTTON_PIN);
178}
179
180int board_uart_read(uint8_t* buf, int len)
181{
182 (void) buf; (void) len;
183 return 0;
184}
185
186int board_uart_write(void const * buf, int len)
187{
188 (void) buf; (void) len;
189 return 0;
190}
191
192#if CFG_TUSB_OS == OPT_OS_NONE
193volatile uint32_t system_ticks = 0;
194void SysTick_Handler (void)
195{
196 system_ticks++;
197}
198
199uint32_t board_millis(void)
200{
201 return system_ticks;
202}
203#endif
204
205void HardFault_Handler (void)
206{
207 asm("bkpt");
208}
209
210// Required by __libc_init_array in startup code if we are compiling using
211// -nostdlib/-nostartfiles.
212void _init(void)
213{
214
215}