From a3b84b22c8deb086e02cff5511e1cc4e3a9b3d1e Mon Sep 17 00:00:00 2001 From: Alexander Bessman Date: Sun, 18 Aug 2024 10:06:17 +0200 Subject: [PATCH 1/3] Fix UART passthrough in firmware 3.x --- pslab/bus/uart.py | 4 +++- pslab/protocol.py | 24 +++++++++++++----------- pslab/sciencelab.py | 34 +++++++++++++++++++++------------- 3 files changed, 37 insertions(+), 25 deletions(-) diff --git a/pslab/bus/uart.py b/pslab/bus/uart.py index e5fe53f8..340ff4ca 100644 --- a/pslab/bus/uart.py +++ b/pslab/bus/uart.py @@ -170,7 +170,9 @@ def _write_byte(self, data: int): self._device.send_byte(CP.UART_2) self._device.send_byte(CP.SEND_BYTE) self._device.send_byte(data) - self._device.get_ack() + + if self._device.firmware.major < 3: + self._device.get_ack() def _write_int(self, data: int): """Write a single int to the UART bus. diff --git a/pslab/protocol.py b/pslab/protocol.py index db1a6346..de7f258f 100644 --- a/pslab/protocol.py +++ b/pslab/protocol.py @@ -1,5 +1,6 @@ """TODO""" +import enum import struct @@ -179,16 +180,16 @@ FILL_BUFFER = Byte.pack(27) # /*---------- BAUDRATE for main comm channel----*/ -SETBAUD = Byte.pack(12) -BAUD9600 = Byte.pack(1) -BAUD14400 = Byte.pack(2) -BAUD19200 = Byte.pack(3) -BAUD28800 = Byte.pack(4) -BAUD38400 = Byte.pack(5) -BAUD57600 = Byte.pack(6) -BAUD115200 = Byte.pack(7) -BAUD230400 = Byte.pack(8) -BAUD1000000 = Byte.pack(9) +SETBAUD_LEGACY = Byte.pack(12) +BAUD9600_LEGACY = Byte.pack(1) +BAUD14400_LEGACY = Byte.pack(2) +BAUD19200_LEGACY = Byte.pack(3) +BAUD28800_LEGACY = Byte.pack(4) +BAUD38400_LEGACY = Byte.pack(5) +BAUD57600_LEGACY = Byte.pack(6) +BAUD115200_LEGACY = Byte.pack(7) +BAUD230400_LEGACY = Byte.pack(8) +BAUD1000000_LEGACY = Byte.pack(9) # /*-----------NRFL01 radio module----------*/ NRFL01 = Byte.pack(13) @@ -229,7 +230,8 @@ # --------COMMUNICATION PASSTHROUGHS-------- # Data sent to the device is directly routed to output ports such as (SCL, SDA for UART) -PASSTHROUGHS = Byte.pack(15) +PASSTHROUGHS = Byte.pack(12) +PASSTHROUGHS_LEGACY = Byte.pack(15) PASS_UART = Byte.pack(1) # /*--------STOP STREAMING------*/ diff --git a/pslab/sciencelab.py b/pslab/sciencelab.py index a70b6cdc..be622694 100644 --- a/pslab/sciencelab.py +++ b/pslab/sciencelab.py @@ -282,30 +282,38 @@ def _write_data_address(self, address: int, value: int): self.send_int(value) self.get_ack() - def enable_uart_passthrough(self, baudrate: int, persist=False): + def enable_uart_passthrough(self, baudrate: int): """Relay all data received by the device to TXD/RXD. - If a period > 0.5 seconds elapses between two transmit/receive events, - the device resets and resumes normal mode. This timeout feature has - been implemented in lieu of a hard reset option. - Can be used to load programs into secondary microcontrollers with bootloaders such ATMEGA or ESP8266 Parameters ---------- baudrate : int - Baudrate of the UART bus. - persist : bool, optional - If set to True, the device will stay in passthrough mode until the - next power cycle. Otherwise(default scenario), the device will - return to normal operation if no data is sent/received for a period - greater than one second at a time. + Baudrate of the UART2 bus. """ + if self.firmware.major < 3: + self._uart_passthrough_legacy(baudrate) + else: + self._uart_passthrough(baudrate) + + def _uart_passthrough(self, baudrate: int) -> None: self.send_byte(CP.PASSTHROUGHS) self.send_byte(CP.PASS_UART) - self.send_byte(1 if persist else 0) - self.send_int(int(round(((64e6 / baudrate) / 4) - 1))) + self.send_int(self._get_brgval(baudrate)) + self.interface.baudrate = baudrate + + def _uart_passthrough_legacy(self, baudrate: int) -> None: + self.send_byte(CP.PASSTHROUGHS_LEGACY) + self.send_byte(CP.PASS_UART) + disable_watchdog = 1 + self.send_byte(disable_watchdog) + self.send_int(self._get_brgval(baudrate)) + + @staticmethod + def _get_brgval(baudrate: int) -> int: + return int((CP.CLOCK_RATE / (4 * baudrate)) - 1) def read_log(self): """Read hardware debug log. From ccc46426d478ba891a2179a54684fbbbf0ff00c5 Mon Sep 17 00:00:00 2001 From: Marc Nause Date: Tue, 24 Sep 2024 22:54:10 +0200 Subject: [PATCH 2/3] Add link to Windows driver to readme --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 6ef29937..2df48ac4 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,8 @@ pslab-python can be installed from PyPI: **Note**: Linux users must either install a udev rule by running 'pslab install' as root, or be part of the 'dialout' group in order for pslab-python to be able to communicate with the PSLab device. +**Note**: Windows users who use the PSLab v6 device must download and install the CP210x Windows Drivers from the [Silicon Labs website](https://www.silabs.com/developers/usb-to-uart-bridge-vcp-drivers?tab=downloads). + **Note**: If you are only interested in using PSLab as an acquisition device without a display/GUI, only pslab-python needs to be installed. If you would like a GUI, install the [pslab-desktop app](https://github.com/fossasia/pslab-desktop) and follow the instructions of the Readme in that repo. From 7c0153d2f6da47fd35b4e472677037a49c7146d9 Mon Sep 17 00:00:00 2001 From: Marc Nause Date: Tue, 24 Sep 2024 23:04:11 +0200 Subject: [PATCH 3/3] Add reason why driver must be installed --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 2df48ac4..f94a1c0f 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,7 @@ pslab-python can be installed from PyPI: **Note**: Linux users must either install a udev rule by running 'pslab install' as root, or be part of the 'dialout' group in order for pslab-python to be able to communicate with the PSLab device. -**Note**: Windows users who use the PSLab v6 device must download and install the CP210x Windows Drivers from the [Silicon Labs website](https://www.silabs.com/developers/usb-to-uart-bridge-vcp-drivers?tab=downloads). +**Note**: Windows users who use the PSLab v6 device must download and install the CP210x Windows Drivers from the [Silicon Labs website](https://www.silabs.com/developers/usb-to-uart-bridge-vcp-drivers?tab=downloads) in order for pslab-python to be able to communicate with the PSLab device. **Note**: If you are only interested in using PSLab as an acquisition device without a display/GUI, only pslab-python needs to be installed. If you would like a GUI, install the [pslab-desktop app](https://github.com/fossasia/pslab-desktop) and follow the instructions of the Readme in that repo.