usart.c 3.2 KB

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