Skip to content

Commit f1dd7d9

Browse files
committed
add support for ESP-IDF platform complete with documentation
1 parent f2a619d commit f1dd7d9

File tree

6 files changed

+497
-0
lines changed

6 files changed

+497
-0
lines changed

src/esp-idf/CMakeLists.txt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
"src/modbus-data.c"
5+
"src/modbus-rtu.c"
6+
"src/modbus-tcp.c"
7+
"src/modbus.c")
8+
9+
set(include_dirs src)
10+
11+
set(priv_include_dirs ../)
12+
13+
list(APPEND priv_include_dirs)
14+
15+
add_prefix(srcs "${CMAKE_CURRENT_LIST_DIR}/libmodbus/" ${srcs})
16+
add_prefix(include_dirs "${CMAKE_CURRENT_LIST_DIR}/libmodbus/" ${include_dirs})
17+
add_prefix(priv_include_dirs "${CMAKE_CURRENT_LIST_DIR}/libmodbus/" ${priv_include_dirs})
18+
19+
message(STATUS "DEBUG: Using libmodbus component folder: ${CMAKE_CURRENT_LIST_DIR}.")
20+
21+
idf_component_register(SRCS "${srcs}"
22+
INCLUDE_DIRS "${include_dirs}"
23+
PRIV_INCLUDE_DIRS "${priv_include_dirs}"
24+
PRIV_REQUIRES driver vfs)
25+

src/esp-idf/README.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Instructions to use with Espressif IoT Development Framework (ESP-IDF)
2+
3+
## Adding libmodbus as a component
4+
5+
- Create a subdirectory at the top level of your ESP-IDF project where you will
6+
place this and create a component, ie. `libmodbus`. This directory will be
7+
referred as *component directory* further down in this text.
8+
9+
- Download the latest version of libmodbus with the method of your choice and
10+
unpack it under component directory in a subdirectory `libmodbus`
11+
12+
- Copy the files supplied in this documentation directory to the component directory,
13+
namely:
14+
- `CMakeLists.txt`: the CMake script that enumerates files and directories used
15+
in the build as well as defines needed dependencies
16+
- `component.mk`: the component build definition
17+
- `config.h`: the library configuration, especially tailored for ESP-IDF. This is
18+
usually generated with the autoconf tool, but this is not present in ESP-IDF and
19+
is therefore manually prepared and customized
20+
- `idf_component.yml`: the component description file
21+
22+
- Add a reference from your main project in the project top level `CMakeLists.txt` to
23+
the newly added module with something like: `set(EXTRA_COMPONENT_DIRS libmodbus/)`.
24+
If you already have other components you may just add the reference to the newly
25+
added component.
26+
27+
Now you are almost ready to use libmodbus in your project!
28+
29+
If you desire to use the library for serial communication, you will need to do a few
30+
more hardware configuration steps before using the `modbus_new_rtu` call, namely:
31+
32+
- Configure, if needed, any pins for the used uart via `uart_set_pin`
33+
34+
- Install the uart driver via the `uart_driver_install`
35+
36+
- Configure, if needed, the uart mode (ie. set it to half duplex) via `uart_set_mode`
37+
38+
These configurations are not included in libmodbus as they are highly hardware specific
39+
and would require a heavy change in the library interface.
40+
41+
## Other details using libmodbus with ESP-IDF
42+
43+
- The serial driver is implemented using the `vfs` virtual filesystem component. This
44+
makes the changes needed for the library minimal, but may not be the most performant
45+
solution.
46+
47+
- The serial name (first parameter to `modbus_new_rtu`) should be a string containing
48+
only the serial index (ie. `"1"` or `"2"`).
49+
50+
- When using the TCP version be aware of the maximum number of sockets that can be
51+
open on the platform: this is by default 10 and can be possibly raised to 16 in
52+
a standard configuration. Please check the `LWIP_MAX_SOCKETS` configuration
53+
variable.
54+

src/esp-idf/component.mk

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
INCLUDEDIRS := src
2+
PRIV_INCLUDEDIRS := ../
3+
SRCDIRS := src
4+
5+
COMPONENT_PRIV_INCLUDEDIRS = $(addprefix libmodbus/, \
6+
$(PRIV_INCLUDEDIRS) \
7+
)
8+
9+
COMPONENT_SRCDIRS = $(addprefix libmodbus/, \
10+
$(SRCDIRS) \
11+
)
12+
13+
COMPONENT_ADD_INCLUDEDIRS = $(addprefix libmodbus/, \
14+
$(INCLUDEDIRS) \
15+
)
16+
17+
18+

