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 extern void initialise_monitor_handles(void); 65 #endif 66 67 /* USER CODE END 0 */ 68 69 /** 70 * @brief The application entry point. 71 * @retval int 72 */ 73 int main(void) 74 { 75 76 /* USER CODE BEGIN 1 */ 77 #if (1 == LOGGER_CONFIG_USE_SEMIHOSTING) 78 initialise_monitor_handles(); 79 #endif 80 /* USER CODE END 1 */ 81 82 /* MCU Configuration--------------------------------------------------------*/ 83 84 /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ 85 HAL_Init(); 86 87 /* USER CODE BEGIN Init */ 88 89 /* USER CODE END Init */ 90 91 /* Configure the system clock */ 92 SystemClock_Config(); 93 94 /* USER CODE BEGIN SysInit */ 95 96 /* USER CODE END SysInit */ 97 98 /* Initialize all configured peripherals */ 99 MX_GPIO_Init(); 100 MX_USART2_UART_Init(); 101 /* USER CODE BEGIN 2 */ 102 103 /* Application Init */ 104 app_init(); 105 106 /* USER CODE END 2 */ 107 108 /* Infinite loop */ 109 /* USER CODE BEGIN WHILE */ 110 while (1) 111 { 112 /* USER CODE END WHILE */ 113 114 /* USER CODE BEGIN 3 */ 115 116 /* Application Update */ 117 app_update(); 118 } 119 /* USER CODE END 3 */ 120 } 121 122 /** 123 * @brief System Clock Configuration 124 * @retval None 125 */ 126 void SystemClock_Config(void) 127 { 128 RCC_OscInitTypeDef RCC_OscInitStruct = {0}; 129 RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; 130 131 /** Initializes the RCC Oscillators according to the specified parameters 132 * in the RCC_OscInitTypeDef structure. 133 */ 134 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI; 135 RCC_OscInitStruct.HSIState = RCC_HSI_ON; 136 RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT; 137 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; 138 RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI_DIV2; 139 RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL16; 140 if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) 141 { 142 Error_Handler(); 143 } 144 145 /** Initializes the CPU, AHB and APB buses clocks 146 */ 147 RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK 148 |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2; 149 RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; 150 RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; 151 RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2; 152 RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; 153 154 if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK) 155 { 156 Error_Handler(); 157 } 158 } 159 160 /** 161 * @brief USART2 Initialization Function 162 * @param None 163 * @retval None 164 */ 165 static void MX_USART2_UART_Init(void) 166 { 167 168 /* USER CODE BEGIN USART2_Init 0 */ 169 170 /* USER CODE END USART2_Init 0 */ 171 172 /* USER CODE BEGIN USART2_Init 1 */ 173 174 /* USER CODE END USART2_Init 1 */ 175 huart2.Instance = USART2; 176 huart2.Init.BaudRate = 115200; 177 huart2.Init.WordLength = UART_WORDLENGTH_8B; 178 huart2.Init.StopBits = UART_STOPBITS_1; 179 huart2.Init.Parity = UART_PARITY_NONE; 180 huart2.Init.Mode = UART_MODE_TX_RX; 181 huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE; 182 huart2.Init.OverSampling = UART_OVERSAMPLING_16; 183 if (HAL_UART_Init(&huart2) != HAL_OK) 184 { 185 Error_Handler(); 186 } 187 /* USER CODE BEGIN USART2_Init 2 */ 188 189 /* USER CODE END USART2_Init 2 */ 190 191 } 192 193 /** 194 * @brief GPIO Initialization Function 195 * @param None 196 * @retval None 197 */ 198 static void MX_GPIO_Init(void) 199 { 200 GPIO_InitTypeDef GPIO_InitStruct = {0}; 201 /* USER CODE BEGIN MX_GPIO_Init_1 */ 202 /* USER CODE END MX_GPIO_Init_1 */ 203 204 /* GPIO Ports Clock Enable */ 205 __HAL_RCC_GPIOC_CLK_ENABLE(); 206 __HAL_RCC_GPIOD_CLK_ENABLE(); 207 __HAL_RCC_GPIOA_CLK_ENABLE(); 208 __HAL_RCC_GPIOB_CLK_ENABLE(); 209 210 /*Configure GPIO pin Output Level */ 211 HAL_GPIO_WritePin(GPIOA, LD2_Pin|LED_A_Pin|LED_B_Pin, GPIO_PIN_RESET); 212 213 /*Configure GPIO pin : B1_Pin */ 214 GPIO_InitStruct.Pin = B1_Pin; 215 GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING; 216 GPIO_InitStruct.Pull = GPIO_NOPULL; 217 HAL_GPIO_Init(B1_GPIO_Port, &GPIO_InitStruct); 218 219 /*Configure GPIO pins : LD2_Pin LED_A_Pin LED_B_Pin */ 220 GPIO_InitStruct.Pin = LD2_Pin|LED_A_Pin|LED_B_Pin; 221 GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; 222 GPIO_InitStruct.Pull = GPIO_NOPULL; 223 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; 224 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); 225 226 /*Configure GPIO pins : BTN_B_Pin BTN_C_Pin BTN_D_Pin */ 227 GPIO_InitStruct.Pin = BTN_B_Pin|BTN_C_Pin|BTN_D_Pin; 228 GPIO_InitStruct.Mode = GPIO_MODE_INPUT; 229 GPIO_InitStruct.Pull = GPIO_PULLUP; 230 HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); 231 232 /* EXTI interrupt init*/ 233 HAL_NVIC_SetPriority(EXTI15_10_IRQn, 0, 0); 234 HAL_NVIC_EnableIRQ(EXTI15_10_IRQn); 235 236 /* USER CODE BEGIN MX_GPIO_Init_2 */ 237 /* USER CODE END MX_GPIO_Init_2 */ 238 } 239 240 /* USER CODE BEGIN 4 */ 241 242 /* USER CODE END 4 */ 243 244 /** 245 * @brief This function is executed in case of error occurrence. 246 * @retval None 247 */ 248 void Error_Handler(void) 249 { 250 /* USER CODE BEGIN Error_Handler_Debug */ 251 /* User can add his own implementation to report the HAL error return state */ 252 __disable_irq(); 253 while (1) 254 { 255 ; 256 } 257 /* USER CODE END Error_Handler_Debug */ 258 } 259 260 #ifdef USE_FULL_ASSERT 261 /** 262 * @brief Reports the name of the source file and the source line number 263 * where the assert_param error has occurred. 264 * @param file: pointer to the source file name 265 * @param line: assert_param error line source number 266 * @retval None 267 */ 268 void assert_failed(uint8_t *file, uint32_t line) 269 { 270 /* USER CODE BEGIN 6 */ 271 /* 272 * User can add his own implementation to report the file name and line 273 * number, i.e. `printf("Error: file %s on line %d\r\n", file, line) 274 */ 275 /* USER CODE END 6 */ 276 } 277 #endif /* USE_FULL_ASSERT */
