2021-09-22 21:30:20 -04:00
|
|
|
# SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
|
|
|
|
# SPDX-License-Identifier: Apache-2.0
|
2021-02-01 05:40:03 -05:00
|
|
|
|
2021-05-14 05:20:38 -04:00
|
|
|
import queue
|
2021-09-22 21:30:20 -04:00
|
|
|
import subprocess
|
2021-02-01 05:40:03 -05:00
|
|
|
import sys
|
|
|
|
import time
|
|
|
|
|
|
|
|
import serial
|
|
|
|
|
2021-05-14 05:20:38 -04:00
|
|
|
from .constants import CHECK_ALIVE_FLAG_TIMEOUT, MINIMAL_EN_LOW_DELAY, RECONNECT_DELAY, TAG_SERIAL
|
2021-02-01 05:40:03 -05:00
|
|
|
from .output_helpers import red_print, yellow_print
|
|
|
|
from .stoppable_thread import StoppableThread
|
|
|
|
|
|
|
|
|
2021-09-22 21:30:20 -04:00
|
|
|
class Reader(StoppableThread):
|
|
|
|
""" Output Reader base class """
|
|
|
|
|
|
|
|
|
|
|
|
class SerialReader(Reader):
|
2021-02-01 05:40:03 -05:00
|
|
|
""" Read serial data from the serial port and push to the
|
|
|
|
event queue, until stopped.
|
|
|
|
"""
|
2021-09-22 21:30:20 -04:00
|
|
|
|
2022-04-17 04:58:53 -04:00
|
|
|
def __init__(self, serial_instance, event_queue, reset):
|
|
|
|
# type: (serial.Serial, queue.Queue, bool) -> None
|
2021-02-01 05:40:03 -05:00
|
|
|
super(SerialReader, self).__init__()
|
|
|
|
self.baud = serial_instance.baudrate
|
|
|
|
self.serial = serial_instance
|
|
|
|
self.event_queue = event_queue
|
2021-04-16 06:59:50 -04:00
|
|
|
self.gdb_exit = False
|
2022-04-17 04:58:53 -04:00
|
|
|
self.reset = reset
|
2021-02-01 05:40:03 -05:00
|
|
|
if not hasattr(self.serial, 'cancel_read'):
|
|
|
|
# enable timeout for checking alive flag,
|
|
|
|
# if cancel_read not available
|
2021-05-14 05:20:38 -04:00
|
|
|
self.serial.timeout = CHECK_ALIVE_FLAG_TIMEOUT
|
2021-02-01 05:40:03 -05:00
|
|
|
|
|
|
|
def run(self):
|
|
|
|
# type: () -> None
|
|
|
|
if not self.serial.is_open:
|
|
|
|
self.serial.baudrate = self.baud
|
2021-04-16 06:59:50 -04:00
|
|
|
# We can come to this thread at startup or from external application line GDB.
|
|
|
|
# If we come from GDB we would like to continue to run without reset.
|
|
|
|
|
2021-05-14 05:20:38 -04:00
|
|
|
high = False
|
|
|
|
low = True
|
|
|
|
|
|
|
|
self.serial.dtr = low # Non reset state
|
|
|
|
self.serial.rts = high # IO0=HIGH
|
2021-07-19 08:56:43 -04:00
|
|
|
self.serial.dtr = self.serial.dtr # usbser.sys workaround
|
2021-04-16 06:59:50 -04:00
|
|
|
# Current state not reset the target!
|
2021-02-01 05:40:03 -05:00
|
|
|
self.serial.open()
|
2022-04-17 04:58:53 -04:00
|
|
|
if not self.gdb_exit and self.reset:
|
2021-05-14 05:20:38 -04:00
|
|
|
self.serial.dtr = high # Set dtr to reset state (affected by rts)
|
|
|
|
self.serial.rts = low # Set rts/dtr to the reset state
|
2021-07-19 08:56:43 -04:00
|
|
|
self.serial.dtr = self.serial.dtr # usbser.sys workaround
|
2021-05-14 05:20:38 -04:00
|
|
|
|
|
|
|
# Add a delay to meet the requirements of minimal EN low time (2ms for ESP32-C3)
|
|
|
|
time.sleep(MINIMAL_EN_LOW_DELAY)
|
2022-05-16 02:54:20 -04:00
|
|
|
elif not self.reset:
|
|
|
|
self.serial.setDTR(high) # IO0=HIGH, default state
|
2021-07-16 16:37:54 -04:00
|
|
|
self.gdb_exit = False
|
2021-05-14 05:20:38 -04:00
|
|
|
self.serial.rts = high # Set rts/dtr to the working state
|
2021-04-16 06:59:50 -04:00
|
|
|
self.serial.dtr = self.serial.dtr # usbser.sys workaround
|
2021-02-01 05:40:03 -05:00
|
|
|
try:
|
|
|
|
while self.alive:
|
|
|
|
try:
|
|
|
|
data = self.serial.read(self.serial.in_waiting or 1)
|
|
|
|
except (serial.serialutil.SerialException, IOError) as e:
|
|
|
|
data = b''
|
|
|
|
# self.serial.open() was successful before, therefore, this is an issue related to
|
2021-05-14 05:20:38 -04:00
|
|
|
# the disappearance of the device
|
2021-02-01 05:40:03 -05:00
|
|
|
red_print(e)
|
|
|
|
yellow_print('Waiting for the device to reconnect', newline='')
|
|
|
|
self.serial.close()
|
|
|
|
while self.alive: # so that exiting monitor works while waiting
|
|
|
|
try:
|
2021-05-14 05:20:38 -04:00
|
|
|
time.sleep(RECONNECT_DELAY)
|
2022-05-19 02:18:11 -04:00
|
|
|
if not self.reset:
|
|
|
|
self.serial.dtr = low # Non reset state
|
|
|
|
self.serial.rts = high # IO0=HIGH
|
|
|
|
self.serial.dtr = self.serial.dtr # usbser.sys workaround
|
2021-02-01 05:40:03 -05:00
|
|
|
self.serial.open()
|
|
|
|
break # device connected
|
|
|
|
except serial.serialutil.SerialException:
|
|
|
|
yellow_print('.', newline='')
|
|
|
|
sys.stderr.flush()
|
|
|
|
yellow_print('') # go to new line
|
|
|
|
if data:
|
|
|
|
self.event_queue.put((TAG_SERIAL, data), False)
|
|
|
|
finally:
|
|
|
|
self.serial.close()
|
|
|
|
|
|
|
|
def _cancel(self):
|
|
|
|
# type: () -> None
|
|
|
|
if hasattr(self.serial, 'cancel_read'):
|
|
|
|
try:
|
|
|
|
self.serial.cancel_read()
|
|
|
|
except Exception: # noqa
|
|
|
|
pass
|
2021-09-22 21:30:20 -04:00
|
|
|
|
|
|
|
|
|
|
|
class LinuxReader(Reader):
|
|
|
|
""" Read data from the subprocess that runs runnable and push to the
|
|
|
|
event queue, until stopped.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, process, event_queue):
|
|
|
|
# type: (subprocess.Popen, queue.Queue) -> None
|
|
|
|
super().__init__()
|
|
|
|
self.proc = process
|
|
|
|
self.event_queue = event_queue
|
|
|
|
|
|
|
|
self._stdout = iter(self.proc.stdout.readline, b'') # type: ignore
|
|
|
|
|
|
|
|
def run(self): # type: () -> None
|
|
|
|
try:
|
|
|
|
while self.alive:
|
|
|
|
for line in self._stdout:
|
|
|
|
if line:
|
|
|
|
self.event_queue.put((TAG_SERIAL, line), False)
|
|
|
|
finally:
|
|
|
|
self.proc.terminate()
|
|
|
|
|
|
|
|
def _cancel(self): # type: () -> None
|
|
|
|
self.proc.terminate()
|