From 0bac148e0a8a87e2336faa4d5681f354bb9590cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franz=20H=C3=B6pfinger?= Date: Sun, 18 Aug 2024 22:53:39 +0200 Subject: [PATCH] Add ESP_PLATFORM support for UART driver selection - Add conditional compilation for ESP_PLATFORM to include UART driver headers - Modify connection flags based on platform - Implement UART driver selection based on device number --- src/modbus-rtu.c | 29 +++++++++++++++++++++++++++++ src/modbus.c | 3 +++ 2 files changed, 32 insertions(+) diff --git a/src/modbus-rtu.c b/src/modbus-rtu.c index b7749230c..e969aab10 100644 --- a/src/modbus-rtu.c +++ b/src/modbus-rtu.c @@ -26,6 +26,12 @@ #include #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, @@ -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 @@ -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); diff --git a/src/modbus.c b/src/modbus.c index 5a9b86783..1e8b5996c 100644 --- a/src/modbus.c +++ b/src/modbus.c @@ -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; @@ -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) {