adc.h 3.3 KB

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