main.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*!
  2. \file main.c
  3. \brief USART half-duplex transmitter and receiver
  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 "gd32f307c_eval.h"
  34. #define ARRAYNUM(arr_nanme) (uint32_t)(sizeof(arr_nanme) / sizeof(*(arr_nanme)))
  35. #define TRANSMIT_SIZE0 (ARRAYNUM(transmitter_buffer0) - 1)
  36. #define TRANSMIT_SIZE1 (ARRAYNUM(transmitter_buffer1) - 1)
  37. uint8_t transmitter_buffer0[] = "\n\ra usart half-duplex test example!\n\r";
  38. uint8_t transmitter_buffer1[] = "\n\ra usart half-duplex test example!\n\r";
  39. uint8_t receiver_buffer0[TRANSMIT_SIZE1];
  40. uint8_t receiver_buffer1[TRANSMIT_SIZE0];
  41. uint8_t transfersize0 = TRANSMIT_SIZE0;
  42. uint8_t transfersize1 = TRANSMIT_SIZE1;
  43. __IO uint8_t txcount0 = 0;
  44. __IO uint16_t rxcount0 = 0;
  45. __IO uint8_t txcount1 = 0;
  46. __IO uint16_t rxcount1 = 0;
  47. ErrStatus state1 = ERROR;
  48. ErrStatus state2 = ERROR;
  49. ErrStatus memory_compare(uint8_t* src, uint8_t* dst, uint16_t length);
  50. /*!
  51. \brief main function
  52. \param[in] none
  53. \param[out] none
  54. \retval none
  55. */
  56. int main(void)
  57. {
  58. gd_eval_led_init(LED2);
  59. gd_eval_led_init(LED3);
  60. /* enable USART and GPIOA clock */
  61. rcu_periph_clock_enable(RCU_GPIOA);
  62. rcu_periph_clock_enable(RCU_USART0);
  63. rcu_periph_clock_enable(RCU_USART1);
  64. /* configure USART0 Tx as alternate function push-pull */
  65. gpio_init(GPIOA, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_9);
  66. /* configure USART1 Tx as alternate function push-pull */
  67. gpio_init(GPIOA, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_2);
  68. /* USART0 and USART1 baudrate configuration */
  69. usart_baudrate_set(USART0, 115200);
  70. usart_baudrate_set(USART1, 115200);
  71. /* enable USART0 half duplex mode*/
  72. usart_halfduplex_enable(USART0);
  73. /* enable USART1 half duplex mode*/
  74. usart_halfduplex_enable(USART1);
  75. /* configure USART transmitter */
  76. usart_transmit_config(USART0, USART_TRANSMIT_ENABLE);
  77. usart_transmit_config(USART1, USART_TRANSMIT_ENABLE);
  78. /* configure USART receiver */
  79. usart_receive_config(USART0, USART_RECEIVE_ENABLE);
  80. usart_receive_config(USART1, USART_RECEIVE_ENABLE);
  81. /* enable USART */
  82. usart_enable(USART0);
  83. usart_enable(USART1);
  84. /* clear the USART1 data register */
  85. usart_data_receive(USART1);
  86. /* USART0 transmit and USART1 receive */
  87. while(transfersize0--) {
  88. /* wait until end of transmit */
  89. while(RESET == usart_flag_get(USART0, USART_FLAG_TBE)) {
  90. }
  91. usart_data_transmit(USART0, transmitter_buffer0[txcount0++]);
  92. while(RESET == usart_flag_get(USART1, USART_FLAG_RBNE)) {
  93. }
  94. /* store the received byte in the receiver_buffer1 */
  95. receiver_buffer1[rxcount0++] = usart_data_receive(USART1);
  96. }
  97. /* clear the USART0 data register */
  98. usart_data_receive(USART0);
  99. /* USART1 transmit and USART0 receive */
  100. while(transfersize1--) {
  101. /* wait until end of transmit */
  102. while(RESET == usart_flag_get(USART1, USART_FLAG_TBE)) {
  103. }
  104. usart_data_transmit(USART1, transmitter_buffer1[txcount1++]);
  105. while(RESET == usart_flag_get(USART0,USART_FLAG_RBNE)) {
  106. }
  107. /* store the received byte in the receiver_buffer0 */
  108. receiver_buffer0[rxcount1++] = usart_data_receive(USART0);
  109. }
  110. /* compare the received data with the send ones */
  111. state1 = memory_compare(transmitter_buffer0, receiver_buffer1, TRANSMIT_SIZE0);
  112. state2 = memory_compare(transmitter_buffer1, receiver_buffer0, TRANSMIT_SIZE1);
  113. if(SUCCESS == state1) {
  114. /* if the data transmitted from USART0 and received by USART1 are the same */
  115. gd_eval_led_on(LED2);
  116. }else {
  117. /* if the data transmitted from USART0 and received by USART1 are not the same */
  118. gd_eval_led_off(LED2);
  119. }
  120. if(SUCCESS == state2) {
  121. /* if the data transmitted from USART1 and received by USART0 are the same */
  122. gd_eval_led_on(LED3);
  123. }else {
  124. /* if the data transmitted from USART1 and received by USART0 are not the same */
  125. gd_eval_led_off(LED3);
  126. }
  127. while(1) {
  128. }
  129. }
  130. /*!
  131. \brief memory compare function
  132. \param[in] src: source data
  133. \param[in] dst: destination data
  134. \param[in] length: the compare data length
  135. \param[out] none
  136. \retval ErrStatus: ERROR or SUCCESS
  137. */
  138. ErrStatus memory_compare(uint8_t* src, uint8_t* dst, uint16_t length)
  139. {
  140. while(length--) {
  141. if(*src++ != *dst++) {
  142. return ERROR;
  143. }
  144. }
  145. return SUCCESS;
  146. }