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