main.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*!
  2. \file main.c
  3. \brief Auto baudrate detection
  4. */
  5. /*
  6. Copyright (C) 2017 GigaDevice
  7. 2017-06-06, V1.0.0, firmware for GD32F3x0
  8. */
  9. #include "gd32f3x0.h"
  10. #include "gd32f3x0_eval.h"
  11. #include <stdio.h>
  12. /*!
  13. \brief main function
  14. \param[in] none
  15. \param[out] none
  16. \retval none
  17. */
  18. int main(void)
  19. {
  20. gd_eval_led_init(LED1);
  21. /* USART configuration */
  22. gd_eval_com_init(EVAL_COM1);
  23. printf("\n\rUSART auto baudrate detection example\n\r");
  24. /* configure the auto-baud rate method */
  25. usart_autobaud_detection_mode_config(EVAL_COM1, USART_ABDM_FTOF);
  26. /* enable autobaudrate feature */
  27. usart_autobaud_detection_enable(EVAL_COM1);
  28. /* wait until receive enable acknowledge flag is set */
  29. while(RESET == usart_flag_get(EVAL_COM1, USART_FLAG_REA));
  30. /* wait until transmit enable acknowledge flag is set */
  31. while(RESET == usart_flag_get(EVAL_COM1, USART_FLAG_TEA));
  32. /* loop until the end of autobaudrate phase */
  33. while(RESET == usart_flag_get(EVAL_COM1, USART_FLAG_ABD));
  34. /* if autobaudbate error occurred */
  35. if(RESET == usart_flag_get(EVAL_COM1, USART_FLAG_ABDE)){
  36. /* wait until RBNE flag is set */
  37. while(RESET == usart_flag_get(EVAL_COM1, USART_FLAG_RBNE));
  38. /* wait until TBE flag is set */
  39. while(RESET == usart_flag_get(EVAL_COM1, USART_FLAG_TBE));
  40. /* USART auto baud rate detection finshed successfully */
  41. gd_eval_led_on(LED1);
  42. /* check the transfer complete flag */
  43. while (RESET == usart_flag_get(EVAL_COM1, USART_FLAG_TC));
  44. usart_transmit_config(EVAL_COM1, USART_TRANSMIT_DISABLE);
  45. usart_receive_config(EVAL_COM1, USART_RECEIVE_DISABLE);
  46. }
  47. /* USART disable */
  48. usart_disable(EVAL_COM1);
  49. while(1);
  50. }
  51. /* retarget the C library printf function to the USART */
  52. int fputc(int ch, FILE *f)
  53. {
  54. usart_data_transmit(EVAL_COM1, (uint8_t) ch);
  55. while(RESET == usart_flag_get(EVAL_COM1, USART_FLAG_TBE));
  56. return ch;
  57. }