portevent_m.c 9.4 KB

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