portserial.c 6.7 KB

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