|
| 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 | +/* metadata: |
| 28 | + name: STM32 L496 Nucleo |
| 29 | + url: https://www.st.com/en/evaluation-tools/nucleo-l496ZG-P.html |
| 30 | +*/ |
| 31 | + |
| 32 | +#ifndef BOARD_H_ |
| 33 | +#define BOARD_H_ |
| 34 | + |
| 35 | +#ifdef __cplusplus |
| 36 | + extern "C" { |
| 37 | +#endif |
| 38 | + |
| 39 | +#define LED_PORT GPIOB |
| 40 | +#define LED_PIN GPIO_PIN_7 |
| 41 | +#define LED_STATE_ON 1 |
| 42 | + |
| 43 | +// Not a real button |
| 44 | +#define BUTTON_PORT GPIOC |
| 45 | +#define BUTTON_PIN GPIO_PIN_13 |
| 46 | +#define BUTTON_STATE_ACTIVE 1 |
| 47 | + |
| 48 | +#define UART_DEV LPUART1 |
| 49 | +#define UART_CLK_EN __HAL_RCC_LPUART1_CLK_ENABLE |
| 50 | +#define UART_GPIO_PORT GPIOG |
| 51 | +#define UART_GPIO_AF GPIO_AF8_LPUART1 |
| 52 | +#define UART_TX_PIN GPIO_PIN_7 |
| 53 | +#define UART_RX_PIN GPIO_PIN_8 |
| 54 | + |
| 55 | +//--------------------------------------------------------------------+ |
| 56 | +// RCC Clock |
| 57 | +//--------------------------------------------------------------------+ |
| 58 | + |
| 59 | +/** |
| 60 | + * @brief System Clock Configuration |
| 61 | + * The system Clock is configured as follow : |
| 62 | + * System Clock source = PLL (MSI) |
| 63 | + * SYSCLK(Hz) = 80000000 |
| 64 | + * HCLK(Hz) = 80000000 |
| 65 | + * AHB Prescaler = 1 |
| 66 | + * APB1 Prescaler = 1 |
| 67 | + * APB2 Prescaler = 1 |
| 68 | + * MSI Frequency(Hz) = 8000000 |
| 69 | + * PLL_M = 1 |
| 70 | + * PLL_N = 10 |
| 71 | + * PLL_Q = 2 |
| 72 | + * PLL_R = 2 |
| 73 | + * VDD(V) = 3.3 |
| 74 | + * @param None |
| 75 | + * @retval None |
| 76 | + */ |
| 77 | + |
| 78 | +static inline void board_clock_init(void) |
| 79 | +{ |
| 80 | + RCC_OscInitTypeDef RCC_OscInitStruct = {0}; |
| 81 | + RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; |
| 82 | + RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0}; |
| 83 | + |
| 84 | + /** Configure the main internal regulator output voltage |
| 85 | + */ |
| 86 | + HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1); |
| 87 | + |
| 88 | + /** Configure LSE Drive Capability |
| 89 | + */ |
| 90 | + HAL_PWR_EnableBkUpAccess(); |
| 91 | + __HAL_RCC_LSEDRIVE_CONFIG(RCC_LSEDRIVE_LOW); |
| 92 | + |
| 93 | + /** Initializes the RCC Oscillators according to the specified parameters |
| 94 | + * in the RCC_OscInitTypeDef structure. |
| 95 | + */ |
| 96 | + RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI48|RCC_OSCILLATORTYPE_HSI; |
| 97 | + RCC_OscInitStruct.HSIState = RCC_HSI_ON; |
| 98 | + RCC_OscInitStruct.HSI48State = RCC_HSI48_ON; |
| 99 | + RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT; |
| 100 | + RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; |
| 101 | + RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI; |
| 102 | + RCC_OscInitStruct.PLL.PLLM = 1; |
| 103 | + RCC_OscInitStruct.PLL.PLLN = 10; |
| 104 | + RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2; |
| 105 | + RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2; |
| 106 | + RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2; |
| 107 | + |
| 108 | + HAL_RCC_OscConfig(&RCC_OscInitStruct); |
| 109 | + |
| 110 | + /** Initializes the CPU, AHB and APB buses clocks |
| 111 | + */ |
| 112 | + RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK |
| 113 | + |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2; |
| 114 | + RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; |
| 115 | + RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; |
| 116 | + RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1; |
| 117 | + RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; |
| 118 | + |
| 119 | + HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4); |
| 120 | + |
| 121 | + // /** Enable the SYSCFG APB clock |
| 122 | + // */ |
| 123 | + // __HAL_RCC_CRS_CLK_ENABLE(); |
| 124 | + // |
| 125 | + // /** Configures CRS |
| 126 | + // */ |
| 127 | + // RCC_CRSInitTypeDef RCC_CRSInitStruct = {0}; |
| 128 | + // RCC_CRSInitStruct.Prescaler = RCC_CRS_SYNC_DIV1; |
| 129 | + // RCC_CRSInitStruct.Source = RCC_CRS_SYNC_SOURCE_USB; |
| 130 | + // RCC_CRSInitStruct.Polarity = RCC_CRS_SYNC_POLARITY_RISING; |
| 131 | + // RCC_CRSInitStruct.ReloadValue = __HAL_RCC_CRS_RELOADVALUE_CALCULATE(48000000,1000); |
| 132 | + // RCC_CRSInitStruct.ErrorLimitValue = 34; |
| 133 | + // RCC_CRSInitStruct.HSI48CalibrationValue = 32; |
| 134 | + // |
| 135 | + // HAL_RCCEx_CRSConfig(&RCC_CRSInitStruct); |
| 136 | + |
| 137 | + /* Select HSI48 output as USB clock source */ |
| 138 | + PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_USB; |
| 139 | + PeriphClkInitStruct.UsbClockSelection = RCC_USBCLKSOURCE_HSI48; |
| 140 | + HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct); |
| 141 | + |
| 142 | + /* Select PLL output as UART clock source */ |
| 143 | + PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_LPUART1; |
| 144 | + PeriphClkInitStruct.Lpuart1ClockSelection = RCC_LPUART1CLKSOURCE_PCLK1; |
| 145 | + HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct); |
| 146 | +} |
| 147 | + |
| 148 | +static inline void board_vbus_sense_init(void) |
| 149 | +{ |
| 150 | + // Enable VBUS sense (B device) via pin PA9 |
| 151 | + USB_OTG_FS->GCCFG |= USB_OTG_GCCFG_VBDEN; |
| 152 | +} |
| 153 | + |
| 154 | +#ifdef __cplusplus |
| 155 | + } |
| 156 | +#endif |
| 157 | + |
| 158 | +#endif /* BOARD_H_ */ |
0 commit comments