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 huart1; 48 UART_HandleTypeDef huart2; 49 DMA_HandleTypeDef hdma_usart1_rx; 50 DMA_HandleTypeDef hdma_usart1_tx; 51 52 /* USER CODE BEGIN PV */ 53 54 /* USER CODE END PV */ 55 56 /* Private function prototypes -----------------------------------------------*/ 57 void SystemClock_Config(void); 58 static void MX_GPIO_Init(void); 59 static void MX_DMA_Init(void); 60 static void MX_USART2_UART_Init(void); 61 static void MX_USART1_UART_Init(void); 62 /* USER CODE BEGIN PFP */ 63 64 /* USER CODE END PFP */ 65 66 /* Private user code ---------------------------------------------------------*/ 67 /* USER CODE BEGIN 0 */ 68 #if (1 == LOGGER_CONFIG_USE_SEMIHOSTING) 69 extern void initialise_monitor_handles(void); 70 #endif 71 72 /* USER CODE END 0 */ 73 74 /** 75 * @brief The application entry point. 76 * @retval int 77 */ 78 int main(void) 79 { 80 81 /* USER CODE BEGIN 1 */ 82 #if (1 == LOGGER_CONFIG_USE_SEMIHOSTING) 83 initialise_monitor_handles(); 84 #endif 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_DMA_Init(); 106 MX_USART2_UART_Init(); 107 MX_USART1_UART_Init(); 108 /* USER CODE BEGIN 2 */ 109 110 /* Application Init */ 111 app_init(); 112 /* USER CODE END 2 */ 113 114 /* Infinite loop */ 115 /* USER CODE BEGIN WHILE */ 116 117 LOGGER_INFO("Running main loop..."); 118 while (1) 119 { 120 /* USER CODE END WHILE */ 121 122 /* USER CODE BEGIN 3 */ 123 124 /* Application Update */ 125 app_update(); 126 // send reset signal to sensor, a low pulse for no less than 480 us 127 // uint8_t data = 0xF0; 128 // HAL_UART_Transmit(&huart1, &data, 1, 100); 129 // DS18B20_Read_temp(); 130 // LOGGER_INFO("%d°C", (int)g_DS18B20_temp); 131 } 132 /* USER CODE END 3 */ 133 } 134 135 /** 136 * @brief System Clock Configuration 137 * @retval None 138 */ 139 void SystemClock_Config(void) 140 { 141 RCC_OscInitTypeDef RCC_OscInitStruct = 142 { 0 }; 143 RCC_ClkInitTypeDef RCC_ClkInitStruct = 144 { 0 }; 145 146 /** Initializes the RCC Oscillators according to the specified parameters 147 * in the RCC_OscInitTypeDef structure. 148 */ 149 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI; 150 RCC_OscInitStruct.HSIState = RCC_HSI_ON; 151 RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT; 152 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; 153 RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI_DIV2; 154 RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL16; 155 if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) 156 { 157 Error_Handler(); 158 } 159 160 /** Initializes the CPU, AHB and APB buses clocks 161 */ 162 RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK 163 | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2; 164 RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; 165 RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; 166 RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2; 167 RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; 168 169 if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK) 170 { 171 Error_Handler(); 172 } 173 } 174 175 /** 176 * @brief USART1 Initialization Function 177 * @param None 178 * @retval None 179 */ 180 static void MX_USART1_UART_Init(void) 181 { 182 183 /* USER CODE BEGIN USART1_Init 0 */ 184 185 /* USER CODE END USART1_Init 0 */ 186 187 /* USER CODE BEGIN USART1_Init 1 */ 188 189 /* USER CODE END USART1_Init 1 */ 190 huart1.Instance = USART1; 191 huart1.Init.BaudRate = 115200; 192 huart1.Init.WordLength = UART_WORDLENGTH_8B; 193 huart1.Init.StopBits = UART_STOPBITS_1; 194 huart1.Init.Parity = UART_PARITY_NONE; 195 huart1.Init.Mode = UART_MODE_TX_RX; 196 huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE; 197 huart1.Init.OverSampling = UART_OVERSAMPLING_16; 198 if (HAL_HalfDuplex_Init(&huart1) != HAL_OK) 199 { 200 Error_Handler(); 201 } 202 /* USER CODE BEGIN USART1_Init 2 */ 203 204 /* USER CODE END USART1_Init 2 */ 205 206 } 207 208 /** 209 * @brief USART2 Initialization Function 210 * @param None 211 * @retval None 212 */ 213 static void MX_USART2_UART_Init(void) 214 { 215 216 /* USER CODE BEGIN USART2_Init 0 */ 217 218 /* USER CODE END USART2_Init 0 */ 219 220 /* USER CODE BEGIN USART2_Init 1 */ 221 222 /* USER CODE END USART2_Init 1 */ 223 huart2.Instance = USART2; 224 huart2.Init.BaudRate = 115200; 225 huart2.Init.WordLength = UART_WORDLENGTH_8B; 226 huart2.Init.StopBits = UART_STOPBITS_1; 227 huart2.Init.Parity = UART_PARITY_NONE; 228 huart2.Init.Mode = UART_MODE_TX_RX; 229 huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE; 230 huart2.Init.OverSampling = UART_OVERSAMPLING_16; 231 if (HAL_UART_Init(&huart2) != HAL_OK) 232 { 233 Error_Handler(); 234 } 235 /* USER CODE BEGIN USART2_Init 2 */ 236 237 /* USER CODE END USART2_Init 2 */ 238 239 } 240 241 /** 242 * Enable DMA controller clock 243 */ 244 static void MX_DMA_Init(void) 245 { 246 247 /* DMA controller clock enable */ 248 __HAL_RCC_DMA1_CLK_ENABLE(); 249 250 /* DMA interrupt init */ 251 /* DMA1_Channel4_IRQn interrupt configuration */ 252 HAL_NVIC_SetPriority(DMA1_Channel4_IRQn, 0, 0); 253 HAL_NVIC_EnableIRQ(DMA1_Channel4_IRQn); 254 /* DMA1_Channel5_IRQn interrupt configuration */ 255 HAL_NVIC_SetPriority(DMA1_Channel5_IRQn, 0, 0); 256 HAL_NVIC_EnableIRQ(DMA1_Channel5_IRQn); 257 258 } 259 260 /** 261 * @brief GPIO Initialization Function 262 * @param None 263 * @retval None 264 */ 265 static void MX_GPIO_Init(void) 266 { 267 GPIO_InitTypeDef GPIO_InitStruct = 268 { 0 }; 269 /* USER CODE BEGIN MX_GPIO_Init_1 */ 270 /* USER CODE END MX_GPIO_Init_1 */ 271 272 /* GPIO Ports Clock Enable */ 273 __HAL_RCC_GPIOC_CLK_ENABLE(); 274 __HAL_RCC_GPIOD_CLK_ENABLE(); 275 __HAL_RCC_GPIOA_CLK_ENABLE(); 276 __HAL_RCC_GPIOB_CLK_ENABLE(); 277 278 /*Configure GPIO pin Output Level */ 279 HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, GPIO_PIN_RESET); 280 281 /*Configure GPIO pin : B1_Pin */ 282 GPIO_InitStruct.Pin = B1_Pin; 283 GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING; 284 GPIO_InitStruct.Pull = GPIO_NOPULL; 285 HAL_GPIO_Init(B1_GPIO_Port, &GPIO_InitStruct); 286 287 /*Configure GPIO pin : LD2_Pin */ 288 GPIO_InitStruct.Pin = LD2_Pin; 289 GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; 290 GPIO_InitStruct.Pull = GPIO_NOPULL; 291 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; 292 HAL_GPIO_Init(LD2_GPIO_Port, &GPIO_InitStruct); 293 294 /* EXTI interrupt init*/ 295 HAL_NVIC_SetPriority(EXTI15_10_IRQn, 0, 0); 296 HAL_NVIC_EnableIRQ(EXTI15_10_IRQn); 297 298 /* USER CODE BEGIN MX_GPIO_Init_2 */ 299 /* USER CODE END MX_GPIO_Init_2 */ 300 } 301 302 /* USER CODE BEGIN 4 */ 303 304 /* USER CODE END 4 */ 305 306 /** 307 * @brief This function is executed in case of error occurrence. 308 * @retval None 309 */ 310 void Error_Handler(void) 311 { 312 /* USER CODE BEGIN Error_Handler_Debug */ 313 /* User can add his own implementation to report the HAL error return state */ 314 __disable_irq(); 315 while (1) 316 { 317 ; 318 } 319 /* USER CODE END Error_Handler_Debug */ 320 } 321 #ifdef USE_FULL_ASSERT 322 /** 323 * @brief Reports the name of the source file and the source line number 324 * where the assert_param error has occurred. 325 * @param file: pointer to the source file name 326 * @param line: assert_param error line source number 327 * @retval None 328 */ 329 void assert_failed(uint8_t *file, uint32_t line) 330 { 331 /* USER CODE BEGIN 6 */ 332 /* 333 * User can add his own implementation to report the file name and line 334 * number, i.e. `printf("Error: file %s on line %d\r\n", file, line) 335 */ 336 /* USER CODE END 6 */ 337 } 338 #endif /* USE_FULL_ASSERT */
