| 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 "crc.h"
|
|---|
| 23 | #include "dma.h"
|
|---|
| 24 | #include "fdcan.h"
|
|---|
| 25 | #include "i2c.h"
|
|---|
| 26 | #include "iwdg.h"
|
|---|
| 27 | #include "rtc.h"
|
|---|
| 28 | #include "spi.h"
|
|---|
| 29 | #include "usart.h"
|
|---|
| 30 | #include "usb.h"
|
|---|
| 31 | #include "gpio.h"
|
|---|
| 32 |
|
|---|
| 33 | /* Private includes ----------------------------------------------------------*/
|
|---|
| 34 | /* USER CODE BEGIN Includes */
|
|---|
| 35 | #include <stdio.h>
|
|---|
| 36 | #include "sysdata.h"
|
|---|
| 37 | #include "wh_counter.h"
|
|---|
| 38 | #include "ah_counter.h"
|
|---|
| 39 | #include "eeprom.h"
|
|---|
| 40 | #include "modbus.h"
|
|---|
| 41 | #include "chip_temperature.h"
|
|---|
| 42 | #include "battery_voltage.h"
|
|---|
| 43 | #include "ads1260.h"
|
|---|
| 44 | #include "shunt_voltage.h"
|
|---|
| 45 | #include "fast_current.h"
|
|---|
| 46 | #include "int_bat_voltage.h"
|
|---|
| 47 | #include "chip_temperature.h"
|
|---|
| 48 | #include "shunt_temperature.h"
|
|---|
| 49 | #include "esr.h"
|
|---|
| 50 | #include "ads1260.h"
|
|---|
| 51 | #include "outputs.h"
|
|---|
| 52 | #include "crc.h"
|
|---|
| 53 | #include "efficiency.h"
|
|---|
| 54 | #include "self_discharge.h"
|
|---|
| 55 |
|
|---|
| 56 | /* USER CODE END Includes */
|
|---|
| 57 |
|
|---|
| 58 | /* Private typedef -----------------------------------------------------------*/
|
|---|
| 59 | /* USER CODE BEGIN PTD */
|
|---|
| 60 |
|
|---|
| 61 | /* USER CODE END PTD */
|
|---|
| 62 |
|
|---|
| 63 | /* Private define ------------------------------------------------------------*/
|
|---|
| 64 | /* USER CODE BEGIN PD */
|
|---|
| 65 | #define MAX_OVERCURRENT_RETRYS 3
|
|---|
| 66 | /* USER CODE END PD */
|
|---|
| 67 |
|
|---|
| 68 | /* Private macro -------------------------------------------------------------*/
|
|---|
| 69 | /* USER CODE BEGIN PM */
|
|---|
| 70 |
|
|---|
| 71 | /* USER CODE END PM */
|
|---|
| 72 |
|
|---|
| 73 | /* Private variables ---------------------------------------------------------*/
|
|---|
| 74 |
|
|---|
| 75 | /* USER CODE BEGIN PV */
|
|---|
| 76 | modbus_t modbusData __attribute__((section(".RAM1")));
|
|---|
| 77 |
|
|---|
| 78 | __IO uint16_t adc12Data[SAMPLE_ARRAY_SIZE][2] __attribute__((section(".RAM1")));
|
|---|
| 79 | __IO uint32_t adc1Data[1] __attribute__((section(".RAM1")));
|
|---|
| 80 | __IO uint32_t adc2Data[1] __attribute__((section(".RAM1")));
|
|---|
| 81 | __IO uint32_t adc3Data[3] __attribute__((section(".RAM1")));
|
|---|
| 82 | __IO uint32_t adc4Data[1] __attribute__((section(".RAM1")));
|
|---|
| 83 | __IO uint32_t adc5Data[4] __attribute__((section(".RAM1")));
|
|---|
| 84 | int silentmode =0;
|
|---|
| 85 | static volatile uint32_t newADC12Data = 0;
|
|---|
| 86 | static volatile uint32_t newADC3Data = 0;
|
|---|
| 87 | static volatile uint32_t newADC4Data = 0;
|
|---|
| 88 | static volatile uint32_t newADC5Data = 0;
|
|---|
| 89 |
|
|---|
| 90 | /* USER CODE END PV */
|
|---|
| 91 |
|
|---|
| 92 | /* Private function prototypes -----------------------------------------------*/
|
|---|
| 93 | void SystemClock_Config(void);
|
|---|
| 94 | /* USER CODE BEGIN PFP */
|
|---|
| 95 | bool SetFlashReadProtection(bool state);
|
|---|
| 96 | uint8_t printprotectionstate(void);
|
|---|
| 97 | bool SetBootFromFlashAndReadOutProtection(void);
|
|---|
| 98 | void LoadBackupRegister(void);
|
|---|
| 99 | void SaveBackupRegister(void);
|
|---|
| 100 | /* USER CODE END PFP */
|
|---|
| 101 |
|
|---|
| 102 | /* Private user code ---------------------------------------------------------*/
|
|---|
| 103 | /* USER CODE BEGIN 0 */
|
|---|
| 104 |
|
|---|
| 105 | /* USER CODE END 0 */
|
|---|
| 106 |
|
|---|
| 107 | /**
|
|---|
| 108 | * @brief The application entry point.
|
|---|
| 109 | * @retval int
|
|---|
| 110 | */
|
|---|
| 111 | int main(void)
|
|---|
| 112 | {
|
|---|
| 113 |
|
|---|
| 114 | /* USER CODE BEGIN 1 */
|
|---|
| 115 | uint8_t firstStartCatcher;
|
|---|
| 116 | int mode_button_disable_time=0;
|
|---|
| 117 | uint32_t oldTime = 0;
|
|---|
| 118 | uint32_t newTime = 0;
|
|---|
| 119 | uint32_t timeDiff;
|
|---|
| 120 | uint8_t sekunde = 0;
|
|---|
| 121 | uint8_t sekundeOld = 0;
|
|---|
| 122 | uint32_t dummyDate;
|
|---|
| 123 |
|
|---|
| 124 | /* USER CODE END 1 */
|
|---|
| 125 |
|
|---|
| 126 | /* MCU Configuration--------------------------------------------------------*/
|
|---|
| 127 |
|
|---|
| 128 | /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
|
|---|
| 129 | HAL_Init();
|
|---|
| 130 |
|
|---|
| 131 | /* USER CODE BEGIN Init */
|
|---|
| 132 |
|
|---|
| 133 | /* USER CODE END Init */
|
|---|
| 134 |
|
|---|
| 135 | /* Configure the system clock */
|
|---|
| 136 | SystemClock_Config();
|
|---|
| 137 |
|
|---|
| 138 | /* USER CODE BEGIN SysInit */
|
|---|
| 139 |
|
|---|
| 140 | /* USER CODE END SysInit */
|
|---|
| 141 |
|
|---|
| 142 | /* Initialize all configured peripherals */
|
|---|
| 143 | MX_GPIO_Init();
|
|---|
| 144 | MX_DMA_Init();
|
|---|
| 145 | MX_ADC1_Init();
|
|---|
| 146 | MX_ADC2_Init();
|
|---|
| 147 | MX_ADC3_Init();
|
|---|
| 148 | MX_ADC4_Init();
|
|---|
| 149 | MX_ADC5_Init();
|
|---|
| 150 | MX_FDCAN2_Init();
|
|---|
| 151 | MX_FDCAN3_Init();
|
|---|
| 152 | MX_I2C3_Init();
|
|---|
| 153 | MX_I2C4_Init();
|
|---|
| 154 | MX_SPI3_Init();
|
|---|
| 155 | MX_USART1_UART_Init();
|
|---|
| 156 | MX_USART2_UART_Init();
|
|---|
| 157 | MX_USB_PCD_Init();
|
|---|
| 158 | MX_CRC_Init();
|
|---|
| 159 | MX_RTC_Init();
|
|---|
| 160 | MX_IWDG_Init();
|
|---|
| 161 | /* USER CODE BEGIN 2 */
|
|---|
| 162 | printf("Test debug io\r\n");
|
|---|
| 163 | SYS_DATA_Init();
|
|---|
| 164 | WH_COUNTER_Init();
|
|---|
| 165 | AH_COUNTER_Init();
|
|---|
| 166 |
|
|---|
| 167 |
|
|---|
| 168 | startType_t startType = EEPROM_isFirstStart();
|
|---|
| 169 |
|
|---|
| 170 | switch(startType)
|
|---|
| 171 | {
|
|---|
| 172 | case FIRST_START_AFTER_ERASE: EEPROM_fullRestore(&sys_data); break;
|
|---|
| 173 | case FIRST_START_AFTER_COMPARTIBLE_UPDATE: EEPROM_readConfig(&sys_data); break;
|
|---|
| 174 | case FIRST_START_AFTER_INCOMPARTIBLE_UPDATE: EEPROM_factoryRestore(&sys_data, 0); break; // Preserving calibration and settings
|
|---|
| 175 | case FIRST_START_ERROR: EEPROM_fullRestore(&sys_data); break;
|
|---|
| 176 | }
|
|---|
| 177 | HAL_IWDG_Refresh(&hiwdg);
|
|---|
| 178 |
|
|---|
| 179 | if(HAL_GPIO_ReadPin(GPIO_INPUT_BTN_MODE_GPIO_Port, GPIO_INPUT_BTN_MODE_Pin) == GPIO_PIN_RESET)
|
|---|
| 180 | {
|
|---|
| 181 | HAL_Delay(50);
|
|---|
| 182 | if(HAL_GPIO_ReadPin(GPIO_INPUT_BTN_MODE_GPIO_Port, GPIO_INPUT_BTN_MODE_Pin) == GPIO_PIN_RESET)
|
|---|
| 183 | {
|
|---|
| 184 | printf("factory restore...\n");
|
|---|
| 185 | EEPROM_factoryRestore(&sys_data, 1);
|
|---|
| 186 | }
|
|---|
| 187 | }
|
|---|
| 188 | HAL_IWDG_Refresh(&hiwdg);
|
|---|
| 189 | LoadBackupRegister();
|
|---|
| 190 |
|
|---|
| 191 |
|
|---|
| 192 | // Modbus Initialisierung
|
|---|
| 193 | mbInit(&modbusData, sys_data.s.parameter.baudrate, sys_data.s.parameter.parityMode, sys_data.s.parameter.stopBit, &huart2);
|
|---|
| 194 | HAL_IWDG_Refresh(&hiwdg);
|
|---|
| 195 | // STM32G0 Chiptemperatur Kalibrierung
|
|---|
| 196 | CHIP_TEMPERATURE_Calibration();
|
|---|
| 197 |
|
|---|
| 198 | HAL_ADCEx_Calibration_Start(&hadc1, ADC_DIFFERENTIAL_ENDED);
|
|---|
| 199 | HAL_ADCEx_Calibration_Start(&hadc2, ADC_DIFFERENTIAL_ENDED);
|
|---|
| 200 | HAL_ADCEx_Calibration_Start(&hadc3, ADC_SINGLE_ENDED);
|
|---|
| 201 | HAL_ADCEx_Calibration_Start(&hadc4, ADC_DIFFERENTIAL_ENDED);
|
|---|
| 202 | HAL_ADCEx_Calibration_Start(&hadc5, ADC_SINGLE_ENDED);
|
|---|
| 203 | HAL_IWDG_Refresh(&hiwdg);
|
|---|
| 204 |
|
|---|
| 205 | //SET_BIT(hadc2.Instance->CFGR, ADC_CFGR_DMAEN); //Enable DMA transfer for ADC slave (ADC12_CCR.MDMA = 0b00 -> MDMA mode disabled)
|
|---|
| 206 | //HAL_DMA_Start(hadc2.DMA_Handle,(uint32_t)&hadc2.Instance->DR, (uint32_t)adc2Data,1); //Start ADC slave DMA
|
|---|
| 207 | //SET_BIT(hadc1.Instance->CFGR, ADC_CFGR_DMAEN); //Enable DMA transfer for ADC master (ADC12_CCR.MDMA = 0b00 -> MDMA mode disabled)
|
|---|
| 208 |
|
|---|
| 209 | //HAL_ADC_Start_DMA(&hadc2, (uint32_t*)adc2Data, 1);
|
|---|
| 210 | if (HAL_ADCEx_MultiModeStart_DMA(&hadc1,(uint32_t *)adc12Data,SAMPLE_ARRAY_SIZE)) //Start ADC interleaved mode
|
|---|
| 211 | {
|
|---|
| 212 | /* Start Error */
|
|---|
| 213 | Error_Handler();
|
|---|
| 214 | }
|
|---|
| 215 |
|
|---|
| 216 | if (HAL_ADC_Start_DMA(&hadc3, (uint32_t *) adc3Data , 3))
|
|---|
| 217 | {
|
|---|
| 218 | /* Start Error */
|
|---|
| 219 | Error_Handler();
|
|---|
| 220 | }
|
|---|
| 221 |
|
|---|
| 222 | if (HAL_ADC_Start_DMA(&hadc4, (uint32_t *) adc4Data , 1))
|
|---|
| 223 | {
|
|---|
| 224 | /* Start Error */
|
|---|
| 225 | Error_Handler();
|
|---|
| 226 | }
|
|---|
| 227 |
|
|---|
| 228 |
|
|---|
| 229 | if (HAL_ADC_Start_DMA(&hadc5, (uint32_t *) adc5Data , 4))
|
|---|
| 230 | {
|
|---|
| 231 | /* Start Error */
|
|---|
| 232 | Error_Handler();
|
|---|
| 233 | }
|
|---|
| 234 |
|
|---|
| 235 |
|
|---|
| 236 | // ADS1260 Initialierung
|
|---|
| 237 | HAL_IWDG_Refresh(&hiwdg);
|
|---|
| 238 | ADS1260_init();
|
|---|
| 239 | HAL_IWDG_Refresh(&hiwdg);
|
|---|
| 240 | printf("ADS1260 Init\n");
|
|---|
| 241 | OUTPUTS_Init();
|
|---|
| 242 |
|
|---|
| 243 | //Display Adress with Error LED blinks
|
|---|
| 244 | for (int n = 0; n < sys_data.s.parameter.slave_address; n++)
|
|---|
| 245 | {
|
|---|
| 246 | HAL_GPIO_WritePin(LED_ERROR_GPIO_Port, LED_ERROR_Pin, GPIO_PIN_SET);
|
|---|
| 247 | HAL_Delay(350);
|
|---|
| 248 | HAL_IWDG_Refresh(&hiwdg);
|
|---|
| 249 | HAL_GPIO_WritePin(LED_ERROR_GPIO_Port, LED_ERROR_Pin, GPIO_PIN_RESET);
|
|---|
| 250 | HAL_Delay(350);
|
|---|
| 251 | HAL_IWDG_Refresh(&hiwdg);
|
|---|
| 252 | }
|
|---|
| 253 |
|
|---|
| 254 |
|
|---|
| 255 | /* USER CODE END 2 */
|
|---|
| 256 |
|
|---|
| 257 | /* Infinite loop */
|
|---|
| 258 | /* USER CODE BEGIN WHILE */
|
|---|
| 259 | while (1)
|
|---|
| 260 | {
|
|---|
| 261 | /* USER CODE END WHILE */
|
|---|
| 262 |
|
|---|
| 263 | /* USER CODE BEGIN 3 */
|
|---|
| 264 | HAL_IWDG_Refresh(&hiwdg);
|
|---|
| 265 | if (newADC12Data == 1)
|
|---|
| 266 | {
|
|---|
| 267 | //Mit ADC_DIV2,Sample time 12,5Cycklen, ADC Clock 50Mhz, Oversampling 256
|
|---|
| 268 | //Tconv = 6400 Takte = 0,128ms Pro Konvertierung. Also für 100 messwerte 12,8mS
|
|---|
| 269 | BATTERY_VOLTAGE_Exec( adc12Data[SAMPLE_ARRAY_SIZE-1][1]);
|
|---|
| 270 | FAST_CURRENT_Exec(adc12Data[SAMPLE_ARRAY_SIZE-1][0]);
|
|---|
| 271 | ESR_FAST_Exec();
|
|---|
| 272 | sys_data.s.values.batteryPower = ((int64_t) sys_data.s.values.batteryVoltage * (int64_t)sys_data.s.values.batteryCurrent) / 1000LL;
|
|---|
| 273 | sys_data.s.values.power_Fast = ((int64_t) sys_data.s.values.batteryVoltage * (int64_t)sys_data.s.values.fast_current) / 1000LL;
|
|---|
| 274 | newADC12Data = 0;
|
|---|
| 275 |
|
|---|
| 276 |
|
|---|
| 277 | if (HAL_ADCEx_MultiModeStart_DMA(&hadc1,(uint32_t *)adc12Data,SAMPLE_ARRAY_SIZE)) //Start ADC interleaved mode
|
|---|
| 278 | {
|
|---|
| 279 | /* Start Error */
|
|---|
| 280 | Error_Handler();
|
|---|
| 281 | }
|
|---|
| 282 |
|
|---|
| 283 | }
|
|---|
| 284 |
|
|---|
| 285 |
|
|---|
| 286 | if (newADC3Data == 1)
|
|---|
| 287 | {
|
|---|
| 288 | SHUNT_TEMPERATURE_Exec(adc3Data[0]);
|
|---|
| 289 | }
|
|---|
| 290 |
|
|---|
| 291 |
|
|---|
| 292 | if (newADC4Data == 1)
|
|---|
| 293 | {
|
|---|
| 294 | SHUNT_VOLTAGE_Exec( adc4Data[0]);
|
|---|
| 295 | }
|
|---|
| 296 |
|
|---|
| 297 |
|
|---|
| 298 | if (newADC5Data == 1)
|
|---|
| 299 | {
|
|---|
| 300 | CHIP_TEMPERATURE_Exec(adc5Data[0]);
|
|---|
| 301 | INT_BAT_VOLTAGE_Exec( adc5Data[1]);
|
|---|
| 302 | sys_data.s.values.ovp_sense = (adc5Data[2] * VREF * 21 ) / 65536.0;
|
|---|
| 303 | sys_data.s.values.lvp_sense = (adc5Data[3] * VREF * 21 ) / 65536.0;
|
|---|
| 304 | }
|
|---|
| 305 |
|
|---|
| 306 |
|
|---|
| 307 | if (newCurrentValue == 1)
|
|---|
| 308 | {
|
|---|
| 309 | ADS1260_ConversionFinished();
|
|---|
| 310 | sys_data.s.values.esrCalcStatus = ESR_Exec();
|
|---|
| 311 |
|
|---|
| 312 | newCurrentValue = 0;
|
|---|
| 313 |
|
|---|
| 314 | }
|
|---|
| 315 |
|
|---|
| 316 |
|
|---|
| 317 |
|
|---|
| 318 | // Zeitbasis ms Systick;
|
|---|
| 319 | newTime = HAL_GetTick();
|
|---|
| 320 |
|
|---|
| 321 | if (newTime != oldTime) {
|
|---|
| 322 |
|
|---|
| 323 | oldTime = newTime;
|
|---|
| 324 |
|
|---|
| 325 | if (mode_button_disable_time > 0)
|
|---|
| 326 | {
|
|---|
| 327 | mode_button_disable_time--;
|
|---|
| 328 | }
|
|---|
| 329 |
|
|---|
| 330 | }
|
|---|
| 331 |
|
|---|
| 332 |
|
|---|
| 333 | if (sys_data.s.values.overcurrentRetryCounter > MAX_OVERCURRENT_RETRYS)
|
|---|
| 334 | {
|
|---|
| 335 | printf("SHORT_CIRCUIT_PROTECTION\n");
|
|---|
| 336 | sys_data.s.values.lvpState = OUTPUTS_LVP_SHORT_PROTECTION;
|
|---|
| 337 | HAL_GPIO_WritePin(DISCHARGE_ENABLE_GPIO_Port, DISCHARGE_ENABLE_Pin, OUTPUTS_DISCHARGE_NOT_ALLOWED);
|
|---|
| 338 |
|
|---|
| 339 | sys_data.s.values.lvpState = OUTPUTS_OVP_SHORT_PROTECTION;
|
|---|
| 340 | HAL_GPIO_WritePin(CHARGE_ENABLE_GPIO_Port, CHARGE_ENABLE_Pin, OUTPUTS_CHARGE_NOT_ALLOWED);
|
|---|
| 341 |
|
|---|
| 342 |
|
|---|
| 343 |
|
|---|
| 344 | }
|
|---|
| 345 |
|
|---|
| 346 | // Zeitbasis Sekunde über RTC
|
|---|
| 347 | // muss immer sowohl das Zeit- als auch das Datumsregister lesen
|
|---|
| 348 | // da beide Shadowregister sind sonst wird die Zeit nicht mehr geupdatet
|
|---|
| 349 | sekunde = hrtc.Instance->TR & 0x0000000F;
|
|---|
| 350 | dummyDate = hrtc.Instance->DR;
|
|---|
| 351 |
|
|---|
| 352 |
|
|---|
| 353 |
|
|---|
| 354 | if (sekundeOld != sekunde)
|
|---|
| 355 | {
|
|---|
| 356 | sekundeOld = sekunde;
|
|---|
| 357 |
|
|---|
| 358 | sys_data.s.values.onTime++;
|
|---|
| 359 | sys_data.s.values.lastTimeVbatEmpty++;
|
|---|
| 360 | sys_data.s.values.lastTimeVbatFull++;
|
|---|
| 361 | // Funktions Led blinken
|
|---|
| 362 | if (silentmode == 0)
|
|---|
| 363 | {
|
|---|
| 364 | HAL_GPIO_TogglePin(LED_FUNCTION_GPIO_Port, LED_FUNCTION_Pin);
|
|---|
| 365 | //HAL_GPIO_TogglePin(AUX_EN_GPIO_Port, AUX_EN_Pin);
|
|---|
| 366 | }
|
|---|
| 367 |
|
|---|
| 368 |
|
|---|
| 369 | // Amperestundenzhler
|
|---|
| 370 | AH_COUNTER_Exec();
|
|---|
| 371 |
|
|---|
| 372 | WH_COUNTER_Exec();
|
|---|
| 373 |
|
|---|
| 374 | // LVP
|
|---|
| 375 | OUTPUTS_CheckLVP();
|
|---|
| 376 |
|
|---|
| 377 | // OVP
|
|---|
| 378 | OUTPUTS_CheckOVP();
|
|---|
| 379 |
|
|---|
| 380 | // AUX
|
|---|
| 381 | OUTPUTS_CheckAUX();
|
|---|
| 382 |
|
|---|
| 383 | SaveBackupRegister();
|
|---|
| 384 |
|
|---|
| 385 | sys_data.s.values.selfDischargeStatus = SELF_DISCHARGE_Exec();
|
|---|
| 386 |
|
|---|
| 387 | sys_data.s.values.efficiency = EFFICIENCY_Exec();
|
|---|
| 388 |
|
|---|
| 389 |
|
|---|
| 390 | }
|
|---|
| 391 |
|
|---|
| 392 |
|
|---|
| 393 | if(sys_data.s.parameter.command != 0)
|
|---|
| 394 | {
|
|---|
| 395 | if (modbusData.current_query == MB_QUERY_NOTHING)
|
|---|
| 396 | {
|
|---|
| 397 | //printf("CMD = %d\n", sys_data.s.parameter.command);
|
|---|
| 398 | switch (sys_data.s.parameter.command )
|
|---|
| 399 | {
|
|---|
| 400 | case COMMAND_STORE_CONFIG: EEPROM_storeConfig(&sys_data,0); break;
|
|---|
| 401 | case COMMAND_FULL_RESTORE: EEPROM_fullRestore(&sys_data); break;
|
|---|
| 402 | case COMMAND_FACTORY_RESTORE: EEPROM_factoryRestore(&sys_data, 1); break;
|
|---|
| 403 | case COMMAND_RESTORE_LAST_SAVED_VALUES: EEPROM_readConfig(&sys_data); break;
|
|---|
| 404 | case COMMAND_STORE_WITH_SERIAL_NUMBER: EEPROM_storeConfig(&sys_data,1); break; // Seriennummer schreiben
|
|---|
| 405 | case COMMAND_RESTART: while(1); break;
|
|---|
| 406 | case COMMAND_BATTERY_CURRENT_OFFSET_CAL:
|
|---|
| 407 | ADS_1260_BatteryCurrentOffsetCalibrationStart(&sys_data);
|
|---|
| 408 | CurrentOffsetCal(adc12Data[SAMPLE_ARRAY_SIZE - 1][0]);
|
|---|
| 409 | break;
|
|---|
| 410 | case COMMAND_BATTERY_CURRENT_OFFSET_COMMONMODE_CAL: ADS_1260_BatteryCurrentOffsetCommonModeErrorComepensationStart(&sys_data); break;
|
|---|
| 411 | case COMMAND_BATTERY_CURRENT_OFFSET_TEMP_CAL: ADS_1260_BatteryCurrentOffsetTemperatureErrorComepensationStart(); break;
|
|---|
| 412 | case COMMAND_BATTERY_CURRENT_GAIN_CAL:
|
|---|
| 413 | ADS_1260_BatteryCurrentGainCalibrationStart(&sys_data);
|
|---|
| 414 | CurrentGainCal(adc12Data[SAMPLE_ARRAY_SIZE - 1][0]);
|
|---|
| 415 | break;
|
|---|
| 416 | case COMMAND_BATTERY_CURRENT_GAIN_TEMP_SHUNT_CAL: ADS_1260_BatteryCurrentGainTemperatureCalibrationShuntStart(); break;
|
|---|
| 417 | // case COMMAND_BATTERY_CURRENT_GAIN_TEMP_CHIP_CAL: ADS_1260_BatteryCurrentGainTemperatureCalibrationChipStart(); break;
|
|---|
| 418 | case COMMAND_SET_RDP_LEVEL0: SetFlashReadProtection(false); break;
|
|---|
| 419 | case COMMAND_SET_RDP_LEVEL1: SetFlashReadProtection(true); break;
|
|---|
| 420 | case COMMAND_SET_RDP_LEVEL1_AND_BOOTSEL: SetBootFromFlashAndReadOutProtection(); break;
|
|---|
| 421 | default: printf("UNKNOWN COMMAND\n");
|
|---|
| 422 | }
|
|---|
| 423 | sys_data.s.parameter.command = 0;
|
|---|
| 424 | }
|
|---|
| 425 | else
|
|---|
| 426 | {
|
|---|
| 427 | //printf("wait with execution till modbus communnikation finished\n");
|
|---|
| 428 | }
|
|---|
| 429 | }
|
|---|
| 430 |
|
|---|
| 431 | if((HAL_GPIO_ReadPin(GPIO_INPUT_BTN_MODE_GPIO_Port, GPIO_INPUT_BTN_MODE_Pin) == GPIO_PIN_RESET) && (mode_button_disable_time == 0))
|
|---|
| 432 | {
|
|---|
| 433 | HAL_Delay(10);
|
|---|
| 434 | //Taste weiterhin gedrckt?
|
|---|
| 435 | if(HAL_GPIO_ReadPin(GPIO_INPUT_BTN_MODE_GPIO_Port, GPIO_INPUT_BTN_MODE_Pin) == GPIO_PIN_RESET)
|
|---|
| 436 | {
|
|---|
| 437 | //Ja, dann Silent Mode umschalten
|
|---|
| 438 | mode_button_disable_time=500;
|
|---|
| 439 | if (silentmode == 0)
|
|---|
| 440 | {
|
|---|
| 441 | silentmode = 1;
|
|---|
| 442 | HAL_GPIO_WritePin(LED_FUNCTION_GPIO_Port, LED_FUNCTION_Pin,GPIO_PIN_SET);
|
|---|
| 443 | }
|
|---|
| 444 | else
|
|---|
| 445 | {
|
|---|
| 446 | silentmode = 0;
|
|---|
| 447 | }
|
|---|
| 448 | }
|
|---|
| 449 | }
|
|---|
| 450 |
|
|---|
| 451 | // Modbus Kommunikation
|
|---|
| 452 |
|
|---|
| 453 | // printf("data12d1=%d,data12d2=%d,data5=%d\r\n", adc12Data[0], adc12Data[1] , adc5Data[2]);
|
|---|
| 454 | if (mbGetFrameComplete(&modbusData) == true)
|
|---|
| 455 | {
|
|---|
| 456 | if (mbSlaveCheckModbusRtuQuery(&modbusData) == RESPOND_TO_QUERY)
|
|---|
| 457 | {
|
|---|
| 458 | if (silentmode == 0)
|
|---|
| 459 | {
|
|---|
| 460 | mbSlaveProcessRtuQuery(&modbusData);
|
|---|
| 461 | }
|
|---|
| 462 | }
|
|---|
| 463 | else
|
|---|
| 464 | {
|
|---|
| 465 | huart1.RxState = HAL_UART_STATE_BUSY_RX;
|
|---|
| 466 | }
|
|---|
| 467 | }
|
|---|
| 468 |
|
|---|
| 469 |
|
|---|
| 470 | }
|
|---|
| 471 | /* USER CODE END 3 */
|
|---|
| 472 | }
|
|---|
| 473 |
|
|---|
| 474 | /**
|
|---|
| 475 | * @brief System Clock Configuration
|
|---|
| 476 | * @retval None
|
|---|
| 477 | */
|
|---|
| 478 | void SystemClock_Config(void)
|
|---|
| 479 | {
|
|---|
| 480 | RCC_OscInitTypeDef RCC_OscInitStruct = {0};
|
|---|
| 481 | RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
|
|---|
| 482 |
|
|---|
| 483 | /** Configure the main internal regulator output voltage
|
|---|
| 484 | */
|
|---|
| 485 | HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1);
|
|---|
| 486 |
|
|---|
| 487 | /** Configure LSE Drive Capability
|
|---|
| 488 | */
|
|---|
| 489 | HAL_PWR_EnableBkUpAccess();
|
|---|
| 490 | __HAL_RCC_LSEDRIVE_CONFIG(RCC_LSEDRIVE_LOW);
|
|---|
| 491 |
|
|---|
| 492 | /** Initializes the RCC Oscillators according to the specified parameters
|
|---|
| 493 | * in the RCC_OscInitTypeDef structure.
|
|---|
| 494 | */
|
|---|
| 495 | RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI48|RCC_OSCILLATORTYPE_LSI
|
|---|
| 496 | |RCC_OSCILLATORTYPE_HSE|RCC_OSCILLATORTYPE_LSE;
|
|---|
| 497 | RCC_OscInitStruct.HSEState = RCC_HSE_ON;
|
|---|
| 498 | RCC_OscInitStruct.LSEState = RCC_LSE_ON;
|
|---|
| 499 | RCC_OscInitStruct.LSIState = RCC_LSI_ON;
|
|---|
| 500 | RCC_OscInitStruct.HSI48State = RCC_HSI48_ON;
|
|---|
| 501 | RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
|
|---|
| 502 | RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
|
|---|
| 503 | RCC_OscInitStruct.PLL.PLLM = RCC_PLLM_DIV2;
|
|---|
| 504 | RCC_OscInitStruct.PLL.PLLN = 16;
|
|---|
| 505 | RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV8;
|
|---|
| 506 | RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
|
|---|
| 507 | RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
|
|---|
| 508 | if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
|
|---|
| 509 | {
|
|---|
| 510 | Error_Handler();
|
|---|
| 511 | }
|
|---|
| 512 |
|
|---|
| 513 | /** Initializes the CPU, AHB and APB buses clocks
|
|---|
| 514 | */
|
|---|
| 515 | RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|
|---|
| 516 | |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
|
|---|
| 517 | RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
|
|---|
| 518 | RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
|
|---|
| 519 | RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
|
|---|
| 520 | RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
|
|---|
| 521 |
|
|---|
| 522 | if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_3) != HAL_OK)
|
|---|
| 523 | {
|
|---|
| 524 | Error_Handler();
|
|---|
| 525 | }
|
|---|
| 526 | }
|
|---|
| 527 |
|
|---|
| 528 | /* USER CODE BEGIN 4 */
|
|---|
| 529 | void LoadBackupRegister(void)
|
|---|
| 530 | {
|
|---|
| 531 | if (HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR1) == 0xBEBE)
|
|---|
| 532 | {
|
|---|
| 533 | printf("BackupRegisterData available\r\n");
|
|---|
| 534 | sys_data.s.values.dischargeTotalAh = HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR2);
|
|---|
| 535 |
|
|---|
| 536 | sys_data.s.values.chargeTotalAh = HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR3);
|
|---|
| 537 | sys_data.s.values.dischargeTotalWh = HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR4);
|
|---|
| 538 | sys_data.s.values.chargeTotalWh = HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR5);
|
|---|
| 539 |
|
|---|
| 540 | sys_data.s.values.onTime = HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR6);
|
|---|
| 541 |
|
|---|
| 542 |
|
|---|
| 543 | sys_data.s.values.detectedCapacity = HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR7);
|
|---|
| 544 | sys_data.s.values.detectedEnergy = HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR8);
|
|---|
| 545 |
|
|---|
| 546 |
|
|---|
| 547 | sys_data.s.values.mAh_AutoMode = (int32_t) HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR9);
|
|---|
| 548 | sys_data.s.values.mWh_AutoMode = (int32_t) HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR10);
|
|---|
| 549 | sys_data.s.values.mAhCounter = (int32_t) HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR11);
|
|---|
| 550 | sys_data.s.values.mWhCounter = (int32_t) HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR12);
|
|---|
| 551 |
|
|---|
| 552 | sys_data.s.values.mAh_AutoModeU = (int32_t) HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR13);
|
|---|
| 553 |
|
|---|
| 554 |
|
|---|
| 555 | sys_data.s.values.mAs_AutoMode = sys_data.s.values.mAh_AutoMode * 3600LL;
|
|---|
| 556 | sys_data.s.values.mAs_AutoModeU = sys_data.s.values.mAh_AutoModeU * 3600LL;
|
|---|
| 557 |
|
|---|
| 558 | sys_data.s.values.mWs_AutoMode = sys_data.s.values.mWh_AutoMode * 3600LL;
|
|---|
| 559 | sys_data.s.values.mAsCounter = sys_data.s.values.mAhCounter * 3600LL;
|
|---|
| 560 |
|
|---|
| 561 |
|
|---|
| 562 |
|
|---|
| 563 | }
|
|---|
| 564 |
|
|---|
| 565 |
|
|---|
| 566 | }
|
|---|
| 567 |
|
|---|
| 568 |
|
|---|
| 569 | void SaveBackupRegister(void)
|
|---|
| 570 | {
|
|---|
| 571 | static uint32_t lastTime;
|
|---|
| 572 |
|
|---|
| 573 |
|
|---|
| 574 | uint32_t time = sys_data.s.values.onTime;
|
|---|
| 575 |
|
|---|
| 576 | // Alle 24 Stunden führen wir ein Speicherbefehl durch um die Counter zu speichern
|
|---|
| 577 | if (time != lastTime)
|
|---|
| 578 | {
|
|---|
| 579 | lastTime = time;
|
|---|
| 580 | HAL_PWR_EnableBkUpAccess();
|
|---|
| 581 | HAL_RTCEx_BKUPWrite(&hrtc, RTC_BKP_DR1, 0xBEBE);
|
|---|
| 582 |
|
|---|
| 583 | // Total Energie und Ah Umsatz der Batterie (seit inbetriebnahme)
|
|---|
| 584 | HAL_RTCEx_BKUPWrite(&hrtc, RTC_BKP_DR2, sys_data.s.values.dischargeTotalAh);
|
|---|
| 585 | HAL_RTCEx_BKUPWrite(&hrtc, RTC_BKP_DR3, sys_data.s.values.chargeTotalAh);
|
|---|
| 586 | HAL_RTCEx_BKUPWrite(&hrtc, RTC_BKP_DR4, sys_data.s.values.dischargeTotalWh);
|
|---|
| 587 | HAL_RTCEx_BKUPWrite(&hrtc, RTC_BKP_DR5, sys_data.s.values.chargeTotalWh);
|
|---|
| 588 |
|
|---|
| 589 | // Betriebszeit
|
|---|
| 590 | HAL_RTCEx_BKUPWrite(&hrtc, RTC_BKP_DR6, sys_data.s.values.onTime);
|
|---|
| 591 |
|
|---|
| 592 | // Erkannte Kapazitäten
|
|---|
| 593 | HAL_RTCEx_BKUPWrite(&hrtc, RTC_BKP_DR7, sys_data.s.values.detectedCapacity);
|
|---|
| 594 | HAL_RTCEx_BKUPWrite(&hrtc, RTC_BKP_DR8, sys_data.s.values.detectedEnergy);
|
|---|
| 595 |
|
|---|
| 596 |
|
|---|
| 597 | HAL_RTCEx_BKUPWrite(&hrtc, RTC_BKP_DR9, sys_data.s.values.mAh_AutoMode);
|
|---|
| 598 | HAL_RTCEx_BKUPWrite(&hrtc, RTC_BKP_DR10, sys_data.s.values.mWh_AutoMode);
|
|---|
| 599 | HAL_RTCEx_BKUPWrite(&hrtc, RTC_BKP_DR11, sys_data.s.values.mAhCounter);
|
|---|
| 600 | HAL_RTCEx_BKUPWrite(&hrtc, RTC_BKP_DR12, sys_data.s.values.mWhCounter);
|
|---|
| 601 |
|
|---|
| 602 | HAL_RTCEx_BKUPWrite(&hrtc, RTC_BKP_DR13, sys_data.s.values.mAh_AutoModeU);
|
|---|
| 603 |
|
|---|
| 604 |
|
|---|
| 605 |
|
|---|
| 606 |
|
|---|
| 607 | }
|
|---|
| 608 |
|
|---|
| 609 |
|
|---|
| 610 | }
|
|---|
| 611 |
|
|---|
| 612 |
|
|---|
| 613 |
|
|---|
| 614 | void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)
|
|---|
| 615 | {
|
|---|
| 616 | if (hadc->Instance==ADC1)
|
|---|
| 617 | {
|
|---|
| 618 | newADC12Data=1;
|
|---|
| 619 | }
|
|---|
| 620 |
|
|---|
| 621 | if (hadc->Instance==ADC3)
|
|---|
| 622 | {
|
|---|
| 623 | newADC3Data=1;
|
|---|
| 624 | }
|
|---|
| 625 |
|
|---|
| 626 |
|
|---|
| 627 | if (hadc->Instance==ADC4)
|
|---|
| 628 | {
|
|---|
| 629 | newADC4Data=1;
|
|---|
| 630 | }
|
|---|
| 631 |
|
|---|
| 632 | if (hadc->Instance==ADC5)
|
|---|
| 633 | {
|
|---|
| 634 | newADC5Data=1;
|
|---|
| 635 | }
|
|---|
| 636 | }
|
|---|
| 637 |
|
|---|
| 638 |
|
|---|
| 639 |
|
|---|
| 640 | /**
|
|---|
| 641 |
|
|---|
| 642 | * @brief Set flash read protection.
|
|---|
| 643 |
|
|---|
| 644 | * @param [in] state: Flash read protection state, true: enable protection, false: disable protection.
|
|---|
| 645 |
|
|---|
| 646 | * @retval true: Successful operation.
|
|---|
| 647 |
|
|---|
| 648 | * @retval false: Operation failed.
|
|---|
| 649 |
|
|---|
| 650 | */
|
|---|
| 651 |
|
|---|
| 652 | bool SetFlashReadProtection(bool state)
|
|---|
| 653 | {
|
|---|
| 654 |
|
|---|
| 655 | FLASH_OBProgramInitTypeDef OptionsBytesStruct = {0};
|
|---|
| 656 | HAL_FLASHEx_OBGetConfig(&OptionsBytesStruct);
|
|---|
| 657 |
|
|---|
| 658 | if(state == true)
|
|---|
| 659 | {
|
|---|
| 660 | printf("Start enable readout protection\n");
|
|---|
| 661 | if(OptionsBytesStruct.RDPLevel == OB_RDP_LEVEL_0)
|
|---|
| 662 | {
|
|---|
| 663 | OptionsBytesStruct.OptionType = OPTIONBYTE_RDP;
|
|---|
| 664 | OptionsBytesStruct.RDPLevel = OB_RDP_LEVEL_1;
|
|---|
| 665 | if (HAL_FLASH_Unlock() != HAL_OK)
|
|---|
| 666 | {
|
|---|
| 667 | printf("Flash unlock error\n");
|
|---|
| 668 | }
|
|---|
| 669 | if (HAL_FLASH_OB_Unlock() != HAL_OK)
|
|---|
| 670 | {
|
|---|
| 671 | printf("Flash ob unlock error\n");
|
|---|
| 672 | }
|
|---|
| 673 |
|
|---|
| 674 | printf("...Flash unlock\n");
|
|---|
| 675 | if(HAL_FLASHEx_OBProgram(&OptionsBytesStruct) != HAL_OK)
|
|---|
| 676 | {
|
|---|
| 677 | printf("...Enable lock error\n");
|
|---|
| 678 | HAL_FLASH_OB_Lock();
|
|---|
| 679 | return false;
|
|---|
| 680 | }
|
|---|
| 681 | HAL_FLASH_OB_Lock();
|
|---|
| 682 | printf("Flash Optionbyte locked\n");
|
|---|
| 683 | HAL_FLASH_Lock();
|
|---|
| 684 | printf("Flash locked\n");
|
|---|
| 685 | printf("...Enable lock process finished\n");
|
|---|
| 686 | }
|
|---|
| 687 | else
|
|---|
| 688 | {
|
|---|
| 689 | printf("...Flash lock already active\n");
|
|---|
| 690 | }
|
|---|
| 691 | }
|
|---|
| 692 | else
|
|---|
| 693 | {
|
|---|
| 694 | if(OptionsBytesStruct.RDPLevel == OB_RDP_LEVEL_1)
|
|---|
| 695 | {
|
|---|
| 696 | OptionsBytesStruct.OptionType = OPTIONBYTE_RDP;
|
|---|
| 697 | OptionsBytesStruct.RDPLevel = OB_RDP_LEVEL_0;
|
|---|
| 698 |
|
|---|
| 699 | if (HAL_FLASH_Unlock() != HAL_OK)
|
|---|
| 700 | {
|
|---|
| 701 | printf("Flash unlock error\n");
|
|---|
| 702 | return false;
|
|---|
| 703 | }
|
|---|
| 704 | printf("...Flash unlocked\n");
|
|---|
| 705 |
|
|---|
| 706 | if (HAL_FLASH_OB_Unlock() != HAL_OK)
|
|---|
| 707 | {
|
|---|
| 708 | printf("Flash ob unlock error\n");
|
|---|
| 709 | return false;
|
|---|
| 710 | }
|
|---|
| 711 | printf("...Flash ob unlocked\n");
|
|---|
| 712 |
|
|---|
| 713 | if(HAL_FLASHEx_OBProgram(&OptionsBytesStruct) != HAL_OK)
|
|---|
| 714 | {
|
|---|
| 715 | HAL_FLASH_OB_Lock();
|
|---|
| 716 | printf("Flash Optionbyte programm failed\n");
|
|---|
| 717 | return false;
|
|---|
| 718 | }
|
|---|
| 719 |
|
|---|
| 720 | printf("Flash Optionbyte programmed\n");
|
|---|
| 721 | HAL_FLASH_OB_Lock();
|
|---|
| 722 | printf("Flash Optionbyte locked\n");
|
|---|
| 723 | HAL_FLASH_Lock();
|
|---|
| 724 | printf("Flash locked\n");
|
|---|
| 725 | printf("...Disable lock process finished\n");
|
|---|
| 726 |
|
|---|
| 727 | ;
|
|---|
| 728 | }
|
|---|
| 729 | }
|
|---|
| 730 | return true;
|
|---|
| 731 | }
|
|---|
| 732 |
|
|---|
| 733 | bool SetBootFromFlashAndReadOutProtection(void)
|
|---|
| 734 | {
|
|---|
| 735 |
|
|---|
| 736 | FLASH_OBProgramInitTypeDef OptionsBytesStruct = {0};
|
|---|
| 737 | HAL_FLASHEx_OBGetConfig(&OptionsBytesStruct);
|
|---|
| 738 |
|
|---|
| 739 | //Konfiguriere RDP fr Readoutprotection and USER OPTION BYTE FR Boot from Flash
|
|---|
| 740 | OptionsBytesStruct.OptionType = OPTIONBYTE_USER | OPTIONBYTE_RDP;
|
|---|
| 741 |
|
|---|
| 742 | //Set Readout Protection Level 1
|
|---|
| 743 | OptionsBytesStruct.OptionType = OPTIONBYTE_USER|OPTIONBYTE_RDP;
|
|---|
| 744 | OptionsBytesStruct.RDPLevel = OB_RDP_LEVEL_1;
|
|---|
| 745 |
|
|---|
| 746 | //Selecting Boot from Main Flash Memory
|
|---|
| 747 | OptionsBytesStruct.USERType = OB_USER_nBOOT0 | OB_USER_nSWBOOT0 | OB_USER_nBOOT1 ;
|
|---|
| 748 | OptionsBytesStruct.USERConfig = OB_USER_nBOOT0 | OB_USER_nSWBOOT0;
|
|---|
| 749 |
|
|---|
| 750 | if (HAL_FLASH_Unlock() != HAL_OK)
|
|---|
| 751 | {
|
|---|
| 752 | printf("Flash unlock error\n");
|
|---|
| 753 | }
|
|---|
| 754 | if (HAL_FLASH_OB_Unlock() != HAL_OK)
|
|---|
| 755 | {
|
|---|
| 756 | printf("Flash ob unlock error\n");
|
|---|
| 757 | }
|
|---|
| 758 |
|
|---|
| 759 | printf("...Flash unlock\n");
|
|---|
| 760 | if(HAL_FLASHEx_OBProgram(&OptionsBytesStruct) != HAL_OK)
|
|---|
| 761 | {
|
|---|
| 762 | printf("...Enable lock error\n");
|
|---|
| 763 | HAL_FLASH_OB_Lock();
|
|---|
| 764 | return false;
|
|---|
| 765 | }
|
|---|
| 766 | HAL_FLASH_OB_Lock();
|
|---|
| 767 | printf("Flash Optionbyte locked\n");
|
|---|
| 768 | HAL_FLASH_Lock();
|
|---|
| 769 | printf("Flash locked\n");
|
|---|
| 770 | printf("...Enable lock process finished\n");
|
|---|
| 771 |
|
|---|
| 772 | return true;
|
|---|
| 773 | }
|
|---|
| 774 | uint8_t printprotectionstate(void)
|
|---|
| 775 | {
|
|---|
| 776 | FLASH_OBProgramInitTypeDef OptionsBytesStruct = {0};
|
|---|
| 777 | HAL_FLASHEx_OBGetConfig(&OptionsBytesStruct);
|
|---|
| 778 | uint8_t result = 0;
|
|---|
| 779 |
|
|---|
| 780 | if(OptionsBytesStruct.RDPLevel == OB_RDP_LEVEL_0)
|
|---|
| 781 | {
|
|---|
| 782 | //OptionsBytesStruct.OptionType = OPTIONBYTE_RDP;
|
|---|
| 783 | //OptionsBytesStruct.RDPLevel = OB_RDP_LEVEL_1;
|
|---|
| 784 | printf("PROTECTION: OB_RDP_LEVEL_0\n");
|
|---|
| 785 | result = 0;
|
|---|
| 786 | }
|
|---|
| 787 | else if(OptionsBytesStruct.RDPLevel == OB_RDP_LEVEL_1)
|
|---|
| 788 | {
|
|---|
| 789 | printf("PROTECTION: OB_RDP_LEVEL_1\n");
|
|---|
| 790 | result = 1;
|
|---|
| 791 | }
|
|---|
| 792 | else if(OptionsBytesStruct.RDPLevel == OB_RDP_LEVEL_2)
|
|---|
| 793 | {
|
|---|
| 794 | printf("PROTECTION: OB_RDP_LEVEL_2\n");
|
|---|
| 795 | result = 2;
|
|---|
| 796 | }
|
|---|
| 797 | return result;
|
|---|
| 798 | }
|
|---|
| 799 |
|
|---|
| 800 |
|
|---|
| 801 | /* USER CODE END 4 */
|
|---|
| 802 |
|
|---|
| 803 | /**
|
|---|
| 804 | * @brief This function is executed in case of error occurrence.
|
|---|
| 805 | * @retval None
|
|---|
| 806 | */
|
|---|
| 807 | void Error_Handler(void)
|
|---|
| 808 | {
|
|---|
| 809 | /* USER CODE BEGIN Error_Handler_Debug */
|
|---|
| 810 | /* User can add his own implementation to report the HAL error return state */
|
|---|
| 811 | __disable_irq();
|
|---|
| 812 | printf("error handler\r\n"); //Eventuell zeile entfernen, um besser den callstack zu sehen
|
|---|
| 813 |
|
|---|
| 814 | while (1)
|
|---|
| 815 | {
|
|---|
| 816 | }
|
|---|
| 817 | /* USER CODE END Error_Handler_Debug */
|
|---|
| 818 | }
|
|---|
| 819 | #ifdef USE_FULL_ASSERT
|
|---|
| 820 | /**
|
|---|
| 821 | * @brief Reports the name of the source file and the source line number
|
|---|
| 822 | * where the assert_param error has occurred.
|
|---|
| 823 | * @param file: pointer to the source file name
|
|---|
| 824 | * @param line: assert_param error line source number
|
|---|
| 825 | * @retval None
|
|---|
| 826 | */
|
|---|
| 827 | void assert_failed(uint8_t *file, uint32_t line)
|
|---|
| 828 | {
|
|---|
| 829 | /* USER CODE BEGIN 6 */
|
|---|
| 830 | /* User can add his own implementation to report the file name and line number,
|
|---|
| 831 | ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
|
|---|
| 832 | printf("Wrong parameters value: file %s on line %d\r\n", file, line);
|
|---|
| 833 | /* USER CODE END 6 */
|
|---|
| 834 | }
|
|---|
| 835 | #endif /* USE_FULL_ASSERT */
|
|---|