adc.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 ADC_REGCHAN_SAMPLE_TIME ADC_SAMPLETIME_7POINT5
  18. #define PHASE_AB 0
  19. #define PHASE_AC 1
  20. #define PHASE_BC 2
  21. //#define ADC_RANK_CHANNEL(c1, c2, l) ((c1)<<ISQ2_OFFSET | (c2)<<ISO3_OFFSET | (l)<<IL_OFFSET)
  22. #define ADC_RANK_CHANNEL(c) ((c)<<ISO3_OFFSET | (0)<<IL_OFFSET)
  23. #define ADC_CALI_RANK_CHANEL(c) ((c)<<ISO3_OFFSET | (0)<<IL_OFFSET)
  24. #ifndef HIGH_SIDE_CURRENT_SENSOR
  25. static u32 adc0_rank_channels[3] = {
  26. ADC_RANK_CHANNEL(U_PHASE_I_CHAN),//0, A, AB
  27. ADC_RANK_CHANNEL(U_PHASE_I_CHAN),//1, A, AC
  28. ADC_RANK_CHANNEL(V_PHASE_I_CHAN),//2, B, BC
  29. };
  30. static u32 adc1_rank_channels[3] = {
  31. ADC_RANK_CHANNEL(V_PHASE_I_CHAN),//0, B
  32. ADC_RANK_CHANNEL(W_PHASE_I_CHAN),//1, C
  33. ADC_RANK_CHANNEL(W_PHASE_I_CHAN),//2, C
  34. };
  35. static u32 volatile * adc_phase_reg1[3] = {
  36. &ADC_IDATA0(ADC0),//0, A
  37. &ADC_IDATA0(ADC0),//1, A
  38. &ADC_IDATA0(ADC0),//2, B
  39. };
  40. static u32 volatile * adc_phase_reg2[3] = {
  41. &ADC_IDATA0(ADC1),//0, B
  42. &ADC_IDATA0(ADC1),//1, C
  43. &ADC_IDATA0(ADC1),//2, C
  44. };
  45. #endif
  46. static void __inline adc_phase_current_read(u8 phases, s32 *v1, s32 *v2) {
  47. #ifdef HIGH_SIDE_CURRENT_SENSOR
  48. *v1 = ADC_IDATA0(ADC0);
  49. *v2 = ADC_IDATA0(ADC1);
  50. #else
  51. *v1 = (s32)(*adc_phase_reg1[phases]) ;
  52. *v2 = (s32)(*adc_phase_reg2[phases]) ;
  53. #endif
  54. }
  55. static void __inline adc_current_sample_config(u8 phases) {
  56. #ifdef HIGH_SIDE_CURRENT_SENSOR
  57. ADC_ISQ(ADC0) = ADC_RANK_CHANNEL(V_PHASE_I_CHAN);
  58. ADC_ISQ(ADC1) = ADC_RANK_CHANNEL(W_PHASE_I_CHAN);
  59. #else
  60. ADC_ISQ(ADC0) = adc0_rank_channels[phases];
  61. ADC_ISQ(ADC1) = adc1_rank_channels[phases];
  62. #endif
  63. }
  64. static void __inline adc_disable_ext_trigger(void) {
  65. ADC_CTL1(ADC0) &= ~ADC_CTL1_ETEIC;
  66. }
  67. static void __inline adc_enable_ext_trigger(void) {
  68. ADC_CTL1(ADC0) |= ADC_CTL1_ETEIC;
  69. }
  70. /* insert len fixed to 2(IL=1), ISQ2 >> ISQ3*/
  71. static __inline__ void adc_update_insert_sample_rank(u32 adc, u8 channel) {
  72. ADC_ISQ(adc) = ADC_RANK_CHANNEL(channel);
  73. }
  74. static __inline__ void adc_update_insert_sample_time(u32 adc, uint8_t adc_channel , uint32_t sample_time)
  75. {
  76. uint32_t sampt;
  77. /* ADC sampling time config */
  78. if(adc_channel < 10U){
  79. sampt = ADC_SAMPT1(adc);
  80. sampt &= ~((u32)(ADC_SAMPTX_SPTN << (3U*adc_channel)));
  81. sampt |= (u32) sample_time << (3U*adc_channel);
  82. ADC_SAMPT1(adc) = sampt;
  83. }else if(adc_channel < 18U){
  84. sampt = ADC_SAMPT0(adc);
  85. sampt &= ~((u32)(ADC_SAMPTX_SPTN << (3U*(adc_channel-10U))));
  86. sampt |= ((u32)sample_time << (3U*(adc_channel-10U)));
  87. ADC_SAMPT0(adc) = sampt;
  88. }
  89. }
  90. static __inline__ bool adc_eoic_interrupt(void)
  91. {
  92. if (ADC_STAT(ADC0) & ADC_STAT_EOIC){
  93. return true;
  94. }
  95. return false;
  96. }
  97. static __inline__ void adc_clear_irq_flags(void) {
  98. ADC_STAT(ADC0) &= ~((u32) ADC_INT_FLAG_EOIC);
  99. ADC_STAT(ADC1) &= ~((u32) ADC_INT_FLAG_EOIC);
  100. }
  101. static __inline void adc_update_ext_trigger(u32 trigger) {
  102. adc_external_trigger_source_config(ADC0, ADC_INSERTED_CHANNEL, trigger);
  103. }
  104. void adc_init(void);
  105. s32 adc_sample_regular_channel(int chan, int times);
  106. void adc_start_convert(void);
  107. void adc_stop_convert(void);
  108. u16 adc_get_vbus(void);
  109. u16 adc_get_throttle(void);
  110. void adc_get_uvw_phases(u16 *u, u16 *v, u16 *w);
  111. #endif /* _ADC_H__ */