-
Notifications
You must be signed in to change notification settings - Fork 1
/
mb_util.c
356 lines (304 loc) · 13.4 KB
/
mb_util.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
/*
*********************************************************************************************************
* uC/Modbus
* The Embedded Modbus Stack
*
* Copyright 2003-2020 Silicon Laboratories Inc. www.silabs.com
*
* SPDX-License-Identifier: APACHE-2.0
*
* This software is subject to an open source license and is distributed by
* Silicon Laboratories Inc. pursuant to the terms of the Apache License,
* Version 2.0 available at www.apache.org/licenses/LICENSE-2.0.
*
*********************************************************************************************************
*/
/*
*********************************************************************************************************
* uC/MODBUS Utilities
*
* Filename : mb_util.h
* Version : V2.14.00
*********************************************************************************************************
*/
/*
*********************************************************************************************************
* INCLUDE FILES
*********************************************************************************************************
*/
#define MB_UTIL_MODULE
#include "mb.h"
/*
*********************************************************************************************************
* LOCAL DEFINES
*********************************************************************************************************
*/
/*
*********************************************************************************************************
* LOCAL CONSTANTS
*********************************************************************************************************
*/
/*
*********************************************************************************************************
* LOCAL DATA TYPES
*********************************************************************************************************
*/
/*
*********************************************************************************************************
* LOCAL TABLES
*********************************************************************************************************
*/
/*
*********************************************************************************************************
* LOCAL GLOBAL VARIABLES
*********************************************************************************************************
*/
/*
*********************************************************************************************************
* LOCAL FUNCTION PROTOTYPES
*********************************************************************************************************
*/
/*
*********************************************************************************************************
* LOCAL CONFIGURATION ERRORS
*********************************************************************************************************
*/
/*
*********************************************************************************************************
* MB_ASCII_BinToHex()
*
* Description : Converts a byte into two ASCII characters into the given buffer.
*
* Argument(s) : value The byte of data to be converted.
* pbuf A pointer to the buffer to store the ASCII chars.
*
* Return(s) : The buffer pointer which has been updated to point to the next char in the buffer.
*
* Caller(s) : MB_ASCII_Tx().
*
* Note(s) : (1) The function ONLY converts the byte to ASCII and DOES NOT null terminate the string.
*********************************************************************************************************
*/
#if (MODBUS_CFG_ASCII_EN == DEF_ENABLED)
CPU_INT08U *MB_ASCII_BinToHex (CPU_INT08U value,
CPU_INT08U *pbuf)
{
CPU_INT08U nibble;
nibble = (value >> 4) & 0x0F; /* Upper Nibble */
if (nibble <= 9) {
*pbuf++ = (CPU_INT08U)(nibble + '0');
} else {
*pbuf++ = (CPU_INT08U)(nibble - 10 + 'A');
}
nibble = value & 0x0F; /* Lower Nibble */
if (nibble <= 9) {
*pbuf++ = (CPU_INT08U)(nibble + '0');
} else {
*pbuf++ = (CPU_INT08U)(nibble - 10 + 'A');
}
return (pbuf);
}
#endif
/*
*********************************************************************************************************
* MB_ASCII_HexToBin()
*
* Description : Converts the first two ASCII hex characters in the buffer into one byte.
*
* Argument(s) : phex Pointer to the buffer that contains the two ascii chars.
*
* Return(s) : value of the two ASCII HEX digits pointed to by 'phex'.
*
* Caller(s) : MB_ASCII_RxByte(),
* MB_ASCII_Rx(),
* MB_ASCII_RxCalcLRC(),
* MB_ASCII_TxCalcLRC().
*
* Note(s) : none.
*********************************************************************************************************
*/
#if (MODBUS_CFG_ASCII_EN == DEF_ENABLED)
CPU_INT08U MB_ASCII_HexToBin (CPU_INT08U *phex)
{
CPU_INT08U value;
CPU_INT08U high;
CPU_INT08U low;
high = *phex; /* Get upper nibble */
phex++;
low = *phex; /* Get lower nibble */
if (high <= '9') { /* Upper Nibble */
value = (CPU_INT08U)(high - '0');
} else if (high <= 'F') {
value = (CPU_INT08U)(high - 'A' + 10);
} else {
value = (CPU_INT08U)(high - 'a' + 10);
}
value <<= 4;
if (low <= '9') { /* Lower Nibble */
value += (CPU_INT08U)(low - '0');
} else if (low <= 'F') {
value += (CPU_INT08U)(low - 'A' + 10);
} else {
value += (CPU_INT08U)(low - 'a' + 10);
}
return (value);
}
#endif
/*
*********************************************************************************************************
* MB_ASCII_RxCalcLRC()
*
* Description : The function calculates an 8-bit Longitudinal Redundancy Check on a MODBUS_FRAME
* structure.
*
* Argument(s) : none.
*
* Return(s) : The calculated LRC value.
*
* Caller(s) : MBS_ASCII_Task().
*
* Note(s) : (1) The LRC is calculated on the ADDR, FC and Data fields, not the ':', CR/LF and LRC
* placed in the message by the sender. We thus need to subtract 5 'ASCII' characters
* from the received message to exclude these.
*********************************************************************************************************
*/
#if (MODBUS_CFG_ASCII_EN == DEF_ENABLED)
CPU_INT08U MB_ASCII_RxCalcLRC (MODBUS_CH *pch)
{
CPU_INT08U lrc;
CPU_INT16U len;
CPU_INT08U *pblock;
len = (pch->RxBufByteCtr - 5) / 2 ; /* LRC to include Addr + FC + Data */
pblock = (CPU_INT08U *)&pch->RxBuf[1];
lrc = 0;
while (len-- > 0) { /* For each byte of data in the data block... */
lrc += MB_ASCII_HexToBin(pblock); /* Add the data byte to LRC, increment data pointer. */
pblock += 2;
}
lrc = ~lrc + 1; /* Two complement the binary sum */
return (lrc); /* Return LRC for all data in block. */
}
#endif
/*
*********************************************************************************************************
* MB_ASCII_TxCalcLRC()
*
* Description : The function calculates an 8-bit Longitudinal Redundancy Check on a MODBUS_FRAME
* structure.
*
* Argument(s) : none.
*
* Return(s) : The calculated LRC value.
*
* Caller(s) : MB_ASCII_Tx().
*
* Note(s) : (1) The LRC is calculated on the ADDR, FC and Data fields, not the ':' which was inserted
* in the TxBuf[]. Thus we subtract 1 ASCII character from the LRC.
*
* (2) The LRC and CR/LF bytes are not YET in the .RxBuf[].
*********************************************************************************************************
*/
#if (MODBUS_CFG_ASCII_EN == DEF_ENABLED)
CPU_INT08U MB_ASCII_TxCalcLRC (MODBUS_CH *pch, CPU_INT16U tx_bytes)
{
CPU_INT08U lrc;
CPU_INT16U len;
CPU_INT08U *pblock;
len = (tx_bytes - 1) / 2; /* LRC to include Addr + FC + Data (exclude ':') */
pblock = (CPU_INT08U *)&pch->TxBuf[1];
lrc = 0;
while (len-- > 0) { /* For each byte of data in the data block... */
lrc += MB_ASCII_HexToBin(pblock); /* Add the data byte to LRC, increment data pointer. */
pblock += 2;
}
lrc = ~lrc + 1; /* Two complement the binary sum */
return (lrc); /* Return LRC for all data in block. */
}
#endif
/*
*********************************************************************************************************
* MB_RTU_RxCalcCRC()
*
* Description : The polynomial is a CRC-16 found for 'MBS_RxFrameNDataBytes' number of characters
* starting at 'MBS_RxFrameAddr'.
*
* Argument(s) : none.
*
* Return(s) : An unsigned 16-bit value representing the CRC-16 of the data.
*
* Caller(s) : MBS_RTU_Task().
*
* Note(s) : none.
*********************************************************************************************************
*/
#if (MODBUS_CFG_RTU_EN == DEF_ENABLED)
CPU_INT16U MB_RTU_RxCalcCRC (MODBUS_CH *pch)
{
CPU_INT16U crc;
CPU_INT08U shiftctr;
CPU_BOOLEAN flag;
CPU_INT16U length;
CPU_INT08U *pblock;
pblock = (CPU_INT08U *)&pch->RxFrameData[0]; /* Starting address of where the CRC data starts */
length = pch->RxFrameNDataBytes + 2; /* Include the address and function code in the CRC */
crc = 0xFFFF; /* Initialize CRC to all ones. */
while (length > 0) { /* Account for each byte of data */
length--;
crc ^= (CPU_INT16U)*pblock++;
shiftctr = 8;
do {
flag = (crc & 0x0001) ? DEF_TRUE : DEF_FALSE; /* Determine if the shift out of rightmost bit is 1 */
crc >>= 1; /* Shift CRC to the right one bit. */
if (flag == DEF_TRUE) { /* If (bit shifted out of rightmost bit was a 1) */
crc ^= MODBUS_CRC16_POLY; /* Exclusive OR the CRC with the generating polynomial. */
}
shiftctr--;
} while (shiftctr > 0);
}
pch->RxFrameCRC_Calc = crc;
return (crc);
}
#endif
/*
*********************************************************************************************************
* MB_RTU_TxCalcCRC()
*
* Description : The polynomial is a CRC-16 found for 'MBS_TxFrameNDataBytes' number of characters
* starting at 'MBS_TxFrameAddr'.
*
* Argument(s) : none.
*
* Return(s) : An unsigned 16-bit value representing the CRC-16 of the data.
*
* Caller(s) : MB_RTU_Tx().
*
* Note*(s) : none.
*********************************************************************************************************
*/
#if (MODBUS_CFG_RTU_EN == DEF_ENABLED)
CPU_INT16U MB_RTU_TxCalcCRC (MODBUS_CH *pch)
{
CPU_INT16U crc;
CPU_INT08U shiftctr;
CPU_BOOLEAN flag;
CPU_INT16U length;
CPU_INT08U *pblock;
pblock = (CPU_INT08U *)&pch->TxFrameData[0]; /* Starting address of where the CRC data starts */
length = pch->TxFrameNDataBytes + 2; /* Include the address and function code in the CRC */
crc = 0xFFFF; /* Initialize CRC to all ones. */
while (length > 0) { /* Account for each byte of data */
length--;
crc ^= (CPU_INT16U)*pblock++;
shiftctr = 8;
do {
flag = (crc & 0x0001) ? DEF_TRUE : DEF_FALSE; /* Determine if the shift out of rightmost bit is 1. */
crc >>= 1; /* Shift CRC to the right one bit. */
if (flag == DEF_TRUE) { /* If (bit shifted out of rightmost bit was a 1) */
crc ^= MODBUS_CRC16_POLY; /* Exclusive OR the CRC with the generating polynomial */
}
shiftctr--;
} while (shiftctr > 0);
}
return (crc); /* Return CRC for all data in block. */
}
#endif