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