| [17] | 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 | #include "adc.h" | 
|---|
 | 22 | #include "dma.h" | 
|---|
 | 23 | #include "tim.h" | 
|---|
 | 24 | #include "usart.h" | 
|---|
 | 25 | #include "gpio.h" | 
|---|
| [28] | 26 | #include "iwdg.h" | 
|---|
| [17] | 27 |  | 
|---|
 | 28 | /* Private includes ----------------------------------------------------------*/ | 
|---|
 | 29 | /* USER CODE BEGIN Includes */ | 
|---|
 | 30 | #include <stdio.h> | 
|---|
 | 31 | #include "button.h" | 
|---|
 | 32 | #include "buzzer.h" | 
|---|
 | 33 | #include "relais.h" | 
|---|
 | 34 | #include "leds.h" | 
|---|
| [24] | 35 | #include "fan.h" | 
|---|
| [17] | 36 | #include "chip_temperature.h" | 
|---|
 | 37 | #include "modeswitch.h" | 
|---|
 | 38 | #include "mode_mainswitch.h" | 
|---|
 | 39 | #include "mode_secondaryprotection.h" | 
|---|
 | 40 | #include "mode_secondaryprotection_plus.h" | 
|---|
 | 41 | #include "mode_lvp.h" | 
|---|
 | 42 | #include "mode_ovp.h" | 
|---|
 | 43 | #include "mode_lvp_ovp.h" | 
|---|
 | 44 | #include "voltage_meas.h" | 
|---|
 | 45 | /* USER CODE END Includes */ | 
|---|
 | 46 |  | 
|---|
 | 47 | /* Private typedef -----------------------------------------------------------*/ | 
|---|
 | 48 | /* USER CODE BEGIN PTD */ | 
|---|
 | 49 |  | 
|---|
 | 50 | /* USER CODE END PTD */ | 
|---|
 | 51 |  | 
|---|
 | 52 | /* Private define ------------------------------------------------------------*/ | 
|---|
 | 53 | /* USER CODE BEGIN PD */ | 
|---|
 | 54 | int oldTimeMSTick; | 
|---|
 | 55 | int oldTimeSeconds; | 
|---|
 | 56 | int msCounter; | 
|---|
 | 57 | /* USER CODE END PD */ | 
|---|
 | 58 |  | 
|---|
 | 59 | /* Private macro -------------------------------------------------------------*/ | 
|---|
 | 60 | /* USER CODE BEGIN PM */ | 
|---|
 | 61 |  | 
|---|
 | 62 | /* USER CODE END PM */ | 
|---|
 | 63 |  | 
|---|
 | 64 | /* Private variables ---------------------------------------------------------*/ | 
|---|
 | 65 |  | 
|---|
 | 66 | /* USER CODE BEGIN PV */ | 
|---|
 | 67 | static volatile uint32_t adcData[5]; | 
|---|
 | 68 | /* USER CODE END PV */ | 
|---|
 | 69 |  | 
|---|
 | 70 | /* Private function prototypes -----------------------------------------------*/ | 
|---|
 | 71 | void SystemClock_Config(void); | 
|---|
 | 72 | /* USER CODE BEGIN PFP */ | 
|---|
 | 73 |  | 
|---|
 | 74 | /* USER CODE END PFP */ | 
|---|
 | 75 |  | 
|---|
 | 76 | /* Private user code ---------------------------------------------------------*/ | 
|---|
 | 77 | /* USER CODE BEGIN 0 */ | 
|---|
 | 78 |  | 
|---|
 | 79 | /* USER CODE END 0 */ | 
|---|
 | 80 |  | 
|---|
 | 81 | /** | 
|---|
 | 82 |   * @brief  The application entry point. | 
|---|
 | 83 |   * @retval int | 
|---|
 | 84 |   */ | 
|---|
 | 85 | int main(void) | 
