main.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*!
  2. \file main.c
  3. \brief TIMER0 dma 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. #define TIMER0_CH0CV ((uint32_t)0x40012C34)
  36. uint16_t buffer[3]={249,499,749};
  37. void gpio_config(void);
  38. void timer_config(void);
  39. void dma_config(void);
  40. /*!
  41. \brief configure the GPIO ports
  42. \param[in] none
  43. \param[out] none
  44. \retval none
  45. */
  46. void gpio_config(void)
  47. {
  48. rcu_periph_clock_enable(RCU_GPIOA);
  49. rcu_periph_clock_enable(RCU_AF);
  50. /*configure PA8(TIMER0 CH0) as alternate function*/
  51. gpio_init(GPIOA,GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_8);
  52. }
  53. /*!
  54. \brief configure the DMA peripheral
  55. \param[in] none
  56. \param[out] none
  57. \retval none
  58. */
  59. void dma_config(void)
  60. {
  61. dma_parameter_struct dma_init_struct;
  62. /* enable DMA clock */
  63. rcu_periph_clock_enable(RCU_DMA0);
  64. /* initialize DMA channel5 */
  65. dma_deinit(DMA0,DMA_CH4);
  66. /* DMA channel5 initialize */
  67. dma_init_struct.periph_addr = (uint32_t)TIMER0_CH0CV;
  68. dma_init_struct.periph_inc = DMA_PERIPH_INCREASE_DISABLE;
  69. dma_init_struct.memory_addr = (uint32_t)buffer;
  70. dma_init_struct.memory_inc = DMA_MEMORY_INCREASE_ENABLE;
  71. dma_init_struct.periph_width = DMA_PERIPHERAL_WIDTH_16BIT;
  72. dma_init_struct.memory_width = DMA_MEMORY_WIDTH_16BIT;
  73. dma_init_struct.direction = DMA_MEMORY_TO_PERIPHERAL;
  74. dma_init_struct.number = 3;
  75. dma_init_struct.priority = DMA_PRIORITY_ULTRA_HIGH;
  76. dma_init(DMA0,DMA_CH4,&dma_init_struct);
  77. dma_circulation_enable(DMA0,DMA_CH4);
  78. /* enable DMA channel5 */
  79. dma_channel_enable(DMA0,DMA_CH4);
  80. }
  81. /*!
  82. \brief configure the TIMER peripheral
  83. \param[in] none
  84. \param[out] none
  85. \retval none
  86. */
  87. void timer_config(void)
  88. {
  89. /* TIMER0 DMA Transfer example -------------------------------------------------
  90. TIMER0CLK = 120MHz, Prescaler = 119
  91. TIMER0 counter clock = systemcoreclock/120 = 1MHz.
  92. the objective is to configure TIMER0 channel 1 to generate PWM
  93. signal with a frequency equal to 1KHz and a variable duty cycle(25%,50%,75%) that is
  94. changed by the DMA after a specific number of update DMA request.
  95. the number of this repetitive requests is defined by the TIMER0 repetition counter,
  96. each 2 update requests, the TIMER0 Channel 0 duty cycle changes to the next new
  97. value defined by the buffer .
  98. -----------------------------------------------------------------------------*/
  99. timer_oc_parameter_struct timer_ocintpara;
  100. timer_parameter_struct timer_initpara;
  101. rcu_periph_clock_enable(RCU_TIMER0);
  102. timer_deinit(TIMER0);
  103. /* TIMER0 configuration */
  104. timer_initpara.prescaler = 119;
  105. timer_initpara.alignedmode = TIMER_COUNTER_EDGE;
  106. timer_initpara.counterdirection = TIMER_COUNTER_UP;
  107. timer_initpara.period = 999;
  108. timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
  109. timer_initpara.repetitioncounter = 1;
  110. timer_init(TIMER0,&timer_initpara);
  111. /* CH0 configuration in PWM1 mode */
  112. timer_ocintpara.outputstate = TIMER_CCX_ENABLE;
  113. timer_ocintpara.outputnstate = TIMER_CCXN_DISABLE;
  114. timer_ocintpara.ocpolarity = TIMER_OC_POLARITY_HIGH;
  115. timer_ocintpara.ocnpolarity = TIMER_OCN_POLARITY_HIGH;
  116. timer_ocintpara.ocidlestate = TIMER_OC_IDLE_STATE_HIGH;
  117. timer_ocintpara.ocnidlestate = TIMER_OCN_IDLE_STATE_LOW;
  118. timer_channel_output_config(TIMER0,TIMER_CH_0,&timer_ocintpara);
  119. timer_channel_output_pulse_value_config(TIMER0,TIMER_CH_0,buffer[0]);
  120. timer_channel_output_mode_config(TIMER0,TIMER_CH_0,TIMER_OC_MODE_PWM0);
  121. timer_channel_output_shadow_config(TIMER0,TIMER_CH_0,TIMER_OC_SHADOW_DISABLE);
  122. /* TIMER0 primary output enable */
  123. timer_primary_output_config(TIMER0,ENABLE);
  124. /* TIMER0 update DMA request enable */
  125. timer_dma_enable(TIMER0,TIMER_DMA_UPD);
  126. /* auto-reload preload enable */
  127. timer_auto_reload_shadow_enable(TIMER0);
  128. /* TIMER0 counter enable */
  129. timer_enable(TIMER0);
  130. }
  131. /*!
  132. \brief main function
  133. \param[in] none
  134. \param[out] none
  135. \retval none
  136. */
  137. int main(void)
  138. {
  139. gpio_config();
  140. dma_config();
  141. timer_config();
  142. while (1);
  143. }