adc.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #ifndef _ADC_H__
  2. #define _ADC_H__
  3. #include "bsp/bsp.h"
  4. #include "os/os_types.h"
  5. float adc_vref_compesion(void);
  6. float adc_5vref_compesion(void);
  7. /*
  8. inserted ADC 由timer0 ch3触发,
  9. 注意:adc所有外部触发都是下降沿触发
  10. */
  11. #define ISQ0_OFFSET 0
  12. #define ISQ1_OFFSET 5
  13. #define ISQ2_OFFSET 10
  14. #define ISQ3_OFFSET 15
  15. #define IL_OFFSET 20
  16. #define ADC_REGCHAN_SAMPLE_TIME ADC_SAMPLETIME_71_5
  17. #define ADC_INSERT_SAMPLE_TIME ADC_SAMPLETIME_1_5
  18. #define ADC_TRIGGER_PHASE ADC12_PREEMPT_TRIG_TMR1CH4
  19. static void __inline adc_phase_current_read(u8 phases, s32 *v1, s32 *v2) {
  20. *v1 = (s32)((float)(ADC1->pdt1_bit.pdt1) * adc_5vref_compesion());
  21. *v2 = (s32)((float)(ADC2->pdt1_bit.pdt1) * adc_5vref_compesion());
  22. }
  23. static void __inline adc_current_sample_config(u8 phases) {
  24. if (phases == PHASE_AB) {
  25. ADC1->psq_bit.psn4 = U_PHASE_I_CHAN;
  26. ADC2->psq_bit.psn4 = V_PHASE_I_CHAN;
  27. }else if (phases == PHASE_AC) {
  28. ADC1->psq_bit.psn4 = U_PHASE_I_CHAN;
  29. ADC2->psq_bit.psn4 = W_PHASE_I_CHAN;
  30. }else {
  31. ADC1->psq_bit.psn4 = V_PHASE_I_CHAN;
  32. ADC2->psq_bit.psn4 = W_PHASE_I_CHAN;
  33. }
  34. }
  35. static void __inline adc_disable_ext_trigger(void) {
  36. ADC1->ctrl2_bit.pcten = FALSE;
  37. ADC2->ctrl2_bit.pcten = FALSE;
  38. }
  39. static void __inline adc_enable_ext_trigger(void) {
  40. ADC1->ctrl2_bit.pcten = TRUE;
  41. ADC2->ctrl2_bit.pcten = TRUE;
  42. }
  43. static __inline__ void adc_clear_irq_flags(void) {
  44. /* clear the ADC flag */
  45. adc_flag_clear(ADC1, ADC_PCCE_FLAG);
  46. adc_flag_clear(ADC2, ADC_PCCE_FLAG);
  47. }
  48. #define adc_update_ext_trigger(trigger) \
  49. adc_preempt_conversion_trigger_set(ADC1, trigger, TRUE)
  50. void adc_init(bool mot_ind);
  51. void adc_start_convert(void);
  52. void adc_stop_convert(void);
  53. u16 adc_get_vbus(void);
  54. u16 adc_get_acc(void);
  55. u16 adc_get_throttle(void);
  56. void adc_get_uvw_phaseV(u16 *uvw);
  57. u16 adc_get_mos_temp(void);
  58. u16 adc_get_motor_temp(void);
  59. u16 adc_get_ibus(void);
  60. u16 adc_get_vref(void);
  61. void adc_set_vref_calc(float v);
  62. void adc_vref_filter(void);
  63. u16 adc_get_5v_ref(void);
  64. void adc_set_5vref_calc(float v);
  65. u16 adc_get_throttle2(void);
  66. u16 adc_get_thro_5v(void);
  67. u16 adc_get_thro2_5v(void);
  68. #endif /* _ADC_H__ */