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 /* Application includes. */ 26 #include "logger.h" 27 #include "app.h" 28 29 /* USER CODE END Includes */ 30 31 /* Private typedef -----------------------------------------------------------*/ 32 /* USER CODE BEGIN PTD */ 33 34 /* USER CODE END PTD */ 35 36 /* Private define ------------------------------------------------------------*/ 37 /* USER CODE BEGIN PD */ 38 39 /* USER CODE END PD */ 40 41 /* Private macro -------------------------------------------------------------*/ 42 /* USER CODE BEGIN PM */ 43 44 /* USER CODE END PM */ 45 46 /* Private variables ---------------------------------------------------------*/ 47 UART_HandleTypeDef huart2; 48 49 /* USER CODE BEGIN PV */ 50 51 /* USER CODE END PV */ 52 53 /* Private function prototypes -----------------------------------------------*/ 54 void SystemClock_Config(void); 55 static void MX_GPIO_Init(void); 56 static void MX_USART2_UART_Init(void); 57 /* USER CODE BEGIN PFP */ 58 59 /* USER CODE END PFP */ 60 61 /* Private user code ---------------------------------------------------------*/ 62 /* USER CODE BEGIN 0 */ 63 #if (1 == LOGGER_CONFIG_USE_SEMIHOSTING) 64 65 extern void initialise_monitor_handles(void); 66 67 #endif 68 69 /* USER CODE END 0 */ 70 71 /** 72 * @brief The application entry point. 73 * @retval int 74 */ 75 int main(void) 76 { 77 78 /* USER CODE BEGIN 1 */ 79 #if (1 == LOGGER_CONFIG_USE_SEMIHOSTING) 80 81 initialise_monitor_handles(); 82 83 #endif 84 85 /* USER CODE END 1 */ 86 87 /* MCU Configuration--------------------------------------------------------*/ 88 89 /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ 90 HAL_Init(); 91 92 /* USER CODE BEGIN Init */ 93 94 /* USER CODE END Init */ 95 96 /* Configure the system clock */ 97 SystemClock_Config(); 98 99 /* USER CODE BEGIN SysInit */ 100 101 /* USER CODE END SysInit */ 102 103 /* Initialize all configured peripherals */ 104 MX_GPIO_Init(); 105 MX_USART2_UART_Init(); 106 /* USER CODE BEGIN 2 */ 107 108 /* Application Init */ 109 app_init(); 110 111 /* USER CODE END 2 */ 112 113 /* Infinite loop */ 114 /* USER CODE BEGIN WHILE */ 115 while (1) 116 { 117 /* USER CODE END WHILE */ 118 119 /* USER CODE BEGIN 3 */ 120 121 /* Application Update */ 122 app_update(); 123 } 124 /* USER CODE END 3 */ 125 } 126 127 /** 128 * @brief System Clock Configuration 129 * @retval None 130 */ 131 void SystemClock_Config(void) 132 { 133 RCC_OscInitTypeDef RCC_OscInitStruct = {0}; 134 RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; 135 136 /** Initializes the RCC Oscillators according to the specified parameters 137 * in the RCC_OscInitTypeDef structure. 138 */ 139 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI; 140 RCC_OscInitStruct.HSIState = RCC_HSI_ON; 141 RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT; 142 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; 143 RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI_DIV2; 144 RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL16; 145 if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) 146 { 147 Error_Handler(); 148 } 149 150 /** Initializes the CPU, AHB and APB buses clocks 151 */ 152 RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK 153 |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2; 154 RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; 155 RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; 156 RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2; 157 RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; 158 159 if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK) 160 { 161 Error_Handler(); 162 } 163 } 164 165 /** 166 * @brief USART2 Initialization Function 167 * @param None 168 * @retval None 169 */ 170 static void MX_USART2_UART_Init(void) 171 { 172 173 /* USER CODE BEGIN USART2_Init 0 */ 174 175 /* USER CODE END USART2_Init 0 */ 176 177 /* USER CODE BEGIN USART2_Init 1 */ 178 179 /* USER CODE END USART2_Init 1 */ 180 huart2.Instance = USART2; 181 huart2.Init.BaudRate = 115200; 182 huart2.Init.WordLength = UART_WORDLENGTH_8B; 183 huart2.Init.StopBits = UART_STOPBITS_1; 184 huart2.Init.Parity = UART_PARITY_NONE; 185 huart2.Init.Mode = UART_MODE_TX_RX; 186 huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE; 187 huart2.Init.OverSampling = UART_OVERSAMPLING_16; 188 if (HAL_UART_Init(&huart2) != HAL_OK) 189 { 190 Error_Handler(); 191 } 192 /* USER CODE BEGIN USART2_Init 2 */ 193 194 /* USER CODE END USART2_Init 2 */ 195 196 } 197 198 /** 199 * @brief GPIO Initialization Function 200 * @param None 201 * @retval None 202 */ 203 static void MX_GPIO_Init(void) 204 { 205 GPIO_InitTypeDef GPIO_InitStruct = {0}; 206 /* USER CODE BEGIN MX_GPIO_Init_1 */ 207 /* USER CODE END MX_GPIO_Init_1 */ 208 209 /* GPIO Ports Clock Enable */ 210 __HAL_RCC_GPIOC_CLK_ENABLE(); 211 __HAL_RCC_GPIOD_CLK_ENABLE(); 212 __HAL_RCC_GPIOA_CLK_ENABLE(); 213 __HAL_RCC_GPIOB_CLK_ENABLE(); 214 215 /*Configure GPIO pin Output Level */ 216 HAL_GPIO_WritePin(GPIOA, LD2_Pin|D7_Pin|D8_Pin, GPIO_PIN_RESET); 217 218 /*Configure GPIO pin Output Level */ 219 HAL_GPIO_WritePin(GPIOB, D6_Pin|D5_Pin|D4_Pin, GPIO_PIN_RESET); 220 221 /*Configure GPIO pin Output Level */ 222 HAL_GPIO_WritePin(D9_GPIO_Port, D9_Pin, GPIO_PIN_RESET); 223 224 /*Configure GPIO pin : B1_Pin */ 225 GPIO_InitStruct.Pin = B1_Pin; 226 GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING; 227 GPIO_InitStruct.Pull = GPIO_NOPULL; 228 HAL_GPIO_Init(B1_GPIO_Port, &GPIO_InitStruct); 229 230 /*Configure GPIO pin : LD2_Pin */ 231 GPIO_InitStruct.Pin = LD2_Pin; 232 GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; 233 GPIO_InitStruct.Pull = GPIO_NOPULL; 234 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; 235 HAL_GPIO_Init(LD2_GPIO_Port, &GPIO_InitStruct); 236 237 /*Configure GPIO pins : D12_Pin D11_Pin D2_Pin */ 238 GPIO_InitStruct.Pin = D12_Pin|D11_Pin|D2_Pin; 239 GPIO_InitStruct.Mode = GPIO_MODE_INPUT; 240 GPIO_InitStruct.Pull = GPIO_PULLUP; 241 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); 242 243 /*Configure GPIO pins : D6_Pin D5_Pin D4_Pin */ 244 GPIO_InitStruct.Pin = D6_Pin|D5_Pin|D4_Pin; 245 GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; 246 GPIO_InitStruct.Pull = GPIO_PULLUP; 247 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; 248 HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); 249 250 /*Configure GPIO pin : D9_Pin */ 251 GPIO_InitStruct.Pin = D9_Pin; 252 GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; 253 GPIO_InitStruct.Pull = GPIO_PULLUP; 254 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; 255 HAL_GPIO_Init(D9_GPIO_Port, &GPIO_InitStruct); 256 257 /*Configure GPIO pins : D7_Pin D8_Pin */ 258 GPIO_InitStruct.Pin = D7_Pin|D8_Pin; 259 GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; 260 GPIO_InitStruct.Pull = GPIO_PULLUP; 261 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; 262 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); 263 264 /*Configure GPIO pin : D10_Pin */ 265 GPIO_InitStruct.Pin = D10_Pin; 266 GPIO_InitStruct.Mode = GPIO_MODE_INPUT; 267 GPIO_InitStruct.Pull = GPIO_PULLUP; 268 HAL_GPIO_Init(D10_GPIO_Port, &GPIO_InitStruct); 269 270 /* EXTI interrupt init*/ 271 HAL_NVIC_SetPriority(EXTI15_10_IRQn, 0, 0); 272 HAL_NVIC_EnableIRQ(EXTI15_10_IRQn); 273 274 /* USER CODE BEGIN MX_GPIO_Init_2 */ 275 /* USER CODE END MX_GPIO_Init_2 */ 276 } 277 278 /* USER CODE BEGIN 4 */ 279 280 /* USER CODE END 4 */ 281 282 /** 283 * @brief This function is executed in case of error occurrence. 284 * @retval None 285 */ 286 void Error_Handler(void) 287 { 288 /* USER CODE BEGIN Error_Handler_Debug */ 289 /* User can add his own implementation to report the HAL error return state */ 290 __disable_irq(); 291 while (1) 292 { 293 } 294 /* USER CODE END Error_Handler_Debug */ 295 } 296 #ifdef USE_FULL_ASSERT 297 /** 298 * @brief Reports the name of the source file and the source line number 299 * where the assert_param error has occurred. 300 * @param file: pointer to the source file name 301 * @param line: assert_param error line source number 302 * @retval None 303 */ 304 void assert_failed(uint8_t *file, uint32_t line) 305 { 306 /* USER CODE BEGIN 6 */ 307 /* User can add his own implementation to report the file name and line number, 308 ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ 309 /* USER CODE END 6 */ 310 } 311 #endif /* USE_FULL_ASSERT */
