Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 | /*
* Copyright (c) 2020 PHYTEC Messtechnik GmbH
* Copyright (c) 2021 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: Apache-2.0
*/
/*
* This file is based on mb.c and mb_util.c from uC/Modbus Stack.
*
* uC/Modbus
* The Embedded Modbus Stack
*
* Copyright 2003-2020 Silicon Laboratories Inc. www.silabs.com
*
* SPDX-License-Identifier: APACHE-2.0
*
* This software is subject to an open source license and is distributed by
* Silicon Laboratories Inc. pursuant to the terms of the Apache License,
* Version 2.0 available at www.apache.org/licenses/LICENSE-2.0.
*/
#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(modbus_serial, CONFIG_MODBUS_LOG_LEVEL);
#include <zephyr/kernel.h>
#include <string.h>
#include <zephyr/sys/byteorder.h>
#include <zephyr/sys/crc.h>
#include <modbus_internal.h>
static void modbus_serial_tx_on(struct modbus_context *ctx)
{
struct modbus_serial_config *cfg = ctx->cfg;
if (cfg->de != NULL) {
gpio_pin_set(cfg->de->port, cfg->de->pin, 1);
}
uart_irq_tx_enable(cfg->dev);
}
static void modbus_serial_tx_off(struct modbus_context *ctx)
{
struct modbus_serial_config *cfg = ctx->cfg;
uart_irq_tx_disable(cfg->dev);
if (cfg->de != NULL) {
gpio_pin_set(cfg->de->port, cfg->de->pin, 0);
}
}
static void modbus_serial_rx_on(struct modbus_context *ctx)
{
struct modbus_serial_config *cfg = ctx->cfg;
if (cfg->re != NULL) {
gpio_pin_set(cfg->re->port, cfg->re->pin, 1);
}
uart_irq_rx_enable(cfg->dev);
}
static void modbus_serial_rx_off(struct modbus_context *ctx)
{
struct modbus_serial_config *cfg = ctx->cfg;
uart_irq_rx_disable(cfg->dev);
if (cfg->re != NULL) {
gpio_pin_set(cfg->re->port, cfg->re->pin, 0);
}
}
#ifdef CONFIG_MODBUS_ASCII_MODE
/* The function calculates an 8-bit Longitudinal Redundancy Check. */
static uint8_t modbus_ascii_get_lrc(uint8_t *src, size_t length)
{
uint8_t lrc = 0;
uint8_t tmp;
uint8_t *pblock = src;
while (length-- > 0) {
/* Add the data byte to LRC, increment data pointer. */
if (hex2bin(pblock, 2, &tmp, sizeof(tmp)) != sizeof(tmp)) {
return 0;
}
lrc += tmp;
pblock += 2;
}
/* Two complement the binary sum */
lrc = ~lrc + 1;
return lrc;
}
/* Parses and converts an ASCII mode frame into a Modbus RTU frame. */
static int modbus_ascii_rx_adu(struct modbus_context *ctx)
{
struct modbus_serial_config *cfg = ctx->cfg;
uint8_t *pmsg;
uint8_t *prx_data;
uint16_t rx_size;
uint8_t frame_lrc;
uint8_t calc_lrc;
rx_size = cfg->uart_buf_ctr;
prx_data = &ctx->rx_adu.data[0];
if (!(rx_size & 0x01)) {
LOG_WRN("Message should have an odd number of bytes");
return -EMSGSIZE;
}
if (rx_size < MODBUS_ASCII_MIN_MSG_SIZE) {
LOG_WRN("Frame length error");
return -EMSGSIZE;
}
if ((cfg->uart_buf[0] != MODBUS_ASCII_START_FRAME_CHAR) ||
(cfg->uart_buf[rx_size - 2] != MODBUS_ASCII_END_FRAME_CHAR1) ||
(cfg->uart_buf[rx_size - 1] != MODBUS_ASCII_END_FRAME_CHAR2)) {
LOG_WRN("Frame character error");
return -EMSGSIZE;
}
/* Take away for the ':', CR, and LF */
rx_size -= 3;
/* Point past the ':' to the address. */
pmsg = &cfg->uart_buf[1];
hex2bin(pmsg, 2, &ctx->rx_adu.unit_id, 1);
pmsg += 2;
rx_size -= 2;
hex2bin(pmsg, 2, &ctx->rx_adu.fc, 1);
pmsg += 2;
rx_size -= 2;
/* Get the data from the message */
ctx->rx_adu.length = 0;
while (rx_size > 2) {
hex2bin(pmsg, 2, prx_data, 1);
prx_data++;
pmsg += 2;
rx_size -= 2;
/* Increment the number of Modbus packets received */
ctx->rx_adu.length++;
}
/* Extract the message's LRC */
hex2bin(pmsg, 2, &frame_lrc, 1);
ctx->rx_adu.crc = frame_lrc;
/*
* The LRC is calculated on the ADDR, FC and Data fields,
* not the ':', CR/LF and LRC placed in the message
* by the sender. We thus need to subtract 5 'ASCII' characters
* from the received message to exclude these.
*/
calc_lrc = modbus_ascii_get_lrc(&cfg->uart_buf[1],
(cfg->uart_buf_ctr - 5) / 2);
if (calc_lrc != frame_lrc) {
LOG_ERR("Calculated LRC does not match received LRC");
return -EIO;
}
return 0;
}
static uint8_t *modbus_ascii_bin2hex(uint8_t value, uint8_t *pbuf)
{
uint8_t u_nibble = (value >> 4) & 0x0F;
uint8_t l_nibble = value & 0x0F;
hex2char(u_nibble, pbuf);
pbuf++;
hex2char(l_nibble, pbuf);
pbuf++;
return pbuf;
}
static void modbus_ascii_tx_adu(struct modbus_context *ctx)
{
struct modbus_serial_config *cfg = ctx->cfg;
uint16_t tx_bytes = 0;
uint8_t lrc;
uint8_t *pbuf;
/* Place the start-of-frame character into output buffer */
cfg->uart_buf[0] = MODBUS_ASCII_START_FRAME_CHAR;
tx_bytes = 1;
pbuf = &cfg->uart_buf[1];
pbuf = modbus_ascii_bin2hex(ctx->tx_adu.unit_id, pbuf);
tx_bytes += 2;
pbuf = modbus_ascii_bin2hex(ctx->tx_adu.fc, pbuf);
tx_bytes += 2;
for (int i = 0; i < ctx->tx_adu.length; i++) {
pbuf = modbus_ascii_bin2hex(ctx->tx_adu.data[i], pbuf);
tx_bytes += 2;
}
/*
* Add the LRC checksum in the packet.
*
* The LRC is calculated on the ADDR, FC and Data fields,
* not the ':' which was inserted in the uart_buf[].
* Thus we subtract 1 ASCII character from the LRC.
* The LRC and CR/LF bytes are not YET in the .uart_buf[].
*/
lrc = modbus_ascii_get_lrc(&cfg->uart_buf[1], (tx_bytes - 1) / 2);
pbuf = modbus_ascii_bin2hex(lrc, pbuf);
tx_bytes += 2;
*pbuf++ = MODBUS_ASCII_END_FRAME_CHAR1;
*pbuf++ = MODBUS_ASCII_END_FRAME_CHAR2;
tx_bytes += 2;
/* Update the total number of bytes to send */
cfg->uart_buf_ctr = tx_bytes;
cfg->uart_buf_ptr = &cfg->uart_buf[0];
LOG_DBG("Start frame transmission");
modbus_serial_rx_off(ctx);
modbus_serial_tx_on(ctx);
}
#else
static int modbus_ascii_rx_adu(struct modbus_context *ctx)
{
return 0;
}
static void modbus_ascii_tx_adu(struct modbus_context *ctx)
{
}
#endif
/* Copy Modbus RTU frame and check if the CRC is valid. */
static int modbus_rtu_rx_adu(struct modbus_context *ctx)
{
struct modbus_serial_config *cfg = ctx->cfg;
uint16_t calc_crc;
uint16_t crc_idx;
uint8_t *data_ptr;
/* Is the message long enough? */
if ((cfg->uart_buf_ctr < MODBUS_RTU_MIN_MSG_SIZE) ||
(cfg->uart_buf_ctr > CONFIG_MODBUS_BUFFER_SIZE)) {
LOG_WRN("Frame length error");
return -EMSGSIZE;
}
ctx->rx_adu.unit_id = cfg->uart_buf[0];
ctx->rx_adu.fc = cfg->uart_buf[1];
data_ptr = &cfg->uart_buf[2];
/* Payload length without node address, function code, and CRC */
ctx->rx_adu.length = cfg->uart_buf_ctr - 4;
/* CRC index */
crc_idx = cfg->uart_buf_ctr - sizeof(uint16_t);
memcpy(ctx->rx_adu.data, data_ptr, ctx->rx_adu.length);
ctx->rx_adu.crc = sys_get_le16(&cfg->uart_buf[crc_idx]);
/* Calculate CRC over address, function code, and payload */
calc_crc = crc16_ansi(&cfg->uart_buf[0],
cfg->uart_buf_ctr - sizeof(ctx->rx_adu.crc));
if (ctx->rx_adu.crc != calc_crc) {
LOG_WRN("Calculated CRC does not match received CRC");
return -EIO;
}
return 0;
}
static void rtu_tx_adu(struct modbus_context *ctx)
{
struct modbus_serial_config *cfg = ctx->cfg;
uint16_t tx_bytes = 0;
uint8_t *data_ptr;
cfg->uart_buf[0] = ctx->tx_adu.unit_id;
cfg->uart_buf[1] = ctx->tx_adu.fc;
tx_bytes = 2 + ctx->tx_adu.length;
data_ptr = &cfg->uart_buf[2];
memcpy(data_ptr, ctx->tx_adu.data, ctx->tx_adu.length);
ctx->tx_adu.crc = crc16_ansi(&cfg->uart_buf[0], ctx->tx_adu.length + 2);
sys_put_le16(ctx->tx_adu.crc,
&cfg->uart_buf[ctx->tx_adu.length + 2]);
tx_bytes += 2;
cfg->uart_buf_ctr = tx_bytes;
cfg->uart_buf_ptr = &cfg->uart_buf[0];
LOG_HEXDUMP_DBG(cfg->uart_buf, cfg->uart_buf_ctr, "uart_buf");
LOG_DBG("Start frame transmission");
modbus_serial_rx_off(ctx);
modbus_serial_tx_on(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 cb_handler_rx(struct modbus_context *ctx)
{
struct modbus_serial_config *cfg = ctx->cfg;
if ((ctx->mode == MODBUS_MODE_ASCII) &&
IS_ENABLED(CONFIG_MODBUS_ASCII_MODE)) {
uint8_t c;
if (uart_fifo_read(cfg->dev, &c, 1) != 1) {
LOG_ERR("Failed to read UART");
return;
}
if (c == MODBUS_ASCII_START_FRAME_CHAR) {
/* Restart a new frame */
cfg->uart_buf_ptr = &cfg->uart_buf[0];
cfg->uart_buf_ctr = 0;
}
if (cfg->uart_buf_ctr < CONFIG_MODBUS_BUFFER_SIZE) {
*cfg->uart_buf_ptr++ = c;
cfg->uart_buf_ctr++;
}
if (c == MODBUS_ASCII_END_FRAME_CHAR2) {
k_work_submit(&ctx->server_work);
}
} else {
int n;
/* Restart timer on a new character */
k_timer_start(&cfg->rtu_timer,
K_USEC(cfg->rtu_timeout), K_NO_WAIT);
n = uart_fifo_read(cfg->dev, cfg->uart_buf_ptr,
(CONFIG_MODBUS_BUFFER_SIZE -
cfg->uart_buf_ctr));
cfg->uart_buf_ptr += n;
cfg->uart_buf_ctr += n;
}
}
static void cb_handler_tx(struct modbus_context *ctx)
{
struct modbus_serial_config *cfg = ctx->cfg;
int n;
if (cfg->uart_buf_ctr > 0) {
n = uart_fifo_fill(cfg->dev, cfg->uart_buf_ptr,
cfg->uart_buf_ctr);
cfg->uart_buf_ctr -= n;
cfg->uart_buf_ptr += n;
return;
}
/* Must wait till the transmission is complete or
* RS-485 transceiver could be disabled before all data has
* been transmitted and message will be corrupted.
*/
if (uart_irq_tx_complete(cfg->dev)) {
/* Disable transmission */
cfg->uart_buf_ptr = &cfg->uart_buf[0];
modbus_serial_tx_off(ctx);
modbus_serial_rx_on(ctx);
}
}
static void uart_cb_handler(const struct device *dev, void *app_data)
{
struct modbus_context *ctx = (struct modbus_context *)app_data;
struct modbus_serial_config *cfg;
if (ctx == NULL) {
LOG_ERR("Modbus hardware is not properly initialized");
return;
}
cfg = ctx->cfg;
if (uart_irq_update(cfg->dev) && uart_irq_is_pending(cfg->dev)) {
if (uart_irq_rx_ready(cfg->dev)) {
cb_handler_rx(ctx);
}
if (uart_irq_tx_ready(cfg->dev)) {
cb_handler_tx(ctx);
}
}
}
/* This function is called when the RTU framing timer expires. */
static void rtu_tmr_handler(struct k_timer *t_id)
{
struct modbus_context *ctx;
ctx = (struct modbus_context *)k_timer_user_data_get(t_id);
if (ctx == NULL) {
LOG_ERR("Failed to get Modbus context");
return;
}
k_work_submit(&ctx->server_work);
}
static int configure_gpio(struct modbus_context *ctx)
{
struct modbus_serial_config *cfg = ctx->cfg;
if (cfg->de != NULL) {
if (!device_is_ready(cfg->de->port)) {
return -ENODEV;
}
if (gpio_pin_configure_dt(cfg->de, GPIO_OUTPUT_INACTIVE)) {
return -EIO;
}
}
if (cfg->re != NULL) {
if (!device_is_ready(cfg->re->port)) {
return -ENODEV;
}
if (gpio_pin_configure_dt(cfg->re, GPIO_OUTPUT_INACTIVE)) {
return -EIO;
}
}
return 0;
}
void modbus_serial_rx_disable(struct modbus_context *ctx)
{
modbus_serial_rx_off(ctx);
}
void modbus_serial_rx_enable(struct modbus_context *ctx)
{
modbus_serial_rx_on(ctx);
}
int modbus_serial_rx_adu(struct modbus_context *ctx)
{
struct modbus_serial_config *cfg = ctx->cfg;
int rc = 0;
switch (ctx->mode) {
case MODBUS_MODE_RTU:
rc = modbus_rtu_rx_adu(ctx);
break;
case MODBUS_MODE_ASCII:
if (!IS_ENABLED(CONFIG_MODBUS_ASCII_MODE)) {
return -ENOTSUP;
}
rc = modbus_ascii_rx_adu(ctx);
break;
default:
LOG_ERR("Unsupported MODBUS mode");
return -ENOTSUP;
}
cfg->uart_buf_ctr = 0;
cfg->uart_buf_ptr = &cfg->uart_buf[0];
return rc;
}
int modbus_serial_tx_adu(struct modbus_context *ctx)
{
switch (ctx->mode) {
case MODBUS_MODE_RTU:
rtu_tx_adu(ctx);
return 0;
case MODBUS_MODE_ASCII:
if (IS_ENABLED(CONFIG_MODBUS_ASCII_MODE)) {
modbus_ascii_tx_adu(ctx);
return 0;
}
default:
break;
}
return -ENOTSUP;
}
int modbus_serial_init(struct modbus_context *ctx,
struct modbus_iface_param param)
{
struct modbus_serial_config *cfg = ctx->cfg;
const uint32_t if_delay_max = 3500000;
const uint32_t numof_bits = 11;
struct uart_config uart_cfg;
switch (param.mode) {
case MODBUS_MODE_RTU:
case MODBUS_MODE_ASCII:
ctx->mode = param.mode;
break;
default:
return -ENOTSUP;
}
if (!device_is_ready(cfg->dev)) {
LOG_ERR("Bus device %s is not ready", cfg->dev->name);
return -ENODEV;
}
uart_cfg.baudrate = param.serial.baud,
uart_cfg.flow_ctrl = UART_CFG_FLOW_CTRL_NONE;
if (ctx->mode == MODBUS_MODE_ASCII) {
uart_cfg.data_bits = UART_CFG_DATA_BITS_7;
} else {
uart_cfg.data_bits = UART_CFG_DATA_BITS_8;
}
switch (param.serial.parity) {
case UART_CFG_PARITY_ODD:
case UART_CFG_PARITY_EVEN:
uart_cfg.parity = param.serial.parity;
uart_cfg.stop_bits = UART_CFG_STOP_BITS_1;
break;
case UART_CFG_PARITY_NONE:
/* Use of no parity requires 2 stop bits */
uart_cfg.parity = param.serial.parity;
uart_cfg.stop_bits = UART_CFG_STOP_BITS_2;
break;
default:
return -EINVAL;
}
if (ctx->client) {
/* Allow custom stop bit settings only in client mode */
switch (param.serial.stop_bits_client) {
case UART_CFG_STOP_BITS_0_5:
case UART_CFG_STOP_BITS_1:
case UART_CFG_STOP_BITS_1_5:
case UART_CFG_STOP_BITS_2:
uart_cfg.stop_bits = param.serial.stop_bits_client;
break;
default:
return -EINVAL;
}
}
if (uart_configure(cfg->dev, &uart_cfg) != 0) {
LOG_ERR("Failed to configure UART");
return -EINVAL;
}
if (param.serial.baud <= 38400) {
cfg->rtu_timeout = (numof_bits * if_delay_max) /
param.serial.baud;
} else {
cfg->rtu_timeout = (numof_bits * if_delay_max) / 38400;
}
if (configure_gpio(ctx) != 0) {
return -EIO;
}
cfg->uart_buf_ctr = 0;
cfg->uart_buf_ptr = &cfg->uart_buf[0];
uart_irq_callback_user_data_set(cfg->dev, uart_cb_handler, ctx);
k_timer_init(&cfg->rtu_timer, rtu_tmr_handler, NULL);
k_timer_user_data_set(&cfg->rtu_timer, ctx);
modbus_serial_rx_on(ctx);
LOG_INF("RTU timeout %u us", cfg->rtu_timeout);
return 0;
}
void modbus_serial_disable(struct modbus_context *ctx)
{
modbus_serial_tx_off(ctx);
modbus_serial_rx_off(ctx);
k_timer_stop(&ctx->cfg->rtu_timer);
}
|