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 ADC_HandleTypeDef hadc1; 48 49 UART_HandleTypeDef huart2; 50 51 /* USER CODE BEGIN PV */ 52 53 /* USER CODE END PV */ 54 55 /* Private function prototypes -----------------------------------------------*/ 56 void SystemClock_Config(void); 57 static void MX_GPIO_Init(void); 58 static void MX_USART2_UART_Init(void); 59 static void MX_ADC1_Init(void); 60 /* USER CODE BEGIN PFP */ 61 62 /* USER CODE END PFP */ 63 64 /* Private user code ---------------------------------------------------------*/ 65 /* USER CODE BEGIN 0 */ 66 #if (1 == LOGGER_CONFIG_USE_SEMIHOSTING) 67 extern void initialise_monitor_handles(void); 68 #endif 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 initialise_monitor_handles(); 81 #endif 82 83 /* USER CODE END 1 */ 84 85 /* MCU Configuration--------------------------------------------------------*/ 86 87 /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ 88 HAL_Init(); 89 90 /* USER CODE BEGIN Init */ 91 92 /* USER CODE END Init */ 93 94 /* Configure the system clock */ 95 SystemClock_Config(); 96 97 /* USER CODE BEGIN SysInit */ 98 99 /* USER CODE END SysInit */ 100 101 /* Initialize all configured peripherals */ 102 MX_GPIO_Init(); 103 MX_USART2_UART_Init(); 104 MX_ADC1_Init(); 105 /* USER CODE BEGIN 2 */ 106 /* Application init */ 107 app_init(); 108 /* USER CODE END 2 */ 109 110 /* Infinite loop */ 111 /* USER CODE BEGIN WHILE */ 112 while (1) 113 { 114 /* USER CODE END WHILE */ 115 116 /* USER CODE BEGIN 3 */ 117 /* Application Update */ 118 app_update(); 119 } 120 /* USER CODE END 3 */ 121 } 122 123 /** 124 * @brief System Clock Configuration 125 * @retval None 126 */ 127 void SystemClock_Config(void) 128 { 129 RCC_OscInitTypeDef RCC_OscInitStruct = {0}; 130 RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; 131 RCC_PeriphCLKInitTypeDef PeriphClkInit = {0}; 132 133 /** Initializes the RCC Oscillators according to the specified parameters 134 * in the RCC_OscInitTypeDef structure. 135 */ 136 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI; 137 RCC_OscInitStruct.HSIState = RCC_HSI_ON; 138 RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT; 139 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; 140 RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI_DIV2; 141 RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL2; 142 if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) 143 { 144 Error_Handler(); 145 } 146 147 /** Initializes the CPU, AHB and APB buses clocks 148 */ 149 RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK 150 |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2; 151 RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; 152 RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; 153 RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2; 154 RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; 155 156 if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK) 157 { 158 Error_Handler(); 159 } 160 PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC; 161 PeriphClkInit.AdcClockSelection = RCC_ADCPCLK2_DIV2; 162 if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK) 163 { 164 Error_Handler(); 165 } 166 } 167 168 /** 169 * @brief ADC1 Initialization Function 170 * @param None 171 * @retval None 172 */ 173 static void MX_ADC1_Init(void) 174 { 175 176 /* USER CODE BEGIN ADC1_Init 0 */ 177 178 /* USER CODE END ADC1_Init 0 */ 179 180 ADC_ChannelConfTypeDef sConfig = {0}; 181 182 /* USER CODE BEGIN ADC1_Init 1 */ 183 184 /* USER CODE END ADC1_Init 1 */ 185 186 /** Common config 187 */ 188 hadc1.Instance = ADC1; 189 hadc1.Init.ScanConvMode = ADC_SCAN_DISABLE; 190 hadc1.Init.ContinuousConvMode = DISABLE; 191 hadc1.Init.DiscontinuousConvMode = DISABLE; 192 hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START; 193 hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT; 194 hadc1.Init.NbrOfConversion = 1; 195 if (HAL_ADC_Init(&hadc1) != HAL_OK) 196 { 197 Error_Handler(); 198 } 199 200 /** Configure Regular Channel 201 */ 202 sConfig.Channel = ADC_CHANNEL_0; 203 sConfig.Rank = ADC_REGULAR_RANK_1; 204 sConfig.SamplingTime = ADC_SAMPLETIME_1CYCLE_5; 205 if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) 206 { 207 Error_Handler(); 208 } 209 /* USER CODE BEGIN ADC1_Init 2 */ 210 211 /* USER CODE END ADC1_Init 2 */ 212 213 } 214 215 /** 216 * @brief USART2 Initialization Function 217 * @param None 218 * @retval None 219 */ 220 static void MX_USART2_UART_Init(void) 221 { 222 223 /* USER CODE BEGIN USART2_Init 0 */ 224 225 /* USER CODE END USART2_Init 0 */ 226 227 /* USER CODE BEGIN USART2_Init 1 */ 228 229 /* USER CODE END USART2_Init 1 */ 230 huart2.Instance = USART2; 231 huart2.Init.BaudRate = 115200; 232 huart2.Init.WordLength = UART_WORDLENGTH_8B; 233 huart2.Init.StopBits = UART_STOPBITS_1; 234 huart2.Init.Parity = UART_PARITY_NONE; 235 huart2.Init.Mode = UART_MODE_TX_RX; 236 huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE; 237 huart2.Init.OverSampling = UART_OVERSAMPLING_16; 238 if (HAL_UART_Init(&huart2) != HAL_OK) 239 { 240 Error_Handler(); 241 } 242 /* USER CODE BEGIN USART2_Init 2 */ 243 244 /* USER CODE END USART2_Init 2 */ 245 246 } 247 248 /** 249 * @brief GPIO Initialization Function 250 * @param None 251 * @retval None 252 */ 253 static void MX_GPIO_Init(void) 254 { 255 GPIO_InitTypeDef GPIO_InitStruct = {0}; 256 /* USER CODE BEGIN MX_GPIO_Init_1 */ 257 258 /* USER CODE END MX_GPIO_Init_1 */ 259 260 /* GPIO Ports Clock Enable */ 261 __HAL_RCC_GPIOC_CLK_ENABLE(); 262 __HAL_RCC_GPIOD_CLK_ENABLE(); 263 __HAL_RCC_GPIOA_CLK_ENABLE(); 264 __HAL_RCC_GPIOB_CLK_ENABLE(); 265 266 /*Configure GPIO pin Output Level */ 267 HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, GPIO_PIN_RESET); 268 269 /*Configure GPIO pin : B1_Pin */ 270 GPIO_InitStruct.Pin = B1_Pin; 271 GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING; 272 GPIO_InitStruct.Pull = GPIO_NOPULL; 273 HAL_GPIO_Init(B1_GPIO_Port, &GPIO_InitStruct); 274 275 /*Configure GPIO pin : LD2_Pin */ 276 GPIO_InitStruct.Pin = LD2_Pin; 277 GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; 278 GPIO_InitStruct.Pull = GPIO_NOPULL; 279 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; 280 HAL_GPIO_Init(LD2_GPIO_Port, &GPIO_InitStruct); 281 282 /* EXTI interrupt init*/ 283 HAL_NVIC_SetPriority(EXTI15_10_IRQn, 0, 0); 284 HAL_NVIC_EnableIRQ(EXTI15_10_IRQn); 285 286 /* USER CODE BEGIN MX_GPIO_Init_2 */ 287 288 /* USER CODE END MX_GPIO_Init_2 */ 289 } 290 291 /* USER CODE BEGIN 4 */ 292 293 /* USER CODE END 4 */ 294 295 /** 296 * @brief This function is executed in case of error occurrence. 297 * @retval None 298 */ 299 void Error_Handler(void) 300 { 301 /* USER CODE BEGIN Error_Handler_Debug */ 302 /* User can add his own implementation to report the HAL error return state */ 303 __disable_irq(); 304 while (1) 305 { 306 } 307 /* USER CODE END Error_Handler_Debug */ 308 } 309 #ifdef USE_FULL_ASSERT 310 /** 311 * @brief Reports the name of the source file and the source line number 312 * where the assert_param error has occurred. 313 * @param file: pointer to the source file name 314 * @param line: assert_param error line source number 315 * @retval None 316 */ 317 void assert_failed(uint8_t *file, uint32_t line) 318 { 319 /* USER CODE BEGIN 6 */ 320 /* User can add his own implementation to report the file name and line number, 321 ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ 322 /* USER CODE END 6 */ 323 } 324 #endif /* USE_FULL_ASSERT */
