adc.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #ifndef _ADC_H__
  2. #define _ADC_H__
  3. #include "bsp/bsp.h"
  4. #include "os/os_types.h"
  5. /*
  6. inserted ADC 由timer0 ch3触发,
  7. 注意:adc所有外部触发都是下降沿触发
  8. */
  9. #define ISQ2_OFFSET 10
  10. #define ISO3_OFFSET 15
  11. #define IL_OFFSET 20
  12. #define ADC_SAMPLE_TIME ADC_SAMPLETIME_7POINT5
  13. #define ADC_TRIGGER_PHASE ADC0_1_EXTTRIG_INSERTED_T0_CH3
  14. #define ADC_TRIGGER_PHASE2 ADC0_1_EXTTRIG_INSERTED_T1_CH0
  15. #define ADC_TRIGGER_NONE ADC0_1_2_EXTTRIG_INSERTED_NONE
  16. #define ADC_TRIGGER_VBUS ADC0_1_EXTTRIG_INSERTED_T1_CH0
  17. #define PHASE_AB 0
  18. #define PHASE_AC 1
  19. #define PHASE_BC 2
  20. //#define ADC_RANK_CHANNEL(c1, c2, l) ((c1)<<ISQ2_OFFSET | (c2)<<ISO3_OFFSET | (l)<<IL_OFFSET)
  21. #define ADC_RANK_CHANNEL(c) ((c)<<ISO3_OFFSET | (0)<<IL_OFFSET)
  22. #define ADC_CALI_RANK_CHANEL(c) ((c)<<ISO3_OFFSET | (0)<<IL_OFFSET)
  23. #ifndef HIGH_SIDE_CURRENT_SENSOR
  24. static u32 adc0_rank_channels[3] = {
  25. ADC_RANK_CHANNEL(U_PHASE_I_CHAN),//0, A, AB
  26. ADC_RANK_CHANNEL(U_PHASE_I_CHAN),//1, A, AC
  27. ADC_RANK_CHANNEL(V_PHASE_I_CHAN),//2, B, BC
  28. };
  29. static u32 adc1_rank_channels[3] = {
  30. ADC_RANK_CHANNEL(V_PHASE_I_CHAN),//0, B
  31. ADC_RANK_CHANNEL(W_PHASE_I_CHAN),//1, C
  32. ADC_RANK_CHANNEL(W_PHASE_I_CHAN),//2, C
  33. };
  34. static u32 volatile * adc_phase_reg1[3] = {
  35. &ADC_IDATA0(ADC0),//0, A
  36. &ADC_IDATA0(ADC0),//1, A
  37. &ADC_IDATA0(ADC0),//2, B
  38. };
  39. static u32 volatile * adc_phase_reg2[3] = {
  40. &ADC_IDATA0(ADC1),//0, B
  41. &ADC_IDATA0(ADC1),//1, C
  42. &ADC_IDATA0(ADC1),//2, C
  43. };
  44. #endif
  45. static void __inline adc_phase_current_read(u8 phases, s32 *v1, s32 *v2) {
  46. #ifdef HIGH_SIDE_CURRENT_SENSOR
  47. *v1 = ADC_IDATA0(ADC0);
  48. *v2 = ADC_IDATA0(ADC1);
  49. #else
  50. *v1 = (s32)(*adc_phase_reg1[phases]) ;
  51. *v2 = (s32)(*adc_phase_reg2[phases]) ;
  52. #endif
  53. }
  54. static void __inline adc_current_sample_config(u8 phases) {
  55. #ifdef HIGH_SIDE_CURRENT_SENSOR
  56. ADC_ISQ(ADC0) = ADC_RANK_CHANNEL(V_PHASE_I_CHAN);
  57. ADC_ISQ(ADC1) = ADC_RANK_CHANNEL(W_PHASE_I_CHAN);
  58. #else
  59. ADC_ISQ(ADC0) = adc0_rank_channels[phases];
  60. ADC_ISQ(ADC1) = adc1_rank_channels[phases];
  61. #endif
  62. }
  63. static void __inline adc_disable_ext_trigger(void) {
  64. ADC_CTL1(ADC0) &= ~ADC_CTL1_ETEIC;
  65. }
  66. static void __inline adc_enable_ext_trigger(void) {
  67. ADC_CTL1(ADC0) |= ADC_CTL1_ETEIC;
  68. }
  69. /* insert len fixed to 2(IL=1), ISQ2 >> ISQ3*/
  70. static __inline__ void adc_update_insert_sample_rank(u32 adc, u8 channel) {
  71. ADC_ISQ(adc) = ADC_RANK_CHANNEL(channel);
  72. }
  73. static __inline__ void adc_update_insert_sample_time(u32 adc, uint8_t adc_channel , uint32_t sample_time)
  74. {
  75. uint32_t sampt;
  76. /* ADC sampling time config */
  77. if(adc_channel < 10U){
  78. sampt = ADC_SAMPT1(adc);
  79. sampt &= ~((u32)(ADC_SAMPTX_SPTN << (3U*adc_channel)));
  80. sampt |= (u32) sample_time << (3U*adc_channel);
  81. ADC_SAMPT1(adc) = sampt;
  82. }else if(adc_channel < 18U){
  83. sampt = ADC_SAMPT0(adc);
  84. sampt &= ~((u32)(ADC_SAMPTX_SPTN << (3U*(adc_channel-10U))));
  85. sampt |= ((u32)sample_time << (3U*(adc_channel-10U)));
  86. ADC_SAMPT0(adc) = sampt;
  87. }
  88. }
  89. static __inline__ bool adc_eoic_interrupt(void)
  90. {
  91. if (ADC_STAT(ADC0) & ADC_STAT_EOIC){
  92. return true;
  93. }
  94. return false;
  95. }
  96. static __inline__ void adc_clear_irq_flags(void) {
  97. ADC_STAT(ADC0) &= ~((u32) ADC_INT_FLAG_EOIC);
  98. ADC_STAT(ADC1) &= ~((u32) ADC_INT_FLAG_EOIC);
  99. }
  100. static __inline void adc_update_ext_trigger(u32 trigger) {
  101. adc_external_trigger_source_config(ADC0, ADC_INSERTED_CHANNEL, trigger);
  102. }
  103. void adc_init(void);
  104. s32 adc_sample_regular_channel(int chan, int times);
  105. void adc_start_convert(void);
  106. void adc_stop_convert(void);
  107. u16 adc_get_vbus(void);
  108. u16 adc_get_acc(void);
  109. u16 adc_get_throttle(void);
  110. void adc_get_uvw_phaseV(u16 *uvw);
  111. u16 adc_get_mos_temp(void);
  112. u16 adc_get_mos_temp2(void);
  113. u16 adc_get_motor_temp(void);
  114. #endif /* _ADC_H__ */