main.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. //##############################################################################
  2. //
  3. // BMS_Master - Battery Management System Master
  4. //
  5. // Firmware for
  6. // - Collecting and Checking Data of BMS_Slave and BMS_UI
  7. // - Flow Control for START, STOP, LOAD and DRIVE
  8. // - SOC/SOH computing
  9. // - Balancer computing
  10. // - Error Check
  11. // - Emergency Shut Off
  12. //
  13. //
  14. // CENTRAL - FILE
  15. //
  16. // - void Main( void )
  17. //
  18. //
  19. //##############################################################################
  20. //
  21. // Hardware Revision: V1
  22. //
  23. // uController: Freescale MPC5646C
  24. //
  25. // Autor: M. Maurer, V. Reiling, KIT Campus Nord, IPE
  26. //
  27. // Date: April 2012
  28. //
  29. // Built with: CodeWarrior Development Studio 10.4 ( Build Id: 130425 )
  30. //
  31. //##############################################################################
  32. //==============================================================================
  33. // History:
  34. //==============================================================================
  35. // Datum: | Name | Version:| Cause: | rev.:
  36. //------------------------------------------------------------------------------
  37. // 04.05.15 | VR | 1.5 | Change for BaSyTec | 005
  38. //------------------------------------------------------------------------------
  39. // 18.11.14 | VR | 1.4 | upgrade 2 CodeWarrior 10.6 id: 140329 | 004
  40. //------------------------------------------------------------------------------
  41. // 18.03.14 | VR | 1.3 | Error Handling changed, Derating added | 003
  42. //------------------------------------------------------------------------------
  43. // 07.02.14 | VR | 1.2 | upgrade 2 CodeWarrior 10.5 id: 130916 | 002
  44. //------------------------------------------------------------------------------
  45. // 05.07.13 | VR | 1.1 | BMS Master Project | 001
  46. //------------------------------------------------------------------------------
  47. // 20.04.12 | TM | 1.0 | Hardware Test Code & Drivers | 000
  48. //==============================================================================
  49. #include "BMS_Master.h"
  50. uint8_t const ucMainVersion = 0x1;
  51. uint8_t const ucSubVersion = 0x5;
  52. // ***** Global Variables *******************************************************************
  53. BSD_t gBSD; // Global Battery Management System Slave Data
  54. BSE_t gBSE; // Global Battery Management System Slave Errors
  55. uint8_t gMaxDischargeCurrent = 60; // Maximum Discharge Current = 120A / 2A per Bit
  56. uint64_t Global_1msCounter = 0; // Million Years till Overrun
  57. uint32_t gEvent; // Control of Main FSM
  58. uint32_t gErrorEvent; // Control of Error FSM
  59. uint32_t gISO_R = 0; // Resistance of Isolation
  60. uint8_t gCANErrorInsert = 0; // Automatic Test over CAN
  61. MASTER_CAN0_STRUCT_t gDUMMY_data_struct;
  62. void main(void)
  63. {
  64. /**
  65. * @brief Main Function of BMS Master
  66. */
  67. volatile uint16_t Vbat = 0;
  68. volatile uint16_t StartTryCounter = 0;
  69. volatile uint32_t DelayCount = 0;
  70. volatile uint32_t NextEvent = 0;
  71. volatile uint32_t CounterCheckVoltageMSG = 12000;
  72. volatile uint32_t CounterCheckStatusUIMSG = 12000;
  73. volatile uint32_t CounterCheckTemperatureMSG = 12000;
  74. volatile uint32_t Counter_20ms = 5000;
  75. volatile uint32_t Counter_50ms = 5000;
  76. volatile uint32_t Counter_100ms = 5000;
  77. volatile uint16_t Copy_r_Voltage = 200;
  78. volatile uint16_t Copy_r_Status = 200;
  79. volatile uint16_t Copy_r_Temperature = 200;
  80. volatile uint16_t Copy_r_UI = 200;
  81. volatile uint32_t ErrorTimeCount = 0;
  82. static float SoC_test;
  83. static uint64_t test_timestamp = 0;
  84. static uint8_t test_data_send[4] = {1,2,3,4};
  85. static MASTER_CAN0_STRUCT_t CAN0_fsm_state;
  86. static MASTER_CAN1_STRUCT_t CAN1_fsm_state;
  87. static BMS_MASTER_OPERATION_t operation_Fsm;
  88. static uint8_t active_states[2]= {4,15} ;
  89. static BMS_SLAVE_CONFIGURATION_t cellConfig[CAN0_MAX_NR_OF_SLAVES-1];
  90. static BMS_UI_CONFIGURATION_t uiConfig;
  91. static uint8_t fram_test[8] ={1,2,3,4,5,6,7,8};
  92. static uint8_t fram_rx_text[8] ={0,0,0,0,0,0,0,0};
  93. int32_t SOC = 0; // State of Charge for DICO in 0.4%
  94. uint32_t BalanceOn = 1; // Send empty Balancing
  95. uint32_t i = 0;
  96. uint32_t j = 0;
  97. uint32_t test = 0;
  98. uint16_t U12, U33; //!! Test der Betriebsspannung
  99. //-------------------------------------------------------------------------------------------
  100. init_Device(); // Device.h
  101. BoardPeriph_init(); // BoardPeripherals.h
  102. CAN0_init(); // CAN Master to Slave
  103. CAN1_init(); // CAN Master to INVERTER
  104. CAN1_init_tx_structure(&(CAN0_fsm_state.inverter.txStruct));
  105. //CAN2_init(); // CAN Master to DICO
  106. MultitaskOS_init(); // MultitaskOS.h
  107. enableIrq(); // Device.h
  108. BoardPeriph_test_init(); // BoardPeripherals.h
  109. gEvent = EV__INITIALISE; // Start Main FSM
  110. gErrorEvent = EEV__NO_ERROR; // Clear Error FSM
  111. SwitchRelais( PWR_SUPPLY, 0) ;// Activate PWR Supply by setting pin to low
  112. //CAN_Tx_BMU_DICO( );
  113. gen_data_initSlave(&gDUMMY_data_struct, 3600, 25, 36000, 1000);
  114. initCellConfig(cellConfig);
  115. initUiConfig(&uiConfig);
  116. CAN0_init_telegrams(&TelegramTxContainer[0],CAN0_MAX_NR_OF_SLAVES);
  117. init_master_CAN0_fsm(&CAN0_fsm_state,cellConfig,&uiConfig);
  118. Master_CAN1_fsm_init(&CAN1_fsm_state);
  119. BMS_Can_ID_init_init_startupConfig (&CAN0_fsm_state);
  120. //init_master_operation_fsm(&operation_Fsm);
  121. // read fram start State
  122. while(Global_1msCounter < DELAY_START)
  123. {
  124. }
  125. while(BMS_Can_ID_init_fsm (&CAN0_fsm_state) == BMS_CAN_ID_INIT_RETURN_RUNNING ) {
  126. // wait
  127. }
  128. //write_fram_clear_startup_state(&CAN0_fsm_state);
  129. write_fram_clear_error_buffer(&CAN0_fsm_state);
  130. //write_fram_set_SoC(0.03);
  131. while(Global_1msCounter < DELAY_START*2)
  132. {
  133. }
  134. CheckSupplyVoltages( &U12, &U33 ); //!! Test der Betriebsspannung
  135. read_fram_get_startup_state(&CAN0_fsm_state);
  136. read_fram_read_error_buffer(&CAN0_fsm_state);
  137. SoC_test= read_fram_get_SoC();
  138. test_timestamp=Global_1msCounter ;
  139. while(1) {
  140. Master_CAN0_fsm(&CAN0_fsm_state,Global_1msCounter);
  141. //Master_operation_fsm(&CAN0_fsm_state,Global_1msCounter,&operation_Fsm);
  142. //Master_CAN1_fsm(&CAN0_fsm_state,&CAN1_fsm_state,Global_1msCounter);
  143. //if(Global_1msCounter > 20000 + test_timestamp) {
  144. // SwitchRelais( PWR_SUPPLY, 1);
  145. //}
  146. }
  147. }
  148. // ***** End Main.c *************************************************************************