|---|
 | 86 | { | 
|---|
 | 87 |  | 
|---|
 | 88 |   /* USER CODE BEGIN 1 */ | 
|---|
| [23] | 89 |   int temperature = 0; | 
|---|
 | 90 |   unsigned pwm = 0U; | 
|---|
 | 91 |   uint32_t uin = 0; | 
|---|
 | 92 |   uint32_t uout = 0; | 
|---|
| [17] | 93 |   /* USER CODE END 1 */ | 
|---|
 | 94 |  | 
|---|
 | 95 |   /* MCU Configuration--------------------------------------------------------*/ | 
|---|
 | 96 |  | 
|---|
 | 97 |   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ | 
|---|
 | 98 |   HAL_Init(); | 
|---|
 | 99 |  | 
|---|
 | 100 |   /* USER CODE BEGIN Init */ | 
|---|
 | 101 |  | 
|---|
 | 102 |   /* USER CODE END Init */ | 
|---|
 | 103 |  | 
|---|
 | 104 |   /* Configure the system clock */ | 
|---|
 | 105 |   SystemClock_Config(); | 
|---|
 | 106 |  | 
|---|
 | 107 |   /* USER CODE BEGIN SysInit */ | 
|---|
 | 108 |  | 
|---|
 | 109 |   /* USER CODE END SysInit */ | 
|---|
 | 110 |  | 
|---|
 | 111 |   /* Initialize all configured peripherals */ | 
|---|
 | 112 |   MX_GPIO_Init(); | 
|---|
 | 113 |   MX_DMA_Init(); | 
|---|
 | 114 |   MX_ADC1_Init(); | 
|---|
| [23] | 115 |   MX_TIM16_Init(); | 
|---|
| [17] | 116 |   MX_TIM17_Init(); | 
|---|
| [28] | 117 |   MX_IWDG_Init(); | 
|---|
| [17] | 118 |  // MX_USART1_UART_Init(); | 
|---|
 | 119 |   /* USER CODE BEGIN 2 */ | 
|---|
 | 120 |   printf("debug io test...ok\r\n"); | 
|---|
 | 121 |   HAL_GPIO_WritePin(GPIO_OUTPUT_BUZZER_GPIO_Port, GPIO_OUTPUT_BUZZER_Pin, GPIO_PIN_SET); | 
|---|
 | 122 |   HAL_GPIO_WritePin(GPIO_OUTPUT_LED_ERROR_GPIO_Port, GPIO_OUTPUT_LED_ERROR_Pin, GPIO_PIN_SET); | 
|---|
 | 123 |   HAL_GPIO_WritePin(GPIO_OUTPUT_LED_ON_GPIO_Port, GPIO_OUTPUT_LED_ON_Pin, GPIO_PIN_SET); | 
|---|
| [28] | 124 |   HAL_Delay(500); | 
|---|
| [17] | 125 |   HAL_GPIO_WritePin(GPIO_OUTPUT_BUZZER_GPIO_Port, GPIO_OUTPUT_BUZZER_Pin, GPIO_PIN_RESET); | 
|---|
 | 126 |   HAL_GPIO_WritePin(GPIO_OUTPUT_LED_ERROR_GPIO_Port, GPIO_OUTPUT_LED_ERROR_Pin, GPIO_PIN_RESET); | 
|---|
 | 127 |   HAL_GPIO_WritePin(GPIO_OUTPUT_LED_ON_GPIO_Port, GPIO_OUTPUT_LED_ON_Pin, GPIO_PIN_RESET); | 
|---|
 | 128 |  | 
|---|
 | 129 |     //--- RELAIS ZURÜCKSETZEN, damit es definitiv aus ist --- | 
|---|
 | 130 |   //Sicherstellen das nicht noch die Set Spule aktiv geschaltet ist | 
|---|
 | 131 |   HAL_GPIO_WritePin(GPIO_OUTPUT_RELAIS_SET_GPIO_Port, GPIO_OUTPUT_RELAIS_SET_Pin, GPIO_PIN_RESET); | 
|---|
 | 132 |  | 
|---|
 | 133 |   //Puls starten | 
|---|
 | 134 |   HAL_GPIO_WritePin(GPIO_OUTPUT_RELAIS_RESET_GPIO_Port, GPIO_OUTPUT_RELAIS_RESET_Pin, GPIO_PIN_SET); | 
|---|
 | 135 |   HAL_Delay(50); | 
|---|
 | 136 |   HAL_GPIO_WritePin(GPIO_OUTPUT_RELAIS_RESET_GPIO_Port, GPIO_OUTPUT_RELAIS_RESET_Pin, GPIO_PIN_RESET); | 
|---|
 | 137 |   //--- RELAIS ZURÜCKSETZEN ENDE ---- | 
|---|
 | 138 |  | 
|---|
 | 139 |   MODESWITCH_ReadMode(); | 
|---|
 | 140 |  | 
|---|
 | 141 |   CHIP_TEMPERATURE_Calibration(/*&sys_data*/); | 
|---|
 | 142 |   HAL_ADCEx_Calibration_Start(&hadc1); | 
|---|
 | 143 |   HAL_ADC_Start_DMA(&hadc1, (uint32_t*)adcData, 5); | 
|---|
 | 144 |  | 
|---|
 | 145 |  | 
|---|
| [24] | 146 |          | 
|---|
 | 147 | extern uint32_t frequency, ic_overflows; | 
|---|
 | 148 |   ic_overflows = 0U; | 
|---|
 | 149 |   __HAL_TIM_SetCounter(&htim16, 0U); | 
|---|
 | 150 |  | 
|---|
 | 151 |   HAL_TIM_Base_Start_IT(&htim16); | 
|---|
 | 152 |   HAL_TIM_IC_Start_IT(&htim16, TIM_CHANNEL_1); | 
|---|
 | 153 |   int oldcaptureValue=0; | 
|---|
 | 154 |  | 
|---|
 | 155 |  | 
|---|
| [17] | 156 |   /* USER CODE END 2 */ | 
|---|
 | 157 |  | 
|---|
 | 158 |   /* Infinite loop */ | 
|---|
 | 159 |   /* USER CODE BEGIN WHILE */ | 
|---|
 | 160 |   while (1) | 
|---|
 | 161 |   { | 
|---|
 | 162 |     /* USER CODE END WHILE */ | 
|---|
 | 163 |  | 
|---|
 | 164 |     /* USER CODE BEGIN 3 */ | 
|---|
| [28] | 165 |         HAL_IWDG_Refresh(&hiwdg); | 
|---|
 | 166 |  | 
|---|
 | 167 |  | 
|---|
| [17] | 168 |         if (oldTimeMSTick != HAL_GetTick()) | 
|---|
 | 169 |     { | 
|---|
 | 170 |       BUTTON_Exec(); | 
|---|
 | 171 |       BUZZER_Exec(); | 
|---|
 | 172 |       RELAIS_Exec(); | 
|---|
 | 173 |       LEDS_Exec(); | 
|---|
 | 174 |           VOLTAGE_MEAS_Exec(adcData[1],adcData[2]); | 
|---|
 | 175 |       oldTimeMSTick = HAL_GetTick(); | 
|---|
 | 176 |           msCounter++; | 
|---|
 | 177 |     } | 
|---|
 | 178 |  | 
|---|
 | 179 |  | 
|---|
 | 180 |         //Sekunden tick | 
|---|
 | 181 |         if (msCounter >= 999) | 
|---|
 | 182 |         { | 
|---|
 | 183 |           msCounter = 0; | 
|---|
| [24] | 184 |       CHIP_TEMPERATURE_Exec(adcData[4]); | 
|---|
 | 185 |           temperature=CHIP_TEMPERATURE_GetTemp(); | 
|---|
 | 186 |           if ( temperature > 50) | 
|---|
| [23] | 187 |           {   | 
|---|
 | 188 |                   pwm++; | 
|---|
 | 189 |                   if (pwm > 100U) pwm = 100U;    | 
|---|
 | 190 |                    | 
|---|
 | 191 |           } | 
|---|
 | 192 |           if (temperature < 50) | 
|---|
 | 193 |           { | 
|---|
 | 194 |                 if (pwm > 0) pwm--; | 
|---|
 | 195 |           } | 
|---|
| [24] | 196 |           | 
|---|
 | 197 |           FAN_SetSpeed(pwm); | 
|---|
| [19] | 198 |  | 
|---|
| [23] | 199 |           uin = VOLTAGE_MEAS_GetUin(); | 
|---|
 | 200 |           uout = VOLTAGE_MEAS_GetUout(); | 
|---|
| [24] | 201 |           uint32_t rpm; | 
|---|
 | 202 |           rpm = frequency / 2; | 
|---|
 | 203 |           rpm = rpm * 60; | 
|---|
 | 204 |           printf("uin= %d, uout=%d, temp=%d, pwm=%d, fan=%d\r\n", uin, uout, temperature,pwm,rpm); | 
|---|
| [23] | 205 |            | 
|---|
 | 206 |            | 
|---|
 | 207 |         } //endsecond tick | 
|---|
 | 208 |  | 
|---|
 | 209 |  | 
|---|
 | 210 |  | 
|---|
| [17] | 211 |     switch (MODESWITCH_GetMode()) | 
|---|
 | 212 |     { | 
|---|
| [24] | 213 |       case MODE_MAINSWITCH: //0 | 
|---|
| [17] | 214 |         MODE_MAINSWITCH_Exec(); | 
|---|
 | 215 |       break; | 
|---|
 | 216 |  | 
|---|
| [24] | 217 |       case MODE_MAINSWITCH_SECONDARYPROTECTION://1 | 
|---|
| [29] | 218 |         MODE_SECONDARYPROTECTION_Exec(0); | 
|---|
| [17] | 219 |       break; | 
|---|
 | 220 |  | 
|---|
| [24] | 221 |       case MODE_MAINSWITCH_SECONDARYPROTECTION_PLUS://2 | 
|---|
| [29] | 222 |         MODE_SECONDARYPROTECTION_PLUS_Exec(0); | 
|---|
| [17] | 223 |       break; | 
|---|
 | 224 |  | 
|---|
| [29] | 225 |       case MODE_MAINSWITCH_SECONDARYPROTECTION_AUTO_RETRY://3 | 
|---|
 | 226 |         MODE_SECONDARYPROTECTION_Exec(1); | 
|---|
 | 227 |       break; | 
|---|
 | 228 |  | 
|---|
 | 229 |       case MODE_MAINSWITCH_SECONDARYPROTECTION_PLUS_AUTO_RETRY://4 | 
|---|
 | 230 |         MODE_SECONDARYPROTECTION_PLUS_Exec(1); | 
|---|
 | 231 |       break; | 
|---|
 | 232 |  | 
|---|
 | 233 |           case MODE_MAINSWITCH_LVP_OVP://5 | 
|---|
| [24] | 234 |         MODE_LVP_OVP_Exec(); | 
|---|
| [17] | 235 |       break; | 
|---|
 | 236 |  | 
|---|
| [29] | 237 |       case MODE_MAINSWITCH_LVP://6 | 
|---|
| [24] | 238 |         MODE_LVP_Exec(); | 
|---|
 | 239 |       break; | 
|---|
| [17] | 240 |  | 
|---|
| [29] | 241 |       case MODE_MAINSWITCH_OVP://7 | 
|---|
| [24] | 242 |         MODE_OVP_Exec(); | 
|---|
 | 243 |       break; | 
|---|
| [17] | 244 |  | 
|---|
 | 245 |  | 
|---|
 | 246 |  | 
|---|
 | 247 |  | 
|---|
 | 248 |       default: | 
|---|
 | 249 |       printf("mode not yet implemented\n"); | 
|---|
 | 250 |  | 
|---|
 | 251 |     } | 
|---|
 | 252 |   } | 
|---|
 | 253 |    | 
|---|
 | 254 |   /* USER CODE END 3 */ | 
|---|
 | 255 | } | 
