main.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*!
  2. \file main.c
  3. \brief USART HalfDuplex transmitter and 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. #define ARRAYNUM(arr_nanme) (uint32_t)(sizeof(arr_nanme) / sizeof(*(arr_nanme)))
  13. #define TRANSMIT_SIZE0 (ARRAYNUM(transmitter_buffer0) - 1)
  14. #define TRANSMIT_SIZE1 (ARRAYNUM(transmitter_buffer1) - 1)
  15. uint8_t transmitter_buffer0[] = "\n\ra usart half-duplex test example!\n\r";
  16. uint8_t transmitter_buffer1[] = "\n\ra usart half-duplex test example!\n\r";
  17. uint8_t receiver_buffer0[TRANSMIT_SIZE1];
  18. uint8_t receiver_buffer1[TRANSMIT_SIZE0];
  19. uint8_t transfersize0 = TRANSMIT_SIZE0;
  20. uint8_t transfersize1 = TRANSMIT_SIZE1;
  21. __IO uint8_t txcount0 = 0;
  22. __IO uint16_t rxcount0 = 0;
  23. __IO uint8_t txcount1 = 0;
  24. __IO uint16_t rxcount1 = 0;
  25. ErrStatus state1 = ERROR;
  26. ErrStatus state2 = ERROR;
  27. ErrStatus memory_compare(uint8_t* src, uint8_t* dst, uint16_t length) ;
  28. /*!
  29. \brief main function
  30. \param[in] none
  31. \param[out] none
  32. \retval none
  33. */
  34. int main(void)
  35. {
  36. gd_eval_led_init(LED1);
  37. gd_eval_led_init(LED2);
  38. /* enable USART and GPIOA clock */
  39. rcu_periph_clock_enable(RCU_GPIOA);
  40. rcu_periph_clock_enable(RCU_USART0);
  41. rcu_periph_clock_enable(RCU_USART1);
  42. /* configure the USART0 Tx pin and USART1 Tx pin */
  43. gpio_af_set(GPIOA, GPIO_AF_1, GPIO_PIN_2);
  44. gpio_af_set(GPIOA, GPIO_AF_1, GPIO_PIN_9);
  45. /* configure USART0 Tx as alternate function push-pull */
  46. gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_9);
  47. gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_9);
  48. /* configure USART1 Tx as alternate function push-pull */
  49. gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_2);
  50. gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_2);
  51. /* USART0 and USART1 baudrate configuration */
  52. usart_baudrate_set(USART0, 115200);
  53. usart_baudrate_set(USART1, 115200);
  54. /* Enable USART0 Half Duplex Mode*/
  55. usart_halfduplex_enable(USART0);
  56. /* Enable USART1 Half Duplex Mode*/
  57. usart_halfduplex_enable(USART1);
  58. /* configure USART transmitter and receiver*/
  59. usart_transmit_config(USART0, USART_TRANSMIT_ENABLE);
  60. usart_receive_config(USART0, USART_RECEIVE_ENABLE);
  61. usart_transmit_config(USART1, USART_TRANSMIT_ENABLE);
  62. usart_receive_config(USART1, USART_RECEIVE_ENABLE);
  63. /* enable USART */
  64. usart_enable(USART0);
  65. usart_enable(USART1);
  66. /* USART0 transmit and USART1 receive*/
  67. usart_data_receive(USART1);
  68. while(transfersize0--)
  69. {
  70. /* wait until end of transmit */
  71. while(RESET == usart_flag_get(USART0, USART_FLAG_TBE) );
  72. usart_data_transmit(USART0, transmitter_buffer0[txcount0++]);
  73. while(RESET == usart_flag_get(USART1, USART_FLAG_RBNE));
  74. /* store the received byte in the receiver_buffer1 */
  75. receiver_buffer1[rxcount0++] = usart_data_receive(USART1);
  76. }
  77. usart_data_receive(USART0);
  78. /* USART1 transmit and USART0 receive*/
  79. while(transfersize1--)
  80. {
  81. /* wait until end of transmit */
  82. while(RESET == usart_flag_get(USART1, USART_FLAG_TBE));
  83. usart_data_transmit(USART1, transmitter_buffer1[txcount1++]);
  84. while(RESET == usart_flag_get(USART0, USART_FLAG_RBNE));
  85. /* store the received byte in the receiver_buffer0 */
  86. receiver_buffer0[rxcount1++] = usart_data_receive(USART0);
  87. }
  88. /* compare the received data with the send ones */
  89. state1 = memory_compare(transmitter_buffer0, receiver_buffer1, TRANSMIT_SIZE0);
  90. state2 = memory_compare(transmitter_buffer1, receiver_buffer0, TRANSMIT_SIZE1);
  91. if(SUCCESS == state1){
  92. /* if the data transmitted from USART0 and received by USART1 are the same */
  93. gd_eval_led_on(LED1);
  94. }else{
  95. /* if the data transmitted from USART0 and received by USART1 are not the same */
  96. gd_eval_led_off(LED1);
  97. }
  98. if(SUCCESS == state2){
  99. /* if the data transmitted from USART1 and received by USART0 are the same */
  100. gd_eval_led_on(LED2);
  101. }else{
  102. /* if the data transmitted from USART1 and received by USART0 are not the same */
  103. gd_eval_led_off(LED2);
  104. }
  105. while (1)
  106. {
  107. }
  108. }
  109. /*!
  110. \brief memory compare function
  111. \param[in] src : source data
  112. \param[in] dst : destination data
  113. \param[in] length : the compare data length
  114. \param[out] none
  115. \retval ErrStatus : ERROR or SUCCESS
  116. */
  117. ErrStatus memory_compare(uint8_t* src, uint8_t* dst, uint16_t length)
  118. {
  119. while(length--){
  120. if (*src++ != *dst++){
  121. return ERROR;
  122. }
  123. }
  124. return SUCCESS;
  125. }
  126. /* retarget the C library printf function to the USART */
  127. int fputc(int ch, FILE *f)
  128. {
  129. usart_data_transmit(EVAL_COM1, (uint8_t) ch);
  130. while(RESET == usart_flag_get(EVAL_COM1, USART_FLAG_TBE));
  131. return ch;
  132. }