3 ******************************************************************************
5 * @brief : Main program body
6 ******************************************************************************
7 ** This notice applies to any and all portions of this file
8 * that are not between comment pairs USER CODE BEGIN and
9 * USER CODE END. Other portions of this file, whether
10 * inserted by the user or by software development tools
11 * are owned by their respective copyright owners.
13 * COPYRIGHT(c) 2018 STMicroelectronics
15 * Redistribution and use in source and binary forms, with or without modification,
16 * are permitted provided that the following conditions are met:
17 * 1. Redistributions of source code must retain the above copyright notice,
18 * this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright notice,
20 * this list of conditions and the following disclaimer in the documentation
21 * and/or other materials provided with the distribution.
22 * 3. Neither the name of STMicroelectronics nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
27 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
29 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
32 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
33 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
34 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 ******************************************************************************
39 /* Includes ------------------------------------------------------------------*/
42 /* USER CODE BEGIN Includes */
44 /* USER CODE END Includes */
46 /* Private variables ---------------------------------------------------------*/
48 /* USER CODE BEGIN PV */
49 /* Private variables ---------------------------------------------------------*/
51 /* USER CODE END PV */
53 /* Private function prototypes -----------------------------------------------*/
54 static void LL_Init(void);
55 void SystemClock_Config(void);
56 static void MX_GPIO_Init(void);
57 static void MX_USART2_UART_Init(void);
59 /* USER CODE BEGIN PFP */
60 /* Private function prototypes -----------------------------------------------*/
62 /* USER CODE END PFP */
64 /* USER CODE BEGIN 0 */
69 * @brief The application entry point.
75 /* USER CODE BEGIN 1 */
79 /* MCU Configuration----------------------------------------------------------*/
81 /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
84 /* USER CODE BEGIN Init */
86 /* USER CODE END Init */
88 /* Configure the system clock */
91 /* USER CODE BEGIN SysInit */
93 /* USER CODE END SysInit */
95 /* Initialize all configured peripherals */
97 MX_USART2_UART_Init();
98 /* USER CODE BEGIN 2 */
100 /* USER CODE END 2 */
103 /* USER CODE BEGIN WHILE */
107 /* USER CODE END WHILE */
109 /* USER CODE BEGIN 3 */
112 /* USER CODE END 3 */
116 static void LL_Init(void)
120 LL_APB1_GRP2_EnableClock(LL_APB1_GRP2_PERIPH_SYSCFG);
122 /* System interrupt init*/
123 /* SVC_IRQn interrupt configuration */
124 NVIC_SetPriority(SVC_IRQn, 0);
125 /* PendSV_IRQn interrupt configuration */
126 NVIC_SetPriority(PendSV_IRQn, 0);
127 /* SysTick_IRQn interrupt configuration */
128 NVIC_SetPriority(SysTick_IRQn, 0);
133 * @brief System Clock Configuration
136 void SystemClock_Config(void)
139 LL_FLASH_SetLatency(LL_FLASH_LATENCY_1);
141 if(LL_FLASH_GetLatency() != LL_FLASH_LATENCY_1)
147 /* Wait till HSI is ready */
148 while(LL_RCC_HSI_IsReady() != 1)
152 LL_RCC_HSI_SetCalibTrimming(16);
154 LL_RCC_PLL_ConfigDomain_SYS(LL_RCC_PLLSOURCE_HSI_DIV_2, LL_RCC_PLL_MUL_8);
158 /* Wait till PLL is ready */
159 while(LL_RCC_PLL_IsReady() != 1)
163 LL_RCC_SetAHBPrescaler(LL_RCC_SYSCLK_DIV_1);
165 LL_RCC_SetAPB1Prescaler(LL_RCC_APB1_DIV_1);
167 LL_RCC_SetSysClkSource(LL_RCC_SYS_CLKSOURCE_PLL);
169 /* Wait till System clock is ready */
170 while(LL_RCC_GetSysClkSource() != LL_RCC_SYS_CLKSOURCE_STATUS_PLL)
174 LL_Init1msTick(32000000);
176 LL_SYSTICK_SetClkSource(LL_SYSTICK_CLKSOURCE_HCLK);
178 LL_SetSystemCoreClock(32000000);
180 /* SysTick_IRQn interrupt configuration */
181 NVIC_SetPriority(SysTick_IRQn, 0);
184 /* USART2 init function */
185 static void MX_USART2_UART_Init(void)
188 LL_USART_InitTypeDef USART_InitStruct;
190 LL_GPIO_InitTypeDef GPIO_InitStruct;
192 /* Peripheral clock enable */
193 LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_USART2);
195 /**USART2 GPIO Configuration
196 PA2 ------> USART2_TX
197 PA3 ------> USART2_RX
199 GPIO_InitStruct.Pin = USART_TX_Pin;
200 GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
201 GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_LOW;
202 GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
203 GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
204 GPIO_InitStruct.Alternate = LL_GPIO_AF_1;
205 LL_GPIO_Init(USART_TX_GPIO_Port, &GPIO_InitStruct);
207 GPIO_InitStruct.Pin = USART_RX_Pin;
208 GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
209 GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_LOW;
210 GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
211 GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
212 GPIO_InitStruct.Alternate = LL_GPIO_AF_1;
213 LL_GPIO_Init(USART_RX_GPIO_Port, &GPIO_InitStruct);
215 USART_InitStruct.BaudRate = 38400;
216 USART_InitStruct.DataWidth = LL_USART_DATAWIDTH_8B;
217 USART_InitStruct.StopBits = LL_USART_STOPBITS_1;
218 USART_InitStruct.Parity = LL_USART_PARITY_NONE;
219 USART_InitStruct.TransferDirection = LL_USART_DIRECTION_TX_RX;
220 USART_InitStruct.HardwareFlowControl = LL_USART_HWCONTROL_NONE;
221 USART_InitStruct.OverSampling = LL_USART_OVERSAMPLING_16;
222 LL_USART_Init(USART2, &USART_InitStruct);
224 LL_USART_DisableIT_CTS(USART2);
226 LL_USART_DisableOverrunDetect(USART2);
228 LL_USART_ConfigAsyncMode(USART2);
230 LL_USART_Enable(USART2);
234 /** Configure pins as
241 static void MX_GPIO_Init(void)
244 LL_EXTI_InitTypeDef EXTI_InitStruct;
245 LL_GPIO_InitTypeDef GPIO_InitStruct;
247 /* GPIO Ports Clock Enable */
248 LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOC);
249 LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOF);
250 LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOA);
253 LL_GPIO_ResetOutputPin(LD2_GPIO_Port, LD2_Pin);
256 LL_SYSCFG_SetEXTISource(LL_SYSCFG_EXTI_PORTC, LL_SYSCFG_EXTI_LINE13);
259 LL_GPIO_SetPinPull(B1_GPIO_Port, B1_Pin, LL_GPIO_PULL_NO);
262 LL_GPIO_SetPinMode(B1_GPIO_Port, B1_Pin, LL_GPIO_MODE_INPUT);
265 EXTI_InitStruct.Line_0_31 = LL_EXTI_LINE_13;
266 EXTI_InitStruct.LineCommand = ENABLE;
267 EXTI_InitStruct.Mode = LL_EXTI_MODE_IT;
268 EXTI_InitStruct.Trigger = LL_EXTI_TRIGGER_FALLING;
269 LL_EXTI_Init(&EXTI_InitStruct);
272 GPIO_InitStruct.Pin = LD2_Pin;
273 GPIO_InitStruct.Mode = LL_GPIO_MODE_OUTPUT;
274 GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_LOW;
275 GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
276 GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
277 LL_GPIO_Init(LD2_GPIO_Port, &GPIO_InitStruct);
281 /* USER CODE BEGIN 4 */
283 /* USER CODE END 4 */
286 * @brief This function is executed in case of error occurrence.
287 * @param file: The file name as string.
288 * @param line: The line in file as a number.
291 void _Error_Handler(char *file, int line)
293 /* USER CODE BEGIN Error_Handler_Debug */
294 /* User can add his own implementation to report the HAL error return state */
298 /* USER CODE END Error_Handler_Debug */
301 #ifdef USE_FULL_ASSERT
303 * @brief Reports the name of the source file and the source line number
304 * where the assert_param error has occurred.
305 * @param file: pointer to the source file name
306 * @param line: assert_param error line source number
309 void assert_failed(uint8_t* file, uint32_t line)
311 /* USER CODE BEGIN 6 */
312 /* User can add his own implementation to report the file name and line number,
313 tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
314 /* USER CODE END 6 */
316 #endif /* USE_FULL_ASSERT */
326 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/