main.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*!
  2. \file main.c
  3. \brief TIMER0 6-steps 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. #include "systick.h"
  36. void gpio_config(void);
  37. void timer_config(void);
  38. void nvic_config(void);
  39. /*!
  40. \brief configure the GPIO ports
  41. \param[in] none
  42. \param[out] none
  43. \retval none
  44. */
  45. void gpio_config(void)
  46. {
  47. rcu_periph_clock_enable(RCU_GPIOA);
  48. rcu_periph_clock_enable(RCU_GPIOB);
  49. rcu_periph_clock_enable(RCU_AF);
  50. /*configure PA8 PA9 PA10(TIMER0 CH0 CH1 CH2) as alternate function*/
  51. gpio_init(GPIOA,GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_8);
  52. gpio_init(GPIOA,GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_9);
  53. gpio_init(GPIOA,GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_10);
  54. /*configure PB13 PB14 PB15(TIMER0 CH0N CH1N CH2N) as alternate function*/
  55. gpio_init(GPIOB,GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_13);
  56. gpio_init(GPIOB,GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_14);
  57. gpio_init(GPIOB,GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_15);
  58. /*configure PB12(TIMER0 BKIN) as alternate function*/
  59. gpio_init(GPIOB,GPIO_MODE_IN_FLOATING,GPIO_OSPEED_50MHZ,GPIO_PIN_12);
  60. }
  61. /*!
  62. \brief configure the nested vectored interrupt controller
  63. \param[in] none
  64. \param[out] none
  65. \retval none
  66. */
  67. void nvic_config(void)
  68. {
  69. #ifndef GD32F30X_HD
  70. nvic_irq_enable(TIMER0_TRG_CMT_TIMER10_IRQn, 0, 1);
  71. #else
  72. nvic_irq_enable(TIMER0_TRG_CMT_IRQn, 0, 1);
  73. #endif
  74. }
  75. /*!
  76. \brief configure the TIMER peripheral
  77. \param[in] none
  78. \param[out] none
  79. \retval none
  80. */
  81. void timer_config(void)
  82. {
  83. /* -----------------------------------------------------------------------
  84. TIMER0 configuration:
  85. generate 3 complementary PWM signal.
  86. TIMER0CLK is fixed to systemcoreclock, the TIMER0 prescaler is equal to 119
  87. so the TIMER0 counter clock used is 1MHz.
  88. insert a dead time equal to 200/systemcoreclock =1.67us
  89. configure the break feature, active at low level, and using the automatic
  90. output enable feature.
  91. use the locking parameters level 0.
  92. ----------------------------------------------------------------------- */
  93. timer_oc_parameter_struct timer_ocintpara;
  94. timer_parameter_struct timer_initpara;
  95. timer_break_parameter_struct timer_breakpara;
  96. rcu_periph_clock_enable(RCU_TIMER0);
  97. timer_deinit(TIMER0);
  98. /* TIMER0 configuration */
  99. timer_initpara.prescaler = 119;
  100. timer_initpara.alignedmode = TIMER_COUNTER_EDGE;
  101. timer_initpara.counterdirection = TIMER_COUNTER_UP;
  102. timer_initpara.period = 599;
  103. timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
  104. timer_initpara.repetitioncounter = 0;
  105. timer_init(TIMER0,&timer_initpara);
  106. /* CH0/CH0N,CH1/CH1N and CH2/CH2N configuration in timing mode */
  107. timer_ocintpara.outputstate = TIMER_CCX_ENABLE;
  108. timer_ocintpara.outputnstate = TIMER_CCXN_ENABLE;
  109. timer_ocintpara.ocpolarity = TIMER_OC_POLARITY_HIGH;
  110. timer_ocintpara.ocnpolarity = TIMER_OCN_POLARITY_HIGH;
  111. timer_ocintpara.ocidlestate = TIMER_OC_IDLE_STATE_HIGH;
  112. timer_ocintpara.ocnidlestate = TIMER_OCN_IDLE_STATE_HIGH;
  113. timer_channel_output_config(TIMER0,TIMER_CH_0,&timer_ocintpara);
  114. timer_channel_output_config(TIMER0,TIMER_CH_1,&timer_ocintpara);
  115. timer_channel_output_config(TIMER0,TIMER_CH_2,&timer_ocintpara);
  116. timer_channel_output_pulse_value_config(TIMER0,TIMER_CH_0,299);
  117. timer_channel_output_mode_config(TIMER0,TIMER_CH_0,TIMER_OC_MODE_TIMING);
  118. timer_channel_output_shadow_config(TIMER0,TIMER_CH_0,TIMER_OC_SHADOW_ENABLE);
  119. timer_channel_output_pulse_value_config(TIMER0,TIMER_CH_1,299);
  120. timer_channel_output_mode_config(TIMER0,TIMER_CH_1,TIMER_OC_MODE_TIMING);
  121. timer_channel_output_shadow_config(TIMER0,TIMER_CH_1,TIMER_OC_SHADOW_ENABLE);
  122. timer_channel_output_pulse_value_config(TIMER0,TIMER_CH_2,299);
  123. timer_channel_output_mode_config(TIMER0,TIMER_CH_2,TIMER_OC_MODE_TIMING);
  124. timer_channel_output_shadow_config(TIMER0,TIMER_CH_2,TIMER_OC_SHADOW_ENABLE);
  125. /* automatic output enable, break, dead time and lock configuration*/
  126. timer_breakpara.runoffstate = TIMER_ROS_STATE_ENABLE;
  127. timer_breakpara.ideloffstate = TIMER_IOS_STATE_ENABLE ;
  128. timer_breakpara.deadtime = 164;
  129. timer_breakpara.breakpolarity = TIMER_BREAK_POLARITY_LOW;
  130. timer_breakpara.outputautostate = TIMER_OUTAUTO_ENABLE;
  131. timer_breakpara.protectmode = TIMER_CCHP_PROT_OFF;
  132. timer_breakpara.breakstate = TIMER_BREAK_DISABLE;
  133. timer_break_config(TIMER0,&timer_breakpara);
  134. /* TIMER0 primary output function enable */
  135. timer_primary_output_config(TIMER0,ENABLE);
  136. /* TIMER0 channel control update interrupt enable */
  137. timer_interrupt_enable(TIMER0,TIMER_INT_CMT);
  138. /* TIMER0 break interrupt disable */
  139. timer_interrupt_disable(TIMER0,TIMER_INT_BRK);
  140. /* TIMER0 counter enable */
  141. timer_enable(TIMER0);
  142. }
  143. /*!
  144. \brief main function
  145. \param[in] none
  146. \param[out] none
  147. \retval none
  148. */
  149. int main(void)
  150. {
  151. gpio_config();
  152. systick_config();
  153. nvic_config();
  154. timer_config();
  155. while (1);
  156. }