forked from apache/nuttx-apps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmb_m.c
487 lines (405 loc) · 14.3 KB
/
mb_m.c
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
/****************************************************************************
* FreeModbus Library: A portable Modbus implementation for Modbus ASCII/RTU.
*
* Copyright (C) 2013 Armink <[email protected]>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <stdlib.h>
#include <string.h>
#include "port.h"
#include "modbus/mb.h"
#include "modbus/mb_m.h"
#include "modbus/mbframe.h"
#include "modbus/mbproto.h"
#include "modbus/mbfunc.h"
#include "modbus/mbport.h"
#ifdef CONFIG_MB_RTU_MASTER
# include "mbrtu_m.h"
#endif
#ifdef CONFIG_MB_ASCII_MASTER
# include "mbascii.h"
#endif
#ifdef CONFIG_MB_TCP_MASTER
# include "mbtcp.h"
#endif
#if defined(CONFIG_MB_RTU_MASTER) || defined(CONFIG_MB_ASCII_MASTER)
#ifndef CONFIG_MB_PORT_HAS_CLOSE
# define MB_PORT_HAS_CLOSE 0
#else
# define MB_PORT_HAS_CLOSE 1
#endif
/****************************************************************************
* Private Data
****************************************************************************/
static uint8_t ucMBMasterDestAddress;
static bool xMBRunInMasterMode = false;
static eMBMasterErrorEventType eMBMasterCurErrorType;
static enum
{
STATE_ENABLED,
STATE_DISABLED,
STATE_NOT_INITIALIZED
} eMBState = STATE_NOT_INITIALIZED;
/* Functions pointer which are initialized in eMBInit( ). Depending on the
* mode (RTU or ASCII) the are set to the correct implementations.
* Using for Modbus Master,Add by Armink 20130813
*/
static peMBFrameSend peMBMasterFrameSendCur;
static pvMBFrameStart pvMBMasterFrameStartCur;
static pvMBFrameStop pvMBMasterFrameStopCur;
static peMBFrameReceive peMBMasterFrameReceiveCur;
static pvMBFrameClose pvMBMasterFrameCloseCur;
/* Callback functions required by the porting layer. They are called when
* an external event has happened which includes a timeout or the reception
* or transmission of a character.
* Using for Modbus Master,Add by Armink 20130813
*/
bool(*pxMBMasterFrameCBByteReceived) (void);
bool(*pxMBMasterFrameCBTransmitterEmpty) (void);
bool(*pxMBMasterPortCBTimerExpired) (void);
bool(*pxMBMasterFrameCBReceiveFSMCur) (void);
bool(*pxMBMasterFrameCBTransmitFSMCur) (void);
/* An array of Modbus functions handlers which associates Modbus function
* codes with implementing functions.
*/
static xMBFunctionHandler xMasterFuncHandlers[CONFIG_MB_FUNC_HANDLERS_MAX] = {
#ifdef CONFIG_MB_FUNC_OTHER_REP_SLAVEID_ENABLED
/* TODO Add Master function define */
{MB_FUNC_OTHER_REPORT_SLAVEID, eMBFuncReportSlaveID},
#endif
#ifdef CONFIG_MB_MASTER_FUNC_READ_INPUT_ENABLED
{MB_FUNC_READ_INPUT_REGISTER, eMBMasterFuncReadInputRegister},
#endif
#ifdef CONFIG_MB_MASTER_FUNC_READ_HOLDING_ENABLED
{MB_FUNC_READ_HOLDING_REGISTER, eMBMasterFuncReadHoldingRegister},
#endif
#ifdef CONFIG_MB_MASTER_FUNC_WRITE_MULTIPLE_HOLDING_ENABLED
{MB_FUNC_WRITE_MULTIPLE_REGISTERS, eMBMasterFuncWriteMultipleHoldingRegister},
#endif
#ifdef CONFIG_MB_MASTER_FUNC_WRITE_HOLDING_ENABLED
{MB_FUNC_WRITE_REGISTER, eMBMasterFuncWriteHoldingRegister},
#endif
#ifdef CONFIG_MB_MASTER_FUNC_READWRITE_HOLDING_ENABLED
{MB_FUNC_READWRITE_MULTIPLE_REGISTERS,
eMBMasterFuncReadWriteMultipleHoldingRegister},
#endif
#ifdef CONFIG_MB_MASTER_FUNC_READ_COILS_ENABLED
{MB_FUNC_READ_COILS, eMBMasterFuncReadCoils},
#endif
#ifdef CONFIG_MB_MASTER_FUNC_WRITE_COIL_ENABLED
{MB_FUNC_WRITE_SINGLE_COIL, eMBMasterFuncWriteCoil},
#endif
#ifdef CONFIG_MB_MASTER_FUNC_WRITE_MULTIPLE_COILS_ENABLED
{MB_FUNC_WRITE_MULTIPLE_COILS, eMBMasterFuncWriteMultipleCoils},
#endif
#ifdef CONFIG_MB_MASTER_FUNC_READ_DISCRETE_INPUTS_ENABLED
{MB_FUNC_READ_DISCRETE_INPUTS, eMBMasterFuncReadDiscreteInputs},
#endif
};
/****************************************************************************
* Public Functions
****************************************************************************/
eMBErrorCode eMBMasterInit(eMBMode eMode, uint8_t ucPort,
speed_t ulBaudRate, eMBParity eParity)
{
eMBErrorCode eStatus = MB_ENOERR;
switch (eMode)
{
#ifdef CONFIG_MB_RTU_MASTER
case MB_RTU:
pvMBMasterFrameStartCur = eMBMasterRTUStart;
pvMBMasterFrameStopCur = eMBMasterRTUStop;
peMBMasterFrameSendCur = eMBMasterRTUSend;
peMBMasterFrameReceiveCur = eMBMasterRTUReceive;
pvMBMasterFrameCloseCur = MB_PORT_HAS_CLOSE ? vMBMasterPortClose : NULL;
pxMBMasterFrameCBByteReceived = xMBMasterRTUReceiveFSM;
pxMBMasterFrameCBTransmitterEmpty = xMBMasterRTUTransmitFSM;
pxMBMasterPortCBTimerExpired = xMBMasterRTUTimerExpired;
eStatus = eMBMasterRTUInit(ucPort, ulBaudRate, eParity);
break;
#endif
#ifdef CONFIG_MB_ASCII_MASTER
case MB_ASCII:
pvMBMasterFrameStartCur = eMBMasterASCIIStart;
pvMBMasterFrameStopCur = eMBMasterASCIIStop;
peMBMasterFrameSendCur = eMBMasterASCIISend;
peMBMasterFrameReceiveCur = eMBMasterASCIIReceive;
pvMBMasterFrameCloseCur = MB_PORT_HAS_CLOSE ? vMBMasterPortClose : NULL;
pxMBMasterFrameCBByteReceived = xMBMasterASCIIReceiveFSM;
pxMBMasterFrameCBTransmitterEmpty = xMBMasterASCIITransmitFSM;
pxMBMasterPortCBTimerExpired = xMBMasterASCIITimerT1SExpired;
eStatus = eMBMasterASCIIInit(ucPort, ulBaudRate, eParity);
break;
#endif
default:
eStatus = MB_EINVAL;
break;
}
if (eStatus == MB_ENOERR)
{
if (!xMBMasterPortEventInit())
{
/* Port dependent event module initialization failed. */
eStatus = MB_EPORTERR;
}
else
{
eMBState = STATE_DISABLED;
}
/* Initialize the OS resource for modbus master. */
vMBMasterOsResInit();
}
return eStatus;
}
eMBErrorCode eMBMasterClose(void)
{
eMBErrorCode eStatus = MB_ENOERR;
if (eMBState == STATE_DISABLED)
{
if (pvMBMasterFrameCloseCur != NULL)
{
pvMBMasterFrameCloseCur();
}
}
else
{
eStatus = MB_EILLSTATE;
}
return eStatus;
}
eMBErrorCode eMBMasterEnable(void)
{
eMBErrorCode eStatus = MB_ENOERR;
if (eMBState == STATE_DISABLED)
{
/* Activate the protocol stack. */
pvMBMasterFrameStartCur();
eMBState = STATE_ENABLED;
}
else
{
eStatus = MB_EILLSTATE;
}
return eStatus;
}
eMBErrorCode eMBMasterDisable(void)
{
eMBErrorCode eStatus;
if (eMBState == STATE_ENABLED)
{
pvMBMasterFrameStopCur();
eMBState = STATE_DISABLED;
eStatus = MB_ENOERR;
}
else if (eMBState == STATE_DISABLED)
{
eStatus = MB_ENOERR;
}
else
{
eStatus = MB_EILLSTATE;
}
return eStatus;
}
eMBErrorCode eMBMasterPoll(void)
{
static uint8_t *ucMBFrame;
static uint8_t ucRcvAddress;
static uint8_t ucFunctionCode;
static uint16_t usLength;
static eMBException eException;
int i;
int j;
eMBErrorCode eStatus = MB_ENOERR;
eMBMasterEventType eEvent;
eMBMasterErrorEventType errorType;
/* Check if the protocol stack is ready. */
if (eMBState != STATE_ENABLED)
{
return MB_EILLSTATE;
}
/* Check if there is a event available. If not return control to caller.
* Otherwise we will handle the event.
*/
if (xMBMasterPortEventGet(&eEvent) == true)
{
switch (eEvent)
{
case EV_MASTER_READY:
break;
case EV_MASTER_FRAME_RECEIVED:
eStatus =
peMBMasterFrameReceiveCur(&ucRcvAddress, &ucMBFrame, &usLength);
/* Check if the frame is for us. If not, send an error process event. */
if ((eStatus == MB_ENOERR) &&
(ucRcvAddress == ucMBMasterGetDestAddress()))
{
xMBMasterPortEventPost(EV_MASTER_EXECUTE);
}
else
{
vMBMasterSetErrorType(EV_ERROR_RECEIVE_DATA);
xMBMasterPortEventPost(EV_MASTER_ERROR_PROCESS);
}
break;
case EV_MASTER_EXECUTE:
ucFunctionCode = ucMBFrame[MB_PDU_FUNC_OFF];
eException = MB_EX_ILLEGAL_FUNCTION;
/* If receive frame has exception. The receive function code highest
* bit is 1.
*/
if (ucFunctionCode >> 7)
{
eException = (eMBException) ucMBFrame[MB_PDU_DATA_OFF];
}
else
{
for (i = 0; i < CONFIG_MB_FUNC_HANDLERS_MAX; i++)
{
/* No more function handlers registered. Abort. */
if (xMasterFuncHandlers[i].ucFunctionCode == 0)
{
break;
}
else if (xMasterFuncHandlers[i].ucFunctionCode ==
ucFunctionCode)
{
vMBMasterSetCBRunInMasterMode(true);
/* If master request is broadcast, the master need
* execute function for all slave. */
if (xMBMasterRequestIsBroadcast())
{
usLength = usMBMasterGetPDUSndLength();
for (j = 1; j <= CONFIG_MB_MASTER_TOTAL_SLAVE_NUM;
j++)
{
vMBMasterSetDestAddress(j);
eException =
xMasterFuncHandlers[i].pxHandler(ucMBFrame,
&usLength);
}
}
else
{
eException =
xMasterFuncHandlers[i].pxHandler(ucMBFrame,
&usLength);
}
vMBMasterSetCBRunInMasterMode(false);
break;
}
}
}
/* If master has exception, Master will send error process. Otherwise
* the Master is idle.
*/
if (eException != MB_EX_NONE)
{
vMBMasterSetErrorType(EV_ERROR_EXECUTE_FUNCTION);
xMBMasterPortEventPost(EV_MASTER_ERROR_PROCESS);
}
else
{
vMBMasterCBRequestSuccess();
vMBMasterRunResRelease();
}
break;
case EV_MASTER_FRAME_SENT:
/* Master is busy now. */
vMBMasterGetPDUSndBuf(&ucMBFrame);
eStatus =
peMBMasterFrameSendCur(ucMBMasterGetDestAddress(), ucMBFrame,
usMBMasterGetPDUSndLength());
break;
case EV_MASTER_ERROR_PROCESS:
/* Execute specified error process callback function. */
errorType = eMBMasterGetErrorType();
vMBMasterGetPDUSndBuf(&ucMBFrame);
switch (errorType)
{
case EV_ERROR_RESPOND_TIMEOUT:
vMBMasterErrorCBRespondTimeout(ucMBMasterGetDestAddress(),
ucMBFrame,
usMBMasterGetPDUSndLength());
break;
case EV_ERROR_RECEIVE_DATA:
vMBMasterErrorCBReceiveData(ucMBMasterGetDestAddress(),
ucMBFrame,
usMBMasterGetPDUSndLength());
break;
case EV_ERROR_EXECUTE_FUNCTION:
vMBMasterErrorCBExecuteFunction(ucMBMasterGetDestAddress(),
ucMBFrame,
usMBMasterGetPDUSndLength());
break;
}
vMBMasterRunResRelease();
break;
case EV_MASTER_PROCESS_SUCCESS:
case EV_MASTER_ERROR_RESPOND_TIMEOUT:
case EV_MASTER_ERROR_RECEIVE_DATA:
case EV_MASTER_ERROR_EXECUTE_FUNCTION:
default:
break;
}
}
return MB_ENOERR;
}
/* Get whether the Modbus Master is run in master mode.*/
bool xMBMasterGetCBRunInMasterMode(void)
{
return xMBRunInMasterMode;
}
/* Set whether the Modbus Master is run in master mode.*/
void vMBMasterSetCBRunInMasterMode(bool IsMasterMode)
{
xMBRunInMasterMode = IsMasterMode;
}
/* Get Modbus Master send destination address. */
uint8_t ucMBMasterGetDestAddress(void)
{
return ucMBMasterDestAddress;
}
/* Set Modbus Master send destination address. */
void vMBMasterSetDestAddress(uint8_t Address)
{
ucMBMasterDestAddress = Address;
}
/* Get Modbus Master current error event type. */
eMBMasterErrorEventType eMBMasterGetErrorType(void)
{
return eMBMasterCurErrorType;
}
/* Set Modbus Master current error event type. */
void vMBMasterSetErrorType(eMBMasterErrorEventType errorType)
{
eMBMasterCurErrorType = errorType;
}
#endif /* defined(CONFIG_MB_RTU_MASTER) || defined(CONFIG_MB_ASCII_MASTER) */