MultitaskOS.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. //##############################################################################
  2. //
  3. // FILE: MultitaskOS.c
  4. //
  5. // TITLE: Real Time 1ms Generator
  6. //
  7. // - void MultitaskOS_init ( void )
  8. // - void ISR_Timer_OS ( void )
  9. //
  10. //##############################################################################
  11. //
  12. //==============================================================================
  13. // Change History:
  14. //==============================================================================
  15. // Datum: | Name | Version:| Change / Cause: | No
  16. //------------------------------------------------------------------------------
  17. // | | | | 002
  18. //------------------------------------------------------------------------------
  19. // 05.07.13 | VR | 1.1 | Change for ReilingOS | 001
  20. //------------------------------------------------------------------------------
  21. // 02.05.12 | TM | 1.0 | New Build | 000
  22. //==============================================================================
  23. //==============================================================================
  24. // Comment Change / Cause:
  25. //==============================================================================
  26. // Change: 003 // 003
  27. //----------------------
  28. //
  29. //
  30. //==============================================================================
  31. // Change: 002 // 002
  32. //----------------------
  33. //
  34. //
  35. //==============================================================================
  36. // Change: 001 // 001
  37. //----------------------
  38. //
  39. //
  40. //==============================================================================
  41. #include "BMS_Master.h"
  42. volatile uint32_t ISO_Start = 0;
  43. volatile uint32_t ISO_PWM = 0;
  44. volatile uint16_t ISO_PIN = 0;
  45. // ***** MultitaskOS_init ******************************************************
  46. void MultitaskOS_init( void )
  47. {
  48. PIT.PITMCR.B.MDIS = 0; // Enable Periodic Interrupt Timer
  49. PIT.PITMCR.B.MDIS_RTI = 0; // Enable Real Time Interrupt
  50. PIT.PITMCR.B.FRZ = 1; // Freeze in Debug Mode
  51. PIT.CH[0].LDVAL.R = SYSCLK_KHZ; // Timeout Period 1ms 64MHz/1kHz
  52. PIT.CH[0].TFLG.B.TIF = 1; // Timer Interrupt Request Clear
  53. PIT.CH[0].TCTRL.B.TIE = 1; // Timer Interrupt Enable
  54. PIT.CH[0].TCTRL.B.TEN = 1; // Timer Active
  55. }
  56. // ***** ISR_Timer_OS **********************************************************
  57. void ISR_Timer_OS( void )
  58. {
  59. uint32_t ISO_CycleTime;
  60. asm(" wrteei 0"); // disable Interrupts
  61. PIT.CH[0].TFLG.B.TIF = 1; // CLear PIT 1 flag by writing 1
  62. Global_1msCounter++;
  63. //''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  64. if( Global_1msCounter > 12000 )
  65. {
  66. if( !READ_INPIN(PIN_REGNR_OKHS) ) // if General Error: Off, Undervoltage, Grounderror, Uniterror, Underrange
  67. {
  68. gISO_R = 0; // show General Error
  69. gBSE.ErrorBMS |= ERROR_ISO_GENERAL;
  70. gErrorEvent = EEV__SHUT_DOWN_EMERGENCY; // and load rejection
  71. }
  72. else
  73. if( READ_INPIN(PIN_REGNR_MHS) != ISO_PIN ) // PWM Signal changed ?
  74. {
  75. ISO_PIN = ISO_PIN?0:1; // new PWM Signal store
  76. if( ISO_PIN ) // new Cycle ?
  77. {
  78. ISO_CycleTime = Global_1msCounter - ISO_Start; // Cycle Time compute
  79. ISO_Start = Global_1msCounter; // new Initial Value store
  80. if( ISO_CycleTime < 35) // 30 Hz ? >>Quickstart<<
  81. {
  82. if( ISO_CycleTime > 31 )
  83. {
  84. gISO_R = 120000; // ISO OK?
  85. if(ISO_PWM > 4) // > 10%
  86. {
  87. gISO_R = 0; // ISO NOK
  88. gBSE.ErrorBMS |= ERROR_ISO_BAD;
  89. gErrorEvent = EEV__SHUT_DOWN_EMERGENCY; // load rejection
  90. }
  91. }
  92. }
  93. else
  94. if( ISO_CycleTime < 102) // 10 Hz ? >>regular Operation<<
  95. {
  96. if( ISO_CycleTime > 98 )
  97. {
  98. if(ISO_PWM < 6) // yes, Duty < 6 % => Overflow
  99. gISO_R = 120000; // yes, show Overflow
  100. else
  101. gISO_R = ( 90 * 1200 ) / ( ISO_PWM - 5 ) - 1200;
  102. if(gCANErrorInsert == 14) //!! 1.5-3
  103. gISO_R = MIN_ISO_R_TEST;
  104. if( gISO_R < MIN_ISO_R) // ISO-R low
  105. {
  106. gBSE.ErrorBMS |= ERROR_ISO_LOW;
  107. gErrorEvent = EEV__SHUT_DOWN_EMERGENCY; // load rejection
  108. }
  109. }
  110. }
  111. }
  112. else // Duty End
  113. ISO_PWM = Global_1msCounter - ISO_Start; // Duty store
  114. }
  115. }
  116. //''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  117. asm(" wrteei 1"); // enable Interrupts
  118. }
  119. // ***** End MultitaskOS.c *****************************************************