Commit graph

47 commits

Author SHA1 Message Date
Björn Stenberg 731f8d4736 modbus: Respect CONFIG_UART_USE_RUNTIME_CONFIGURE
Only perform runtime UART configuration if it is enabled.

Signed-off-by: Björn Stenberg <bjorn@haxx.se>
2024-04-10 10:01:55 +02:00
Jean Nanchen 80f7d518c6 modbus: Remove mode reset in modbus_disable function
Remove reset mode in modbus_disable.

Signed-off-by: Jean Nanchen <jean.nanchen@gmail.com>
2024-01-25 16:00:38 +00:00
Krzysztof Chruściński b032382011 modbus: Add dependency to UART runtime configuration
Force enabling of the UART runtime configuration for modbus serial.

Signed-off-by: Krzysztof Chruściński <krzysztof.chruscinski@nordicsemi.no>
2023-11-15 10:02:18 +01:00
Henrik Lindblom 01757cfd32 modbus: add support for defining custom functions
Enables support for custom function codes. Modbus specification allows
vendor specific function codes in the range 65-72 & 100-110 [1] and this
feature allows users to implement custom logic for those codes.
Additionally, since the Zephyr Modbus stack doesn't implement all defined
Modbus fcs this feature allows users to add support for codes outside the
basic register reading / writing functionality offered by Zephyr.

Custom function codes can be added on a per-interface basis and the handler
structures are allocated by the caller.

[1]: https://modbus.org/docs/Modbus_Application_Protocol_V1_1b.pdf

Signed-off-by: Henrik Lindblom <henrik.lindblom@vaisala.com>
2023-09-29 13:05:42 +02:00
Parthiban Nallathambi 2c4fb882bb modbus: use macro for function code for input register
use MODBUS_FC04_IN_REG_RD for input register reading function code 4.

Signed-off-by: Parthiban Nallathambi <parthiban@linumiz.com>
2023-08-16 14:58:47 +02:00
Michael Trimarchi b4bac3d7f1 modbus: cancel the server_work in modbus disable
Allow to periodically initializes/disables the modbus interface.
Switch from modbus line discipline to serial and switch back to modbus.
This can be required when the serial is shared by two sensors

Signed-off-by: Michael Trimarchi <michael@amarulasolutions.com>
2023-05-22 10:50:20 +02:00
Andrei Emeltchenko 250f212688 modbus: Remove redundant check
ctx is the result of pointer math and never NULL.

Signed-off-by: Andrei Emeltchenko <andrei.emeltchenko@intel.com>
2023-02-06 10:03:20 +01:00
Anas Nashif cffe98d9de crc: Make the build of crc function dependent on a Kconfig
Add CONFIG_CRC for building CRC related routines.
CRC routines are now being built for each application, whether used or
not and are add in the build system unconditionally.

Keep CONFIG_CRC enabled by default for now and until all users have
converted to use the new option.

Partial fix for #50654

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2022-11-23 13:30:00 +01:00
Gerard Marull-Paretas 79e6b0e0f6 includes: prefer <zephyr/kernel.h> over <zephyr/zephyr.h>
As of today <zephyr/zephyr.h> is 100% equivalent to <zephyr/kernel.h>.
This patch proposes to then include <zephyr/kernel.h> instead of
<zephyr/zephyr.h> since it is more clear that you are including the
Kernel APIs and (probably) nothing else. <zephyr/zephyr.h> sounds like a
catch-all header that may be confusing. Most applications need to
include a bunch of other things to compile, e.g. driver headers or
subsystem headers like BT, logging, etc.

The idea of a catch-all header in Zephyr is probably not feasible
anyway. Reason is that Zephyr is not a library, like it could be for
example `libpython`. Zephyr provides many utilities nowadays: a kernel,
drivers, subsystems, etc and things will likely grow. A catch-all header
would be massive, difficult to keep up-to-date. It is also likely that
an application will only build a small subset. Note that subsystem-level
headers may use a catch-all approach to make things easier, though.

NOTE: This patch is **NOT** removing the header, just removing its usage
in-tree. I'd advocate for its deprecation (add a #warning on it), but I
understand many people will have concerns.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2022-09-05 16:31:47 +02:00
Kumar Gala 26574e239a modbus: Update Kconfig
Utilize DT_HAS_<COMPAT>_ENABLED for devicetree based driver

Signed-off-by: Kumar Gala <galak@kernel.org>
2022-09-02 11:00:08 +00:00
Parthiban Nallathambi 75479f5338 modbus: add user data for adu callback
add user data for adu callback, which helps in passing
socket and relevant application parameters.

Signed-off-by: Parthiban Nallathambi <parthiban@linumiz.com>
2022-09-02 10:51:57 +02:00
Johann Fischer 46a0e5347d modbus: remove the use of DT_INST_LABEL and DT_PROP(inst, label)
Although it is possible to simply use the interface number,
it has proven convenient to use the names for the interfaces
in the samples.

