Changeset 22 for trunk/fw_g473rct/SES/src/main.c
- Timestamp:
- Aug 29, 2025, 6:15:42 PM (24 hours ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/fw_g473rct/SES/src/main.c
r20 r22 37 37 #include "modbus.h" 38 38 #include "chip_temperature.h" 39 #include "battery_voltage.h" 40 #include "ads1260.h" 39 41 /* USER CODE END Includes */ 40 42 … … 57 59 58 60 /* USER CODE BEGIN PV */ 59 modbus_t modbusData ;60 #define ADCCONVERTEDVALUES_BUFFER_SIZE 2 61 __IO uint 32_t adc12Data[ADCCONVERTEDVALUES_BUFFER_SIZE] __attribute__((section(".RAM1")));61 modbus_t modbusData __attribute__((section(".RAM1"))); 62 63 __IO uint16_t adc12Data[2] __attribute__((section(".RAM1"))); 62 64 __IO uint32_t adc1Data[1] __attribute__((section(".RAM1"))); 63 65 __IO uint32_t adc2Data[1] __attribute__((section(".RAM1"))); … … 66 68 __IO uint32_t adc5Data[4] __attribute__((section(".RAM1"))); 67 69 int silentmode =0; 70 static volatile uint32_t newADC1Data = 0; 71 static volatile uint32_t newADC2Data = 0; 68 72 /* USER CODE END PV */ 69 73 … … 71 75 void SystemClock_Config(void); 72 76 /* USER CODE BEGIN PFP */ 73 77 bool SetFlashReadProtection(bool state); 78 uint8_t printprotectionstate(void); 79 bool SetBootFromFlashAndReadOutProtection(void); 74 80 /* USER CODE END PFP */ 75 81 … … 88 94 /* USER CODE BEGIN 1 */ 89 95 uint8_t firstStartCatcher; 96 int mode_button_disable_time=0; 90 97 /* USER CODE END 1 */ 91 98 … … 168 175 169 176 //HAL_ADC_Start_DMA(&hadc2, (uint32_t*)adc2Data, 1); 170 if (HAL_ADCEx_MultiModeStart_DMA(&hadc1,(uint32_t *)adc12Data, ADCCONVERTEDVALUES_BUFFER_SIZE)) //Start ADC interleaved mode177 if (HAL_ADCEx_MultiModeStart_DMA(&hadc1,(uint32_t *)adc12Data,1)) //Start ADC interleaved mode 171 178 { 172 179 /* Start Error */ … … 189 196 190 197 /* USER CODE BEGIN 3 */ 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 191 256 // Modbus Kommunikation 192 HAL_Delay(1000); 193 printf("data12d1=%d,data12d2=%d,data5=%d\r\n", adc12Data[0], adc12Data[1], adc5Data[2]);257 258 // printf("data12d1=%d,data12d2=%d,data5=%d\r\n", adc12Data[0], adc12Data[1] , adc5Data[2]); 194 259 if (mbGetFrameComplete(&modbusData) == true) 195 260 { … … 258 323 259 324 /* USER CODE BEGIN 4 */ 260 325 void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc) 326 { 327 if (hadc->Instance==ADC1) 328 { 329 newADC1Data=1; 330 } 331 332 if (hadc->Instance==ADC2) 333 { 334 newADC2Data=1; 335 } 336 } 337 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 } 261 499 262 500 /* USER CODE END 4 */
Note: See TracChangeset
for help on using the changeset viewer.