portevent_m.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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: portevent_m.c v 1.60 2013/08/13 15:07:05 Armink add Master Functions$
  20. */
  21. /* ----------------------- Modbus includes ----------------------------------*/
  22. #include "mb.h"
  23. #include "mb_m.h"
  24. #include "mbport.h"
  25. #include "port.h"
  26. #if MB_MASTER_RTU_ENABLED > 0 || MB_MASTER_ASCII_ENABLED > 0
  27. /* ----------------------- Defines ------------------------------------------*/
  28. /* ----------------------- Variables ----------------------------------------*/
  29. static struct rt_semaphore xMasterRunRes;
  30. static struct rt_event xMasterOsEvent;
  31. /* ----------------------- Start implementation -----------------------------*/
  32. BOOL
  33. xMBMasterPortEventInit( void )
  34. {
  35. rt_event_init(&xMasterOsEvent,"master event",RT_IPC_FLAG_PRIO);
  36. return TRUE;
  37. }
  38. BOOL
  39. xMBMasterPortEventPost( eMBMasterEventType eEvent )
  40. {
  41. rt_event_send(&xMasterOsEvent, eEvent);
  42. return TRUE;
  43. }
  44. BOOL
  45. xMBMasterPortEventGet( eMBMasterEventType * eEvent )
  46. {
  47. rt_uint32_t recvedEvent;
  48. /* waiting forever OS event */
  49. rt_event_recv(&xMasterOsEvent,
  50. EV_MASTER_READY | EV_MASTER_FRAME_RECEIVED | EV_MASTER_EXECUTE |
  51. EV_MASTER_FRAME_SENT | EV_MASTER_ERROR_PROCESS,
  52. RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR, RT_WAITING_FOREVER,
  53. &recvedEvent);
  54. /* the enum type couldn't convert to int type */
  55. switch (recvedEvent)
  56. {
  57. case EV_MASTER_READY:
  58. *eEvent = EV_MASTER_READY;
  59. break;
  60. case EV_MASTER_FRAME_RECEIVED:
  61. *eEvent = EV_MASTER_FRAME_RECEIVED;
  62. break;
  63. case EV_MASTER_EXECUTE:
  64. *eEvent = EV_MASTER_EXECUTE;
  65. break;
  66. case EV_MASTER_FRAME_SENT:
  67. *eEvent = EV_MASTER_FRAME_SENT;
  68. break;
  69. case EV_MASTER_ERROR_PROCESS:
  70. *eEvent = EV_MASTER_ERROR_PROCESS;
  71. break;
  72. }
  73. return TRUE;
  74. }
  75. /**
  76. * This function is initialize the OS resource for modbus master.
  77. * Note:The resource is define by OS.If you not use OS this function can be empty.
  78. *
  79. */
  80. void vMBMasterOsResInit( void )
  81. {
  82. rt_sem_init(&xMasterRunRes, "master res", 0x01 , RT_IPC_FLAG_PRIO);
  83. }
  84. /**
  85. * This function is take Mobus Master running resource.
  86. * Note:The resource is define by Operating System.If you not use OS this function can be just return TRUE.
  87. *
  88. * @param lTimeOut the waiting time.
  89. *
  90. * @return resource taked result
  91. */
  92. BOOL xMBMasterRunResTake( LONG lTimeOut )
  93. {
  94. /*If waiting time is -1 .It will wait forever */
  95. return rt_sem_take(&xMasterRunRes, lTimeOut) ? FALSE : TRUE ;
  96. }
  97. /**
  98. * This function is release Mobus Master running resource.
  99. * Note:The resource is define by Operating System.If you not use OS this function can be empty.
  100. *
  101. */
  102. void vMBMasterRunResRelease( void )
  103. {
  104. /* release resource */
  105. rt_sem_release(&xMasterRunRes);
  106. }
  107. /**
  108. * This is modbus master respond timeout error process callback function.
  109. * @note There functions will block modbus master poll while execute OS waiting.
  110. * So,for real-time of system.Do not execute too much waiting process.
  111. *
  112. * @param ucDestAddress destination salve address
  113. * @param pucPDUData PDU buffer data
  114. * @param ucPDULength PDU buffer length
  115. *
  116. */
  117. void vMBMasterErrorCBRespondTimeout(UCHAR ucDestAddress, const UCHAR* pucPDUData,
  118. USHORT ucPDULength) {
  119. /**
  120. * @note This code is use OS's event mechanism for modbus master protocol stack.
  121. * If you don't use OS, you can change it.
  122. */
  123. rt_event_send(&xMasterOsEvent, EV_MASTER_ERROR_RESPOND_TIMEOUT);
  124. /* You can add your code under here. */
  125. }
  126. /**
  127. * This is modbus master receive data error process callback function.
  128. * @note There functions will block modbus master poll while execute OS waiting.
  129. * So,for real-time of system.Do not execute too much waiting process.
  130. *
  131. * @param ucDestAddress destination salve address
  132. * @param pucPDUData PDU buffer data
  133. * @param ucPDULength PDU buffer length
  134. *
  135. */
  136. void vMBMasterErrorCBReceiveData(UCHAR ucDestAddress, const UCHAR* pucPDUData,
  137. USHORT ucPDULength) {
  138. /**
  139. * @note This code is use OS's event mechanism for modbus master protocol stack.
  140. * If you don't use OS, you can change it.
  141. */
  142. rt_event_send(&xMasterOsEvent, EV_MASTER_ERROR_RECEIVE_DATA);
  143. /* You can add your code under here. */
  144. }
  145. /**
  146. * This is modbus master execute function error process callback function.
  147. * @note There functions will block modbus master poll while execute OS waiting.
  148. * So,for real-time of system.Do not execute too much waiting process.
  149. *
  150. * @param ucDestAddress destination salve address
  151. * @param pucPDUData PDU buffer data
  152. * @param ucPDULength PDU buffer length
  153. *
  154. */
  155. void vMBMasterErrorCBExecuteFunction(UCHAR ucDestAddress, const UCHAR* pucPDUData,
  156. USHORT ucPDULength) {
  157. /**
  158. * @note This code is use OS's event mechanism for modbus master protocol stack.
  159. * If you don't use OS, you can change it.
  160. */
  161. rt_event_send(&xMasterOsEvent, EV_MASTER_ERROR_EXECUTE_FUNCTION);
  162. /* You can add your code under here. */
  163. }
  164. /**
  165. * This is modbus master request process success callback function.
  166. * @note There functions will block modbus master poll while execute OS waiting.
  167. * So,for real-time of system.Do not execute too much waiting process.
  168. *
  169. */
  170. void vMBMasterCBRequestScuuess( void ) {
  171. /**
  172. * @note This code is use OS's event mechanism for modbus master protocol stack.
  173. * If you don't use OS, you can change it.
  174. */
  175. rt_event_send(&xMasterOsEvent, EV_MASTER_PROCESS_SUCESS);
  176. /* You can add your code under here. */
  177. }
  178. /**
  179. * This function is wait for modbus master request finish and return result.
  180. * Waiting result include request process success, request respond timeout,
  181. * receive data error and execute function error.You can use the above callback function.
  182. * @note If you are use OS, you can use OS's event mechanism. Otherwise you have to run
  183. * much user custom delay for waiting.
  184. *
  185. * @return request error code
  186. */
  187. eMBMasterReqErrCode eMBMasterWaitRequestFinish( void ) {
  188. eMBMasterReqErrCode eErrStatus = MB_MRE_NO_ERR;
  189. rt_uint32_t recvedEvent;
  190. /* waiting for OS event */
  191. rt_event_recv(&xMasterOsEvent,
  192. EV_MASTER_PROCESS_SUCESS | EV_MASTER_ERROR_RESPOND_TIMEOUT
  193. | EV_MASTER_ERROR_RECEIVE_DATA
  194. | EV_MASTER_ERROR_EXECUTE_FUNCTION,
  195. RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR, RT_WAITING_FOREVER,
  196. &recvedEvent);
  197. switch (recvedEvent)
  198. {
  199. case EV_MASTER_PROCESS_SUCESS:
  200. break;
  201. case EV_MASTER_ERROR_RESPOND_TIMEOUT:
  202. {
  203. eErrStatus = MB_MRE_TIMEDOUT;
  204. break;
  205. }
  206. case EV_MASTER_ERROR_RECEIVE_DATA:
  207. {
  208. eErrStatus = MB_MRE_REV_DATA;
  209. break;
  210. }
  211. case EV_MASTER_ERROR_EXECUTE_FUNCTION:
  212. {
  213. eErrStatus = MB_MRE_EXE_FUN;
  214. break;
  215. }
  216. }
  217. return eErrStatus;
  218. }
  219. #endif