Skip to content

Add RFC2217 support to serialupdi #36

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

Open
wants to merge 1 commit into
base: main
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ pymcuprog supports:
* EDBG - on-board debugger on Xplained Pro/Ultra
* mEDBG - on-board debugger on Xplained Mini/Nano
* JTAGICE3 (firmware version 3.0 or newer)
* Serial (TTY/COM) and RFC2217 compatible devices (serialupdi)

Although not all functionality is provided on all debuggers/boards. See device support section below.

Expand Down
19 changes: 17 additions & 2 deletions pymcuprog/serialupdi/physical.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import time
from logging import getLogger
import serial
import serial.rfc2217
from serial.serialutil import SerialException

from . import constants
Expand Down Expand Up @@ -48,6 +49,20 @@ def __init__(self, port, baud=DEFAULT_SERIALUPDI_BAUD, timeout=None):
# send an initial break as handshake
self.send([constants.UPDI_BREAK])

def _open_serial(self, port, *args, **kwargs):
"""
Open a serial device (tty/com or rfc2217) based on the provided port type

:param port: Serial port name to connect to.
:param \**args: Additional arguments passed to Serial class.
:param \**args: Additional keyword arguments passed to Serial class.
"""

if port.startswith("rfc2217://"):
return serial.rfc2217.Serial(port, *args, **kwargs)
else:
return serial.Serial(port, *args, **kwargs)

def initialise_serial(self, port, baud, timeout):
"""
Standard serial port initialisation
Expand All @@ -62,7 +77,7 @@ def initialise_serial(self, port, baud, timeout):
"""
self.logger.info("Opening port '%s' at %d baud (timeout %.01fs)", port, baud, timeout)
try:
self.ser = serial.Serial(port, baud, parity=serial.PARITY_EVEN, timeout=timeout, stopbits=serial.STOPBITS_TWO)
self.ser = self._open_serial(port, baud, parity=serial.PARITY_EVEN, timeout=timeout, stopbits=serial.STOPBITS_TWO)
except SerialException:
self.logger.error("Unable to open serial port '%s'", port)
raise
Expand Down Expand Up @@ -90,7 +105,7 @@ def send_double_break(self):
# At 300 bauds, the break character will pull the line low for 30ms
# Which is slightly above the recommended 24.6ms
self.ser.close()
temporary_serial = serial.Serial(self.port, 300, parity=serial.PARITY_EVEN, timeout=self.timeout,
temporary_serial = self._open_serial(self.port, 300, parity=serial.PARITY_EVEN, timeout=self.timeout,
stopbits=serial.STOPBITS_ONE)

# Send two break characters, with 1 stop bit in between
Expand Down