usart.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #include "usart.h"
  2. void nvic_config(void);
  3. void gd_485_DE_pin_init(void)
  4. {
  5. /* enable the 485 DE pin clock */
  6. rcu_periph_clock_enable(DE485_GPIO_CLK);
  7. /* configure 485 DE GPIO port */
  8. gpio_init(DE485_GPIO_PORT, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, DE485_PIN);
  9. GPIO_BC(DE485_GPIO_PORT) = DE485_PIN;
  10. }
  11. /*!
  12. \brief turn 485 to tx
  13. \param[out] none
  14. \retval none
  15. */
  16. void gd_485_DE_tx(void)
  17. {
  18. GPIO_BOP(DE485_GPIO_PORT) = DE485_PIN;
  19. }
  20. /*!
  21. \brief turn 485 to rx
  22. \param[out] none
  23. \retval none
  24. */
  25. void gd_485_DE_rx(void)
  26. {
  27. GPIO_BC(DE485_GPIO_PORT) = DE485_PIN;
  28. }
  29. /*!
  30. \brief COM port to send buff
  31. \param[in] com: COM on the board
  32. \arg COM_485
  33. \param[out] none
  34. \retval none
  35. */
  36. void gd_com_485_send(uint8_t *message,uint16_t size){
  37. uint16_t i=0;
  38. gd_485_DE_tx();
  39. for (i = 0; i < size; i++)
  40. {
  41. usart_data_transmit(COM_485, message[i]);
  42. while (RESET == usart_flag_get(COM_485, USART_FLAG_TC));
  43. }
  44. gd_485_DE_rx();
  45. }
  46. /*
  47. * 函数名:void config_485_port(uint32_t com,uint32_t baudrate, uint8_t databits, uint8_t stopbits, uint8_t parity, uint8_t flowcontrol)
  48. * 输入参数:com 串口,baudrate,databits,stopbits,parity,flowcontol 串口的配置参数
  49. * 输出参数:无
  50. * 返回值:无
  51. * 函数作用:配置485串口参数
  52. */
  53. void config_485_port(uint32_t com,uint32_t baudrate, uint8_t databits, uint8_t stopbits, uint8_t parity)
  54. {
  55. uint32_t com_id = 0U;
  56. uint8_t wordLength;
  57. if(parity!=0)
  58. {
  59. wordLength=databits+1;
  60. }else wordLength=databits;
  61. nvic_irq_enable(COM_485_IT_HANDLER, 0, 0);
  62. /* enable GPIO clock */
  63. rcu_periph_clock_enable(COM_485_GPIO_CLK);
  64. /* enable USART clock */
  65. rcu_periph_clock_enable(COM_485_CLK);
  66. /* connect port to USARTx_Tx */
  67. gpio_init(COM_485_GPIO_PORT, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, COM_485_TX_PIN);
  68. /* connect port to USARTx_Rx */
  69. gpio_init(COM_485_GPIO_PORT, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, COM_485_RX_PIN);
  70. //usart_deinit(com);
  71. usart_baudrate_set(com, baudrate);
  72. usart_word_length_set(com, (wordLength == 9) ? USART_WL_9BIT : USART_WL_8BIT);
  73. usart_stop_bit_set(com, (stopbits == 2) ? USART_STB_2BIT : USART_STB_1BIT);
  74. usart_parity_config(com, parity == 1 ? (USART_PM_ODD) : (parity == 2 ? USART_PM_EVEN : USART_PM_NONE));
  75. usart_hardware_flow_rts_config(com, USART_RTS_DISABLE);
  76. usart_hardware_flow_cts_config(com, USART_CTS_DISABLE);
  77. usart_receive_config(com, USART_RECEIVE_ENABLE);
  78. usart_transmit_config(com, USART_TRANSMIT_ENABLE);
  79. /* 使能串口 */
  80. usart_enable(com);
  81. // nvic_config();
  82. // usart_interrupt_enable(com, USART_INT_RBNE);
  83. // usart_interrupt_enable(com, USART_INT_IDLE);
  84. //解决485第一个帧数据第一个字节丢失问题
  85. usart_flag_clear(COM_485,USART_FLAG_TC);
  86. }
  87. void nvic_config(void)
  88. {
  89. nvic_irq_enable(USART0_IRQn, 0, 0);
  90. }
  91. void USART0_IRQHandler(void)
  92. {
  93. if (RESET != usart_interrupt_flag_get(USART0, USART_INT_FLAG_RBNE))
  94. {
  95. usart_interrupt_flag_clear(USART0, USART_INT_FLAG_RBNE);
  96. }
  97. if (RESET != usart_interrupt_flag_get(USART0, USART_INT_FLAG_IDLE))
  98. {
  99. usart_interrupt_flag_clear(USART0, USART_INT_FLAG_IDLE);
  100. }
  101. else
  102. {
  103. usart_interrupt_flag_clear(USART0, USART_INT_FLAG_RBNE);
  104. }
  105. }