Austin Schuh | 41baf20 | 2022-01-01 14:33:40 -0800 | [diff] [blame^] | 1 | /* |
| 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 "../board.h" |
| 28 | |
| 29 | #include "stm32f2xx_hal.h" |
| 30 | |
| 31 | //--------------------------------------------------------------------+ |
| 32 | // Forward USB interrupt events to TinyUSB IRQ Handler |
| 33 | //--------------------------------------------------------------------+ |
| 34 | void OTG_FS_IRQHandler(void) |
| 35 | { |
| 36 | tud_int_handler(0); |
| 37 | } |
| 38 | |
| 39 | //--------------------------------------------------------------------+ |
| 40 | // MACRO TYPEDEF CONSTANT ENUM |
| 41 | //--------------------------------------------------------------------+ |
| 42 | |
| 43 | #define LED_PORT GPIOB |
| 44 | #define LED_PIN GPIO_PIN_14 |
| 45 | #define LED_STATE_ON 1 |
| 46 | |
| 47 | #define BUTTON_PORT GPIOC |
| 48 | #define BUTTON_PIN GPIO_PIN_13 |
| 49 | #define BUTTON_STATE_ACTIVE 1 |
| 50 | |
| 51 | |
| 52 | // enable all LED, Button, Uart, USB clock |
| 53 | static void all_rcc_clk_enable(void) |
| 54 | { |
| 55 | __HAL_RCC_GPIOA_CLK_ENABLE(); // USB D+, D- |
| 56 | __HAL_RCC_GPIOB_CLK_ENABLE(); // LED |
| 57 | __HAL_RCC_GPIOC_CLK_ENABLE(); // Button |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * @brief System Clock Configuration |
| 62 | * The system Clock is configured as follow : |
| 63 | * System Clock source = PLL (HSE) |
| 64 | * SYSCLK(Hz) = 120000000 |
| 65 | * HCLK(Hz) = 120000000 |
| 66 | * AHB Prescaler = 1 |
| 67 | * APB1 Prescaler = 4 |
| 68 | * APB2 Prescaler = 2 |
| 69 | * HSE Frequency(Hz) = 8000000 |
| 70 | * PLL_M = HSE_VALUE/1000000 |
| 71 | * PLL_N = 240 |
| 72 | * PLL_P = 2 |
| 73 | * PLL_Q = 5 |
| 74 | * VDD(V) = 3.3 |
| 75 | * Flash Latency(WS) = 3 |
| 76 | * @param None |
| 77 | * @retval None |
| 78 | */ |
| 79 | void SystemClock_Config(void) |
| 80 | { |
| 81 | RCC_ClkInitTypeDef RCC_ClkInitStruct; |
| 82 | RCC_OscInitTypeDef RCC_OscInitStruct; |
| 83 | |
| 84 | /* Enable HSE Oscillator and activate PLL with HSE as source */ |
| 85 | RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE; |
| 86 | RCC_OscInitStruct.HSEState = RCC_HSE_BYPASS; |
| 87 | RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; |
| 88 | RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; |
| 89 | RCC_OscInitStruct.PLL.PLLM = HSE_VALUE/1000000; |
| 90 | RCC_OscInitStruct.PLL.PLLN = 240; |
| 91 | RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2; |
| 92 | RCC_OscInitStruct.PLL.PLLQ = 5; |
| 93 | HAL_RCC_OscConfig(&RCC_OscInitStruct); |
| 94 | |
| 95 | /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 |
| 96 | clocks dividers */ |
| 97 | RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2); |
| 98 | RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; |
| 99 | RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; |
| 100 | RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4; |
| 101 | RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2; |
| 102 | HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_3); |
| 103 | } |
| 104 | |
| 105 | void board_init(void) |
| 106 | { |
| 107 | SystemClock_Config(); |
| 108 | |
| 109 | #if CFG_TUSB_OS == OPT_OS_NONE |
| 110 | // 1ms tick timer |
| 111 | SysTick_Config(SystemCoreClock / 1000); |
| 112 | #endif |
| 113 | |
| 114 | |
| 115 | all_rcc_clk_enable(); |
| 116 | |
| 117 | GPIO_InitTypeDef GPIO_InitStruct; |
| 118 | |
| 119 | // LED |
| 120 | GPIO_InitStruct.Pin = LED_PIN; |
| 121 | GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; |
| 122 | GPIO_InitStruct.Pull = GPIO_PULLUP; |
| 123 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; |
| 124 | HAL_GPIO_Init(LED_PORT, &GPIO_InitStruct); |
| 125 | |
| 126 | board_led_write(false); |
| 127 | |
| 128 | // Button |
| 129 | GPIO_InitStruct.Pin = BUTTON_PIN; |
| 130 | GPIO_InitStruct.Mode = GPIO_MODE_INPUT; |
| 131 | GPIO_InitStruct.Pull = GPIO_PULLDOWN; |
| 132 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; |
| 133 | HAL_GPIO_Init(BUTTON_PORT, &GPIO_InitStruct); |
| 134 | |
| 135 | /* Configure DM DP Pins */ |
| 136 | GPIO_InitStruct.Pin = (GPIO_PIN_11 | GPIO_PIN_12); |
| 137 | GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; |
| 138 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
| 139 | GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; |
| 140 | GPIO_InitStruct.Alternate = GPIO_AF10_OTG_FS; |
| 141 | HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); |
| 142 | |
| 143 | /* Configure VBUS Pin */ |
| 144 | GPIO_InitStruct.Pin = GPIO_PIN_9; |
| 145 | GPIO_InitStruct.Mode = GPIO_MODE_INPUT; |
| 146 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
| 147 | HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); |
| 148 | |
| 149 | /* Configure ID pin */ |
| 150 | GPIO_InitStruct.Pin = GPIO_PIN_10; |
| 151 | GPIO_InitStruct.Mode = GPIO_MODE_AF_OD; |
| 152 | GPIO_InitStruct.Pull = GPIO_PULLUP; |
| 153 | GPIO_InitStruct.Alternate = GPIO_AF10_OTG_FS; |
| 154 | HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); |
| 155 | |
| 156 | /* Enable USB FS Clocks */ |
| 157 | __HAL_RCC_USB_OTG_FS_CLK_ENABLE(); |
| 158 | |
| 159 | // Enable VBUS sense (B device) via pin PA9 |
| 160 | USB_OTG_FS->GCCFG &= ~USB_OTG_GCCFG_NOVBUSSENS; |
| 161 | USB_OTG_FS->GCCFG |= USB_OTG_GCCFG_VBUSBSEN; |
| 162 | } |
| 163 | |
| 164 | //--------------------------------------------------------------------+ |
| 165 | // Board porting API |
| 166 | //--------------------------------------------------------------------+ |
| 167 | |
| 168 | void board_led_write(bool state) |
| 169 | { |
| 170 | HAL_GPIO_WritePin(LED_PORT, LED_PIN, state ? LED_STATE_ON : (1-LED_STATE_ON)); |
| 171 | } |
| 172 | |
| 173 | uint32_t board_button_read(void) |
| 174 | { |
| 175 | return BUTTON_STATE_ACTIVE == HAL_GPIO_ReadPin(BUTTON_PORT, BUTTON_PIN); |
| 176 | } |
| 177 | |
| 178 | int board_uart_read(uint8_t* buf, int len) |
| 179 | { |
| 180 | (void) buf; (void) len; |
| 181 | return 0; |
| 182 | } |
| 183 | |
| 184 | int board_uart_write(void const * buf, int len) |
| 185 | { |
| 186 | (void) buf; (void) len; |
| 187 | return 0; |
| 188 | } |
| 189 | |
| 190 | #if CFG_TUSB_OS == OPT_OS_NONE |
| 191 | volatile uint32_t system_ticks = 0; |
| 192 | void SysTick_Handler (void) |
| 193 | { |
| 194 | system_ticks++; |
| 195 | } |
| 196 | |
| 197 | uint32_t board_millis(void) |
| 198 | { |
| 199 | return system_ticks; |
| 200 | } |
| 201 | #endif |
| 202 | |
| 203 | void HardFault_Handler (void) |
| 204 | { |
| 205 | asm("bkpt"); |
| 206 | } |
| 207 | |
| 208 | // Required by __libc_init_array in startup code if we are compiling using |
| 209 | // -nostdlib/-nostartfiles. |
| 210 | void _init(void) |
| 211 | { |
| 212 | |
| 213 | } |