Skip to content

Commit a3b84b2

Browse files
committed
Fix UART passthrough in firmware 3.x
1 parent 3742998 commit a3b84b2

File tree

3 files changed

+37
-25
lines changed

3 files changed

+37
-25
lines changed

pslab/bus/uart.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,9 @@ def _write_byte(self, data: int):
170170
self._device.send_byte(CP.UART_2)
171171
self._device.send_byte(CP.SEND_BYTE)
172172
self._device.send_byte(data)
173-
self._device.get_ack()
173+
174+
if self._device.firmware.major < 3:
175+
self._device.get_ack()
174176

175177
def _write_int(self, data: int):
176178
"""Write a single int to the UART bus.

pslab/protocol.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""TODO"""
22

3+
import enum
34
import struct
45

56

@@ -179,16 +180,16 @@
179180
FILL_BUFFER = Byte.pack(27)
180181

181182
# /*---------- BAUDRATE for main comm channel----*/
182-
SETBAUD = Byte.pack(12)
183-
BAUD9600 = Byte.pack(1)
184-
BAUD14400 = Byte.pack(2)
185-
BAUD19200 = Byte.pack(3)
186-
BAUD28800 = Byte.pack(4)
187-
BAUD38400 = Byte.pack(5)
188-
BAUD57600 = Byte.pack(6)
189-
BAUD115200 = Byte.pack(7)
190-
BAUD230400 = Byte.pack(8)
191-
BAUD1000000 = Byte.pack(9)
183+
SETBAUD_LEGACY = Byte.pack(12)
184+
BAUD9600_LEGACY = Byte.pack(1)
185+
BAUD14400_LEGACY = Byte.pack(2)
186+
BAUD19200_LEGACY = Byte.pack(3)
187+
BAUD28800_LEGACY = Byte.pack(4)
188+
BAUD38400_LEGACY = Byte.pack(5)
189+
BAUD57600_LEGACY = Byte.pack(6)
190+
BAUD115200_LEGACY = Byte.pack(7)
191+
BAUD230400_LEGACY = Byte.pack(8)
192+
BAUD1000000_LEGACY = Byte.pack(9)
192193

193194
# /*-----------NRFL01 radio module----------*/
194195
NRFL01 = Byte.pack(13)
@@ -229,7 +230,8 @@
229230
# --------COMMUNICATION PASSTHROUGHS--------
230231
# Data sent to the device is directly routed to output ports such as (SCL, SDA for UART)
231232

232-
PASSTHROUGHS = Byte.pack(15)
233+
PASSTHROUGHS = Byte.pack(12)
234+
PASSTHROUGHS_LEGACY = Byte.pack(15)
233235
PASS_UART = Byte.pack(1)
234236

235237
# /*--------STOP STREAMING------*/

pslab/sciencelab.py

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -282,30 +282,38 @@ def _write_data_address(self, address: int, value: int):
282282
self.send_int(value)
283283
self.get_ack()
284284

285-
def enable_uart_passthrough(self, baudrate: int, persist=False):
285+
def enable_uart_passthrough(self, baudrate: int):
286286
"""Relay all data received by the device to TXD/RXD.
287287
288-
If a period > 0.5 seconds elapses between two transmit/receive events,
289-
the device resets and resumes normal mode. This timeout feature has
290-
been implemented in lieu of a hard reset option.
291-
292288
Can be used to load programs into secondary microcontrollers with
293289
bootloaders such ATMEGA or ESP8266
294290
295291
Parameters
296292
----------
297293
baudrate : int
298-
Baudrate of the UART bus.
299-
persist : bool, optional
300-
If set to True, the device will stay in passthrough mode until the
301-
next power cycle. Otherwise(default scenario), the device will
302-
return to normal operation if no data is sent/received for a period
303-
greater than one second at a time.
294+
Baudrate of the UART2 bus.
304295
"""
296+
if self.firmware.major < 3:
297+
self._uart_passthrough_legacy(baudrate)
298+
else:
299+
self._uart_passthrough(baudrate)
300+
301+
def _uart_passthrough(self, baudrate: int) -> None:
305302
self.send_byte(CP.PASSTHROUGHS)
306303
self.send_byte(CP.PASS_UART)
307-
self.send_byte(1 if persist else 0)
308-
self.send_int(int(round(((64e6 / baudrate) / 4) - 1)))
304+
self.send_int(self._get_brgval(baudrate))
305+
self.interface.baudrate = baudrate
306+
307+
def _uart_passthrough_legacy(self, baudrate: int) -> None:
308+
self.send_byte(CP.PASSTHROUGHS_LEGACY)
309+
self.send_byte(CP.PASS_UART)
310+
disable_watchdog = 1
311+
self.send_byte(disable_watchdog)
312+
self.send_int(self._get_brgval(baudrate))
313+
314+
@staticmethod
315+
def _get_brgval(baudrate: int) -> int:
316+
return int((CP.CLOCK_RATE / (4 * baudrate)) - 1)
309317

310318
def read_log(self):
311319
"""Read hardware debug log.

0 commit comments

Comments
 (0)