|---|
 | 256 |  | 
|---|
 | 257 | /** | 
|---|
 | 258 |   * @brief System Clock Configuration | 
|---|
 | 259 |   * @retval None | 
|---|
 | 260 |   */ | 
|---|
 | 261 | void SystemClock_Config(void) | 
|---|
 | 262 | { | 
|---|
 | 263 |   RCC_OscInitTypeDef RCC_OscInitStruct = {0}; | 
|---|
 | 264 |   RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; | 
|---|
 | 265 |  | 
|---|
 | 266 |   __HAL_FLASH_SET_LATENCY(FLASH_LATENCY_0); | 
|---|
 | 267 |  | 
|---|
 | 268 |   /** Initializes the RCC Oscillators according to the specified parameters | 
|---|
 | 269 |   * in the RCC_OscInitTypeDef structure. | 
|---|
 | 270 |   */ | 
|---|
 | 271 |   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI; | 
|---|
 | 272 |   RCC_OscInitStruct.HSIState = RCC_HSI_ON; | 
|---|
 | 273 |   RCC_OscInitStruct.HSIDiv = RCC_HSI_DIV4; | 
|---|
 | 274 |   RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT; | 
|---|
 | 275 |   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) | 
|---|
 | 276 |   { | 
|---|
 | 277 |     Error_Handler(); | 
|---|
 | 278 |   } | 
