blob: 8de3147e270e57bfa1a683262dc858fc1a172418 [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 "stm32f0xx_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_IRQHandler(void)
35{
36 tud_int_handler(0);
37}
38
39//--------------------------------------------------------------------+
40// MACRO TYPEDEF CONSTANT ENUM
41//--------------------------------------------------------------------+
42UART_HandleTypeDef UartHandle;
43
44void board_init(void)
45{
46 board_stm32f0_clock_init();
47
48 // Enable All GPIOs clocks
49 __HAL_RCC_GPIOA_CLK_ENABLE();
50 __HAL_RCC_GPIOB_CLK_ENABLE();
51 __HAL_RCC_GPIOC_CLK_ENABLE();
52 __HAL_RCC_GPIOD_CLK_ENABLE();
53 __HAL_RCC_GPIOF_CLK_ENABLE();
54
55 // Enable UART Clock
56 UART_CLK_EN();
57
58#if CFG_TUSB_OS == OPT_OS_NONE
59 // 1ms tick timer
60 SysTick_Config(SystemCoreClock / 1000);
61
62#elif CFG_TUSB_OS == OPT_OS_FREERTOS
63 // Explicitly disable systick to prevent its ISR runs before scheduler start
64 SysTick->CTRL &= ~1U;
65
66 // If freeRTOS is used, IRQ priority is limit by max syscall ( smaller is higher )
67 NVIC_SetPriority(USB_IRQn, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY);
68#endif
69
70 // LED
71 GPIO_InitTypeDef GPIO_InitStruct;
72 GPIO_InitStruct.Pin = LED_PIN;
73 GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
74 GPIO_InitStruct.Pull = GPIO_PULLUP;
75 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
76 HAL_GPIO_Init(LED_PORT, &GPIO_InitStruct);
77
78 // Button
79 GPIO_InitStruct.Pin = BUTTON_PIN;
80 GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
81 GPIO_InitStruct.Pull = GPIO_PULLDOWN;
82 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
83 HAL_GPIO_Init(BUTTON_PORT, &GPIO_InitStruct);
84
85 // Uart
86 GPIO_InitStruct.Pin = UART_TX_PIN | UART_RX_PIN;
87 GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
88 GPIO_InitStruct.Pull = GPIO_PULLUP;
89 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
90 GPIO_InitStruct.Alternate = UART_GPIO_AF;
91 HAL_GPIO_Init(UART_GPIO_PORT, &GPIO_InitStruct);
92
93 UartHandle.Instance = UART_DEV;
94 UartHandle.Init.BaudRate = CFG_BOARD_UART_BAUDRATE;
95 UartHandle.Init.WordLength = UART_WORDLENGTH_8B;
96 UartHandle.Init.StopBits = UART_STOPBITS_1;
97 UartHandle.Init.Parity = UART_PARITY_NONE;
98 UartHandle.Init.HwFlowCtl = UART_HWCONTROL_NONE;
99 UartHandle.Init.Mode = UART_MODE_TX_RX;
100 UartHandle.Init.OverSampling = UART_OVERSAMPLING_16;
101 HAL_UART_Init(&UartHandle);
102
103 // USB Pins
104 // Configure USB DM and DP pins. This is optional, and maintained only for user guidance.
105 GPIO_InitStruct.Pin = (GPIO_PIN_11 | GPIO_PIN_12);
106 GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
107 GPIO_InitStruct.Pull = GPIO_NOPULL;
108 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
109 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
110
111 // USB Clock enable
112 __HAL_RCC_USB_CLK_ENABLE();
113}
114
115//--------------------------------------------------------------------+
116// Board porting API
117//--------------------------------------------------------------------+
118
119void board_led_write(bool state)
120{
121 HAL_GPIO_WritePin(LED_PORT, LED_PIN, state ? LED_STATE_ON : (1-LED_STATE_ON));
122}
123
124uint32_t board_button_read(void)
125{
126 return BUTTON_STATE_ACTIVE == HAL_GPIO_ReadPin(BUTTON_PORT, BUTTON_PIN);
127}
128
129int board_uart_read(uint8_t* buf, int len)
130{
131 (void) buf; (void) len;
132 return 0;
133}
134
135int board_uart_write(void const * buf, int len)
136{
137 HAL_UART_Transmit(&UartHandle, (uint8_t*)(uintptr_t) buf, len, 0xffff);
138 return len;
139}
140
141#if CFG_TUSB_OS == OPT_OS_NONE
142volatile uint32_t system_ticks = 0;
143void SysTick_Handler (void)
144{
145 system_ticks++;
146}
147
148uint32_t board_millis(void)
149{
150 return system_ticks;
151}
152#endif
153
154void HardFault_Handler (void)
155{
156 asm("bkpt");
157}
158
159#ifdef USE_FULL_ASSERT
160/**
161 * @brief Reports the name of the source file and the source line number
162 * where the assert_param error has occurred.
163 * @param file: pointer to the source file name
164 * @param line: assert_param error line source number
165 * @retval None
166 */
167void assert_failed(uint8_t* file, uint32_t line)
168{
169 (void) file; (void) line;
170 /* USER CODE BEGIN 6 */
171 /* User can add his own implementation to report the file name and line number,
172 tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
173 /* USER CODE END 6 */
174}
175#endif /* USE_FULL_ASSERT */
176
177// Required by __libc_init_array in startup code if we are compiling using
178// -nostdlib/-nostartfiles.
179void _init(void)
180{
181
182}