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 | #include "stm32l0xx_hal.h" |
| 29 | |
| 30 | //--------------------------------------------------------------------+ |
| 31 | // Forward USB interrupt events to TinyUSB IRQ Handler |
| 32 | //--------------------------------------------------------------------+ |
| 33 | void USB_IRQHandler(void) |
| 34 | { |
| 35 | tud_int_handler(0); |
| 36 | } |
| 37 | |
| 38 | //--------------------------------------------------------------------+ |
| 39 | // MACRO TYPEDEF CONSTANT ENUM |
| 40 | //--------------------------------------------------------------------+ |
| 41 | #define LED_PORT GPIOA |
| 42 | #define LED_PIN GPIO_PIN_5 |
| 43 | #define LED_STATE_ON 1 |
| 44 | |
| 45 | #define BUTTON_PORT GPIOA |
| 46 | #define BUTTON_PIN GPIO_PIN_0 |
| 47 | #define BUTTON_STATE_ACTIVE 1 |
| 48 | |
| 49 | /** |
| 50 | * @brief System Clock Configuration |
| 51 | * The system Clock is configured as follow: |
| 52 | * HSI48 used as USB clock source |
| 53 | * - System Clock source = HSI |
| 54 | * - HSI Frequency(Hz) = 16000000 |
| 55 | * - SYSCLK(Hz) = 16000000 |
| 56 | * - HCLK(Hz) = 16000000 |
| 57 | * - AHB Prescaler = 1 |
| 58 | * - APB1 Prescaler = 1 |
| 59 | * - APB2 Prescaler = 1 |
| 60 | * - Flash Latency(WS) = 0 |
| 61 | * - Main regulator output voltage = Scale1 mode |
| 62 | * @param None |
| 63 | * @retval None |
| 64 | */ |
| 65 | static void SystemClock_Config(void) |
| 66 | { |
| 67 | RCC_ClkInitTypeDef RCC_ClkInitStruct; |
| 68 | RCC_OscInitTypeDef RCC_OscInitStruct; |
| 69 | RCC_PeriphCLKInitTypeDef PeriphClkInitStruct; |
| 70 | static RCC_CRSInitTypeDef RCC_CRSInitStruct; |
| 71 | |
| 72 | /* Enable HSI Oscillator to be used as System clock source |
| 73 | Enable HSI48 Oscillator to be used as USB clock source */ |
| 74 | RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI | RCC_OSCILLATORTYPE_HSI48; |
| 75 | RCC_OscInitStruct.HSIState = RCC_HSI_ON; |
| 76 | RCC_OscInitStruct.HSI48State = RCC_HSI48_ON; |
| 77 | HAL_RCC_OscConfig(&RCC_OscInitStruct); |
| 78 | |
| 79 | /* Select HSI48 as USB clock source */ |
| 80 | PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_USB; |
| 81 | PeriphClkInitStruct.UsbClockSelection = RCC_USBCLKSOURCE_HSI48; |
| 82 | HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct); |
| 83 | |
| 84 | /* Select HSI as system clock source and configure the HCLK, PCLK1 and PCLK2 |
| 85 | clock dividers */ |
| 86 | RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2); |
| 87 | RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI; |
| 88 | RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; |
| 89 | RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1; |
| 90 | RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; |
| 91 | HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0); |
| 92 | |
| 93 | /*Configure the clock recovery system (CRS)**********************************/ |
| 94 | |
| 95 | /*Enable CRS Clock*/ |
| 96 | __HAL_RCC_CRS_CLK_ENABLE(); |
| 97 | |
| 98 | /* Default Synchro Signal division factor (not divided) */ |
| 99 | RCC_CRSInitStruct.Prescaler = RCC_CRS_SYNC_DIV1; |
| 100 | /* Set the SYNCSRC[1:0] bits according to CRS_Source value */ |
| 101 | RCC_CRSInitStruct.Source = RCC_CRS_SYNC_SOURCE_USB; |
| 102 | /* HSI48 is synchronized with USB SOF at 1KHz rate */ |
| 103 | RCC_CRSInitStruct.ReloadValue = __HAL_RCC_CRS_RELOADVALUE_CALCULATE(48000000, 1000); |
| 104 | RCC_CRSInitStruct.ErrorLimitValue = RCC_CRS_ERRORLIMIT_DEFAULT; |
| 105 | /* Set the TRIM[5:0] to the default value*/ |
| 106 | RCC_CRSInitStruct.HSI48CalibrationValue = 0x20; |
| 107 | /* Start automatic synchronization */ |
| 108 | HAL_RCCEx_CRSConfig (&RCC_CRSInitStruct); |
| 109 | } |
| 110 | |
| 111 | void board_init(void) |
| 112 | { |
| 113 | SystemClock_Config(); |
| 114 | |
| 115 | #if CFG_TUSB_OS == OPT_OS_NONE |
| 116 | // 1ms tick timer |
| 117 | SysTick_Config(SystemCoreClock / 1000); |
| 118 | #elif CFG_TUSB_OS == OPT_OS_FREERTOS |
| 119 | // If freeRTOS is used, IRQ priority is limit by max syscall ( smaller is higher ) |
| 120 | //NVIC_SetPriority(USB0_IRQn, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY ); |
| 121 | #endif |
| 122 | |
| 123 | GPIO_InitTypeDef GPIO_InitStruct; |
| 124 | |
| 125 | // LED |
| 126 | __HAL_RCC_GPIOA_CLK_ENABLE(); |
| 127 | GPIO_InitStruct.Pin = LED_PIN; |
| 128 | GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; |
| 129 | GPIO_InitStruct.Pull = GPIO_PULLUP; |
| 130 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; |
| 131 | HAL_GPIO_Init(LED_PORT, &GPIO_InitStruct); |
| 132 | |
| 133 | board_led_write(false); |
| 134 | |
| 135 | // Button |
| 136 | //__HAL_RCC_GPIOA_CLK_ENABLE(); |
| 137 | GPIO_InitStruct.Pin = BUTTON_PIN; |
| 138 | GPIO_InitStruct.Mode = GPIO_MODE_INPUT; |
| 139 | GPIO_InitStruct.Pull = GPIO_PULLDOWN; |
| 140 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; |
| 141 | HAL_GPIO_Init(BUTTON_PORT, &GPIO_InitStruct); |
| 142 | |
| 143 | // USB |
| 144 | /* Configure DM DP Pins */ |
| 145 | __HAL_RCC_GPIOA_CLK_ENABLE(); |
| 146 | GPIO_InitStruct.Pin = (GPIO_PIN_11 | GPIO_PIN_12); |
| 147 | GPIO_InitStruct.Mode = GPIO_MODE_INPUT; |
| 148 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
| 149 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; |
| 150 | HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); |
| 151 | |
| 152 | /* Enable USB FS Clock */ |
| 153 | __HAL_RCC_USB_CLK_ENABLE(); |
| 154 | } |
| 155 | |
| 156 | //--------------------------------------------------------------------+ |
| 157 | // Board porting API |
| 158 | //--------------------------------------------------------------------+ |
| 159 | |
| 160 | void board_led_write(bool state) |
| 161 | { |
| 162 | HAL_GPIO_WritePin(LED_PORT, LED_PIN, state ? LED_STATE_ON : (1-LED_STATE_ON)); |
| 163 | } |
| 164 | |
| 165 | uint32_t board_button_read(void) |
| 166 | { |
| 167 | return BUTTON_STATE_ACTIVE == HAL_GPIO_ReadPin(BUTTON_PORT, BUTTON_PIN); |
| 168 | } |
| 169 | |
| 170 | int board_uart_read(uint8_t* buf, int len) |
| 171 | { |
| 172 | (void) buf; (void) len; |
| 173 | return 0; |
| 174 | } |
| 175 | |
| 176 | int board_uart_write(void const * buf, int len) |
| 177 | { |
| 178 | (void) buf; (void) len; |
| 179 | return 0; |
| 180 | } |
| 181 | |
| 182 | #if CFG_TUSB_OS == OPT_OS_NONE |
| 183 | volatile uint32_t system_ticks = 0; |
| 184 | void SysTick_Handler (void) |
| 185 | { |
| 186 | system_ticks++; |
| 187 | } |
| 188 | |
| 189 | uint32_t board_millis(void) |
| 190 | { |
| 191 | return system_ticks; |
| 192 | } |
| 193 | #endif |
| 194 | |
| 195 | void HardFault_Handler (void) |
| 196 | { |
| 197 | asm("bkpt"); |
| 198 | } |
| 199 | |
| 200 | // Required by __libc_init_array in startup code if we are compiling using |
| 201 | // -nostdlib/-nostartfiles. |
| 202 | void _init(void) |
| 203 | { |
| 204 | |
| 205 | } |