Migrate to DEVICE_DT_NAME().

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2022-08-11 09:39:24 -07:00
Johann Fischer 700323dd3d modbus: use DT_INST_PARENT instead of DT_INST_BUS
Node is not a bus controller,
use DT_INST_PARENT instead of DT_INST_BUS.

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2022-07-13 11:36:59 +02:00
Kumar Gala e1e8179690 modbus: serial: Convert to DEVICE_DT_GET
Move to using DEVICE_DT_GET so we can phase out DT_LABEL.

Signed-off-by: Kumar Gala <galak@kernel.org>
2022-06-30 10:32:42 +02:00
Krzysztof Chruscinski 041f0e5379 all: logging: Remove log_strdup function
Logging v1 has been removed and log_strdup wrapper function is no
longer needed. Removing the function and its use in the tree.

Signed-off-by: Krzysztof Chruscinski <krzysztof.chruscinski@nordicsemi.no>
2022-06-23 13:42:23 +02:00
Gerard Marull-Paretas 5113c1418d subsystems: migrate includes to <zephyr/...>
In order to bring consistency in-tree, migrate all subsystems code to
the new prefix <zephyr/...>. Note that the conversion has been scripted,
refer to zephyrproject-rtos#45388 for more details.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2022-05-09 12:07:35 +02:00
Constantin Krischke 8ee675946c modbus: stop bits can now be set individually in client mode
Added the stop_bits_client parameter to the modbus_serial_param struct.
Being able to configure the number of stop bits for the client
independently from the parity setting, allows to support connecting to
modbus server that do not follow the MODBUS over Serial Line Specification
and Implementation Guide.

Signed-off-by: Constantin Krischke <constantin.krischke@lemonbeat.com>
Signed-off-by: Jan Geldmacher <jan.geldmacher@lemonbeat.com>
2022-03-24 08:23:17 -04:00
Nazar Kazakov f483b1bc4c everywhere: fix typos
Fix a lot of typos

Signed-off-by: Nazar Kazakov <nazar.kazakov.work@gmail.com>
2022-03-18 13:24:08 -04:00
Gerard Marull-Paretas 95fb0ded6b kconfig: remove Enable from boolean prompts
According to Kconfig guidelines, boolean prompts must not start with
"Enable...". The following command has been used to automate the changes
in this patch:

sed -i "s/bool \"[Ee]nables\? \(\w\)/bool \"\U\1/g" **/Kconfig*

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2022-03-09 15:35:54 +01:00
Krzysztof Chruscinski 47ae656cc1 all: Deprecate UTIL_LISTIFY and replace with LISTIFY
UTIL_LISTIFY is deprecated. Replacing it with LISTIFY.

Signed-off-by: Krzysztof Chruscinski <krzysztof.chruscinski@nordicsemi.no>
2022-03-08 11:03:30 +01:00
Johann Fischer da47f41e42 modbus: do not loop in UART IRQ handler
On some platforms, if there are multiple MODBUS interfaces,
receiving of RTU frames may fail. Replacing while loop with
just "if" fixes this problem.

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2022-02-09 22:11:16 -05:00
Marius Scholtz 3ea5343f0d modbus: serial: Fix incomplete transmission issue
This patch is to fix an issue where the transceiver chip is
disabled before it has transmitted all data. This causes the
message to be corrupted because the last few bytes are missing.

The fix adds a check to make sure the transmission is completed
before disabling the transceiver.

Signed-off-by: Marius Scholtz <mariuss@ricelectronics.com>
2022-02-09 15:47:07 +01:00
Carles Cufi d0ebeee0aa modbus: Get rid of custom CRC16 function
In previous commits the crc16_ansi() function has been made compliant
with the CRC-16-ANSI aka CRC-16-MODBUS standard. Use that standard
function instead of a custom one.

Signed-off-by: Carles Cufi <carles.cufi@nordicsemi.no>
2022-02-04 12:33:22 -05:00
Johann Fischer 767a87b1b6 modbus: keep transaction and protocol IDs in gateway mode
Serial line interface does not use transaction and protocol
IDs. Continue to use memcpy() for the whole frame as it is safer
and more efficient but temporarily store transaction and protocol
IDs, and write it back if the transfer was successful.

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2022-01-10 09:45:42 -06:00
Luc Viala 19c519eb05 modbus: copy trans_id & proto_id to response header
Copy Transaction and Protocol Identifiers to response header.

Signed-off-by: Luc Viala <luc.viala19@gmail.com>
2021-08-26 06:56:25 -04:00
Johann Fischer a42c51654b modbus: fix untrusted loop bound in modbus client
Use size_t type where it makes sense and
check if the buffer is large enough before it is used.

Fixes: #33786
Fixes: #33795

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2021-05-05 11:33:56 +02:00
David Leach 3f0da93902 subsys/modbus: Fix potential memory overwrite (coverity)
The size of ctx->rx_adu.data is not CONFIG_MODBUS_BUFFER_SIZE. Coverity
flagged the potential overwrite so just use sizeof().

