main.c 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*!
  2. \file main.c
  3. \brief USART DMA transmitter receiver
  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. uint8_t rxbuffer[10];
  13. uint8_t txbuffer[]="\n\rUSART DMA receive and transmit example, please input 10 bytes:\n\r";
  14. #define ARRAYNUM(arr_nanme) (uint32_t)(sizeof(arr_nanme) / sizeof(*(arr_nanme)))
  15. #define USART0_TDATA_ADDRESS ((uint32_t)0x40013828)
  16. #define USART0_RDATA_ADDRESS ((uint32_t)0x40013824)
  17. extern __IO uint8_t txcount;
  18. extern __IO uint16_t rxcount;
  19. /*!
  20. \brief main function
  21. \param[in] none
  22. \param[out] none
  23. \retval none
  24. */
  25. int main(void)
  26. {
  27. dma_parameter_struct dma_init_struct;
  28. /* enable DMA clock */
  29. rcu_periph_clock_enable(RCU_DMA);
  30. gd_eval_com_init(EVAL_COM1);
  31. printf("\n\ra usart dma function test example!\n\r");
  32. /* deinitialize DMA channel1 */
  33. dma_deinit(DMA_CH1);
  34. dma_init_struct.direction = DMA_MEMORY_TO_PERIPHERAL;
  35. dma_init_struct.memory_addr = (uint32_t)txbuffer;
  36. dma_init_struct.memory_inc = DMA_MEMORY_INCREASE_ENABLE;
  37. dma_init_struct.memory_width = DMA_MEMORY_WIDTH_8BIT;
  38. dma_init_struct.number = ARRAYNUM(txbuffer);
  39. dma_init_struct.periph_addr = USART0_TDATA_ADDRESS;
  40. dma_init_struct.periph_inc = DMA_PERIPH_INCREASE_DISABLE;
  41. dma_init_struct.periph_width = DMA_PERIPHERAL_WIDTH_8BIT;
  42. dma_init_struct.priority = DMA_PRIORITY_ULTRA_HIGH;
  43. dma_init(DMA_CH1,dma_init_struct);
  44. /* configure DMA mode */
  45. dma_circulation_disable(DMA_CH1);
  46. dma_memory_to_memory_disable(DMA_CH1);
  47. /* enable DMA channel1 */
  48. dma_channel_enable(DMA_CH1);
  49. /* USART DMA enable for transmission and reception */
  50. usart_dma_transmit_config(USART0, USART_DENT_ENABLE);
  51. /* wait DMA Channel transfer complete */
  52. while(RESET == dma_flag_get(DMA_CH1, DMA_FLAG_FTF));
  53. while(1){
  54. /* deinitialize DMA channel2 */
  55. dma_deinit(DMA_CH2);
  56. dma_init_struct.direction = DMA_PERIPHERAL_TO_MEMORY;
  57. dma_init_struct.memory_addr = (uint32_t)rxbuffer;
  58. dma_init_struct.memory_inc = DMA_MEMORY_INCREASE_ENABLE;
  59. dma_init_struct.memory_width = DMA_MEMORY_WIDTH_8BIT;
  60. dma_init_struct.number = 10;
  61. dma_init_struct.periph_addr = USART0_RDATA_ADDRESS;
  62. dma_init_struct.periph_inc = DMA_PERIPH_INCREASE_DISABLE;
  63. dma_init_struct.periph_width = DMA_PERIPHERAL_WIDTH_8BIT;
  64. dma_init_struct.priority = DMA_PRIORITY_ULTRA_HIGH;
  65. dma_init(DMA_CH2, dma_init_struct);
  66. dma_circulation_disable(DMA_CH2);
  67. dma_memory_to_memory_disable(DMA_CH2);
  68. dma_channel_enable(DMA_CH2);
  69. usart_dma_receive_config(USART0, USART_DENR_ENABLE);
  70. /* wait DMA channel transfer complete */
  71. while(RESET == dma_flag_get( DMA_CH2, DMA_FLAG_FTF));
  72. printf("\n\r%s\n\r",rxbuffer);
  73. }
  74. }
  75. /* retarget the C library printf function to the USART */
  76. int fputc(int ch, FILE *f)
  77. {
  78. usart_data_transmit(EVAL_COM1, (uint8_t) ch);
  79. while(RESET == usart_flag_get(EVAL_COM1, USART_FLAG_TBE));
  80. return ch;
  81. }