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