main.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*!
  2. \file main.c
  3. \brief TIMER0 dma burst 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. #define TIMER0_DMATB ((uint32_t)0x040012C4C)
  13. uint16_t buffer[8] = {99,199,299,399,499,599,699,799};
  14. void gpio_config(void);
  15. void timer_config(void);
  16. void dma_config(void);
  17. /*!
  18. \brief configure the GPIO ports
  19. \param[in] none
  20. \param[out] none
  21. \retval none
  22. */
  23. void gpio_config(void)
  24. {
  25. rcu_periph_clock_enable(RCU_GPIOA);
  26. /*configure PA8(TIMER0 CH0) as alternate function*/
  27. gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_8);
  28. gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_8);
  29. gpio_af_set(GPIOA, GPIO_AF_2, GPIO_PIN_8);
  30. /*configure PA9(TIMER0 CH1) as alternate function*/
  31. gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_9);
  32. gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_9);
  33. gpio_af_set(GPIOA, GPIO_AF_2, GPIO_PIN_9);
  34. /*configure PA10(TIMER0 CH2) as alternate function*/
  35. gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_10);
  36. gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_10);
  37. gpio_af_set(GPIOA, GPIO_AF_2, GPIO_PIN_10);
  38. /*configure PA11(TIMER0 CH3) as alternate function*/
  39. gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_11);
  40. gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_11);
  41. gpio_af_set(GPIOA, GPIO_AF_2, GPIO_PIN_11);
  42. }
  43. /*!
  44. \brief configure the DMA peripheral
  45. \param[in] none
  46. \param[out] none
  47. \retval none
  48. */
  49. void dma_config(void)
  50. {
  51. dma_parameter_struct dma_init_struct;
  52. /* enable DMA clock */
  53. rcu_periph_clock_enable(RCU_DMA);
  54. /* initialize DMA channel4 */
  55. dma_deinit(DMA_CH4);
  56. /* DMA channel4 initialize */
  57. dma_deinit(DMA_CH4);
  58. dma_init_struct.direction = DMA_MEMORY_TO_PERIPHERAL;
  59. dma_init_struct.memory_addr = (uint32_t)buffer;
  60. dma_init_struct.memory_inc = DMA_MEMORY_INCREASE_ENABLE;
  61. dma_init_struct.memory_width = DMA_MEMORY_WIDTH_16BIT;
  62. dma_init_struct.number = 8;
  63. dma_init_struct.periph_addr = (uint32_t)TIMER0_DMATB;
  64. dma_init_struct.periph_inc = DMA_PERIPH_INCREASE_DISABLE;
  65. dma_init_struct.periph_width = DMA_PERIPHERAL_WIDTH_16BIT;
  66. dma_init_struct.priority = DMA_PRIORITY_HIGH;
  67. dma_init(DMA_CH4,dma_init_struct);
  68. /* configure DMA mode */
  69. dma_circulation_enable(DMA_CH4);
  70. dma_memory_to_memory_disable(DMA_CH4);
  71. /* enable DMA channel4 */
  72. dma_channel_enable(DMA_CH4);
  73. }
  74. /*!
  75. \brief configure the TIMER peripheral
  76. \param[in] none
  77. \param[out] none
  78. \retval none
  79. */
  80. void timer_config(void)
  81. {
  82. /* TIMER0 DMA transfer example -------------------------------------------------
  83. TIMER0CLK = 84MHz(GD32F330) or 108MHz(GD32F350), prescaler = 84(GD32F330) or 108(GD32F350)
  84. TIMER0 counter clock 1MHz.
  85. The objective is to configure TIMER0 channel 0~3(PA8~PA11) to generate PWM signal.
  86. capture compare register 0~3 are to be updated twice per circle.On the first update
  87. DMA request, data1 is transferred to CH0CV, data2 is transferred to CH1CV, data3 is
  88. transferred to CH2CV,data4 is transferred to CH3CV and duty cycle(10%,20%,30%,40%).
  89. On the second update DMA request, data5 is transferred to CH0CV, data6 is transferred
  90. to CH1CV, data7 is transferred to CH2CV,data8 is transferred to CH3CV and duty cycle
  91. (50%,60%,70%,80%).
  92. -----------------------------------------------------------------------------*/
  93. timer_oc_parameter_struct timer_ocintpara;
  94. timer_parameter_struct timer_initpara;
  95. rcu_periph_clock_enable(RCU_TIMER0);
  96. timer_deinit(TIMER0);
  97. /* TIMER0 configuration */
  98. #ifdef GD32F330
  99. timer_initpara.prescaler = 83;
  100. #endif /* GD32F330 */
  101. #ifdef GD32F350
  102. timer_initpara.prescaler = 107;
  103. #endif /* GD32F350 */
  104. timer_initpara.alignedmode = TIMER_COUNTER_EDGE;
  105. timer_initpara.counterdirection = TIMER_COUNTER_UP;
  106. timer_initpara.period = 999;
  107. timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
  108. timer_initpara.repetitioncounter = 0;
  109. timer_init(TIMER0,&timer_initpara);
  110. timer_ocintpara.outputstate = TIMER_CCX_ENABLE;
  111. timer_ocintpara.outputnstate = TIMER_CCXN_ENABLE;
  112. timer_ocintpara.ocpolarity = TIMER_OC_POLARITY_HIGH;
  113. timer_ocintpara.ocnpolarity = TIMER_OCN_POLARITY_HIGH;
  114. timer_ocintpara.ocidlestate = TIMER_OC_IDLE_STATE_HIGH;
  115. timer_ocintpara.ocnidlestate = TIMER_OCN_IDLE_STATE_LOW;
  116. timer_channel_output_config(TIMER0,TIMER_CH_0,&timer_ocintpara);
  117. timer_channel_output_config(TIMER0,TIMER_CH_1,&timer_ocintpara);
  118. timer_channel_output_config(TIMER0,TIMER_CH_2,&timer_ocintpara);
  119. timer_channel_output_config(TIMER0,TIMER_CH_3,&timer_ocintpara);
  120. /* CH0 configuration in PWM0 mode */
  121. timer_channel_output_pulse_value_config(TIMER0,TIMER_CH_0,buffer[0]);
  122. timer_channel_output_mode_config(TIMER0,TIMER_CH_0,TIMER_OC_MODE_PWM0);
  123. timer_channel_output_shadow_config(TIMER0,TIMER_CH_0,TIMER_OC_SHADOW_DISABLE);
  124. /* CH1 configuration in PWM0 mode */
  125. timer_channel_output_pulse_value_config(TIMER0,TIMER_CH_1,buffer[0]);
  126. timer_channel_output_mode_config(TIMER0,TIMER_CH_1,TIMER_OC_MODE_PWM0);
  127. timer_channel_output_shadow_config(TIMER0,TIMER_CH_1,TIMER_OC_SHADOW_DISABLE);
  128. /* CH2 configuration in PWM0 mode */
  129. timer_channel_output_pulse_value_config(TIMER0,TIMER_CH_2,buffer[0]);
  130. timer_channel_output_mode_config(TIMER0,TIMER_CH_2,TIMER_OC_MODE_PWM0);
  131. timer_channel_output_shadow_config(TIMER0,TIMER_CH_2,TIMER_OC_SHADOW_DISABLE);
  132. /* CH3 configuration in PWM0 mode */
  133. timer_channel_output_pulse_value_config(TIMER0,TIMER_CH_3,buffer[0]);
  134. timer_channel_output_mode_config(TIMER0,TIMER_CH_3,TIMER_OC_MODE_PWM0);
  135. timer_channel_output_shadow_config(TIMER0,TIMER_CH_3,TIMER_OC_SHADOW_DISABLE);
  136. /* TIMER0 primary output enable */
  137. timer_primary_output_config(TIMER0,ENABLE);
  138. /* TIMER0 update DMA request enable */
  139. timer_dma_transfer_config(TIMER0,TIMER_DMACFG_DMATA_CH0CV,TIMER_DMACFG_DMATC_4TRANSFER);
  140. timer_dma_enable(TIMER0,TIMER_DMA_UPD);
  141. /* auto-reload preload enable */
  142. timer_auto_reload_shadow_enable(TIMER0);
  143. /* TIMER0 counter enable */
  144. timer_enable(TIMER0);
  145. }
  146. /*!
  147. \brief main function
  148. \param[in] none
  149. \param[out] none
  150. \retval none
  151. */
  152. int main(void)
  153. {
  154. gpio_config();
  155. dma_config();
  156. timer_config();
  157. while (1);
  158. }