forked from RevolutionPi/piControl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRS485FwuCommand.c
More file actions
311 lines (264 loc) · 7.87 KB
/
Copy pathRS485FwuCommand.c
File metadata and controls
311 lines (264 loc) · 7.87 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
307
308
309
310
311
/*=======================================================================================
*
* KK KK UU UU NN NN BBBBBB UU UU SSSSSS
* KK KK UU UU NNN NN BB BB UU UU SS
* KK KK UU UU NNNN NN BB BB UU UU SS
* +----- KKKKK UU UU NN NN NN BBBBB UU UU SSSSS
* | KK KK UU UU NN NNNN BB BB UU UU SS
* | KK KK UU UU NN NNN BB BB UU UU SS
* | KK KKK UUUUUU NN NN BBBBBB UUUUUU SSSSSS GmbH
* |
* | [#] I N D U S T R I A L C O M M U N I C A T I O N
* | |
* +-------------+
*
*---------------------------------------------------------------------------------------
*
* (C) KUNBUS GmbH, Heerweg 15C, 73770 Denkendorf, Germany
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License V2 as published by
* the Free Software Foundation
*
* This program 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 General Public License for more details.
*
* For licencing details see COPYING
*
*=======================================================================================
*/
#include <project.h>
#include <common_define.h>
#include <linux/module.h> // included for all kernel modules
#include <linux/kernel.h> // included for KERN_INFO
#include <linux/slab.h> // included for KERN_INFO
#include <linux/delay.h>
#include "piIOComm.h"
#include "RS485FwuCommand.h"
#define TEL_MAX_BUF_LEN 300
////*************************************************************************************************
INT32S fwuEnterFwuMode (INT8U address)
{
fwuSendTel(address, eCmdSetFwUpdateMode, NULL, 0);
msleep(100);
// there is no response for this command
return 0;
}
////*************************************************************************************************
INT32S fwuWriteSerialNum (
INT8U address,
INT32U i32uSerNum_p)
{
SRs485Telegram suRecvTelegram_l;
INT32S i32sErr_l;
fwuSendTel(address, eCmdWriteSerialNumber, (INT8U*)&i32uSerNum_p, sizeof (INT32U));
msleep(100);
i32sErr_l = fwuReceiveTel(&suRecvTelegram_l);
if (i32sErr_l != 0)
{ // Error occurred
return i32sErr_l;
}
if (suRecvTelegram_l.i16uCmd & MODGATE_RS485_COMMAND_ANSWER_ERROR)
{
if (suRecvTelegram_l.i8uDataLen == 4)
{
i32sErr_l = *((INT32S *)suRecvTelegram_l.ai8uData);
pr_err("fwuWriteSerialNum: module reported error 0x%08x", i32sErr_l);
return -12;
}
else
{
return -13;
}
}
return 0;
}
INT32S fwuEraseFlash (INT8U address)
{
SRs485Telegram suRecvTelegram_l;
INT32S i32sErr_l;
fwuSendTel(address, eCmdEraseFwFlash, 0, 0);
i32sErr_l = fwuReceiveTelTimeout(&suRecvTelegram_l, 6000);
if (i32sErr_l != 0)
{
// Error occurred
return i32sErr_l;
}
if (suRecvTelegram_l.i16uCmd & MODGATE_RS485_COMMAND_ANSWER_ERROR)
{
if (suRecvTelegram_l.i8uDataLen == 4)
{
i32sErr_l = *((INT32S *)suRecvTelegram_l.ai8uData);
pr_err("fwuEraseFlash: module reported error 0x%08x", i32sErr_l);
return -12;
}
else
{
return -13;
}
}
return 0;
}
INT32S fwuWrite(INT8U address, INT32U flashAddr, char *data, INT32U length)
{
SRs485Telegram suRecvTelegram_l;
INT32S i32sErr_l;
INT8U ai8uSendBuf_l[TEL_MAX_BUF_LEN];
memcpy (ai8uSendBuf_l, &flashAddr, sizeof (flashAddr));
if (length <= 0 || length > TEL_MAX_BUF_LEN-sizeof(flashAddr))
return -14;
memcpy (ai8uSendBuf_l + sizeof (flashAddr), data, length);
fwuSendTel(address, eCmdWriteFwFlash, ai8uSendBuf_l, sizeof (flashAddr) + length);
i32sErr_l = fwuReceiveTelTimeout(&suRecvTelegram_l, 1000);
if (i32sErr_l != 0)
{ // Error occurred
return i32sErr_l;
}
if (suRecvTelegram_l.i16uCmd & MODGATE_RS485_COMMAND_ANSWER_ERROR)
{
if (suRecvTelegram_l.i8uDataLen == 4)
{
i32sErr_l = *((INT32S *)suRecvTelegram_l.ai8uData);
pr_err("fwuWrite: module reported error 0x%08x", i32sErr_l);
return -12;
}
else
{
return -13;
}
}
return 0;
}
INT32S fwuResetModule (INT8U address)
{
SRs485Telegram suRecvTelegram_l;
INT32S i32sErr_l;
fwuSendTel(address, eCmdResetModule, NULL, 0);
msleep(100);
i32sErr_l = fwuReceiveTelTimeout(&suRecvTelegram_l, 10000);
if (i32sErr_l != 0)
{ // Error occurred
return i32sErr_l;
}
if (suRecvTelegram_l.i16uCmd & MODGATE_RS485_COMMAND_ANSWER_ERROR)
{
if (suRecvTelegram_l.i8uDataLen == 4)
{
i32sErr_l = *((INT32S *)suRecvTelegram_l.ai8uData);
pr_err("fwuResetModule: module reported error 0x%08x", i32sErr_l);
return -12;
}
else
{
return -13;
}
}
return 0;
}
////*************************************************************************************************
////| Function: crc
////|
////! \brief
////!
////! \detailed
////!
////!
////!
////! \ingroup
////-------------------------------------------------------------------------------------------------
INT8U fwuCrc(INT8U *piData, INT16U len)
{
INT8U i8uCrc = 0;
INT16U i16uI;
for(i16uI = 0; i16uI < len; i16uI++)
{
i8uCrc ^= piData[i16uI];
}
return i8uCrc;
}
////*************************************************************************************************
////| Function: sendTel
////|
////! \brief
////!
////! \detailed
////!
////!
////!
////! \ingroup
////-------------------------------------------------------------------------------------------------
INT32U fwuSendTel (
INT8U address,
INT8U i16uCmd_p,
INT8U *pi8uData_p,
INT8U i8uDataLen_p)
{
SRs485Telegram suSendTelegram_l;
INT32U i32uRv_l = 0;
memset(&suSendTelegram_l, 0, sizeof(SRs485Telegram));
suSendTelegram_l.i8uDstAddr = address; // destination
suSendTelegram_l.i8uSrcAddr = 0; // sender PC/RevPi
suSendTelegram_l.i16uCmd = i16uCmd_p; // command
if(pi8uData_p != 0)
{
suSendTelegram_l.i8uDataLen = i8uDataLen_p;
memcpy(suSendTelegram_l.ai8uData, pi8uData_p, i8uDataLen_p);
}
else
{
suSendTelegram_l.i8uDataLen = 0;
}
suSendTelegram_l.ai8uData[i8uDataLen_p] = fwuCrc((INT8U*)&suSendTelegram_l, RS485_HDRLEN + i8uDataLen_p);
i32uRv_l = piIoComm_send((INT8U*)&suSendTelegram_l, RS485_HDRLEN + suSendTelegram_l.i8uDataLen + 1);
return i32uRv_l;
}
////*************************************************************************************************
INT32S fwuReceiveTelTimeout (
SRs485Telegram *psuRecvTelegram_p, INT16U timeout_p)
{
INT8U i8uLen_l;
if (piIoComm_recv_timeout((INT8U *)psuRecvTelegram_p, RS485_HDRLEN, timeout_p) == RS485_HDRLEN)
{
// header was received -> receive data part
i8uLen_l = piIoComm_recv(psuRecvTelegram_p->ai8uData, psuRecvTelegram_p->i8uDataLen + 1);
if (i8uLen_l != psuRecvTelegram_p->i8uDataLen + 1)
{
return -2;
}
if (psuRecvTelegram_p->ai8uData[psuRecvTelegram_p->i8uDataLen] != fwuCrc((INT8U*)psuRecvTelegram_p, RS485_HDRLEN + psuRecvTelegram_p->i8uDataLen))
{
return -3;
}
}
else
{
return -1;
}
return (0);
}
INT32S fwuReceiveTel (
SRs485Telegram *psuRecvTelegram_p)
{
INT8U i8uLen_l;
if (piIoComm_recv((INT8U *)psuRecvTelegram_p, RS485_HDRLEN) == RS485_HDRLEN)
{
// header was received -> receive data part
i8uLen_l = piIoComm_recv(psuRecvTelegram_p->ai8uData, psuRecvTelegram_p->i8uDataLen + 1);
if (i8uLen_l != psuRecvTelegram_p->i8uDataLen + 1)
{
return -2;
}
if (psuRecvTelegram_p->ai8uData[psuRecvTelegram_p->i8uDataLen] != fwuCrc((INT8U*)psuRecvTelegram_p, RS485_HDRLEN + psuRecvTelegram_p->i8uDataLen))
{
return -3;
}
}
else
{
return -1;
}
return (0);
}
////*************************************************************************************************