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

Add ESP_PLATFORM support for UART driver selection #766

Open
wants to merge 1 commit 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
29 changes: 29 additions & 0 deletions src/modbus-rtu.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@
#include <linux/serial.h>
#endif

#if defined(ESP_PLATFORM)
#include "driver/uart_vfs.h"
#include "driver/uart.h"
#endif


/* Table of CRC values for high-order byte */
static const uint8_t table_crc_hi[] = {
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
Expand Down Expand Up @@ -641,7 +647,11 @@ static int _modbus_rtu_connect(modbus_t *ctx)

Timeouts are ignored in canonical input mode or when the
NDELAY option is set on the file via open or fcntl */
#if defined(ESP_PLATFORM)
flags = O_RDWR;
#else
flags = O_RDWR | O_NOCTTY | O_NDELAY | O_EXCL;
#endif
#ifdef O_CLOEXEC
flags |= O_CLOEXEC;
#endif
Expand All @@ -656,6 +666,25 @@ static int _modbus_rtu_connect(modbus_t *ctx)
}
return -1;
}
#if defined(ESP_PLATFORM)
// We have a driver now installed so set up the read/write functions to use driver also.
switch(ctx_rtu->device[10]) { //extract from /dev/uart/1
case '0':
uart_vfs_dev_use_driver(UART_NUM_0);
break;
case '1':
uart_vfs_dev_use_driver(UART_NUM_1);
break;
#if SOC_UART_HP_NUM > 2
case '2':
uart_vfs_dev_use_driver(UART_NUM_2);
break;
#endif // SOC_UART_HP_NUM > 2
default:
return -1;
break;
}
#endif

/* Save */
tcgetattr(ctx->s, &ctx_rtu->old_tios);
Expand Down
3 changes: 3 additions & 0 deletions src/modbus.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ static void _sleep_response_timeout(modbus_t *ctx)
#ifdef _WIN32
/* usleep doesn't exist on Windows */
Sleep((ctx->response_timeout.tv_sec * 1000) + (ctx->response_timeout.tv_usec / 1000));
#elif (ESP_PLATFORM)
usleep((ctx->response_timeout.tv_sec * 1000) + (ctx->response_timeout.tv_usec / 1000));
#else
/* usleep source code */
struct timespec request, remaining;
Expand Down Expand Up @@ -494,6 +496,7 @@ int _modbus_receive_msg(modbus_t *ctx, uint8_t *msg, msg_type_t msg_type)
step = _STEP_META;
break;
} /* else switches straight to the next step */
// ... falls through ...
case _STEP_META:
length_to_read = compute_data_length_after_meta(ctx, msg, msg_type);
if ((msg_length + length_to_read) > ctx->backend->max_adu_length) {
Expand Down