|---|
 | 279 |  | 
|---|
 | 280 |   /** Initializes the CPU, AHB and APB buses clocks | 
|---|
 | 281 |   */ | 
|---|
 | 282 |   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK | 
|---|
 | 283 |                               |RCC_CLOCKTYPE_PCLK1; | 
|---|
 | 284 |   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI; | 
|---|
 | 285 |   RCC_ClkInitStruct.SYSCLKDivider = RCC_SYSCLK_DIV1; | 
|---|
 | 286 |   RCC_ClkInitStruct.AHBCLKDivider = RCC_HCLK_DIV1; | 
|---|
 | 287 |   RCC_ClkInitStruct.APB1CLKDivider = RCC_APB1_DIV1; | 
|---|
 | 288 |  | 
|---|
 | 289 |   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK) | 
|---|
 | 290 |   { | 
|---|
 | 291 |     Error_Handler(); | 
|---|
 | 292 |   } | 
|---|
 | 293 | } | 
|---|
 | 294 |  | 
|---|
 | 295 | /* USER CODE BEGIN 4 */ | 
|---|
 | 296 |  | 
|---|
 | 297 | /* USER CODE END 4 */ | 
|---|
 | 298 |  | 
|---|
 | 299 | /** | 
|---|
 | 300 |   * @brief  This function is executed in case of error occurrence. | 
|---|
 | 301 |   * @retval None | 
|---|
 | 302 |   */ | 
