-
Notifications
You must be signed in to change notification settings - Fork 7.1k
/
Copy pathmodbus.h
673 lines (622 loc) · 20.6 KB
/
modbus.h
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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
/*
* Copyright (c) 2020 PHYTEC Messtechnik GmbH
* Copyright (c) 2021 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: Apache-2.0
*/
/*
* Client API in this file is based on mbm_core.c from uC/Modbus Stack.
*
* 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.
*/
/**
* @brief MODBUS transport protocol API
* @defgroup modbus MODBUS
* @ingroup io_interfaces
* @{
*/
#ifndef ZEPHYR_INCLUDE_MODBUS_H_
#define ZEPHYR_INCLUDE_MODBUS_H_
#include <zephyr/sys_clock.h>
#include <zephyr/drivers/uart.h>
#include <zephyr/sys/slist.h>
#ifdef __cplusplus
extern "C" {
#endif
/** Length of MBAP Header */
#define MODBUS_MBAP_LENGTH 7
/** Length of MBAP Header plus function code */
#define MODBUS_MBAP_AND_FC_LENGTH (MODBUS_MBAP_LENGTH + 1)
/** @name Modbus exception codes
* @{
*/
/** No exception */
#define MODBUS_EXC_NONE 0
/** Illegal function code */
#define MODBUS_EXC_ILLEGAL_FC 1
/** Illegal data address */
#define MODBUS_EXC_ILLEGAL_DATA_ADDR 2
/** Illegal data value */
#define MODBUS_EXC_ILLEGAL_DATA_VAL 3
/** Server device failure */
#define MODBUS_EXC_SERVER_DEVICE_FAILURE 4
/** Acknowledge */
#define MODBUS_EXC_ACK 5
/** Server device busy */
#define MODBUS_EXC_SERVER_DEVICE_BUSY 6
/** Memory parity error */
#define MODBUS_EXC_MEM_PARITY_ERROR 8
/** Gateway path unavailable */
#define MODBUS_EXC_GW_PATH_UNAVAILABLE 10
/** Gateway target device failed to respond */
#define MODBUS_EXC_GW_TARGET_FAILED_TO_RESP 11
/** @} */
/**
* @brief Frame struct used internally and for raw ADU support.
*/
struct modbus_adu {
/** Transaction Identifier */
uint16_t trans_id;
/** Protocol Identifier */
uint16_t proto_id;
/** Length of the data only (not the length of unit ID + PDU) */
uint16_t length;
/** Unit Identifier */
uint8_t unit_id;
/** Function Code */
uint8_t fc;
/** Transaction Data */
uint8_t data[CONFIG_MODBUS_BUFFER_SIZE - 4];
/** RTU CRC */
uint16_t crc;
};
/**
* @brief Coil read (FC01)
*
* Sends a Modbus message to read the status of coils from a server.
*
* @param iface Modbus interface index
* @param unit_id Modbus unit ID of the server
* @param start_addr Coil starting address
* @param coil_tbl Pointer to an array of bytes containing the value
* of the coils read.
* The format is:
*
* MSB LSB
* B7 B6 B5 B4 B3 B2 B1 B0
* -------------------------------------
* coil_tbl[0] #8 #7 #1
* coil_tbl[1] #16 #15 #9
* :
* :
*
* Note that the array that will be receiving the coil
* values must be greater than or equal to:
* (num_coils - 1) / 8 + 1
* @param num_coils Quantity of coils to read
*
* @retval 0 If the function was successful
*/
int modbus_read_coils(const int iface,
const uint8_t unit_id,
const uint16_t start_addr,
uint8_t *const coil_tbl,
const uint16_t num_coils);
/**
* @brief Read discrete inputs (FC02)
*
* Sends a Modbus message to read the status of discrete inputs from
* a server.
*
* @param iface Modbus interface index
* @param unit_id Modbus unit ID of the server
* @param start_addr Discrete input starting address
* @param di_tbl Pointer to an array that will receive the state
* of the discrete inputs.
* The format of the array is as follows:
*
* MSB LSB
* B7 B6 B5 B4 B3 B2 B1 B0
* -------------------------------------
* di_tbl[0] #8 #7 #1
* di_tbl[1] #16 #15 #9
* :
* :
*
* Note that the array that will be receiving the discrete
* input values must be greater than or equal to:
* (num_di - 1) / 8 + 1
* @param num_di Quantity of discrete inputs to read
*
* @retval 0 If the function was successful
*/
int modbus_read_dinputs(const int iface,
const uint8_t unit_id,
const uint16_t start_addr,
uint8_t *const di_tbl,
const uint16_t num_di);
/**
* @brief Read holding registers (FC03)
*
* Sends a Modbus message to read the value of holding registers
* from a server.
*
* @param iface Modbus interface index
* @param unit_id Modbus unit ID of the server
* @param start_addr Register starting address
* @param reg_buf Is a pointer to an array that will receive
* the current values of the holding registers from
* the server. The array pointed to by 'reg_buf' needs
* to be able to hold at least 'num_regs' entries.
* @param num_regs Quantity of registers to read
*
* @retval 0 If the function was successful
*/
int modbus_read_holding_regs(const int iface,
const uint8_t unit_id,
const uint16_t start_addr,
uint16_t *const reg_buf,
const uint16_t num_regs);
/**
* @brief Read input registers (FC04)
*
* Sends a Modbus message to read the value of input registers from
* a server.
*
* @param iface Modbus interface index
* @param unit_id Modbus unit ID of the server
* @param start_addr Register starting address
* @param reg_buf Is a pointer to an array that will receive
* the current value of the holding registers
* from the server. The array pointed to by 'reg_buf'
* needs to be able to hold at least 'num_regs' entries.
* @param num_regs Quantity of registers to read
*
* @retval 0 If the function was successful
*/
int modbus_read_input_regs(const int iface,
const uint8_t unit_id,
const uint16_t start_addr,
uint16_t *const reg_buf,
const uint16_t num_regs);
/**
* @brief Write single coil (FC05)
*
* Sends a Modbus message to write the value of single coil to a server.
*
* @param iface Modbus interface index
* @param unit_id Modbus unit ID of the server
* @param coil_addr Coils starting address
* @param coil_state Is the desired state of the coil
*
* @retval 0 If the function was successful
*/
int modbus_write_coil(const int iface,
const uint8_t unit_id,
const uint16_t coil_addr,
const bool coil_state);
/**
* @brief Write single holding register (FC06)
*
* Sends a Modbus message to write the value of single holding register
* to a server unit.
*
* @param iface Modbus interface index
* @param unit_id Modbus unit ID of the server
* @param start_addr Coils starting address
* @param reg_val Desired value of the holding register
*
* @retval 0 If the function was successful
*/
int modbus_write_holding_reg(const int iface,
const uint8_t unit_id,
const uint16_t start_addr,
const uint16_t reg_val);
/**
* @brief Read diagnostic (FC08)
*
* Sends a Modbus message to perform a diagnostic function of a server unit.
*
* @param iface Modbus interface index
* @param unit_id Modbus unit ID of the server
* @param sfunc Diagnostic sub-function code
* @param data Sub-function data
* @param data_out Pointer to the data value
*
* @retval 0 If the function was successful
*/
int modbus_request_diagnostic(const int iface,
const uint8_t unit_id,
const uint16_t sfunc,
const uint16_t data,
uint16_t *const data_out);
/**
* @brief Write coils (FC15)
*
* Sends a Modbus message to write to coils on a server unit.
*
* @param iface Modbus interface index
* @param unit_id Modbus unit ID of the server
* @param start_addr Coils starting address
* @param coil_tbl Pointer to an array of bytes containing the value
* of the coils to write.
* The format is:
*
* MSB LSB
* B7 B6 B5 B4 B3 B2 B1 B0
* -------------------------------------
* coil_tbl[0] #8 #7 #1
* coil_tbl[1] #16 #15 #9
* :
* :
*
* Note that the array that will be receiving the coil
* values must be greater than or equal to:
* (num_coils - 1) / 8 + 1
* @param num_coils Quantity of coils to write
*
* @retval 0 If the function was successful
*/
int modbus_write_coils(const int iface,
const uint8_t unit_id,
const uint16_t start_addr,
uint8_t *const coil_tbl,
const uint16_t num_coils);
/**
* @brief Write holding registers (FC16)
*
* Sends a Modbus message to write to integer holding registers
* to a server unit.
*
* @param iface Modbus interface index
* @param unit_id Modbus unit ID of the server
* @param start_addr Register starting address
* @param reg_buf Is a pointer to an array containing
* the value of the holding registers to write.
* Note that the array containing the register values must
* be greater than or equal to 'num_regs'
* @param num_regs Quantity of registers to write
*
* @retval 0 If the function was successful
*/
int modbus_write_holding_regs(const int iface,
const uint8_t unit_id,
const uint16_t start_addr,
uint16_t *const reg_buf,
const uint16_t num_regs);
/**
* @brief Read floating-point holding registers (FC03)
*
* Sends a Modbus message to read the value of floating-point
* holding registers from a server unit.
*
* @param iface Modbus interface index
* @param unit_id Modbus unit ID of the server
* @param start_addr Register starting address
* @param reg_buf Is a pointer to an array that will receive
* the current values of the holding registers from
* the server. The array pointed to by 'reg_buf' needs
* to be able to hold at least 'num_regs' entries.
* @param num_regs Quantity of registers to read
*
* @retval 0 If the function was successful
*/
int modbus_read_holding_regs_fp(const int iface,
const uint8_t unit_id,
const uint16_t start_addr,
float *const reg_buf,
const uint16_t num_regs);
/**
* @brief Write floating-point holding registers (FC16)
*
* Sends a Modbus message to write to floating-point holding registers
* to a server unit.
*
* @param iface Modbus interface index
* @param unit_id Modbus unit ID of the server
* @param start_addr Register starting address
* @param reg_buf Is a pointer to an array containing
* the value of the holding registers to write.
* Note that the array containing the register values must
* be greater than or equal to 'num_regs'
* @param num_regs Quantity of registers to write
*
* @retval 0 If the function was successful
*/
int modbus_write_holding_regs_fp(const int iface,
const uint8_t unit_id,
const uint16_t start_addr,
float *const reg_buf,
const uint16_t num_regs);
/** Modbus Server User Callback structure */
struct modbus_user_callbacks {
/** Coil read callback */
int (*coil_rd)(uint16_t addr, bool *state);
/** Coil write callback */
int (*coil_wr)(uint16_t addr, bool state);
/** Discrete Input read callback */
int (*discrete_input_rd)(uint16_t addr, bool *state);
/** Input Register read callback */
int (*input_reg_rd)(uint16_t addr, uint16_t *reg);
/** Floating Point Input Register read callback */
int (*input_reg_rd_fp)(uint16_t addr, float *reg);
/** Holding Register read callback */
int (*holding_reg_rd)(uint16_t addr, uint16_t *reg);
/** Holding Register write callback */
int (*holding_reg_wr)(uint16_t addr, uint16_t reg);
/** Floating Point Holding Register read callback */
int (*holding_reg_rd_fp)(uint16_t addr, float *reg);
/** Floating Point Holding Register write callback */
int (*holding_reg_wr_fp)(uint16_t addr, float reg);
};
/**
* @brief Get Modbus interface index according to interface name
*
* If there is more than one interface, it can be used to clearly
* identify interfaces in the application.
*
* @param iface_name Modbus interface name
*
* @retval Modbus interface index or negative error value.
*/
int modbus_iface_get_by_name(const char *iface_name);
/**
* @brief ADU raw callback function signature
*
* @param iface Modbus RTU interface index
* @param adu Pointer to the RAW ADU struct to send
* @param user_data Pointer to the user data
*
* @retval 0 If transfer was successful
*/
typedef int (*modbus_raw_cb_t)(const int iface, const struct modbus_adu *adu,
void *user_data);
/**
* @brief Custom function code handler function signature.
*
* Modbus allows user defined function codes which can be used to extend
* the base protocol. These callbacks can also be used to implement
* function codes currently not supported by Zephyr's Modbus subsystem.
*
* If an error occurs during the handling of the request, the handler should
* signal this by setting excep_code to a modbus exception code.
*
* User data pointer can be used to pass state between subsequent calls to
* the handler.
*
* @param iface Modbus interface index
* @param rx_adu Pointer to the received ADU struct
* @param tx_adu Pointer to the outgoing ADU struct
* @param excep_code Pointer to possible exception code
* @param user_data Pointer to user data
*
* @retval true If response should be sent, false otherwise
*/
typedef bool (*modbus_custom_cb_t)(const int iface,
const struct modbus_adu *const rx_adu,
struct modbus_adu *const tx_adu,
uint8_t *const excep_code,
void *const user_data);
/** @cond INTERNAL_HIDDEN */
/**
* @brief Custom function code definition.
*/
struct modbus_custom_fc {
sys_snode_t node;
modbus_custom_cb_t cb;
void *user_data;
uint8_t fc;
uint8_t excep_code;
};
/** @endcond INTERNAL_HIDDEN */
/**
* @brief Helper macro for initializing custom function code structs
*/
#define MODBUS_CUSTOM_FC_DEFINE(name, user_cb, user_fc, userdata) \
static struct modbus_custom_fc modbus_cfg_##name = { \
.cb = user_cb, \
.user_data = userdata, \
.fc = user_fc, \
.excep_code = MODBUS_EXC_NONE, \
}
/**
* @brief Modbus interface mode
*/
enum modbus_mode {
/** Modbus over serial line RTU mode */
MODBUS_MODE_RTU,
/** Modbus over serial line ASCII mode */
MODBUS_MODE_ASCII,
/** Modbus raw ADU mode */
MODBUS_MODE_RAW,
};
/**
* @brief Modbus serial line parameter
*/
struct modbus_serial_param {
/** Baudrate of the serial line */
uint32_t baud;
/** parity UART's parity setting:
* UART_CFG_PARITY_NONE,
* UART_CFG_PARITY_EVEN,
* UART_CFG_PARITY_ODD
*/
enum uart_config_parity parity;
/** stop_bits_client UART's stop bits setting if in client mode:
* UART_CFG_STOP_BITS_0_5,
* UART_CFG_STOP_BITS_1,
* UART_CFG_STOP_BITS_1_5,
* UART_CFG_STOP_BITS_2,
*/
enum uart_config_stop_bits stop_bits_client;
};
/**
* @brief Modbus server parameter
*/
struct modbus_server_param {
/** Pointer to the User Callback structure */
struct modbus_user_callbacks *user_cb;
/** Modbus unit ID of the server */
uint8_t unit_id;
};
struct modbus_raw_cb {
modbus_raw_cb_t raw_tx_cb;
void *user_data;
};
/**
* @brief User parameter structure to configure Modbus interface
* as client or server.
*/
struct modbus_iface_param {
/** Mode of the interface */
enum modbus_mode mode;
union {
struct modbus_server_param server;
/** Amount of time client will wait for
* a response from the server.
*/
uint32_t rx_timeout;
};
union {
/** Serial support parameter of the interface */
struct modbus_serial_param serial;
/** Pointer to raw ADU callback function */
struct modbus_raw_cb rawcb;
};
};
/**
* @brief Configure Modbus Interface as raw ADU server
*
* @param iface Modbus RTU interface index
* @param param Configuration parameter of the server interface
*
* @retval 0 If the function was successful
*/
int modbus_init_server(const int iface, struct modbus_iface_param param);
/**
* @brief Configure Modbus Interface as raw ADU client
*
* @param iface Modbus RTU interface index
* @param param Configuration parameter of the client interface
*
* @retval 0 If the function was successful
*/
int modbus_init_client(const int iface, struct modbus_iface_param param);
/**
* @brief Disable Modbus Interface
*
* This function is called to disable Modbus interface.
*
* @param iface Modbus interface index
*
* @retval 0 If the function was successful
*/
int modbus_disable(const uint8_t iface);
#if IS_ENABLED(CONFIG_MODBUS_LOCKING)
/**
* @brief Lock Modbus Interface
*
* The lock ensures that no new requests are handled while the inetraface is locked.asm
* May be used to ensure atomic update of register values in server role.
*
* @param iface Modbus interface index
* @param timeout Waiting period to lock the interface,
* or one of the special values K_NO_WAIT and
* K_FOREVER.
*
* @retval 0 Interface locked.
* @retval -ENODEV Invalid interface index
* @retval -EBUSY Returned without waiting.
* @retval -EAGAIN Waiting period timed out.
*/
int modbus_lock(const int iface, k_timeout_t timeout);
/**
* @brief Unlock Modbus Interface
*
* @param iface Modbus interface index
*
* @retval 0 Interface unlocked.
* @retval -ENODEV Invalid interface index
* @retval -EPERM The current thread does not own the lock
* @retval -EINVAL The interface is not locked
*/
int modbus_unlock(const int iface);
#endif
/**
* @brief Submit raw ADU
*
* @param iface Modbus RTU interface index
* @param adu Pointer to the RAW ADU struct that is received
*
* @retval 0 If transfer was successful
*/
int modbus_raw_submit_rx(const int iface, const struct modbus_adu *adu);
/**
* @brief Put MBAP header into a buffer
*
* @param adu Pointer to the RAW ADU struct
* @param header Pointer to the buffer in which MBAP header
* will be placed.
*/
void modbus_raw_put_header(const struct modbus_adu *adu, uint8_t *header);
/**
* @brief Get MBAP header from a buffer
*
* @param adu Pointer to the RAW ADU struct
* @param header Pointer to the buffer containing MBAP header
*/
void modbus_raw_get_header(struct modbus_adu *adu, const uint8_t *header);
/**
* @brief Set Server Device Failure exception
*
* This function modifies ADU passed by the pointer.
*
* @param adu Pointer to the RAW ADU struct
*/
void modbus_raw_set_server_failure(struct modbus_adu *adu);
/**
* @brief Use interface as backend to send and receive ADU
*
* This function overwrites ADU passed by the pointer and generates
* exception responses if backend interface is misconfigured or
* target device is unreachable.
*
* @param iface Modbus client interface index
* @param adu Pointer to the RAW ADU struct
*
* @retval 0 If transfer was successful
*/
int modbus_raw_backend_txn(const int iface, struct modbus_adu *adu);
/**
* @brief Register a user-defined function code handler.
*
* The Modbus specification allows users to define standard function codes
* missing from Zephyr's Modbus implementation as well as add non-standard
* function codes in the ranges 65 to 72 and 100 to 110 (decimal), as per
* specification.
*
* This function registers a new handler at runtime for the given
* function code.
*
* @param iface Modbus client interface index
* @param custom_fc User defined function code and callback pair
*
* @retval 0 on success
*/
int modbus_register_user_fc(const int iface, struct modbus_custom_fc *custom_fc);
#ifdef __cplusplus
}
#endif
/**
* @}
*/
#endif /* ZEPHYR_INCLUDE_MODBUS_H_ */