source: ecs_cellMon/firmware/Core/Src/main.c

Last change on this file was 13, checked in by f.jahn, 19 months ago

IWDG timeout is set to 1.024 sec.

File size: 12.9 KB
Line 
1/* USER CODE BEGIN Header */
2/**
3  ******************************************************************************
4  * @file           : main.c
5  * @brief          : Main program body
6  ******************************************************************************
7  * @attention
8  *
9  * <h2><center>&copy; Copyright (c) 2021 STMicroelectronics.
10  * All rights reserved.</center></h2>
11  *
12  * This software component is licensed by ST under BSD 3-Clause license,
13  * the "License"; You may not use this file except in compliance with the
14  * License. You may obtain a copy of the License at:
15  *                        opensource.org/licenses/BSD-3-Clause
16  *
17  ******************************************************************************
18  */
19/* USER CODE END Header */
20/* Includes ------------------------------------------------------------------*/
21#include "main.h"
22#include "adc.h"
23#include "comp.h"
24#include "dac.h"
25#include "dma.h"
26#include "tim.h"
27#include "usart.h"
28#include "gpio.h"
29
30/* Private includes ----------------------------------------------------------*/
31/* USER CODE BEGIN Includes */
32#include "led.h"
33#include "output.h"
34#include "meas.h"
35#include "sysdata.h"
36#include "balancer.h"
37#include "modbus.h"
38#include "eeprom.h"
39#include "bms.h"
40#include <stdio.h>
41/* USER CODE END Includes */
42
43/* Private typedef -----------------------------------------------------------*/
44/* USER CODE BEGIN PTD */
45#define LED_INTERVALL                 1
46#define BALANCER_EXEC_INTERVALL       1
47#define COMMUNICATION_TIMEOUT  (300000) //in ms --> 5 Minuten
48/* USER CODE END PTD */
49
50/* Private define ------------------------------------------------------------*/
51/* USER CODE BEGIN PD */
52/* USER CODE END PD */
53
54/* Private macro -------------------------------------------------------------*/
55/* USER CODE BEGIN PM */
56
57/* USER CODE END PM */
58
59/* Private variables ---------------------------------------------------------*/
60IWDG_HandleTypeDef hiwdg;
61
62/* USER CODE BEGIN PV */
63modbus_t modbusData;
64uint32_t communicationTimeoutCounter = 0;
65uint16_t keyAccepted=0;
66uint16_t savedLockKey;
67
68/* USER CODE END PV */
69
70/* Private function prototypes -----------------------------------------------*/
71void SystemClock_Config(void);
72/* USER CODE BEGIN PFP */
73bool SetFlashReadProtection(bool state);
74bool SetBootFromFlashAndReadOutProtection(void);
75void applyMBParameter(void);
76void resetCounters(void);
77void resetMinMaxValues(void);
78void mb_save_lock_key(void);
79static void MX_IWDG_Init(void);
80/* USER CODE END PFP */
81
82/* Private user code ---------------------------------------------------------*/
83/* USER CODE BEGIN 0 */
84
85/* USER CODE END 0 */
86
87/**
88  * @brief  The application entry point.
89  * @retval int
90  */
91int main(void)
92{
93  /* USER CODE BEGIN 1 */
94  uint32_t ledExecTime = 0;
95 // uint32_t balancerCheckTime = 0;
96  uint32_t balancerExecTime = 0;
97  uint32_t oldTime=0;
98
99
100  /* USER CODE END 1 */
101
102  /* MCU Configuration--------------------------------------------------------*/
103
104  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
105  HAL_Init();
106
107  /* USER CODE BEGIN Init */
108
109
110  /* USER CODE END Init */
111
112  /* Configure the system clock */
113  SystemClock_Config();
114
115  /* USER CODE BEGIN SysInit */
116
117  /* USER CODE END SysInit */
118
119  /* Initialize all configured peripherals */
120  MX_GPIO_Init();
121  MX_DMA_Init();
122  MX_ADC1_Init();
123  MX_COMP1_Init();
124  MX_COMP2_Init();
125  MX_DAC1_Init();
126  MX_TIM1_Init();
127  MX_USART1_UART_Init();
128  /* USER CODE BEGIN 2 */
129  HAL_Delay(100);
130  SYSDATA_Init();
131  if(EEPROM_isFirstStart())
132  {
133    // zurücksetzen aller Werte
134    printf("full restore...\n");
135    EEPROM_fullRestore(&sysData,false);
136    EEPROM_ResetLogData();
137  }
138
139  if(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_3) == GPIO_PIN_RESET)
140  {
141    // zurücksetzen auf Werkseinsellung
142    printf("factore restore (Pin)...\n");
143    EEPROM_fullRestore(&sysData,true);   
144  }
145
146  // Lade gespeicherte Einstellungen
147  printf("read config...\n");
148  EEPROM_readConfig(&sysData);
149  EEPROM_readLogData();
150
151
152  //Display Address with Error LED blinks
153  int n;
154  for (n=0; n<sysData.s.slaveAddress; n++)
155  {
156    LED_ErrorOn();
157    HAL_Delay(350);
158    LED_ErrorOff();
159    HAL_Delay(350);
160  }
161
162
163
164  LED_FunctionStartBlink(100,900);
165  OUTPUT_LVP_Disable();
166  OUTPUT_OVP_Disable();
167  BALANCER_Init();
168  MEAS_Init();
169
170  // Modbus Initialisierung
171  mbInit(&modbusData, sysData.s.baudrate, sysData.s.parityMode, &huart1, accessModeTable, &keyAccepted);
172 
173
174  MX_IWDG_Init();
175  /* USER CODE END 2 */
176
177  /* Infinite loop */
178  /* USER CODE BEGIN WHILE */
179  while (1)
180  {
181    HAL_IWDG_Refresh(&hiwdg);
182
183    if (HAL_GetTick() >= (ledExecTime + LED_INTERVALL))
184    {
185      LED_Exec();
186      ledExecTime = HAL_GetTick();
187    }
188
189
190    if (HAL_GetTick() >= (balancerExecTime + BALANCER_EXEC_INTERVALL))
191    {
192     
193      balancerExecTime = HAL_GetTick();
194      MEAS_Exec();
195      BMS_Exec();
196      BALANCER_Exec();   
197    }
198   
199       
200
201    if (HAL_GetTick() > oldTime)
202    {
203      //1ms Intervall
204      oldTime = HAL_GetTick();
205
206      if (communicationTimeoutCounter) 
207      {
208        communicationTimeoutCounter--;
209        if (communicationTimeoutCounter==0)
210        {
211          sysData.s.masterMode=0;
212        }
213
214      }
215    }
216
217    if (sysData.s.command != 0) 
218    {   
219      if  (modbusData.current_query ==  MB_QUERY_NOTHING)
220      {
221        //printf("CMD = %d\n", sys_data.s.parameter.command);
222        switch (sysData.s.command )
223        {
224          case CMD_SAVE_PARAMETER_TO_EEPROM: 
225            printf("EXE: Store parameter\n");
226            EEPROM_storeConfig(&sysData,0,false);
227            break;
228          case CMD_RESET_FACTORY:
229            printf("EXEC: Factory Restore\n");
230            EEPROM_fullRestore(&sysData,1);
231            break;
232          case CMD_SAVE_SN:
233            printf("EXEC: Store with sn\n");
234             EEPROM_storeConfig(&sysData,1,false);
235            break;
236          case CMD_RESET:
237            printf("EXEC: RESTART\n");
238            NVIC_SystemReset();
239            break;
240          case CMD_APPLY_MB_PARAMETER:
241            printf("EXEC: Apply MB Parameter\n");
242            applyMBParameter();
243            break;
244          case CMD_RESET_COUNTERS:
245            printf("EXEC: Reset Counters\n");
246            resetCounters();
247            break;
248          case CMD_RESET_MIN_MAX_VALUES:
249            printf("EXEC: Reset Min and Max Values\n");
250            resetMinMaxValues();
251            break;
252           case CMD_SAVE_LOCK_KEY:
253            printf("EXEC: Save lock key\n");
254            mb_save_lock_key();
255            break;
256           case CMD_DELAY:
257            printf("EXEC: Delay of 1 second! Device must reset.\n");
258            HAL_Delay(1500);
259            break;
260          default:
261            printf("UNKNOWN COMMAND\n");
262         
263        }
264        sysData.s.command = 0;
265      }
266      else
267      {
268        //printf("wait with execution till modbus communnikation finished\n");
269      }
270    }
271
272
273
274    // Modbus Kommunikation
275    if (mbGetFrameComplete(&modbusData) == true)
276    {
277      //printf("RX frame complete\n");
278      if (mbSlaveCheckModbusRtuQuery(&modbusData) == RESPOND_TO_QUERY) 
279      {
280        //printf("->send response...\n");
281        mbSlaveProcessRtuQuery(&modbusData);   
282
283        //Prüfe KEY
284        if (sysData.s.lockKey == savedLockKey)
285        {
286          sysData.s.keyAccepted = 1; 
287          keyAccepted = 1;       
288        }
289        else
290        {
291          sysData.s.keyAccepted = 0;
292          keyAccepted = 0;
293        }
294
295        //Wenn Ausgleichsspannungsregister von einem Mastergerät geändert wird. In den Master Modus gehen
296        if (sysData.s.balancerVoltageRemote != 0)
297        {
298          communicationTimeoutCounter = COMMUNICATION_TIMEOUT;
299          sysData.s.masterMode = 1;       
300          sysData.s.balancerVoltage = sysData.s.balancerVoltageRemote;
301        }
302
303      }
304      else
305      {
306        //printf("->don't respond...\n");
307        modbusData.uart->RxState = HAL_UART_STATE_BUSY_RX;
308      }
309    }
310    else
311    {
312      if (sysData.s.balancerPower == 0)
313      {
314  //      HAL_PWR_EnterSLEEPMode(PWR_MAINREGULATOR_ON,PWR_SLEEPENTRY_WFI);
315      }
316    }
317 
318
319    /* USER CODE END WHILE */
320
321    /* USER CODE BEGIN 3 */
322  }
323  /* USER CODE END 3 */
324}
325
326/**
327  * @brief System Clock Configuration
328  * @retval None
329  */
330void SystemClock_Config(void)
331{
332  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
333  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
334  RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
335
336  /** Configure the main internal regulator output voltage
337  */
338  HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1);
339  /** Initializes the RCC Oscillators according to the specified parameters
340  * in the RCC_OscInitTypeDef structure.
341  */
342  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
343  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
344  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
345  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
346  {
347    Error_Handler();
348  }
349  /** Initializes the CPU, AHB and APB buses clocks
350  */
351  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
352                              |RCC_CLOCKTYPE_PCLK1;
353  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSE;
354  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
355  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
356
357  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
358  {
359    Error_Handler();
360  }
361  /** Initializes the peripherals clocks
362  */
363  PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART1|RCC_PERIPHCLK_ADC
364                              |RCC_PERIPHCLK_TIM1;
365  PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK1;
366  PeriphClkInit.AdcClockSelection = RCC_ADCCLKSOURCE_SYSCLK;
367  PeriphClkInit.Tim1ClockSelection = RCC_TIM1CLKSOURCE_PCLK1;
368  if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
369  {
370    Error_Handler();
371  }
372}
373
374/* USER CODE BEGIN 4 */
375
376/**
377  * @brief IWDG Initialization Function
378  * @param None
379  * @retval None
380  */
381static void MX_IWDG_Init(void)
382{
383
384  /* USER CODE BEGIN IWDG_Init 0 */
385
386  /* USER CODE END IWDG_Init 0 */
387
388  /* USER CODE BEGIN IWDG_Init 1 */
389
390  /* USER CODE END IWDG_Init 1 */
391  hiwdg.Instance = IWDG;
392  hiwdg.Init.Prescaler = IWDG_PRESCALER_8;
393  hiwdg.Init.Window = 4095;
394  hiwdg.Init.Reload = 4095;
395  if (HAL_IWDG_Init(&hiwdg) != HAL_OK)
396  {
397    Error_Handler();
398  }
399  /* USER CODE BEGIN IWDG_Init 2 */
400
401  /* USER CODE END IWDG_Init 2 */
402
403}
404
405
406void mb_save_lock_key()
407{
408  if (sysData.s.lockKey == savedLockKey)
409  {
410    EEPROM_storeConfig(&sysData,false,true);
411    savedLockKey = sysData.s.newLockKey;
412    sysData.s.lockKey = savedLockKey;
413  }
414
415  if (savedLockKey != 0)
416  {
417    sysData.s.writeLocked = 1;
418  }
419  else
420  {
421    sysData.s.writeLocked = 0;
422  }
423}
424
425void applyMBParameter()
426{
427  HAL_UART_DeInit(&huart1);
428  mbInit(&modbusData, sysData.s.baudrate, sysData.s.parityMode, &huart1, accessModeTable, &keyAccepted);
429}
430
431void resetCounters()
432{
433  if (sysData.s.lockKey == savedLockKey)
434  {
435    sysData.s.criticalOverTempTime = 0;
436    sysData.s.criticalUnderTempTime = 0;
437    sysData.s.criticalOverVoltageTime = 0;
438    sysData.s.criticalUnderVoltageTime = 0;
439    sysData.s.cycleCounter = 0;
440    EEPROM_storeLogData();
441   }
442   else
443   {
444    printf("not allowed, paramter locked!\n");
445   }
446}
447
448void resetMinMaxValues()
449{
450  if (sysData.s.lockKey == savedLockKey)
451  {
452    sysData.s.minTemperature = sysData.s.cellTemperature;
453    sysData.s.maxTemperature = sysData.s.cellTemperature;
454    sysData.s.minVoltage = sysData.s.cellVoltage;
455    sysData.s.maxVoltage = sysData.s.cellVoltage; 
456    EEPROM_storeLogData();
457   }
458   else
459   {
460    printf("not allowed, paramter locked!\n");
461   }
462}
463
464/* USER CODE END 4 */
465
466/**
467  * @brief  This function is executed in case of error occurrence.
468  * @retval None
469  */
470void Error_Handler(void)
471{
472  /* USER CODE BEGIN Error_Handler_Debug */
473  /* User can add his own implementation to report the HAL error return state */
474  __disable_irq();
475  while (1)
476  {
477  }
478  /* USER CODE END Error_Handler_Debug */
479}
480
481#ifdef  USE_FULL_ASSERT
482/**
483  * @brief  Reports the name of the source file and the source line number
484  *         where the assert_param error has occurred.
485  * @param  file: pointer to the source file name
486  * @param  line: assert_param error line source number
487  * @retval None
488  */
489void assert_failed(uint8_t *file, uint32_t line)
490{
491  /* USER CODE BEGIN 6 */
492  /* User can add his own implementation to report the file name and line number,
493     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
494     printf("Wrong parameters value: file %s on line %d\r\n", file, line); 
495  /* USER CODE END 6 */
496}
497#endif /* USE_FULL_ASSERT */
498
499/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Note: See TracBrowser for help on using the repository browser.