modbus: remove RTU from configuration and headers

Remove RTU from configuration and headers

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
This commit is contained in:
Johann Fischer 2021-03-02 19:26:04 +01:00 committed by Carles Cufí
parent ed0629f623
commit 699b2cdf9b
15 changed files with 163 additions and 163 deletions

View file

@ -26,8 +26,8 @@
* @{
*/
#ifndef ZEPHYR_INCLUDE_MODBUS_RTU_H_
#define ZEPHYR_INCLUDE_MODBUS_RTU_H_
#ifndef ZEPHYR_INCLUDE_MODBUS_H_
#define ZEPHYR_INCLUDE_MODBUS_H_
#include <drivers/uart.h>
@ -404,4 +404,4 @@ int modbus_disable(const uint8_t iface);
* @}
*/
#endif /* ZEPHYR_INCLUDE_MODBUS_RTU_H_ */
#endif /* ZEPHYR_INCLUDE_MODBUS_H_ */

View file

@ -4,5 +4,5 @@ CONFIG_SERIAL=y
CONFIG_UART_INTERRUPT_DRIVEN=y
CONFIG_UART_LINE_CTRL=n
CONFIG_MODBUS_RTU=y
CONFIG_MODBUS_RTU_ROLE_CLIENT=y
CONFIG_MODBUS=y
CONFIG_MODBUS_ROLE_CLIENT=y

View file

@ -7,7 +7,7 @@
#include <zephyr.h>
#include <sys/util.h>
#include <drivers/gpio.h>
#include <modbus/modbus_rtu.h>
#include <modbus/modbus.h>
#include <logging/log.h>
LOG_MODULE_REGISTER(mbc_sample, LOG_LEVEL_INF);

View file

@ -4,5 +4,5 @@ CONFIG_SERIAL=y
CONFIG_UART_INTERRUPT_DRIVEN=y
CONFIG_UART_LINE_CTRL=n
CONFIG_MODBUS_RTU=y
CONFIG_MODBUS_RTU_ROLE_SERVER=y
CONFIG_MODBUS=y
CONFIG_MODBUS_ROLE_SERVER=y

View file

@ -7,7 +7,7 @@
#include <zephyr.h>
#include <sys/util.h>
#include <drivers/gpio.h>
#include <modbus/modbus_rtu.h>
#include <modbus/modbus.h>
#include <logging/log.h>
LOG_MODULE_REGISTER(mbs_sample, LOG_LEVEL_INF);

View file

@ -1,7 +1,7 @@
# Copyright (c) 2020 PHYTEC Messtechnik GmbH
# SPDX-License-Identifier: Apache-2.0
if(CONFIG_MODBUS_RTU)
if(CONFIG_MODBUS)
zephyr_library()
zephyr_include_directories(${ZEPHYR_BASE}/subsys/modbus)
@ -11,12 +11,12 @@ if(CONFIG_MODBUS_RTU)
)
zephyr_library_sources_ifdef(
CONFIG_MODBUS_RTU_SERVER
CONFIG_MODBUS_SERVER
mb_rtu_server.c
)
zephyr_library_sources_ifdef(
CONFIG_MODBUS_RTU_CLIENT
CONFIG_MODBUS_CLIENT
mb_rtu_client.c
)
endif()

View file

@ -1,13 +1,13 @@
# Copyright (c) 2020 PHYTEC Messtechnik GmbH
# SPDX-License-Identifier: Apache-2.0
config MODBUS_RTU
bool "Modbus RTU support"
config MODBUS
bool "Modbus support"
depends on SERIAL && SERIAL_HAS_DRIVER
if MODBUS_RTU
if MODBUS
config MODBUS_RTU_BUFFER_SIZE
config MODBUS_BUFFER_SIZE
int "Modbus buffer size"
default 256
range 64 256
@ -16,50 +16,50 @@ config MODBUS_RTU_BUFFER_SIZE
choice
prompt "Supported node roles"
default MODBUS_RTU_ROLE_CLIENT_SERVER
default MODBUS_ROLE_CLIENT_SERVER
help
Specify the type of supported node roles.
config MODBUS_RTU_ROLE_CLIENT
config MODBUS_ROLE_CLIENT
bool "Client support"
config MODBUS_RTU_ROLE_SERVER
config MODBUS_ROLE_SERVER
bool "Server support"
config MODBUS_RTU_ROLE_CLIENT_SERVER
config MODBUS_ROLE_CLIENT_SERVER
bool "Client and server support"
endchoice
config MODBUS_RTU_SERVER
config MODBUS_SERVER
bool
default y if MODBUS_RTU_ROLE_SERVER || MODBUS_RTU_ROLE_CLIENT_SERVER
default y if MODBUS_ROLE_SERVER || MODBUS_ROLE_CLIENT_SERVER
config MODBUS_RTU_CLIENT
config MODBUS_CLIENT
bool
default y if MODBUS_RTU_ROLE_CLIENT || MODBUS_RTU_ROLE_CLIENT_SERVER
default y if MODBUS_ROLE_CLIENT || MODBUS_ROLE_CLIENT_SERVER
config MODBUS_RTU_ASCII_MODE
config MODBUS_ASCII_MODE
bool "Modbus transmission mode ASCII"
help
Enable ASCII transmission mode.
config MODBUS_RTU_FP_EXTENSIONS
config MODBUS_FP_EXTENSIONS
bool "Floating-Point extensions"
default y
help
Enable Floating-Point extensions
config MODBUS_RTU_FC08_DIAGNOSTIC
config MODBUS_FC08_DIAGNOSTIC
bool "Enable FC08 Diagnostic support"
depends on MODBUS_RTU_SERVER
depends on MODBUS_SERVER
default y
help
Enable function code 08 Diagnostic support
module = MODBUS_RTU
module-str = Modbus RTU Support
module-help = Sets log level for Modbus RTU support
module = MODBUS
module-str = Modbus Support
module-help = Sets log level for Modbus support
source "subsys/logging/Kconfig.template.log_config"
endif

View file

