Ignore:
Timestamp:
Sep 7, 2025, 3:51:43 PM (7 weeks ago)
Author:
f.jahn
Message:

RTC implementiert

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/fw_g473rct/Core/Src/main.c

    r24 r25  
    2424#include "fdcan.h"
    2525#include "i2c.h"
     26#include "rtc.h"
    2627#include "spi.h"
    2728#include "usart.h"
     
    3132/* Private includes ----------------------------------------------------------*/
    3233/* USER CODE BEGIN Includes */
     34#include <stdio.h>
     35#include "sysdata.h"
     36#include "wh_counter.h"
     37#include "ah_counter.h"
     38#include "eeprom.h"
     39#include "modbus.h"
     40#include "chip_temperature.h"
     41#include "battery_voltage.h"
     42#include "ads1260.h"
     43#include "shunt_voltage.h"
     44#include "fast_current.h"
     45#include "int_bat_voltage.h"
     46#include "chip_temperature.h"
     47#include "shunt_temperature.h"
     48#include "esr.h"
     49#include "ads1260.h"
     50#include "outputs.h"
     51#include "crc.h"
     52#include "efficiency.h"
     53#include "self_discharge.h"
    3354
    3455/* USER CODE END Includes */
     
    5273
    5374/* USER CODE BEGIN PV */
    54 
     75modbus_t modbusData __attribute__((section(".RAM1")));
     76
     77__IO uint16_t adc12Data[SAMPLE_ARRAY_SIZE][2] __attribute__((section(".RAM1")));
     78__IO uint32_t adc1Data[1] __attribute__((section(".RAM1")));
     79__IO uint32_t adc2Data[1] __attribute__((section(".RAM1")));
     80__IO uint32_t adc3Data[3] __attribute__((section(".RAM1")));
     81__IO uint32_t adc4Data[1] __attribute__((section(".RAM1")));
     82__IO uint32_t adc5Data[4] __attribute__((section(".RAM1")));
     83int silentmode =0;
     84static volatile uint32_t newADC12Data = 0;
     85static volatile uint32_t newADC3Data = 0;
     86static volatile uint32_t newADC4Data = 0;
     87static volatile uint32_t newADC5Data = 0;
    5588/* USER CODE END PV */
    5689
     
    5891void SystemClock_Config(void);
    5992/* USER CODE BEGIN PFP */
    60 
     93bool SetFlashReadProtection(bool state);
     94uint8_t printprotectionstate(void);
     95bool SetBootFromFlashAndReadOutProtection(void);
     96void LoadBackupRegister(void);
     97void SaveBackupRegister(void);
    6198/* USER CODE END PFP */
    6299
     
    74111
    75112  /* USER CODE BEGIN 1 */
     113  uint8_t firstStartCatcher;
     114  int mode_button_disable_time=0;
     115  uint32_t oldTime = 0;
     116  uint32_t newTime = 0;
     117  uint32_t timeDiff;
     118  uint8_t sekunde = 0;
     119  uint8_t sekundeOld = 0;
     120  uint32_t dummyDate;
    76121
    77122  /* USER CODE END 1 */
     
    110155  MX_USB_PCD_Init();
    111156  MX_CRC_Init();
     157  MX_RTC_Init();
    112158  /* USER CODE BEGIN 2 */
    113 
     159  printf("Test debug io\r\n");
     160  SYS_DATA_Init();
     161  WH_COUNTER_Init();
     162  AH_COUNTER_Init();
     163
     164
     165  startType_t startType = EEPROM_isFirstStart();
     166
     167  switch(startType)
     168  {
     169          case FIRST_START_AFTER_ERASE:                                 EEPROM_fullRestore(&sys_data);            break;
     170          case FIRST_START_AFTER_COMPARTIBLE_UPDATE:    EEPROM_readConfig(&sys_data);             break;
     171          case FIRST_START_AFTER_INCOMPARTIBLE_UPDATE:  EEPROM_factoryRestore(&sys_data, 0);  break;  // Preserving calibration and settings
     172          case FIRST_START_ERROR:                                               EEPROM_fullRestore(&sys_data);            break;
     173  }
     174
     175  if(HAL_GPIO_ReadPin(GPIO_INPUT_BTN_MODE_GPIO_Port, GPIO_INPUT_BTN_MODE_Pin) == GPIO_PIN_RESET)
     176  {
     177    HAL_Delay(50);
     178    if(HAL_GPIO_ReadPin(GPIO_INPUT_BTN_MODE_GPIO_Port, GPIO_INPUT_BTN_MODE_Pin) == GPIO_PIN_RESET)
     179    {
     180      printf("factory restore...\n");
     181      EEPROM_factoryRestore(&sys_data, 1);
     182    }
     183  }
     184
     185    // Modbus Initialisierung
     186  mbInit(&modbusData, sys_data.s.parameter.baudrate, sys_data.s.parameter.parityMode, sys_data.s.parameter.stopBit, &huart2);
     187
     188    // STM32G0 Chiptemperatur Kalibrierung
     189  CHIP_TEMPERATURE_Calibration();
     190
     191  HAL_ADCEx_Calibration_Start(&hadc1, ADC_DIFFERENTIAL_ENDED);
     192  HAL_ADCEx_Calibration_Start(&hadc2, ADC_DIFFERENTIAL_ENDED);
     193  HAL_ADCEx_Calibration_Start(&hadc3, ADC_SINGLE_ENDED);
     194  HAL_ADCEx_Calibration_Start(&hadc4, ADC_DIFFERENTIAL_ENDED);
     195  HAL_ADCEx_Calibration_Start(&hadc5, ADC_SINGLE_ENDED);
     196
     197
     198  //SET_BIT(hadc2.Instance->CFGR, ADC_CFGR_DMAEN); //Enable DMA transfer for ADC slave (ADC12_CCR.MDMA = 0b00 -> MDMA mode disabled)
     199  //HAL_DMA_Start(hadc2.DMA_Handle,(uint32_t)&hadc2.Instance->DR, (uint32_t)adc2Data,1); //Start ADC slave DMA
     200  //SET_BIT(hadc1.Instance->CFGR, ADC_CFGR_DMAEN); //Enable DMA transfer for ADC master (ADC12_CCR.MDMA = 0b00 -> MDMA mode disabled)
     201 
     202    //HAL_ADC_Start_DMA(&hadc2, (uint32_t*)adc2Data, 1);
     203  if (HAL_ADCEx_MultiModeStart_DMA(&hadc1,(uint32_t *)adc12Data,SAMPLE_ARRAY_SIZE))  //Start ADC interleaved mode
     204  {
     205     /* Start Error */
     206     Error_Handler();
     207  }
     208
     209  if (HAL_ADC_Start_DMA(&hadc3, (uint32_t *) adc3Data , 3))
     210  {
     211     /* Start Error */
     212     Error_Handler();
     213  }
     214
     215  if (HAL_ADC_Start_DMA(&hadc4, (uint32_t *) adc4Data , 1))
     216  {
     217     /* Start Error */
     218     Error_Handler();
     219  }
     220
     221
     222  if (HAL_ADC_Start_DMA(&hadc5, (uint32_t *) adc5Data , 4))
     223  {
     224     /* Start Error */
     225     Error_Handler();
     226  }
     227
     228
     229  // ADS1260 Initialierung
     230  ADS1260_init();
     231  printf("ADS1260 Init\n");
     232  OUTPUTS_Init();
     233
     234  //Display Adress with Error LED blinks
     235  for (int n = 0; n < sys_data.s.parameter.slave_address; n++)
     236  {
     237    HAL_GPIO_WritePin(LED_ERROR_GPIO_Port, LED_ERROR_Pin, GPIO_PIN_SET);
     238    HAL_Delay(350);
     239    HAL_GPIO_WritePin(LED_ERROR_GPIO_Port, LED_ERROR_Pin, GPIO_PIN_RESET);
     240    HAL_Delay(350);
     241  }
     242
     243  LoadBackupRegister();
    114244  /* USER CODE END 2 */
    115245
     
    121251
    122252    /* USER CODE BEGIN 3 */
     253if (newADC12Data == 1)
     254    {
     255          //Mit ADC_DIV2,Sample time 12,5Cycklen, ADC Clock 50Mhz, Oversampling 256
     256          //Tconv = 6400 Takte = 0,128ms Pro Konvertierung. Also für 100 messwerte 12,8mS
     257          BATTERY_VOLTAGE_Exec( adc12Data[0][1]);
     258          FAST_CURRENT_Exec(adc12Data[0][0]);
     259          sys_data.s.values.esrCalcStatus =  ESR_FAST_Exec();
     260          newADC12Data = 0;
     261
     262        }
     263
     264
     265        if (newADC3Data == 1)
     266    {
     267          SHUNT_TEMPERATURE_Exec(adc3Data[0]);
     268        }
     269
     270
     271        if (newADC4Data == 1)
     272    {
     273          SHUNT_VOLTAGE_Exec( adc4Data[0]);
     274        }
     275
     276
     277        if (newADC5Data == 1)
     278    {
     279          CHIP_TEMPERATURE_Exec(adc5Data[0]);
     280          INT_BAT_VOLTAGE_Exec( adc5Data[1]);
     281          sys_data.s.values.ovp_sense =  (adc5Data[2] * VREF * 21 ) / 65536.0;
     282      sys_data.s.values.lvp_sense =  (adc5Data[3] * VREF * 21 ) / 65536.0;
     283        }
     284
     285
     286        if (newCurrentValue == 1)
     287    {
     288      ADS1260_ConversionFinished();
     289      ESR_Exec();
     290
     291      newCurrentValue = 0;
     292
     293    }
     294
     295         // Zeitbasis ms Systick;
     296    newTime = HAL_GetTick();
     297    timeDiff = newTime - oldTime;
     298    oldTime = newTime;
     299
     300    if ((mode_button_disable_time > 0) && (timeDiff > 0))
     301    {
     302      mode_button_disable_time--;
     303    }
     304
     305    // Zeitbasis Sekunde ber RTC
     306    // muss immer sowohl das Zeit- als auch das Datumsregister lesen
     307    // da beide Shadowregister sind sonst wird die Zeit nicht mehr geupdatet
     308    sekunde = hrtc.Instance->TR & 0x0000000F;
     309    dummyDate = hrtc.Instance->DR;
     310
     311
     312
     313    if (sekundeOld != sekunde)
     314    {
     315      sekundeOld = sekunde;
     316
     317      sys_data.s.values.onTime++;
     318      // Funktions Led blinken
     319      if (silentmode == 0)
     320      {
     321        HAL_GPIO_TogglePin(LED_FUNCTION_GPIO_Port, LED_FUNCTION_Pin);
     322                HAL_GPIO_TogglePin(AUX_EN_GPIO_Port, AUX_EN_Pin);
     323      }
     324
     325
     326      // Amperestundenzhler
     327      AH_COUNTER_Exec();
     328
     329      WH_COUNTER_Exec();
     330
     331      // LVP
     332      OUTPUTS_CheckLVP();
     333
     334      // OVP
     335      OUTPUTS_CheckOVP();
     336
     337      SaveBackupRegister();
     338
     339      sys_data.s.values.selfDischargeStatus = SELF_DISCHARGE_Exec();
     340
     341      sys_data.s.values.efficiency = EFFICIENCY_Exec();
     342
     343
     344    }
     345
     346
     347        if(sys_data.s.parameter.command != 0)
     348    {
     349      if  (modbusData.current_query ==  MB_QUERY_NOTHING)
     350      {
     351        //printf("CMD = %d\n", sys_data.s.parameter.command);
     352        switch (sys_data.s.parameter.command )
     353        {
     354          case COMMAND_STORE_CONFIG:                          EEPROM_storeConfig(&sys_data,0);                                                                                    break;
     355          case COMMAND_FULL_RESTORE:                          EEPROM_fullRestore(&sys_data);                                                                                      break;
     356          case COMMAND_FACTORY_RESTORE:                       EEPROM_factoryRestore(&sys_data, 1);                                                                                break;
     357          case COMMAND_RESTORE_LAST_SAVED_VALUES:             EEPROM_readConfig(&sys_data);                                                                                               break;
     358          case COMMAND_STORE_WITH_SERIAL_NUMBER:              EEPROM_storeConfig(&sys_data,1);                                                                                    break;        // Seriennummer schreiben
     359          case COMMAND_RESTART:                               NVIC_SystemReset();                                                                                                                 break;
     360          case COMMAND_BATTERY_CURRENT_OFFSET_CAL:            ADS_1260_BatteryCurrentOffsetCalibrationStart(&sys_data);                                   break;
     361          case COMMAND_BATTERY_CURRENT_OFFSET_COMMONMODE_CAL: ADS_1260_BatteryCurrentOffsetCommonModeErrorComepensationStart(&sys_data);  break;
     362          case COMMAND_BATTERY_CURRENT_OFFSET_TEMP_CAL:       ADS_1260_BatteryCurrentOffsetTemperatureErrorComepensationStart();                  break;
     363          case COMMAND_BATTERY_CURRENT_GAIN_CAL:              ADS_1260_BatteryCurrentGainCalibrationStart(&sys_data);                                     break;
     364          case COMMAND_BATTERY_CURRENT_GAIN_TEMP_SHUNT_CAL:   ADS_1260_BatteryCurrentGainTemperatureCalibrationShuntStart();                      break;
     365//        case COMMAND_BATTERY_CURRENT_GAIN_TEMP_CHIP_CAL:    ADS_1260_BatteryCurrentGainTemperatureCalibrationChipStart(); break;
     366          case COMMAND_SET_RDP_LEVEL0:                        SetFlashReadProtection(false);                                                                                      break;
     367          case COMMAND_SET_RDP_LEVEL1:                        SetFlashReadProtection(true);                                                                                               break;
     368          case COMMAND_SET_RDP_LEVEL1_AND_BOOTSEL:            SetBootFromFlashAndReadOutProtection();                                                                     break;
     369          default:                                            printf("UNKNOWN COMMAND\n");
     370        }
     371        sys_data.s.parameter.command = 0;
     372      }
     373      else
     374      {
     375        //printf("wait with execution till modbus communnikation finished\n");
     376      }
     377    }
     378
     379    if((HAL_GPIO_ReadPin(GPIO_INPUT_BTN_MODE_GPIO_Port, GPIO_INPUT_BTN_MODE_Pin) == GPIO_PIN_RESET) && (mode_button_disable_time == 0))
     380    {
     381      HAL_Delay(10);
     382      //Taste weiterhin gedrckt?
     383      if(HAL_GPIO_ReadPin(GPIO_INPUT_BTN_MODE_GPIO_Port, GPIO_INPUT_BTN_MODE_Pin) == GPIO_PIN_RESET)
     384      {
     385        //Ja, dann Silent Mode umschalten
     386        mode_button_disable_time=500;
     387        if (silentmode == 0)
     388        {
     389          silentmode = 1;
     390          HAL_GPIO_WritePin(LED_FUNCTION_GPIO_Port, LED_FUNCTION_Pin,GPIO_PIN_SET);
     391        }
     392        else
     393        {
     394          silentmode = 0;
     395        }
     396       }
     397    }
     398
     399        // Modbus Kommunikation
     400
     401//      printf("data12d1=%d,data12d2=%d,data5=%d\r\n", adc12Data[0], adc12Data[1] , adc5Data[2]);
     402    if (mbGetFrameComplete(&modbusData) == true)
     403    {
     404      if (mbSlaveCheckModbusRtuQuery(&modbusData) == RESPOND_TO_QUERY)
     405      {
     406          if (silentmode == 0)
     407          {
     408            mbSlaveProcessRtuQuery(&modbusData);
     409          }
     410      }
     411      else
     412      {
     413        huart1.RxState = HAL_UART_STATE_BUSY_RX;
     414      }
     415    }
     416
     417 
    123418  }
    124419  /* USER CODE END 3 */
     
    138433  HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1);
    139434
     435  /** Configure LSE Drive Capability
     436  */
     437  HAL_PWR_EnableBkUpAccess();
     438  __HAL_RCC_LSEDRIVE_CONFIG(RCC_LSEDRIVE_LOW);
     439
    140440  /** Initializes the RCC Oscillators according to the specified parameters
    141441  * in the RCC_OscInitTypeDef structure.
    142442  */
    143   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI48|RCC_OSCILLATORTYPE_HSE;
     443  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI48|RCC_OSCILLATORTYPE_HSE
     444                              |RCC_OSCILLATORTYPE_LSE;
    144445  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
     446  RCC_OscInitStruct.LSEState = RCC_LSE_ON;
    145447  RCC_OscInitStruct.HSI48State = RCC_HSI48_ON;
    146448  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
     
    172474
    173475/* USER CODE BEGIN 4 */
    174 
     476void LoadBackupRegister()
     477{
     478    if (HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR1) == 0xBEBE)
     479    {
     480          printf("BackupRegisterData available\r\n");
     481          sys_data.s.values.dischargeTotalAh = HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR2);
     482        }
     483
     484
     485}
     486
     487
     488void SaveBackupRegister(void)
     489{
     490        static uint32_t lastTime;
     491
     492       
     493        uint32_t time = sys_data.s.values.onTime;
     494
     495        // Alle 24 Stunden führen wir ein Speicherbefehl durch um die Counter zu speichern
     496        if (time != lastTime)
     497        {
     498                lastTime = time;
     499                HAL_PWR_EnableBkUpAccess();
     500                HAL_RTCEx_BKUPWrite(&hrtc, RTC_BKP_DR1, 0xBEBE);
     501
     502                // Total Energie und Ah Umsatz der Batterie (seit inbetriebnahme)
     503            HAL_RTCEx_BKUPWrite(&hrtc, RTC_BKP_DR2, sys_data.s.values.dischargeTotalAh);
     504                HAL_RTCEx_BKUPWrite(&hrtc, RTC_BKP_DR3, sys_data.s.values.chargeTotalAh);
     505                HAL_RTCEx_BKUPWrite(&hrtc, RTC_BKP_DR4, sys_data.s.values.dischargeTotalWh);
     506                HAL_RTCEx_BKUPWrite(&hrtc, RTC_BKP_DR5, sys_data.s.values.chargeTotalWh);
     507
     508                // Betriebszeit
     509                HAL_RTCEx_BKUPWrite(&hrtc, RTC_BKP_DR6, sys_data.s.values.onTime);
     510
     511                // Erkannte Kapazitäten
     512                HAL_RTCEx_BKUPWrite(&hrtc, RTC_BKP_DR7, sys_data.s.values.detectedCapacity);
     513                HAL_RTCEx_BKUPWrite(&hrtc, RTC_BKP_DR8, sys_data.s.values.detectedEnergy);
     514
     515
     516                HAL_RTCEx_BKUPWrite(&hrtc, RTC_BKP_DR8, sys_data.s.values.mAh_AutoMode);
     517                HAL_RTCEx_BKUPWrite(&hrtc, RTC_BKP_DR8, sys_data.s.values.mWh_AutoMode);
     518                HAL_RTCEx_BKUPWrite(&hrtc, RTC_BKP_DR8, sys_data.s.values.mAhCounter);
     519
     520
     521
     522
     523
     524        }
     525
     526
     527}
     528
     529
     530
     531 void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)
     532 {
     533    if (hadc->Instance==ADC1)
     534        {
     535          newADC12Data=1;
     536        }
     537
     538        if (hadc->Instance==ADC3)
     539        {
     540          newADC3Data=1;
     541        }
     542
     543
     544        if (hadc->Instance==ADC4)
     545        {
     546          newADC4Data=1;
     547        }
     548
     549        if (hadc->Instance==ADC5)
     550        {
     551          newADC5Data=1;
     552        }
     553 }
     554
     555
     556 
     557/**
     558
     559  * @brief  Set flash read protection.
     560
     561  * @param  [in] state: Flash read protection state, true: enable protection, false: disable protection.
     562
     563  * @retval true:  Successful operation.
     564
     565  * @retval false: Operation failed.
     566
     567  */
     568
     569bool SetFlashReadProtection(bool state)
     570{
     571
     572  FLASH_OBProgramInitTypeDef OptionsBytesStruct = {0};
     573  HAL_FLASHEx_OBGetConfig(&OptionsBytesStruct);
     574
     575  if(state == true)
     576  {
     577    printf("Start enable readout protection\n");
     578    if(OptionsBytesStruct.RDPLevel == OB_RDP_LEVEL_0)
     579    {
     580      OptionsBytesStruct.OptionType = OPTIONBYTE_RDP;
     581      OptionsBytesStruct.RDPLevel   = OB_RDP_LEVEL_1;
     582      if (HAL_FLASH_Unlock() != HAL_OK)
     583      {
     584        printf("Flash unlock error\n");
     585      }
     586      if (HAL_FLASH_OB_Unlock() != HAL_OK)
     587      {
     588        printf("Flash ob unlock error\n");
     589      }
     590
     591      printf("...Flash unlock\n");
     592      if(HAL_FLASHEx_OBProgram(&OptionsBytesStruct) != HAL_OK)
     593      {
     594        printf("...Enable lock error\n");
     595        HAL_FLASH_OB_Lock();
     596        return false;
     597      }
     598      HAL_FLASH_OB_Lock();
     599      printf("Flash Optionbyte locked\n");
     600      HAL_FLASH_Lock();
     601      printf("Flash  locked\n");
     602      printf("...Enable lock process finished\n");
     603    }
     604    else
     605    {
     606      printf("...Flash lock already active\n");
     607    }
     608  }
     609  else
     610  {
     611    if(OptionsBytesStruct.RDPLevel == OB_RDP_LEVEL_1)
     612    {
     613      OptionsBytesStruct.OptionType = OPTIONBYTE_RDP;
     614      OptionsBytesStruct.RDPLevel   = OB_RDP_LEVEL_0;
     615
     616      if (HAL_FLASH_Unlock() != HAL_OK)
     617      {
     618        printf("Flash unlock error\n");
     619        return false;
     620      }
     621      printf("...Flash unlocked\n");
     622
     623      if (HAL_FLASH_OB_Unlock() != HAL_OK)
     624      {
     625        printf("Flash ob unlock error\n");
     626        return false;
     627      }
     628      printf("...Flash ob unlocked\n");
     629
     630      if(HAL_FLASHEx_OBProgram(&OptionsBytesStruct) != HAL_OK)
     631      {
     632        HAL_FLASH_OB_Lock();
     633        printf("Flash Optionbyte programm failed\n");
     634        return false;
     635      }
     636
     637      printf("Flash Optionbyte programmed\n");
     638      HAL_FLASH_OB_Lock();
     639      printf("Flash Optionbyte locked\n");
     640      HAL_FLASH_Lock();
     641      printf("Flash  locked\n");
     642      printf("...Disable lock process finished\n");
     643
     644;
     645    }
     646  }
     647  return true;
     648}
     649
     650bool SetBootFromFlashAndReadOutProtection(void)
     651{
     652
     653  FLASH_OBProgramInitTypeDef OptionsBytesStruct = {0};
     654  HAL_FLASHEx_OBGetConfig(&OptionsBytesStruct);
     655
     656  //Konfiguriere RDP fr Readoutprotection and USER OPTION BYTE FR Boot from Flash
     657  OptionsBytesStruct.OptionType = OPTIONBYTE_USER | OPTIONBYTE_RDP;
     658
     659  //Set Readout Protection Level 1
     660  OptionsBytesStruct.OptionType = OPTIONBYTE_USER|OPTIONBYTE_RDP;
     661  OptionsBytesStruct.RDPLevel   = OB_RDP_LEVEL_1;
     662
     663  //Selecting Boot from Main Flash Memory
     664  OptionsBytesStruct.USERType =    OB_USER_nBOOT0 | OB_USER_nSWBOOT0 | OB_USER_nBOOT1 ;
     665  OptionsBytesStruct.USERConfig  = OB_USER_nBOOT0 | OB_USER_nSWBOOT0;
     666
     667  if (HAL_FLASH_Unlock() != HAL_OK)
     668  {
     669    printf("Flash unlock error\n");
     670  }
     671  if (HAL_FLASH_OB_Unlock() != HAL_OK)
     672  {
     673    printf("Flash ob unlock error\n");
     674  }
     675
     676  printf("...Flash unlock\n");
     677  if(HAL_FLASHEx_OBProgram(&OptionsBytesStruct) != HAL_OK)
     678  {
     679    printf("...Enable lock error\n");
     680    HAL_FLASH_OB_Lock();
     681    return false;
     682  }
     683  HAL_FLASH_OB_Lock();
     684  printf("Flash Optionbyte locked\n");
     685  HAL_FLASH_Lock();
     686  printf("Flash  locked\n");
     687  printf("...Enable lock process finished\n");
     688
     689  return true;
     690}
     691uint8_t printprotectionstate(void)
     692{
     693  FLASH_OBProgramInitTypeDef OptionsBytesStruct = {0};
     694  HAL_FLASHEx_OBGetConfig(&OptionsBytesStruct);
     695  uint8_t result = 0;
     696
     697  if(OptionsBytesStruct.RDPLevel == OB_RDP_LEVEL_0)
     698  {
     699    //OptionsBytesStruct.OptionType = OPTIONBYTE_RDP;
     700    //OptionsBytesStruct.RDPLevel   = OB_RDP_LEVEL_1;
     701    printf("PROTECTION: OB_RDP_LEVEL_0\n");
     702    result = 0;
     703  }
     704  else if(OptionsBytesStruct.RDPLevel == OB_RDP_LEVEL_1)
     705  {
     706    printf("PROTECTION: OB_RDP_LEVEL_1\n");
     707    result = 1;
     708  }
     709  else if(OptionsBytesStruct.RDPLevel == OB_RDP_LEVEL_2)
     710  {
     711    printf("PROTECTION: OB_RDP_LEVEL_2\n");
     712    result = 2;
     713  }
     714  return  result;
     715}
    175716/* USER CODE END 4 */
    176717
Note: See TracChangeset for help on using the changeset viewer.