blob: 8fcf9ebd64ba4391d2a76504dc25fc771ed421ae [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 "stm32f1xx_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//--------------------------------------------------------------------+
52
53void board_init(void)
54{
55 board_stm32f1_clock_init();
56
57 // Enable All GPIOs clocks
58 __HAL_RCC_GPIOA_CLK_ENABLE();
59 __HAL_RCC_GPIOB_CLK_ENABLE();
60 __HAL_RCC_GPIOC_CLK_ENABLE();
61 __HAL_RCC_GPIOD_CLK_ENABLE();
62
63#if CFG_TUSB_OS == OPT_OS_NONE
64 // 1ms tick timer
65 SysTick_Config(SystemCoreClock / 1000);
66
67#elif CFG_TUSB_OS == OPT_OS_FREERTOS
68 // If freeRTOS is used, IRQ priority is limit by max syscall ( smaller is higher )
69 NVIC_SetPriority(USB_HP_CAN1_TX_IRQn, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY);
70 NVIC_SetPriority(USB_LP_CAN1_RX0_IRQn, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY);
71 NVIC_SetPriority(USBWakeUp_IRQn, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY);
72#endif
73
74 // LED
75 GPIO_InitTypeDef GPIO_InitStruct;
76 GPIO_InitStruct.Pin = LED_PIN;
77 GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
78 GPIO_InitStruct.Pull = LED_STATE_ON ? GPIO_PULLDOWN : GPIO_PULLUP;
79 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
80 HAL_GPIO_Init(LED_PORT, &GPIO_InitStruct);
81
82 // Button
83 GPIO_InitStruct.Pin = BUTTON_PIN;
84 GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
85 GPIO_InitStruct.Pull = BUTTON_STATE_ACTIVE ? GPIO_PULLDOWN : GPIO_PULLUP;
86 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
87 HAL_GPIO_Init(BUTTON_PORT, &GPIO_InitStruct);
88
89 // USB Pins
90 // Configure USB DM and DP pins.
91 GPIO_InitStruct.Pin = (GPIO_PIN_11 | GPIO_PIN_12);
92 GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
93 GPIO_InitStruct.Pull = GPIO_NOPULL;
94 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
95 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
96
97 // USB Clock enable
98 __HAL_RCC_USB_CLK_ENABLE();
99}
100
101//--------------------------------------------------------------------+
102// Board porting API
103//--------------------------------------------------------------------+
104
105void board_led_write(bool state)
106{
107 HAL_GPIO_WritePin(LED_PORT, LED_PIN, state ? LED_STATE_ON : (1-LED_STATE_ON));
108}
109
110uint32_t board_button_read(void)
111{
112 return BUTTON_STATE_ACTIVE == HAL_GPIO_ReadPin(BUTTON_PORT, BUTTON_PIN);
113}
114
115int board_uart_read(uint8_t* buf, int len)
116{
117 (void) buf; (void) len;
118 return 0;
119}
120
121int board_uart_write(void const * buf, int len)
122{
123 (void) buf; (void) len;
124 return 0;
125}
126
127#if CFG_TUSB_OS == OPT_OS_NONE
128volatile uint32_t system_ticks = 0;
129void SysTick_Handler (void)
130{
131 system_ticks++;
132}
133
134uint32_t board_millis(void)
135{
136 return system_ticks;
137}
138#endif
139
140void HardFault_Handler (void)
141{
142 asm("bkpt");
143}
144
145#ifdef USE_FULL_ASSERT
146/**
147 * @brief Reports the name of the source file and the source line number
148 * where the assert_param error has occurred.
149 * @param file: pointer to the source file name
150 * @param line: assert_param error line source number
151 * @retval None
152 */
153void assert_failed(char *file, uint32_t line)
154{
155 /* USER CODE BEGIN 6 */
156 /* User can add his own implementation to report the file name and line number,
157 tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
158 /* USER CODE END 6 */
159}
160#endif /* USE_FULL_ASSERT */
161
162// Required by __libc_init_array in startup code if we are compiling using
163// -nostdlib/-nostartfiles.
164void _init(void)
165{
166
167}