Skip to content

Commit c758052

Browse files
committed
feat(modbus): Bring freemodbus component and example from IDF
Commit ID: d0b9829e
1 parent 4b466c7 commit c758052

File tree

115 files changed

+18603
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

115 files changed

+18603
-0
lines changed

components/freemodbus/CMakeLists.txt

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# The following five lines of boilerplate have to be in your project's
2+
# CMakeLists in this exact order for cmake to work correctly
3+
set(srcs
4+
"common/esp_modbus_master.c"
5+
"common/esp_modbus_slave.c"
6+
"modbus/mb.c"
7+
"modbus/mb_m.c"
8+
"modbus/ascii/mbascii.c"
9+
"modbus/ascii/mbascii_m.c"
10+
"modbus/rtu/mbrtu_m.c"
11+
"modbus/rtu/mbrtu.c"
12+
"modbus/rtu/mbcrc.c"
13+
"modbus/tcp/mbtcp.c"
14+
"modbus/tcp/mbtcp_m.c"
15+
"port/port.c"
16+
"port/portevent.c"
17+
"port/portevent_m.c"
18+
"port/portother.c"
19+
"port/portother_m.c"
20+
"port/portserial.c"
21+
"port/portserial_m.c"
22+
"port/porttimer.c"
23+
"port/porttimer_m.c"
24+
"modbus/functions/mbfunccoils.c"
25+
"modbus/functions/mbfunccoils_m.c"
26+
"modbus/functions/mbfuncdiag.c"
27+
"modbus/functions/mbfuncdisc.c"
28+
"modbus/functions/mbfuncdisc_m.c"
29+
"modbus/functions/mbfuncholding.c"
30+
"modbus/functions/mbfuncholding_m.c"
31+
"modbus/functions/mbfuncinput.c"
32+
"modbus/functions/mbfuncinput_m.c"
33+
"modbus/functions/mbfuncother.c"
34+
"modbus/functions/mbutils.c"
35+
"serial_slave/modbus_controller/mbc_serial_slave.c"
36+
"serial_master/modbus_controller/mbc_serial_master.c"
37+
"tcp_slave/port/port_tcp_slave.c"
38+
"tcp_slave/modbus_controller/mbc_tcp_slave.c"
39+
"tcp_master/modbus_controller/mbc_tcp_master.c"
40+
"tcp_master/port/port_tcp_master.c"
41+
"common/esp_modbus_master_tcp.c"
42+
"common/esp_modbus_slave_tcp.c"
43+
"common/esp_modbus_master_serial.c"
44+
"common/esp_modbus_slave_serial.c")
45+
46+
set(include_dirs common/include)
47+
48+
set(priv_include_dirs common port modbus modbus/ascii modbus/functions
49+
modbus/rtu modbus/tcp modbus/include)
50+
51+
list(APPEND priv_include_dirs serial_slave/port serial_slave/modbus_controller
52+
serial_master/port serial_master/modbus_controller
53+
tcp_slave/port tcp_slave/modbus_controller
54+
tcp_master/port tcp_master/modbus_controller)
55+
56+
idf_component_register(SRCS "${srcs}"
57+
INCLUDE_DIRS "${include_dirs}"
58+
PRIV_INCLUDE_DIRS "${priv_include_dirs}"
59+
REQUIRES driver)

