| [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"
|
|---|
| 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 |
|
|---|
| 88 | /* USER CODE END 1 */
|
|---|
| 89 |
|
|---|
| 90 | /* MCU Configuration--------------------------------------------------------*/
|
|---|
| 91 |
|
|---|
| 92 | /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
|
|---|
| 93 | HAL_Init();
|
|---|
| 94 |
|
|---|
| 95 | /* USER CODE BEGIN Init */
|
|---|
| 96 |
|
|---|
| 97 | /* USER CODE END Init */
|
|---|
| 98 |
|
|---|
| 99 | /* Configure the system clock */
|
|---|
| 100 | SystemClock_Config();
|
|---|
| 101 |
|
|---|
| 102 | /* USER CODE BEGIN SysInit */
|
|---|
| 103 |
|
|---|
| 104 | /* USER CODE END SysInit */
|
|---|
| 105 |
|
|---|
| 106 | /* Initialize all configured peripherals */
|
|---|
| 107 | MX_GPIO_Init();
|
|---|
| 108 | MX_DMA_Init();
|
|---|
| 109 | MX_ADC1_Init();
|
|---|
| 110 | MX_TIM16_Init();
|
|---|
| 111 | MX_TIM17_Init();
|
|---|
| 112 | // MX_USART1_UART_Init();
|
|---|
| 113 | /* USER CODE BEGIN 2 */
|
|---|
| 114 | printf("debug io test...ok\r\n");
|
|---|
| 115 | HAL_GPIO_WritePin(GPIO_OUTPUT_BUZZER_GPIO_Port, GPIO_OUTPUT_BUZZER_Pin, GPIO_PIN_SET);
|
|---|
| 116 | HAL_GPIO_WritePin(GPIO_OUTPUT_LED_ERROR_GPIO_Port, GPIO_OUTPUT_LED_ERROR_Pin, GPIO_PIN_SET);
|
|---|
| 117 | HAL_GPIO_WritePin(GPIO_OUTPUT_LED_ON_GPIO_Port, GPIO_OUTPUT_LED_ON_Pin, GPIO_PIN_SET);
|
|---|
| 118 | HAL_Delay(1000);
|
|---|
| 119 | HAL_GPIO_WritePin(GPIO_OUTPUT_BUZZER_GPIO_Port, GPIO_OUTPUT_BUZZER_Pin, GPIO_PIN_RESET);
|
|---|
| 120 | HAL_GPIO_WritePin(GPIO_OUTPUT_LED_ERROR_GPIO_Port, GPIO_OUTPUT_LED_ERROR_Pin, GPIO_PIN_RESET);
|
|---|
| 121 | HAL_GPIO_WritePin(GPIO_OUTPUT_LED_ON_GPIO_Port, GPIO_OUTPUT_LED_ON_Pin, GPIO_PIN_RESET);
|
|---|
| 122 |
|
|---|
| 123 | //--- RELAIS ZURÜCKSETZEN, damit es definitiv aus ist ---
|
|---|
| 124 | //Sicherstellen das nicht noch die Set Spule aktiv geschaltet ist
|
|---|
| 125 | HAL_GPIO_WritePin(GPIO_OUTPUT_RELAIS_SET_GPIO_Port, GPIO_OUTPUT_RELAIS_SET_Pin, GPIO_PIN_RESET);
|
|---|
| 126 |
|
|---|
| 127 | //Puls starten
|
|---|
| 128 | HAL_GPIO_WritePin(GPIO_OUTPUT_RELAIS_RESET_GPIO_Port, GPIO_OUTPUT_RELAIS_RESET_Pin, GPIO_PIN_SET);
|
|---|
| 129 | HAL_Delay(50);
|
|---|
| 130 | HAL_GPIO_WritePin(GPIO_OUTPUT_RELAIS_RESET_GPIO_Port, GPIO_OUTPUT_RELAIS_RESET_Pin, GPIO_PIN_RESET);
|
|---|
| 131 | //--- RELAIS ZURÜCKSETZEN ENDE ----
|
|---|
| 132 |
|
|---|
| 133 | MODESWITCH_ReadMode();
|
|---|
| 134 |
|
|---|
| 135 | CHIP_TEMPERATURE_Calibration(/*&sys_data*/);
|
|---|
| 136 | HAL_ADCEx_Calibration_Start(&hadc1);
|
|---|
| 137 | HAL_ADC_Start_DMA(&hadc1, (uint32_t*)adcData, 5);
|
|---|
| 138 |
|
|---|
| 139 |
|
|---|
| 140 | /* USER CODE END 2 */
|
|---|
| 141 |
|
|---|
| 142 | /* Infinite loop */
|
|---|
| 143 | /* USER CODE BEGIN WHILE */
|
|---|
| 144 | while (1)
|
|---|
| 145 | {
|
|---|
| 146 | /* USER CODE END WHILE */
|
|---|
| 147 |
|
|---|
| 148 | /* USER CODE BEGIN 3 */
|
|---|
| 149 | if (oldTimeMSTick != HAL_GetTick())
|
|---|
| 150 | {
|
|---|
| 151 | BUTTON_Exec();
|
|---|
| 152 | BUZZER_Exec();
|
|---|
| 153 | RELAIS_Exec();
|
|---|
| 154 | LEDS_Exec();
|
|---|
| 155 | VOLTAGE_MEAS_Exec(adcData[1],adcData[2]);
|
|---|
| 156 | oldTimeMSTick = HAL_GetTick();
|
|---|
| 157 | msCounter++;
|
|---|
| 158 | }
|
|---|
| 159 |
|
|---|
| 160 |
|
|---|
| 161 | //Sekunden tick
|
|---|
| 162 | if (msCounter >= 999)
|
|---|
| 163 | {
|
|---|
| 164 | msCounter = 0;
|
|---|
| 165 | CHIP_TEMPERATURE_Exec( adcData[4]);
|
|---|
| 166 |
|
|---|
| 167 | }
|
|---|
| 168 |
|
|---|
| 169 |
|
|---|
| 170 |
|
|---|
| 171 | switch (MODESWITCH_GetMode())
|
|---|
| 172 | {
|
|---|
| 173 | case MODE_MAINSWITCH:
|
|---|
| 174 | MODE_MAINSWITCH_Exec();
|
|---|
| 175 | break;
|
|---|
| 176 |
|
|---|
| 177 | case MODE_MAINSWITCH_SECONDARYPROTECTION:
|
|---|
| 178 | MODE_SECONDARYPROTECTION_Exec();
|
|---|
| 179 | break;
|
|---|
| 180 |
|
|---|
| 181 | case MODE_MAINSWITCH_SECONDARYPROTECTION_PLUS:
|
|---|
| 182 | MODE_SECONDARYPROTECTION_PLUS_Exec();
|
|---|
| 183 | break;
|
|---|
| 184 |
|
|---|
| 185 | case MODE_MAINSWITCH_LVP_OVP:
|
|---|
| 186 | MODE_LVP_OVP_Exec();
|
|---|
| 187 | break;
|
|---|
| 188 |
|
|---|
| 189 | //case MODE_MAINSWITCH_LVP:
|
|---|
| 190 | // MODE_LVP_Exec();
|
|---|
| 191 | //break;
|
|---|
| 192 |
|
|---|
| 193 | //case MODE_MAINSWITCH_OVP:
|
|---|
| 194 | // MODE_OVP_Exec();
|
|---|
| 195 | //break;
|
|---|
| 196 |
|
|---|
| 197 |
|
|---|
| 198 |
|
|---|
| 199 |
|
|---|
| 200 | default:
|
|---|
| 201 | printf("mode not yet implemented\n");
|
|---|
| 202 |
|
|---|
| 203 | }
|
|---|
| 204 | }
|
|---|
| 205 |
|
|---|
| 206 | /* USER CODE END 3 */
|
|---|
| 207 | }
|
|---|
| 208 |
|
|---|
| 209 | /**
|
|---|
| 210 | * @brief System Clock Configuration
|
|---|
| 211 | * @retval None
|
|---|
| 212 | */
|
|---|
| 213 | void SystemClock_Config(void)
|
|---|
| 214 | {
|
|---|
| 215 | RCC_OscInitTypeDef RCC_OscInitStruct = {0};
|
|---|
| 216 | RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
|
|---|
| 217 |
|
|---|
| 218 | __HAL_FLASH_SET_LATENCY(FLASH_LATENCY_0);
|
|---|
| 219 |
|
|---|
| 220 | /** Initializes the RCC Oscillators according to the specified parameters
|
|---|
| 221 | * in the RCC_OscInitTypeDef structure.
|
|---|
| 222 | */
|
|---|
| 223 | RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
|
|---|
| 224 | RCC_OscInitStruct.HSIState = RCC_HSI_ON;
|
|---|
| 225 | RCC_OscInitStruct.HSIDiv = RCC_HSI_DIV4;
|
|---|
| 226 | RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
|
|---|
| 227 | if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
|
|---|
| 228 | {
|
|---|
| 229 | Error_Handler();
|
|---|
| 230 | }
|
|---|
| 231 |
|
|---|
| 232 | /** Initializes the CPU, AHB and APB buses clocks
|
|---|
| 233 | */
|
|---|
| 234 | RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|
|---|
| 235 | |RCC_CLOCKTYPE_PCLK1;
|
|---|
| 236 | RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
|
|---|
| 237 | RCC_ClkInitStruct.SYSCLKDivider = RCC_SYSCLK_DIV1;
|
|---|
| 238 | RCC_ClkInitStruct.AHBCLKDivider = RCC_HCLK_DIV1;
|
|---|
| 239 | RCC_ClkInitStruct.APB1CLKDivider = RCC_APB1_DIV1;
|
|---|
| 240 |
|
|---|
| 241 | if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
|
|---|
| 242 | {
|
|---|
| 243 | Error_Handler();
|
|---|
| 244 | }
|
|---|
| 245 | }
|
|---|
| 246 |
|
|---|
| 247 | /* USER CODE BEGIN 4 */
|
|---|
| 248 |
|
|---|
| 249 | /* USER CODE END 4 */
|
|---|
| 250 |
|
|---|
| 251 | /**
|
|---|
| 252 | * @brief This function is executed in case of error occurrence.
|
|---|
| 253 | * @retval None
|
|---|
| 254 | */
|
|---|
| 255 | void Error_Handler(void)
|
|---|
| 256 | {
|
|---|
| 257 | /* USER CODE BEGIN Error_Handler_Debug */
|
|---|
| 258 | /* User can add his own implementation to report the HAL error return state */
|
|---|
| 259 | __disable_irq();
|
|---|
| 260 | while (1)
|
|---|
| 261 | {
|
|---|
| 262 | }
|
|---|
| 263 | /* USER CODE END Error_Handler_Debug */
|
|---|
| 264 | }
|
|---|
| 265 | #ifdef USE_FULL_ASSERT
|
|---|
| 266 | /**
|
|---|
| 267 | * @brief Reports the name of the source file and the source line number
|
|---|
| 268 | * where the assert_param error has occurred.
|
|---|
| 269 | * @param file: pointer to the source file name
|
|---|
| 270 | * @param line: assert_param error line source number
|
|---|
| 271 | * @retval None
|
|---|
| 272 | */
|
|---|
| 273 | void assert_failed(uint8_t *file, uint32_t line)
|
|---|
| 274 | {
|
|---|
| 275 | /* USER CODE BEGIN 6 */
|
|---|
| 276 | /* User can add his own implementation to report the file name and line number,
|
|---|
| 277 | ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
|
|---|
| 278 | /* USER CODE END 6 */
|
|---|
| 279 | }
|
|---|
| 280 | #endif /* USE_FULL_ASSERT */
|
|---|