Skip to content

Commit 8dd26d1

Browse files
committed
Add support for ESP-IDF platform complete with documentation
1 parent bb13443 commit 8dd26d1

File tree

6 files changed

+506
-0
lines changed

6 files changed

+506
-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: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
- As the ESP-IDF does not provide a `nanosleep` function in its SDK, you should add
28+
this in your project so you will be able to find it at linking time, for example:
29+
30+
```
31+
int nanosleep(const struct timespec *req, struct timespec *_Nullable rem) {
32+
return usleep(req->tv_sec*1000 + req->tv_nsec / 1000);
33+
}
34+
```
35+
36+
Now you are almost ready to use libmodbus in your project!
37+
38+
If you desire to use the library for serial communication, you will need to do a few
39+
more hardware configuration steps before using the `modbus_new_rtu` call, namely:
40+
41+
- Configure, if needed, any pins for the used uart via `uart_set_pin`
42+
43+
- Install the uart driver via the `uart_driver_install`
44+
45+
- Configure, if needed, the uart mode (ie. set it to half duplex) via `uart_set_mode`
46+
47+
These configurations are not included in libmodbus as they are highly hardware specific
48+
and would require a heavy change in the library interface.
49+
50+
## Other details using libmodbus with ESP-IDF
51+
52+
- The serial driver is implemented using the `vfs` virtual filesystem component. This
53+
makes the changes needed for the library minimal, but may not be the most performant
54+
solution.
55+
56+
- The serial name (first parameter to `modbus_new_rtu`) should be a string containing
57+
only the serial index (ie. `"1"` or `"2"`).
58+
59+
- When using the TCP version be aware of the maximum number of sockets that can be
60+
open on the platform: this is by default 10 and can be possibly raised to 16 in
61+
a standard configuration. Please check the `LWIP_MAX_SOCKETS` configuration
62+
variable.
63+

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+

0 commit comments

Comments
 (0)