src/esp-idf/config.h

Lines changed: 296 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,296 @@
1+
/* config.h. Generated from config.h.in by configure. */
2+
/* config.h.in. Generated from configure.ac by autoheader. */
3+
4+
#define ESP_IDF 1
5+
6+
#define O_NDELAY O_NONBLOCK
7+
8+
/* Define to 1 if you have the `accept4' function. */
9+
// #define HAVE_ACCEPT4 1
10+
11+
/* Define to 1 if you have the <arpa/inet.h> header file. */
12+
#define HAVE_ARPA_INET_H 1
13+
14+
/* Define to 1 if you have the <byteswap.h> header file. */
15+
// #define HAVE_BYTESWAP_H 1
16+
17+
/* Define to 1 if you have the declaration of `TIOCM_RTS', and to 0 if you
18+
don't. */
19+
// #define HAVE_DECL_TIOCM_RTS 1
20+
21+
/* Define to 1 if you have the declaration of `TIOCSRS485', and to 0 if you
22+
don't. */
23+
// #define HAVE_DECL_TIOCSRS485 1
24+
25+
/* Define to 1 if you have the declaration of `__CYGWIN__', and to 0 if you
26+
don't. */
27+
#define HAVE_DECL___CYGWIN__ 0
28+
29+
/* Define to 1 if you have the <dlfcn.h> header file. */
30+
#define HAVE_DLFCN_H 1
31+
32+
/* Define to 1 if you have the <errno.h> header file. */
33+
#define HAVE_ERRNO_H 1
34+
35+
/* Define to 1 if you have the <fcntl.h> header file. */
36+
#define HAVE_FCNTL_H 1
37+
38+
/* Define to 1 if you have the `getaddrinfo' function. */
39+
#define HAVE_GETADDRINFO 1
40+
41+
/* Define to 1 if you have the `gettimeofday' function. */
42+
#define HAVE_GETTIMEOFDAY 1
43+
44+
/* Define to 1 if you have the `inet_ntop' function. */
45+
#define HAVE_INET_NTOP 1
46+
47+
/* Define to 1 if you have the `inet_pton' function. */
48+
#define HAVE_INET_PTON 1
49+
50+
/* Define to 1 if you have the <inttypes.h> header file. */
51+
#define HAVE_INTTYPES_H 1
52+
53+
/* Define to 1 if you have the <limits.h> header file. */
54+
#define HAVE_LIMITS_H 1
55+
56+
/* Define to 1 if you have the <linux/serial.h> header file. */
57+
// #define HAVE_LINUX_SERIAL_H 1
58+
59+
/* Define to 1 if you have the <minix/config.h> header file. */
60+
/* #undef HAVE_MINIX_CONFIG_H */
61+
62+
/* Define to 1 if you have the <netdb.h> header file. */
63+
#define HAVE_NETDB_H 1
64+
65+
/* Define to 1 if you have the <netinet/in.h> header file. */
66+
#define HAVE_NETINET_IN_H 1
67+
68+
/* Define to 1 if you have the <netinet/tcp.h> header file. */
69+
#define HAVE_NETINET_TCP_H 1
70+
71+
/* Define to 1 if you have the `select' function. */
72+
#define HAVE_SELECT 1
73+
74+
/* Define to 1 if you have the `socket' function. */
75+
#define HAVE_SOCKET 1
76+
77+
/* Define to 1 if you have the <stdint.h> header file. */
78+
#define HAVE_STDINT_H 1
79+
80+
/* Define to 1 if you have the <stdio.h> header file. */
81+
#define HAVE_STDIO_H 1
82+
83+
/* Define to 1 if you have the <stdlib.h> header file. */
84+
#define HAVE_STDLIB_H 1
85+
86+
/* Define to 1 if you have the `strerror' function. */
87+
#define HAVE_STRERROR 1
88+
89+
/* Define to 1 if you have the <strings.h> header file. */
90+
#define HAVE_STRINGS_H 1
91+
92+
/* Define to 1 if you have the <string.h> header file. */
93+
#define HAVE_STRING_H 1
94+
95+
/* Define to 1 if you have the `strlcpy' function. */
96+
/* #undef HAVE_STRLCPY */
97+
98+
/* Define to 1 if you have the <sys/ioctl.h> header file. */
99+
#define HAVE_SYS_IOCTL_H 1
100+
101+
/* Define to 1 if you have the <sys/params.h> header file. */
102+
/* #undef HAVE_SYS_PARAMS_H */
103+
104+
/* Define to 1 if you have the <sys/socket.h> header file. */
105+
#define HAVE_SYS_SOCKET_H 1
106+
107+
/* Define to 1 if you have the <sys/stat.h> header file. */
108+
#define HAVE_SYS_STAT_H 1
109+
110+
/* Define to 1 if you have the <sys/time.h> header file. */
111+
#define HAVE_SYS_TIME_H 1
112+
113+
/* Define to 1 if you have the <sys/types.h> header file. */
114+
#define HAVE_SYS_TYPES_H 1
115+
116+
/* Define to 1 if you have the <termios.h> header file. */
117+
#define HAVE_TERMIOS_H 1
118+
119+
/* Define to 1 if you have the <time.h> header file. */
120+
#define HAVE_TIME_H 1
121+
122+
/* Define to 1 if you have the <unistd.h> header file. */
123+
#define HAVE_UNISTD_H 1
124+
125+
/* Define to 1 if you have the <wchar.h> header file. */
126+
#define HAVE_WCHAR_H 1
127+
128+
/* Define to 1 if you have the <winsock2.h> header file. */
129+
/* #undef HAVE_WINSOCK2_H */
130+
131+
/* Define to the sub-directory where libtool stores uninstalled libraries. */
132+
#define LT_OBJDIR ".libs/"
133+
134+
/* Name of package */
135+
#define PACKAGE "libmodbus"
136+
137+
/* Define to the address where bug reports for this package should be sent. */
138+
#define PACKAGE_BUGREPORT "https://github.com/stephane/libmodbus/issues"
139+
140+
/* Define to the full name of this package. */
141+
#define PACKAGE_NAME "libmodbus"
142+
143+
/* Define to the full name and version of this package. */
144+
#define PACKAGE_STRING "libmodbus 3.1.10"
145+
146+
/* Define to the one symbol short name of this package. */
147+
#define PACKAGE_TARNAME "libmodbus"
148+
149+
/* Define to the home page for this package. */
150+
#define PACKAGE_URL "http://libmodbus.org/"
151+
152+
/* Define to the version of this package. */
153+
#define PACKAGE_VERSION "3.1.10"
154+
155+
/* Define to 1 if all of the C90 standard headers exist (not just the ones
156+
required in a freestanding environment). This macro is provided for
157+
backward compatibility; new code need not use it. */
158+
#define STDC_HEADERS 1
159+
160+
/* Enable extensions on AIX 3, Interix. */
161+
#ifndef _ALL_SOURCE
162+
# define _ALL_SOURCE 1
163+
#endif
164+
/* Enable general extensions on macOS. */
165+
#ifndef _DARWIN_C_SOURCE
166+
# define _DARWIN_C_SOURCE 1
167+
#endif
168+
/* Enable general extensions on Solaris. */
169+
#ifndef __EXTENSIONS__
170+
# define __EXTENSIONS__ 1
171+
#endif
172+
/* Enable GNU extensions on systems that have them. */
173+
#ifndef _GNU_SOURCE
174+
# define _GNU_SOURCE 1
175+
#endif
176+
/* Enable X/Open compliant socket functions that do not require linking
177+
with -lxnet on HP-UX 11.11. */
178+
#ifndef _HPUX_ALT_XOPEN_SOCKET_API
179+
# define _HPUX_ALT_XOPEN_SOCKET_API 1
180+
#endif
181+
/* Identify the host operating system as Minix.
182+
This macro does not affect the system headers' behavior.
183+
A future release of Autoconf may stop defining this macro. */
184+
#ifndef _MINIX
185+
/* # undef _MINIX */
186+
#endif
187+
/* Enable general extensions on NetBSD.
188+
Enable NetBSD compatibility extensions on Minix. */
189+
#ifndef _NETBSD_SOURCE
190+
# define _NETBSD_SOURCE 1
191+
#endif
192+
/* Enable OpenBSD compatibility extensions on NetBSD.
193+
Oddly enough, this does nothing on OpenBSD. */
194+
#ifndef _OPENBSD_SOURCE
195+
# define _OPENBSD_SOURCE 1
196+
#endif
197+
/* Define to 1 if needed for POSIX-compatible behavior. */
198+
#ifndef _POSIX_SOURCE
199+
/* # undef _POSIX_SOURCE */
200+
#endif
201+
/* Define to 2 if needed for POSIX-compatible behavior. */
202+
#ifndef _POSIX_1_SOURCE
203+
/* # undef _POSIX_1_SOURCE */
204+
#endif
205+
/* Enable POSIX-compatible threading on Solaris. */
206+
#ifndef _POSIX_PTHREAD_SEMANTICS
207+
# define _POSIX_PTHREAD_SEMANTICS 1
208+
#endif
209+
/* Enable extensions specified by ISO/IEC TS 18661-5:2014. */
210+
#ifndef __STDC_WANT_IEC_60559_ATTRIBS_EXT__
211+
# define __STDC_WANT_IEC_60559_ATTRIBS_EXT__ 1
212+
#endif
213+
/* Enable extensions specified by ISO/IEC TS 18661-1:2014. */
214+
#ifndef __STDC_WANT_IEC_60559_BFP_EXT__
215+
# define __STDC_WANT_IEC_60559_BFP_EXT__ 1
216+
#endif
217+
/* Enable extensions specified by ISO/IEC TS 18661-2:2015. */
218+
#ifndef __STDC_WANT_IEC_60559_DFP_EXT__
219+
# define __STDC_WANT_IEC_60559_DFP_EXT__ 1
220+
#endif
221+
/* Enable extensions specified by ISO/IEC TS 18661-4:2015. */
222+
#ifndef __STDC_WANT_IEC_60559_FUNCS_EXT__
223+
# define __STDC_WANT_IEC_60559_FUNCS_EXT__ 1
224+
#endif
225+
/* Enable extensions specified by ISO/IEC TS 18661-3:2015. */
226+
#ifndef __STDC_WANT_IEC_60559_TYPES_EXT__
227+
# define __STDC_WANT_IEC_60559_TYPES_EXT__ 1
228+
#endif
229+
/* Enable extensions specified by ISO/IEC TR 24731-2:2010. */
230+
#ifndef __STDC_WANT_LIB_EXT2__
231+
# define __STDC_WANT_LIB_EXT2__ 1
232+
#endif
233+
/* Enable extensions specified by ISO/IEC 24747:2009. */
234+
#ifndef __STDC_WANT_MATH_SPEC_FUNCS__
235+
# define __STDC_WANT_MATH_SPEC_FUNCS__ 1
236+
#endif
237+
/* Enable extensions on HP NonStop. */
238+
#ifndef _TANDEM_SOURCE
239+
# define _TANDEM_SOURCE 1
240+
#endif
241+
/* Enable X/Open extensions. Define to 500 only if necessary
242+
to make mbstate_t available. */
243+
#ifndef _XOPEN_SOURCE
244+
/* # undef _XOPEN_SOURCE */
245+
#endif
246+
247+
248+
/* Version number of package */
249+
#define VERSION "3.1.10"
250+
251+
/* _ */
252+
#define WINVER 0x0501
253+
254+
/* Number of bits in a file offset, on hosts where this is settable. */
255+
/* #undef _FILE_OFFSET_BITS */
256+
257+
/* Define for large files, on AIX-style hosts. */
258+
/* #undef _LARGE_FILES */
259+
260+
/* Define for Solaris 2.5.1 so the uint32_t typedef from <sys/synch.h>,
261+
<pthread.h>, or <semaphore.h> is not used. If the typedef were allowed, the
262+
#define below would cause a syntax error. */
263+
/* #undef _UINT32_T */
264+
265+
/* Define for Solaris 2.5.1 so the uint8_t typedef from <sys/synch.h>,
266+
<pthread.h>, or <semaphore.h> is not used. If the typedef were allowed, the
267+
#define below would cause a syntax error. */
268+
/* #undef _UINT8_T */
269+
270+
/* Define to `__inline__' or `__inline' if that's what the C compiler
271+
calls it, or to nothing if 'inline' is not supported under any name. */
272+
#ifndef __cplusplus
273+
/* #undef inline */
274+
#endif
275+
276+
/* Define to the type of a signed integer type of width exactly 64 bits if
277+
such a type exists and the standard includes do not define it. */
278+
/* #undef int64_t */
279+
280+
/* Define to `unsigned int' if <sys/types.h> does not define. */
281+
/* #undef size_t */
282+
283+
/* Define to `int' if <sys/types.h> does not define. */
284+
/* #undef ssize_t */
285+
286+
/* Define to the type of an unsigned integer type of width exactly 16 bits if
287+
such a type exists and the standard includes do not define it. */
288+
/* #undef uint16_t */
289+
290+
/* Define to the type of an unsigned integer type of width exactly 32 bits if
291+
such a type exists and the standard includes do not define it. */
292+
/* #undef uint32_t */
293+
294+
/* Define to the type of an unsigned integer type of width exactly 8 bits if
295+
such a type exists and the standard includes do not define it. */
296+
/* #undef uint8_t */

0 commit comments

Comments
 (0)