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 | #include "stdio.h" |
---|
25 | #include "button.h" |
---|
26 | #include "buzzer.h" |
---|
27 | #include "relais.h" |
---|
28 | #include "leds.h" |
---|
29 | #include "modeswitch.h" |
---|
30 | #include "mode_mainswitch.h" |
---|
31 | #include "mode_secondaryprotection.h" |
---|
32 | |
---|
33 | /* USER CODE END Includes */ |
---|
34 | |
---|
35 | /* Private typedef -----------------------------------------------------------*/ |
---|
36 | /* USER CODE BEGIN PTD */ |
---|
37 | |
---|
38 | /* USER CODE END PTD */ |
---|
39 | |
---|
40 | /* Private define ------------------------------------------------------------*/ |
---|
41 | /* USER CODE BEGIN PD */ |
---|
42 | int oldTimeMSTick; |
---|
43 | /* USER CODE END PD */ |
---|
44 | |
---|
45 | /* Private macro -------------------------------------------------------------*/ |
---|
46 | /* USER CODE BEGIN PM */ |
---|
47 | |
---|
48 | /* USER CODE END PM */ |
---|
49 | |
---|
50 | /* Private variables ---------------------------------------------------------*/ |
---|
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 | /* USER CODE BEGIN PFP */ |
---|
60 | |
---|
61 | /* USER CODE END PFP */ |
---|
62 | |
---|
63 | /* Private user code ---------------------------------------------------------*/ |
---|
64 | /* USER CODE BEGIN 0 */ |
---|
65 | |
---|
66 | /* USER CODE END 0 */ |
---|
67 | |
---|
68 | /** |
---|
69 | * @brief The application entry point. |
---|
70 | * @retval int |
---|
71 | */ |
---|
72 | int main(void) |
---|
73 | { |
---|
74 | |
---|
75 | /* USER CODE BEGIN 1 */ |
---|
76 | |
---|
77 | /* USER CODE END 1 */ |
---|
78 | |
---|
79 | /* MCU Configuration--------------------------------------------------------*/ |
---|
80 | |
---|
81 | /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ |
---|
82 | HAL_Init(); |
---|
83 | |
---|
84 | /* USER CODE BEGIN Init */ |
---|
85 | |
---|
86 | /* USER CODE END Init */ |
---|
87 | |
---|
88 | /* Configure the system clock */ |
---|
89 | SystemClock_Config(); |
---|
90 | |
---|
91 | /* USER CODE BEGIN SysInit */ |
---|
92 | |
---|
93 | /* USER CODE END SysInit */ |
---|
94 | |
---|
95 | /* Initialize all configured peripherals */ |
---|
96 | MX_GPIO_Init(); |
---|
97 | /* USER CODE BEGIN 2 */ |
---|
98 | //Selbsttest |
---|
99 | HAL_GPIO_WritePin(GPIO_OUTPUT_BUZZER_GPIO_Port, GPIO_OUTPUT_BUZZER_Pin, GPIO_PIN_SET); |
---|
100 | HAL_GPIO_WritePin(GPIO_OUTPUT_LED_ERROR_GPIO_Port, GPIO_OUTPUT_LED_ERROR_Pin, GPIO_PIN_SET); |
---|
101 | HAL_GPIO_WritePin(GPIO_OUTPUT_LED_ON_GPIO_Port, GPIO_OUTPUT_LED_ON_Pin, GPIO_PIN_SET); |
---|
102 | HAL_Delay(1000); |
---|
103 | HAL_GPIO_WritePin(GPIO_OUTPUT_BUZZER_GPIO_Port, GPIO_OUTPUT_BUZZER_Pin, GPIO_PIN_RESET); |
---|
104 | HAL_GPIO_WritePin(GPIO_OUTPUT_LED_ERROR_GPIO_Port, GPIO_OUTPUT_LED_ERROR_Pin, GPIO_PIN_RESET); |
---|
105 | HAL_GPIO_WritePin(GPIO_OUTPUT_LED_ON_GPIO_Port, GPIO_OUTPUT_LED_ON_Pin, GPIO_PIN_RESET); |
---|
106 | |
---|
107 | //--- RELAIS ZURÜCKSETZEN, damit es definitiv aus ist --- |
---|
108 | //Sicherstellen das nicht noch die Set Spule aktiv geschaltet ist |
---|
109 | HAL_GPIO_WritePin(GPIO_OUTPUT_RELAIS_SET_GPIO_Port, GPIO_OUTPUT_RELAIS_SET_Pin, GPIO_PIN_RESET); |
---|
110 | |
---|
111 | //Puls starten |
---|
112 | HAL_GPIO_WritePin(GPIO_OUTPUT_RELAIS_RESET_GPIO_Port, GPIO_OUTPUT_RELAIS_RESET_Pin, GPIO_PIN_SET); |
---|
113 | HAL_Delay(50); |
---|
114 | HAL_GPIO_WritePin(GPIO_OUTPUT_RELAIS_RESET_GPIO_Port, GPIO_OUTPUT_RELAIS_RESET_Pin, GPIO_PIN_RESET); |
---|
115 | //--- RELAIS ZURÜCKSETZEN ENDE ---- |
---|
116 | |
---|
117 | |
---|
118 | |
---|
119 | MODESWITCH_ReadMode(); |
---|
120 | |
---|
121 | /* USER CODE END 2 */ |
---|
122 | |
---|
123 | /* Infinite loop */ |
---|
124 | /* USER CODE BEGIN WHILE */ |
---|
125 | while (1) |
---|
126 | { |
---|
127 | /* USER CODE END WHILE */ |
---|
128 | |
---|
129 | /* USER CODE BEGIN 3 */ |
---|
130 | if (oldTimeMSTick != HAL_GetTick()) |
---|
131 | { |
---|
132 | BUTTON_Exec(); |
---|
133 | BUZZER_Exec(); |
---|
134 | RELAIS_Exec(); |
---|
135 | LEDS_Exec(); |
---|
136 | oldTimeMSTick = HAL_GetTick(); |
---|
137 | } |
---|
138 | |
---|
139 | |
---|
140 | switch (MODESWITCH_GetMode()) |
---|
141 | { |
---|
142 | case MODE_MAINSWITCH: |
---|
143 | MODE_MAINSWITCH_Exec(); |
---|
144 | break; |
---|
145 | |
---|
146 | case MODE_MAINSWITCH_SECONDARYPROTECTION: |
---|
147 | MODE_SECONDARYPROTECTION_Exec(); |
---|
148 | break; |
---|
149 | |
---|
150 | default: |
---|
151 | printf("mode not yet implemented\n"); |
---|
152 | |
---|
153 | } |
---|
154 | } |
---|
155 | /* USER CODE END 3 */ |
---|
156 | } |
---|
157 | |
---|
158 | /** |
---|
159 | * @brief System Clock Configuration |
---|
160 | * @retval None |
---|
161 | */ |
---|
162 | void SystemClock_Config(void) |
---|
163 | { |
---|
164 | RCC_OscInitTypeDef RCC_OscInitStruct = {0}; |
---|
165 | RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; |
---|
166 | |
---|
167 | __HAL_FLASH_SET_LATENCY(FLASH_LATENCY_0); |
---|
168 | |
---|
169 | /** Initializes the RCC Oscillators according to the specified parameters |
---|
170 | * in the RCC_OscInitTypeDef structure. |
---|
171 | */ |
---|
172 | RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI; |
---|
173 | RCC_OscInitStruct.HSIState = RCC_HSI_ON; |
---|
174 | RCC_OscInitStruct.HSIDiv = RCC_HSI_DIV4; |
---|
175 | RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT; |
---|
176 | if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) |
---|
177 | { |
---|
178 | Error_Handler(); |
---|
179 | } |
---|
180 | |
---|
181 | /** Initializes the CPU, AHB and APB buses clocks |
---|
182 | */ |
---|
183 | RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK |
---|
184 | |RCC_CLOCKTYPE_PCLK1; |
---|
185 | RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI; |
---|
186 | RCC_ClkInitStruct.SYSCLKDivider = RCC_SYSCLK_DIV1; |
---|
187 | RCC_ClkInitStruct.AHBCLKDivider = RCC_HCLK_DIV1; |
---|
188 | RCC_ClkInitStruct.APB1CLKDivider = RCC_APB1_DIV1; |
---|
189 | |
---|
190 | if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK) |
---|
191 | { |
---|
192 | Error_Handler(); |
---|
193 | } |
---|
194 | } |
---|
195 | |
---|
196 | /** |
---|
197 | * @brief GPIO Initialization Function |
---|
198 | * @param None |
---|
199 | * @retval None |
---|
200 | */ |
---|
201 | static void MX_GPIO_Init(void) |
---|
202 | { |
---|
203 | GPIO_InitTypeDef GPIO_InitStruct = {0}; |
---|
204 | /* USER CODE BEGIN MX_GPIO_Init_1 */ |
---|
205 | /* USER CODE END MX_GPIO_Init_1 */ |
---|
206 | |
---|
207 | /* GPIO Ports Clock Enable */ |
---|
208 | __HAL_RCC_GPIOB_CLK_ENABLE(); |
---|
209 | __HAL_RCC_GPIOC_CLK_ENABLE(); |
---|
210 | __HAL_RCC_GPIOA_CLK_ENABLE(); |
---|
211 | |
---|
212 | /*Configure GPIO pin Output Level */ |
---|
213 | HAL_GPIO_WritePin(GPIOA, GPIO_OUTPUT_LED_ON_Pin|GPIO_OUTPUT_LED_ERROR_Pin|GPIO_OUTPUT_RELAIS_SET_Pin|GPIO_OUTPUT_RELAIS_RESET_Pin |
---|
214 | |GPIO_OUTPUT_BUZZER_Pin, GPIO_PIN_RESET); |
---|
215 | |
---|
216 | /*Configure GPIO pins : GPIO_INPUT_MODE_B3_Pin GPIO_INPUT_MODE_B2_Pin */ |
---|
217 | GPIO_InitStruct.Pin = GPIO_INPUT_MODE_B3_Pin|GPIO_INPUT_MODE_B2_Pin; |
---|
218 | GPIO_InitStruct.Mode = GPIO_MODE_INPUT; |
---|
219 | GPIO_InitStruct.Pull = GPIO_PULLUP; |
---|
220 | HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); |
---|
221 | |
---|
222 | /*Configure GPIO pins : GPIO_INPUT_MODE_B1_Pin GPIO_INPUT_MODE_B0_Pin */ |
---|
223 | GPIO_InitStruct.Pin = GPIO_INPUT_MODE_B1_Pin|GPIO_INPUT_MODE_B0_Pin; |
---|
224 | GPIO_InitStruct.Mode = GPIO_MODE_INPUT; |
---|
225 | GPIO_InitStruct.Pull = GPIO_PULLUP; |
---|
226 | HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); |
---|
227 | |
---|
228 | /*Configure GPIO pins : GPIO_INPUT_LVP_Pin GPIO_INPUT_OVP_Pin */ |
---|
229 | GPIO_InitStruct.Pin = GPIO_INPUT_LVP_Pin|GPIO_INPUT_OVP_Pin; |
---|
230 | GPIO_InitStruct.Mode = GPIO_MODE_INPUT; |
---|
231 | GPIO_InitStruct.Pull = GPIO_PULLUP; |
---|
232 | HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); |
---|
233 | |
---|
234 | /*Configure GPIO pins : GPIO_INPUT_BTN_ON_Pin GPIO_INPUT_BTN_OFF_Pin GPIO_INPUT_FAULT_Pin GPIO_INPUT_BMS_Pin */ |
---|
235 | GPIO_InitStruct.Pin = GPIO_INPUT_BTN_ON_Pin|GPIO_INPUT_BTN_OFF_Pin|GPIO_INPUT_FAULT_Pin|GPIO_INPUT_BMS_Pin; |
---|
236 | GPIO_InitStruct.Mode = GPIO_MODE_INPUT; |
---|
237 | GPIO_InitStruct.Pull = GPIO_PULLUP; |
---|
238 | HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); |
---|
239 | |
---|
240 | /*Configure GPIO pins : GPIO_OUTPUT_LED_ON_Pin GPIO_OUTPUT_LED_ERROR_Pin GPIO_OUTPUT_RELAIS_SET_Pin GPIO_OUTPUT_RELAIS_RESET_Pin |
---|
241 | GPIO_OUTPUT_BUZZER_Pin */ |
---|
242 | GPIO_InitStruct.Pin = GPIO_OUTPUT_LED_ON_Pin|GPIO_OUTPUT_LED_ERROR_Pin|GPIO_OUTPUT_RELAIS_SET_Pin|GPIO_OUTPUT_RELAIS_RESET_Pin |
---|
243 | |GPIO_OUTPUT_BUZZER_Pin; |
---|
244 | GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; |
---|
245 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
---|
246 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; |
---|
247 | HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); |
---|
248 | |
---|
249 | /* USER CODE BEGIN MX_GPIO_Init_2 */ |
---|
250 | /* USER CODE END MX_GPIO_Init_2 */ |
---|
251 | } |
---|
252 | |
---|
253 | /* USER CODE BEGIN 4 */ |
---|
254 | |
---|
255 | /* USER CODE END 4 */ |
---|
256 | |
---|
257 | /** |
---|
258 | * @brief This function is executed in case of error occurrence. |
---|
259 | * @retval None |
---|
260 | */ |
---|
261 | void Error_Handler(void) |
---|
262 | { |
---|
263 | /* USER CODE BEGIN Error_Handler_Debug */ |
---|
264 | /* User can add his own implementation to report the HAL error return state */ |
---|
265 | __disable_irq(); |
---|
266 | while (1) |
---|
267 | { |
---|
268 | } |
---|
269 | /* USER CODE END Error_Handler_Debug */ |
---|
270 | } |
---|
271 | |
---|
272 | #ifdef USE_FULL_ASSERT |
---|
273 | /** |
---|
274 | * @brief Reports the name of the source file and the source line number |
---|
275 | * where the assert_param error has occurred. |
---|
276 | * @param file: pointer to the source file name |
---|
277 | * @param line: assert_param error line source number |
---|
278 | * @retval None |
---|
279 | */ |
---|
280 | void assert_failed(uint8_t *file, uint32_t line) |
---|
281 | { |
---|
282 | /* USER CODE BEGIN 6 */ |
---|
283 | /* User can add his own implementation to report the file name and line number, |
---|
284 | ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ |
---|
285 | /* USER CODE END 6 */ |
---|
286 | } |
---|
287 | #endif /* USE_FULL_ASSERT */ |
---|