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

subsys: add MODBUS subsystem #26516

Merged
merged 31 commits into from
Mar 19, 2021

Conversation

jfischer-no
Copy link
Collaborator

@jfischer-no jfischer-no commented Jun 29, 2020

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.

Copy link
Member

@alexanderwachter alexanderwachter left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A brief review of the "main" commit.

subsys/modbus/mb_rtu_core.c Outdated Show resolved Hide resolved
subsys/modbus/mb_rtu_core.c Outdated Show resolved Hide resolved
subsys/modbus/mb_rtu_core.c Outdated Show resolved Hide resolved
subsys/modbus/mb_rtu_core.c Outdated Show resolved Hide resolved
subsys/modbus/mb_rtu_client.c Outdated Show resolved Hide resolved
subsys/modbus/mb_rtu_client.c Outdated Show resolved Hide resolved
subsys/modbus/mb_rtu_internal.h Outdated Show resolved Hide resolved
subsys/modbus/mb_rtu_server.c Outdated Show resolved Hide resolved
@github-actions github-actions bot added area: Device Tree area: Devicetree Binding PR modifies or adds a Device Tree binding labels Jun 30, 2020
atomic_t state;

#ifdef CONFIG_MODBUS_RTU_FC08_DIAGNOSTIC
uint16_t mbs_msg_ctr;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you need the mbs_ prefix in those? they are already namespaced inside the struct

Copy link
Collaborator Author

@jfischer-no jfischer-no Jul 10, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only reason is that this counters are used only by the server (MODBUS_RTU_FC08_DIAGNOSTIC depends on server mode).

@brooksprumo

This comment has been minimized.

@jfischer-no

This comment has been minimized.

Copy link
Member

@carlescufi carlescufi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this live under subsys/mgmt/modbus?

@martinjaeger martinjaeger self-requested a review September 1, 2020 16:38
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 <[email protected]>
Fix ASCII frame reception and add test for ASCII mode.

Signed-off-by: Johann Fischer <[email protected]>
Reset buffer after reception, even if an error occurs.

Signed-off-by: Johann Fischer <[email protected]>
Rename MODBUS RTU API to common MODBUS API.

Signed-off-by: Johann Fischer <[email protected]>
Remove RTU from configuration and headers

Signed-off-by: Johann Fischer <[email protected]>
Rename mb_rtu_ to modbus_.

Signed-off-by: Johann Fischer <[email protected]>
Remove _rtu_ from modbus test.

Signed-off-by: Johann Fischer <[email protected]>
Use enum for MODBUS mode.

Signed-off-by: Johann Fischer <[email protected]>
Move MODBUS over serial line code to own source file.

Signed-off-by: Johann Fischer <[email protected]>
Move serial line config outside context.

Signed-off-by: Johann Fischer <[email protected]>
Make MODBUS support over serial line optional.

Signed-off-by: Johann Fischer <[email protected]>
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 <[email protected]>
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 <[email protected]>
Return ETIMEDOUT on timeout instead of EIO.

Signed-off-by: Johann Fischer <[email protected]>
Document return values of internal functions.

Signed-off-by: Johann Fischer <[email protected]>
Use commot parameter structure to configure server
or client interfaces.

Signed-off-by: Johann Fischer <[email protected]>
MODBUS raw ADU support allows to implement
MODBUS messaging service over TCP or UDP.

Signed-off-by: Johann Fischer <[email protected]>
Add raw ADU support test.

Signed-off-by: Johann Fischer <[email protected]>
Add MODBUS TCP server sample.

Signed-off-by: Johann Fischer <[email protected]>
Add TCP to serial line gateway.

Signed-off-by: Johann Fischer <[email protected]>
Update description and add TCP sample references.

Signed-off-by: Johann Fischer <[email protected]>
Follow modern way to configure LEDs GPIO using
struct gpio_dt_spec and GPIO_DT_SPEC_GET.

Signed-off-by: Johann Fischer <[email protected]>
Follow modern way to configure DE,RE GPIO using
struct gpio_dt_spec.

Signed-off-by: Johann Fischer <[email protected]>
Copy link
Member

@carlescufi carlescufi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The scancode toolkit failures are false positives. The files include an SPDX identifier for Apache v2.

@carlescufi carlescufi merged commit f943a55 into zephyrproject-rtos:master Mar 19, 2021
Copy link
Member

@anangl anangl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a bit late with my comments, but I'm adding them anyway, just in case there will be a follow up PR.


static int init_modbus_server(void)
{
const uint32_t mb_rtu_br = 19200;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not seem to be used.

static int init_modbus_server(void)
{
const uint32_t mb_rtu_br = 19200;
const char iface_name[] = {DT_PROP(DT_INST(0, zephyr_modbus_serial), label)};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This way you create a local array (on stack) and fill it with the provided string (those brackets are actually confusing). You need to use then log_strdup(iface_name) below, otherwise its content will be lost.
But I guess this was not intended and the following would work here:

const char *iface_name = DT_PROP(DT_INST(0, zephyr_modbus_serial), label);

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@anangl agree, Agree. Nicer to see the latter.

The const will make sure that this data is stored in RO region (flash). How is stack related here?


static int init_modbus_client(void)
{
const char iface_name[] = {DT_PROP(DT_INST(0, zephyr_modbus_serial), label)};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See the corresponding comment in samples/subsys/modbus/rtu_server/src/main.c.

Suggested change
const char iface_name[] = {DT_PROP(DT_INST(0, zephyr_modbus_serial), label)};
const char *iface_name = DT_PROP(DT_INST(0, zephyr_modbus_serial), label);

Comment on lines +78 to +84
err = modbus_write_coil(client_iface, node, addr++, true);
if (err != 0) {
LOG_ERR("FC05 failed with %d", err);
return;
}

k_msleep(sleep);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be placed in a loop, to avoid code duplication.

Comment on lines +17 to +18
If connected directly the MCU pin should be configured
as active high.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this might be confusing, as in both provided overlays this signal is actually active low. Perhaps this sentence could be dropped. Similarly in the RE case. For the same reason, "nRE" and "DE" markings in descriptions could be dropped as well. I don't think they actually bring much value.


static int init_backend_iface(void)
{
const char bend_name[] = {DT_PROP(DT_INST(0, zephyr_modbus_serial), label)};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See the corresponding comment in samples/subsys/modbus/rtu_server/src/main.c.

Suggested change
const char bend_name[] = {DT_PROP(DT_INST(0, zephyr_modbus_serial), label)};
const char *bend_name = DT_PROP(DT_INST(0, zephyr_modbus_serial), label);

After this change, you won't need this log_strdup below.

@jfischer-no jfischer-no deleted the pr-modbus-rtu branch September 15, 2021 12:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: API Changes to public APIs area: Devicetree Binding PR modifies or adds a Device Tree binding area: Devicetree area: Documentation area: Samples Samples area: Tests Issues related to a particular existing or missing test
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Modbus RTU Support