main.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*!
  2. \file main.c
  3. \brief USART DMA receive by IDLE interrupt
  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 USART0_RDATA_ADDRESS ((uint32_t)&USART_DATA(USART0))
  36. uint8_t rxbuffer[256];
  37. __IO uint8_t rx_count = 0;
  38. __IO uint8_t tx_count = 0;
  39. __IO uint8_t receive_flag = 0;
  40. void dma_config(void);
  41. void usart_config(void);
  42. void nvic_config(void);
  43. /*!
  44. \brief main function
  45. \param[in] none
  46. \param[out] none
  47. \retval none
  48. */
  49. int main(void)
  50. {
  51. gd_eval_led_init(LED2);
  52. gd_eval_led_on(LED2);
  53. nvic_config();
  54. /* initialize DMA */
  55. dma_config();
  56. /* initialize USART */
  57. usart_config();
  58. usart_interrupt_enable(USART0, USART_INT_IDLE);
  59. printf("\n\rPlease send data less than 256 bytes:\n\r");
  60. /* wait the data is received and send to the hyperterminal */
  61. while(1) {
  62. if(1 == receive_flag) {
  63. for(tx_count = 0; tx_count < rx_count; tx_count++) {
  64. while(RESET == usart_flag_get(USART0, USART_FLAG_TBE)) {
  65. }
  66. usart_data_transmit(USART0, rxbuffer[tx_count]);
  67. }
  68. receive_flag = 0;
  69. }
  70. }
  71. }
  72. /*!
  73. \brief configure USART DMA
  74. \param[in] none
  75. \param[out] none
  76. \retval none
  77. */
  78. void dma_config(void)
  79. {
  80. dma_parameter_struct dma_init_struct;
  81. rcu_periph_clock_enable(RCU_DMA0);
  82. /* deinitialize DMA channel4 (USART0 rx) */
  83. dma_deinit(DMA0, DMA_CH4);
  84. dma_init_struct.direction = DMA_PERIPHERAL_TO_MEMORY;
  85. dma_init_struct.memory_addr = (uint32_t)rxbuffer;
  86. dma_init_struct.memory_inc = DMA_MEMORY_INCREASE_ENABLE;
  87. dma_init_struct.memory_width = DMA_MEMORY_WIDTH_8BIT;
  88. dma_init_struct.number = 256;
  89. dma_init_struct.periph_addr = USART0_RDATA_ADDRESS;
  90. dma_init_struct.periph_inc = DMA_PERIPH_INCREASE_DISABLE;
  91. dma_init_struct.periph_width = DMA_PERIPHERAL_WIDTH_8BIT;
  92. dma_init_struct.priority = DMA_PRIORITY_ULTRA_HIGH;
  93. dma_init(DMA0, DMA_CH4, &dma_init_struct);
  94. /* configure DMA mode */
  95. dma_circulation_disable(DMA0, DMA_CH4);
  96. /* enable DMA channel4 */
  97. dma_channel_enable(DMA0, DMA_CH4);
  98. }
  99. /*!
  100. \brief configure USART
  101. \param[in] none
  102. \param[out] none
  103. \retval none
  104. */
  105. void usart_config(void)
  106. {
  107. /* enable GPIO clock */
  108. rcu_periph_clock_enable(RCU_GPIOA);
  109. /* enable USART clock */
  110. rcu_periph_clock_enable(RCU_USART0);
  111. /* connect port to USARTx_Tx */
  112. gpio_init(GPIOA, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_9);
  113. /* connect port to USARTx_Rx */
  114. gpio_init(GPIOA, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, GPIO_PIN_10);
  115. /* configure USART */
  116. usart_deinit(USART0);
  117. usart_baudrate_set(USART0, 115200U);
  118. usart_receive_config(USART0, USART_RECEIVE_ENABLE);
  119. usart_transmit_config(USART0, USART_TRANSMIT_ENABLE);
  120. usart_dma_receive_config(USART0, USART_RECEIVE_DMA_ENABLE);
  121. usart_enable(USART0);
  122. }
  123. /*!
  124. \brief configure NVIC
  125. \param[in] none
  126. \param[out] none
  127. \retval none
  128. */
  129. void nvic_config(void)
  130. {
  131. nvic_irq_enable(USART0_IRQn, 0, 0);
  132. }
  133. /* retarget the C library printf function to the USART */
  134. int fputc(int ch, FILE *f)
  135. {
  136. usart_data_transmit(USART0, (uint8_t)ch);
  137. while(RESET == usart_flag_get(USART0, USART_FLAG_TBE));
  138. return ch;
  139. }