Changeset 22 for trunk/fw_g473rct/SES
- Timestamp:
- Aug 29, 2025, 6:15:42 PM (33 hours ago)
- Location:
- trunk/fw_g473rct/SES
- Files:
-
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/fw_g473rct/SES/inc/battery_voltage.h
r20 r22 20 20 21 21 //--- GLOBALE FUNKTIONS PROTOTYPEN --------------------------------------------- 22 void BATTERY_VOLTAGE_Exec( uint32_t newvalP );22 void BATTERY_VOLTAGE_Exec(int32_t newvalP ); 23 23 24 24 #endif // -
trunk/fw_g473rct/SES/inc/sysdata.h
r20 r22 33 33 //------------------------------------------------------------------------------ 34 34 // This structure must be aligned to 4-byte address 35 typedef struct35 volatile typedef struct 36 36 { 37 37 // Device spezifisch / Einstellungsbereich … … 58 58 uint16_t ibn_year; // 16 Inbetriebnahme Datum: Jahr 59 59 uint16_t user_id; // 17 Freies Feld für eine Kunden ID oder ähnlich 60 uint16_t UNIQUE_NAME(reserved)[8]; // 18-25 RESERVIERT60 uint16_t test[8]; // 18-25 RESERVIERT 61 61 62 62 //--- Konfiguration Schaltausgänge --- … … 175 175 /* 4 of 8 */ uint32_t chargeTotalWh; // 156-157 [mAs] 176 176 /* 6 of 8 */ uint16_t fullCyclesCnt; // 158 177 /* 8 of 8 */ uint16_t UNIQUE_NAME(reserved); // 159177 /* 8 of 8 */ uint16_t volatile UNIQUE_NAME(reserved); // 159 178 178 /* 8 of 8 */ int64_t mAs_AutoMode; // 160-163 179 179 /* 8 of 8 */ int64_t mWs_AutoMode; // 164-167 -
trunk/fw_g473rct/SES/smartPro.emProject
r20 r22 1 1 <!DOCTYPE CrossStudio_Project_File> 2 2 <solution Name="smartPro" target="8" version="2"> 3 <configuration Name="Common" link_merge_sections="No" /> 3 4 <configuration 4 Name="Debug "5 Name="Debug125A_12V" 5 6 c_preprocessor_definitions="DEBUG" 6 7 gcc_debugging_level="Level 3" … … 8 9 gcc_optimization_level="None" /> 9 10 <configuration 10 Name="Release "11 Name="Release125A_12V" 11 12 c_preprocessor_definitions="NDEBUG" 12 13 gcc_debugging_level="Level 2" … … 33 34 c_preprocessor_definitions="__VTOR_CONFIG;ARM_MATH_CM4;STM32G473xx;__STM32G473_SUBFAMILY;__STM32G4XX_FAMILY" 34 35 c_user_include_directories="$(ProjectDir)/../Drivers/STM32G4xx_HAL_Driver/Inc;$(ProjectDir)/../Core/Inc;$(ProjectDir)/../Drivers/CMSIS/Include;$(ProjectDir)/../Drivers/CMSIS/Device/ST/STM32G4xx/Include;$(ProjectDir)/inc" 36 debug_initial_breakpoint_set_option="Never" 35 37 debug_register_definition_file="$(ProjectDir)/STM32G473xx_Registers.xml" 36 38 debug_stack_pointer_start="__stack_end__" … … 45 47 target_load_end_script="Reset();" 46 48 target_reset_script="Reset();" /> 47 <configuration Name="Debug" c_preprocessor_definitions="DEVICETYPE=500" /> 49 <configuration 50 Name="Debug125A_12V" 51 arm_compiler_variant="gcc" 52 c_preprocessor_definitions="DEVICETYPE=125" /> 53 <configuration 54 Name="Release125A_12V" 55 c_preprocessor_definitions="DEVICETYPE=125" /> 48 56 <folder Name="CMSIS Files"> 49 57 <file file_name="STM32G4xx/Device/Include/stm32g4xx.h" /> -
trunk/fw_g473rct/SES/src/battery_voltage.c
r20 r22 21 21 22 22 #define BATTERY_VOLTAGE_VOLTAGE_DIVIDER 6 23 23 #define ADC_RESOLUTION 65536 //65536/2 da im differential mode 24 #define ADC_OFFSET 0 25 #define VREF 3000 24 26 // --- LOKALE TYPE DEFS - bitte hier dokumentieren------------------------------- 25 27 … … 35 37 36 38 37 void BATTERY_VOLTAGE_Exec( uint32_t newvalP )39 void BATTERY_VOLTAGE_Exec(int32_t newvalP ) 38 40 { 39 41 static int measCounter; 40 42 static unsigned long avgsumP = 0; 41 uint32_t avgvalP;43 int32_t avgvalP; 42 44 43 45 if (measCounter < INT32_MAX) measCounter++; … … 53 55 54 56 //Umrechung auf Eingangsspannung am Gerät mit Teiler 55 sys_data.s.values.batteryVoltage = ( avgvalP * (uint64_t)sys_data.s.values.realVdd * BATTERY_VOLTAGE_VOLTAGE_DIVIDER ) / 65536;57 sys_data.s.values.batteryVoltage = ((avgvalP-2048) * 3000 * BATTERY_VOLTAGE_VOLTAGE_DIVIDER ) / 2048; 56 58 57 59 … … 75 77 //Berechnung schnellen Wert ohne Glättung: 76 78 //Umrechung auf Eingangsspannung am Gerät mit Teiler 77 sys_data.s.values.fast_voltage = ( newvalP * (uint64_t)sys_data.s.values.realVdd * BATTERY_VOLTAGE_VOLTAGE_DIVIDER ) / 65536;79 sys_data.s.values.fast_voltage = ((newvalP-ADC_OFFSET) * VREF * BATTERY_VOLTAGE_VOLTAGE_DIVIDER ) / ADC_RESOLUTION; 78 80 79 81 -
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 */ -
trunk/fw_g473rct/SES/src/modbus.c
r20 r22 67 67 #define FAST_BAUDRATE_INTERFRAME_DELAY_us (1750UL) 68 68 // --- Externe Variablen -------------------------------------------- 69 extern modbus_t modbusData;70 extern sys_data_t sys_data;69 extern volatile modbus_t modbusData; 70 extern volatile sys_data_t sys_data; 71 71 72 72 … … 158 158 159 159 // Init aus Cube 160 mb_data->uart->Instance = USART 1;161 mb_data->uart->Init.BaudRate = 19200;160 mb_data->uart->Instance = USART2; 161 mb_data->uart->Init.BaudRate = baudrate; 162 162 mb_data->uart->Init.WordLength = UART_WORDLENGTH_9B; 163 163 mb_data->uart->Init.StopBits = UART_STOPBITS_1; … … 168 168 mb_data->uart->Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE; 169 169 mb_data->uart->Init.ClockPrescaler = UART_PRESCALER_DIV1; 170 mb_data->uart->AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT; 171 172 // Init nderungen 170 mb_data->uart->AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_SWAP_INIT; 171 mb_data->uart->AdvancedInit.Swap = UART_ADVFEATURE_SWAP_ENABLE; 172 173 // Init änderungen 173 174 174 175 // Baudrate … … 200 201 } 201 202 // Init 202 if (RS485_ModbusEx_Init(mb_data->uart, UART_DE_POLARITY_HIGH, 0, 0,TIMEOUT_FRAME_COMPLETE*nrOfBitsPerChar) != HAL_OK) 203 { 204 Error_Handler(); 205 } 206 if (HAL_UARTEx_DisableFifoMode(mb_data->uart) != HAL_OK) 207 { 208 Error_Handler(); 209 } 210 211 if(HAL_UART_Receive_IT(mb_data->uart, mb_data->rx_buffer, RXBUFFERSIZE) != HAL_OK) 203 // if (RS485_ModbusEx_Init(mb_data->uart, UART_DE_POLARITY_HIGH, 0, 0,TIMEOUT_FRAME_COMPLETE*nrOfBitsPerChar) != HAL_OK) 204 // { 205 // Error_Handler(); 206 // } 207 // if (HAL_UARTEx_DisableFifoMode(mb_data->uart) != HAL_OK) 208 // { 209 // Error_Handler(); 210 // } 211 uint32_t fixedDelayInBitDurations = (FAST_BAUDRATE_INTERFRAME_DELAY_us * baudrate) / 1000000UL + 1UL; 212 // HAL_UART_EnableReceiverTimeout( usart); 213 // HAL_UART_ReceiverTimeout_Config(usart, fixedDelayInBitDurations); 214 215 if(HAL_UARTEx_ReceiveToIdle_DMA(mb_data->uart, mb_data->rx_buffer, RXBUFFERSIZE) != HAL_OK) 212 216 { 213 217 printf("uart error \n\r"); … … 218 222 } 219 223 220 /* 221 * 222 * @brief End ongoing Rx transfer on UART peripheral (following error detection or Reception completion). 223 * @param huart: UART handle. 224 * @retval None 225 */ 226 void mbUartEndRxTransfer(UART_HandleTypeDef *huart) 227 { 228 /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */ 229 //CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE)); 230 //CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE); 231 232 /* At end of Rx process, restore huart->RxState to Ready */ 233 huart->RxState = HAL_UART_STATE_READY; 234 } 235 236 void mbUartRx(modbus_t* mb_data) 237 { 238 uint32_t pos; 239 uint8_t data; 240 241 pos = mb_data->rx_head; 242 243 if (pos >= RXBUFFERSIZE) 244 { 245 return; 246 } 247 //data = mb_data->uart->Instance->RDR 248 data = mb_data->uart->Instance->RDR & (uint8_t)0x00FF; //NOTE: mb_data->uart.Instance->DR gendert 249 mb_data->rx_buffer[pos] = data; 250 251 mb_data->rx_head++; 252 253 //mbTimerStart(mb_data); 254 } 255 256 HAL_StatusTypeDef mbUartTx(UART_HandleTypeDef *huart) 257 { 258 uint16_t* tmp; 259 260 if(huart->Init.WordLength == UART_WORDLENGTH_9B) 261 { 262 tmp = (uint16_t*) huart->pTxBuffPtr; 263 huart->Instance->TDR = (uint16_t)(*tmp & (uint16_t)0x01FF); //NOTE: huart->Instance->DR gendert 264 265 if(huart->Init.Parity == UART_PARITY_NONE) 266 { 267 huart->pTxBuffPtr += 2; 268 } 269 else 270 { 271 huart->pTxBuffPtr += 1; 272 } 273 } 274 else 275 { 276 huart->Instance->TDR = (uint8_t)(*huart->pTxBuffPtr++ & (uint8_t)0x00FF); //NOTE:huart->Instance->DR gendert 277 } 278 279 if(--huart->TxXferCount == 0) 280 { 281 /* Disable the UART Transmit Complete Interrupt */ 282 __HAL_UART_DISABLE_IT(huart, UART_IT_TXE); 283 284 /* Enable the UART Transmit Complete Interrupt */ 285 __HAL_UART_ENABLE_IT(huart, UART_IT_TC); 286 } 287 return HAL_OK; 288 } 289 290 // huart->State gibt es nicht mehr. Ersetzt durch huart->gState. 291 /** 292 * @brief Wraps up transmission in non blocking mode. 293 * @param huart: pointer to a UART_HandleTypeDef structure that contains 294 * the configuration information for the specified UART module. 295 * @retval HAL status 296 */ 297 static void UART_EndTransmit_IT(UART_HandleTypeDef *huart) 298 { 299 /* Disable the UART Transmit Complete Interrupt */ 300 CLEAR_BIT(huart->Instance->CR1, USART_CR1_TCIE); 301 302 /* Tx process is ended, restore huart->gState to Ready */ 303 huart->gState = HAL_UART_STATE_READY; 304 305 /* Cleat TxISR function pointer */ 306 huart->TxISR = NULL; 307 308 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1) 309 /*Call registered Tx complete callback*/ 310 huart->TxCpltCallback(huart); 311 #else 312 /*Call legacy weak Tx complete callback*/ 313 HAL_UART_TxCpltCallback(huart); 314 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */ 315 } 316 317 318 static HAL_StatusTypeDef RS485_ModbusEx_Init(UART_HandleTypeDef *huart, uint32_t Polarity, uint32_t AssertionTime, uint32_t DeassertionTime, uint32_t charReceiveTimeout) 224 225 226 void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart) 319 227 { 320 uint32_t temp; 321 322 /* Check the UART handle allocation */ 323 if (huart == NULL) 324 { 325 return HAL_ERROR; 326 } 327 /* Check the Driver Enable UART instance */ 328 assert_param(IS_UART_DRIVER_ENABLE_INSTANCE(huart->Instance)); 329 330 /* Check the Driver Enable polarity */ 331 assert_param(IS_UART_DE_POLARITY(Polarity)); 332 333 /* Check the Driver Enable assertion time */ 334 assert_param(IS_UART_ASSERTIONTIME(AssertionTime)); 335 336 /* Check the Driver Enable deassertion time */ 337 assert_param(IS_UART_DEASSERTIONTIME(DeassertionTime)); 338 339 if (huart->gState == HAL_UART_STATE_RESET) 340 { 341 /* Allocate lock resource and initialize it */ 342 huart->Lock = HAL_UNLOCKED; 343 344 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1) 345 UART_InitCallbacksToDefault(huart); 346 347 if (huart->MspInitCallback == NULL) 348 { 349 huart->MspInitCallback = HAL_UART_MspInit; 350 } 351 352 /* Init the low level hardware */ 353 huart->MspInitCallback(huart); 354 #else 355 /* Init the low level hardware : GPIO, CLOCK, CORTEX */ 356 HAL_UART_MspInit(huart); 357 #endif /* (USE_HAL_UART_REGISTER_CALLBACKS) */ 358 } 359 360 huart->gState = HAL_UART_STATE_BUSY; 361 362 /* Disable the Peripheral */ 363 __HAL_UART_DISABLE(huart); 364 365 /* Set the UART Communication parameters */ 366 if (UART_SetConfig(huart) == HAL_ERROR) 367 { 368 return HAL_ERROR; 369 } 370 371 if (huart->AdvancedInit.AdvFeatureInit != UART_ADVFEATURE_NO_INIT) 372 { 373 UART_AdvFeatureConfig(huart); 374 } 375 376 /* Enable the Driver Enable mode by setting the DEM bit in the CR3 register */ 377 //EDIT SMART_SWITCH: HIER wurde ein chip mit autoerkennung implementiert 378 // SET_BIT(huart->Instance->CR3, USART_CR3_DEM); 379 380 /* Set the Driver Enable polarity */ 381 // MODIFY_REG(huart->Instance->CR3, USART_CR3_DEP, Polarity); 382 383 /* Set the Driver Enable assertion and deassertion times */ 384 // temp = (AssertionTime << UART_CR1_DEAT_ADDRESS_LSB_POS); 385 // temp |= (DeassertionTime << UART_CR1_DEDT_ADDRESS_LSB_POS); 386 // MODIFY_REG(huart->Instance->CR1, (USART_CR1_DEDT | USART_CR1_DEAT), temp); 387 388 // EDIT ECS START 389 /*Set receive timeout time*/ 390 SET_BIT(huart->Instance->CR2, USART_CR2_RTOEN); 391 SET_BIT(huart->Instance->CR1, USART_CR1_RTOIE); 392 //MODIFY_REG(huart->Instance->RTOR, USART_RTOR_RTO, charReceiveTimeout); 393 if (huart->Init.BaudRate <= 19200) MODIFY_REG(huart->Instance->RTOR, USART_RTOR_RTO, charReceiveTimeout); 394 else 395 { 396 uint32_t fixedDelayInBitDurations = (FAST_BAUDRATE_INTERFRAME_DELAY_us * huart->Init.BaudRate) / 1000000UL + 1UL; 397 MODIFY_REG(huart->Instance->RTOR, USART_RTOR_RTO, fixedDelayInBitDurations); 398 } 399 // EDIT ECS END 400 401 /* Enable the Peripheral */ 402 __HAL_UART_ENABLE(huart); 403 404 /* TEACK and/or REACK to check before moving huart->gState and huart->RxState to Ready */ 405 return (UART_CheckIdleState(huart)); 406 } 407 408 409 static int test; 410 void MODBUS_UART_IRQHandler(UART_HandleTypeDef *huart) 411 { 412 uint32_t isrflags = READ_REG(huart->Instance->ISR); 413 uint32_t cr1its = READ_REG(huart->Instance->CR1); 414 uint32_t cr2its = READ_REG(huart->Instance->CR2); 415 uint32_t cr3its = READ_REG(huart->Instance->CR3); 416 417 uint32_t errorflags; 418 uint32_t errorcode; 419 420 errorflags = (isrflags & (uint32_t)(USART_ISR_PE | USART_ISR_FE | USART_ISR_ORE | USART_ISR_NE)); 421 422 /* If some errors occur */ 423 if ((errorflags != 0U) 424 && ((((cr3its & (USART_CR3_RXFTIE | USART_CR3_EIE)) != 0U) 425 || ((cr1its & (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE)) != 0U)))) 426 { 427 /* UART parity error interrupt occurred -------------------------------------*/ 428 if (((isrflags & USART_ISR_PE) != 0U) && ((cr1its & USART_CR1_PEIE) != 0U)) 429 { 430 __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_PEF); 431 432 huart->ErrorCode |= HAL_UART_ERROR_PE; 433 } 434 435 /* UART frame error interrupt occurred --------------------------------------*/ 436 if (((isrflags & USART_ISR_FE) != 0U) && ((cr3its & USART_CR3_EIE) != 0U)) 437 { 438 __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_FEF); 439 440 huart->ErrorCode |= HAL_UART_ERROR_FE; 441 } 442 443 /* UART noise error interrupt occurred --------------------------------------*/ 444 if (((isrflags & USART_ISR_NE) != 0U) && ((cr3its & USART_CR3_EIE) != 0U)) 445 { 446 __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_NEF); 447 448 huart->ErrorCode |= HAL_UART_ERROR_NE; 449 } 450 451 /* UART Over-Run interrupt occurred -----------------------------------------*/ 452 if (((isrflags & USART_ISR_ORE) != 0U) 453 && (((cr1its & USART_CR1_RXNEIE_RXFNEIE) != 0U) || 454 ((cr3its & (USART_CR3_RXFTIE | USART_CR3_EIE)) != 0U))) 455 { 456 __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF); 457 458 huart->ErrorCode |= HAL_UART_ERROR_ORE; 459 } 460 } /* End if some error occurs */ 461 462 463 /* UART in mode Receiver (receive Timeout occured)--------------------------*/ 464 if (((isrflags & USART_ISR_RTOF) != 0U) 465 && (((cr1its & USART_CR1_RTOIE) != 0U) 466 || ((cr3its & USART_CR2_RTOEN) != 0U))) 467 { 468 __HAL_UART_CLEAR_FLAG(huart, USART_ICR_RTOCF); 469 huart->RxState = HAL_UART_STATE_READY; 470 huart->gState = HAL_UART_STATE_READY; 228 if (huart->ErrorCode == HAL_UART_ERROR_RTO) 229 { 471 230 modbusData.mb_rx_frame_complete = 1; 472 231 modbusData.setRxLed = true; 473 474 } 475 476 /* UART in mode Receiver ---------------------------------------------------*/ 477 if (((isrflags & USART_ISR_RXNE_RXFNE) != 0U) 478 && (((cr1its & USART_CR1_RXNEIE_RXFNEIE) != 0U) 479 || ((cr3its & USART_CR3_RXFTIE) != 0U))) 480 { 481 if ((huart->RxState == HAL_UART_STATE_BUSY_RX) && (modbusData.rx_head < RXBUFFERSIZE)) //-> empfngt dann ein byte aber das ist nicht wild 482 { 483 modbusData.rx_buffer[modbusData.rx_head] = huart->Instance->RDR; 484 /*DEBUG//printf("xx%d: nr:%d %d\n",test ++,modbusData.rx_head, modbusData.rx_buffer[modbusData.rx_head]);*/ 485 modbusData.rx_head++; 486 modbusData.setRxLed = true; 487 } 488 else 489 { 490 __HAL_UART_SEND_REQ(huart, UART_RXDATA_FLUSH_REQUEST); 491 } 492 } 493 494 /* UART in mode Transmitter ------------------------------------------------*/ 495 if (((isrflags & USART_ISR_TXE_TXFNF) != 0U) 496 && (((cr1its & USART_CR1_TXEIE_TXFNFIE) != 0U) 497 || ((cr3its & USART_CR3_TXFTIE) != 0U))) 498 { 499 UART_TxISR_8BIT(modbusData.uart); 500 modbusData.setTxLed = true; 501 } 502 503 /* UART in mode Transmitter (transmission end) -----------------------------*/ 504 if (((isrflags & USART_ISR_TC) != 0U) && ((cr1its & USART_CR1_TCIE) != 0U)) 505 { 506 modbusData.current_query = MB_QUERY_NOTHING; 507 UART_EndTransmit_IT(huart); 508 huart->RxState = HAL_UART_STATE_BUSY_RX; 509 // /*Reset TX complete interrupt flag*/ 510 // __HAL_UART_CLEAR_FLAG(huart, USART_ISR_TC); 511 // /* Disable the UART Transmit Complete Interrupt */ 512 // CLEAR_BIT(huart->Instance->CR1, USART_CR1_TCIE); 513 // /*TX complete callback function*/ 514 // HAL_UART_TxCpltCallback(huart); 515 // /* Tx process is ended, restore huart->gState to Ready */ 516 // huart->gState = HAL_UART_STATE_READY; 517 } 518 519 /* Call UART Error Call back function if need be --------------------------*/ 520 if (huart->ErrorCode != HAL_UART_ERROR_NONE) 521 { 522 huart->RxState = HAL_UART_STATE_BUSY_RX; 523 /* UART in mode Receiver ---------------------------------------------------*/ 524 if (((isrflags & USART_ISR_RXNE_RXFNE) != 0U) 525 && (((cr1its & USART_CR1_RXNEIE_RXFNEIE) != 0U) 526 || ((cr3its & USART_CR3_RXFTIE) != 0U))) 527 { 528 __HAL_UART_SEND_REQ(huart, UART_RXDATA_FLUSH_REQUEST); 529 huart->ErrorCode = HAL_UART_ERROR_NONE; 530 } 531 else if (((isrflags & USART_ISR_TXE_TXFNF) != 0U) 532 && (((cr1its & USART_CR1_TXEIE_TXFNFIE) != 0U) 533 || ((cr3its & USART_CR3_TXFTIE) != 0U))) 534 { 535 modbusData.current_query = MB_QUERY_NOTHING; 536 UART_EndTransmit_IT(huart); 537 huart->RxState = HAL_UART_STATE_BUSY_RX; 538 modbusData.setTxLed = true; 539 } 540 } 232 modbusData.rx_head= 8; 233 printf("MB RTO Event! \n\r"); 234 } 235 236 if(HAL_UARTEx_ReceiveToIdle_DMA(huart, huart->pRxBuffPtr, RXBUFFERSIZE) != HAL_OK) 237 { 238 printf("uart error \n\r"); 239 // while(1) 240 // { 241 // } 242 } 243 541 244 } 542 245 543 544 545 546 547 548 549 /** 550 * @brief TX interrrupt handler for 7 or 8 bits data word length . 551 * @note Function is called under interruption only, once 552 * interruptions have been enabled by HAL_UART_Transmit_IT(). 553 * @param huart UART handle. 554 * @retval None 555 */ 556 static void UART_TxISR_8BIT(UART_HandleTypeDef *huart) 246 void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size) 557 247 { 558 /* Check that a Tx process is ongoing */ 559 if (huart->gState == HAL_UART_STATE_BUSY_TX) 560 { 561 if (huart->TxXferCount == 0U) 562 { 563 /* Disable the UART Transmit Data Register Empty Interrupt */ 564 CLEAR_BIT(huart->Instance->CR1, USART_CR1_TXEIE_TXFNFIE); 565 566 /* Enable the UART Transmit Complete Interrupt */ 567 SET_BIT(huart->Instance->CR1, USART_CR1_TCIE); 568 } 569 else 570 { 571 huart->Instance->TDR = (uint8_t)(*huart->pTxBuffPtr & (uint8_t)0xFF); 572 huart->pTxBuffPtr++; 573 huart->TxXferCount--; 574 } 575 } 576 else 577 { 578 /* Disable the UART Transmit Data Register Empty Interrupt */ 579 CLEAR_BIT(huart->Instance->CR1, USART_CR1_TXEIE_TXFNFIE); 580 581 /* Enable the UART Transmit Complete Interrupt */ 582 SET_BIT(huart->Instance->CR1, USART_CR1_TCIE); 583 } 248 printf("MB rxEvent!RX=%d \n\r",Size); 249 modbusData.setRxLed = true; 250 251 modbusData.mb_rx_frame_complete = 1; 252 modbusData.rx_head= Size +1; 253 254 if(HAL_UARTEx_ReceiveToIdle_DMA(huart, huart->pRxBuffPtr, RXBUFFERSIZE) != HAL_OK) 255 { 256 printf("uart error \n\r"); 257 // while(1) 258 // { 259 // } 260 } 261 262 584 263 } 585 264 … … 595 274 596 275 597 /** 598 * @brief Output Compare callback in non blocking mode 599 * @param htim : TIM OC handle 600 * @retval None 601 */ 602 void mbTimerIsr(modbus_t * mb_data) 603 { 604 /* Capture compare 1 event */ 605 // if(__HAL_TIM_GET_FLAG(mb_data->timer, TIM_FLAG_CC1) != RESET) 606 // { 607 // if(__HAL_TIM_GET_IT_SOURCE(mb_data->timer, TIM_IT_CC1) !=RESET) 608 // { 609 // __HAL_TIM_CLEAR_IT(mb_data->timer, TIM_IT_CC1); 610 // mb_data->timer->Channel = HAL_TIM_ACTIVE_CHANNEL_1; 611 612 mb_data->mb_rx_frame_complete=true; 613 // mb_data->timer->Instance->CNT =0; 614 // HAL_TIM_OC_Stop_IT(mb_data->timer, TIM_CHANNEL_1); 615 // mb_data->timer->Channel = HAL_TIM_ACTIVE_CHANNEL_CLEARED; 616 // 617 // } 618 // } 619 } 276 277 278 279 280 281 282 620 283 621 284 -
trunk/fw_g473rct/SES/src/sysdata.c
r20 r22 49 49 50 50 sys_data.s.values.adc_restarts = 0U; 51 sys_data.s.parameter.test[1] = 1; 52 sys_data.s.parameter.test[2] = 3; 53 sys_data.s.parameter.test[3] = 5; 54 51 55 }
Note: See TracChangeset
for help on using the changeset viewer.