main.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*!
  2. \file main.c
  3. \brief TIMERs cascade synchro demo for gd32f30x
  4. \version 2017-02-10, V1.0.0, firmware for GD32F30x
  5. \version 2018-10-10, V1.1.0, firmware for GD32F30x
  6. \version 2018-12-25, V2.0.0, firmware for GD32F30x
  7. \version 2020-09-30, V2.1.0, firmware for GD32F30x
  8. */
  9. /*
  10. Copyright (c) 2020, GigaDevice Semiconductor Inc.
  11. Redistribution and use in source and binary forms, with or without modification,
  12. are permitted provided that the following conditions are met:
  13. 1. Redistributions of source code must retain the above copyright notice, this
  14. list of conditions and the following disclaimer.
  15. 2. Redistributions in binary form must reproduce the above copyright notice,
  16. this list of conditions and the following disclaimer in the documentation
  17. and/or other materials provided with the distribution.
  18. 3. Neither the name of the copyright holder nor the names of its contributors
  19. may be used to endorse or promote products derived from this software without
  20. specific prior written permission.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  22. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  23. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  24. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  25. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  26. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  27. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  28. WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  30. OF SUCH DAMAGE.
  31. */
  32. #include "gd32f30x.h"
  33. #include <stdio.h>
  34. #include "gd32f307c_eval.h"
  35. void gpio_config(void);
  36. void timer_config(void);
  37. /*!
  38. \brief configure the GPIO ports
  39. \param[in] none
  40. \param[out] none
  41. \retval none
  42. */
  43. void gpio_config(void)
  44. {
  45. rcu_periph_clock_enable(RCU_GPIOA);
  46. rcu_periph_clock_enable(RCU_AF);
  47. /*configure PA6(TIMER2 CH0) as alternate function*/
  48. gpio_init(GPIOA,GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_6);
  49. /*configure PA1(TIMER1 CH1) as alternate function*/
  50. gpio_init(GPIOA,GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_1);
  51. /*configure PA8(TIMER0 CH0) as alternate function*/
  52. gpio_init(GPIOA,GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_8);
  53. }
  54. /*!
  55. \brief configure the TIMER peripheral
  56. \param[in] none
  57. \param[out] none
  58. \retval none
  59. */
  60. void timer_config(void)
  61. {
  62. /* timers synchronisation in cascade mode ----------------------------
  63. 1/TIMER1 is configured as master timer:
  64. - PWM mode is used
  65. - The TIMER1 update event is used as trigger output
  66. 2/TIMER2 is slave for TIMER1 and master for TIMER0,
  67. - PWM mode is used
  68. - The ITR1(TIMER1) is used as input trigger
  69. - external clock mode is used,the counter counts on the rising edges of
  70. the selected trigger.
  71. - the TIMER2 update event is used as trigger output.
  72. 3/TIMER0 is slave for TIMER2,
  73. - PWM mode is used
  74. - The ITR2(TIMER2) is used as input trigger
  75. - external clock mode is used,the counter counts on the rising edges of
  76. the selected trigger.
  77. -------------------------------------------------------------------- */
  78. timer_oc_parameter_struct timer_ocintpara;
  79. timer_parameter_struct timer_initpara;
  80. rcu_periph_clock_enable(RCU_TIMER0);
  81. rcu_periph_clock_enable(RCU_TIMER1);
  82. rcu_periph_clock_enable(RCU_TIMER2);
  83. /* TIMER1 configuration */
  84. timer_deinit(TIMER1);
  85. timer_initpara.prescaler = 5999;
  86. timer_initpara.alignedmode = TIMER_COUNTER_EDGE;
  87. timer_initpara.counterdirection = TIMER_COUNTER_UP;
  88. timer_initpara.period = 3999;
  89. timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
  90. timer_initpara.repetitioncounter = 0;
  91. timer_init(TIMER1,&timer_initpara);
  92. /* CH1 configuration in PWM0 mode */
  93. timer_ocintpara.outputstate = TIMER_CCX_ENABLE;
  94. timer_ocintpara.outputnstate = TIMER_CCXN_DISABLE;
  95. timer_ocintpara.ocpolarity = TIMER_OC_POLARITY_HIGH;
  96. timer_ocintpara.ocnpolarity = TIMER_OCN_POLARITY_HIGH;
  97. timer_ocintpara.ocidlestate = TIMER_OC_IDLE_STATE_LOW;
  98. timer_ocintpara.ocnidlestate = TIMER_OCN_IDLE_STATE_LOW;
  99. timer_channel_output_config(TIMER1,TIMER_CH_1,&timer_ocintpara);
  100. timer_channel_output_pulse_value_config(TIMER1,TIMER_CH_1,1999);
  101. timer_channel_output_mode_config(TIMER1,TIMER_CH_1,TIMER_OC_MODE_PWM0);
  102. timer_channel_output_shadow_config(TIMER1,TIMER_CH_1,TIMER_OC_SHADOW_DISABLE);
  103. /* auto-reload preload enable */
  104. timer_auto_reload_shadow_enable(TIMER1);
  105. /* select the master slave mode */
  106. timer_master_slave_mode_config(TIMER1,TIMER_MASTER_SLAVE_MODE_ENABLE);
  107. /* TIMER1 update event is used as trigger output */
  108. timer_master_output_trigger_source_select(TIMER1,TIMER_TRI_OUT_SRC_UPDATE);
  109. /* TIMER2 configuration */
  110. timer_deinit(TIMER2);
  111. timer_initpara.prescaler = 0;
  112. timer_initpara.alignedmode = TIMER_COUNTER_EDGE;
  113. timer_initpara.counterdirection = TIMER_COUNTER_UP;
  114. timer_initpara.period = 1;
  115. timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
  116. timer_initpara.repetitioncounter = 0;
  117. timer_init(TIMER2,&timer_initpara);
  118. /* CH0 configuration in PWM0 mode */
  119. timer_ocintpara.outputstate = TIMER_CCX_ENABLE;
  120. timer_ocintpara.outputnstate = TIMER_CCXN_DISABLE;
  121. timer_ocintpara.ocpolarity = TIMER_OC_POLARITY_HIGH;
  122. timer_ocintpara.ocnpolarity = TIMER_OCN_POLARITY_HIGH;
  123. timer_ocintpara.ocidlestate = TIMER_OC_IDLE_STATE_LOW;
  124. timer_ocintpara.ocnidlestate = TIMER_OCN_IDLE_STATE_LOW;
  125. timer_channel_output_config(TIMER2,TIMER_CH_0,&timer_ocintpara);
  126. timer_channel_output_pulse_value_config(TIMER2,TIMER_CH_0,1);
  127. timer_channel_output_mode_config(TIMER2,TIMER_CH_0,TIMER_OC_MODE_PWM0);
  128. timer_channel_output_shadow_config(TIMER2,TIMER_CH_0,TIMER_OC_SHADOW_DISABLE);
  129. /* auto-reload preload enable */
  130. timer_auto_reload_shadow_enable(TIMER2);
  131. /* slave mode selection: TIMER2 */
  132. timer_slave_mode_select(TIMER2,TIMER_SLAVE_MODE_EXTERNAL0);
  133. timer_input_trigger_source_select(TIMER2,TIMER_SMCFG_TRGSEL_ITI1);
  134. /* select the master slave mode */
  135. timer_master_slave_mode_config(TIMER2,TIMER_MASTER_SLAVE_MODE_ENABLE);
  136. /* TIMER2 update event is used as trigger output */
  137. timer_master_output_trigger_source_select(TIMER2,TIMER_TRI_OUT_SRC_UPDATE);
  138. /* TIMER0 configuration */
  139. timer_deinit(TIMER0);
  140. timer_initpara.prescaler = 0;
  141. timer_initpara.alignedmode = TIMER_COUNTER_EDGE;
  142. timer_initpara.counterdirection = TIMER_COUNTER_UP;
  143. timer_initpara.period = 1;
  144. timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
  145. timer_initpara.repetitioncounter = 0;
  146. timer_init(TIMER0,&timer_initpara);
  147. /* CH0 configuration in PWM0 mode */
  148. timer_ocintpara.outputstate = TIMER_CCX_ENABLE;
  149. timer_ocintpara.outputnstate = TIMER_CCXN_DISABLE;
  150. timer_ocintpara.ocpolarity = TIMER_OC_POLARITY_HIGH;
  151. timer_ocintpara.ocnpolarity = TIMER_OCN_POLARITY_HIGH;
  152. timer_ocintpara.ocidlestate = TIMER_OC_IDLE_STATE_LOW;
  153. timer_ocintpara.ocnidlestate = TIMER_OCN_IDLE_STATE_LOW;
  154. timer_channel_output_config(TIMER0,TIMER_CH_0,&timer_ocintpara);
  155. timer_channel_output_pulse_value_config(TIMER0,TIMER_CH_0,1);
  156. timer_channel_output_mode_config(TIMER0,TIMER_CH_0,TIMER_OC_MODE_PWM0);
  157. timer_channel_output_shadow_config(TIMER0,TIMER_CH_0,TIMER_OC_SHADOW_DISABLE);
  158. /* auto-reload preload enable */
  159. timer_auto_reload_shadow_enable(TIMER0);
  160. /* TIMER0 output enable */
  161. timer_primary_output_config(TIMER0,ENABLE);
  162. /* slave mode selection: TIMER0 */
  163. timer_slave_mode_select(TIMER0,TIMER_SLAVE_MODE_EXTERNAL0);
  164. timer_input_trigger_source_select(TIMER0,TIMER_SMCFG_TRGSEL_ITI2);
  165. /* TIMER counter enable */
  166. timer_enable(TIMER1);
  167. timer_enable(TIMER2);
  168. timer_enable(TIMER0);
  169. }
  170. /*!
  171. \brief main function
  172. \param[in] none
  173. \param[out] none
  174. \retval none
  175. */
  176. int main(void)
  177. {
  178. gpio_config();
  179. timer_config();
  180. while (1);
  181. }