pwm.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. #include "bsp/pwm.h"
  2. #include "bsp/adc.h"
  3. #include "os/timer.h"
  4. /*
  5. 以下主要是在某一相电路无法采集的时候,需要对这相的pwm挖坑处理
  6. timer 分配:
  7. timer0 -> ch0-2 互补pwm
  8. ch4 event, update event 触发DMA(ch3,4)实现CCR的自更新
  9. timer1 -> 触发ADC采样,GD32不支持多channel 或方式触发输出,通过timer1的 ch0 compara 配置 TRGO触发ADC,但是需要在一个PWM周期内触发2次(单电阻)
  10. timer0 master --> timer1 slave/master 确保timer0,1同步开始,同频同相位
  11. DMA 分配:
  12. DMA0 ch4 -> timer0 update event
  13. ch3 -> timer0 chan3 CC event
  14. ch1 -> timer1 update event,需要更新CCR
  15. */
  16. static void _init_pwm_timer(void);
  17. #ifdef ENABLE_AUX_TIMER
  18. static void _init_adc_timer(void);
  19. static void _init_aux_timer(void);
  20. #endif
  21. #if SHUNT_NUM==ONE_SHUNT_SAMPLE
  22. static void timer0_dma_config(void);
  23. #endif
  24. static void _pwm_gpio_config(void);
  25. static void _gpio_brakein_irq_enable(void);
  26. u16 timer_update_buffer[6] = {0};
  27. void pwm_3phase_init(void){
  28. _init_pwm_timer();
  29. #ifdef ENABLE_AUX_TIMER
  30. _init_adc_timer();
  31. _init_aux_timer();
  32. #endif
  33. #if SHUNT_NUM==ONE_SHUNT_SAMPLE
  34. timer0_dma_config();
  35. #endif
  36. _pwm_gpio_config();
  37. }
  38. static rcu_periph_enum _rcu_clk(u32 timer) {
  39. if (timer == TIMER0) {
  40. return RCU_TIMER0;
  41. }
  42. if (timer == TIMER1) {
  43. return RCU_TIMER1;
  44. }
  45. if (timer == TIMER2) {
  46. return RCU_TIMER2;
  47. }
  48. return RCU_TIMER2;
  49. }
  50. static void _pwm_gpio_config(void)
  51. {
  52. rcu_periph_clock_enable(RCU_GPIOA);
  53. rcu_periph_clock_enable(RCU_GPIOB);
  54. rcu_periph_clock_enable(RCU_AF);
  55. //gpio_pin_remap_config(GPIO_TIMER0_PARTIAL_REMAP, ENABLE);
  56. //gpio_pin_remap_config(GPIO_TIMER0_FULL_REMAP, DISABLE);
  57. /*configure PA8 PA9 PA10(TIMER0 CH0 CH1 CH2) as alternate function*/
  58. gpio_init(GPIOA,GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_8);
  59. gpio_init(GPIOA,GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_9);
  60. gpio_init(GPIOA,GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_10);
  61. /*configure PB13 PB14 PB15(TIMER0 CH0N CH1N CH2N) as alternate function*/
  62. gpio_init(GPIOB,GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_13);
  63. gpio_init(GPIOB,GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_14);
  64. gpio_init(GPIOB,GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_15);
  65. /*configure BRAKE IN*/
  66. #if USER_ITMER_BRAKE==1
  67. gpio_init(GPIOA,GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_6);
  68. #else
  69. gpio_init(GPIOB,GPIO_MODE_IN_FLOATING,GPIO_OSPEED_50MHZ,GPIO_PIN_4);//高刹车
  70. gpio_init(GPIOB,GPIO_MODE_IN_FLOATING,GPIO_OSPEED_50MHZ,GPIO_PIN_5);//低刹车
  71. #endif
  72. }
  73. static void _init_pwm_timer(void) {
  74. timer_oc_parameter_struct timer_ocintpara;
  75. timer_parameter_struct timer_initpara;
  76. u32 timer = pwm_timer;
  77. u32 half_period = FOC_PWM_Half_Period;
  78. rcu_periph_clock_enable(_rcu_clk(timer));
  79. timer_deinit(timer);
  80. /* TIMER0 configuration */
  81. memset(&timer_initpara, 0, sizeof(timer_initpara));
  82. memset(&timer_ocintpara, 0, sizeof(timer_ocintpara));
  83. timer_initpara.prescaler = 0;
  84. timer_initpara.alignedmode = TIMER_COUNTER_CENTER_UP;
  85. timer_initpara.period = half_period;
  86. timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
  87. timer_initpara.repetitioncounter = 0;
  88. timer_init(timer,&timer_initpara);
  89. /* auto-reload preload enable */
  90. timer_auto_reload_shadow_enable(timer);
  91. /* CH1,CH2 and CH3 configuration in PWM mode */
  92. timer_ocintpara.outputstate = TIMER_CCX_ENABLE;
  93. timer_ocintpara.outputnstate = TIMER_CCXN_ENABLE;
  94. timer_ocintpara.ocpolarity = TIMER_OC_POLARITY_LOW;
  95. timer_ocintpara.ocnpolarity = TIMER_OCN_POLARITY_LOW;
  96. timer_ocintpara.ocidlestate = TIMER_OC_IDLE_STATE_HIGH;
  97. timer_ocintpara.ocnidlestate = TIMER_OCN_IDLE_STATE_HIGH;
  98. timer_channel_output_config(timer,TIMER_CH_0,&timer_ocintpara);
  99. timer_channel_output_pulse_value_config(timer,TIMER_CH_0,half_period);
  100. timer_channel_output_mode_config(timer,TIMER_CH_0,TIMER_OC_MODE_PWM0);
  101. timer_channel_output_shadow_config(timer,TIMER_CH_0,TIMER_OC_SHADOW_ENABLE);
  102. timer_channel_output_config(timer,TIMER_CH_1,&timer_ocintpara);
  103. timer_channel_output_pulse_value_config(timer,TIMER_CH_1,half_period);
  104. timer_channel_output_mode_config(timer,TIMER_CH_1,TIMER_OC_MODE_PWM0);
  105. timer_channel_output_shadow_config(timer,TIMER_CH_1,TIMER_OC_SHADOW_ENABLE);
  106. timer_channel_output_config(timer,TIMER_CH_2,&timer_ocintpara);
  107. timer_channel_output_pulse_value_config(timer,TIMER_CH_2,half_period);
  108. timer_channel_output_mode_config(timer,TIMER_CH_2,TIMER_OC_MODE_PWM0);
  109. timer_channel_output_shadow_config(timer,TIMER_CH_2,TIMER_OC_SHADOW_ENABLE);
  110. timer_ocintpara.outputstate = TIMER_CCX_ENABLE;
  111. timer_ocintpara.outputnstate = TIMER_CCXN_DISABLE;
  112. timer_ocintpara.ocpolarity = TIMER_OC_POLARITY_HIGH;
  113. timer_ocintpara.ocnpolarity = TIMER_OCN_POLARITY_HIGH;
  114. timer_ocintpara.ocidlestate = TIMER_OC_IDLE_STATE_LOW;
  115. timer_ocintpara.ocnidlestate = TIMER_OCN_IDLE_STATE_LOW;
  116. /* chan3 trigger adc O3CPRE is alwary active high, adc trigger is rising */
  117. timer_channel_output_config(timer,TIMER_CH_3,&timer_ocintpara);
  118. timer_channel_output_pulse_value_config(timer,TIMER_CH_3,half_period-1);
  119. timer_channel_output_mode_config(timer,TIMER_CH_3,TIMER_OC_MODE_PWM1);
  120. timer_channel_output_shadow_config(timer,TIMER_CH_3,TIMER_OC_SHADOW_ENABLE);
  121. #if USER_ITMER_BRAKE==1
  122. timer_break_parameter_struct timer_breakpara;
  123. /* automatic output enable, break, dead time and lock configuration*/
  124. timer_breakpara.runoffstate = TIMER_ROS_STATE_DISABLE;
  125. timer_breakpara.ideloffstate = TIMER_IOS_STATE_DISABLE ;
  126. timer_breakpara.deadtime = TDead; // one Time of DTS, abort deadtime
  127. timer_breakpara.breakpolarity = TIMER_BREAK_POLARITY_LOW;
  128. timer_breakpara.outputautostate = TIMER_OUTAUTO_ENABLE;
  129. timer_breakpara.protectmode = TIMER_CCHP_PROT_0;
  130. timer_breakpara.breakstate = TIMER_BREAK_ENABLE;
  131. timer_break_config(TIMER0,&timer_breakpara);
  132. timer_interrupt_enable(timer, TIMER_INT_BRK);
  133. timer_interrupt_flag_clear(timer, TIMER_INT_FLAG_BRK);
  134. nvic_irq_enable(TIMER0_BRK_IRQn, 1, 0);
  135. #else
  136. _gpio_brakein_irq_enable();
  137. #endif
  138. #ifdef ENABLE_AUX_TIMER
  139. /* select the master slave mode */
  140. timer_master_slave_mode_config(timer,TIMER_MASTER_SLAVE_MODE_ENABLE);
  141. /* slave mode selection: enabled when time2 is enable*/
  142. timer_slave_mode_select(timer,TIMER_SLAVE_MODE_EVENT);
  143. timer_input_trigger_source_select(timer,TIMER_SMCFG_TRGSEL_ITI2);
  144. #endif
  145. #if SHUNT_NUM==ONE_SHUNT_SAMPLE
  146. timer_channel_output_shadow_config(timer,TIMER_CH_0,TIMER_OC_SHADOW_DISABLE);
  147. timer_channel_output_shadow_config(timer,TIMER_CH_1,TIMER_OC_SHADOW_DISABLE);
  148. timer_channel_output_shadow_config(timer,TIMER_CH_2,TIMER_OC_SHADOW_DISABLE);
  149. timer_dma_transfer_config(TIMER0, TIMER_DMACFG_DMATA_CH0CV, TIMER_DMACFG_DMATC_3TRANSFER);
  150. timer_dma_enable(TIMER0, TIMER_DMA_UPD);
  151. #else
  152. timer_master_slave_mode_config(timer,TIMER_MASTER_SLAVE_MODE_DISABLE);
  153. #endif /* ONE_SHUNT_SAMPLE_1 */
  154. pwm_enable_channel();
  155. timer_interrupt_disable(timer, TIMER_INT_UP);
  156. timer_interrupt_flag_clear(timer, TIMER_INT_FLAG_UP);
  157. nvic_irq_enable(TIMER0_UP_IRQn, TIMER_UP_IRQ_PRIORITY, 0);
  158. #if SHUNT_NUM==THREE_SHUNTS_SAMPLE
  159. timer_enable(timer);
  160. #endif
  161. }
  162. #ifdef ENABLE_AUX_TIMER
  163. static void _init_adc_timer(void) {
  164. timer_oc_parameter_struct timer_ocintpara;
  165. timer_parameter_struct timer_initpara;
  166. u32 timer = adc_timer;
  167. u32 half_period = FOC_PWM_Half_Period;
  168. rcu_periph_clock_enable(_rcu_clk(timer));
  169. timer_deinit(timer);
  170. memset(&timer_initpara, 0, sizeof(timer_initpara));
  171. memset(&timer_ocintpara, 0, sizeof(timer_ocintpara));
  172. timer_initpara.prescaler = 0;
  173. timer_initpara.alignedmode = TIMER_COUNTER_CENTER_UP;
  174. timer_initpara.period = half_period;
  175. timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
  176. timer_initpara.repetitioncounter = 0;
  177. timer_init(timer,&timer_initpara);
  178. timer_ocintpara.outputstate = TIMER_CCX_ENABLE;
  179. timer_ocintpara.outputnstate = TIMER_CCXN_DISABLE;
  180. timer_ocintpara.ocpolarity = TIMER_OC_POLARITY_HIGH;
  181. timer_ocintpara.ocnpolarity = TIMER_OCN_POLARITY_HIGH;
  182. timer_ocintpara.ocidlestate = TIMER_OC_IDLE_STATE_LOW;
  183. timer_ocintpara.ocnidlestate = TIMER_OCN_IDLE_STATE_LOW;
  184. timer_channel_output_config(timer,TIMER_CH_0,&timer_ocintpara);
  185. timer_channel_output_pulse_value_config(timer,TIMER_CH_0,half_period-1);
  186. #if SHUNT_NUM==ONE_SHUNT_SAMPLE
  187. timer_channel_output_mode_config(timer,TIMER_CH_0,TIMER_OC_MODE_PWM1);
  188. #else
  189. timer_channel_output_mode_config(timer,TIMER_CH_0,TIMER_OC_MODE_PWM0);
  190. #endif
  191. timer_channel_output_shadow_config(timer,TIMER_CH_0,TIMER_OC_SHADOW_ENABLE);
  192. /* select the master slave mode */
  193. timer_master_slave_mode_config(timer,TIMER_MASTER_SLAVE_MODE_ENABLE);
  194. /* slave mode selection: enabled when time2 is enable*/
  195. timer_slave_mode_select(timer,TIMER_SLAVE_MODE_EVENT);
  196. timer_input_trigger_source_select(timer,TIMER_SMCFG_TRGSEL_ITI2);
  197. //timer_primary_output_config(timer,ENABLE);
  198. /* auto-reload preload enable */
  199. timer_auto_reload_shadow_enable(timer);
  200. //timer_enable(timer);
  201. }
  202. static void _init_aux_timer(void) {
  203. timer_parameter_struct timer_initpara;
  204. u32 timer = aux_timer;
  205. rcu_periph_clock_enable(_rcu_clk(timer));
  206. timer_deinit(timer);
  207. memset(&timer_initpara, 0, sizeof(timer_initpara));
  208. timer_initpara.prescaler = 0;
  209. timer_initpara.alignedmode = TIMER_COUNTER_CENTER_UP;
  210. timer_initpara.period = FOC_PWM_period;
  211. timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
  212. timer_initpara.repetitioncounter = 0;
  213. timer_init(timer,&timer_initpara);
  214. /* select the master slave mode */
  215. timer_master_slave_mode_config(timer,TIMER_MASTER_SLAVE_MODE_ENABLE);
  216. /* timer update event is used as trigger output */
  217. timer_master_output_trigger_source_select(timer,TIMER_TRI_OUT_SRC_ENABLE);
  218. timer_primary_output_config(timer,ENABLE);
  219. timer_enable(timer);
  220. }
  221. #endif
  222. #if SHUNT_NUM==ONE_SHUNT_SAMPLE
  223. static void timer0_dma_config(void)
  224. {
  225. dma_parameter_struct dma_init_struct;
  226. rcu_periph_clock_enable(RCU_DMA0);
  227. /* TIMER0 update */
  228. dma_deinit(DMA0, DMA_CH4);
  229. dma_init_struct.direction = DMA_MEMORY_TO_PERIPHERAL;
  230. dma_init_struct.memory_addr = (uint32_t)timer_update_buffer;
  231. dma_init_struct.memory_inc = DMA_MEMORY_INCREASE_ENABLE;
  232. dma_init_struct.memory_width = DMA_MEMORY_WIDTH_16BIT;
  233. dma_init_struct.number = 6;
  234. dma_init_struct.periph_addr = (uint32_t)0x40012C4C;
  235. dma_init_struct.periph_inc = DMA_PERIPH_INCREASE_DISABLE;
  236. dma_init_struct.periph_width = DMA_PERIPHERAL_WIDTH_16BIT;
  237. dma_init_struct.priority = DMA_PRIORITY_ULTRA_HIGH;
  238. dma_init(DMA0, DMA_CH4, &dma_init_struct);
  239. dma_circulation_enable(DMA0, DMA_CH4);
  240. dma_memory_to_memory_disable(DMA0, DMA_CH4);
  241. }
  242. #endif
  243. static void _gpio_brakein_irq_enable(void){
  244. gpio_exti_source_select(GPIO_PORT_SOURCE_GPIOB, GPIO_PIN_SOURCE_4);
  245. exti_init(EXTI_4, EXTI_INTERRUPT, EXTI_TRIG_BOTH);
  246. nvic_irq_enable(EXTI4_IRQn, 4U, 0U);
  247. exti_interrupt_flag_clear(EXTI_4);
  248. exti_interrupt_enable(EXTI_4);
  249. gpio_exti_source_select(GPIO_PORT_SOURCE_GPIOB, GPIO_PIN_SOURCE_5);
  250. exti_init(EXTI_5, EXTI_INTERRUPT, EXTI_TRIG_BOTH);
  251. nvic_irq_enable(EXTI5_9_IRQn, 4U, 0U);
  252. exti_interrupt_flag_clear(EXTI_5);
  253. exti_interrupt_enable(EXTI_5);
  254. }
  255. void pwm_start(void){
  256. #if SHUNT_NUM==THREE_SHUNTS_SAMPLE
  257. pwm_update_duty(FOC_PWM_Half_Period/2, FOC_PWM_Half_Period/2, FOC_PWM_Half_Period/2);
  258. pwm_update_2smaples(FOC_PWM_Half_Period-1, FOC_PWM_Half_Period+1);
  259. #else
  260. pwm_update_duty_dma(0,0,0,0,0,0);
  261. dma_channel_enable(DMA0, DMA_CH4);
  262. pwm_update_2smaples(FOC_PWM_Half_Period-(2 * TADC + TRise), FOC_PWM_Half_Period-TADC);
  263. #endif
  264. /* wait for a new PWM period to flush last HF task */
  265. timer_flag_clear(pwm_timer, TIMER_FLAG_UP);
  266. timer_event_software_generate(pwm_timer, TIMER_EVENT_SRC_UPG);
  267. while ( timer_flag_get(pwm_timer, TIMER_FLAG_UP) == RESET ){}
  268. /* Clear Update Flag */
  269. timer_flag_clear(pwm_timer, TIMER_FLAG_UP);
  270. timer_primary_output_config(pwm_timer,ENABLE);
  271. #ifdef ENABLE_AUX_TIMER
  272. timer_primary_output_config(adc_timer,ENABLE);
  273. #endif
  274. timer_flag_clear(pwm_timer, TIMER_FLAG_UP);
  275. timer_interrupt_enable(pwm_timer, TIMER_INT_UP);
  276. }
  277. void pwm_stop(void){
  278. dma_channel_disable(DMA0, DMA_CH4);
  279. timer_primary_output_config(pwm_timer,DISABLE);
  280. #ifdef ENABLE_AUX_TIMER
  281. timer_primary_output_config(adc_timer,DISABLE);
  282. #endif
  283. //timer_interrupt_disable(pwm_timer, TIMER_INT_UP);
  284. /* wait for a new PWM period to flush last HF task */
  285. timer_flag_clear(pwm_timer, TIMER_FLAG_UP);
  286. //while ( timer_flag_get(pwm_timer, TIMER_FLAG_UP) == RESET ){}
  287. /* Clear Update Flag */
  288. timer_flag_clear(pwm_timer, TIMER_FLAG_UP);
  289. }
  290. void pwm_enable_output(bool enable) {
  291. if (enable) {
  292. timer_primary_output_config(pwm_timer,ENABLE);
  293. }else {
  294. timer_primary_output_config(pwm_timer,DISABLE);
  295. }
  296. }
  297. /*open low side of the mosfet*/
  298. void pwm_turn_on_low_side(void)
  299. {
  300. #if SHUNT_NUM==THREE_SHUNTS_SAMPLE
  301. pwm_update_duty(0, 0, 0);
  302. pwm_update_2smaples(FOC_PWM_Half_Period-1, FOC_PWM_Half_Period+1);
  303. #else
  304. pwm_update_duty_dma(0,0,0,0,0,0);
  305. dma_channel_enable(DMA0, DMA_CH4);
  306. pwm_update_2smaples(FOC_PWM_Half_Period-(2 * TADC + TRise), FOC_PWM_Half_Period-TADC);
  307. #endif
  308. timer_flag_clear(pwm_timer,TIMER_FLAG_UP);
  309. timer_event_software_generate(pwm_timer, TIMER_EVENT_SRC_UPG);
  310. while (timer_flag_get(pwm_timer,TIMER_FLAG_UP) == RESET );
  311. /* Main PWM Output Enable */
  312. timer_primary_output_config(pwm_timer, ENABLE);
  313. #ifdef ENABLE_AUX_TIMER
  314. timer_primary_output_config(adc_timer, ENABLE);
  315. #endif
  316. }
  317. __weak void foc_brake_handler(bool brake) {
  318. //dumy function, must implemented in FOC controller
  319. }
  320. /*do 50 times filter*/
  321. static void brake_timer_handler(timer_t *t) {
  322. int count = 50;
  323. int settimes = 0;
  324. while(count-- >= 0) {
  325. bool b1 = gpio_input_bit_get(GPIOB, GPIO_PIN_4) == SET;
  326. bool b2 = gpio_input_bit_get(GPIOB, GPIO_PIN_5) == SET;
  327. if (b1 && b2) {
  328. settimes++;
  329. }
  330. }
  331. if (settimes == 0) {
  332. foc_brake_handler(true);
  333. }else if (settimes == 50) {
  334. foc_brake_handler(false);
  335. }else {
  336. //有干扰,do nothing
  337. }
  338. }
  339. static timer_t _brake_timer = TIMER_INIT(_brake_timer, brake_timer_handler);
  340. void mc_brake_irq(void) {
  341. timer_post(&_brake_timer, 0);
  342. }