cp_detection.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. #ifndef _CP_DETECTION_H
  2. #define _CP_DETECTION_H
  3. /**
  4. ******************************************************************************
  5. * @file cp_detection.h
  6. * @author Edward Lien
  7. * @brief This file provides Control Pilot Voltage detection with filter algorithm.
  8. * @Version D0.01
  9. * @Date 2019/13/11
  10. ******************************************************************************
  11. */
  12. /* Private includes ----------------------------------------------------------*/
  13. /* USER CODE BEGIN Includes */
  14. #include "cmsis_os.h"
  15. #include "main.h"
  16. /* USER CODE END Includes */
  17. /* Private typedef -----------------------------------------------------------*/
  18. /* USER CODE BEGIN PTD */
  19. typedef struct
  20. {
  21. uint8_t State;
  22. uint32_t PositiveValue;
  23. uint32_t NegativeValue;
  24. uint8_t MaxPTB;
  25. uint16_t MaxPTBCount;
  26. uint8_t MaxNTB;
  27. uint16_t MaxNTBCount;
  28. }sCPModuleResult_t;
  29. typedef struct
  30. {
  31. uint16_t CpP12VHigh;
  32. uint16_t CpP12VLow;
  33. uint16_t CpP9VHigh;
  34. uint16_t CpP9VLow;
  35. uint16_t CpP6VHigh;
  36. uint16_t CpP6VLow;
  37. uint16_t CpP3VHigh;
  38. uint16_t CpP3VLow;
  39. uint16_t Cp0VHigh;
  40. uint16_t Cp0VLow;
  41. uint16_t CpN12VHigh;
  42. uint16_t CpN12VLow;
  43. }sCPVoltageBoundary_t;
  44. typedef struct
  45. {
  46. uint16_t HysteresisP12VHigh;
  47. uint16_t HysteresisP12VLow;
  48. uint16_t HysteresisP9VHigh;
  49. uint16_t HysteresisP9VLow;
  50. uint16_t HysteresisP6VHigh;
  51. uint16_t HysteresisP6VLow;
  52. uint16_t HysteresisP3VHigh;
  53. uint16_t HysteresisP3VLow;
  54. uint16_t Hysteresis0VHigh;
  55. uint16_t Hysteresis0VLow;
  56. uint16_t HysteresisN12VHigh;
  57. uint16_t HysteresisN12VLow;
  58. }sCPVoltageHysteresis_t;
  59. //typedef struct
  60. //{
  61. // uint16_t *TB_P12V;
  62. // uint16_t *TB_P9V;
  63. // uint16_t *TB_P6V;
  64. // uint16_t *TB_P3V;
  65. // uint16_t *TB_0V;
  66. // uint16_t *TB_N12V;
  67. // uint16_t *TB_UD;
  68. //
  69. //}sCPTempBuffer_t;
  70. //******************************************************************
  71. #define FUNC_CP_ADC_MODIFY //Hao@20210113: To avoid hard-coding in cp_detection.h/.c
  72. #ifdef FUNC_CP_ADC_MODIFY
  73. //#define FUNC_CP_ADC_MODIFY_DEBUG //Comment this line to DISABLE debug function
  74. //#define TRACE_CP //Hao@20210120: Test
  75. #define NEW_CP_SPEC
  76. #ifdef NEW_CP_SPEC
  77. #define NEW_CP_SPEC_20210525
  78. #endif //NEW_CP_SPEC
  79. #define FUNC_CP_CLASSIFY_TO_OV_RANGE
  80. #define MODIFY_UNKNOWN_TO_STATE_E
  81. #define MODIFY_STATE_F_HYSTERESIS
  82. #define MODIFY_STATE_UNKNOWN_HYSTERESIS
  83. #ifdef MODIFY_CP_DETECTION_STATE_F
  84. #define MODIFY_UNKNOWN_TO_STATE_F
  85. #endif
  86. /*
  87. _______(Rf)________
  88. | |
  89. | |
  90. (CP) | |\ |
  91. VinN______(RinN)__|_____|-\ |
  92. | \________|____Vout (to MCU ADC pin)
  93. | /
  94. VinP___(RinP)___(VopP)__|+/
  95. | |/
  96. |
  97. (Rg)
  98. |
  99. ////
  100. */
  101. #define CP_ADC_REFVOLT (3.343) //AW
  102. //#define CP_ADC_REFVOLT (3.37) //AX
  103. #define CP_ADC_MIN_COUNT (0)
  104. #define CP_ADC_MAX_COUNT (4096 - 1)
  105. #define CP_ADC_RESOLUTION (CP_ADC_REFVOLT / (CP_ADC_MAX_COUNT - CP_ADC_MIN_COUNT))
  106. #define CP_ADC_OPA_RIN_P (113.0)
  107. #define CP_ADC_OPA_RIN_N (475.0)
  108. #define CP_ADC_OPA_RG (15.0 + 0.287)
  109. #define CP_ADC_OPA_RF (53.6)
  110. #define CP_ADC_OPA_VIN_P (12.0)
  111. #define CP_ADC_OPA_VOP_P ((CP_ADC_OPA_VIN_P * CP_ADC_OPA_RG) / (CP_ADC_OPA_RIN_P + CP_ADC_OPA_RG))
  112. #define CP_ADC_OPA_GAIN (CP_ADC_OPA_RF / CP_ADC_OPA_RIN_N)
  113. #define CP_ADC_OPA_GAIN_RECIPROCAL (CP_ADC_OPA_RIN_N / CP_ADC_OPA_RF)
  114. #define CP_ADC_OPA_CALC_OPA_VOUT_SLOP ((-1) * CP_ADC_OPA_GAIN)
  115. #define CP_ADC_OPA_CALC_OPA_VOUT_OFFS ((1 + CP_ADC_OPA_GAIN) * CP_ADC_OPA_VOP_P)
  116. #define CP_ADC_OPA_CALC_OPA_VIN_SLOP ((-1) * CP_ADC_OPA_GAIN_RECIPROCAL)
  117. #define CP_ADC_OPA_CALC_OPA_VIN_OFFS ((1 + CP_ADC_OPA_GAIN_RECIPROCAL) * CP_ADC_OPA_VOP_P)
  118. #define CP_GET_OPA_VOUT(Vin) (((Vin) * CP_ADC_OPA_CALC_OPA_VOUT_SLOP) + CP_ADC_OPA_CALC_OPA_VOUT_OFFS)
  119. #define CP_GET_OPA_VOUT_ADC(Vin) ((uint32_t)(CP_GET_OPA_VOUT(Vin) / CP_ADC_RESOLUTION))
  120. #define CP_GET_OPA_VOLT_DIFF_ADC(VoltDiff) (CP_GET_OPA_VOUT_ADC(0) - CP_GET_OPA_VOUT_ADC(VoltDiff))
  121. #define CP_GET_OPA_VIN(Vout) (((Vout) * CP_ADC_OPA_CALC_OPA_VIN_SLOP) + CP_ADC_OPA_CALC_OPA_VIN_OFFS)
  122. #define CP_GET_OPA_VIN_USE_ADC(AdcCount) (CP_GET_OPA_VIN(CP_ADC_RESOLUTION * (AdcCount)))
  123. #ifdef FUNC_CP_ADC_MODIFY_DEBUG
  124. void CP_ADC_OPA_PrintParam(void);
  125. void CP_ADC_OPA_TestFormula(void);
  126. void CP_ADC_OPA_TestBoundaryValue(void);
  127. #endif
  128. #ifdef TRACE_CP
  129. void TRACE_CP_Print_CPModuleResult(sCPModuleResult_t *p, uint8_t bOK, long EQ_Sum, long EQ_NG1, long EQ_NG2);
  130. #endif
  131. #endif //FUNC_CP_ADC_MODIFY
  132. //******************************************************************
  133. /* USER CODE END PTD */
  134. /* Private define ------------------------------------------------------------*/
  135. /* USER CODE BEGIN PD */
  136. #define SYSTEM_STATE_UNKNOWN 0x00
  137. #define SYSTEM_STATE_A 0x01
  138. #define SYSTEM_STATE_B 0x02
  139. #define SYSTEM_STATE_C 0x03
  140. #define SYSTEM_STATE_D 0x04
  141. #define SYSTEM_STATE_E 0x05
  142. #define SYSTEM_STATE_F 0x06
  143. #define MAX_CLASSIFY_SECTION 8
  144. #define MAX_SECTION_COUNT 1000
  145. #define MAX_STATE_COUNTER 1
  146. //******************************************************************
  147. #ifdef FUNC_CP_ADC_MODIFY
  148. #ifdef NEW_CP_SPEC
  149. extern void CpDetectModuleInitialize(char SafetyRegulationCode);
  150. #endif //NEW_CP_SPEC
  151. #endif //FUNC_CP_ADC_MODIFY
  152. extern void CpCalculate(uint32_t * adc_value, uint16_t sample_times, sCPModuleResult_t * result);
  153. /* USER CODE END PD */
  154. /* Private macro -------------------------------------------------------------*/
  155. /* USER CODE BEGIN PM */
  156. /* USER CODE END PM */
  157. /* Private variables ---------------------------------------------------------*/
  158. /* USER CODE BEGIN PV */
  159. /* USER CODE END PV */
  160. /* Private function prototypes -----------------------------------------------*/
  161. /* USER CODE BEGIN PFP */
  162. /* USER CODE END PFP */
  163. /* Private user code ---------------------------------------------------------*/
  164. /* USER CODE BEGIN 0 */
  165. /* USER CODE END 0 */
  166. #endif //_CP_DETECTION_H