Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ESP-IDF support #745

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/esp-idf/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# The following five lines of boilerplate have to be in your project's
# CMakeLists in this exact order for cmake to work correctly
set(srcs
"src/modbus-data.c"
"src/modbus-rtu.c"
"src/modbus-tcp.c"
"src/modbus.c")

set(include_dirs src)

set(priv_include_dirs ../)

list(APPEND priv_include_dirs)

add_prefix(srcs "${CMAKE_CURRENT_LIST_DIR}/libmodbus/" ${srcs})
add_prefix(include_dirs "${CMAKE_CURRENT_LIST_DIR}/libmodbus/" ${include_dirs})
add_prefix(priv_include_dirs "${CMAKE_CURRENT_LIST_DIR}/libmodbus/" ${priv_include_dirs})

message(STATUS "DEBUG: Using libmodbus component folder: ${CMAKE_CURRENT_LIST_DIR}.")

idf_component_register(SRCS "${srcs}"
INCLUDE_DIRS "${include_dirs}"
PRIV_INCLUDE_DIRS "${priv_include_dirs}"
PRIV_REQUIRES driver vfs)

70 changes: 70 additions & 0 deletions src/esp-idf/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# Instructions to use with Espressif IoT Development Framework (ESP-IDF)

## Adding libmodbus as a component

- Create a subdirectory at the top level of your ESP-IDF project where you will
place this and create a component, ie. `libmodbus`. This directory will be
referred as *component directory* further down in this text.

- Download the latest version of libmodbus with the method of your choice and
unpack it under component directory in a subdirectory `libmodbus`

- Copy the files supplied in this documentation directory to the component directory,
namely:
- `CMakeLists.txt`: the CMake script that enumerates files and directories used
in the build as well as defines needed dependencies
- `component.mk`: the component build definition
- `config.h`: the library configuration, especially tailored for ESP-IDF. This is
usually generated with the autoconf tool, but this is not present in ESP-IDF and
is therefore manually prepared and customized
- `idf_component.yml`: the component description file

- Add a reference from your main project in the project top level `CMakeLists.txt` to
the newly added module with something like: `set(EXTRA_COMPONENT_DIRS libmodbus/)`.
If you already have other components you may just add the reference to the newly
added component.

- As the ESP-IDF does not provide a `nanosleep` function in its SDK, you should add
this in your project so you will be able to find it at linking time, for example:

```
int nanosleep(const struct timespec *req, struct timespec *_Nullable rem) {
return usleep(req->tv_sec*1000 + req->tv_nsec / 1000);
}
```

Now you are almost ready to use libmodbus in your project!

If you desire to use the library for serial communication, you will need to do a few
more hardware configuration steps before using the `modbus_new_rtu` call, namely:

- Configure, if needed, any pins for the used uart via `uart_set_pin`
fedepell marked this conversation as resolved.
Show resolved Hide resolved

- Install the uart driver via the `uart_driver_install`
fedepell marked this conversation as resolved.
Show resolved Hide resolved

- Configure, if needed, the uart mode (ie. set it to half duplex) via `uart_set_mode`
fedepell marked this conversation as resolved.
Show resolved Hide resolved

These configurations are not included in libmodbus as they are highly hardware specific
and would require a heavy change in the library interface.

## Other details using libmodbus with ESP-IDF

- The serial driver is implemented using the `vfs` virtual filesystem component. This
makes the changes needed for the library minimal, but may not be the most performant
solution. Be aware that if you are not using the UART0 as console (ie. you are
disabling console or you are using the USB Serial/JTAG Controller) you may need to
explicitly initialize UART VFS in your main program as well just by calling
`uart_vfs_dev_register()` (from `driver/uart_vfs.h`).

- The serial name (first parameter to `modbus_new_rtu`) should be a string containing
only the serial index (ie. `"1"` or `"2"`).

- When using the TCP version be aware of the maximum number of sockets that can be
open on the platform: this is by default 10 and can be possibly raised to 16 in
a standard configuration. Please check the `LWIP_MAX_SOCKETS` configuration
variable.

- Older versions (<=5.3.1) of ESP-IDF contain a buglet in the serial read code that
may cause some packet loss under certain circumstances (when line ending
chars are present), see also: https://github.com/espressif/esp-idf/issues/14155
Please use a newer version of ESP-IDF if you see this behavior.
18 changes: 18 additions & 0 deletions src/esp-idf/component.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
INCLUDEDIRS := src
PRIV_INCLUDEDIRS := ../
SRCDIRS := src

COMPONENT_PRIV_INCLUDEDIRS = $(addprefix libmodbus/, \
$(PRIV_INCLUDEDIRS) \
)

COMPONENT_SRCDIRS = $(addprefix libmodbus/, \
$(SRCDIRS) \
)

COMPONENT_ADD_INCLUDEDIRS = $(addprefix libmodbus/, \
$(INCLUDEDIRS) \
)



Loading