main.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*!
  2. \file main.c
  3. \brief TIMERs cascade synchro demo
  4. */
  5. /*
  6. Copyright (C) 2017 GigaDevice
  7. 2017-06-06, V1.0.0, firmware for GD32F3x0
  8. */
  9. #include "gd32f3x0.h"
  10. #include <stdio.h>
  11. #include "gd32f3x0_eval.h"
  12. void gpio_config(void);
  13. void timer_config(void);
  14. /*!
  15. \brief configure the GPIO ports
  16. \param[in] none
  17. \param[out] none
  18. \retval none
  19. */
  20. void gpio_config(void)
  21. {
  22. rcu_periph_clock_enable(RCU_GPIOA);
  23. rcu_periph_clock_enable(RCU_GPIOB);
  24. /*configure PA6(TIMER2 CH0) as alternate function*/
  25. gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_6);
  26. gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_6);
  27. gpio_af_set(GPIOA, GPIO_AF_1, GPIO_PIN_6);
  28. /*configure PB3(TIMER1 CH1) as alternate function*/
  29. gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_3);
  30. gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_3);
  31. gpio_af_set(GPIOB, GPIO_AF_2, GPIO_PIN_3);
  32. /*configure PA8(TIMER0 CH0) as alternate function*/
  33. gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_8);
  34. gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_8);
  35. gpio_af_set(GPIOA, GPIO_AF_2, GPIO_PIN_8);
  36. }
  37. /*!
  38. \brief configure the TIMER peripheral
  39. \param[in] none
  40. \param[out] none
  41. \retval none
  42. */
  43. void timer_config(void)
  44. {
  45. /* timers synchronisation in cascade mode ----------------------------
  46. 1/TIMER1 is configured as master timer:
  47. - PWM mode is used
  48. - The TIMER1 update event is used as trigger output
  49. 2/TIMER2 is slave for TIMER1 and master for TIMER0,
  50. - PWM mode is used
  51. - The ITI1(TIMER1) is used as input trigger
  52. - external clock mode is used,the counter counts on the rising edges of
  53. the selected trigger.
  54. - the TIMER2 update event is used as trigger output.
  55. 3/TIMER0 is slave for TIMER2,
  56. - PWM mode is used
  57. - The ITI2(TIMER2) is used as input trigger
  58. - external clock mode is used,the counter counts on the rising edges of
  59. the selected trigger.
  60. -------------------------------------------------------------------- */
  61. timer_oc_parameter_struct timer_ocintpara;
  62. timer_parameter_struct timer_initpara;
  63. rcu_periph_clock_enable(RCU_TIMER0);
  64. rcu_periph_clock_enable(RCU_TIMER1);
  65. rcu_periph_clock_enable(RCU_TIMER2);
  66. /* TIMER1 configuration */
  67. timer_deinit(TIMER1);
  68. #ifdef GD32F330
  69. timer_initpara.prescaler = 41999;
  70. #endif /* GD32F330 */
  71. #ifdef GD32F350
  72. timer_initpara.prescaler = 53999;
  73. #endif /* GD32F350 */
  74. timer_initpara.alignedmode = TIMER_COUNTER_EDGE;
  75. timer_initpara.counterdirection = TIMER_COUNTER_UP;
  76. timer_initpara.period = 3999;
  77. timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
  78. timer_initpara.repetitioncounter = 0;
  79. timer_init(TIMER1,&timer_initpara);
  80. /* CH1 configuration in PWM1 mode */
  81. timer_ocintpara.outputstate = TIMER_CCX_ENABLE;
  82. timer_ocintpara.outputnstate = TIMER_CCXN_DISABLE;
  83. timer_ocintpara.ocpolarity = TIMER_OC_POLARITY_HIGH;
  84. timer_ocintpara.ocnpolarity = TIMER_OCN_POLARITY_HIGH;
  85. timer_ocintpara.ocidlestate = TIMER_OC_IDLE_STATE_LOW;
  86. timer_ocintpara.ocnidlestate = TIMER_OCN_IDLE_STATE_LOW;
  87. timer_channel_output_config(TIMER1,TIMER_CH_1,&timer_ocintpara);
  88. timer_channel_output_pulse_value_config(TIMER1,TIMER_CH_1,1999);
  89. timer_channel_output_mode_config(TIMER1,TIMER_CH_1,TIMER_OC_MODE_PWM1);
  90. timer_channel_output_shadow_config(TIMER1,TIMER_CH_1,TIMER_OC_SHADOW_DISABLE);
  91. /* auto-reload preload enable */
  92. timer_auto_reload_shadow_enable(TIMER1);
  93. /* select the master slave mode */
  94. timer_master_slave_mode_config(TIMER1,TIMER_MASTER_SLAVE_MODE_ENABLE);
  95. /* TIMER1 update event is used as trigger output */
  96. timer_master_output_trigger_source_select(TIMER1,TIMER_TRI_OUT_SRC_UPDATE);
  97. /* TIMER2 configuration */
  98. timer_deinit(TIMER2);
  99. timer_initpara.prescaler = 0;
  100. timer_initpara.alignedmode = TIMER_COUNTER_EDGE;
  101. timer_initpara.counterdirection = TIMER_COUNTER_UP;
  102. timer_initpara.period = 1;
  103. timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
  104. timer_initpara.repetitioncounter = 0;
  105. timer_init(TIMER2,&timer_initpara);
  106. /* CH0 configuration in PWM1 mode */
  107. timer_ocintpara.outputstate = TIMER_CCX_ENABLE;
  108. timer_ocintpara.outputnstate = TIMER_CCXN_DISABLE;
  109. timer_ocintpara.ocpolarity = TIMER_OC_POLARITY_HIGH;
  110. timer_ocintpara.ocnpolarity = TIMER_OCN_POLARITY_HIGH;
  111. timer_ocintpara.ocidlestate = TIMER_OC_IDLE_STATE_LOW;
  112. timer_ocintpara.ocnidlestate = TIMER_OCN_IDLE_STATE_LOW;
  113. timer_channel_output_config(TIMER2,TIMER_CH_0,&timer_ocintpara);
  114. timer_channel_output_pulse_value_config(TIMER2,TIMER_CH_0,1);
  115. timer_channel_output_mode_config(TIMER2,TIMER_CH_0,TIMER_OC_MODE_PWM1);
  116. timer_channel_output_shadow_config(TIMER2,TIMER_CH_0,TIMER_OC_SHADOW_DISABLE);
  117. /* auto-reload preload enable */
  118. timer_auto_reload_shadow_enable(TIMER2);
  119. /* slave mode selection: TIMER2 */
  120. timer_slave_mode_select(TIMER2,TIMER_SLAVE_MODE_EXTERNAL0);
  121. timer_input_trigger_source_select(TIMER2,TIMER_SMCFG_TRGSEL_ITI1);
  122. /* select the master slave mode */
  123. timer_master_slave_mode_config(TIMER2,TIMER_MASTER_SLAVE_MODE_ENABLE);
  124. /* TIMER2 update event is used as trigger output */
  125. timer_master_output_trigger_source_select(TIMER2,TIMER_TRI_OUT_SRC_UPDATE);
  126. /* TIMER0 configuration */
  127. timer_deinit(TIMER0);
  128. timer_initpara.prescaler = 0;
  129. timer_initpara.alignedmode = TIMER_COUNTER_EDGE;
  130. timer_initpara.counterdirection = TIMER_COUNTER_UP;
  131. timer_initpara.period = 1;
  132. timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
  133. timer_initpara.repetitioncounter = 0;
  134. timer_init(TIMER0,&timer_initpara);
  135. /* CH0 configuration in PWM1 mode */
  136. timer_ocintpara.outputstate = TIMER_CCX_ENABLE;
  137. timer_ocintpara.outputnstate = TIMER_CCXN_DISABLE;
  138. timer_ocintpara.ocpolarity = TIMER_OC_POLARITY_HIGH;
  139. timer_ocintpara.ocnpolarity = TIMER_OCN_POLARITY_HIGH;
  140. timer_ocintpara.ocidlestate = TIMER_OC_IDLE_STATE_LOW;
  141. timer_ocintpara.ocnidlestate = TIMER_OCN_IDLE_STATE_LOW;
  142. timer_channel_output_config(TIMER0,TIMER_CH_0,&timer_ocintpara);
  143. timer_channel_output_pulse_value_config(TIMER0,TIMER_CH_0,1);
  144. timer_channel_output_mode_config(TIMER0,TIMER_CH_0,TIMER_OC_MODE_PWM1);
  145. timer_channel_output_shadow_config(TIMER0,TIMER_CH_0,TIMER_OC_SHADOW_DISABLE);
  146. /* auto-reload preload enable */
  147. timer_auto_reload_shadow_enable(TIMER0);
  148. /* TIMER0 output enable */
  149. timer_primary_output_config(TIMER0,ENABLE);
  150. /* slave mode selection: TIMER0 */
  151. timer_slave_mode_select(TIMER0,TIMER_SLAVE_MODE_EXTERNAL0);
  152. timer_input_trigger_source_select(TIMER0,TIMER_SMCFG_TRGSEL_ITI2);
  153. /* TIMER counter enable */
  154. timer_enable(TIMER1);
  155. timer_enable(TIMER2);
  156. timer_enable(TIMER0);
  157. }
  158. /*!
  159. \brief main function
  160. \param[in] none
  161. \param[out] none
  162. \retval none
  163. */
  164. int main(void)
  165. {
  166. gpio_config();
  167. timer_config();
  168. while (1);
  169. }