source: trunk/firmware/Src/main.c@ 26

Last change on this file since 26 was 26, checked in by f.jahn, 3 months ago
  • Bug in ADC Kalibrierung (STM32 ADC Strom) behoben
  • DMA Buffer für ADC 1 und ADC wird vor Überschreibung während bearbeitung geschützt, indem Datenübertragung nur einmalig erfolgt und erst nach Auswertung wieder gestartet wird
  • RS485Modbus: Timeout Zeit wird für Baudraten >19200 korrekt berechnet
  • Hardware ID geändert
  • Separates Register für "Batterie Empty detection mode" auf Adresse 92 angelegt
File size: 21.0 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) 2019 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
21/* Includes ------------------------------------------------------------------*/
22#include "main.h"
23#include "adc.h"
24#include "dma.h"
25#include "rtc.h"
26#include "spi.h"
27#include "usart.h"
28#include "gpio.h"
29
30/* Private includes ----------------------------------------------------------*/
31/* USER CODE BEGIN Includes */
32#include <stdio.h>
33
34#include "sysdata.h"
35#include "chip_temperature.h"
36#include "shunt_voltage.h"
37#include "shunt_temperature.h"
38#include "ads1260.h"
39#include "modbus.h"
40#include "hsi16.h"
41#include "hsi16_calibration.h"
42#include "eeprom.h"
43#include "ah_counter.h"
44#include "battery_voltage.h"
45#include "ref_voltage.h"
46#include "fast_current.h"
47#include "wh_counter.h"
48#include "efficiency.h"
49#include "esr.h"
50#include "self_discharge.h"
51#include "outputs.h"
52#include "stm32g0xx_hal_crc.h"
53/* USER CODE END Includes */
54
55/* Private typedef -----------------------------------------------------------*/
56/* USER CODE BEGIN PTD */
57
58/* USER CODE END PTD */
59
60/* Private define ------------------------------------------------------------*/
61/* USER CODE BEGIN PD */
62
63/* USER CODE END PD */
64
65/* Private macro -------------------------------------------------------------*/
66/* USER CODE BEGIN PM */
67
68/* USER CODE END PM */
69
70/* Private variables ---------------------------------------------------------*/
71CRC_HandleTypeDef hcrc;
72/* USER CODE BEGIN PV */
73modbus_t modbusData;
74
75static volatile uint32_t adcData[9];
76static RTC_TimeTypeDef Time;
77static uint32_t conversionCounter;
78static double submAhCounter;
79static volatile uint32_t newADC = 0;
80
81/* USER CODE END PV */
82
83/* Private function prototypes -----------------------------------------------*/
84void SystemClock_Config(void);
85static void MX_CRC_Init(void);
86/* USER CODE BEGIN PFP */
87bool SetFlashReadProtection(bool state);
88uint8_t printprotectionstate(void);
89bool SetBootFromFlashAndReadOutProtection(void);
90void SaveCounter(void);
91/* USER CODE END PFP */
92
93/* Private user code ---------------------------------------------------------*/
94/* USER CODE BEGIN 0 */
95
96/* USER CODE END 0 */
97
98/**
99 * @brief The application entry point.
100 * @retval int
101 */
102int main(void)
103{
104 /* USER CODE BEGIN 1 */
105 uint32_t oldTime = 0;
106 uint32_t newTime = 0;
107 uint32_t timeDiff;
108 uint32_t timeDiff10ms = 0;
109 uint32_t timeDiff100ms = 0;
110 uint32_t i = 0;
111 uint32_t x;
112 int32_t mwValueCounter;
113 int silentmode =0;
114 int mode_button_disable_time=0;
115
116 uint8_t sekunde = 0;
117 uint8_t sekundeOld = 0;
118 uint32_t dummyDate;
119 uint8_t firstStartCatcher;
120
121 if (printprotectionstate() == 0)
122 {
123// SetFlashReadProtection(true);
124// HAL_FLASH_OB_Launch();
125 }
126
127 //Wenn Speichernutzung größer 50% (debug) nötig??
128 //https://community.st.com/t5/stm32-mcus-products/hard-fault-with-stm32g0b1-and-lvgl/td-p/777366
129 __HAL_FLASH_PREFETCH_BUFFER_DISABLE();
130
131 /* USER CODE END 1 */
132
133 /* MCU Configuration--------------------------------------------------------*/
134
135 /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
136 HAL_Init();
137
138 /* USER CODE BEGIN Init */
139
140 /* USER CODE END Init */
141
142 /* Configure the system clock */
143 SystemClock_Config();
144
145 /* USER CODE BEGIN SysInit */
146 // printprotectionstate();
147 printf("ISENSOR: Init\n");
148 #ifdef DO_HSI16_CALIBRATION
149 // HSI16 Frequenzkompensation Initilisierung
150 HSI16_MeasurementInit();
151 HSI16_GetCurve();
152 orderTrimmingValues();
153 #endif
154
155 /* USER CODE END SysInit */
156
157 /* Initialize all configured peripherals */
158 MX_GPIO_Init();
159 MX_DMA_Init();
160 MX_SPI1_Init();
161 //MX_USART1_UART_Init();
162 MX_ADC1_Init();
163 MX_RTC_Init();
164 MX_CRC_Init();
165 /* USER CODE BEGIN 2 */
166
167 SYS_DATA_Init();
168 WH_COUNTER_Init();
169 AH_COUNTER_Init();
170
171 startType_t startType = EEPROM_isFirstStart(&firstStartCatcher);
172 sys_data.s.parameter.reserved0 = startType;
173 sys_data.s.parameter.reserved1 = firstStartCatcher;
174 switch(startType)
175 {
176 case FIRST_START_AFTER_ERASE: EEPROM_fullRestore(&sys_data); break;
177 case FIRST_START_AFTER_COMPARTIBLE_UPDATE: EEPROM_readConfig(&sys_data); break;
178 case FIRST_START_AFTER_INCOMPARTIBLE_UPDATE: EEPROM_factoryRestore(&sys_data, 0); break; // Preserving calibration and settings
179 case FIRST_START_ERROR: EEPROM_fullRestore(&sys_data); break;
180 }
181
182 printf("read config...\n");
183 EEPROM_readConfig(&sys_data);
184
185 if(HAL_GPIO_ReadPin(BUTTON_GPIO_Port, BUTTON_Pin) == GPIO_PIN_RESET)
186 {
187 HAL_Delay(50);
188 if(HAL_GPIO_ReadPin(BUTTON_GPIO_Port, BUTTON_Pin) == GPIO_PIN_RESET)
189 {
190 printf("factory restore...\n");
191 EEPROM_factoryRestore(&sys_data, 1);
192 }
193 }
194
195 // Modbus Initialisierung
196 mbInit(&modbusData, sys_data.s.parameter.baudrate, sys_data.s.parameter.parityMode, sys_data.s.parameter.stopBit, &huart1);
197
198 // STM32G0 Chiptemperatur Kalibrierung
199 CHIP_TEMPERATURE_Calibration(/*&sys_data*/);
200
201 // ADC auf STM32G0 starten
202 HAL_ADCEx_Calibration_Start(&hadc1);
203 HAL_ADC_Start_DMA(&hadc1, (uint32_t*)adcData, 9);
204
205 // Power up Sequenz fr INA240 Strommessverstrker
206 HAL_Delay(100);
207 // ADS1260 Initialierung
208 ADS1260_init();
209 printf("ADS1260 Init\n");
210 OUTPUTS_Init();
211
212 //Display Adress with Error LED blinks
213 for (int n = 0; n < sys_data.s.parameter.slave_address; n++)
214 {
215 HAL_GPIO_WritePin(LED_ERROR_GPIO_Port, LED_ERROR_Pin, GPIO_PIN_SET);
216 HAL_Delay(350);
217 HAL_GPIO_WritePin(LED_ERROR_GPIO_Port, LED_ERROR_Pin, GPIO_PIN_RESET);
218 HAL_Delay(350);
219 }
220 /* USER CODE END 2 */
221
222 /* Infinite loop */
223 /* USER CODE BEGIN WHILE */
224
225 while (1)
226 {
227
228 if (newADC == 1)
229 {
230 // Systemtakt 64Mhz
231 // ADC Takt 64Mhz
232 // ADC DIV 1
233 // SAMPLE Time 160,5 Cycles
234 // OVERSAMPLING 16
235 // TOTAL TIME 160,5 + 12 * 16 * 8 == 22080 -->2,899 kHz
236 //Neue ADC Daten mit einer Frequenz von ca. 1,9 Khz
237 newADC = 0;
238
239 // Messe genaue Spannungsreferenz aus ADS1260 um eigene Spannungsmessung damit zu kalibrieren
240 REF_VOLTAGE_Exec(adcData[2]);
241
242 // Differenz der Spannung Ubat- und Ushunt
243 SHUNT_VOLTAGE_Exec( adcData[0]);
244
245
246 BATTERY_VOLTAGE_Exec( adcData[1]);
247
248 FAST_CURRENT_Exec( adcData[3], adcData[4]);
249
250 sys_data.s.values.batteryPower = ((int64_t) sys_data.s.values.batteryVoltage * (int64_t)sys_data.s.values.batteryCurrent) / 1000LL;
251
252 ESR_FAST_Exec();
253
254 //HAL_GPIO_TogglePin(LED_ERROR_GPIO_Port, LED_ERROR_Pin);
255
256 }
257
258 if (newCurrentValue == 1)
259 {
260 ADS1260_ConversionFinished();
261 ESR_Exec();
262
263 newCurrentValue = 0;
264
265 }
266
267 // Zeitbasis ms Systick;
268 newTime = HAL_GetTick();
269 timeDiff = newTime - oldTime;
270 oldTime = newTime;
271 timeDiff10ms += timeDiff;
272 timeDiff100ms += timeDiff;
273
274 if ((mode_button_disable_time > 0) && (timeDiff > 0))
275 {
276 mode_button_disable_time--;
277 }
278
279 // Zeitbasis Sekunde ber RTC
280 // muss immer sowohl das Zeit- als auch das Datumsregister lesen
281 // da beide Shadowregister sind sonst wird die Zeit nicht mehr geupdatet
282 sekunde = hrtc.Instance->TR & 0x0000000F;
283 dummyDate = hrtc.Instance->DR;
284
285
286
287 if (sekundeOld != sekunde)
288 {
289 sekundeOld = sekunde;
290
291 sys_data.s.values.onTime++;
292 // Funktions Led blinken
293 if (silentmode == 0)
294 {
295 HAL_GPIO_TogglePin(LED_FUNCTION_GPIO_Port, LED_FUNCTION_Pin);
296 HAL_GPIO_TogglePin(AUX_EN_GPIO_Port, AUX_EN_Pin);
297 }
298
299
300 // Amperestundenzhler
301 AH_COUNTER_Exec();
302
303 WH_COUNTER_Exec();
304
305 // LVP
306 OUTPUTS_CheckLVP();
307
308 // OVP
309 OUTPUTS_CheckOVP();
310
311 SaveCounter();
312
313 sys_data.s.values.selfDischargeStatus = SELF_DISCHARGE_Exec();
314
315 sys_data.s.values.efficiency = EFFICIENCY_Exec();
316
317
318
319 // Ausgabe der Temperatur des STM32G0
320 CHIP_TEMPERATURE_Exec( adcData[8]);
321
322 SHUNT_TEMPERATURE_Exec(adcData[7]);
323 sys_data.s.values.ovp_sense = (adcData[5] * (uint64_t)sys_data.s.values.realVdd * 11 ) / 655360.0;
324 sys_data.s.values.lvp_sense = (adcData[6] * (uint64_t)sys_data.s.values.realVdd * 11 ) / 655360.0;
325
326
327
328 }
329
330
331 if(sys_data.s.parameter.command != 0)
332 {
333 if (modbusData.current_query == MB_QUERY_NOTHING)
334 {
335 //printf("CMD = %d\n", sys_data.s.parameter.command);
336 switch (sys_data.s.parameter.command )
337 {
338 case COMMAND_STORE_CONFIG: EEPROM_storeConfig(&sys_data,0); break;
339 case COMMAND_FULL_RESTORE: EEPROM_fullRestore(&sys_data); break;
340 case COMMAND_FACTORY_RESTORE: EEPROM_factoryRestore(&sys_data, 1); break;
341 case COMMAND_RESTORE_LAST_SAVED_VALUES: EEPROM_readConfig(&sys_data); break;
342 case COMMAND_STORE_WITH_SERIAL_NUMBER: EEPROM_storeConfig(&sys_data,1); break; // Seriennummer schreiben
343 case COMMAND_RESTART: NVIC_SystemReset(); break;
344 case COMMAND_BATTERY_CURRENT_OFFSET_CAL: ADS_1260_BatteryCurrentOffsetCalibrationStart(&sys_data); break;
345 case COMMAND_BATTERY_CURRENT_OFFSET_COMMONMODE_CAL: ADS_1260_BatteryCurrentOffsetCommonModeErrorComepensationStart(&sys_data); break;
346 case COMMAND_BATTERY_CURRENT_OFFSET_TEMP_CAL: ADS_1260_BatteryCurrentOffsetTemperatureErrorComepensationStart(); break;
347 case COMMAND_BATTERY_CURRENT_GAIN_CAL: ADS_1260_BatteryCurrentGainCalibrationStart(&sys_data); break;
348 case COMMAND_BATTERY_CURRENT_GAIN_TEMP_SHUNT_CAL: ADS_1260_BatteryCurrentGainTemperatureCalibrationShuntStart(); break;
349// case COMMAND_BATTERY_CURRENT_GAIN_TEMP_CHIP_CAL: ADS_1260_BatteryCurrentGainTemperatureCalibrationChipStart(); break;
350 case COMMAND_SET_RDP_LEVEL0: SetFlashReadProtection(false); break;
351 case COMMAND_SET_RDP_LEVEL1: SetFlashReadProtection(true); break;
352 case COMMAND_SET_RDP_LEVEL1_AND_BOOTSEL: SetBootFromFlashAndReadOutProtection(); break;
353 default: printf("UNKNOWN COMMAND\n");
354 }
355 sys_data.s.parameter.command = 0;
356 }
357 else
358 {
359 //printf("wait with execution till modbus communnikation finished\n");
360 }
361 }
362
363 if((HAL_GPIO_ReadPin(BUTTON_GPIO_Port, BUTTON_Pin) == GPIO_PIN_RESET) && (mode_button_disable_time == 0))
364 {
365 HAL_Delay(10);
366 //Taste weiterhin gedrckt?
367 if(HAL_GPIO_ReadPin(BUTTON_GPIO_Port, BUTTON_Pin) == GPIO_PIN_RESET)
368 {
369 //Ja, dann Silent Mode umschalten
370 mode_button_disable_time=500;
371 if (silentmode == 0)
372 {
373 silentmode = 1;
374 HAL_GPIO_WritePin(LED_FUNCTION_GPIO_Port, LED_FUNCTION_Pin,GPIO_PIN_SET);
375 }
376 else
377 {
378 silentmode = 0;
379 }
380 }
381 }
382
383 // Modbus Kommunikation
384
385 if (mbGetFrameComplete(&modbusData) == true)
386 {
387 if (mbSlaveCheckModbusRtuQuery(&modbusData) == RESPOND_TO_QUERY)
388 {
389 if (silentmode == 0)
390 {
391 mbSlaveProcessRtuQuery(&modbusData);
392 }
393 }
394 else
395 {
396 huart1.RxState = HAL_UART_STATE_BUSY_RX;
397 }
398 }
399
400 /* USER CODE END WHILE */
401
402 /* USER CODE BEGIN 3 */
403 }
404 /* USER CODE END 3 */
405}
406
407/**
408 * @brief System Clock Configuration
409 * @retval None
410 */
411void SystemClock_Config(void)
412{
413 RCC_OscInitTypeDef RCC_OscInitStruct = {0};
414 RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
415 RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
416
417 /** Configure the main internal regulator output voltage
418 */
419 HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE2);
420 /** Configure LSE Drive Capability
421 */
422 HAL_PWR_EnableBkUpAccess();
423 __HAL_RCC_LSEDRIVE_CONFIG(RCC_LSEDRIVE_HIGH);
424 HAL_PWR_DisableBkUpAccess();
425 /** Initializes the CPU, AHB and APB busses clocks
426 */
427 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_LSE;
428 RCC_OscInitStruct.LSEState = RCC_LSE_ON;
429 RCC_OscInitStruct.HSIState = RCC_HSI_ON;
430 RCC_OscInitStruct.HSIDiv = RCC_HSI_DIV1;
431 RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
432 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
433 RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
434 RCC_OscInitStruct.PLL.PLLM = RCC_PLLM_DIV1;
435 RCC_OscInitStruct.PLL.PLLN = 8;
436 RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
437 RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
438 if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
439 {
440 Error_Handler();
441 }
442 /** Initializes the CPU, AHB and APB busses clocks
443 */
444 RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
445 |RCC_CLOCKTYPE_PCLK1;
446 RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
447 RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
448 RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
449
450 if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
451 {
452 Error_Handler();
453 }
454 /** Initializes the peripherals clocks
455 */
456 PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_RTC|RCC_PERIPHCLK_USART1
457 |RCC_PERIPHCLK_ADC;
458 PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK1;
459 PeriphClkInit.AdcClockSelection = RCC_ADCCLKSOURCE_PLLADC;
460 PeriphClkInit.RTCClockSelection = RCC_RTCCLKSOURCE_LSE;
461
462 if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
463 {
464 Error_Handler();
465 }
466}
467
468/* USER CODE BEGIN 4 */
469
470//-----------------------------------------------------------------------------
471
472static void MX_CRC_Init(void)
473{
474
475 /* USER CODE BEGIN CRC_Init 0 */
476
477 __HAL_RCC_CRC_CLK_ENABLE();
478
479 /* USER CODE END CRC_Init 0 */
480
481 /* USER CODE BEGIN CRC_Init 1 */
482
483 /* USER CODE END CRC_Init 1 */
484 hcrc.Instance = CRC;
485 hcrc.Init.DefaultPolynomialUse = DEFAULT_POLYNOMIAL_DISABLE;
486 hcrc.Init.DefaultInitValueUse = DEFAULT_INIT_VALUE_DISABLE;
487 hcrc.Init.GeneratingPolynomial = 7;
488 hcrc.Init.CRCLength = CRC_POLYLENGTH_8B;
489 hcrc.Init.InitValue = 0xFF;
490 hcrc.Init.InputDataInversionMode = CRC_INPUTDATA_INVERSION_NONE;
491 hcrc.Init.OutputDataInversionMode = CRC_OUTPUTDATA_INVERSION_DISABLE;
492 hcrc.InputDataFormat = CRC_INPUTDATA_FORMAT_BYTES;
493 if (HAL_CRC_Init(&hcrc) != HAL_OK)
494 {
495 Error_Handler();
496 }
497 /* USER CODE BEGIN CRC_Init 2 */
498
499 /* USER CODE END CRC_Init 2 */
500
501}
502
503//-----------------------------------------------------------------------------
504
505void SaveCounter(void)
506{
507 static uint32_t last_days;
508
509 // Converting seconds into days
510 uint32_t days = sys_data.s.values.onTime / (24U * 3600U);
511
512 // Alle 24 Stunden führen wir ein Speicherbefehl durch um die Counter zu spiechern
513 if (days != last_days)
514 {
515 last_days = days;
516
517 // Here we can save our counter(s)
518 EEPROM_storeConfig(&sys_data, 0);
519 }
520}
521
522//------------------------------------------------------------------------------
523
524
525
526 void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)
527 {
528 newADC = 1;
529 }
530
531
532
533/* USER CODE END 4 */
534
535/**
536 * @brief This function is executed in case of error occurrence.
537 * @retval None
538 */
539void Error_Handler(void)
540{
541 /* USER CODE BEGIN Error_Handler_Debug */
542 /* User can add his own implementation to report the HAL error return state */
543
544 /* USER CODE END Error_Handler_Debug */
545}
546
547#ifdef USE_FULL_ASSERT
548/**
549 * @brief Reports the name of the source file and the source line number
550 * where the assert_param error has occurred.
551 * @param file: pointer to the source file name
552 * @param line: assert_param error line source number
553 * @retval None
554 */
555void assert_failed(uint8_t *file, uint32_t line)
556{
557 /* USER CODE BEGIN 6 */
558 /* User can add his own implementation to report the file name and line number,
559 tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
560 /* USER CODE END 6 */
561}
562#endif /* USE_FULL_ASSERT */
563
564
565/**
566
567 * @brief Set flash read protection.
568
569 * @param [in] state: Flash read protection state, true: enable protection, false: disable protection.
570
571 * @retval true: Successful operation.
572
573 * @retval false: Operation failed.
574
575 */
576
577bool SetFlashReadProtection(bool state)
578{
579
580 FLASH_OBProgramInitTypeDef OptionsBytesStruct = {0};
581 HAL_FLASHEx_OBGetConfig(&OptionsBytesStruct);
582
583 if(state == true)
584 {
585 printf("Start enable readout protection\n");
586 if(OptionsBytesStruct.RDPLevel == OB_RDP_LEVEL_0)
587 {
588 OptionsBytesStruct.OptionType = OPTIONBYTE_RDP;
589 OptionsBytesStruct.RDPLevel = OB_RDP_LEVEL_1;
590 if (HAL_FLASH_Unlock() != HAL_OK)
591 {
592 printf("Flash unlock error\n");
593 }
594 if (HAL_FLASH_OB_Unlock() != HAL_OK)
595 {
596 printf("Flash ob unlock error\n");
597 }
598
599 printf("...Flash unlock\n");
600 if(HAL_FLASHEx_OBProgram(&OptionsBytesStruct) != HAL_OK)
601 {
602 printf("...Enable lock error\n");
603 HAL_FLASH_OB_Lock();
604 return false;
605 }
606 HAL_FLASH_OB_Lock();
607 printf("Flash Optionbyte locked\n");
608 HAL_FLASH_Lock();
609 printf("Flash locked\n");
610 printf("...Enable lock process finished\n");
611 }
612 else
613 {
614 printf("...Flash lock already active\n");
615 }
616 }
617 else
618 {
619 if(OptionsBytesStruct.RDPLevel == OB_RDP_LEVEL_1)
620 {
621 OptionsBytesStruct.OptionType = OPTIONBYTE_RDP;
622 OptionsBytesStruct.RDPLevel = OB_RDP_LEVEL_0;
623
624 if (HAL_FLASH_Unlock() != HAL_OK)
625 {
626 printf("Flash unlock error\n");
627 return false;
628 }
629 printf("...Flash unlocked\n");
630
631 if (HAL_FLASH_OB_Unlock() != HAL_OK)
632 {
633 printf("Flash ob unlock error\n");
634 return false;
635 }
636 printf("...Flash ob unlocked\n");
637
638 if(HAL_FLASHEx_OBProgram(&OptionsBytesStruct) != HAL_OK)
639 {
640 HAL_FLASH_OB_Lock();
641 printf("Flash Optionbyte programm failed\n");
642 return false;
643 }
644
645 printf("Flash Optionbyte programmed\n");
646 HAL_FLASH_OB_Lock();
647 printf("Flash Optionbyte locked\n");
648 HAL_FLASH_Lock();
649 printf("Flash locked\n");
650 printf("...Disable lock process finished\n");
651
652;
653 }
654 }
655 return true;
656}
657
658bool SetBootFromFlashAndReadOutProtection(void)
659{
660
661 FLASH_OBProgramInitTypeDef OptionsBytesStruct = {0};
662 HAL_FLASHEx_OBGetConfig(&OptionsBytesStruct);
663
664 //Konfiguriere RDP fr Readoutprotection and USER OPTION BYTE FR Boot from Flash
665 OptionsBytesStruct.OptionType = OPTIONBYTE_USER | OPTIONBYTE_RDP;
666
667 //Set Readout Protection Level 1
668 OptionsBytesStruct.OptionType = OPTIONBYTE_USER|OPTIONBYTE_RDP;
669 OptionsBytesStruct.RDPLevel = OB_RDP_LEVEL_1;
670
671 //Selecting Boot from Main Flash Memory
672 OptionsBytesStruct.USERType = OB_USER_nBOOT0 | OB_USER_nBOOT_SEL | OB_USER_nBOOT1 ;
673 OptionsBytesStruct.USERConfig = OB_USER_nBOOT0 | OB_USER_nBOOT_SEL;
674
675 if (HAL_FLASH_Unlock() != HAL_OK)
676 {
677 printf("Flash unlock error\n");
678 }
679 if (HAL_FLASH_OB_Unlock() != HAL_OK)
680 {
681 printf("Flash ob unlock error\n");
682 }
683
684 printf("...Flash unlock\n");
685 if(HAL_FLASHEx_OBProgram(&OptionsBytesStruct) != HAL_OK)
686 {
687 printf("...Enable lock error\n");
688 HAL_FLASH_OB_Lock();
689 return false;
690 }
691 HAL_FLASH_OB_Lock();
692 printf("Flash Optionbyte locked\n");
693 HAL_FLASH_Lock();
694 printf("Flash locked\n");
695 printf("...Enable lock process finished\n");
696
697 return true;
698}
699uint8_t printprotectionstate(void)
700{
701 FLASH_OBProgramInitTypeDef OptionsBytesStruct = {0};
702 HAL_FLASHEx_OBGetConfig(&OptionsBytesStruct);
703 uint8_t result = 0;
704
705 if(OptionsBytesStruct.RDPLevel == OB_RDP_LEVEL_0)
706 {
707 //OptionsBytesStruct.OptionType = OPTIONBYTE_RDP;
708 //OptionsBytesStruct.RDPLevel = OB_RDP_LEVEL_1;
709 printf("PROTECTION: OB_RDP_LEVEL_0\n");
710 result = 0;
711 }
712 else if(OptionsBytesStruct.RDPLevel == OB_RDP_LEVEL_1)
713 {
714 printf("PROTECTION: OB_RDP_LEVEL_1\n");
715 result = 1;
716 }
717 else if(OptionsBytesStruct.RDPLevel == OB_RDP_LEVEL_2)
718 {
719 printf("PROTECTION: OB_RDP_LEVEL_2\n");
720 result = 2;
721 }
722 return result;
723}
724
725/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Note: See TracBrowser for help on using the repository browser.