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