pwm.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #ifndef _PWM_H__
  2. #define _PWM_H__
  3. #include "bsp/bsp.h"
  4. #include "os/os_types.h"
  5. #define TIMER_CHCTL2_CH0EN BIT(0) /*!< channel 0 capture/compare function enable */
  6. #define TIMER_CHCTL2_CH0P BIT(1) /*!< channel 0 capture/compare function polarity */
  7. #define TIMER_CHCTL2_CH0NEN BIT(2) /*!< channel 0 complementary output enable */
  8. #define TIMER_CHCTL2_CH0NP BIT(3) /*!< channel 0 complementary output polarity */
  9. #define TIMER_CHCTL2_CH1EN BIT(4) /*!< channel 1 capture/compare function enable */
  10. #define TIMER_CHCTL2_CH1P BIT(5) /*!< channel 1 capture/compare function polarity */
  11. #define TIMER_CHCTL2_CH1NEN BIT(6) /*!< channel 1 complementary output enable */
  12. #define TIMER_CHCTL2_CH1NP BIT(7) /*!< channel 1 complementary output polarity */
  13. #define TIMER_CHCTL2_CH2EN BIT(8) /*!< channel 2 capture/compare function enable */
  14. #define TIMER_CHCTL2_CH2P BIT(9) /*!< channel 2 capture/compare function polarity */
  15. #define TIMER_CHCTL2_CH2NEN BIT(10) /*!< channel 2 complementary output enable */
  16. #define TIMER_CHCTL2_CH2NP BIT(11) /*!< channel 2 complementary output polarity */
  17. #define TIMxCCER_MASK_CH012 ((uint16_t) (TIMER_CHCTL2_CH0EN|TIMER_CHCTL2_CH0NEN|\
  18. TIMER_CHCTL2_CH1EN|TIMER_CHCTL2_CH1NEN|\
  19. TIMER_CHCTL2_CH2EN|TIMER_CHCTL2_CH2NEN))
  20. #define pwm_enable_channel() {TIMER_CHCTL2(pwm_timer) |= TIMxCCER_MASK_CH012;}
  21. #define pwm_disable_channel() {TIMER_CHCTL2(pwm_timer) &= ~TIMxCCER_MASK_CH012;}
  22. #define ch0_update_duty(duty) TIMER_CH0CV(pwm_timer) = (uint32_t)duty
  23. #define ch1_update_duty(duty) TIMER_CH1CV(pwm_timer) = (uint32_t)duty
  24. #define ch2_update_duty(duty) TIMER_CH2CV(pwm_timer) = (uint32_t)duty
  25. #define update_adc_trigger(time) TIMER_CH3CV(pwm_timer) = (uint32_t)time
  26. #ifdef CONFIG_PWM_UV_SWAP
  27. #define pwm_update_duty(dutyA, dutyB, dutyC) \
  28. do {\
  29. ch0_update_duty(dutyC);\
  30. ch1_update_duty(dutyB);\
  31. ch2_update_duty(dutyA);\
  32. }while(0)
  33. #else
  34. #define pwm_update_duty(dutyA, dutyB, dutyC) \
  35. do {\
  36. ch0_update_duty(dutyA);\
  37. ch1_update_duty(dutyB);\
  38. ch2_update_duty(dutyC);\
  39. }while(0)
  40. #endif
  41. #define pwm_update_2smaples(samp1, sampl2) \
  42. do { \
  43. TIMER_CH3CV(pwm_timer) = (uint32_t)samp1; \
  44. }while(0)
  45. #define pwm_wait_and_clear_updata() \
  46. do { \
  47. while ( TIM_GetFlagStatus(pwm_timer, TIM_FLAG_UPDATE) == RESET ); \
  48. TIM_ClearFlag(pwm_timer, TIM_FLAG_UPDATE); \
  49. }while(0)
  50. #define pwm_change_t3_mode(m) \
  51. do { \
  52. if (((TIMER_CHCTL1(pwm_timer) >> 8)) != m) { \
  53. TIMER_CHCTL1(pwm_timer) &= (~(uint32_t)TIM_CCMOD2_OC4MD); \
  54. TIMER_CHCTL1(pwm_timer) |= (m << 8); \
  55. } \
  56. }while(0)
  57. #define pwm_brake_enable(n) \
  58. do { \
  59. if (n) { \
  60. nvic_irq_enable(PWM_BRK_IRQ, EBREAK_IRQ_PRIORITY, 0); \
  61. }else { \
  62. nvic_irq_disable(PWM_BRK_IRQ); \
  63. } \
  64. }while(0)
  65. #define pwm_up_enable(n) \
  66. do { \
  67. if (n) { \
  68. TIM_ClearFlag(pwm_timer, TIM_FLAG_UPDATE); \
  69. TIM_ConfigInt(pwm_timer, TIM_INT_UPDATE, ENABLE); \
  70. }else { \
  71. TIM_ConfigInt(pwm_timer, TIM_INT_UPDATE, DISABLE); \
  72. TIM_ClrIntPendingBit(pwm_timer, TIM_INT_UPDATE); \
  73. } \
  74. }while(0)
  75. #define get_deadtime() (TIMER_CCHP(pwm_timer) & 0xFF)
  76. void pwm_3phase_init(void);
  77. void pwm_3phase_sides(bool hon, bool lon);
  78. void pwm_start(void);
  79. void pwm_stop(void);
  80. void pwm_turn_on_low_side(void);
  81. void pwm_enable_output(bool enable);
  82. void pwm_update_sample(u32 samp1, u32 samp2, u8 sector);
  83. #endif /*_PWM_H__*/