serial_reader.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. # Copyright 2015-2021 Espressif Systems (Shanghai) CO LTD
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import sys
  15. import time
  16. import serial
  17. from .constants import TAG_SERIAL
  18. from .output_helpers import red_print, yellow_print
  19. from .stoppable_thread import StoppableThread
  20. try:
  21. import queue
  22. except ImportError:
  23. import Queue as queue # type: ignore # noqa
  24. class SerialReader(StoppableThread):
  25. """ Read serial data from the serial port and push to the
  26. event queue, until stopped.
  27. """
  28. def __init__(self, serial_instance, event_queue):
  29. # type: (serial.Serial, queue.Queue) -> None
  30. super(SerialReader, self).__init__()
  31. self.baud = serial_instance.baudrate
  32. self.serial = serial_instance
  33. self.event_queue = event_queue
  34. self.gdb_exit = False
  35. if not hasattr(self.serial, 'cancel_read'):
  36. # enable timeout for checking alive flag,
  37. # if cancel_read not available
  38. self.serial.timeout = 0.25
  39. def run(self):
  40. # type: () -> None
  41. if not self.serial.is_open:
  42. self.serial.baudrate = self.baud
  43. # We can come to this thread at startup or from external application line GDB.
  44. # If we come from GDB we would like to continue to run without reset.
  45. if self.gdb_exit:
  46. self.serial.rts = False
  47. self.serial.dtr = True
  48. else: # if we exit from GDB, we don't need to reset the target
  49. # This sequence of DTR/RTS and open/close set the serial port to
  50. # condition when GDB not make reset of the target by switching DTR/RTS.
  51. self.serial.rts = True # IO0=LOW
  52. self.serial.dtr = self.serial.dtr # usbser.sys workaround
  53. self.serial.rts = False # IO0=HIGH
  54. self.serial.dtr = False
  55. # Current state not reset the target!
  56. self.gdb_exit = False
  57. self.serial.open()
  58. time.sleep(0.005) # Add a delay to meet the requirements of minimal EN low time (2ms for ESP32-C3)
  59. self.serial.rts = False # Set rts/dtr to the working state
  60. self.serial.dtr = self.serial.dtr # usbser.sys workaround
  61. try:
  62. while self.alive:
  63. try:
  64. data = self.serial.read(self.serial.in_waiting or 1)
  65. except (serial.serialutil.SerialException, IOError) as e:
  66. data = b''
  67. # self.serial.open() was successful before, therefore, this is an issue related to
  68. # the disapperence of the device
  69. red_print(e)
  70. yellow_print('Waiting for the device to reconnect', newline='')
  71. self.serial.close()
  72. while self.alive: # so that exiting monitor works while waiting
  73. try:
  74. time.sleep(0.5)
  75. self.serial.open()
  76. break # device connected
  77. except serial.serialutil.SerialException:
  78. yellow_print('.', newline='')
  79. sys.stderr.flush()
  80. yellow_print('') # go to new line
  81. if data:
  82. self.event_queue.put((TAG_SERIAL, data), False)
  83. finally:
  84. self.serial.close()
  85. def _cancel(self):
  86. # type: () -> None
  87. if hasattr(self.serial, 'cancel_read'):
  88. try:
  89. self.serial.cancel_read()
  90. except Exception: # noqa
  91. pass