components/freemodbus/Kconfig

Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
menu "Modbus configuration"
2+
3+
config FMB_COMM_MODE_TCP_EN
4+
bool "Enable Modbus stack support for TCP communication mode"
5+
default y
6+
help
7+
Enable Modbus TCP option for stack.
8+
9+
config FMB_TCP_PORT_DEFAULT
10+
int "Modbus TCP port number"
11+
range 0 65535
12+
default 502
13+
depends on FMB_COMM_MODE_TCP_EN
14+
help
15+
Modbus default port number used by Modbus TCP stack
16+
17+
config FMB_TCP_PORT_MAX_CONN
18+
int "Maximum allowed connections for TCP stack"
19+
range 1 6
20+
default 5
21+
depends on FMB_COMM_MODE_TCP_EN
22+
help
23+
Maximum allowed connections number for Modbus TCP stack.
24+
This is used by Modbus master and slave port layer to establish connections.
25+
This parameter may decrease performance of Modbus stack and can cause
26+
increasing of processing time (increase only if absolutely necessary).
27+
28+
config FMB_TCP_CONNECTION_TOUT_SEC
29+
int "Modbus TCP connection timeout"
30+
range 1 3600
31+
default 20
32+
depends on FMB_COMM_MODE_TCP_EN
33+
help
34+
Modbus TCP connection timeout in seconds.
35+
Once expired the current connection with the client will be closed
36+
and Modbus slave will be waiting for new connection to accept.
37+
38+
config FMB_COMM_MODE_RTU_EN
39+
bool "Enable Modbus stack support for RTU mode"
40+
default y
41+
help
42+
Enable RTU Modbus communication mode option for Modbus serial stack.
43+
44+
config FMB_COMM_MODE_ASCII_EN
45+
bool "Enable Modbus stack support for ASCII mode"
46+
default y
47+
help
48+
Enable ASCII Modbus communication mode option for Modbus serial stack.
49+
50+
config FMB_MASTER_TIMEOUT_MS_RESPOND
51+
int "Slave respond timeout (Milliseconds)"
52+
default 150
53+
range 50 3000
54+
help
55+
If master sends a frame which is not broadcast, it has to wait sometime for slave response.
56+
if slave is not respond in this time, the master will process timeout error.
57+
58+
config FMB_MASTER_DELAY_MS_CONVERT
59+
int "Slave conversion delay (Milliseconds)"
60+
default 200
61+
range 50 400
62+
help
63+
If master sends a broadcast frame, it has to wait conversion time to delay,
64+
then master can send next frame.
65+
66+
config FMB_QUEUE_LENGTH
67+
int "Modbus serial task queue length"
68+
range 0 200
69+
default 20
70+
help
71+
Modbus serial driver queue length. It is used by event queue task.
72+
See the serial driver API for more information.
73+
74+
config FMB_PORT_TASK_STACK_SIZE
75+
int "Modbus port task stack size"
76+
range 2048 8192
77+
default 4096
78+
help
79+
Modbus port task stack size for rx/tx event processing.
80+
It may be adjusted when debugging is enabled (for example).
81+
82+
config FMB_SERIAL_BUF_SIZE
83+
int "Modbus serial task RX/TX buffer size"
84+
range 0 2048
85+
default 256
86+
help
87+
Modbus serial task RX and TX buffer size for UART driver initialization.
88+
This buffer is used for modbus frame transfer. The Modbus protocol maximum
89+
frame size is 256 bytes. Bigger size can be used for non standard implementations.
90+
91+
config FMB_SERIAL_ASCII_BITS_PER_SYMB
92+
int "Number of data bits per ASCII character"
93+
default 8
94+
range 7 8
95+
depends on FMB_COMM_MODE_ASCII_EN
96+
help
97+
This option defines the number of data bits per ASCII character.
98+
99+
config FMB_SERIAL_ASCII_TIMEOUT_RESPOND_MS
100+
int "Response timeout for ASCII communication mode (ms)"
101+
default 1000
102+
range 300 2000
103+
depends on FMB_COMM_MODE_ASCII_EN
104+
help
105+
This option defines response timeout of slave in milliseconds for ASCII communication mode.
106+
Thus the timeout will expire and allow the master program to handle the error.
107+
108+
config FMB_PORT_TASK_PRIO
109+
int "Modbus port task priority"
110+
range 3 10
111+
default 10
112+
help
113+
Modbus port data processing task priority.
114+
The priority of Modbus controller task is equal to (CONFIG_FMB_PORT_TASK_PRIO - 1).
115+
116+
config FMB_CONTROLLER_SLAVE_ID_SUPPORT
117+
bool "Modbus controller slave ID support"
118+
default y
119+
help
120+
Modbus slave ID support enable.
121+
When enabled the Modbus <Report Slave ID> command is supported by stack.
122+
123+
config FMB_CONTROLLER_SLAVE_ID
124+
hex "Modbus controller slave ID"
125+
range 0 4294967295
126+
default 0x00112233
127+
depends on FMB_CONTROLLER_SLAVE_ID_SUPPORT
128+
help
129+
Modbus slave ID value to identify modbus device
130+
in the network using <Report Slave ID> command.
131+
Most significant byte of ID is used as short device ID and
132+
other three bytes used as long ID.
133+
134+
config FMB_CONTROLLER_NOTIFY_TIMEOUT
135+
int "Modbus controller notification timeout (ms)"
136+
range 0 200
137+
default 20
138+
help
139+
Modbus controller notification timeout in milliseconds.
140+
This timeout is used to send notification about accessed parameters.
141+
142+
config FMB_CONTROLLER_NOTIFY_QUEUE_SIZE
143+
int "Modbus controller notification queue size"
144+
range 0 200
145+
default 20
146+
help
147+
Modbus controller notification queue size.
148+
The notification queue is used to get information about accessed parameters.
149+
150+
config FMB_CONTROLLER_STACK_SIZE
151+
int "Modbus controller stack size"
152+
range 0 8192
153+
default 4096
154+
help
155+
Modbus controller task stack size. The Stack size may be adjusted when
156+
debug mode is used which requires more stack size (for example).
157+
158+
config FMB_EVENT_QUEUE_TIMEOUT
159+
int "Modbus stack event queue timeout (ms)"
160+
range 0 500
161+
default 20
162+
help
163+
Modbus stack event queue timeout in milliseconds. This may help to optimize
164+
Modbus stack event processing time.
165+
166+
config FMB_TIMER_PORT_ENABLED
167+
bool "Modbus slave stack use timer for 3.5T symbol time measurement"
168+
default y
169+
help
170+
If this option is set the Modbus stack uses timer for T3.5 time measurement.
171+
Else the internal UART TOUT timeout is used for 3.5T symbol time measurement.
172+
173+
config FMB_TIMER_GROUP
174+
int "Modbus Timer group number"
175+
range 0 1
176+
default 0
177+
help
178+
Modbus Timer group number that is used for timeout measurement.
179+
180+
config FMB_TIMER_INDEX
181+
int "Modbus Timer index in the group"
182+
range 0 1
183+
default 0
184+
help
185+
Modbus Timer Index in the group that is used for timeout measurement.
186+
187+
config FMB_TIMER_ISR_IN_IRAM
188+
bool "Place timer interrupt handler into IRAM"
189+
default n
190+
select UART_ISR_IN_IRAM
191+
help
192+
This option places Modbus timer IRQ handler into IRAM.
193+
This allows to avoid delays related to processing of non-IRAM-safe interrupts
194+
during a flash write operation (NVS updating a value, or some other
195+
flash API which has to perform an read/write operation and disable CPU cache).
196+
This option has dependency with the UART_ISR_IN_IRAM option which places UART interrupt
197+
handler into IRAM to prevent delays related to processing of UART events.
198+
199+
endmenu
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/* Copyright 2018 Espressif Systems (Shanghai) PTE LTD
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
// Stack callback functions prototypes
17+
18+
#ifndef _ESP_MODBUS_CALLBACKS_H_
19+
#define _ESP_MODBUS_CALLBACKS_H_
20+
21+
#include "mb.h"
22+
#include "mb_m.h"
23+
24+
typedef eMBErrorCode (*reg_input_cb)(UCHAR*, USHORT, USHORT);
25+
typedef eMBErrorCode (*reg_holding_cb)(UCHAR*, USHORT, USHORT, eMBRegisterMode);
26+
typedef eMBErrorCode (*reg_coils_cb)(UCHAR*, USHORT, USHORT, eMBRegisterMode);
27+
typedef eMBErrorCode (*reg_discrete_cb)(UCHAR*, USHORT, USHORT);
28+
29+
#endif /* _ESP_MODBUS_CALLBACKS_H_ */

0 commit comments

Comments
 (0)