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