portserial_m.c 6.9 KB

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