portevent_m.c
9.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/*
* FreeModbus Libary: RT-Thread Port
* Copyright (C) 2013 Armink <armink.ztl@gmail.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* File: $Id: portevent_m.c v 1.60 2013/08/13 15:07:05 Armink add Master Functions$
*/
/* ----------------------- Modbus includes ----------------------------------*/
#include "mb.h"
#include "mb_m.h"
#include "mbport.h"
#include "port.h"
#include "mbconfig.h"
#include "bsp.h"
#if MB_MASTER_RTU_ENABLED > 0 || MB_MASTER_ASCII_ENABLED > 0
/* ----------------------- Defines ------------------------------------------*/
/* ----------------------- Variables ----------------------------------------*/
//static struct rt_semaphore xMasterRunRes;
//static struct rt_event xMasterOsEvent;
static OS_FLAG_GRP PtrEvent;
static OS_SEM Mbsem;
/* ----------------------- Start implementation -----------------------------*/
BOOL
xMBMasterPortEventInit( void )
{
OS_ERR mberror;
OSFlagCreate(&PtrEvent,"MBevent",0,&mberror);
return TRUE;
// rt_event_init(&xMasterOsEvent,"master event",RT_IPC_FLAG_PRIO);
// return TRUE;
}
BOOL
xMBMasterPortEventPost( eMBMasterEventType eEvent )
{
OS_ERR mberror;
OS_FLAGS fla;
fla = OSFlagPost(&PtrEvent,eEvent,OS_OPT_POST_FLAG_SET,&mberror);
return TRUE;
// rt_event_send(&xMasterOsEvent, eEvent);
// return TRUE;
}
BOOL
xMBMasterPortEventGet( eMBMasterEventType * eEvent )
{
OS_ERR mberror;
*eEvent = OSFlagPend(&PtrEvent,
EV_MASTER_READY | EV_MASTER_FRAME_RECEIVED | EV_MASTER_EXECUTE |
EV_MASTER_FRAME_SENT | EV_MASTER_ERROR_PROCESS,
0,
OS_OPT_PEND_FLAG_SET_ANY + OS_OPT_PEND_FLAG_CONSUME + OS_OPT_PEND_NON_BLOCKING,
NULL,
&mberror);
return TRUE;
// rt_uint32_t recvedEvent;
// /* waiting forever OS event */
// rt_event_recv(&xMasterOsEvent,
// EV_MASTER_READY | EV_MASTER_FRAME_RECEIVED | EV_MASTER_EXECUTE |
// EV_MASTER_FRAME_SENT | EV_MASTER_ERROR_PROCESS,
// RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR, RT_WAITING_FOREVER,
// &recvedEvent);
// /* the enum type couldn't convert to int type */
// switch (recvedEvent)
// {
// case EV_MASTER_READY:
// *eEvent = EV_MASTER_READY;
// break;
// case EV_MASTER_FRAME_RECEIVED:
// *eEvent = EV_MASTER_FRAME_RECEIVED;
// break;
// case EV_MASTER_EXECUTE:
// *eEvent = EV_MASTER_EXECUTE;
// break;
// case EV_MASTER_FRAME_SENT:
// *eEvent = EV_MASTER_FRAME_SENT;
// break;
// case EV_MASTER_ERROR_PROCESS:
// *eEvent = EV_MASTER_ERROR_PROCESS;
// break;
// }
// return TRUE;
}
/**
* This function is initialize the OS resource for modbus master.
* Note:The resource is define by OS.If you not use OS this function can be empty.
*
*/
void vMBMasterOsResInit( void )
{
OS_ERR semerror;
OSSemCreate(&Mbsem,"MbSemm",1,&semerror);
// rt_sem_init(&xMasterRunRes, "master res", 0x01 , RT_IPC_FLAG_PRIO);
}
/**
* This function is take Mobus Master running resource.
* Note:The resource is define by Operating System.If you not use OS this function can be just return TRUE.
*
* @param lTimeOut the waiting time.
*
* @return resource taked result
*/
BOOL xMBMasterRunResTake( LONG lTimeOut )
{
OS_ERR semerror;
OSSemPend(&Mbsem,lTimeOut,OS_OPT_PEND_NON_BLOCKING,0,&semerror);
return semerror ? FALSE : TRUE;
/*If waiting time is -1 .It will wait forever */
// return rt_sem_take(&xMasterRunRes, lTimeOut) ? FALSE : TRUE ;
}
/**
* This function is release Mobus Master running resource.
* Note:The resource is define by Operating System.If you not use OS this function can be empty.
*
*/
void vMBMasterRunResRelease( void )
{
OS_ERR semerror;
OSSemPost(&Mbsem,OS_OPT_POST_ALL,&semerror);
/* release resource */
// rt_sem_release(&xMasterRunRes);
}
/**
* This is modbus master respond timeout error process callback function.
* @note There functions will block modbus master poll while execute OS waiting.
* So,for real-time of system.Do not execute too much waiting process.
*
* @param ucDestAddress destination salve address
* @param pucPDUData PDU buffer data
* @param ucPDULength PDU buffer length
*
*/
void vMBMasterErrorCBRespondTimeout(UCHAR ucDestAddress, const UCHAR* pucPDUData,
USHORT ucPDULength) {
OS_ERR mberror;
OSFlagPost(&PtrEvent,EV_MASTER_ERROR_RESPOND_TIMEOUT,OS_OPT_POST_FLAG_SET,&mberror);
/**
* @note This code is use OS's event mechanism for modbus master protocol stack.
* If you don't use OS, you can change it.
*/
//rt_event_send(&xMasterOsEvent, EV_MASTER_ERROR_RESPOND_TIMEOUT);
/* You can add your code under here. */
}
/**
* This is modbus master receive data error process callback function.
* @note There functions will block modbus master poll while execute OS waiting.
* So,for real-time of system.Do not execute too much waiting process.
*
* @param ucDestAddress destination salve address
* @param pucPDUData PDU buffer data
* @param ucPDULength PDU buffer length
*
*/
void vMBMasterErrorCBReceiveData(UCHAR ucDestAddress, const UCHAR* pucPDUData,
USHORT ucPDULength) {
OS_ERR mberror;
OSFlagPost(&PtrEvent,EV_MASTER_ERROR_RECEIVE_DATA,OS_OPT_POST_FLAG_SET,&mberror);
/**
* @note This code is use OS's event mechanism for modbus master protocol stack.
* If you don't use OS, you can change it.
*/
//rt_event_send(&xMasterOsEvent, EV_MASTER_ERROR_RECEIVE_DATA);
/* You can add your code under here. */
}
/**
* This is modbus master execute function error process callback function.
* @note There functions will block modbus master poll while execute OS waiting.
* So,for real-time of system.Do not execute too much waiting process.
*
* @param ucDestAddress destination salve address
* @param pucPDUData PDU buffer data
* @param ucPDULength PDU buffer length
*
*/
void vMBMasterErrorCBExecuteFunction(UCHAR ucDestAddress, const UCHAR* pucPDUData,
USHORT ucPDULength) {
OS_ERR mberror;
OSFlagPost(&PtrEvent,EV_MASTER_ERROR_EXECUTE_FUNCTION,OS_OPT_POST_FLAG_SET,&mberror);
/**
* @note This code is use OS's event mechanism for modbus master protocol stack.
* If you don't use OS, you can change it.
*/
//rt_event_send(&xMasterOsEvent, EV_MASTER_ERROR_EXECUTE_FUNCTION);
/* You can add your code under here. */
}
/**
* This is modbus master request process success callback function.
* @note There functions will block modbus master poll while execute OS waiting.
* So,for real-time of system.Do not execute too much waiting process.
*
*/
void vMBMasterCBRequestScuuess( void ) {
OS_ERR mberror;
OSFlagPost(&PtrEvent,EV_MASTER_PROCESS_SUCESS,OS_OPT_POST_FLAG_SET,&mberror);
/**
* @note This code is use OS's event mechanism for modbus master protocol stack.
* If you don't use OS, you can change it.
*/
//rt_event_send(&xMasterOsEvent, EV_MASTER_PROCESS_SUCESS);
/* You can add your code under here. */
}
/**
* This function is wait for modbus master request finish and return result.
* Waiting result include request process success, request respond timeout,
* receive data error and execute function error.You can use the above callback function.
* @note If you are use OS, you can use OS's event mechanism. Otherwise you have to run
* much user custom delay for waiting.
*
* @return request error code
*/
eMBMasterReqErrCode eMBMasterWaitRequestFinish( void ) {
eMBMasterReqErrCode eErrStatus = MB_MRE_NO_ERR;
OS_ERR mberror;
u32 eEvent;
eEvent = OSFlagPend(&PtrEvent,
EV_MASTER_PROCESS_SUCESS | EV_MASTER_ERROR_RESPOND_TIMEOUT |
EV_MASTER_ERROR_RECEIVE_DATA |
EV_MASTER_ERROR_EXECUTE_FUNCTION,
0,
OS_OPT_PEND_FLAG_SET_ANY + OS_OPT_PEND_FLAG_CONSUME + OS_OPT_PEND_NON_BLOCKING,
NULL,
&mberror);
switch (eEvent)
{
case EV_MASTER_PROCESS_SUCESS:
break;
case EV_MASTER_ERROR_RESPOND_TIMEOUT:
{
eErrStatus = MB_MRE_TIMEDOUT;
break;
}
case EV_MASTER_ERROR_RECEIVE_DATA:
{
eErrStatus = MB_MRE_REV_DATA;
break;
}
case EV_MASTER_ERROR_EXECUTE_FUNCTION:
{
eErrStatus = MB_MRE_EXE_FUN;
break;
}
}
return eErrStatus;
// rt_uint32_t recvedEvent;
// /* waiting for OS event */
// rt_event_recv(&xMasterOsEvent,
// EV_MASTER_PROCESS_SUCESS | EV_MASTER_ERROR_RESPOND_TIMEOUT
// | EV_MASTER_ERROR_RECEIVE_DATA
// | EV_MASTER_ERROR_EXECUTE_FUNCTION,
// RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR, RT_WAITING_FOREVER,
// &recvedEvent);
// switch (recvedEvent)
// {
// case EV_MASTER_PROCESS_SUCESS:
// break;
// case EV_MASTER_ERROR_RESPOND_TIMEOUT:
// {
// eErrStatus = MB_MRE_TIMEDOUT;
// break;
// }
// case EV_MASTER_ERROR_RECEIVE_DATA:
// {
// eErrStatus = MB_MRE_REV_DATA;
// break;
// }
// case EV_MASTER_ERROR_EXECUTE_FUNCTION:
// {
// eErrStatus = MB_MRE_EXE_FUN;
// break;
// }
// }
// return eErrStatus;
}
#endif