1 /* USER CODE BEGIN Header */ 2 /** 3 ****************************************************************************** 4 * @file : main.c 5 * @brief : Main program body 6 ****************************************************************************** 7 * @attention 8 * 9 * Copyright (c) 2025 STMicroelectronics. 10 * All rights reserved. 11 * 12 * This software is licensed under terms that can be found in the LICENSE file 13 * in the root directory of this software component. 14 * If no LICENSE file comes with this software, it is provided AS-IS. 15 * 16 ****************************************************************************** 17 */ 18 /* USER CODE END Header */ 19 /* Includes ------------------------------------------------------------------*/ 20 #include "main.h" 21 22 /* Private includes ----------------------------------------------------------*/ 23 /* USER CODE BEGIN Includes */ 24 25 /* USER CODE END Includes */ 26 27 /* Private typedef -----------------------------------------------------------*/ 28 /* USER CODE BEGIN PTD */ 29 30 /* USER CODE END PTD */ 31 32 /* Private define ------------------------------------------------------------*/ 33 /* USER CODE BEGIN PD */ 34 35 /* USER CODE END PD */ 36 37 /* Private macro -------------------------------------------------------------*/ 38 /* USER CODE BEGIN PM */ 39 40 /* USER CODE END PM */ 41 42 /* Private variables ---------------------------------------------------------*/ 43 UART_HandleTypeDef huart2; 44 45 /* USER CODE BEGIN PV */ 46 47 /* USER CODE END PV */ 48 49 /* Private function prototypes -----------------------------------------------*/ 50 void SystemClock_Config(void); 51 static void MX_GPIO_Init(void); 52 static void MX_USART2_UART_Init(void); 53 /* USER CODE BEGIN PFP */ 54 55 /* USER CODE END PFP */ 56 57 /* Private user code ---------------------------------------------------------*/ 58 /* USER CODE BEGIN 0 */ 59 60 /* USER CODE END 0 */ 61 62 /** 63 * @brief The application entry point. 64 * @retval int 65 */ 66 int main(void) 67 { 68 69 /* USER CODE BEGIN 1 */ 70 71 /* USER CODE END 1 */ 72 73 /* MCU Configuration--------------------------------------------------------*/ 74 75 /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ 76 HAL_Init(); 77 78 /* USER CODE BEGIN Init */ 79 80 /* USER CODE END Init */ 81 82 /* Configure the system clock */ 83 SystemClock_Config(); 84 85 /* USER CODE BEGIN SysInit */ 86 87 /* USER CODE END SysInit */ 88 89 /* Initialize all configured peripherals */ 90 MX_GPIO_Init(); 91 MX_USART2_UART_Init(); 92 /* USER CODE BEGIN 2 */ 93 94 /* USER CODE END 2 */ 95 96 /* Infinite loop */ 97 /* USER CODE BEGIN WHILE */ 98 while (1) 99 { 100 /* USER CODE END WHILE */ 101 102 /* USER CODE BEGIN 3 */ 103 HAL_GPIO_TogglePin(LD2_GPIO_Port, LD2_Pin); 104 HAL_Delay(500); 105 } 106 /* USER CODE END 3 */ 107 } 108 109 /** 110 * @brief System Clock Configuration 111 * @retval None 112 */ 113 void SystemClock_Config(void) 114 { 115 RCC_OscInitTypeDef RCC_OscInitStruct = {0}; 116 RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; 117 118 /** Initializes the RCC Oscillators according to the specified parameters 119 * in the RCC_OscInitTypeDef structure. 120 */ 121 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI; 122 RCC_OscInitStruct.HSIState = RCC_HSI_ON; 123 RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT; 124 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; 125 RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI_DIV2; 126 RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL16; 127 if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) 128 { 129 Error_Handler(); 130 } 131 132 /** Initializes the CPU, AHB and APB buses clocks 133 */ 134 RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK 135 |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2; 136 RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; 137 RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; 138 RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2; 139 RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; 140 141 if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK) 142 { 143 Error_Handler(); 144 } 145 } 146 147 /** 148 * @brief USART2 Initialization Function 149 * @param None 150 * @retval None 151 */ 152 static void MX_USART2_UART_Init(void) 153 { 154 155 /* USER CODE BEGIN USART2_Init 0 */ 156 157 /* USER CODE END USART2_Init 0 */ 158 159 /* USER CODE BEGIN USART2_Init 1 */ 160 161 /* USER CODE END USART2_Init 1 */ 162 huart2.Instance = USART2; 163 huart2.Init.BaudRate = 115200; 164 huart2.Init.WordLength = UART_WORDLENGTH_8B; 165 huart2.Init.StopBits = UART_STOPBITS_1; 166 huart2.Init.Parity = UART_PARITY_NONE; 167 huart2.Init.Mode = UART_MODE_TX_RX; 168 huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE; 169 huart2.Init.OverSampling = UART_OVERSAMPLING_16; 170 if (HAL_UART_Init(&huart2) != HAL_OK) 171 { 172 Error_Handler(); 173 } 174 /* USER CODE BEGIN USART2_Init 2 */ 175 176 /* USER CODE END USART2_Init 2 */ 177 178 } 179 180 /** 181 * @brief GPIO Initialization Function 182 * @param None 183 * @retval None 184 */ 185 static void MX_GPIO_Init(void) 186 { 187 GPIO_InitTypeDef GPIO_InitStruct = {0}; 188 /* USER CODE BEGIN MX_GPIO_Init_1 */ 189 190 /* USER CODE END MX_GPIO_Init_1 */ 191 192 /* GPIO Ports Clock Enable */ 193 __HAL_RCC_GPIOC_CLK_ENABLE(); 194 __HAL_RCC_GPIOD_CLK_ENABLE(); 195 __HAL_RCC_GPIOA_CLK_ENABLE(); 196 __HAL_RCC_GPIOB_CLK_ENABLE(); 197 198 /*Configure GPIO pin Output Level */ 199 HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, GPIO_PIN_RESET); 200 201 /*Configure GPIO pin : B1_Pin */ 202 GPIO_InitStruct.Pin = B1_Pin; 203 GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING; 204 GPIO_InitStruct.Pull = GPIO_NOPULL; 205 HAL_GPIO_Init(B1_GPIO_Port, &GPIO_InitStruct); 206 207 /*Configure GPIO pin : LD2_Pin */ 208 GPIO_InitStruct.Pin = LD2_Pin; 209 GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; 210 GPIO_InitStruct.Pull = GPIO_NOPULL; 211 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; 212 HAL_GPIO_Init(LD2_GPIO_Port, &GPIO_InitStruct); 213 214 /* EXTI interrupt init*/ 215 HAL_NVIC_SetPriority(EXTI15_10_IRQn, 0, 0); 216 HAL_NVIC_EnableIRQ(EXTI15_10_IRQn); 217 218 /* USER CODE BEGIN MX_GPIO_Init_2 */ 219 220 /* USER CODE END MX_GPIO_Init_2 */ 221 } 222 223 /* USER CODE BEGIN 4 */ 224 225 /* USER CODE END 4 */ 226 227 /** 228 * @brief This function is executed in case of error occurrence. 229 * @retval None 230 */ 231 void Error_Handler(void) 232 { 233 /* USER CODE BEGIN Error_Handler_Debug */ 234 /* User can add his own implementation to report the HAL error return state */ 235 __disable_irq(); 236 while (1) 237 { 238 } 239 /* USER CODE END Error_Handler_Debug */ 240 } 241 #ifdef USE_FULL_ASSERT 242 /** 243 * @brief Reports the name of the source file and the source line number 244 * where the assert_param error has occurred. 245 * @param file: pointer to the source file name 246 * @param line: assert_param error line source number 247 * @retval None 248 */ 249 void assert_failed(uint8_t *file, uint32_t line) 250 { 251 /* USER CODE BEGIN 6 */ 252 /* User can add his own implementation to report the file name and line number, 253 ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ 254 /* USER CODE END 6 */ 255 } 256 #endif /* USE_FULL_ASSERT */
