portserial.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. * FreeModbus Libary: BARE Port
  3. * Copyright (C) 2006 Christian Walter <wolti@sil.at>
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2.1 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  18. *
  19. * File: $Id$
  20. */
  21. #include "port.h"
  22. /* ----------------------- Modbus includes ----------------------------------*/
  23. #include "mb.h"
  24. #include "mbport.h"
  25. #include "gd32e230x_conf.h"
  26. /* ----------------------- static functions ---------------------------------*/
  27. static void prvvUARTTxReadyISR( void );
  28. static void prvvUARTRxISR( void );
  29. /* ----------------------- Start implementation -----------------------------*/
  30. void
  31. vMBPortSerialEnable( BOOL xRxEnable, BOOL xTxEnable )
  32. {
  33. /* If xRXEnable enable serial receive interrupts. If xTxENable enable
  34. * transmitter empty interrupts.
  35. */
  36. if(TRUE == xRxEnable)
  37. {
  38. usart_interrupt_enable(USART0,USART_INT_RBNE);//使能接收中断
  39. gpio_bit_reset(GPIOA, GPIO_PIN_11); //接收
  40. }
  41. else
  42. {
  43. usart_interrupt_disable(USART0,USART_INT_RBNE);//失能接收中断
  44. gpio_bit_set(GPIOA, GPIO_PIN_11); //恢复发送
  45. }
  46. // if(TRUE == xTxEnable)
  47. // {
  48. // usart_interrupt_enable(USART0,USART_INT_TBE); //使能发送中断
  49. // gpio_bit_set(GPIOA, GPIO_PIN_11); //发送
  50. // }
  51. // else
  52. // {
  53. // usart_interrupt_disable(USART0,USART_INT_TBE); //失能发送为空中断
  54. // usart_interrupt_enable(USART0,USART_INT_TC); //使能发送完成中断
  55. // }
  56. if(TRUE == xTxEnable)
  57. {
  58. usart_interrupt_enable(USART0,USART_INT_TC); //使能发送中断
  59. gpio_bit_set(GPIOA, GPIO_PIN_11); //发送
  60. }
  61. else
  62. {
  63. usart_interrupt_disable(USART0,USART_INT_TC); //使能发送完成中断
  64. gpio_bit_reset(GPIOA, GPIO_PIN_11); //发送
  65. }
  66. }
  67. BOOL
  68. xMBPortSerialInit( UCHAR ucPORT, ULONG ulBaudRate, UCHAR ucDataBits, eMBParity eParity )
  69. {
  70. nvic_irq_enable(USART0_IRQn, 1); //使能串口中断,设置优先级
  71. rcu_periph_clock_enable(RCU_GPIOA); //使能GPIOA时钟
  72. //配置TX的GPIO
  73. gpio_af_set(GPIOA, GPIO_AF_1, GPIO_PIN_9); //复用PA9
  74. gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_9);
  75. gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_10MHZ, GPIO_PIN_9);
  76. //配置RX的GPIO
  77. gpio_af_set(GPIOA, GPIO_AF_1, GPIO_PIN_10); //复用PA10
  78. gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_10);
  79. gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_10MHZ, GPIO_PIN_10);
  80. //配置TX_RX的切换
  81. gpio_mode_set(GPIOA, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_11); //设置GPIO模式
  82. gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_11); //设置GPIO输出模式及速度
  83. gpio_bit_reset(GPIOA, GPIO_PIN_11);
  84. //配置USART的参数
  85. rcu_periph_clock_enable(RCU_USART0); //使能串口时钟
  86. usart_deinit(USART0); //RCU配置恢复默认值
  87. usart_baudrate_set(USART0, ulBaudRate); //设置波特率
  88. usart_stop_bit_set(USART0,USART_STB_1BIT); //设置停止位
  89. usart_word_length_set(USART0,USART_WL_8BIT); //设置数据字长度
  90. usart_parity_config(USART0,USART_PM_NONE); //设置奇偶校验位
  91. usart_receive_config(USART0, USART_RECEIVE_ENABLE); //使能接收
  92. usart_transmit_config(USART0, USART_TRANSMIT_ENABLE); //使能发送
  93. usart_enable(USART0); //使能串口
  94. return TRUE;
  95. //return FALSE;
  96. }
  97. BOOL
  98. xMBPortSerialPutByte( CHAR ucByte )
  99. {
  100. /* Put a byte in the UARTs transmit buffer. This function is called
  101. * by the protocol stack if pxMBFrameCBTransmitterEmpty( ) has been
  102. * called. */
  103. usart_data_transmit(USART0,ucByte);
  104. return TRUE;
  105. }
  106. BOOL
  107. xMBPortSerialGetByte( CHAR * pucByte )
  108. {
  109. /* Return the byte in the UARTs receive buffer. This function is called
  110. * by the protocol stack after pxMBFrameCBByteReceived( ) has been called.
  111. */
  112. *pucByte = usart_data_receive(USART0);
  113. return TRUE;
  114. }
  115. void USART0_IRQHandler(void)
  116. {
  117. if(usart_interrupt_flag_get(USART0, USART_INT_FLAG_RBNE) != RESET) //接收缓冲区非空中断
  118. {
  119. prvvUARTRxISR();
  120. usart_interrupt_flag_clear(USART0, USART_INT_FLAG_RBNE); //清除USART1中断挂起
  121. }
  122. // if(usart_interrupt_flag_get(USART0, USART_INT_FLAG_ERR_ORERR) == SET) //溢出错误标志为1
  123. // {
  124. // prvvUARTRxISR();
  125. // usart_interrupt_flag_clear(USART0, USART_INT_FLAG_ERR_ORERR); //清除溢出错误标志
  126. // }
  127. if(usart_flag_get(USART0, USART_FLAG_ORERR) == SET) //溢出错误标志为1
  128. {
  129. usart_flag_clear(USART0, USART_FLAG_ORERR); //清除溢出错误标志
  130. prvvUARTRxISR();
  131. }
  132. // if(usart_flag_get(USART0, USART_FLAG_ORERR) == SET) //溢出错误标志为1
  133. // {
  134. // usart_flag_clear(USART0, USART_FLAG_ORERR); //清除溢出错误标志
  135. // prvvUARTRxISR();
  136. // }
  137. // if(usart_interrupt_flag_get(USART0, USART_INT_FLAG_TBE)!= RESET) //发送缓冲区空中断
  138. // {
  139. // prvvUARTTxReadyISR();
  140. // usart_interrupt_flag_clear(USART0, USART_INT_FLAG_TBE); //清除发送中断标志
  141. // }
  142. // if(usart_interrupt_flag_get(USART0, USART_INT_FLAG_TC)!= RESET) //发送完成中断
  143. // {
  144. // usart_interrupt_flag_clear(USART0, USART_INT_FLAG_TC); //清除发送完成中断
  145. // gpio_bit_reset(GPIOA, GPIO_PIN_11); //接收
  146. // }
  147. if(usart_interrupt_flag_get(USART0, USART_INT_FLAG_TBE)!= RESET) //发送缓冲区空中断
  148. {
  149. prvvUARTTxReadyISR();
  150. usart_interrupt_flag_clear(USART0, USART_INT_FLAG_TBE); //清除发送中断标志
  151. }
  152. if(usart_interrupt_flag_get(USART0, USART_INT_FLAG_TC)!= RESET) //发送完成中断
  153. {
  154. prvvUARTTxReadyISR();
  155. usart_interrupt_flag_clear(USART0, USART_INT_FLAG_TC); //清除发送完成中断
  156. // gpio_bit_reset(GPIOA, GPIO_PIN_11); //接收
  157. }
  158. }
  159. /* Create an interrupt handler for the transmit buffer empty interrupt
  160. * (or an equivalent) for your target processor. This function should then
  161. * call pxMBFrameCBTransmitterEmpty( ) which tells the protocol stack that
  162. * a new character can be sent. The protocol stack will then call
  163. * xMBPortSerialPutByte( ) to send the character.
  164. */
  165. static void prvvUARTTxReadyISR( void )
  166. {
  167. pxMBFrameCBTransmitterEmpty( );
  168. }
  169. /* Create an interrupt handler for the receive interrupt for your target
  170. * processor. This function should then call pxMBFrameCBByteReceived( ). The
  171. * protocol stack will then call xMBPortSerialGetByte( ) to retrieve the
  172. * character.
  173. */
  174. static void prvvUARTRxISR( void )
  175. {
  176. pxMBFrameCBByteReceived( );
  177. }