fan_pwm.c 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #include "fan_pwm.h"
  2. void fan_pwm_init(void){
  3. uint16_t chno = FAN_PWM_CHAN;
  4. /* -----------------------------------------------------------------------
  5. TIMER1 configuration: generate 3 PWM signals with 3 different duty cycles:
  6. TIMER1CLK = SystemCoreClock / 108 = 1MHz
  7. TIMER1 channel1 duty cycle = (4000/ 16000)* 100 = 25%
  8. TIMER1 channel2 duty cycle = (8000/ 16000)* 100 = 50%
  9. TIMER1 channel3 duty cycle = (12000/ 16000)* 100 = 75%
  10. ----------------------------------------------------------------------- */
  11. timer_oc_parameter_struct timer_ocintpara;
  12. timer_parameter_struct timer_initpara;
  13. rcu_periph_clock_enable(GPIO_FAN_OUT_RCU);
  14. rcu_periph_clock_enable(RCU_AF);
  15. /*Configure PA1 PA2 PA3(TIMER1 CH1 CH2 CH3) as alternate function*/
  16. gpio_init(GPIO_FAN_OUT_GROUP, GPIO_FAN_OUT_MODE, GPIO_OSPEED_50MHZ, GPIO_FAN_OUT_PIN);
  17. rcu_periph_clock_enable(FAN_TIMER_RCU);
  18. timer_deinit(FAN_PWM_TIMER);
  19. /* TIMER1 configuration 1M*/
  20. timer_initpara.prescaler = 119;
  21. timer_initpara.alignedmode = TIMER_COUNTER_EDGE;
  22. timer_initpara.counterdirection = TIMER_COUNTER_UP;
  23. timer_initpara.period = FAN_MAX_DUTY_COUNT;
  24. timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
  25. timer_initpara.repetitioncounter = 1;
  26. timer_init(FAN_PWM_TIMER,&timer_initpara);
  27. /* CH2 and CH3 configuration in PWM mode1 */
  28. timer_ocintpara.ocpolarity = TIMER_OC_POLARITY_HIGH;
  29. timer_ocintpara.outputstate = TIMER_CCX_ENABLE;
  30. timer_ocintpara.ocnpolarity = TIMER_OCN_POLARITY_LOW;
  31. timer_ocintpara.outputnstate = TIMER_CCXN_DISABLE;
  32. timer_ocintpara.ocidlestate = TIMER_OC_IDLE_STATE_LOW;
  33. timer_ocintpara.ocnidlestate = TIMER_OCN_IDLE_STATE_HIGH;
  34. timer_channel_output_config(FAN_PWM_TIMER,chno,&timer_ocintpara);
  35. /* CH2 configuration in PWM mode1,duty cycle 46% */
  36. timer_channel_output_pulse_value_config(FAN_PWM_TIMER,chno,FAN_MAX_DUTY_COUNT-1);
  37. timer_channel_output_mode_config(FAN_PWM_TIMER,chno,TIMER_OC_MODE_PWM0);
  38. timer_channel_output_shadow_config(FAN_PWM_TIMER,chno,TIMER_OC_SHADOW_ENABLE);
  39. /* auto-reload preload enable */
  40. timer_auto_reload_shadow_enable(FAN_PWM_TIMER);
  41. timer_enable(FAN_PWM_TIMER);
  42. }
  43. void fan_stop(void) {
  44. timer_disable(FAN_PWM_TIMER);
  45. }
  46. void fan_set_duty(u8 duty) {
  47. uint16_t chno = FAN_PWM_CHAN;
  48. if (duty > 100) {
  49. duty = 100;
  50. }else if (duty > 0 && duty < 30) {
  51. duty = 30;
  52. }
  53. u32 count = (float)duty * (float)FAN_MAX_DUTY_COUNT / 100.0f;
  54. timer_channel_output_pulse_value_config(FAN_PWM_TIMER,chno, count);
  55. if (count == 0) {
  56. timer_primary_output_config(FAN_PWM_TIMER,DISABLE);
  57. }else {
  58. timer_primary_output_config(FAN_PWM_TIMER,ENABLE);
  59. }
  60. }
  61. bool fan_pwm_is_running(void) {
  62. if (TIMER_CCHP(FAN_PWM_TIMER) & (uint32_t)TIMER_CCHP_POEN) {
  63. return true;
  64. }
  65. return false;
  66. }