main.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*!
  2. \file main.c
  3. \brief transfer data from RAM to USART
  4. */
  5. /*
  6. Copyright (C) 2017 GigaDevice
  7. 2017-06-06, V1.0.0, firmware for GD32F3x0
  8. */
  9. #include "gd32f3x0.h"
  10. #include "gd32f3x0_eval.h"
  11. #define USART0_DATA_ADDRESS ((uint32_t)0x40013828)
  12. #define ARRAYNUM(arr_nanme) (uint32_t)(sizeof(arr_nanme) / sizeof(*(arr_nanme)))
  13. uint8_t welcome[]="hi,this is a example: RAM_TO_USART by DMA !\n";
  14. __IO FlagStatus g_transfer_complete = RESET;
  15. void led_config(void);
  16. void nvic_config(void);
  17. /*!
  18. \brief main function
  19. \param[in] none
  20. \param[out] none
  21. \retval none
  22. */
  23. int main(void)
  24. {
  25. dma_parameter_struct dma_init_struct;
  26. /* enable DMA clock */
  27. rcu_periph_clock_enable(RCU_DMA);
  28. /* USART configure */
  29. gd_eval_com_init(EVAL_COM1);
  30. /*configure DMA interrupt*/
  31. nvic_config();
  32. /* initialize LED */
  33. led_config();
  34. /* all LED off */
  35. gd_eval_led_off(LED1);
  36. /* initialize DMA channel1 */
  37. dma_deinit(DMA_CH1);
  38. dma_init_struct.direction = DMA_MEMORY_TO_PERIPHERAL;
  39. dma_init_struct.memory_addr = (uint32_t)welcome;
  40. dma_init_struct.memory_inc = DMA_MEMORY_INCREASE_ENABLE;
  41. dma_init_struct.memory_width = DMA_MEMORY_WIDTH_8BIT;
  42. dma_init_struct.number = ARRAYNUM(welcome);
  43. dma_init_struct.periph_addr = USART0_DATA_ADDRESS;
  44. dma_init_struct.periph_inc = DMA_PERIPH_INCREASE_DISABLE;
  45. dma_init_struct.periph_width = DMA_PERIPHERAL_WIDTH_8BIT;
  46. dma_init_struct.priority = DMA_PRIORITY_ULTRA_HIGH;
  47. dma_init(DMA_CH1, dma_init_struct);
  48. /* configure DMA mode */
  49. dma_circulation_disable(DMA_CH1);
  50. dma_memory_to_memory_disable(DMA_CH1);
  51. /* USART DMA enable for transmission */
  52. usart_dma_transmit_config(USART0, USART_DENT_ENABLE);
  53. /* enable DMA transfer complete interrupt */
  54. dma_interrupt_enable(DMA_CH1, DMA_INT_FTF);
  55. /* enable DMA channel1 */
  56. dma_channel_enable(DMA_CH1);
  57. /* waiting for the transfer to complete*/
  58. while(RESET == g_transfer_complete);
  59. /* light LED1 */
  60. gd_eval_led_on(LED1);
  61. while (1);
  62. }
  63. /*!
  64. \brief LEDs configure
  65. \param[in] none
  66. \param[out] none
  67. \retval none
  68. */
  69. void led_config(void)
  70. {
  71. gd_eval_led_init (LED1);
  72. }
  73. /*!
  74. \brief configure DMA interrupt
  75. \param[in] none
  76. \param[out] none
  77. \retval none
  78. */
  79. void nvic_config(void)
  80. {
  81. nvic_irq_enable(DMA_Channel1_2_IRQn, 0, 0);
  82. }