blob: c15be98e5afe8fb4f91b3b46d0dae9bd7326f5ae [file] [log] [blame]
Austin Schuh41baf202022-01-01 14:33:40 -08001/*
2 * The MIT License (MIT)
3 *
4 * Copyright (c) 2019 William D. Jones (thor0505@comcast.net),
5 * Ha Thach (tinyusb.org)
6 * Uwe Bonnes (bon@elektron.ikp.physik.tu-darmstadt.de)
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 *
26 * This file is part of the TinyUSB stack.
27 */
28
29#include "stm32l4xx_hal.h"
30#include "bsp/board.h"
31#include "board.h"
32
33//--------------------------------------------------------------------+
34// Forward USB interrupt events to TinyUSB IRQ Handler
35//--------------------------------------------------------------------+
36void OTG_FS_IRQHandler(void)
37{
38 tud_int_handler(0);
39}
40
41//--------------------------------------------------------------------+
42// MACRO TYPEDEF CONSTANT ENUM
43//--------------------------------------------------------------------+
44
45UART_HandleTypeDef UartHandle;
46
47void board_init(void)
48{
49 board_clock_init();
50
51 // Enable All GPIOs clocks
52 __HAL_RCC_GPIOA_CLK_ENABLE();
53 __HAL_RCC_GPIOB_CLK_ENABLE();
54 __HAL_RCC_GPIOC_CLK_ENABLE();
55 __HAL_RCC_GPIOD_CLK_ENABLE();
56 __HAL_RCC_GPIOE_CLK_ENABLE();
57 __HAL_RCC_GPIOF_CLK_ENABLE();
58 __HAL_RCC_GPIOG_CLK_ENABLE();
59 __HAL_RCC_GPIOH_CLK_ENABLE();
60 UART_CLK_EN();
61
62#if CFG_TUSB_OS == OPT_OS_NONE
63 // 1ms tick timer
64 SysTick_Config(SystemCoreClock / 1000);
65#elif CFG_TUSB_OS == OPT_OS_FREERTOS
66 // If freeRTOS is used, IRQ priority is limit by max syscall ( smaller is higher )
67 //NVIC_SetPriority(USB0_IRQn, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY );
68#endif
69
70 /* Enable USB power on Pwrctrl CR2 register */
71 /* Enable Power Clock*/
72 __HAL_RCC_PWR_CLK_ENABLE();
73
74#if defined(PWR_CR5_R1MODE)
75 /* Enable voltage range 1 boost mode for frequency above 80 Mhz */
76 HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1_BOOST);
77#endif
78
79 /* Enable USB power on Pwrctrl CR2 register */
80 HAL_PWREx_EnableVddUSB();
81
82 GPIO_InitTypeDef GPIO_InitStruct;
83
84 // LED
85 GPIO_InitStruct.Pin = LED_PIN;
86 GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
87 GPIO_InitStruct.Pull = GPIO_PULLUP;
88 HAL_GPIO_Init(LED_PORT, &GPIO_InitStruct);
89
90 // Button
91 GPIO_InitStruct.Pin = BUTTON_PIN;
92 GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
93 GPIO_InitStruct.Pull = GPIO_NOPULL;
94 HAL_GPIO_Init(BUTTON_PORT, &GPIO_InitStruct);
95
96 // IOSV bit MUST be set to access GPIO port G[2:15] */
97 __HAL_RCC_PWR_CLK_ENABLE();
98 HAL_PWREx_EnableVddIO2();
99
100 // Uart
101 GPIO_InitStruct.Pin = UART_TX_PIN | UART_RX_PIN;
102 GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
103 GPIO_InitStruct.Pull = GPIO_PULLUP;
104 GPIO_InitStruct.Alternate = UART_GPIO_AF;
105 HAL_GPIO_Init(UART_GPIO_PORT, &GPIO_InitStruct);
106
107 UartHandle.Instance = UART_DEV;
108 UartHandle.Init.BaudRate = CFG_BOARD_UART_BAUDRATE;
109 UartHandle.Init.WordLength = UART_WORDLENGTH_8B;
110 UartHandle.Init.StopBits = UART_STOPBITS_1;
111 UartHandle.Init.Parity = UART_PARITY_NONE;
112 UartHandle.Init.HwFlowCtl = UART_HWCONTROL_NONE;
113 UartHandle.Init.Mode = UART_MODE_TX_RX;
114 UartHandle.Init.OverSampling = UART_OVERSAMPLING_16;
115 UartHandle.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
116 //UartHandle.Init.ClockPrescaler = UART_PRESCALER_DIV1;
117 UartHandle.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
118
119 HAL_UART_Init(&UartHandle);
120
121 /* Configure USB FS GPIOs */
122 /* Configure DM DP Pins */
123 GPIO_InitStruct.Pin = (GPIO_PIN_11 | GPIO_PIN_12);
124 GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
125 GPIO_InitStruct.Pull = GPIO_NOPULL;
126 GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
127 GPIO_InitStruct.Alternate = GPIO_AF10_OTG_FS;
128 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
129
130 /* Configure VBUS Pin */
131 GPIO_InitStruct.Pin = GPIO_PIN_9;
132 GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
133 GPIO_InitStruct.Pull = GPIO_NOPULL;
134 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
135
136 /* Configure ID pin */
137 GPIO_InitStruct.Pin = GPIO_PIN_10;
138 GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
139 GPIO_InitStruct.Pull = GPIO_PULLUP;
140 GPIO_InitStruct.Alternate = GPIO_AF10_OTG_FS;
141 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
142
143 /* Enable USB FS Clocks */
144 __HAL_RCC_USB_OTG_FS_CLK_ENABLE();
145
146 board_vbus_sense_init();
147}
148
149//--------------------------------------------------------------------+
150// Board porting API
151//--------------------------------------------------------------------+
152
153void board_led_write(bool state)
154{
155 HAL_GPIO_WritePin(LED_PORT, LED_PIN, state ? LED_STATE_ON : (1-LED_STATE_ON));
156}
157
158uint32_t board_button_read(void)
159{
160 return BUTTON_STATE_ACTIVE == HAL_GPIO_ReadPin(BUTTON_PORT, BUTTON_PIN);
161}
162
163int board_uart_read(uint8_t* buf, int len)
164{
165 (void) buf; (void) len;
166 return 0;
167}
168
169int board_uart_write(void const * buf, int len)
170{
171 HAL_UART_Transmit(&UartHandle, (uint8_t*)(uintptr_t) buf, len, 0xffff);
172 return len;
173}
174
175#if CFG_TUSB_OS == OPT_OS_NONE
176volatile uint32_t system_ticks = 0;
177void SysTick_Handler (void)
178{
179 system_ticks++;
180}
181
182uint32_t board_millis(void)
183{
184 return system_ticks;
185}
186#endif
187
188void HardFault_Handler (void)
189{
190 asm("bkpt");
191}
192
193// Required by __libc_init_array in startup code if we are compiling using
194// -nostdlib/-nostartfiles.
195void _init(void)
196{
197
198}