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

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