pwm.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. #include "bsp/bsp.h"
  2. #include "bsp/pwm.h"
  3. #include "bsp/adc.h"
  4. #include "os/os_task.h"
  5. /*
  6. 以下主要是在某一相电路无法采集的时候,需要对这相的pwm挖坑处理
  7. timer 分配:
  8. timer0 -> ch0-2 互补pwm
  9. ch4 event, update event 触发DMA(ch3,4)实现CCR的自更新
  10. timer1 -> 触发ADC采样,GD32不支持多channel 或方式触发输出,通过timer1的 ch0 compara 配置 TRGO触发ADC,但是需要在一个PWM周期内触发2次(单电阻)
  11. timer0 master --> timer1 slave/master 确保timer0,1同步开始,同频同相位
  12. DMA 分配:
  13. DMA0 ch4 -> timer0 update event
  14. ch3 -> timer0 chan3 CC event
  15. ch1 -> timer1 update event,需要更新CCR
  16. */
  17. static void _init_pwm_timer(void);
  18. static void _pwm_gpio_config(void);
  19. #ifndef PWM_BRAKE_GROUP
  20. static void _gpio_brakein_irq_enable(void);
  21. #endif
  22. u16 timer_update_buffer[6] = {0};
  23. void pwm_3phase_init(void){
  24. _pwm_gpio_config();
  25. _init_pwm_timer();
  26. }
  27. static rcu_periph_enum _rcu_clk(u32 timer) {
  28. if (timer == TIMER0) {
  29. return RCU_TIMER0;
  30. }
  31. if (timer == TIMER1) {
  32. return RCU_TIMER1;
  33. }
  34. if (timer == TIMER2) {
  35. return RCU_TIMER2;
  36. }
  37. return RCU_TIMER2;
  38. }
  39. static void _pwm_gpio_config(void)
  40. {
  41. rcu_periph_clock_enable(PWM_U_P_RCU);
  42. rcu_periph_clock_enable(PWM_V_P_RCU);
  43. rcu_periph_clock_enable(PWM_W_P_RCU);
  44. rcu_periph_clock_enable(PWM_U_N_RCU);
  45. rcu_periph_clock_enable(PWM_V_N_RCU);
  46. rcu_periph_clock_enable(PWM_W_N_RCU);
  47. rcu_periph_clock_enable(RCU_AF);
  48. /*configure PA8 PA9 PA10(TIMER0 CH0 CH1 CH2) as alternate function*/
  49. gpio_init(PWM_U_P_GROUP,PWM_U_P_MODE,GPIO_OSPEED_50MHZ,PWM_U_P_PIN);
  50. gpio_init(PWM_V_P_GROUP,PWM_V_P_MODE,GPIO_OSPEED_50MHZ,PWM_V_P_PIN);
  51. gpio_init(PWM_W_P_GROUP,PWM_W_P_MODE,GPIO_OSPEED_50MHZ,PWM_W_P_PIN);
  52. /*configure PB13 PB14 PB15(TIMER0 CH0N CH1N CH2N) as alternate function*/
  53. gpio_init(PWM_U_N_GROUP,PWM_U_N_MODE,GPIO_OSPEED_50MHZ,PWM_U_N_PIN);
  54. gpio_init(PWM_V_N_GROUP,PWM_V_N_MODE,GPIO_OSPEED_50MHZ,PWM_V_N_PIN);
  55. gpio_init(PWM_W_N_GROUP,PWM_W_N_MODE,GPIO_OSPEED_50MHZ,PWM_W_N_PIN);
  56. /*configure BRAKE IN*/
  57. #ifdef PWM_BRAKE_GROUP
  58. /* TIMER0 BKIN */
  59. rcu_periph_clock_enable(PWM_BRAKE_RCU);
  60. gpio_init(PWM_BRAKE_GROUP, PWM_BRAKE_MODE, GPIO_OSPEED_50MHZ, PWM_BRAKE_PIN);
  61. #endif
  62. }
  63. static u8 _dead_time(u16 t) {
  64. if (t < 128) {
  65. return (u8 )t;
  66. }else if (t < (64 + 63) * 2) { //11 1111
  67. return (((u8)2<<6 + (t-64)/2));
  68. }else if (t < (32 + 31) * 8) {
  69. return ((u8)3 << 3 + (t - 32)/8);
  70. }else {
  71. if ((t-32)/16 > 63) {
  72. return 0xFF;
  73. }
  74. return ((u8)7<<3 + (t - 32)/16);
  75. }
  76. }
  77. static void _init_pwm_timer(void) {
  78. timer_oc_parameter_struct timer_ocintpara;
  79. timer_parameter_struct timer_initpara;
  80. u32 timer = pwm_timer;
  81. u32 half_period = FOC_PWM_Half_Period;
  82. rcu_periph_clock_enable(_rcu_clk(timer));
  83. timer_deinit(timer);
  84. /* TIMER0 configuration */
  85. memset(&timer_initpara, 0, sizeof(timer_initpara));
  86. memset(&timer_ocintpara, 0, sizeof(timer_ocintpara));
  87. timer_initpara.prescaler = 0;
  88. timer_initpara.alignedmode = TIMER_COUNTER_CENTER_UP;
  89. timer_initpara.counterdirection = TIMER_COUNTER_UP;
  90. timer_initpara.period = half_period;
  91. timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
  92. timer_initpara.repetitioncounter = 1;
  93. timer_init(timer,&timer_initpara);
  94. /* auto-reload preload enable */
  95. timer_auto_reload_shadow_enable(timer);
  96. /* CH1,CH2 and CH3 configuration in PWM mode */
  97. timer_ocintpara.outputstate = TIMER_CCX_ENABLE;
  98. timer_ocintpara.outputnstate = TIMER_CCXN_ENABLE;
  99. timer_ocintpara.ocpolarity = TIMER_OC_POLARITY_HIGH;
  100. timer_ocintpara.ocnpolarity = TIMER_OCN_POLARITY_HIGH;
  101. timer_ocintpara.ocidlestate = TIMER_OC_IDLE_STATE_LOW;
  102. timer_ocintpara.ocnidlestate = TIMER_OCN_IDLE_STATE_LOW;
  103. timer_channel_output_config(timer,TIMER_CH_0,&timer_ocintpara);
  104. timer_channel_output_pulse_value_config(timer,TIMER_CH_0,half_period/2);
  105. timer_channel_output_mode_config(timer,TIMER_CH_0,PWM_MODE);
  106. timer_channel_output_shadow_config(timer,TIMER_CH_0,TIMER_OC_SHADOW_ENABLE);
  107. timer_channel_output_config(timer,TIMER_CH_1,&timer_ocintpara);
  108. timer_channel_output_pulse_value_config(timer,TIMER_CH_1,half_period/2);
  109. timer_channel_output_mode_config(timer,TIMER_CH_1,PWM_MODE);
  110. timer_channel_output_shadow_config(timer,TIMER_CH_1,TIMER_OC_SHADOW_ENABLE);
  111. timer_channel_output_config(timer,TIMER_CH_2,&timer_ocintpara);
  112. timer_channel_output_pulse_value_config(timer,TIMER_CH_2,half_period/2);
  113. timer_channel_output_mode_config(timer,TIMER_CH_2,PWM_MODE);
  114. timer_channel_output_shadow_config(timer,TIMER_CH_2,TIMER_OC_SHADOW_ENABLE);
  115. timer_ocintpara.outputstate = TIMER_CCX_ENABLE;
  116. timer_ocintpara.outputnstate = TIMER_CCXN_DISABLE;
  117. timer_ocintpara.ocpolarity = TIMER_OC_POLARITY_HIGH;
  118. timer_ocintpara.ocnpolarity = TIMER_OCN_POLARITY_HIGH;
  119. timer_ocintpara.ocidlestate = TIMER_OC_IDLE_STATE_LOW;
  120. timer_ocintpara.ocnidlestate = TIMER_OCN_IDLE_STATE_LOW;
  121. /* chan3 trigger adc O3CPRE is alwary active high, adc trigger is rising */
  122. timer_channel_output_config(timer,TIMER_CH_3,&timer_ocintpara);
  123. timer_channel_output_pulse_value_config(timer,TIMER_CH_3,half_period-5);
  124. timer_channel_output_mode_config(timer,TIMER_CH_3,TIMER_OC_MODE_PWM1);
  125. timer_channel_output_shadow_config(timer,TIMER_CH_3,TIMER_OC_SHADOW_ENABLE);
  126. #ifdef PWM_BRAKE_GROUP
  127. timer_break_parameter_struct timer_breakpara;
  128. timer_breakpara.runoffstate = TIMER_ROS_STATE_DISABLE;
  129. timer_breakpara.ideloffstate = TIMER_ROS_STATE_DISABLE;
  130. timer_breakpara.protectmode = TIMER_CCHP_PROT_OFF;
  131. timer_breakpara.deadtime = _dead_time(NS_2_TCLK(PWM_DEAD_TIME_NS));
  132. timer_breakpara.breakstate = TIMER_BREAK_ENABLE;
  133. timer_breakpara.breakpolarity = TIMER_BREAK_POLARITY_LOW;
  134. timer_breakpara.outputautostate = TIMER_OUTAUTO_DISABLE;
  135. timer_break_config(timer,&timer_breakpara);
  136. timer_interrupt_enable(timer, TIMER_INT_BRK);
  137. timer_interrupt_flag_clear(timer, TIMER_INT_FLAG_BRK);
  138. nvic_irq_enable(TIMER0_BRK_IRQn, EBREAK_IRQ_PRIORITY, 0);
  139. #else
  140. _gpio_brakein_irq_enable();
  141. #endif
  142. timer_master_slave_mode_config(timer,TIMER_MASTER_SLAVE_MODE_DISABLE);
  143. pwm_enable_channel();
  144. timer_interrupt_disable(timer, TIMER_INT_UP);
  145. timer_interrupt_flag_clear(timer, TIMER_INT_FLAG_UP);
  146. nvic_irq_enable(TIMER0_UP_IRQn, TIMER_UP_IRQ_PRIORITY, 0);
  147. timer_enable(timer);
  148. #ifdef GD32_FOC_DEMO
  149. /* IR2136S enable */
  150. gpio_ir2136_enable(true);
  151. #endif
  152. }
  153. #ifndef PWM_BRAKE_GROUP
  154. static void _gpio_brakein_irq_enable(void){
  155. #ifndef GD32_FOC_DEMO
  156. gpio_exti_source_select(GPIO_PORT_SOURCE_GPIOB, GPIO_PIN_SOURCE_4);
  157. exti_init(EXTI_4, EXTI_INTERRUPT, EXTI_TRIG_BOTH);
  158. nvic_irq_enable(EXTI4_IRQn, EBREAK_IRQ_PRIORITY, 0U);
  159. exti_interrupt_flag_clear(EXTI_4);
  160. exti_interrupt_enable(EXTI_4);
  161. gpio_exti_source_select(GPIO_PORT_SOURCE_GPIOB, GPIO_PIN_SOURCE_5);
  162. exti_init(EXTI_5, EXTI_INTERRUPT, EXTI_TRIG_BOTH);
  163. nvic_irq_enable(EXTI5_9_IRQn, EBREAK_IRQ_PRIORITY, 0U);
  164. exti_interrupt_flag_clear(EXTI_5);
  165. exti_interrupt_enable(EXTI_5);
  166. #endif
  167. }
  168. #endif
  169. void pwm_start(void){
  170. pwm_update_duty(FOC_PWM_Half_Period/2, FOC_PWM_Half_Period/2, FOC_PWM_Half_Period/2);
  171. pwm_update_2smaples(FOC_PWM_Half_Period-1, FOC_PWM_Half_Period + 1);
  172. /* wait for a new PWM period to flush last HF task */
  173. timer_flag_clear(pwm_timer, TIMER_FLAG_UP);
  174. timer_event_software_generate(pwm_timer, TIMER_EVENT_SRC_UPG);
  175. while ( timer_flag_get(pwm_timer, TIMER_FLAG_UP) == RESET ){}
  176. /* Clear Update Flag */
  177. timer_flag_clear(pwm_timer, TIMER_FLAG_UP);
  178. timer_primary_output_config(pwm_timer,ENABLE);
  179. }
  180. void pwm_stop(void){
  181. timer_primary_output_config(pwm_timer,DISABLE);
  182. timer_interrupt_disable(pwm_timer, TIMER_INT_UP);
  183. /* wait for a new PWM period to flush last HF task */
  184. timer_flag_clear(pwm_timer, TIMER_FLAG_UP);
  185. while ( timer_flag_get(pwm_timer, TIMER_FLAG_UP) == RESET ){}
  186. /* Clear Update Flag */
  187. timer_flag_clear(pwm_timer, TIMER_FLAG_UP);
  188. }
  189. void pwm_enable_output(bool enable) {
  190. if (enable) {
  191. timer_primary_output_config(pwm_timer,ENABLE);
  192. }else {
  193. timer_primary_output_config(pwm_timer,DISABLE);
  194. }
  195. }
  196. /*open low side of the mosfet*/
  197. void pwm_turn_on_low_side(void)
  198. {
  199. pwm_update_duty(0, 0, 0);
  200. pwm_update_2smaples(FOC_PWM_Half_Period-1, FOC_PWM_Half_Period + 1);
  201. timer_flag_clear(pwm_timer,TIMER_FLAG_UP);
  202. timer_event_software_generate(pwm_timer, TIMER_EVENT_SRC_UPG);
  203. while (timer_flag_get(pwm_timer,TIMER_FLAG_UP) == RESET );
  204. /* Main PWM Output Enable */
  205. timer_primary_output_config(pwm_timer, ENABLE);
  206. }
  207. void pwm_update_sample(u32 samp1, u32 samp2, u8 sector) {
  208. if (samp1 < FOC_PWM_Half_Period) {
  209. TIMER_CH3CV(pwm_timer) = samp1;
  210. pwm_change_t3_mode(TIMER_OC_MODE_PWM1);
  211. }else {
  212. TIMER_CH3CV(pwm_timer) = samp2;
  213. pwm_change_t3_mode(TIMER_OC_MODE_PWM0);
  214. }
  215. adc_current_sample_config(sector);
  216. }