|---|
 | 303 | void Error_Handler(void) | 
|---|
 | 304 | { | 
|---|
 | 305 |   /* USER CODE BEGIN Error_Handler_Debug */ | 
|---|
 | 306 |   /* User can add his own implementation to report the HAL error return state */ | 
|---|
 | 307 |   __disable_irq(); | 
|---|
 | 308 |   while (1) | 
|---|
 | 309 |   { | 
|---|
 | 310 |   } | 
|---|
 | 311 |   /* USER CODE END Error_Handler_Debug */ | 
|---|
 | 312 | } | 
|---|
 | 313 | #ifdef USE_FULL_ASSERT | 
|---|
 | 314 | /** | 
|---|
 | 315 |   * @brief  Reports the name of the source file and the source line number | 
|---|
 | 316 |   *         where the assert_param error has occurred. | 
|---|
 | 317 |   * @param  file: pointer to the source file name | 
|---|
 | 318 |   * @param  line: assert_param error line source number | 
|---|
 | 319 |   * @retval None | 
|---|
 | 320 |   */ | 
|---|
 | 321 | void assert_failed(uint8_t *file, uint32_t line) | 
|---|
 | 322 | { | 
|---|
 | 323 |   /* USER CODE BEGIN 6 */ | 
|---|
 | 324 |   /* User can add his own implementation to report the file name and line number, | 
|---|
 | 325 |      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ | 
|---|
 | 326 |   /* USER CODE END 6 */ | 
|---|
 | 327 | } | 
|---|
 | 328 | #endif /* USE_FULL_ASSERT */ | 
|---|