main.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*!
  2. \file main.c
  3. \brief USART transmit and receive interrupt
  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_SIZE (ARRAYNUM(transmitter_buffer) - 1)
  14. uint8_t transmitter_buffer[] = "\n\rUSART interrupt test\n\r";
  15. uint8_t receiver_buffer[32];
  16. uint8_t transfersize = TRANSMIT_SIZE;
  17. uint8_t receivesize = 32;
  18. __IO uint8_t txcount = 0;
  19. __IO uint16_t rxcount = 0;
  20. /*!
  21. \brief main function
  22. \param[in] none
  23. \param[out] none
  24. \retval none
  25. */
  26. int main(void)
  27. {
  28. /* USART interrupt configuration */
  29. nvic_irq_enable(USART0_IRQn, 0, 0);
  30. gd_eval_com_init(EVAL_COM1);
  31. /* enable USART TBE interrupt */
  32. usart_interrupt_enable(EVAL_COM1, USART_INT_TBE);
  33. /* wait until USART send the transmitter_buffer */
  34. while(txcount < transfersize);
  35. while (RESET == usart_flag_get(EVAL_COM1, USART_FLAG_TC));
  36. usart_interrupt_enable(EVAL_COM1, USART_INT_RBNE);
  37. /* wait until USART receive the receiver_buffer */
  38. while(rxcount < receivesize);
  39. if(rxcount == receivesize)
  40. printf("\n\rUSART receive successfully!\n\r");
  41. while (1);
  42. }
  43. /* retarget the C library printf function to the USART */
  44. int fputc(int ch, FILE *f)
  45. {
  46. usart_data_transmit(EVAL_COM1, (uint8_t) ch);
  47. while(RESET == usart_flag_get(EVAL_COM1, USART_FLAG_TBE));
  48. return ch;
  49. }