portserial.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. * FreeModbus Libary: RT-Thread Port
  3. * Copyright (C) 2013 Armink <armink.ztl@gmail.com>
  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: portserial.c,v 1.60 2013/08/13 15:07:05 Armink $
  20. */
  21. #include "port.h"
  22. /* ----------------------- Modbus includes ----------------------------------*/
  23. #include "mb.h"
  24. #include "mbport.h"
  25. #include "rtdevice.h"
  26. #include "board.h"
  27. /* ----------------------- Static variables ---------------------------------*/
  28. /* software simulation serial transmit IRQ handler thread stack */
  29. #ifdef rt_align
  30. rt_align(RT_ALIGN_SIZE)
  31. #else
  32. ALIGN(RT_ALIGN_SIZE)
  33. #endif
  34. static rt_uint8_t serial_soft_trans_irq_stack[512];
  35. /* software simulation serial transmit IRQ handler thread */
  36. static struct rt_thread thread_serial_soft_trans_irq;
  37. /* serial event */
  38. static struct rt_event event_serial;
  39. /* modbus slave serial device */
  40. static struct rt_serial_device *serial;
  41. /* ----------------------- Defines ------------------------------------------*/
  42. /* serial transmit event */
  43. #define EVENT_SERIAL_TRANS_START (1<<0)
  44. /* ----------------------- static functions ---------------------------------*/
  45. static void prvvUARTTxReadyISR(void);
  46. static void prvvUARTRxISR(void);
  47. static rt_err_t serial_rx_ind(rt_device_t dev, rt_size_t size);
  48. static void serial_soft_trans_irq(void* parameter);
  49. /* ----------------------- Start implementation -----------------------------*/
  50. BOOL xMBPortSerialInit(UCHAR ucPORT, ULONG ulBaudRate, UCHAR ucDataBits,
  51. eMBParity eParity)
  52. {
  53. rt_device_t dev = RT_NULL;
  54. char uart_name[20];
  55. /**
  56. * set 485 mode receive and transmit control IO
  57. * @note MODBUS_SLAVE_RT_CONTROL_PIN_INDEX need be defined by user
  58. */
  59. #if defined(RT_MODBUS_SLAVE_USE_CONTROL_PIN)
  60. rt_pin_mode(MODBUS_SLAVE_RT_CONTROL_PIN_INDEX, PIN_MODE_OUTPUT);
  61. #endif
  62. /* set serial name */
  63. rt_snprintf(uart_name,sizeof(uart_name), "uart%d", ucPORT);
  64. dev = rt_device_find(uart_name);
  65. if(dev == RT_NULL)
  66. {
  67. /* can not find uart */
  68. return FALSE;
  69. }
  70. else
  71. {
  72. serial = (struct rt_serial_device*)dev;
  73. }
  74. /* set serial configure parameter */
  75. serial->config.baud_rate = ulBaudRate;
  76. serial->config.stop_bits = STOP_BITS_1;
  77. switch(eParity){
  78. case MB_PAR_NONE: {
  79. serial->config.data_bits = DATA_BITS_8;
  80. serial->config.parity = PARITY_NONE;
  81. break;
  82. }
  83. case MB_PAR_ODD: {
  84. serial->config.data_bits = DATA_BITS_9;
  85. serial->config.parity = PARITY_ODD;
  86. break;
  87. }
  88. case MB_PAR_EVEN: {
  89. serial->config.data_bits = DATA_BITS_9;
  90. serial->config.parity = PARITY_EVEN;
  91. break;
  92. }
  93. }
  94. /* set serial configure */
  95. serial->ops->configure(serial, &(serial->config));
  96. /* open serial device */
  97. if (!rt_device_open(&serial->parent, RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_INT_RX)) {
  98. rt_device_set_rx_indicate(&serial->parent, serial_rx_ind);
  99. } else {
  100. return FALSE;
  101. }
  102. /* software initialize */
  103. rt_event_init(&event_serial, "slave event", RT_IPC_FLAG_PRIO);
  104. rt_thread_init(&thread_serial_soft_trans_irq,
  105. "slave trans",
  106. serial_soft_trans_irq,
  107. RT_NULL,
  108. serial_soft_trans_irq_stack,
  109. sizeof(serial_soft_trans_irq_stack),
  110. 10, 5);
  111. rt_thread_startup(&thread_serial_soft_trans_irq);
  112. return TRUE;
  113. }
  114. void vMBPortSerialEnable(BOOL xRxEnable, BOOL xTxEnable)
  115. {
  116. rt_uint32_t recved_event;
  117. if (xRxEnable)
  118. {
  119. /* enable RX interrupt */
  120. serial->ops->control(serial, RT_DEVICE_CTRL_SET_INT, (void *)RT_DEVICE_FLAG_INT_RX);
  121. /* switch 485 to receive mode */
  122. #if defined(RT_MODBUS_SLAVE_USE_CONTROL_PIN)
  123. rt_pin_write(MODBUS_SLAVE_RT_CONTROL_PIN_INDEX, PIN_LOW);
  124. #endif
  125. }
  126. else
  127. {
  128. /* switch 485 to transmit mode */
  129. #if defined(RT_MODBUS_SLAVE_USE_CONTROL_PIN)
  130. rt_pin_write(MODBUS_SLAVE_RT_CONTROL_PIN_INDEX, PIN_HIGH);
  131. #endif
  132. /* disable RX interrupt */
  133. serial->ops->control(serial, RT_DEVICE_CTRL_CLR_INT, (void *)RT_DEVICE_FLAG_INT_RX);
  134. }
  135. if (xTxEnable)
  136. {
  137. /* start serial transmit */
  138. rt_event_send(&event_serial, EVENT_SERIAL_TRANS_START);
  139. }
  140. else
  141. {
  142. /* stop serial transmit */
  143. rt_event_recv(&event_serial, EVENT_SERIAL_TRANS_START,
  144. RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR, 0,
  145. &recved_event);
  146. }
  147. }
  148. void vMBPortClose(void)
  149. {
  150. serial->parent.close(&(serial->parent));
  151. }
  152. BOOL xMBPortSerialPutByte(CHAR ucByte)
  153. {
  154. serial->parent.write(&(serial->parent), 0, &ucByte, 1);
  155. return TRUE;
  156. }
  157. BOOL xMBPortSerialGetByte(CHAR * pucByte)
  158. {
  159. serial->parent.read(&(serial->parent), 0, pucByte, 1);
  160. return TRUE;
  161. }
  162. /*
  163. * Create an interrupt handler for the transmit buffer empty interrupt
  164. * (or an equivalent) for your target processor. This function should then
  165. * call pxMBFrameCBTransmitterEmpty( ) which tells the protocol stack that
  166. * a new character can be sent. The protocol stack will then call
  167. * xMBPortSerialPutByte( ) to send the character.
  168. */
  169. void prvvUARTTxReadyISR(void)
  170. {
  171. pxMBFrameCBTransmitterEmpty();
  172. }
  173. /*
  174. * Create an interrupt handler for the receive interrupt for your target
  175. * processor. This function should then call pxMBFrameCBByteReceived( ). The
  176. * protocol stack will then call xMBPortSerialGetByte( ) to retrieve the
  177. * character.
  178. */
  179. void prvvUARTRxISR(void)
  180. {
  181. pxMBFrameCBByteReceived();
  182. }
  183. /**
  184. * Software simulation serial transmit IRQ handler.
  185. *
  186. * @param parameter parameter
  187. */
  188. static void serial_soft_trans_irq(void* parameter) {
  189. rt_uint32_t recved_event;
  190. while (1)
  191. {
  192. /* waiting for serial transmit start */
  193. rt_event_recv(&event_serial, EVENT_SERIAL_TRANS_START, RT_EVENT_FLAG_OR,
  194. RT_WAITING_FOREVER, &recved_event);
  195. /* execute modbus callback */
  196. prvvUARTTxReadyISR();
  197. }
  198. }
  199. /**
  200. * This function is serial receive callback function
  201. *
  202. * @param dev the device of serial
  203. * @param size the data size that receive
  204. *
  205. * @return return RT_EOK
  206. */
  207. static rt_err_t serial_rx_ind(rt_device_t dev, rt_size_t size) {
  208. while(size--)
  209. prvvUARTRxISR();
  210. return RT_EOK;
  211. }