forked from RevolutionPi/piControl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RevPiDevice.c
284 lines (262 loc) · 10.8 KB
/
RevPiDevice.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
//+=============================================================================================
//|
//! \file RevPiDevice.cpp
//! Device administration for PiBridge master modules
//|
//+---------------------------------------------------------------------------------------------
//|
//| File-ID: $Id: RevPiDevice.c 506 2017-01-31 07:03:32Z mduckeck $
//| Location: $URL: http://srv-kunbus03.de.pilz.local/raspi/trunk/products/PiCore/piKernelMod/RevPiDevice.c $
//| Company: $Cpn:$
//|
//+=============================================================================================
//#include <stdio.h>
#include <linux/module.h> // included for all kernel modules
#include <linux/delay.h>
#include <project.h>
#include <common_define.h>
#include <kbUtilities.h>
#include <ModGateRS485.h>
#include <ModGateComMain.h>
#include <PiBridgeMaster.h>
#include <RevPiDevice.h>
#include <piControlMain.h>
#include <piDIOComm.h>
SDeviceConfig RevPiScan;
static const MODGATECOM_IDResp RevPi_ID_s =
{
.i32uSerialnumber = 1,
.i16uModulType = KUNBUS_FW_DESCR_TYP_PI_CORE,
.i16uHW_Revision = 1,
.i16uSW_Major = 1,
.i16uSW_Minor = 1, //TODO
.i32uSVN_Revision = 0,
.i16uFBS_InputLength = 4,
.i16uFBS_OutputLength = 5,
.i16uFeatureDescriptor = MODGATE_feature_IODataExchange
};
void RevPiDevice_init(void)
{
printk("RevPiDevice_init()\n");
piDev_g.i8uLeftMGateIdx = REV_PI_DEV_UNDEF;
piDev_g.i8uRightMGateIdx = REV_PI_DEV_UNDEF;
RevPiScan.i8uAddressRight = REV_PI_DEV_FIRST_RIGHT; // first address of a right side module
RevPiScan.i8uAddressLeft = REV_PI_DEV_FIRST_RIGHT-1; // first address of a left side module
RevPiScan.i8uDeviceCount = 0; // counter for detected devices
RevPiScan.i16uErrorCnt = 0;
// RevPi as first entry to device list
RevPiScan.dev[RevPiScan.i8uDeviceCount].i8uAddress = 0;
RevPiScan.dev[RevPiScan.i8uDeviceCount].sId = RevPi_ID_s;
RevPiScan.dev[RevPiScan.i8uDeviceCount].i16uInputOffset = 0;
RevPiScan.dev[RevPiScan.i8uDeviceCount].i16uOutputOffset = (INT16U)((int)&((SRevPiCoreImage *)0)->i8uLED);
RevPiScan.dev[RevPiScan.i8uDeviceCount].i8uActive = 1;
RevPiScan.i8uDeviceCount++;
}
void RevPiDevice_finish(void)
{
//printk("RevPiDevice_finish()\n");
piIoComm_finish();
}
//*************************************************************************************************
//| Function: RevPiDevice_run
//|
//! \brief cyclically called run function
//!
//! \detailed performs the cyclic communication with all modules
//! connected to the RS485 Bus
//!
//!
//! \ingroup
//-------------------------------------------------------------------------------------------------
int RevPiDevice_run(void)
{
INT8U i8uDevice = 0;
INT32U r;
int retval = 0;
RevPiScan.i16uErrorCnt = 0;
for (i8uDevice = 0; i8uDevice < RevPiScan.i8uDeviceCount; i8uDevice++)
{
if (RevPiScan.dev[i8uDevice].i8uActive)
{
switch (RevPiScan.dev[i8uDevice].sId.i16uModulType)
{
case KUNBUS_FW_DESCR_TYP_PI_DIO_14:
case KUNBUS_FW_DESCR_TYP_PI_DI_16:
case KUNBUS_FW_DESCR_TYP_PI_DO_16:
r = piDIOComm_sendCyclicTelegram(i8uDevice);
if (r)
{
if (RevPiScan.dev[i8uDevice].i16uErrorCnt < 255)
{
RevPiScan.dev[i8uDevice].i16uErrorCnt++;
retval = -1; // tell calling function that an error occured
}
}
else
{
RevPiScan.dev[i8uDevice].i16uErrorCnt = 0;
}
break;
case KUNBUS_FW_DESCR_TYP_MG_CAN_OPEN:
case KUNBUS_FW_DESCR_TYP_MG_CCLINK:
case KUNBUS_FW_DESCR_TYP_MG_DEV_NET:
case KUNBUS_FW_DESCR_TYP_MG_ETHERCAT:
case KUNBUS_FW_DESCR_TYP_MG_ETHERNET_IP:
case KUNBUS_FW_DESCR_TYP_MG_POWERLINK:
case KUNBUS_FW_DESCR_TYP_MG_PROFIBUS:
case KUNBUS_FW_DESCR_TYP_MG_PROFINET_RT:
case KUNBUS_FW_DESCR_TYP_MG_PROFINET_IRT:
case KUNBUS_FW_DESCR_TYP_MG_CAN_OPEN_MASTER:
case KUNBUS_FW_DESCR_TYP_MG_SERCOS3:
case KUNBUS_FW_DESCR_TYP_MG_SERIAL:
case KUNBUS_FW_DESCR_TYP_MG_PROFINET_RT_MASTER:
case KUNBUS_FW_DESCR_TYP_MG_PROFINET_IRT_MASTER:
case KUNBUS_FW_DESCR_TYP_MG_ETHERCAT_MASTER:
case KUNBUS_FW_DESCR_TYP_MG_MODBUS_RTU:
case KUNBUS_FW_DESCR_TYP_MG_MODBUS_TCP:
case KUNBUS_FW_DESCR_TYP_MG_DMX:
if ( piDev_g.i8uRightMGateIdx == REV_PI_DEV_UNDEF
&& RevPiScan.dev[i8uDevice].i8uAddress >= REV_PI_DEV_FIRST_RIGHT)
{
piDev_g.i8uRightMGateIdx = i8uDevice;
}
else if (piDev_g.i8uLeftMGateIdx == REV_PI_DEV_UNDEF
&& RevPiScan.dev[i8uDevice].i8uAddress < REV_PI_DEV_FIRST_RIGHT)
{
piDev_g.i8uLeftMGateIdx = i8uDevice;
}
break;
default:
// user devices are ignored here
break;
}
RevPiScan.i16uErrorCnt += RevPiScan.dev[i8uDevice].i16uErrorCnt;
}
}
return retval;
}
TBOOL RevPiDevice_writeNextConfiguration(INT8U i8uAddress_p, MODGATECOM_IDResp *pModgateId_p)
{
INT32U ret_l;
//
ret_l = piIoComm_sendRS485Tel(eCmdGetDeviceInfo, i8uAddress_p, NULL, 0, (INT8U*)pModgateId_p, sizeof(MODGATECOM_IDResp));
msleep(10); // wait a while
if (ret_l)
{
#ifdef DEBUG_DEVICE
DF_PRINTK("piIoComm_sendRS485Tel(GetDeviceInfo) failed %d\n", ret_l);
#endif
return bFALSE;
}
ret_l = piIoComm_sendRS485Tel(eCmdPiIoConfigure, i8uAddress_p, NULL, 0, NULL, 0);
msleep(10); // wait a while
if (ret_l)
{
#ifdef DEBUG_DEVICE
DF_PRINTK("piIoComm_sendRS485Tel(PiIoConfigure) failed %d\n", ret_l);
#endif
return bFALSE;
}
ret_l = piIoComm_sendRS485Tel(eCmdPiIoSetAddress, i8uAddress_p, NULL, 0, NULL, 0);
msleep(10); // wait a while
if (ret_l)
{
#ifdef DEBUG_DEVICE
DF_PRINTK("piIoComm_sendRS485Tel(PiIoSetAddress) failed %d\n", ret_l);
#endif
return bFALSE;
}
return bTRUE;
}
TBOOL RevPiDevice_writeNextConfigurationRight(void)
{
if (RevPiDevice_writeNextConfiguration(RevPiScan.i8uAddressRight, &RevPiScan.dev[RevPiScan.i8uDeviceCount].sId))
{
RevPiScan.dev[RevPiScan.i8uDeviceCount].i8uAddress = RevPiScan.i8uAddressRight;
if (RevPiScan.i8uDeviceCount == 0)
{
RevPiScan.dev[RevPiScan.i8uDeviceCount].i16uInputOffset = 0;
RevPiScan.dev[RevPiScan.i8uDeviceCount].i16uOutputOffset = RevPiScan.dev[RevPiScan.i8uDeviceCount].sId.i16uFBS_InputLength;
}
else
{
RevPiScan.dev[RevPiScan.i8uDeviceCount].i16uInputOffset = RevPiScan.dev[RevPiScan.i8uDeviceCount-1].i16uOutputOffset
+ RevPiScan.dev[RevPiScan.i8uDeviceCount-1].sId.i16uFBS_OutputLength;
RevPiScan.dev[RevPiScan.i8uDeviceCount].i16uOutputOffset = RevPiScan.dev[RevPiScan.i8uDeviceCount].i16uInputOffset
+ RevPiScan.dev[RevPiScan.i8uDeviceCount].sId.i16uFBS_InputLength;
}
#ifdef DEBUG_DEVICE
DF_PRINTK("found %d. device on right side. Moduletype %d. Designated address %d\n",
RevPiScan.i8uDeviceCount+1, RevPiScan.dev[RevPiScan.i8uDeviceCount].sId.i16uModulType, RevPiScan.i8uAddressRight);
DF_PRINTK("input offset %5d len %3d\n",
RevPiScan.dev[RevPiScan.i8uDeviceCount].i16uInputOffset,
RevPiScan.dev[RevPiScan.i8uDeviceCount].sId.i16uFBS_InputLength);
DF_PRINTK("output offset %5d len %3d\n",
RevPiScan.dev[RevPiScan.i8uDeviceCount].i16uOutputOffset,
RevPiScan.dev[RevPiScan.i8uDeviceCount].sId.i16uFBS_OutputLength);
#endif
RevPiScan.dev[RevPiScan.i8uDeviceCount].i8uActive = 1;
RevPiScan.i8uDeviceCount++;
RevPiScan.i8uAddressRight++;
return bTRUE;
}
return bFALSE;
}
TBOOL RevPiDevice_writeNextConfigurationLeft(void)
{
if (RevPiDevice_writeNextConfiguration(RevPiScan.i8uAddressLeft, &RevPiScan.dev[RevPiScan.i8uDeviceCount].sId))
{
RevPiScan.dev[RevPiScan.i8uDeviceCount].i8uAddress = RevPiScan.i8uAddressLeft;
if (RevPiScan.i8uDeviceCount == 0)
{
RevPiScan.dev[RevPiScan.i8uDeviceCount].i16uInputOffset = 0;
RevPiScan.dev[RevPiScan.i8uDeviceCount].i16uOutputOffset = RevPiScan.dev[RevPiScan.i8uDeviceCount].sId.i16uFBS_InputLength;
}
else
{
RevPiScan.dev[RevPiScan.i8uDeviceCount].i16uInputOffset = RevPiScan.dev[RevPiScan.i8uDeviceCount-1].i16uOutputOffset
+ RevPiScan.dev[RevPiScan.i8uDeviceCount-1].sId.i16uFBS_OutputLength;
RevPiScan.dev[RevPiScan.i8uDeviceCount].i16uOutputOffset = RevPiScan.dev[RevPiScan.i8uDeviceCount].i16uInputOffset
+ RevPiScan.dev[RevPiScan.i8uDeviceCount].sId.i16uFBS_InputLength;
}
#ifdef DEBUG_DEVICE
DF_PRINTK("found %d. device on left side. Moduletype %d. Designated address %d\n",
RevPiScan.i8uDeviceCount+1,
RevPiScan.dev[RevPiScan.i8uDeviceCount].sId.i16uModulType,
RevPiScan.i8uAddressLeft);
DF_PRINTK("input offset %5d len %3d\n",
RevPiScan.dev[RevPiScan.i8uDeviceCount].i16uInputOffset,
RevPiScan.dev[RevPiScan.i8uDeviceCount].sId.i16uFBS_InputLength);
DF_PRINTK("output offset %5d len %3d\n",
RevPiScan.dev[RevPiScan.i8uDeviceCount].i16uOutputOffset,
RevPiScan.dev[RevPiScan.i8uDeviceCount].sId.i16uFBS_OutputLength);
#endif
RevPiScan.dev[RevPiScan.i8uDeviceCount].i8uActive = 1;
RevPiScan.i8uDeviceCount++;
RevPiScan.i8uAddressLeft--;
return bTRUE;
}
return bFALSE;
}
void RevPiDevice_startDataexchange(void)
{
INT32U ret_l = piIoComm_sendRS485Tel(eCmdPiIoStartDataExchange, MODGATE_RS485_BROADCAST_ADDR, NULL, 0, NULL, 0);
msleep(90); // wait a while
if (ret_l)
{
#ifdef DEBUG_DEVICE
DF_PRINTK("piIoComm_sendRS485Tel(PiIoStartDataExchange) failed %d\n", ret_l);
#endif
}
}
void RevPiDevice_stopDataexchange(void)
{
INT32U ret_l = piIoComm_sendRS485Tel(eCmdPiIoStartDataExchange, MODGATE_RS485_BROADCAST_ADDR, NULL, 0, NULL, 0);
msleep(90); // wait a while
if (ret_l)
{
#ifdef DEBUG_DEVICE
DF_PRINTK("piIoComm_sendRS485Tel(PiIoStartDataExchange) failed %d\n", ret_l);
#endif
}
}