Austin Schuh | 41baf20 | 2022-01-01 14:33:40 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * The MIT License (MIT) |
| 3 | * |
| 4 | * Copyright (c) 2020, 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 | #ifndef BOARD_H_ |
| 28 | #define BOARD_H_ |
| 29 | |
| 30 | #ifdef __cplusplus |
| 31 | extern "C" { |
| 32 | #endif |
| 33 | |
| 34 | // G474RE Nucleo does not has usb connection. We need to manually connect |
| 35 | // - PA12 for D+, CN10.12 |
| 36 | // - PA11 for D-, CN10.14 |
| 37 | |
| 38 | // LED |
| 39 | #define LED_PORT GPIOA |
| 40 | #define LED_PIN GPIO_PIN_5 |
| 41 | #define LED_STATE_ON 0 |
| 42 | |
| 43 | // Button |
| 44 | #define BUTTON_PORT GPIOC |
| 45 | #define BUTTON_PIN GPIO_PIN_13 |
| 46 | #define BUTTON_STATE_ACTIVE 1 |
| 47 | |
| 48 | // UART Enable for STLink VCOM |
| 49 | #define UART_DEV LPUART1 |
| 50 | #define UART_CLK_EN __HAL_RCC_LPUART1_CLK_ENABLE |
| 51 | #define UART_GPIO_PORT GPIOA |
| 52 | #define UART_GPIO_AF GPIO_AF12_LPUART1 |
| 53 | #define UART_TX_PIN GPIO_PIN_2 |
| 54 | #define UART_RX_PIN GPIO_PIN_3 |
| 55 | |
| 56 | |
| 57 | //--------------------------------------------------------------------+ |
| 58 | // RCC Clock |
| 59 | //--------------------------------------------------------------------+ |
| 60 | static inline void board_clock_init(void) |
| 61 | { |
| 62 | RCC_OscInitTypeDef RCC_OscInitStruct = {0}; |
| 63 | RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; |
| 64 | RCC_PeriphCLKInitTypeDef PeriphClkInit = {0}; |
| 65 | |
| 66 | // Configure the main internal regulator output voltage |
| 67 | HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1_BOOST); |
| 68 | |
| 69 | // Initializes the CPU, AHB and APB busses clocks |
| 70 | RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI48 | RCC_OSCILLATORTYPE_HSE; |
| 71 | RCC_OscInitStruct.HSEState = RCC_HSE_ON; |
| 72 | RCC_OscInitStruct.HSI48State = RCC_HSI48_ON; |
| 73 | RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; |
| 74 | RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; |
| 75 | RCC_OscInitStruct.PLL.PLLM = RCC_PLLM_DIV4; |
| 76 | RCC_OscInitStruct.PLL.PLLN = 50; |
| 77 | RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2; |
| 78 | RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2; |
| 79 | RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2; |
| 80 | HAL_RCC_OscConfig(&RCC_OscInitStruct); |
| 81 | |
| 82 | // Initializes the CPU, AHB and APB busses clocks |
| 83 | RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2; |
| 84 | RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; |
| 85 | RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; |
| 86 | RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1; |
| 87 | RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; |
| 88 | HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_8); |
| 89 | |
| 90 | PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USB; |
| 91 | PeriphClkInit.UsbClockSelection = RCC_USBCLKSOURCE_HSI48; |
| 92 | HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) ; |
| 93 | |
| 94 | #if 0 // TODO need to check if USB clock is enabled |
| 95 | /* Enable HSI48 */ |
| 96 | memset(&RCC_OscInitStruct, 0, sizeof(RCC_OscInitStruct)); |
| 97 | |
| 98 | RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI48; |
| 99 | RCC_OscInitStruct.HSI48State = RCC_HSI48_ON; |
| 100 | RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; |
| 101 | HAL_RCC_OscConfig(&RCC_OscInitStruct); |
| 102 | |
| 103 | /*Enable CRS Clock*/ |
| 104 | RCC_CRSInitTypeDef RCC_CRSInitStruct= {0}; |
| 105 | __HAL_RCC_CRS_CLK_ENABLE(); |
| 106 | |
| 107 | /* Default Synchro Signal division factor (not divided) */ |
| 108 | RCC_CRSInitStruct.Prescaler = RCC_CRS_SYNC_DIV1; |
| 109 | |
| 110 | /* Set the SYNCSRC[1:0] bits according to CRS_Source value */ |
| 111 | RCC_CRSInitStruct.Source = RCC_CRS_SYNC_SOURCE_USB; |
| 112 | |
| 113 | /* HSI48 is synchronized with USB SOF at 1KHz rate */ |
| 114 | RCC_CRSInitStruct.ReloadValue = __HAL_RCC_CRS_RELOADVALUE_CALCULATE(48000000, 1000); |
| 115 | RCC_CRSInitStruct.ErrorLimitValue = RCC_CRS_ERRORLIMIT_DEFAULT; |
| 116 | |
| 117 | /* Set the TRIM[5:0] to the default value */ |
| 118 | RCC_CRSInitStruct.HSI48CalibrationValue = RCC_CRS_HSI48CALIBRATION_DEFAULT; |
| 119 | |
| 120 | /* Start automatic synchronization */ |
| 121 | HAL_RCCEx_CRSConfig(&RCC_CRSInitStruct); |
| 122 | #endif |
| 123 | } |
| 124 | |
| 125 | static inline void board_vbus_sense_init(void) |
| 126 | { |
| 127 | // Enable VBUS sense (B device) via pin PA9 |
| 128 | } |
| 129 | |
| 130 | #ifdef __cplusplus |
| 131 | } |
| 132 | #endif |
| 133 | |
| 134 | #endif /* BOARD_H_ */ |