From 836ca4a86c05aec73ed413c8bf2f0da091e2fe80 Mon Sep 17 00:00:00 2001 From: tejaspadole1-max Date: Sat, 24 Jan 2026 17:36:34 +0530 Subject: [PATCH 1/5] Fix typo FERQUENCY -> FREQUENCY and resolve TypeError in tests --- tests/test_spi.py | 6 +++--- tests/test_uart.py | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/test_spi.py b/tests/test_spi.py index ecd73d9..903c766 100644 --- a/tests/test_spi.py +++ b/tests/test_spi.py @@ -30,7 +30,7 @@ CS = "LA3" SPIMaster._primary_prescaler = PPRE = 0 SPIMaster._secondary_prescaler = SPRE = 0 -PWM_FERQUENCY = SPIMaster._frequency * 2 / 3 +PWM_FREQUENCY = 1000 MICROSECONDS = 1e-6 RELTOL = 0.05 # Number of expected logic level changes. @@ -61,7 +61,7 @@ def slave(handler: SerialHandler) -> SPISlave: @pytest.fixture def la(handler: SerialHandler) -> LogicAnalyzer: pwm = PWMGenerator(handler) - pwm.generate(SDI[1], PWM_FERQUENCY, 0.5) + pwm.generate(SDI[1], PWM_FREQUENCY, 0.5) return LogicAnalyzer(handler) @@ -73,7 +73,7 @@ def verify_value( smp: int = 0, ): sck_ts = sck_timestamps[smp::2] - pwm_half_period = ((1 / PWM_FERQUENCY) * 1e6) / 2 # microsecond + pwm_half_period = ((1 / PWM_FREQUENCY) * 1e6) / 2 # microsecond pattern = "" for t in sck_ts: diff --git a/tests/test_uart.py b/tests/test_uart.py index fd861e5..560ffb8 100644 --- a/tests/test_uart.py +++ b/tests/test_uart.py @@ -16,7 +16,7 @@ WRITE_DATA = 0x55 TXD2 = "LA1" RXD2 = "SQ1" -PWM_FERQUENCY = UART._baudrate // 2 +PWM_FREQUENCY = 1000 MICROSECONDS = 1e-6 RELTOL = 0.05 # Number of expected logic level changes. @@ -38,7 +38,7 @@ def la(handler: SerialHandler) -> LogicAnalyzer: @pytest.fixture def pwm(handler: SerialHandler) -> None: pwm = PWMGenerator(handler) - pwm.generate(RXD2, PWM_FERQUENCY, 0.5) + pwm.generate(RXD2, PWM_FREQUENCY, 0.5) def test_configure(la: LogicAnalyzer, uart: UART): From 6690924dc7c95c50bb29097f73960fe12b81f606 Mon Sep 17 00:00:00 2001 From: tejaspadole1-max Date: Mon, 9 Feb 2026 00:20:52 +0530 Subject: [PATCH 2/5] Fix: Use realistic static frequencies (100kHz/500kHz) with comments (Fixes #270) --- tests/test_spi.py | 3 ++- tests/test_uart.py | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/test_spi.py b/tests/test_spi.py index 903c766..16cdc4e 100644 --- a/tests/test_spi.py +++ b/tests/test_spi.py @@ -30,7 +30,8 @@ CS = "LA3" SPIMaster._primary_prescaler = PPRE = 0 SPIMaster._secondary_prescaler = SPRE = 0 -PWM_FREQUENCY = 1000 +# Static value 100kHz used because instance property '_frequency' cannot be accessed on the class. +PWM_FREQUENCY = 100000.0 MICROSECONDS = 1e-6 RELTOL = 0.05 # Number of expected logic level changes. diff --git a/tests/test_uart.py b/tests/test_uart.py index 560ffb8..0f249a0 100644 --- a/tests/test_uart.py +++ b/tests/test_uart.py @@ -16,7 +16,8 @@ WRITE_DATA = 0x55 TXD2 = "LA1" RXD2 = "SQ1" -PWM_FREQUENCY = 1000 +# Static value 500kHz (half of default 1MHz baudrate) used as instance property cannot be accessed here. +PWM_FREQUENCY = 500000.0 MICROSECONDS = 1e-6 RELTOL = 0.05 # Number of expected logic level changes. From 17c901e7263001e678a8ed6eca0f6515b633b971 Mon Sep 17 00:00:00 2001 From: Tejas Padole Date: Sat, 14 Feb 2026 23:51:34 +0530 Subject: [PATCH 3/5] Fix SPI driver for firmware v3 by using _BURST commands --- pslab/bus/spi.py | 22 +++------------------- 1 file changed, 3 insertions(+), 19 deletions(-) diff --git a/pslab/bus/spi.py b/pslab/bus/spi.py index ad885ce..6de991d 100644 --- a/pslab/bus/spi.py +++ b/pslab/bus/spi.py @@ -51,9 +51,9 @@ class _SPIPrimitive: """ _TRANSFER_COMMANDS_MAP = { - 8: CP.SEND_SPI8, - 16: CP.SEND_SPI16, - } # PSLab only supports 8 and 16 bits. + 8: CP.SEND_SPI8_BURST, + 16: CP.SEND_SPI16_BURST, + } # PSLab only supports 8 and 16 bits. _INTEGER_TYPE_MAP = { 8: CP.Byte, 16: CP.ShortInt, @@ -508,9 +508,7 @@ def transfer8(self, data: int) -> int: data_in : int Data returned by slave device. """ - self._start() data_in = self._transfer(data, 8) - self._stop() return data_in @@ -527,9 +525,7 @@ def transfer16(self, data: int) -> int: data_in : int Data returned by slave device. """ - self._start() data_in = self._transfer(data, 16) - self._stop() return data_in @@ -546,9 +542,7 @@ def transfer8_bulk(self, data: List[int]) -> List[int]: data_in : list of int List of 8-bit data returned by slave device. """ - self._start() data_in = self._transfer_bulk(data, 8) - self._stop() return data_in @@ -565,9 +559,7 @@ def transfer16_bulk(self, data: List[int]) -> List[int]: data_in : list of int List of 16-bit data returned by slave device. """ - self._start() data_in = self._transfer_bulk(data, 16) - self._stop() return data_in @@ -579,9 +571,7 @@ def read8(self) -> int: int Data returned by slave device. """ - self._start() data_in = self._read(8) - self._stop() return data_in @@ -593,9 +583,7 @@ def read16(self) -> int: int Data returned by slave device. """ - self._start() data_in = self._read(16) - self._stop() return data_in @@ -612,9 +600,7 @@ def read8_bulk(self, data_to_read: int) -> List[int]: list of int List of 8-bit data returned by slave device. """ - self._start() data_in = self._read_bulk(data_to_read, 8) - self._stop() return data_in @@ -631,9 +617,7 @@ def read16_bulk(self, data_to_read: int) -> List[int]: list of int List of 16-bit date returned by slave device. """ - self._start() data_in = self._read_bulk(data_to_read, 16) - self._stop() return data_in From 2e3d2aa6db6059fecf79539b6f727cce5f469841 Mon Sep 17 00:00:00 2001 From: Tejas Padole Date: Mon, 16 Feb 2026 21:53:33 +0530 Subject: [PATCH 4/5] Refactor: Remove dead SPI methods and avoid hardcoded test frequencies --- pslab/bus/spi.py | 23 ----------------------- tests/test_spi.py | 7 ++++--- tests/test_uart.py | 8 ++++---- 3 files changed, 8 insertions(+), 30 deletions(-) diff --git a/pslab/bus/spi.py b/pslab/bus/spi.py index 6de991d..c55f2c3 100644 --- a/pslab/bus/spi.py +++ b/pslab/bus/spi.py @@ -214,29 +214,6 @@ def _get_parameters(cls) -> Tuple[int]: cls._smp, ) - def _start(self): - """Select SPI channel to enable. - - Basically sets the relevant chip select pin to LOW. - - External ChipSelect pins: - version < 5 : {6, 7} # RC5, RC4 (dropped support) - version == 5 : {} (don't have any external CS pins) - version == 6 : {7} # RC4 - """ - self._device.send_byte(CP.SPI_HEADER) - self._device.send_byte(CP.START_SPI) - self._device.send_byte(7) # SPI.CS v6 - # No ACK because `RESPONSE == DO_NOT_BOTHER` in firmware. - - def _stop(self): - """Select SPI channel to disable. - - Sets the relevant chip select pin to HIGH. - """ - self._device.send_byte(CP.SPI_HEADER) - self._device.send_byte(CP.STOP_SPI) - self._device.send_byte(7) # SPI.CS v6 def _transfer(self, data: int, bits: int) -> int: """Send data over SPI and receive data from SPI simultaneously. diff --git a/tests/test_spi.py b/tests/test_spi.py index 16cdc4e..442e224 100644 --- a/tests/test_spi.py +++ b/tests/test_spi.py @@ -31,7 +31,6 @@ SPIMaster._primary_prescaler = PPRE = 0 SPIMaster._secondary_prescaler = SPRE = 0 # Static value 100kHz used because instance property '_frequency' cannot be accessed on the class. -PWM_FREQUENCY = 100000.0 MICROSECONDS = 1e-6 RELTOL = 0.05 # Number of expected logic level changes. @@ -60,9 +59,11 @@ def slave(handler: SerialHandler) -> SPISlave: @pytest.fixture -def la(handler: SerialHandler) -> LogicAnalyzer: +def la(handler: SerialHandler, spi_master: SPIMaster) -> LogicAnalyzer: pwm = PWMGenerator(handler) - pwm.generate(SDI[1], PWM_FREQUENCY, 0.5) + # Bot ka formula: Static frequency ki jagah dynamic use karein + pwm_frequency = spi_master._frequency * 2 / 3 + pwm.generate(SDI[1], pwm_frequency, 0.5) return LogicAnalyzer(handler) diff --git a/tests/test_uart.py b/tests/test_uart.py index 0f249a0..f04fa1c 100644 --- a/tests/test_uart.py +++ b/tests/test_uart.py @@ -17,7 +17,6 @@ TXD2 = "LA1" RXD2 = "SQ1" # Static value 500kHz (half of default 1MHz baudrate) used as instance property cannot be accessed here. -PWM_FREQUENCY = 500000.0 MICROSECONDS = 1e-6 RELTOL = 0.05 # Number of expected logic level changes. @@ -37,10 +36,11 @@ def la(handler: SerialHandler) -> LogicAnalyzer: @pytest.fixture -def pwm(handler: SerialHandler) -> None: +def pwm(handler: SerialHandler, uart: UART) -> None: pwm = PWMGenerator(handler) - pwm.generate(RXD2, PWM_FREQUENCY, 0.5) - + # 500000.0 ki jagah dynamic formula + pwm_frequency = uart._baudrate / 2.0 + pwm.generate(RXD2, pwm_frequency, 0.5) def test_configure(la: LogicAnalyzer, uart: UART): baudrate = 1000000 From 5a3b713c906a76dcc97f2149c5268c8e6e8f97e0 Mon Sep 17 00:00:00 2001 From: Tejas Padole Date: Fri, 20 Feb 2026 23:13:47 +0530 Subject: [PATCH 5/5] Docs: Replace TODO with descriptive module docstring in protocol.py --- pslab/protocol.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/pslab/protocol.py b/pslab/protocol.py index de7f258..1af3c16 100644 --- a/pslab/protocol.py +++ b/pslab/protocol.py @@ -1,5 +1,11 @@ -"""TODO""" - +""" +PSLab Communication Protocol Constants. + +This module defines the command headers, function codes, and constant +values used to communicate with the PSLab hardware. It serves as a +centralized registry for the byte-level protocol between the Python +library and the device firmware. +""" import enum import struct