adc.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #ifndef _ADC_H__
  2. #define _ADC_H__
  3. #include "bsp/bsp.h"
  4. #include "os/os_type.h"
  5. /*
  6. inserted ADC 由timer0 ch3触发,
  7. 注意:adc所有外部触发都是下降沿触发
  8. */
  9. #define MOTOR_TEMP_CHAN ADC_CHANNEL_0
  10. #define HANDLERBAR_CHAN ADC_CHANNEL_1 //转把信号
  11. #define VBUS_V_CHAN ADC_CHANNEL_2
  12. #define W_PHASE_V_CHAN ADC_CHANNEL_3
  13. #define V_PHASE_V_CHAN ADC_CHANNEL_4
  14. #define U_PHASE_V_CHAN ADC_CHANNEL_5
  15. #define W_PHASE_I_CHAN ADC_CHANNEL_6
  16. #define V_PHASE_I_CHAN ADC_CHANNEL_7
  17. #define U_PHASE_I_CHAN ADC_CHANNEL_8
  18. #define VBUS_I_CHAN ADC_CHANNEL_9
  19. #define ISQ2_OFFSET 10
  20. #define ISO3_OFFSET 15
  21. #define IL_OFFSET 20
  22. #define ADC_SAMPLE_TIME ADC_SAMPLETIME_7POINT5
  23. #define ADC_TRIGGER_PHASE ADC0_1_EXTTRIG_INSERTED_T0_CH3
  24. #define ADC_TRIGGER_PHASE2 ADC0_1_EXTTRIG_INSERTED_T1_CH0
  25. #define ADC_TRIGGER_NONE ADC0_1_2_EXTTRIG_INSERTED_NONE
  26. #define ADC_TRIGGER_VBUS ADC0_1_EXTTRIG_INSERTED_T1_CH0
  27. //#define ADC_RANK_CHANNEL(c1, c2, l) ((c1)<<ISQ2_OFFSET | (c2)<<ISO3_OFFSET | (l)<<IL_OFFSET)
  28. #define ADC_RANK_CHANNEL(c) ((c)<<ISO3_OFFSET | (0)<<IL_OFFSET)
  29. #define ADC_CALI_RANK_CHANEL(c) ((c)<<ISO3_OFFSET | (0)<<IL_OFFSET)
  30. static u32 adc0_rank_channels[6] = {
  31. ADC_RANK_CHANNEL(V_PHASE_I_CHAN),//1, B, BC
  32. ADC_RANK_CHANNEL(U_PHASE_I_CHAN),//2, A, AC
  33. ADC_RANK_CHANNEL(W_PHASE_I_CHAN),//3, C, CA
  34. ADC_RANK_CHANNEL(V_PHASE_I_CHAN),//4, B, BA
  35. ADC_RANK_CHANNEL(U_PHASE_I_CHAN),//5, A, AB
  36. ADC_RANK_CHANNEL(W_PHASE_I_CHAN),//6, C, CB
  37. };
  38. static u32 adc1_rank_channels[6] = {
  39. ADC_RANK_CHANNEL(W_PHASE_I_CHAN),//1, C
  40. ADC_RANK_CHANNEL(W_PHASE_I_CHAN),//2, C
  41. ADC_RANK_CHANNEL(U_PHASE_I_CHAN),//3, A
  42. ADC_RANK_CHANNEL(U_PHASE_I_CHAN),//4, A
  43. ADC_RANK_CHANNEL(V_PHASE_I_CHAN),//5, B
  44. ADC_RANK_CHANNEL(V_PHASE_I_CHAN),//5, B
  45. };
  46. static u32 volatile * adc_phase_reg1[6] = {
  47. &ADC_IDATA0(ADC0),//1, B
  48. &ADC_IDATA0(ADC0),//2, A
  49. &ADC_IDATA0(ADC1),//3, A
  50. &ADC_IDATA0(ADC0),//4, B
  51. &ADC_IDATA0(ADC1),//5, B
  52. &ADC_IDATA0(ADC1),//6, B
  53. };
  54. static u32 volatile * adc_phase_reg2[6] = {
  55. &ADC_IDATA0(ADC1),//1, C
  56. &ADC_IDATA0(ADC1),//2, C
  57. &ADC_IDATA0(ADC0),//3, C
  58. &ADC_IDATA0(ADC1),//4, A
  59. &ADC_IDATA0(ADC0),//5, A
  60. &ADC_IDATA0(ADC0),//6, C
  61. };
  62. static void __inline adc_phase_current_read(u8 sector, s32 *v1, s32 *v2) {
  63. #if SHUNT_NUM==THREE_SHUNTS_SAMPLE
  64. *v1 = (s32)(*adc_phase_reg1[sector]) ;
  65. *v2 = (s32)(*adc_phase_reg2[sector]) ;
  66. #else
  67. *v1 = (s32) ADC_IDATA0(ADC0);
  68. *v2 = (s32) ADC_IDATA0(ADC1);
  69. #endif
  70. }
  71. static void __inline adc_current_sample_config(u8 sector) {
  72. #if SHUNT_NUM==THREE_SHUNTS_SAMPLE
  73. ADC_ISQ(ADC0) = adc0_rank_channels[sector];
  74. ADC_ISQ(ADC1) = adc1_rank_channels[sector];
  75. #endif
  76. }
  77. static void __inline adc_disable_ext_trigger(void) {
  78. ADC_CTL1(ADC0) &= ~ADC_CTL1_ETEIC;
  79. #if SHUNT_NUM==ONE_SHUNT_SAMPLE
  80. ADC_CTL1(ADC1) &= ~ADC_CTL1_ETEIC;
  81. #endif
  82. }
  83. static void __inline adc_enable_ext_trigger(void) {
  84. ADC_CTL1(ADC0) |= ADC_CTL1_ETEIC;
  85. #if SHUNT_NUM==ONE_SHUNT_SAMPLE
  86. ADC_CTL1(ADC1) |= ADC_CTL1_ETEIC;
  87. #endif
  88. }
  89. /* insert len fixed to 2(IL=1), ISQ2 >> ISQ3*/
  90. static __inline__ void adc_update_insert_sample_rank(u32 adc, u8 channel) {
  91. ADC_ISQ(adc) = ADC_RANK_CHANNEL(channel);
  92. }
  93. static __inline__ void adc_update_insert_sample_time(u32 adc, uint8_t adc_channel , uint32_t sample_time)
  94. {
  95. uint32_t sampt;
  96. /* ADC sampling time config */
  97. if(adc_channel < 10U){
  98. sampt = ADC_SAMPT1(adc);
  99. sampt &= ~((u32)(ADC_SAMPTX_SPTN << (3U*adc_channel)));
  100. sampt |= (u32) sample_time << (3U*adc_channel);
  101. ADC_SAMPT1(adc) = sampt;
  102. }else if(adc_channel < 18U){
  103. sampt = ADC_SAMPT0(adc);
  104. sampt &= ~((u32)(ADC_SAMPTX_SPTN << (3U*(adc_channel-10U))));
  105. sampt |= ((u32)sample_time << (3U*(adc_channel-10U)));
  106. ADC_SAMPT0(adc) = sampt;
  107. }
  108. }
  109. static __inline__ bool adc_eoic_interrupt(void)
  110. {
  111. #if SHUNT_NUM==THREE_SHUNTS_SAMPLE
  112. if (ADC_STAT(ADC0) & ADC_STAT_EOIC){
  113. return true;
  114. }
  115. #endif
  116. #if SHUNT_NUM==ONE_SHUNT_SAMPLE
  117. if (ADC_STAT(ADC1) & ADC_STAT_EOIC){
  118. return true;
  119. }
  120. #endif
  121. return false;
  122. }
  123. static __inline__ void adc_clear_irq_flags(void) {
  124. #if SHUNT_NUM==THREE_SHUNTS_SAMPLE
  125. ADC_STAT(ADC0) &= ~((u32) ADC_INT_FLAG_EOIC);
  126. #else
  127. ADC_STAT(ADC0) &= ~((u32) ADC_INT_FLAG_EOIC);
  128. ADC_STAT(ADC1) &= ~((u32) ADC_INT_FLAG_EOIC);
  129. #endif
  130. }
  131. static __inline void adc_update_ext_trigger(u32 trigger) {
  132. adc_external_trigger_source_config(ADC0, ADC_INSERTED_CHANNEL, trigger);
  133. }
  134. void adc_init(void);
  135. s32 adc_sample_regular_channel(int chan, int times);
  136. void adc_start_insert_convert(void);
  137. void adc_stop_insert_convert(void);
  138. #endif /* _ADC_H__ */