source: trunk/firmware_v2/SES/src/main.c@ 28

Last change on this file since 28 was 28, checked in by f.jahn, 8 weeks ago

24V Version implementiert

File size: 8.2 KB
Line 
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#include "iwdg.h"
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"
35#include "fan.h"
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 */
54int oldTimeMSTick;
55int oldTimeSeconds;
56int 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 */
67static volatile uint32_t adcData[5];
68/* USER CODE END PV */
69
70/* Private function prototypes -----------------------------------------------*/
71void 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 */
85int main(void)
86{
87
88 /* USER CODE BEGIN 1 */
89 int temperature = 0;
90 unsigned pwm = 0U;
91 uint32_t uin = 0;
92 uint32_t uout = 0;
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();
115 MX_TIM16_Init();
116 MX_TIM17_Init();
117 MX_IWDG_Init();
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);
124 HAL_Delay(500);
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
146
147extern 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
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 */
165 HAL_IWDG_Refresh(&hiwdg);
166
167
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;
184 CHIP_TEMPERATURE_Exec(adcData[4]);
185 temperature=CHIP_TEMPERATURE_GetTemp();
186 if ( temperature > 50)
187 {
188 pwm++;
189 if (pwm > 100U) pwm = 100U;
190
191 }
192 if (temperature < 50)
193 {
194 if (pwm > 0) pwm--;
195 }
196
197 FAN_SetSpeed(pwm);
198
199 uin = VOLTAGE_MEAS_GetUin();
200 uout = VOLTAGE_MEAS_GetUout();
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);
205
206
207 } //endsecond tick
208
209
210
211 switch (MODESWITCH_GetMode())
212 {
213 case MODE_MAINSWITCH: //0
214 MODE_MAINSWITCH_Exec();
215 break;
216
217 case MODE_MAINSWITCH_SECONDARYPROTECTION://1
218 MODE_SECONDARYPROTECTION_Exec();
219 break;
220
221 case MODE_MAINSWITCH_SECONDARYPROTECTION_PLUS://2
222 MODE_SECONDARYPROTECTION_PLUS_Exec();
223 break;
224
225 case MODE_MAINSWITCH_LVP_OVP://3
226 MODE_LVP_OVP_Exec();
227 break;
228
229 case MODE_MAINSWITCH_LVP://4
230 MODE_LVP_Exec();
231 break;
232
233 case MODE_MAINSWITCH_OVP://5
234 MODE_OVP_Exec();
235 break;
236
237
238
239
240 default:
241 printf("mode not yet implemented\n");
242
243 }
244 }
245
246 /* USER CODE END 3 */
247}
248
249/**
250 * @brief System Clock Configuration
251 * @retval None
252 */
253void SystemClock_Config(void)
254{
255 RCC_OscInitTypeDef RCC_OscInitStruct = {0};
256 RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
257
258 __HAL_FLASH_SET_LATENCY(FLASH_LATENCY_0);
259
260 /** Initializes the RCC Oscillators according to the specified parameters
261 * in the RCC_OscInitTypeDef structure.
262 */
263 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
264 RCC_OscInitStruct.HSIState = RCC_HSI_ON;
265 RCC_OscInitStruct.HSIDiv = RCC_HSI_DIV4;
266 RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
267 if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
268 {
269 Error_Handler();
270 }
271
272 /** Initializes the CPU, AHB and APB buses clocks
273 */
274 RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
275 |RCC_CLOCKTYPE_PCLK1;
276 RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
277 RCC_ClkInitStruct.SYSCLKDivider = RCC_SYSCLK_DIV1;
278 RCC_ClkInitStruct.AHBCLKDivider = RCC_HCLK_DIV1;
279 RCC_ClkInitStruct.APB1CLKDivider = RCC_APB1_DIV1;
280
281 if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
282 {
283 Error_Handler();
284 }
285}
286
287/* USER CODE BEGIN 4 */
288
289/* USER CODE END 4 */
290
291/**
292 * @brief This function is executed in case of error occurrence.
293 * @retval None
294 */
295void Error_Handler(void)
296{
297 /* USER CODE BEGIN Error_Handler_Debug */
298 /* User can add his own implementation to report the HAL error return state */
299 __disable_irq();
300 while (1)
301 {
302 }
303 /* USER CODE END Error_Handler_Debug */
304}
305#ifdef USE_FULL_ASSERT
306/**
307 * @brief Reports the name of the source file and the source line number
308 * where the assert_param error has occurred.
309 * @param file: pointer to the source file name
310 * @param line: assert_param error line source number
311 * @retval None
312 */
313void assert_failed(uint8_t *file, uint32_t line)
314{
315 /* USER CODE BEGIN 6 */
316 /* User can add his own implementation to report the file name and line number,
317 ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
318 /* USER CODE END 6 */
319}
320#endif /* USE_FULL_ASSERT */
Note: See TracBrowser for help on using the repository browser.