From 2b58f5b122110b34c82282bd3cc2060c8719cd21 Mon Sep 17 00:00:00 2001 From: Sergey Nazaryev <136235212+nazaryev-cool@users.noreply.github.com> Date: Mon, 14 Oct 2024 15:56:55 +0300 Subject: [PATCH] Fix the MSTP turnaround delay implementation. (#809) The usleep() function takes microseconds, not milliseconds. So, T_turnaround needs to be calculated in microseconds. If we don't do this, at a baud rate of 9600, we end up waiting 4 microseconds instead of 4 milliseconds, which goes against the rule. --- ports/bsd/rs485.c | 6 +++--- ports/linux/rs485.c | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/ports/bsd/rs485.c b/ports/bsd/rs485.c index f7f04e67..839f56a0 100644 --- a/ports/bsd/rs485.c +++ b/ports/bsd/rs485.c @@ -399,7 +399,7 @@ void RS485_Send_Frame( const uint8_t *buffer, /* frame to send (up to 501 bytes of data) */ uint16_t nbytes) { /* number of bytes of data (up to 501) */ - uint32_t turnaround_time = Tturnaround * 1000; + uint32_t turnaround_time_usec = Tturnaround * 1000000UL; uint32_t baud; ssize_t written = 0; int greska; @@ -412,7 +412,7 @@ void RS485_Send_Frame( baud = RS485_Get_Baud_Rate(); /* sleeping for turnaround time is necessary to give other devices time to change from sending to receiving state. */ - usleep(turnaround_time / baud); + usleep(turnaround_time_usec / baud); /* On success, the number of bytes written are returned (zero indicates nothing was written). On error, -1 is returned, and @@ -438,7 +438,7 @@ void RS485_Send_Frame( baud = RS485_Get_Port_Baud_Rate(mstp_port); /* sleeping for turnaround time is necessary to give other devices time to change from sending to receiving state. */ - usleep(turnaround_time / baud); + usleep(turnaround_time_usec / baud); /* On success, the number of bytes written are returned (zero indicates nothing was written). On error, -1 is returned, and diff --git a/ports/linux/rs485.c b/ports/linux/rs485.c index 4032c746..acd04b41 100644 --- a/ports/linux/rs485.c +++ b/ports/linux/rs485.c @@ -358,7 +358,7 @@ void RS485_Send_Frame( const uint8_t *buffer, /* frame to send (up to 501 bytes of data) */ uint16_t nbytes) { /* number of bytes of data (up to 501) */ - uint32_t turnaround_time = Tturnaround * 1000; + uint32_t turnaround_time_usec = Tturnaround * 1000000UL; uint32_t baud; ssize_t written = 0; int greska; @@ -371,7 +371,7 @@ void RS485_Send_Frame( baud = RS485_Get_Baud_Rate(); /* sleeping for turnaround time is necessary to give other devices time to change from sending to receiving state. */ - usleep(turnaround_time / baud); + usleep(turnaround_time_usec / baud); /* On success, the number of bytes written are returned (zero indicates nothing was written). On error, -1 is returned, and @@ -397,7 +397,7 @@ void RS485_Send_Frame( baud = RS485_Get_Port_Baud_Rate(mstp_port); /* sleeping for turnaround time is necessary to give other devices time to change from sending to receiving state. */ - usleep(turnaround_time / baud); + usleep(turnaround_time_usec / baud); /* On success, the number of bytes written are returned (zero indicates nothing was written). On error, -1 is returned, and