[20] | 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 "dma.h" |
---|
| 23 | #include "fdcan.h" |
---|
| 24 | #include "i2c.h" |
---|
| 25 | #include "spi.h" |
---|
| 26 | #include "usart.h" |
---|
| 27 | #include "usb.h" |
---|
| 28 | #include "gpio.h" |
---|
| 29 | |
---|
| 30 | /* Private includes ----------------------------------------------------------*/ |
---|
| 31 | /* USER CODE BEGIN Includes */ |
---|
| 32 | #include <stdio.h> |
---|
| 33 | #include "sysdata.h" |
---|
| 34 | #include "wh_counter.h" |
---|
| 35 | #include "ah_counter.h" |
---|
| 36 | #include "eeprom.h" |
---|
| 37 | #include "modbus.h" |
---|
| 38 | #include "chip_temperature.h" |
---|
[22] | 39 | #include "battery_voltage.h" |
---|
| 40 | #include "ads1260.h" |
---|
[20] | 41 | /* USER CODE END Includes */ |
---|
| 42 | |
---|
| 43 | /* Private typedef -----------------------------------------------------------*/ |
---|
| 44 | /* USER CODE BEGIN PTD */ |
---|
| 45 | |
---|
| 46 | /* USER CODE END PTD */ |
---|
| 47 | |
---|
| 48 | /* Private define ------------------------------------------------------------*/ |
---|
| 49 | /* USER CODE BEGIN PD */ |
---|
| 50 | |
---|
| 51 | /* USER CODE END PD */ |
---|
| 52 | |
---|
| 53 | /* Private macro -------------------------------------------------------------*/ |
---|
| 54 | /* USER CODE BEGIN PM */ |
---|
| 55 | |
---|
| 56 | /* USER CODE END PM */ |
---|
| 57 | |
---|
| 58 | /* Private variables ---------------------------------------------------------*/ |
---|
| 59 | |
---|
| 60 | /* USER CODE BEGIN PV */ |
---|
[22] | 61 | modbus_t modbusData __attribute__((section(".RAM1"))); |
---|
| 62 | |
---|
| 63 | __IO uint16_t adc12Data[2] __attribute__((section(".RAM1"))); |
---|
[20] | 64 | __IO uint32_t adc1Data[1] __attribute__((section(".RAM1"))); |
---|
| 65 | __IO uint32_t adc2Data[1] __attribute__((section(".RAM1"))); |
---|
| 66 | __IO uint32_t adc3Data[3] __attribute__((section(".RAM1"))); |
---|
| 67 | __IO uint32_t adc4Data[1] __attribute__((section(".RAM1"))); |
---|
| 68 | __IO uint32_t adc5Data[4] __attribute__((section(".RAM1"))); |
---|
| 69 | int silentmode =0; |
---|
[22] | 70 | static volatile uint32_t newADC1Data = 0; |
---|
| 71 | static volatile uint32_t newADC2Data = 0; |
---|
[20] | 72 | /* USER CODE END PV */ |
---|
| 73 | |
---|
| 74 | /* Private function prototypes -----------------------------------------------*/ |
---|
| 75 | void SystemClock_Config(void); |
---|
| 76 | /* USER CODE BEGIN PFP */ |
---|
[22] | 77 | bool SetFlashReadProtection(bool state); |
---|
| 78 | uint8_t printprotectionstate(void); |
---|
| 79 | bool SetBootFromFlashAndReadOutProtection(void); |
---|
[20] | 80 | /* USER CODE END PFP */ |
---|
| 81 | |
---|
| 82 | /* Private user code ---------------------------------------------------------*/ |
---|
| 83 | /* USER CODE BEGIN 0 */ |
---|
| 84 | |
---|
| 85 | /* USER CODE END 0 */ |
---|
| 86 | |
---|
| 87 | /** |
---|
| 88 | * @brief The application entry point. |
---|
| 89 | * @retval int |
---|
| 90 | */ |
---|
| 91 | int main(void) |
---|
| 92 | { |
---|
| 93 | |
---|
| 94 | /* USER CODE BEGIN 1 */ |
---|
| 95 | uint8_t firstStartCatcher; |
---|
[22] | 96 | int mode_button_disable_time=0; |
---|
[20] | 97 | /* USER CODE END 1 */ |
---|
| 98 | |
---|
| 99 | /* MCU Configuration--------------------------------------------------------*/ |
---|
| 100 | |
---|
| 101 | /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ |
---|
| 102 | HAL_Init(); |
---|
| 103 | |
---|
| 104 | /* USER CODE BEGIN Init */ |
---|
| 105 | |
---|
| 106 | /* USER CODE END Init */ |
---|
| 107 | |
---|
| 108 | /* Configure the system clock */ |
---|
| 109 | SystemClock_Config(); |
---|
| 110 | |
---|
| 111 | /* USER CODE BEGIN SysInit */ |
---|
| 112 | |
---|
| 113 | /* USER CODE END SysInit */ |
---|
| 114 | |
---|
| 115 | /* Initialize all configured peripherals */ |
---|
| 116 | MX_GPIO_Init(); |
---|
| 117 | MX_DMA_Init(); |
---|
| 118 | MX_ADC1_Init(); |
---|
| 119 | MX_ADC2_Init(); |
---|
| 120 | MX_ADC3_Init(); |
---|
| 121 | MX_ADC4_Init(); |
---|
| 122 | MX_ADC5_Init(); |
---|
| 123 | MX_FDCAN2_Init(); |
---|
| 124 | MX_FDCAN3_Init(); |
---|
| 125 | MX_I2C3_Init(); |
---|
| 126 | MX_I2C4_Init(); |
---|
| 127 | MX_SPI3_Init(); |
---|
| 128 | MX_USART1_UART_Init(); |
---|
| 129 | MX_USART2_UART_Init(); |
---|
| 130 | MX_USB_PCD_Init(); |
---|
| 131 | /* USER CODE BEGIN 2 */ |
---|
| 132 | printf("Test debug io\r\n"); |
---|
| 133 | SYS_DATA_Init(); |
---|
| 134 | WH_COUNTER_Init(); |
---|
| 135 | AH_COUNTER_Init(); |
---|
| 136 | |
---|
| 137 | |
---|
| 138 | startType_t startType = EEPROM_isFirstStart(); |
---|
| 139 | |
---|
| 140 | switch(startType) |
---|
| 141 | { |
---|
| 142 | case FIRST_START_AFTER_ERASE: EEPROM_fullRestore(&sys_data); break; |
---|
| 143 | case FIRST_START_AFTER_COMPARTIBLE_UPDATE: EEPROM_readConfig(&sys_data); break; |
---|
| 144 | case FIRST_START_AFTER_INCOMPARTIBLE_UPDATE: EEPROM_factoryRestore(&sys_data, 0); break; // Preserving calibration and settings |
---|
| 145 | case FIRST_START_ERROR: EEPROM_fullRestore(&sys_data); break; |
---|
| 146 | } |
---|
| 147 | |
---|
| 148 | if(HAL_GPIO_ReadPin(GPIO_INPUT_BTN_MODE_GPIO_Port, GPIO_INPUT_BTN_MODE_Pin) == GPIO_PIN_RESET) |
---|
| 149 | { |
---|
| 150 | HAL_Delay(50); |
---|
| 151 | if(HAL_GPIO_ReadPin(GPIO_INPUT_BTN_MODE_GPIO_Port, GPIO_INPUT_BTN_MODE_Pin) == GPIO_PIN_RESET) |
---|
| 152 | { |
---|
| 153 | printf("factory restore...\n"); |
---|
| 154 | EEPROM_factoryRestore(&sys_data, 1); |
---|
| 155 | } |
---|
| 156 | } |
---|
| 157 | |
---|
| 158 | // Modbus Initialisierung |
---|
| 159 | mbInit(&modbusData, sys_data.s.parameter.baudrate, sys_data.s.parameter.parityMode, sys_data.s.parameter.stopBit, &huart2); |
---|
| 160 | |
---|
| 161 | // STM32G0 Chiptemperatur Kalibrierung |
---|
| 162 | CHIP_TEMPERATURE_Calibration(/*&sys_data*/); |
---|
| 163 | |
---|
| 164 | HAL_ADCEx_Calibration_Start(&hadc1, ADC_DIFFERENTIAL_ENDED); |
---|
| 165 | HAL_ADCEx_Calibration_Start(&hadc2, ADC_DIFFERENTIAL_ENDED); |
---|
| 166 | HAL_ADCEx_Calibration_Start(&hadc3, ADC_SINGLE_ENDED); |
---|
| 167 | HAL_ADCEx_Calibration_Start(&hadc4, ADC_DIFFERENTIAL_ENDED); |
---|
| 168 | HAL_ADCEx_Calibration_Start(&hadc5, ADC_SINGLE_ENDED); |
---|
| 169 | |
---|
| 170 | |
---|
| 171 | |
---|
| 172 | //SET_BIT(hadc2.Instance->CFGR, ADC_CFGR_DMAEN); //Enable DMA transfer for ADC slave (ADC12_CCR.MDMA = 0b00 -> MDMA mode disabled) |
---|
| 173 | //HAL_DMA_Start(hadc2.DMA_Handle,(uint32_t)&hadc2.Instance->DR, (uint32_t)adc2Data,1); //Start ADC slave DMA |
---|
| 174 | //SET_BIT(hadc1.Instance->CFGR, ADC_CFGR_DMAEN); //Enable DMA transfer for ADC master (ADC12_CCR.MDMA = 0b00 -> MDMA mode disabled) |
---|
| 175 | |
---|
| 176 | //HAL_ADC_Start_DMA(&hadc2, (uint32_t*)adc2Data, 1); |
---|
[22] | 177 | if (HAL_ADCEx_MultiModeStart_DMA(&hadc1,(uint32_t *)adc12Data,1)) //Start ADC interleaved mode |
---|
[20] | 178 | { |
---|
| 179 | /* Start Error */ |
---|
| 180 | Error_Handler(); |
---|
| 181 | } |
---|
| 182 | |
---|
| 183 | if (HAL_ADC_Start_DMA(&hadc5, (uint32_t *) adc5Data , 4)) |
---|
| 184 | { |
---|
| 185 | /* Start Error */ |
---|
| 186 | Error_Handler(); |
---|
| 187 | } |
---|
| 188 | |
---|
| 189 | /* USER CODE END 2 */ |
---|
| 190 | |
---|
| 191 | /* Infinite loop */ |
---|
| 192 | /* USER CODE BEGIN WHILE */ |
---|
| 193 | while (1) |
---|
| 194 | { |
---|
| 195 | /* USER CODE END WHILE */ |
---|
| 196 | |
---|
| 197 | /* USER CODE BEGIN 3 */ |
---|
[22] | 198 | if (newADC1Data == 1) |
---|
| 199 | { |
---|
| 200 | BATTERY_VOLTAGE_Exec( adc12Data[1]); |
---|
| 201 | } |
---|
| 202 | |
---|
| 203 | |
---|
| 204 | if(sys_data.s.parameter.command != 0) |
---|
| 205 | { |
---|
| 206 | if (modbusData.current_query == MB_QUERY_NOTHING) |
---|
| 207 | { |
---|
| 208 | //printf("CMD = %d\n", sys_data.s.parameter.command); |
---|
| 209 | switch (sys_data.s.parameter.command ) |
---|
| 210 | { |
---|
| 211 | case COMMAND_STORE_CONFIG: EEPROM_storeConfig(&sys_data,0); break; |
---|
| 212 | case COMMAND_FULL_RESTORE: EEPROM_fullRestore(&sys_data); break; |
---|
| 213 | case COMMAND_FACTORY_RESTORE: EEPROM_factoryRestore(&sys_data, 1); break; |
---|
| 214 | case COMMAND_RESTORE_LAST_SAVED_VALUES: EEPROM_readConfig(&sys_data); break; |
---|
| 215 | case COMMAND_STORE_WITH_SERIAL_NUMBER: EEPROM_storeConfig(&sys_data,1); break; // Seriennummer schreiben |
---|
| 216 | case COMMAND_RESTART: NVIC_SystemReset(); break; |
---|
| 217 | case COMMAND_BATTERY_CURRENT_OFFSET_CAL: ADS_1260_BatteryCurrentOffsetCalibrationStart(&sys_data); break; |
---|
| 218 | case COMMAND_BATTERY_CURRENT_OFFSET_COMMONMODE_CAL: ADS_1260_BatteryCurrentOffsetCommonModeErrorComepensationStart(&sys_data); break; |
---|
| 219 | case COMMAND_BATTERY_CURRENT_OFFSET_TEMP_CAL: ADS_1260_BatteryCurrentOffsetTemperatureErrorComepensationStart(); break; |
---|
| 220 | case COMMAND_BATTERY_CURRENT_GAIN_CAL: ADS_1260_BatteryCurrentGainCalibrationStart(&sys_data); break; |
---|
| 221 | case COMMAND_BATTERY_CURRENT_GAIN_TEMP_SHUNT_CAL: ADS_1260_BatteryCurrentGainTemperatureCalibrationShuntStart(); break; |
---|
| 222 | // case COMMAND_BATTERY_CURRENT_GAIN_TEMP_CHIP_CAL: ADS_1260_BatteryCurrentGainTemperatureCalibrationChipStart(); break; |
---|
| 223 | case COMMAND_SET_RDP_LEVEL0: SetFlashReadProtection(false); break; |
---|
| 224 | case COMMAND_SET_RDP_LEVEL1: SetFlashReadProtection(true); break; |
---|
| 225 | case COMMAND_SET_RDP_LEVEL1_AND_BOOTSEL: SetBootFromFlashAndReadOutProtection(); break; |
---|
| 226 | default: printf("UNKNOWN COMMAND\n"); |
---|
| 227 | } |
---|
| 228 | sys_data.s.parameter.command = 0; |
---|
| 229 | } |
---|
| 230 | else |
---|
| 231 | { |
---|
| 232 | //printf("wait with execution till modbus communnikation finished\n"); |
---|
| 233 | } |
---|
| 234 | } |
---|
| 235 | |
---|
| 236 | if((HAL_GPIO_ReadPin(GPIO_INPUT_BTN_MODE_GPIO_Port, GPIO_INPUT_BTN_MODE_Pin) == GPIO_PIN_RESET) && (mode_button_disable_time == 0)) |
---|
| 237 | { |
---|
| 238 | HAL_Delay(10); |
---|
| 239 | //Taste weiterhin gedrckt? |
---|
| 240 | if(HAL_GPIO_ReadPin(GPIO_INPUT_BTN_MODE_GPIO_Port, GPIO_INPUT_BTN_MODE_Pin) == GPIO_PIN_RESET) |
---|
| 241 | { |
---|
| 242 | //Ja, dann Silent Mode umschalten |
---|
| 243 | mode_button_disable_time=500; |
---|
| 244 | if (silentmode == 0) |
---|
| 245 | { |
---|
| 246 | silentmode = 1; |
---|
| 247 | HAL_GPIO_WritePin(LED_FUNC_GPIO_Port, LED_FUNC_Pin,GPIO_PIN_SET); |
---|
| 248 | } |
---|
| 249 | else |
---|
| 250 | { |
---|
| 251 | silentmode = 0; |
---|
| 252 | } |
---|
| 253 | } |
---|
| 254 | } |
---|
| 255 | |
---|
[20] | 256 | // Modbus Kommunikation |
---|
[22] | 257 | |
---|
| 258 | // printf("data12d1=%d,data12d2=%d,data5=%d\r\n", adc12Data[0], adc12Data[1] , adc5Data[2]); |
---|
[20] | 259 | if (mbGetFrameComplete(&modbusData) == true) |
---|
| 260 | { |
---|
| 261 | if (mbSlaveCheckModbusRtuQuery(&modbusData) == RESPOND_TO_QUERY) |
---|
| 262 | { |
---|
| 263 | if (silentmode == 0) |
---|
| 264 | { |
---|
| 265 | mbSlaveProcessRtuQuery(&modbusData); |
---|
| 266 | } |
---|
| 267 | } |
---|
| 268 | else |
---|
| 269 | { |
---|
| 270 | huart1.RxState = HAL_UART_STATE_BUSY_RX; |
---|
| 271 | } |
---|
| 272 | } |
---|
| 273 | |
---|
| 274 | } |
---|
| 275 | /* USER CODE END 3 */ |
---|
| 276 | } |
---|
| 277 | |
---|
| 278 | /** |
---|
| 279 | * @brief System Clock Configuration |
---|
| 280 | * @retval None |
---|
| 281 | */ |
---|
| 282 | void SystemClock_Config(void) |
---|
| 283 | { |
---|
| 284 | RCC_OscInitTypeDef RCC_OscInitStruct = {0}; |
---|
| 285 | RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; |
---|
| 286 | |
---|
| 287 | /** Configure the main internal regulator output voltage |
---|
| 288 | */ |
---|
| 289 | HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1); |
---|
| 290 | |
---|
| 291 | /** Initializes the RCC Oscillators according to the specified parameters |
---|
| 292 | * in the RCC_OscInitTypeDef structure. |
---|
| 293 | */ |
---|
| 294 | RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI48|RCC_OSCILLATORTYPE_HSE; |
---|
| 295 | RCC_OscInitStruct.HSEState = RCC_HSE_ON; |
---|
| 296 | RCC_OscInitStruct.HSI48State = RCC_HSI48_ON; |
---|
| 297 | RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; |
---|
| 298 | RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; |
---|
| 299 | RCC_OscInitStruct.PLL.PLLM = RCC_PLLM_DIV2; |
---|
| 300 | RCC_OscInitStruct.PLL.PLLN = 16; |
---|
| 301 | RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2; |
---|
| 302 | RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2; |
---|
| 303 | RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2; |
---|
| 304 | if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) |
---|
| 305 | { |
---|
| 306 | Error_Handler(); |
---|
| 307 | } |
---|
| 308 | |
---|
| 309 | /** Initializes the CPU, AHB and APB buses clocks |
---|
| 310 | */ |
---|
| 311 | RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK |
---|
| 312 | |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2; |
---|
| 313 | RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; |
---|
| 314 | RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; |
---|
| 315 | RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1; |
---|
| 316 | RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; |
---|
| 317 | |
---|
| 318 | if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_3) != HAL_OK) |
---|
| 319 | { |
---|
| 320 | Error_Handler(); |
---|
| 321 | } |
---|
| 322 | } |
---|
| 323 | |
---|
| 324 | /* USER CODE BEGIN 4 */ |
---|
[22] | 325 | void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc) |
---|
| 326 | { |
---|
| 327 | if (hadc->Instance==ADC1) |
---|
| 328 | { |
---|
| 329 | newADC1Data=1; |
---|
| 330 | } |
---|
[20] | 331 | |
---|
[22] | 332 | if (hadc->Instance==ADC2) |
---|
| 333 | { |
---|
| 334 | newADC2Data=1; |
---|
| 335 | } |
---|
| 336 | } |
---|
[20] | 337 | |
---|
[22] | 338 | |
---|
| 339 | |
---|
| 340 | /** |
---|
| 341 | |
---|
| 342 | * @brief Set flash read protection. |
---|
| 343 | |
---|
| 344 | * @param [in] state: Flash read protection state, true: enable protection, false: disable protection. |
---|
| 345 | |
---|
| 346 | * @retval true: Successful operation. |
---|
| 347 | |
---|
| 348 | * @retval false: Operation failed. |
---|
| 349 | |
---|
| 350 | */ |
---|
| 351 | |
---|
| 352 | bool SetFlashReadProtection(bool state) |
---|
| 353 | { |
---|
| 354 | |
---|
| 355 | FLASH_OBProgramInitTypeDef OptionsBytesStruct = {0}; |
---|
| 356 | HAL_FLASHEx_OBGetConfig(&OptionsBytesStruct); |
---|
| 357 | |
---|
| 358 | if(state == true) |
---|
| 359 | { |
---|
| 360 | printf("Start enable readout protection\n"); |
---|
| 361 | if(OptionsBytesStruct.RDPLevel == OB_RDP_LEVEL_0) |
---|
| 362 | { |
---|
| 363 | OptionsBytesStruct.OptionType = OPTIONBYTE_RDP; |
---|
| 364 | OptionsBytesStruct.RDPLevel = OB_RDP_LEVEL_1; |
---|
| 365 | if (HAL_FLASH_Unlock() != HAL_OK) |
---|
| 366 | { |
---|
| 367 | printf("Flash unlock error\n"); |
---|
| 368 | } |
---|
| 369 | if (HAL_FLASH_OB_Unlock() != HAL_OK) |
---|
| 370 | { |
---|
| 371 | printf("Flash ob unlock error\n"); |
---|
| 372 | } |
---|
| 373 | |
---|
| 374 | printf("...Flash unlock\n"); |
---|
| 375 | if(HAL_FLASHEx_OBProgram(&OptionsBytesStruct) != HAL_OK) |
---|
| 376 | { |
---|
| 377 | printf("...Enable lock error\n"); |
---|
| 378 | HAL_FLASH_OB_Lock(); |
---|
| 379 | return false; |
---|
| 380 | } |
---|
| 381 | HAL_FLASH_OB_Lock(); |
---|
| 382 | printf("Flash Optionbyte locked\n"); |
---|
| 383 | HAL_FLASH_Lock(); |
---|
| 384 | printf("Flash locked\n"); |
---|
| 385 | printf("...Enable lock process finished\n"); |
---|
| 386 | } |
---|
| 387 | else |
---|
| 388 | { |
---|
| 389 | printf("...Flash lock already active\n"); |
---|
| 390 | } |
---|
| 391 | } |
---|
| 392 | else |
---|
| 393 | { |
---|
| 394 | if(OptionsBytesStruct.RDPLevel == OB_RDP_LEVEL_1) |
---|
| 395 | { |
---|
| 396 | OptionsBytesStruct.OptionType = OPTIONBYTE_RDP; |
---|
| 397 | OptionsBytesStruct.RDPLevel = OB_RDP_LEVEL_0; |
---|
| 398 | |
---|
| 399 | if (HAL_FLASH_Unlock() != HAL_OK) |
---|
| 400 | { |
---|
| 401 | printf("Flash unlock error\n"); |
---|
| 402 | return false; |
---|
| 403 | } |
---|
| 404 | printf("...Flash unlocked\n"); |
---|
| 405 | |
---|
| 406 | if (HAL_FLASH_OB_Unlock() != HAL_OK) |
---|
| 407 | { |
---|
| 408 | printf("Flash ob unlock error\n"); |
---|
| 409 | return false; |
---|
| 410 | } |
---|
| 411 | printf("...Flash ob unlocked\n"); |
---|
| 412 | |
---|
| 413 | if(HAL_FLASHEx_OBProgram(&OptionsBytesStruct) != HAL_OK) |
---|
| 414 | { |
---|
| 415 | HAL_FLASH_OB_Lock(); |
---|
| 416 | printf("Flash Optionbyte programm failed\n"); |
---|
| 417 | return false; |
---|
| 418 | } |
---|
| 419 | |
---|
| 420 | printf("Flash Optionbyte programmed\n"); |
---|
| 421 | HAL_FLASH_OB_Lock(); |
---|
| 422 | printf("Flash Optionbyte locked\n"); |
---|
| 423 | HAL_FLASH_Lock(); |
---|
| 424 | printf("Flash locked\n"); |
---|
| 425 | printf("...Disable lock process finished\n"); |
---|
| 426 | |
---|
| 427 | ; |
---|
| 428 | } |
---|
| 429 | } |
---|
| 430 | return true; |
---|
| 431 | } |
---|
| 432 | |
---|
| 433 | bool SetBootFromFlashAndReadOutProtection(void) |
---|
| 434 | { |
---|
| 435 | |
---|
| 436 | FLASH_OBProgramInitTypeDef OptionsBytesStruct = {0}; |
---|
| 437 | HAL_FLASHEx_OBGetConfig(&OptionsBytesStruct); |
---|
| 438 | |
---|
| 439 | //Konfiguriere RDP fr Readoutprotection and USER OPTION BYTE FR Boot from Flash |
---|
| 440 | OptionsBytesStruct.OptionType = OPTIONBYTE_USER | OPTIONBYTE_RDP; |
---|
| 441 | |
---|
| 442 | //Set Readout Protection Level 1 |
---|
| 443 | OptionsBytesStruct.OptionType = OPTIONBYTE_USER|OPTIONBYTE_RDP; |
---|
| 444 | OptionsBytesStruct.RDPLevel = OB_RDP_LEVEL_1; |
---|
| 445 | |
---|
| 446 | //Selecting Boot from Main Flash Memory |
---|
| 447 | OptionsBytesStruct.USERType = OB_USER_nBOOT0 | OB_USER_nSWBOOT0 | OB_USER_nBOOT1 ; |
---|
| 448 | OptionsBytesStruct.USERConfig = OB_USER_nBOOT0 | OB_USER_nSWBOOT0; |
---|
| 449 | |
---|
| 450 | if (HAL_FLASH_Unlock() != HAL_OK) |
---|
| 451 | { |
---|
| 452 | printf("Flash unlock error\n"); |
---|
| 453 | } |
---|
| 454 | if (HAL_FLASH_OB_Unlock() != HAL_OK) |
---|
| 455 | { |
---|
| 456 | printf("Flash ob unlock error\n"); |
---|
| 457 | } |
---|
| 458 | |
---|
| 459 | printf("...Flash unlock\n"); |
---|
| 460 | if(HAL_FLASHEx_OBProgram(&OptionsBytesStruct) != HAL_OK) |
---|
| 461 | { |
---|
| 462 | printf("...Enable lock error\n"); |
---|
| 463 | HAL_FLASH_OB_Lock(); |
---|
| 464 | return false; |
---|
| 465 | } |
---|
| 466 | HAL_FLASH_OB_Lock(); |
---|
| 467 | printf("Flash Optionbyte locked\n"); |
---|
| 468 | HAL_FLASH_Lock(); |
---|
| 469 | printf("Flash locked\n"); |
---|
| 470 | printf("...Enable lock process finished\n"); |
---|
| 471 | |
---|
| 472 | return true; |
---|
| 473 | } |
---|
| 474 | uint8_t printprotectionstate(void) |
---|
| 475 | { |
---|
| 476 | FLASH_OBProgramInitTypeDef OptionsBytesStruct = {0}; |
---|
| 477 | HAL_FLASHEx_OBGetConfig(&OptionsBytesStruct); |
---|
| 478 | uint8_t result = 0; |
---|
| 479 | |
---|
| 480 | if(OptionsBytesStruct.RDPLevel == OB_RDP_LEVEL_0) |
---|
| 481 | { |
---|
| 482 | //OptionsBytesStruct.OptionType = OPTIONBYTE_RDP; |
---|
| 483 | //OptionsBytesStruct.RDPLevel = OB_RDP_LEVEL_1; |
---|
| 484 | printf("PROTECTION: OB_RDP_LEVEL_0\n"); |
---|
| 485 | result = 0; |
---|
| 486 | } |
---|
| 487 | else if(OptionsBytesStruct.RDPLevel == OB_RDP_LEVEL_1) |
---|
| 488 | { |
---|
| 489 | printf("PROTECTION: OB_RDP_LEVEL_1\n"); |
---|
| 490 | result = 1; |
---|
| 491 | } |
---|
| 492 | else if(OptionsBytesStruct.RDPLevel == OB_RDP_LEVEL_2) |
---|
| 493 | { |
---|
| 494 | printf("PROTECTION: OB_RDP_LEVEL_2\n"); |
---|
| 495 | result = 2; |
---|
| 496 | } |
---|
| 497 | return result; |
---|
| 498 | } |
---|
| 499 | |
---|
[20] | 500 | /* USER CODE END 4 */ |
---|
| 501 | |
---|
| 502 | /** |
---|
| 503 | * @brief This function is executed in case of error occurrence. |
---|
| 504 | * @retval None |
---|
| 505 | */ |
---|
| 506 | void Error_Handler(void) |
---|
| 507 | { |
---|
| 508 | /* USER CODE BEGIN Error_Handler_Debug */ |
---|
| 509 | /* User can add his own implementation to report the HAL error return state */ |
---|
| 510 | __disable_irq(); |
---|
| 511 | printf("error handler!\r\n"); |
---|
| 512 | while (1) |
---|
| 513 | { |
---|
| 514 | } |
---|
| 515 | /* USER CODE END Error_Handler_Debug */ |
---|
| 516 | } |
---|
| 517 | #ifdef USE_FULL_ASSERT |
---|
| 518 | /** |
---|
| 519 | * @brief Reports the name of the source file and the source line number |
---|
| 520 | * where the assert_param error has occurred. |
---|
| 521 | * @param file: pointer to the source file name |
---|
| 522 | * @param line: assert_param error line source number |
---|
| 523 | * @retval None |
---|
| 524 | */ |
---|
| 525 | void assert_failed(uint8_t *file, uint32_t line) |
---|
| 526 | { |
---|
| 527 | /* USER CODE BEGIN 6 */ |
---|
| 528 | /* User can add his own implementation to report the file name and line number, |
---|
| 529 | ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ |
---|
| 530 | printf("Wrong parameters value: file %s on line %d\r\n", file, line); |
---|
| 531 | /* USER CODE END 6 */ |
---|
| 532 | } |
---|
| 533 | #endif /* USE_FULL_ASSERT */ |
---|