Fixes #33789
Fixes #33792
Fixes #33818

Signed-off-by: David Leach <david.leach@nxp.com>
2021-04-01 04:52:10 -04:00
Jacob Siverskog 2954dcf4ed modbus: fix potential null pointer dereference
reorganize code so that null pointer check is done prior to accessing
element.

Signed-off-by: Jacob Siverskog <jacob@teenage.engineering>
2021-03-24 10:56:06 +02:00
Johann Fischer f943a5581d modbus: rework RS-485 transceiver DE,RE GPIO configuration
Follow modern way to configure DE,RE GPIO using
struct gpio_dt_spec.

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2021-03-19 15:50:21 +01:00
Johann Fischer 6394298960 modbus: add raw ADU support
MODBUS raw ADU support allows to implement
MODBUS messaging service over TCP or UDP.

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2021-03-19 15:50:21 +01:00
Johann Fischer 4ff616b647 modbus: rework interface configuration
Use commot parameter structure to configure server
or client interfaces.

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2021-03-19 15:50:21 +01:00
Johann Fischer 55204d504e modbus: document return values of internal functions
Document return values of internal functions.

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2021-03-19 15:50:21 +01:00
Johann Fischer 144a3a5ba2 modbus: return ETIMEDOUT on timeout instead of EIO
Return ETIMEDOUT on timeout instead of EIO.

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2021-03-19 15:50:21 +01:00
Johann Fischer 0bf4916efd modbus: move some RX/TX ADU related code to the core
Let the core call the modbus_tx_adu() to make
the process more comprehensible.
Move tx-wait-for-rx handling outside of client code.

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2021-03-19 15:50:21 +01:00
Johann Fischer e3c630d25f modbus: rename internal designations and do few minor fixups
Prefix internal functions and structs with modbus_.
Use unit_id consistently instead of node_addr.
Fix mbm_ remainder and rename to mbc_.
Rename struct modbus_frame to modbus_adu since
ADU is closer to what the structure represents.

Let the compiler/linker do the job and
remove ifdef around mbc_validate_fc03fp_response().

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2021-03-19 15:50:21 +01:00
Johann Fischer 669d3e9e91 modbus: make MODBUS support over serial line optional
Make MODBUS support over serial line optional.

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2021-03-19 15:50:21 +01:00
Johann Fischer b132ab556c modbus: move serial line config outside context
Move serial line config outside context.

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2021-03-19 15:50:21 +01:00
Johann Fischer 3ddb47cc43 modbus: move MODBUS over serial line code to own file
Move MODBUS over serial line code to own source file.

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2021-03-19 15:50:21 +01:00
Johann Fischer 19d0b474c1 modbus: use enum for MODBUS mode
Use enum for MODBUS mode.

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2021-03-19 15:50:21 +01:00
Johann Fischer 9792a6c43e modbus: rename mb_rtu_ to modbus_
Rename mb_rtu_ to modbus_.

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2021-03-19 15:50:21 +01:00
Johann Fischer 699b2cdf9b modbus: remove RTU from configuration and headers
Remove RTU from configuration and headers

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2021-03-19 15:50:21 +01:00
Johann Fischer ed0629f623 modbus: rename MODBUS RTU API to common MODBUS API
Rename MODBUS RTU API to common MODBUS API.

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2021-03-19 15:50:21 +01:00
Johann Fischer ac487727df modbus: reset buffer after reception
Reset buffer after reception, even if an error occurs.

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2021-03-19 15:50:21 +01:00
Johann Fischer a9690e0862 modbus: fix ASCII frame reception and add test for ASCII mode
Fix ASCII frame reception and add test for ASCII mode.

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2021-03-19 15:50:21 +01:00
Johann Fischer b0518d6100 modbus: get interface index according to interface name
Add function to get Modbus RTU interface index according
to interface name. This can be used to clearly identify
interfaces in the application.

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2021-03-19 15:50:21 +01:00
Johann Fischer c96aeff785 modbus: add control for DE/nRE RS-485 transceiver signals
Add support to control DE/nRE RS-485 transceiver signals
over GPIO pins. Useful if the UART controller does not
support RS-485 mode.

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2021-03-19 15:50:21 +01:00
Johann Fischer 07ab652c6c modbus: add MODBUS RTU subsystem
Add MODBUS RTU (over serial line) subsystem.
MODBUS RTU implementation supports booth server and
client roles. Some components of the implementation are based
on the uC/Modbus stack, which was published under Apache license,
(https://github.com/SiliconLabs/uC-Modbus
 fdd1218a28e313c1212fed5ed42e5c65d3056a2c).

Resolves: #2854

Signed-off-by: Johann Fischer <j.fischer@phytec.de>
2021-03-19 15:50:21 +01:00