adc.h 3.4 KB

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