Skip to content

Commit c025b3f

Browse files
author
JTrantow
committed
Back to original voodoo caused by conflating modbus timing calculation with pktuart hardware implementation.
Note: (175u * baudrate + 99999u) / 100000u; could be implemented as (7u * baudrate + 3999(/4000; which would improve the calculation range but doesn't matter because already screening out large baud rates.
1 parent c28ccaa commit c025b3f

1 file changed

Lines changed: 5 additions & 9 deletions

File tree

src/hal/drivers/mesa-hostmot2/hm2_modbus.c

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -374,24 +374,20 @@ static void setup_icdelay(hm2_modbus_inst_t *inst, unsigned baudrate, unsigned p
374374
inst->hal->icdelay = inst->maxicharbits;
375375
}
376376

377-
#define HARDWARE_MAX_DELAY (1020) //!< Maximum ifdelay in number of bits limited by hardware.
378-
#define HARDWARE_BAUD_LIMIT ((HARDWARE_MAX_DELAY*100000-99999)/175) //!< Baud that limits out the hardware ifdelay.
379377
//
380-
// Calculate the inter-frame delay time in units of numbers of bits.
381-
// Returns:
382-
// - HARDWARE_MAX_DELAY bits if baud > HARDWARE_BAUD_LIMIT
378+
// Calculate the inter-frame delay time:
383379
// - 3.5 chars if baudrate <= 19200
384-
// - 1750 microseconds worth of bits when baudrate > 19200 and <= HARDWARE_BAUD_LIMIT
380+
// - 1750 microseconds if baudrate > 19200
385381
//
386382
static unsigned calc_ifdelay(hm2_modbus_inst_t *inst, unsigned baudrate, unsigned parity, unsigned stopbits)
387383
{
388-
if(baudrate > HARDWARE_BAUD_LIMIT) {
384+
if(baudrate > 582000) {
389385
MSG_WARN("%s: warning: Baudrate > 582000 will make inter-frame timer overflow. Setting to maximum.\n", inst->name);
390-
return HARDWARE_MAX_DELAY;
386+
return 1020;
391387
}
392388

393389
// calculation works for baudrates less than ~24 Mbit/s
394-
if(baudrate > 19200)
390+
if(19200 < baudrate)
395391
return (175u * baudrate + 99999u) / 100000u;
396392
unsigned bits = 1 + 8 + (parity ? 1 : 0) + (stopbits > 1 ? 2 : 1);
397393
return (bits * 35 + 9) / 10; // Ceil of bits in 3.5 characters.

0 commit comments

Comments
 (0)