blob: 461dc61a188d0c75687ea5450940ab528662382f [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 "stm32g4xx_hal.h"
28#include "bsp/board.h"
29#include "board.h"
30
31//--------------------------------------------------------------------+
32// Forward USB interrupt events to TinyUSB IRQ Handler
33//--------------------------------------------------------------------+
34void USB_HP_IRQHandler(void)
35{
36 tud_int_handler(0);
37}
38
39void USB_LP_IRQHandler(void)
40{
41 tud_int_handler(0);
42}
43
44void USBWakeUp_IRQHandler(void)
45{
46 tud_int_handler(0);
47}
48
49//--------------------------------------------------------------------+
50// MACRO TYPEDEF CONSTANT ENUM
51//--------------------------------------------------------------------+
52UART_HandleTypeDef UartHandle;
53
54void board_init(void)
55{
56 board_clock_init();
57
58 // Enable All GPIOs clocks
59 __HAL_RCC_GPIOA_CLK_ENABLE();
60 __HAL_RCC_GPIOB_CLK_ENABLE();
61 __HAL_RCC_GPIOC_CLK_ENABLE();
62 __HAL_RCC_GPIOD_CLK_ENABLE();
63 __HAL_RCC_GPIOE_CLK_ENABLE();
64 __HAL_RCC_GPIOG_CLK_ENABLE();
65
66 UART_CLK_EN();
67
68#if CFG_TUSB_OS == OPT_OS_NONE
69 // 1ms tick timer
70 SysTick_Config(SystemCoreClock / 1000);
71#elif CFG_TUSB_OS == OPT_OS_FREERTOS
72 // Explicitly disable systick to prevent its ISR runs before scheduler start
73 SysTick->CTRL &= ~1U;
74
75 // If freeRTOS is used, IRQ priority is limit by max syscall ( smaller is higher )
76 NVIC_SetPriority(OTG_FS_IRQn, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY );
77#endif
78
79 GPIO_InitTypeDef GPIO_InitStruct;
80
81 // LED
82 GPIO_InitStruct.Pin = LED_PIN;
83 GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
84 GPIO_InitStruct.Pull = GPIO_PULLUP;
85 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
86 HAL_GPIO_Init(LED_PORT, &GPIO_InitStruct);
87
88 board_led_write(false);
89
90 // Button
91 GPIO_InitStruct.Pin = BUTTON_PIN;
92 GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
93 GPIO_InitStruct.Pull = BUTTON_STATE_ACTIVE ? GPIO_PULLDOWN : GPIO_PULLUP;
94 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
95 HAL_GPIO_Init(BUTTON_PORT, &GPIO_InitStruct);
96
97#ifdef UART_DEV
98 // UART
99 GPIO_InitStruct.Pin = UART_TX_PIN | UART_RX_PIN;
100 GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
101 GPIO_InitStruct.Pull = GPIO_PULLUP;
102 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
103 GPIO_InitStruct.Alternate = UART_GPIO_AF;
104 HAL_GPIO_Init(UART_GPIO_PORT, &GPIO_InitStruct);
105
106 UartHandle = (UART_HandleTypeDef){
107 .Instance = UART_DEV,
108 .Init.BaudRate = CFG_BOARD_UART_BAUDRATE,
109 .Init.WordLength = UART_WORDLENGTH_8B,
110 .Init.StopBits = UART_STOPBITS_1,
111 .Init.Parity = UART_PARITY_NONE,
112 .Init.HwFlowCtl = UART_HWCONTROL_NONE,
113 .Init.Mode = UART_MODE_TX_RX,
114 .Init.OverSampling = UART_OVERSAMPLING_16
115 };
116 HAL_UART_Init(&UartHandle);
117#endif
118
119 // USB Pins TODO double check USB clock and pin setup
120 // Configure USB DM and DP pins. This is optional, and maintained only for user guidance.
121 GPIO_InitStruct.Pin = (GPIO_PIN_11 | GPIO_PIN_12);
122 GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
123 GPIO_InitStruct.Pull = GPIO_NOPULL;
124 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
125 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
126
127 __HAL_RCC_USB_CLK_ENABLE();
128
129 board_vbus_sense_init();
130}
131
132//--------------------------------------------------------------------+
133// Board porting API
134//--------------------------------------------------------------------+
135
136void board_led_write(bool state)
137{
138 HAL_GPIO_WritePin(LED_PORT, LED_PIN, state ? LED_STATE_ON : (1-LED_STATE_ON));
139}
140
141uint32_t board_button_read(void)
142{
143 return BUTTON_STATE_ACTIVE == HAL_GPIO_ReadPin(BUTTON_PORT, BUTTON_PIN);
144}
145
146int board_uart_read(uint8_t* buf, int len)
147{
148 (void) buf; (void) len;
149 return 0;
150}
151
152int board_uart_write(void const * buf, int len)
153{
154#ifdef UART_DEV
155 HAL_UART_Transmit(&UartHandle, (uint8_t*)(uintptr_t) buf, len, 0xffff);
156 return len;
157#else
158 (void) buf; (void) len; (void) UartHandle;
159 return 0;
160#endif
161}
162
163#if CFG_TUSB_OS == OPT_OS_NONE
164volatile uint32_t system_ticks = 0;
165void SysTick_Handler (void)
166{
167 system_ticks++;
168}
169
170uint32_t board_millis(void)
171{
172 return system_ticks;
173}
174#endif
175
176void HardFault_Handler (void)
177{
178 asm("bkpt");
179}
180
181// Required by __libc_init_array in startup code if we are compiling using
182// -nostdlib/-nostartfiles.
183void _init(void)
184{
185
186}