pwm.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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. #ifdef ENABLE_AUX_TIMER
  19. static void _init_adc_timer(void);
  20. static void _init_aux_timer(void);
  21. #endif
  22. #if SHUNT_NUM==ONE_SHUNT_SAMPLE
  23. static void timer0_dma_config(void);
  24. #endif
  25. static void _pwm_gpio_config(void);
  26. #if USER_ITMER_BRAKE==0
  27. static void _gpio_brakein_irq_enable(void);
  28. #endif
  29. u16 timer_update_buffer[6] = {0};
  30. void pwm_3phase_init(void){
  31. _init_pwm_timer();
  32. #ifdef ENABLE_AUX_TIMER
  33. _init_adc_timer();
  34. _init_aux_timer();
  35. #endif
  36. #if SHUNT_NUM==ONE_SHUNT_SAMPLE
  37. timer0_dma_config();
  38. #endif
  39. _pwm_gpio_config();
  40. }
  41. static rcu_periph_enum _rcu_clk(u32 timer) {
  42. if (timer == TIMER0) {
  43. return RCU_TIMER0;
  44. }
  45. if (timer == TIMER1) {
  46. return RCU_TIMER1;
  47. }
  48. if (timer == TIMER2) {
  49. return RCU_TIMER2;
  50. }
  51. return RCU_TIMER2;
  52. }
  53. static void _pwm_gpio_config(void)
  54. {
  55. rcu_periph_clock_enable(RCU_GPIOA);
  56. rcu_periph_clock_enable(RCU_GPIOB);
  57. rcu_periph_clock_enable(RCU_AF);
  58. //gpio_pin_remap_config(GPIO_TIMER0_PARTIAL_REMAP, ENABLE);
  59. //gpio_pin_remap_config(GPIO_TIMER0_FULL_REMAP, DISABLE);
  60. /*configure PA8 PA9 PA10(TIMER0 CH0 CH1 CH2) as alternate function*/
  61. gpio_init(GPIOA,GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_8);
  62. gpio_init(GPIOA,GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_9);
  63. gpio_init(GPIOA,GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_10);
  64. /*configure PB13 PB14 PB15(TIMER0 CH0N CH1N CH2N) as alternate function*/
  65. gpio_init(GPIOB,GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_13);
  66. gpio_init(GPIOB,GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_14);
  67. gpio_init(GPIOB,GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_15);
  68. /*configure BRAKE IN*/
  69. #if USER_ITMER_BRAKE==1
  70. #ifdef GD32_DEMO
  71. /* TIMER0 BKIN */
  72. gpio_init(GPIOB, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_12);
  73. #else
  74. gpio_init(GPIOA,GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_6);
  75. #endif
  76. #else
  77. gpio_init(GPIOB,GPIO_MODE_IN_FLOATING,GPIO_OSPEED_50MHZ,GPIO_PIN_4);//高刹车
  78. gpio_init(GPIOB,GPIO_MODE_IN_FLOATING,GPIO_OSPEED_50MHZ,GPIO_PIN_5);//低刹车
  79. #endif
  80. #ifdef GD32_DEMO
  81. /* IR2136S enable pin */
  82. gpio_bit_reset(GPIOA, GPIO_PIN_12);
  83. gpio_init(GPIOA, GPIO_MODE_OUT_PP, GPIO_OSPEED_2MHZ, GPIO_PIN_12);
  84. #endif
  85. }
  86. static void _init_pwm_timer(void) {
  87. timer_oc_parameter_struct timer_ocintpara;
  88. timer_parameter_struct timer_initpara;
  89. u32 timer = pwm_timer;
  90. u32 half_period = FOC_PWM_Half_Period;
  91. rcu_periph_clock_enable(_rcu_clk(timer));
  92. timer_deinit(timer);
  93. /* TIMER0 configuration */
  94. memset(&timer_initpara, 0, sizeof(timer_initpara));
  95. memset(&timer_ocintpara, 0, sizeof(timer_ocintpara));
  96. timer_initpara.prescaler = 0;
  97. timer_initpara.alignedmode = TIMER_COUNTER_CENTER_UP;
  98. timer_initpara.period = half_period;
  99. timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
  100. timer_initpara.repetitioncounter = 0;
  101. timer_init(timer,&timer_initpara);
  102. /* auto-reload preload enable */
  103. timer_auto_reload_shadow_enable(timer);
  104. /* CH1,CH2 and CH3 configuration in PWM mode */
  105. timer_ocintpara.outputstate = TIMER_CCX_ENABLE;
  106. timer_ocintpara.outputnstate = TIMER_CCXN_ENABLE;
  107. timer_ocintpara.ocpolarity = TIMER_OC_POLARITY_LOW;
  108. timer_ocintpara.ocnpolarity = TIMER_OCN_POLARITY_LOW;
  109. timer_ocintpara.ocidlestate = TIMER_OC_IDLE_STATE_HIGH;
  110. timer_ocintpara.ocnidlestate = TIMER_OCN_IDLE_STATE_HIGH;
  111. timer_channel_output_config(timer,TIMER_CH_0,&timer_ocintpara);
  112. timer_channel_output_pulse_value_config(timer,TIMER_CH_0,half_period);
  113. timer_channel_output_mode_config(timer,TIMER_CH_0,TIMER_OC_MODE_PWM0);
  114. timer_channel_output_shadow_config(timer,TIMER_CH_0,TIMER_OC_SHADOW_ENABLE);
  115. timer_channel_output_config(timer,TIMER_CH_1,&timer_ocintpara);
  116. timer_channel_output_pulse_value_config(timer,TIMER_CH_1,half_period);
  117. timer_channel_output_mode_config(timer,TIMER_CH_1,TIMER_OC_MODE_PWM0);
  118. timer_channel_output_shadow_config(timer,TIMER_CH_1,TIMER_OC_SHADOW_ENABLE);
  119. timer_channel_output_config(timer,TIMER_CH_2,&timer_ocintpara);
  120. timer_channel_output_pulse_value_config(timer,TIMER_CH_2,half_period);
  121. timer_channel_output_mode_config(timer,TIMER_CH_2,TIMER_OC_MODE_PWM0);
  122. timer_channel_output_shadow_config(timer,TIMER_CH_2,TIMER_OC_SHADOW_ENABLE);
  123. timer_ocintpara.outputstate = TIMER_CCX_ENABLE;
  124. timer_ocintpara.outputnstate = TIMER_CCXN_DISABLE;
  125. timer_ocintpara.ocpolarity = TIMER_OC_POLARITY_HIGH;
  126. timer_ocintpara.ocnpolarity = TIMER_OCN_POLARITY_HIGH;
  127. timer_ocintpara.ocidlestate = TIMER_OC_IDLE_STATE_LOW;
  128. timer_ocintpara.ocnidlestate = TIMER_OCN_IDLE_STATE_LOW;
  129. /* chan3 trigger adc O3CPRE is alwary active high, adc trigger is rising */
  130. timer_channel_output_config(timer,TIMER_CH_3,&timer_ocintpara);
  131. timer_channel_output_pulse_value_config(timer,TIMER_CH_3,half_period-1);
  132. timer_channel_output_mode_config(timer,TIMER_CH_3,TIMER_OC_MODE_PWM1);
  133. timer_channel_output_shadow_config(timer,TIMER_CH_3,TIMER_OC_SHADOW_ENABLE);
  134. #if USER_ITMER_BRAKE==1
  135. timer_break_parameter_struct timer_breakpara;
  136. timer_breakpara.runoffstate = TIMER_ROS_STATE_ENABLE;
  137. timer_breakpara.ideloffstate = TIMER_IOS_STATE_ENABLE;
  138. timer_breakpara.protectmode = TIMER_CCHP_PROT_OFF;
  139. timer_breakpara.deadtime = NS_2_TCLK(TIMER_DT_NS);
  140. timer_breakpara.breakstate = TIMER_BREAK_ENABLE;
  141. timer_breakpara.breakpolarity = TIMER_BREAK_POLARITY_HIGH;
  142. timer_breakpara.outputautostate = TIMER_OUTAUTO_DISABLE;
  143. timer_break_config(TIMER0,&timer_breakpara);
  144. timer_interrupt_enable(timer, TIMER_INT_BRK);
  145. timer_interrupt_flag_clear(timer, TIMER_INT_FLAG_BRK);
  146. nvic_irq_enable(TIMER0_BRK_IRQn, 1, 0);
  147. #else
  148. _gpio_brakein_irq_enable();
  149. #endif
  150. #ifdef ENABLE_AUX_TIMER
  151. /* select the master slave mode */
  152. timer_master_slave_mode_config(timer,TIMER_MASTER_SLAVE_MODE_ENABLE);
  153. /* slave mode selection: enabled when time2 is enable*/
  154. timer_slave_mode_select(timer,TIMER_SLAVE_MODE_EVENT);
  155. timer_input_trigger_source_select(timer,TIMER_SMCFG_TRGSEL_ITI2);
  156. #endif
  157. #if SHUNT_NUM==ONE_SHUNT_SAMPLE
  158. timer_channel_output_shadow_config(timer,TIMER_CH_0,TIMER_OC_SHADOW_DISABLE);
  159. timer_channel_output_shadow_config(timer,TIMER_CH_1,TIMER_OC_SHADOW_DISABLE);
  160. timer_channel_output_shadow_config(timer,TIMER_CH_2,TIMER_OC_SHADOW_DISABLE);
  161. timer_dma_transfer_config(TIMER0, TIMER_DMACFG_DMATA_CH0CV, TIMER_DMACFG_DMATC_3TRANSFER);
  162. timer_dma_enable(TIMER0, TIMER_DMA_UPD);
  163. #else
  164. timer_master_slave_mode_config(timer,TIMER_MASTER_SLAVE_MODE_DISABLE);
  165. #endif /* ONE_SHUNT_SAMPLE_1 */
  166. pwm_enable_channel();
  167. timer_interrupt_disable(timer, TIMER_INT_UP);
  168. timer_interrupt_flag_clear(timer, TIMER_INT_FLAG_UP);
  169. nvic_irq_enable(TIMER0_UP_IRQn, TIMER_UP_IRQ_PRIORITY, 0);
  170. #if SHUNT_NUM==THREE_SHUNTS_SAMPLE
  171. timer_enable(timer);
  172. #endif
  173. #ifdef GD32_DEMO
  174. /* IR2136S enable */
  175. gpio_bit_set(GPIOA, GPIO_PIN_12);
  176. #endif
  177. }
  178. #ifdef ENABLE_AUX_TIMER
  179. static void _init_adc_timer(void) {
  180. timer_oc_parameter_struct timer_ocintpara;
  181. timer_parameter_struct timer_initpara;
  182. u32 timer = adc_timer;
  183. u32 half_period = FOC_PWM_Half_Period;
  184. rcu_periph_clock_enable(_rcu_clk(timer));
  185. timer_deinit(timer);
  186. memset(&timer_initpara, 0, sizeof(timer_initpara));
  187. memset(&timer_ocintpara, 0, sizeof(timer_ocintpara));
  188. timer_initpara.prescaler = 0;
  189. timer_initpara.alignedmode = TIMER_COUNTER_CENTER_UP;
  190. timer_initpara.period = half_period;
  191. timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
  192. timer_initpara.repetitioncounter = 0;
  193. timer_init(timer,&timer_initpara);
  194. timer_ocintpara.outputstate = TIMER_CCX_ENABLE;
  195. timer_ocintpara.outputnstate = TIMER_CCXN_DISABLE;
  196. timer_ocintpara.ocpolarity = TIMER_OC_POLARITY_HIGH;
  197. timer_ocintpara.ocnpolarity = TIMER_OCN_POLARITY_HIGH;
  198. timer_ocintpara.ocidlestate = TIMER_OC_IDLE_STATE_LOW;
  199. timer_ocintpara.ocnidlestate = TIMER_OCN_IDLE_STATE_LOW;
  200. timer_channel_output_config(timer,TIMER_CH_0,&timer_ocintpara);
  201. timer_channel_output_pulse_value_config(timer,TIMER_CH_0,half_period-1);
  202. #if SHUNT_NUM==ONE_SHUNT_SAMPLE
  203. timer_channel_output_mode_config(timer,TIMER_CH_0,TIMER_OC_MODE_PWM1);
  204. #else
  205. timer_channel_output_mode_config(timer,TIMER_CH_0,TIMER_OC_MODE_PWM0);
  206. #endif
  207. timer_channel_output_shadow_config(timer,TIMER_CH_0,TIMER_OC_SHADOW_ENABLE);
  208. /* select the master slave mode */
  209. timer_master_slave_mode_config(timer,TIMER_MASTER_SLAVE_MODE_ENABLE);
  210. /* slave mode selection: enabled when time2 is enable*/
  211. timer_slave_mode_select(timer,TIMER_SLAVE_MODE_EVENT);
  212. timer_input_trigger_source_select(timer,TIMER_SMCFG_TRGSEL_ITI2);
  213. //timer_primary_output_config(timer,ENABLE);
  214. /* auto-reload preload enable */
  215. timer_auto_reload_shadow_enable(timer);
  216. //timer_enable(timer);
  217. }
  218. static void _init_aux_timer(void) {
  219. timer_parameter_struct timer_initpara;
  220. u32 timer = aux_timer;
  221. rcu_periph_clock_enable(_rcu_clk(timer));
  222. timer_deinit(timer);
  223. memset(&timer_initpara, 0, sizeof(timer_initpara));
  224. timer_initpara.prescaler = 0;
  225. timer_initpara.alignedmode = TIMER_COUNTER_CENTER_UP;
  226. timer_initpara.period = FOC_PWM_period;
  227. timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
  228. timer_initpara.repetitioncounter = 0;
  229. timer_init(timer,&timer_initpara);
  230. /* select the master slave mode */
  231. timer_master_slave_mode_config(timer,TIMER_MASTER_SLAVE_MODE_ENABLE);
  232. /* timer update event is used as trigger output */
  233. timer_master_output_trigger_source_select(timer,TIMER_TRI_OUT_SRC_ENABLE);
  234. timer_primary_output_config(timer,ENABLE);
  235. timer_enable(timer);
  236. }
  237. #endif
  238. #if SHUNT_NUM==ONE_SHUNT_SAMPLE
  239. static void timer0_dma_config(void)
  240. {
  241. dma_parameter_struct dma_init_struct;
  242. rcu_periph_clock_enable(RCU_DMA0);
  243. /* TIMER0 update */
  244. dma_deinit(DMA0, DMA_CH4);
  245. dma_init_struct.direction = DMA_MEMORY_TO_PERIPHERAL;
  246. dma_init_struct.memory_addr = (uint32_t)timer_update_buffer;
  247. dma_init_struct.memory_inc = DMA_MEMORY_INCREASE_ENABLE;
  248. dma_init_struct.memory_width = DMA_MEMORY_WIDTH_16BIT;
  249. dma_init_struct.number = 6;
  250. dma_init_struct.periph_addr = (uint32_t)0x40012C4C;
  251. dma_init_struct.periph_inc = DMA_PERIPH_INCREASE_DISABLE;
  252. dma_init_struct.periph_width = DMA_PERIPHERAL_WIDTH_16BIT;
  253. dma_init_struct.priority = DMA_PRIORITY_ULTRA_HIGH;
  254. dma_init(DMA0, DMA_CH4, &dma_init_struct);
  255. dma_circulation_enable(DMA0, DMA_CH4);
  256. dma_memory_to_memory_disable(DMA0, DMA_CH4);
  257. }
  258. #endif
  259. #if USER_ITMER_BRAKE==0
  260. static void _gpio_brakein_irq_enable(void){
  261. gpio_exti_source_select(GPIO_PORT_SOURCE_GPIOB, GPIO_PIN_SOURCE_4);
  262. exti_init(EXTI_4, EXTI_INTERRUPT, EXTI_TRIG_BOTH);
  263. nvic_irq_enable(EXTI4_IRQn, 4U, 0U);
  264. exti_interrupt_flag_clear(EXTI_4);
  265. exti_interrupt_enable(EXTI_4);
  266. gpio_exti_source_select(GPIO_PORT_SOURCE_GPIOB, GPIO_PIN_SOURCE_5);
  267. exti_init(EXTI_5, EXTI_INTERRUPT, EXTI_TRIG_BOTH);
  268. nvic_irq_enable(EXTI5_9_IRQn, 4U, 0U);
  269. exti_interrupt_flag_clear(EXTI_5);
  270. exti_interrupt_enable(EXTI_5);
  271. }
  272. #endif
  273. void pwm_start(void){
  274. #if SHUNT_NUM==THREE_SHUNTS_SAMPLE
  275. pwm_update_duty(FOC_PWM_Half_Period/2, FOC_PWM_Half_Period/2, FOC_PWM_Half_Period/2);
  276. pwm_update_2smaples(FOC_PWM_Half_Period-1, FOC_PWM_Half_Period + 1);
  277. #else
  278. pwm_update_duty_dma(0,0,0,0,0,0);
  279. dma_channel_enable(DMA0, DMA_CH4);
  280. pwm_update_2smaples(FOC_PWM_Half_Period-(2 * TADC + TRise), FOC_PWM_Half_Period-TADC);
  281. #endif
  282. /* wait for a new PWM period to flush last HF task */
  283. timer_flag_clear(pwm_timer, TIMER_FLAG_UP);
  284. timer_event_software_generate(pwm_timer, TIMER_EVENT_SRC_UPG);
  285. while ( timer_flag_get(pwm_timer, TIMER_FLAG_UP) == RESET ){}
  286. /* Clear Update Flag */
  287. timer_flag_clear(pwm_timer, TIMER_FLAG_UP);
  288. timer_primary_output_config(pwm_timer,ENABLE);
  289. #ifdef ENABLE_AUX_TIMER
  290. timer_primary_output_config(adc_timer,ENABLE);
  291. #endif
  292. timer_flag_clear(pwm_timer, TIMER_FLAG_UP);
  293. timer_interrupt_enable(pwm_timer, TIMER_INT_UP);
  294. }
  295. void pwm_stop(void){
  296. dma_channel_disable(DMA0, DMA_CH4);
  297. timer_primary_output_config(pwm_timer,DISABLE);
  298. #ifdef ENABLE_AUX_TIMER
  299. timer_primary_output_config(adc_timer,DISABLE);
  300. #endif
  301. //timer_interrupt_disable(pwm_timer, TIMER_INT_UP);
  302. /* wait for a new PWM period to flush last HF task */
  303. timer_flag_clear(pwm_timer, TIMER_FLAG_UP);
  304. //while ( timer_flag_get(pwm_timer, TIMER_FLAG_UP) == RESET ){}
  305. /* Clear Update Flag */
  306. timer_flag_clear(pwm_timer, TIMER_FLAG_UP);
  307. }
  308. void pwm_enable_output(bool enable) {
  309. if (enable) {
  310. timer_primary_output_config(pwm_timer,ENABLE);
  311. }else {
  312. timer_primary_output_config(pwm_timer,DISABLE);
  313. }
  314. }
  315. /*open low side of the mosfet*/
  316. void pwm_turn_on_low_side(void)
  317. {
  318. #if SHUNT_NUM==THREE_SHUNTS_SAMPLE
  319. pwm_update_duty(0, 0, 0);
  320. pwm_update_2smaples(FOC_PWM_Half_Period-1, FOC_PWM_Half_Period + 1);
  321. #else
  322. pwm_update_duty_dma(0,0,0,0,0,0);
  323. dma_channel_enable(DMA0, DMA_CH4);
  324. pwm_update_2smaples(FOC_PWM_Half_Period-(2 * TADC + TRise), FOC_PWM_Half_Period-TADC);
  325. #endif
  326. timer_flag_clear(pwm_timer,TIMER_FLAG_UP);
  327. timer_event_software_generate(pwm_timer, TIMER_EVENT_SRC_UPG);
  328. while (timer_flag_get(pwm_timer,TIMER_FLAG_UP) == RESET );
  329. /* Main PWM Output Enable */
  330. timer_primary_output_config(pwm_timer, ENABLE);
  331. #ifdef ENABLE_AUX_TIMER
  332. timer_primary_output_config(adc_timer, ENABLE);
  333. #endif
  334. }
  335. void pwm_update_sample(u32 samp1, u32 samp2, u8 sector) {
  336. pwm_update_2smaples(samp1, samp2);
  337. #ifdef ENABLE_AUX_TIMER
  338. if (samp2 < FOC_PWM_Half_Period) {
  339. adc_update_ext_trigger(ADC_TRIGGER_PHASE);
  340. }else {
  341. adc_update_ext_trigger(ADC_TRIGGER_PHASE2);
  342. }
  343. #endif
  344. adc_current_sample_config(sector);
  345. }
  346. __weak void foc_brake_handler(bool brake) {
  347. //dumy function, must implemented in FOC controller
  348. }
  349. /*do 50 times filter*/
  350. static void brake_timer_handler(shark_timer_t *t) {
  351. int count = 50;
  352. int settimes = 0;
  353. while(count-- >= 0) {
  354. bool b1 = gpio_input_bit_get(GPIOB, GPIO_PIN_4) == SET;
  355. bool b2 = gpio_input_bit_get(GPIOB, GPIO_PIN_5) == SET;
  356. if (b1 && b2) {
  357. settimes++;
  358. }
  359. }
  360. if (settimes == 0) {
  361. foc_brake_handler(true);
  362. }else if (settimes == 50) {
  363. foc_brake_handler(false);
  364. }else {
  365. //有干扰,do nothing
  366. }
  367. }
  368. static shark_timer_t _brake_timer = TIMER_INIT(_brake_timer, brake_timer_handler);
  369. void mc_brake_irq(void) {
  370. shark_timer_post(&_brake_timer, 0);
  371. }