usart.c 3.5 KB

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