From 74fa8a3907359a7c7e9159601b8a8c2c6b79e960 Mon Sep 17 00:00:00 2001 From: Kevin O'Connor Date: Fri, 9 Dec 2016 19:04:30 -0500 Subject: [PATCH] serialhdl: Clear "hupcl" bit from serial port The arduino style serial port interfaces can reset the MCU when the serial port is opened. Clearing the HUPCL flag makes this less likely. Signed-off-by: Kevin O'Connor --- klippy/serialhdl.py | 3 ++- klippy/util.py | 6 ++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/klippy/serialhdl.py b/klippy/serialhdl.py index 7120c43c..2a539cf1 100644 --- a/klippy/serialhdl.py +++ b/klippy/serialhdl.py @@ -6,7 +6,7 @@ import time, logging, threading import serial -import msgproto, chelper +import msgproto, chelper, util class SerialReader: BITS_PER_BYTE = 10. @@ -298,6 +298,7 @@ class SerialBootStrap: # Attempt to place an AVR stk500v2 style programmer into normal mode def stk500v2_leave(ser, reactor): logging.debug("Starting stk500v2 leave programmer sequence") + util.clear_hupcl(ser.fileno()) origbaud = ser.baudrate # Request a dummy speed first as this seems to help reset the port ser.baudrate = 2400 diff --git a/klippy/util.py b/klippy/util.py index caac827e..6bbfd9af 100644 --- a/klippy/util.py +++ b/klippy/util.py @@ -16,6 +16,12 @@ def set_nonblock(fd): fcntl.fcntl(fd, fcntl.F_SETFL , fcntl.fcntl(fd, fcntl.F_GETFL) | os.O_NONBLOCK) +# Clear HUPCL flag +def clear_hupcl(fd): + attrs = termios.tcgetattr(fd) + attrs[2] = attrs[2] & ~termios.HUPCL + termios.tcsetattr(fd, termios.TCSADRAIN, attrs) + # Support for creating a pseudo-tty for emulating a serial port def create_pty(ptyname): mfd, sfd = pty.openpty()