@ -21,12 +21,12 @@
#include <string.h>
#include <sys/byteorder.h>
#include <mb_rtu_internal.h>
#include <modbus_internal.h>
#include <logging/log.h>
LOG_MODULE_REGISTER(mb_rtu_c, CONFIG_MODBUS_RTU_LOG_LEVEL);
LOG_MODULE_REGISTER(modbus_c, CONFIG_MODBUS_LOG_LEVEL);
static int mbm_validate_response_fc(struct mb_rtu_context *ctx,
static int mbm_validate_response_fc(struct modbus_context *ctx,
const uint8_t node_addr,
uint8_t fc)
{
@ -54,8 +54,8 @@ static int mbm_validate_response_fc(struct mb_rtu_context *ctx,
return 0;
}
#ifdef CONFIG_MODBUS_RTU_FP_EXTENSIONS
static int mbm_validate_fc03fp_response(struct mb_rtu_context *ctx, float *ptbl)
#ifdef CONFIG_MODBUS_FP_EXTENSIONS
static int mbm_validate_fc03fp_response(struct modbus_context *ctx, float *ptbl)
{
uint8_t resp_byte_cnt;
uint8_t req_byte_cnt;
@ -83,7 +83,7 @@ static int mbm_validate_fc03fp_response(struct mb_rtu_context *ctx, float *ptbl)
}
#endif
static int mbm_validate_rd_response(struct mb_rtu_context *ctx,
static int mbm_validate_rd_response(struct modbus_context *ctx,
const uint8_t node_addr,
uint8_t fc,
uint8_t *data)
@ -122,8 +122,8 @@ static int mbm_validate_rd_response(struct mb_rtu_context *ctx,
break;
case MODBUS_FC03_HOLDING_REG_RD:
if (IS_ENABLED(CONFIG_MODBUS_RTU_FP_EXTENSIONS) &&
(req_addr >= MODBUS_RTU_FP_ADDR)) {
if (IS_ENABLED(CONFIG_MODBUS_FP_EXTENSIONS) &&
(req_addr >= MODBUS_FP_EXTENSIONS_ADDR)) {
err = mbm_validate_fc03fp_response(ctx, (float *)data);
break;
}
@ -151,7 +151,7 @@ static int mbm_validate_rd_response(struct mb_rtu_context *ctx,
return err;
}
static int mbm_validate_fc08_response(struct mb_rtu_context *ctx,
static int mbm_validate_fc08_response(struct modbus_context *ctx,
const uint8_t node_addr,
uint16_t *data)
{
@ -203,7 +203,7 @@ static int mbm_validate_fc08_response(struct mb_rtu_context *ctx,
return err;
}
static int mbm_validate_wr_response(struct mb_rtu_context *ctx,
static int mbm_validate_wr_response(struct modbus_context *ctx,
const uint8_t node_addr,
uint8_t fc)
{
@ -238,7 +238,7 @@ static int mbm_validate_wr_response(struct mb_rtu_context *ctx,
return err;
}
static int mbm_send_cmd(struct mb_rtu_context *ctx, const uint8_t node_addr,
static int mbm_send_cmd(struct modbus_context *ctx, const uint8_t node_addr,
uint8_t fc, void *data)
{
int err;
@ -302,7 +302,7 @@ int modbus_read_coils(const int iface,
uint8_t *const coil_tbl,
const uint16_t num_coils)
{
struct mb_rtu_context *ctx = mb_get_context(iface);
struct modbus_context *ctx = mb_get_context(iface);
int err;
if (ctx == NULL) {
@ -327,7 +327,7 @@ int modbus_read_dinputs(const int iface,
uint8_t *const di_tbl,
const uint16_t num_di)
{
struct mb_rtu_context *ctx = mb_get_context(iface);
struct modbus_context *ctx = mb_get_context(iface);
int err;
if (ctx == NULL) {
@ -352,7 +352,7 @@ int modbus_read_holding_regs(const int iface,
uint16_t *const reg_buf,
const uint16_t num_regs)
{
struct mb_rtu_context *ctx = mb_get_context(iface);
struct modbus_context *ctx = mb_get_context(iface);
int err;
if (ctx == NULL) {
@ -372,14 +372,14 @@ int modbus_read_holding_regs(const int iface,
}
#ifdef CONFIG_MODBUS_RTU_FP_EXTENSIONS
#ifdef CONFIG_MODBUS_FP_EXTENSIONS
int modbus_read_holding_regs_fp(const int iface,
const uint8_t node_addr,
const uint16_t start_addr,
float *const reg_buf,
const uint16_t num_regs)
{
struct mb_rtu_context *ctx = mb_get_context(iface);
struct modbus_context *ctx = mb_get_context(iface);
int err;
if (ctx == NULL) {
@ -405,7 +405,7 @@ int modbus_read_input_regs(const int iface,
uint16_t *const reg_buf,
const uint16_t num_regs)
{
struct mb_rtu_context *ctx = mb_get_context(iface);
struct modbus_context *ctx = mb_get_context(iface);
int err;
if (ctx == NULL) {
@ -429,7 +429,7 @@ int modbus_write_coil(const int iface,
const uint16_t coil_addr,
const bool coil_state)
{
struct mb_rtu_context *ctx = mb_get_context(iface);
struct modbus_context *ctx = mb_get_context(iface);
int err;
uint16_t coil_val;
@ -460,7 +460,7 @@ int modbus_write_holding_reg(const int iface,
const uint16_t start_addr,
const uint16_t reg_val)
{
struct mb_rtu_context *ctx = mb_get_context(iface);
struct modbus_context *ctx = mb_get_context(iface);
int err;
if (ctx == NULL) {
@ -485,7 +485,7 @@ int modbus_request_diagnostic(const int iface,
const uint16_t data,
uint16_t *const data_out)
{
struct mb_rtu_context *ctx = mb_get_context(iface);
struct modbus_context *ctx = mb_get_context(iface);
int err;
if (ctx == NULL) {
@ -510,7 +510,7 @@ int modbus_write_coils(const int iface,
uint8_t *const coil_tbl,
const uint16_t num_coils)
{
struct mb_rtu_context *ctx = mb_get_context(iface);
struct modbus_context *ctx = mb_get_context(iface);
uint8_t num_bytes;
int err;
uint8_t *data_ptr;
@ -543,7 +543,7 @@ int modbus_write_holding_regs(const int iface,
uint16_t *const reg_buf,
const uint16_t num_regs)
{
struct mb_rtu_context *ctx = mb_get_context(iface);
struct modbus_context *ctx = mb_get_context(iface);
uint8_t num_bytes;
int err;
uint8_t *data_ptr;
@ -573,14 +573,14 @@ int modbus_write_holding_regs(const int iface,
return err;
}
#ifdef CONFIG_MODBUS_RTU_FP_EXTENSIONS
#ifdef CONFIG_MODBUS_FP_EXTENSIONS
int modbus_write_holding_regs_fp(const int iface,
const uint8_t node_addr,
const uint16_t start_addr,
float *const reg_buf,
const uint16_t num_regs)
{
struct mb_rtu_context *ctx = mb_get_context(iface);
struct modbus_context *ctx = mb_get_context(iface);
uint8_t num_bytes;
int err;
uint8_t *data_ptr;

View file

@ -20,12 +20,12 @@
*/
#include <logging/log.h>
LOG_MODULE_REGISTER(mb_rtu, CONFIG_MODBUS_RTU_LOG_LEVEL);
LOG_MODULE_REGISTER(modbus, CONFIG_MODBUS_LOG_LEVEL);
#include <kernel.h>
#include <string.h>
#include <sys/byteorder.h>
#include <mb_rtu_internal.h>
#include <modbus_internal.h>
#define DT_DRV_COMPAT zephyr_modbus_serial
@ -55,11 +55,11 @@ DT_INST_FOREACH_STATUS_OKAY(MB_RTU_DEFINE_GPIO_CFGS)
.re = MB_RTU_ASSIGN_GPIO_CFG(n, re_gpios), \
},
static struct mb_rtu_context mb_ctx_tbl[] = {
static struct modbus_context mb_ctx_tbl[] = {
DT_INST_FOREACH_STATUS_OKAY(MODBUS_DT_GET_DEV)
};
static void mb_tx_enable(struct mb_rtu_context *ctx)
static void mb_tx_enable(struct modbus_context *ctx)
{
if (ctx->de != NULL) {
gpio_pin_set(ctx->de->dev, ctx->de->pin, 1);
@ -68,7 +68,7 @@ static void mb_tx_enable(struct mb_rtu_context *ctx)
uart_irq_tx_enable(ctx->dev);
}
static void mb_tx_disable(struct mb_rtu_context *ctx)
static void mb_tx_disable(struct modbus_context *ctx)
{
uart_irq_tx_disable(ctx->dev);
if (ctx->de != NULL) {
@ -76,7 +76,7 @@ static void mb_tx_disable(struct mb_rtu_context *ctx)
}
}
static void mb_rx_enable(struct mb_rtu_context *ctx)
static void mb_rx_enable(struct modbus_context *ctx)
{
if (ctx->re != NULL) {
gpio_pin_set(ctx->re->dev, ctx->re->pin, 1);
@ -85,7 +85,7 @@ static void mb_rx_enable(struct mb_rtu_context *ctx)
uart_irq_rx_enable(ctx->dev);
}
static void mb_rx_disable(struct mb_rtu_context *ctx)
static void mb_rx_disable(struct modbus_context *ctx)
{
uart_irq_rx_disable(ctx->dev);
if (ctx->re != NULL) {
@ -93,7 +93,7 @@ static void mb_rx_disable(struct mb_rtu_context *ctx)
}
}
#ifdef CONFIG_MODBUS_RTU_ASCII_MODE
#ifdef CONFIG_MODBUS_ASCII_MODE
/* The function calculates an 8-bit Longitudinal Redundancy Check. */
static uint8_t mb_ascii_get_lrc(uint8_t *src, size_t length)
{
@ -118,7 +118,7 @@ static uint8_t mb_ascii_get_lrc(uint8_t *src, size_t length)
}
/* Parses and converts an ASCII mode frame into a Modbus RTU frame. */
static int mb_rx_ascii_frame(struct mb_rtu_context *ctx)
static int mb_rx_ascii_frame(struct modbus_context *ctx)
{
uint8_t *pmsg;
uint8_t *prx_data;
@ -203,7 +203,7 @@ static uint8_t *mb_bin2hex(uint8_t value, uint8_t *pbuf)
return pbuf;
}
static void mb_tx_ascii_frame(struct mb_rtu_context *ctx)
static void mb_tx_ascii_frame(struct modbus_context *ctx)
{
uint16_t tx_bytes = 0;
uint8_t lrc;
@ -249,12 +249,12 @@ static void mb_tx_ascii_frame(struct mb_rtu_context *ctx)
mb_tx_enable(ctx);
}
#else
static int mb_rx_ascii_frame(struct mb_rtu_context *ctx)
static int mb_rx_ascii_frame(struct modbus_context *ctx)
{
return 0;
}
static void mb_tx_ascii_frame(struct mb_rtu_context *ctx)
static void mb_tx_ascii_frame(struct modbus_context *ctx)
{
}
#endif
@ -291,7 +291,7 @@ static uint16_t mb_rtu_crc16(uint8_t *src, size_t length)
}
/* Copy Modbus RTU frame and check if the CRC is valid. */
static int mb_rx_rtu_frame(struct mb_rtu_context *ctx)
static int mb_rx_rtu_frame(struct modbus_context *ctx)
{
uint16_t calc_crc;
uint16_t crc_idx;
@ -299,7 +299,7 @@ static int mb_rx_rtu_frame(struct mb_rtu_context *ctx)
/* Is the message long enough? */
if ((ctx->uart_buf_ctr < MODBUS_RTU_MIN_MSG_SIZE) ||
(ctx->uart_buf_ctr > CONFIG_MODBUS_RTU_BUFFER_SIZE)) {
(ctx->uart_buf_ctr > CONFIG_MODBUS_BUFFER_SIZE)) {
LOG_WRN("Frame length error");
return -EMSGSIZE;
}
@ -327,7 +327,7 @@ static int mb_rx_rtu_frame(struct mb_rtu_context *ctx)
return 0;
}
static void mb_tx_rtu_frame(struct mb_rtu_context *ctx)
static void mb_tx_rtu_frame(struct modbus_context *ctx)
{
uint16_t tx_bytes = 0;
uint8_t *data_ptr;
@ -354,9 +354,9 @@ static void mb_tx_rtu_frame(struct mb_rtu_context *ctx)
mb_tx_enable(ctx);
}
void mb_tx_frame(struct mb_rtu_context *ctx)
void mb_tx_frame(struct modbus_context *ctx)
{
if (IS_ENABLED(CONFIG_MODBUS_RTU_ASCII_MODE) &&
if (IS_ENABLED(CONFIG_MODBUS_ASCII_MODE) &&
(ctx->ascii_mode == true)) {
mb_tx_ascii_frame(ctx);
} else {
@ -368,10 +368,10 @@ void mb_tx_frame(struct mb_rtu_context *ctx)
* A byte has been received from a serial port. We just store it in the buffer
* for processing when a complete packet has been received.
*/
static void mb_cb_handler_rx(struct mb_rtu_context *ctx)
static void mb_cb_handler_rx(struct modbus_context *ctx)
{
if ((ctx->ascii_mode == true) &&
IS_ENABLED(CONFIG_MODBUS_RTU_ASCII_MODE)) {
IS_ENABLED(CONFIG_MODBUS_ASCII_MODE)) {
uint8_t c;
if (uart_fifo_read(ctx->dev, &c, 1) != 1) {
@ -385,7 +385,7 @@ static void mb_cb_handler_rx(struct mb_rtu_context *ctx)
ctx->uart_buf_ctr = 0;
}
if (ctx->uart_buf_ctr < CONFIG_MODBUS_RTU_BUFFER_SIZE) {
if (ctx->uart_buf_ctr < CONFIG_MODBUS_BUFFER_SIZE) {
*ctx->uart_buf_ptr++ = c;
ctx->uart_buf_ctr++;
}
@ -402,7 +402,7 @@ static void mb_cb_handler_rx(struct mb_rtu_context *ctx)
K_USEC(ctx->rtu_timeout), K_NO_WAIT);
n = uart_fifo_read(ctx->dev, ctx->uart_buf_ptr,
(CONFIG_MODBUS_RTU_BUFFER_SIZE -
(CONFIG_MODBUS_BUFFER_SIZE -
ctx->uart_buf_ctr));
ctx->uart_buf_ptr += n;
@ -410,7 +410,7 @@ static void mb_cb_handler_rx(struct mb_rtu_context *ctx)
}
}
static void mb_cb_handler_tx(struct mb_rtu_context *ctx)
static void mb_cb_handler_tx(struct modbus_context *ctx)
{
int n;
@ -429,7 +429,7 @@ static void mb_cb_handler_tx(struct mb_rtu_context *ctx)
static void mb_uart_cb_handler(const struct device *dev, void *app_data)
{
struct mb_rtu_context *ctx = (struct mb_rtu_context *)app_data;
struct modbus_context *ctx = (struct modbus_context *)app_data;
if (ctx == NULL) {
LOG_ERR("Modbus hardware is not properly initialized");
@ -450,8 +450,8 @@ static void mb_uart_cb_handler(const struct device *dev, void *app_data)
static void mb_rx_handler(struct k_work *item)
{
struct mb_rtu_context *ctx =
CONTAINER_OF(item, struct mb_rtu_context, server_work);
struct modbus_context *ctx =
CONTAINER_OF(item, struct modbus_context, server_work);
if (ctx == NULL) {
LOG_ERR("Where is my pointer?");
@ -460,7 +460,7 @@ static void mb_rx_handler(struct k_work *item)
mb_rx_disable(ctx);
if (IS_ENABLED(CONFIG_MODBUS_RTU_ASCII_MODE) &&
if (IS_ENABLED(CONFIG_MODBUS_ASCII_MODE) &&
(ctx->ascii_mode == true)) {
ctx->rx_frame_err = mb_rx_ascii_frame(ctx);
} else {
@ -472,7 +472,7 @@ static void mb_rx_handler(struct k_work *item)
if (ctx->client == true) {
k_sem_give(&ctx->client_wait_sem);
} else if (IS_ENABLED(CONFIG_MODBUS_RTU_SERVER)) {
} else if (IS_ENABLED(CONFIG_MODBUS_SERVER)) {
if (mbs_rx_handler(ctx) == false) {
/* Server does not send response, re-enable RX */
mb_rx_enable(ctx);
@ -483,9 +483,9 @@ static void mb_rx_handler(struct k_work *item)
/* This function is called when the RTU framing timer expires. */
static void mb_rtu_tmr_handler(struct k_timer *t_id)
{
struct mb_rtu_context *ctx;
struct modbus_context *ctx;
ctx = (struct mb_rtu_context *)k_timer_user_data_get(t_id);
ctx = (struct modbus_context *)k_timer_user_data_get(t_id);
if (ctx == NULL) {
LOG_ERR("Failed to get Modbus context");
@ -495,7 +495,7 @@ static void mb_rtu_tmr_handler(struct k_timer *t_id)
k_work_submit(&ctx->server_work);
}
static int mb_configure_uart(struct mb_rtu_context *ctx,
static int mb_configure_uart(struct modbus_context *ctx,
uint32_t baudrate,
enum uart_config_parity parity)
{
@ -553,9 +553,9 @@ static int mb_configure_uart(struct mb_rtu_context *ctx,
return 0;
}
struct mb_rtu_context *mb_get_context(const uint8_t iface)
struct modbus_context *mb_get_context(const uint8_t iface)
{
struct mb_rtu_context *ctx;
struct modbus_context *ctx;
if (iface >= ARRAY_SIZE(mb_ctx_tbl)) {
LOG_ERR("Interface %u not available", iface);
@ -564,7 +564,7 @@ struct mb_rtu_context *mb_get_context(const uint8_t iface)
ctx = &mb_ctx_tbl[iface];
if (!atomic_test_bit(&ctx->state, MB_RTU_STATE_CONFIGURED)) {
if (!atomic_test_bit(&ctx->state, MODBUS_STATE_CONFIGURED)) {
LOG_ERR("Interface not configured");
return NULL;
}
@ -572,7 +572,7 @@ struct mb_rtu_context *mb_get_context(const uint8_t iface)
return ctx;
}
static int mb_configure_gpio(struct mb_rtu_context *ctx)
static int mb_configure_gpio(struct modbus_context *ctx)
{
if (ctx->de != NULL) {
ctx->de->dev = device_get_binding(ctx->de->name);
@ -602,7 +602,7 @@ static int mb_configure_gpio(struct mb_rtu_context *ctx)
return 0;
}
static struct mb_rtu_context *mb_cfg_iface(const uint8_t iface,
static struct modbus_context *mb_cfg_iface(const uint8_t iface,
const uint8_t node_addr,
const uint32_t baud,
const enum uart_config_parity parity,
@ -610,7 +610,7 @@ static struct mb_rtu_context *mb_cfg_iface(const uint8_t iface,
const bool client,
const bool ascii_mode)
{
struct mb_rtu_context *ctx;
struct modbus_context *ctx;
if (iface >= ARRAY_SIZE(mb_ctx_tbl)) {
LOG_ERR("Interface %u not available", iface);
@ -619,13 +619,13 @@ static struct mb_rtu_context *mb_cfg_iface(const uint8_t iface,
ctx = &mb_ctx_tbl[iface];
if (atomic_test_and_set_bit(&ctx->state, MB_RTU_STATE_CONFIGURED)) {
if (atomic_test_and_set_bit(&ctx->state, MODBUS_STATE_CONFIGURED)) {
LOG_ERR("Interface allready used");
return NULL;
}
if ((client == true) &&
!IS_ENABLED(CONFIG_MODBUS_RTU_CLIENT)) {
!IS_ENABLED(CONFIG_MODBUS_CLIENT)) {
LOG_ERR("Modbus client support is not enabled");
ctx->client = false;
return NULL;
@ -644,7 +644,7 @@ static struct mb_rtu_context *mb_cfg_iface(const uint8_t iface,
k_sem_init(&ctx->client_wait_sem, 0, 1);
k_work_init(&ctx->server_work, mb_rx_handler);
if (IS_ENABLED(CONFIG_MODBUS_RTU_FC08_DIAGNOSTIC)) {
if (IS_ENABLED(CONFIG_MODBUS_FC08_DIAGNOSTIC)) {
mbs_reset_statistics(ctx);
}
@ -669,9 +669,9 @@ int modbus_init_server(const uint8_t iface, const uint8_t node_addr,
struct modbus_user_callbacks *const cb,
const bool ascii_mode)
{
struct mb_rtu_context *ctx;
struct modbus_context *ctx;
if (!IS_ENABLED(CONFIG_MODBUS_RTU_SERVER)) {
if (!IS_ENABLED(CONFIG_MODBUS_SERVER)) {
LOG_ERR("Modbus server support is not enabled");
return -ENOTSUP;
}
@ -709,9 +709,9 @@ int modbus_init_client(const uint8_t iface,
const uint32_t rx_timeout,
const bool ascii_mode)
{
struct mb_rtu_context *ctx;
struct modbus_context *ctx;
if (!IS_ENABLED(CONFIG_MODBUS_RTU_CLIENT)) {
if (!IS_ENABLED(CONFIG_MODBUS_CLIENT)) {
LOG_ERR("Modbus client support is not enabled");
return -ENOTSUP;
}
@ -728,7 +728,7 @@ int modbus_init_client(const uint8_t iface,
int modbus_disable(const uint8_t iface)
{
struct mb_rtu_context *ctx;
struct modbus_context *ctx;
if (iface >= ARRAY_SIZE(mb_ctx_tbl)) {
LOG_ERR("Interface %u not available", iface);
@ -745,7 +745,7 @@ int modbus_disable(const uint8_t iface)
ctx->node_addr = 0;
ctx->ascii_mode = false;
ctx->mbs_user_cb = NULL;
atomic_clear_bit(&ctx->state, MB_RTU_STATE_CONFIGURED);
atomic_clear_bit(&ctx->state, MODBUS_STATE_CONFIGURED);
LOG_INF("Disable Modbus interface");

View file

@ -21,17 +21,17 @@
#include <string.h>
#include <sys/byteorder.h>
#include <mb_rtu_internal.h>
#include <modbus_internal.h>
#include <logging/log.h>
LOG_MODULE_REGISTER(mb_rtu_s, CONFIG_MODBUS_RTU_LOG_LEVEL);
LOG_MODULE_REGISTER(modbus_s, CONFIG_MODBUS_LOG_LEVEL);
/*
* This functions are used to reset and update server's
* statistics and communications counters.
*/
#ifdef CONFIG_MODBUS_RTU_FC08_DIAGNOSTIC
void mbs_reset_statistics(struct mb_rtu_context *ctx)
#ifdef CONFIG_MODBUS_FC08_DIAGNOSTIC
void mbs_reset_statistics(struct modbus_context *ctx)
{
/* Initialize all MODBUS event counters. */
ctx->mbs_msg_ctr = 0;
@ -41,27 +41,27 @@ void mbs_reset_statistics(struct mb_rtu_context *ctx)
ctx->mbs_noresp_ctr = 0;
}
static void update_msg_ctr(struct mb_rtu_context *ctx)
static void update_msg_ctr(struct modbus_context *ctx)
{
ctx->mbs_msg_ctr++;
}
static void update_crcerr_ctr(struct mb_rtu_context *ctx)
static void update_crcerr_ctr(struct modbus_context *ctx)
{
ctx->mbs_crc_err_ctr++;
}
static void update_excep_ctr(struct mb_rtu_context *ctx)
static void update_excep_ctr(struct modbus_context *ctx)
{
ctx->mbs_except_ctr++;
}
static void update_server_msg_ctr(struct mb_rtu_context *ctx)
static void update_server_msg_ctr(struct modbus_context *ctx)
{
ctx->mbs_server_msg_ctr++;
}
static void update_noresp_ctr(struct mb_rtu_context *ctx)
static void update_noresp_ctr(struct modbus_context *ctx)
{
ctx->mbs_noresp_ctr++;
}
@ -72,13 +72,13 @@ static void update_noresp_ctr(struct mb_rtu_context *ctx)
#define update_excep_ctr(...)
#define update_server_msg_ctr(...)
#define update_noresp_ctr(...)
#endif /* CONFIG_MODBUS_RTU_FC08_DIAGNOSTIC */
#endif /* CONFIG_MODBUS_FC08_DIAGNOSTIC */
/*
* This function sets the indicated error response code into the response frame.
* Then the routine is called to calculate the error check value.
*/
static void mbs_exception_rsp(struct mb_rtu_context *ctx, uint8_t excep_code)
static void mbs_exception_rsp(struct modbus_context *ctx, uint8_t excep_code)
{
const uint8_t excep_bit = BIT(7);
@ -104,7 +104,7 @@ static void mbs_exception_rsp(struct mb_rtu_context *ctx, uint8_t excep_code)
* Byte count 1 Bytes
* Coil status N * 1 Byte
*/
static bool mbs_fc01_coil_read(struct mb_rtu_context *ctx)
static bool mbs_fc01_coil_read(struct modbus_context *ctx)
{
const uint16_t coils_limit = 2000;
const uint8_t request_len = 4;
@ -203,7 +203,7 @@ static bool mbs_fc01_coil_read(struct mb_rtu_context *ctx)
* Byte count 1 Bytes
* Input status N * 1 Byte
*/
static bool mbs_fc02_di_read(struct mb_rtu_context *ctx)
static bool mbs_fc02_di_read(struct modbus_context *ctx)
{
const uint16_t di_limit = 2000;
const uint8_t request_len = 4;
@ -304,7 +304,7 @@ static bool mbs_fc02_di_read(struct mb_rtu_context *ctx)
* Byte count 1 Bytes
* Register Value N * 2 Byte
*/
static bool mbs_fc03_hreg_read(struct mb_rtu_context *ctx)
static bool mbs_fc03_hreg_read(struct modbus_context *ctx)
{
const uint16_t regs_limit = 125;
const uint8_t request_len = 4;
@ -322,8 +322,8 @@ static bool mbs_fc03_hreg_read(struct mb_rtu_context *ctx)
reg_addr = sys_get_be16(&ctx->rx_frame.data[0]);
reg_qty = sys_get_be16(&ctx->rx_frame.data[2]);
if ((reg_addr < MODBUS_RTU_FP_ADDR) ||
!IS_ENABLED(CONFIG_MODBUS_RTU_FP_EXTENSIONS)) {
if ((reg_addr < MODBUS_FP_EXTENSIONS_ADDR) ||
!IS_ENABLED(CONFIG_MODBUS_FP_EXTENSIONS)) {
/* Read integer register */
if (ctx->mbs_user_cb->holding_reg_rd == NULL) {
mbs_exception_rsp(ctx, MODBUS_EXC_ILLEGAL_FC);
@ -364,7 +364,7 @@ static bool mbs_fc03_hreg_read(struct mb_rtu_context *ctx)
presp = &ctx->tx_frame.data[1];
/* Loop through each register requested. */
while (reg_qty > 0) {
if (reg_addr < MODBUS_RTU_FP_ADDR) {
if (reg_addr < MODBUS_FP_EXTENSIONS_ADDR) {
uint16_t reg;
/* Read integer register */
@ -374,7 +374,7 @@ static bool mbs_fc03_hreg_read(struct mb_rtu_context *ctx)
presp += sizeof(uint16_t);
}
} else if (IS_ENABLED(CONFIG_MODBUS_RTU_FP_EXTENSIONS)) {
} else if (IS_ENABLED(CONFIG_MODBUS_FP_EXTENSIONS)) {
float fp;
uint32_t reg;
@ -414,7 +414,7 @@ static bool mbs_fc03_hreg_read(struct mb_rtu_context *ctx)
* Byte count 1 Bytes
* Register Value N * 2 Byte
*/
static bool mbs_fc04_inreg_read(struct mb_rtu_context *ctx)
static bool mbs_fc04_inreg_read(struct modbus_context *ctx)
{
const uint16_t regs_limit = 125;
const uint8_t request_len = 4;
@ -432,8 +432,8 @@ static bool mbs_fc04_inreg_read(struct mb_rtu_context *ctx)
reg_addr = sys_get_be16(&ctx->rx_frame.data[0]);
reg_qty = sys_get_be16(&ctx->rx_frame.data[2]);
if ((reg_addr < MODBUS_RTU_FP_ADDR) ||
!IS_ENABLED(CONFIG_MODBUS_RTU_FP_EXTENSIONS)) {
if ((reg_addr < MODBUS_FP_EXTENSIONS_ADDR) ||
!IS_ENABLED(CONFIG_MODBUS_FP_EXTENSIONS)) {
/* Read integer register */
if (ctx->mbs_user_cb->input_reg_rd == NULL) {
mbs_exception_rsp(ctx, MODBUS_EXC_ILLEGAL_FC);
@ -474,7 +474,7 @@ static bool mbs_fc04_inreg_read(struct mb_rtu_context *ctx)
presp = &ctx->tx_frame.data[1];
/* Loop through each register requested. */
while (reg_qty > 0) {
if (reg_addr < MODBUS_RTU_FP_ADDR) {
if (reg_addr < MODBUS_FP_EXTENSIONS_ADDR) {
uint16_t reg;
/* Read integer register */
@ -484,7 +484,7 @@ static bool mbs_fc04_inreg_read(struct mb_rtu_context *ctx)
presp += sizeof(uint16_t);
}
} else if (IS_ENABLED(CONFIG_MODBUS_RTU_FP_EXTENSIONS)) {
} else if (IS_ENABLED(CONFIG_MODBUS_FP_EXTENSIONS)) {
float fp;
uint32_t reg;
@ -524,7 +524,7 @@ static bool mbs_fc04_inreg_read(struct mb_rtu_context *ctx)
* Output Address 2 Bytes
* Output Value 2 Bytes
*/
static bool mbs_fc05_coil_write(struct mb_rtu_context *ctx)
static bool mbs_fc05_coil_write(struct modbus_context *ctx)
{
const uint8_t request_len = 4;
const uint8_t response_len = 4;
@ -583,7 +583,7 @@ static bool mbs_fc05_coil_write(struct mb_rtu_context *ctx)
* Register Address 2 Bytes
* Register Value 2 Bytes
*/
static bool mbs_fc06_hreg_write(struct mb_rtu_context *ctx)
static bool mbs_fc06_hreg_write(struct modbus_context *ctx)
{
const uint8_t request_len = 4;
const uint8_t response_len = 4;
@ -633,8 +633,8 @@ static bool mbs_fc06_hreg_write(struct mb_rtu_context *ctx)
* Sub-function code 2 Bytes
* Data N * 2 Byte
*/
#ifdef CONFIG_MODBUS_RTU_FC08_DIAGNOSTIC
static bool mbs_fc08_diagnostics(struct mb_rtu_context *ctx)
#ifdef CONFIG_MODBUS_FC08_DIAGNOSTIC
static bool mbs_fc08_diagnostics(struct modbus_context *ctx)
{
const uint8_t request_len = 4;
const uint8_t response_len = 4;
@ -698,7 +698,7 @@ static bool mbs_fc08_diagnostics(struct mb_rtu_context *ctx)
return true;
}
#else
static bool mbs_fc08_diagnostics(struct mb_rtu_context *ctx)
static bool mbs_fc08_diagnostics(struct modbus_context *ctx)
{
mbs_exception_rsp(ctx, MODBUS_EXC_ILLEGAL_FC);
@ -721,7 +721,7 @@ static bool mbs_fc08_diagnostics(struct mb_rtu_context *ctx)
* Starting Address 2 Bytes
* Quantity of Outputs 2 Bytes
*/
static bool mbs_fc15_coils_write(struct mb_rtu_context *ctx)
static bool mbs_fc15_coils_write(struct modbus_context *ctx)
{
const uint16_t coils_limit = 2000;
const uint8_t request_len = 6;
@ -818,12 +818,12 @@ static bool mbs_fc15_coils_write(struct mb_rtu_context *ctx)
* Starting Address 2 Bytes
* Quantity of Registers 2 Bytes
*
* If the address of the request exceeds or is equal to MODBUS_RTU_FP_ADDR,
* If the address of the request exceeds or is equal to MODBUS_FP_EXTENSIONS_ADDR,
* then the function would write to multiple 'floating-point' according to
* the 'Daniels Flow Meter' extensions. This means that each register
* requested is considered as a 32-bit IEEE-754 floating-point format.
*/
static bool mbs_fc16_hregs_write(struct mb_rtu_context *ctx)
static bool mbs_fc16_hregs_write(struct modbus_context *ctx)
{
const uint16_t regs_limit = 125;
const uint8_t request_len = 6;
@ -845,8 +845,8 @@ static bool mbs_fc16_hregs_write(struct mb_rtu_context *ctx)
/* Get the byte count for the data. */
num_bytes = ctx->rx_frame.data[4];
if ((reg_addr < MODBUS_RTU_FP_ADDR) ||
!IS_ENABLED(CONFIG_MODBUS_RTU_FP_EXTENSIONS)) {
if ((reg_addr < MODBUS_FP_EXTENSIONS_ADDR) ||
!IS_ENABLED(CONFIG_MODBUS_FP_EXTENSIONS)) {
/* Write integer register */
if (ctx->mbs_user_cb->holding_reg_wr == NULL) {
mbs_exception_rsp(ctx, MODBUS_EXC_ILLEGAL_FC);
@ -895,8 +895,8 @@ static bool mbs_fc16_hregs_write(struct mb_rtu_context *ctx)
for (uint16_t reg_cntr = 0; reg_cntr < reg_qty; reg_cntr++) {
uint16_t addr = reg_addr + reg_cntr;
if ((reg_addr < MODBUS_RTU_FP_ADDR) ||
!IS_ENABLED(CONFIG_MODBUS_RTU_FP_EXTENSIONS)) {
if ((reg_addr < MODBUS_FP_EXTENSIONS_ADDR) ||
!IS_ENABLED(CONFIG_MODBUS_FP_EXTENSIONS)) {
uint16_t reg_val = sys_get_be16(prx_data);
prx_data += sizeof(uint16_t);
@ -926,7 +926,7 @@ static bool mbs_fc16_hregs_write(struct mb_rtu_context *ctx)
return true;
}
static bool mbs_fc_handler(struct mb_rtu_context *ctx)
static bool mbs_fc_handler(struct modbus_context *ctx)
{
bool send_reply = false;
uint8_t addr = ctx->rx_frame.addr;
@ -994,7 +994,7 @@ static bool mbs_fc_handler(struct mb_rtu_context *ctx)
}
}
bool mbs_rx_handler(struct mb_rtu_context *ctx)
bool mbs_rx_handler(struct modbus_context *ctx)
{
LOG_DBG("Server RX handler %p", ctx);

View file

@ -19,17 +19,17 @@
* Version 2.0 available at www.apache.org/licenses/LICENSE-2.0.
*/
#ifndef ZEPHYR_INCLUDE_MODBUS_RTU_INTERNAL_H_
#define ZEPHYR_INCLUDE_MODBUS_RTU_INTERNAL_H_
#ifndef ZEPHYR_INCLUDE_MODBUS_INTERNAL_H_
#define ZEPHYR_INCLUDE_MODBUS_INTERNAL_H_
#include <zephyr.h>
#include <drivers/gpio.h>
#include <modbus/modbus_rtu.h>
#include <modbus/modbus.h>
#ifdef CONFIG_MODBUS_RTU_FP_EXTENSIONS
#define MODBUS_RTU_FP_ADDR 5000
#ifdef CONFIG_MODBUS_FP_EXTENSIONS
#define MODBUS_FP_EXTENSIONS_ADDR 5000
#else
#define MODBUS_RTU_FP_ADDR UINT16_MAX
#define MODBUS_FP_EXTENSIONS_ADDR UINT16_MAX
#endif
#define MODBUS_RTU_MTU 256
@ -75,7 +75,7 @@ struct mb_rtu_frame {
uint16_t length;
uint8_t addr;
uint8_t fc;
uint8_t data[CONFIG_MODBUS_RTU_BUFFER_SIZE - 4];
uint8_t data[CONFIG_MODBUS_BUFFER_SIZE - 4];
uint16_t crc;
};
@ -86,9 +86,9 @@ struct mb_rtu_gpio_config {
gpio_dt_flags_t flags;
};
#define MB_RTU_STATE_CONFIGURED 0
#define MODBUS_STATE_CONFIGURED 0
struct mb_rtu_context {
struct modbus_context {
/* Interface name */
const char *iface_name;
/* UART device name */
@ -132,7 +132,7 @@ struct mb_rtu_context {
/* Records error from frame reception, e.g. CRC error */
uint16_t rx_frame_err;
#ifdef CONFIG_MODBUS_RTU_FC08_DIAGNOSTIC
#ifdef CONFIG_MODBUS_FC08_DIAGNOSTIC
uint16_t mbs_msg_ctr;
uint16_t mbs_crc_err_ctr;
uint16_t mbs_except_ctr;
@ -142,15 +142,15 @@ struct mb_rtu_context {
/* Node address */
uint8_t node_addr;
/* Storage of received characters or characters to send */
uint8_t uart_buf[CONFIG_MODBUS_RTU_BUFFER_SIZE];
uint8_t uart_buf[CONFIG_MODBUS_BUFFER_SIZE];
};
struct mb_rtu_context *mb_get_context(const uint8_t iface);
int mb_rx_frame(struct mb_rtu_context *ctx);
void mb_tx_frame(struct mb_rtu_context *ctx);
struct modbus_context *mb_get_context(const uint8_t iface);
int mb_rx_frame(struct modbus_context *ctx);
void mb_tx_frame(struct modbus_context *ctx);
bool mbs_rx_handler(struct mb_rtu_context *ctx);
void mbs_reset_statistics(struct mb_rtu_context *pch);
bool mbs_rx_handler(struct modbus_context *ctx);
void mbs_reset_statistics(struct modbus_context *pch);
#endif /* ZEPHYR_INCLUDE_MODBUS_RTU_INTERNAL_H_ */
#endif /* ZEPHYR_INCLUDE_MODBUS_INTERNAL_H_ */

View file

@ -6,6 +6,6 @@ CONFIG_ZTEST=y
CONFIG_SERIAL=y
CONFIG_UART_INTERRUPT_DRIVEN=y
CONFIG_UART_LINE_CTRL=n
CONFIG_MODBUS_RTU_ASCII_MODE=y
CONFIG_MODBUS_ASCII_MODE=y
CONFIG_MODBUS_RTU=y
CONFIG_MODBUS=y

View file

@ -9,7 +9,7 @@
#include <drivers/uart.h>
#include <ztest.h>
#include <modbus/modbus_rtu.h>
#include <modbus/modbus.h>
#define MB_TEST_BAUDRATE_LOW 9600
#define MB_TEST_BAUDRATE_HIGH 115200

View file

@ -9,7 +9,7 @@
#include <logging/log.h>
LOG_MODULE_REGISTER(mbc_test, LOG_LEVEL_INF);
#ifdef CONFIG_MODBUS_RTU_CLIENT
#ifdef CONFIG_MODBUS_CLIENT
const static uint16_t fp_offset = MB_TEST_FP_OFFSET;
const static uint8_t iface = MB_TEST_IFACE_CLIENT;
const static uint8_t node = MB_TEST_NODE_ADDR;

View file

@ -168,7 +168,7 @@ void test_server_rtu_setup_low_odd(void)
{
int err;
if (IS_ENABLED(CONFIG_MODBUS_RTU_SERVER)) {
if (IS_ENABLED(CONFIG_MODBUS_SERVER)) {
err = modbus_init_server(MB_TEST_IFACE_SERVER, MB_TEST_NODE_ADDR,
MB_TEST_BAUDRATE_LOW,
UART_CFG_PARITY_ODD,
@ -183,7 +183,7 @@ void test_server_rtu_setup_low_none(void)
{
int err;
if (IS_ENABLED(CONFIG_MODBUS_RTU_SERVER)) {
if (IS_ENABLED(CONFIG_MODBUS_SERVER)) {
err = modbus_init_server(MB_TEST_IFACE_SERVER, MB_TEST_NODE_ADDR,
MB_TEST_BAUDRATE_LOW,
UART_CFG_PARITY_NONE,
@ -198,7 +198,7 @@ void test_server_rtu_setup_high_even(void)
{
int err;
if (IS_ENABLED(CONFIG_MODBUS_RTU_SERVER)) {
if (IS_ENABLED(CONFIG_MODBUS_SERVER)) {
err = modbus_init_server(MB_TEST_IFACE_SERVER, MB_TEST_NODE_ADDR,
MB_TEST_BAUDRATE_HIGH,
UART_CFG_PARITY_EVEN,
@ -213,7 +213,7 @@ void test_server_rtu_setup_ascii(void)
{
int err;
if (IS_ENABLED(CONFIG_MODBUS_RTU_SERVER)) {
if (IS_ENABLED(CONFIG_MODBUS_SERVER)) {
err = modbus_init_server(MB_TEST_IFACE_SERVER, MB_TEST_NODE_ADDR,
MB_TEST_BAUDRATE_HIGH,
UART_CFG_PARITY_EVEN,
@ -228,7 +228,7 @@ void test_server_rtu_disable(void)
{
int err;
if (IS_ENABLED(CONFIG_MODBUS_RTU_SERVER)) {
if (IS_ENABLED(CONFIG_MODBUS_SERVER)) {
err = modbus_disable(MB_TEST_IFACE_SERVER);
zassert_equal(err, 0, "Failed to disable RTU server");
} else {