adc.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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_INJ_SAMPLE_TIME ADC_SAMP_TIME_7CYCLES5
  17. #define ADC_REGCHAN_SAMPLE_TIME ADC_SAMP_TIME_239CYCLES5
  18. #define ADC_TRIGGER_PHASE ADC_EXT_TRIG_INJ_CONV_T1_CC4
  19. #define PHASE_AB 0
  20. #define PHASE_AC 1
  21. #define PHASE_BC 2
  22. #define JDAT_OFFSET ((uint8_t)0x28)
  23. #define CTRL2_INJ_EXT_TRIG_SET ((uint32_t)0x00008000)
  24. #define CTRL2_INJ_EXT_TRIG_RESET ((uint32_t)0xFFFF7FFF)
  25. static u16 __inline ADC_GetInjectedDat(ADC_Module* ADCx, uint8_t ADC_InjectedChannel)
  26. {
  27. __IO uint32_t tmp = 0;
  28. tmp = (uint32_t)ADCx;
  29. tmp += ADC_InjectedChannel + JDAT_OFFSET;
  30. /* Returns the selected injected channel conversion data value */
  31. return (uint16_t)(*(__IO uint32_t*)tmp);
  32. }
  33. static void __inline adc_phase_current_read(u8 phases, s32 *v1, s32 *v2) {
  34. *v1 = (s32)((float)ADC_GetInjectedDat(ADC1, ADC_INJ_CH_1) * adc_5vref_compesion());
  35. *v2 = (s32)((float)ADC_GetInjectedDat(ADC2, ADC_INJ_CH_1) * adc_5vref_compesion());
  36. }
  37. static void __inline adc_current_sample_config(u8 phases) {
  38. }
  39. static void __inline adc_disable_ext_trigger(void) {
  40. ADC1->CTRL2 &= CTRL2_INJ_EXT_TRIG_RESET;
  41. }
  42. static void __inline adc_enable_ext_trigger(void) {
  43. ADC1->CTRL2 |= CTRL2_INJ_EXT_TRIG_SET;
  44. }
  45. static __inline__ void adc_clear_irq_flags(void) {
  46. ADC1->STS = (~(uint32_t)ADC_FLAG_JENDC);
  47. ADC2->STS = (~(uint32_t)ADC_FLAG_JENDC);
  48. }
  49. static __inline void adc_update_ext_trigger(u32 trigger) {
  50. ADC1->CTRL2 |= trigger;
  51. }
  52. void adc_init(void);
  53. s32 adc_sample_regular_channel(int chan, int times);
  54. void adc_start_convert(void);
  55. void adc_stop_convert(void);
  56. u16 adc_get_vbus(void);
  57. u16 adc_get_acc(void);
  58. u16 adc_get_throttle(void);
  59. void adc_get_uvw_phaseV(u16 *uvw);
  60. u16 adc_get_mos_temp(void);
  61. u16 adc_get_motor_temp(void);
  62. u16 adc_get_ibus(void);
  63. u16 adc_get_vref(void);
  64. void adc_set_vref_calc(float v);
  65. void adc_vref_filter(void);
  66. u16 adc_get_5v_ref(void);
  67. void adc_set_5vref_calc(float v);
  68. u16 adc_get_throttle2(void);
  69. u16 adc_get_thro_5v(void);
  70. u16 adc_get_thro2_5v(void);
  71. #endif /* _ADC_H__ */