hw/ssi/stm32f2xx_spi: avoid transferring data on DR reads
Subsystems (2)
PATCH 1/1 hw/ssi/stm32f2xx_spi: avoid transferring data on DR reads
Writing SPI_DR already performs the synchronous SSI transfer and stores its response in spi_dr. Reading SPI_DR performs another transfer, using the previous response as transmit data, and returns the new response. Consequently a normal write/read pair clocks two frames on the bus and can skip bytes in a peripheral's response.
Return the response saved by the write and clear RXNE without starting another transfer. Repeated reads then leave the peripheral state alone.
Add a qtest using the upstream netduino2 machine and an M25P80 flash. Check the JEDEC ID, RXNE clearing, and repeated DR reads. The test requires no board changes and reproduces the unwanted extra transfers before the fix.
--- MAINTAINERS | 1 + hw/ssi/stm32f2xx_spi.c | 1 - tests/qtest/meson.build | 2 ++ tests/qtest/stm32f2xx-spi-test.c | 50 ++++++++++++++++++++++++++++++++ 4 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 tests/qtest/stm32f2xx-spi-test.c
MAINTAINERS +1 -0
@@ -1201,6 +1201,7 @@ F: hw/char/stm32f2xx_usart.c F: hw/timer/stm32f2xx_timer.c F: hw/adc/* F: hw/ssi/stm32f2xx_spi.c +F: tests/qtest/stm32f2xx-spi-test.c F: include/hw/*/stm32*.h STM32F405
hw/ssi/stm32f2xx_spi.c +0 -1
@@ -82,7 +82,6 @@ static uint64_t stm32f2xx_spi_read(void *opaque, hwaddr addr,case STM_SPI_SR :return s->spi_sr;case STM_SPI_DR : -stm32f2xx_spi_transfer (s); s->spi_sr &= ~STM_SPI_SR_RXNE ;return s->spi_dr;case STM_SPI_CRCPR :
tests/qtest/meson.build +2 -0
@@ -238,6 +238,8 @@ qtests_stm32l4x5 = \ 'stm32l4x5_usart-test'] qtests_arm = \ + (config_all_devices.has_key('CONFIG_NETDUINO2') and + config_all_devices.has_key('CONFIG_SSI_M25P80') ? ['stm32f2xx-spi-test'] : []) + \ (config_all_devices.has_key('CONFIG_MPS2') ? ['sse-timer-test'] : []) + \ (config_all_devices.has_key('CONFIG_CMSDK_APB_DUALTIMER') ? ['cmsdk-apb-dualtimer-test'] : []) + \ (config_all_devices.has_key('CONFIG_CMSDK_APB_TIMER') ? ['cmsdk-apb-timer-test'] : []) + \
tests/qtest/stm32f2xx-spi-test.c new file +50 -0
50 lines not on this page · raw message
Signature · 1 line
2.54.0 (Apple Git-157)
Quoted 8 lines · Manuele Conti
Writing
SPI_DRalready performs the synchronous SSI transfer and stores
its response inspi_dr. ReadingSPI_DRperforms another transfer, using
the previous response as transmit data, and returns the new response. Consequently a normal write/read pair clocks two frames on the bus and
can skip bytes in a peripheral's response.
Yeah, this seems broken as it is today.
It does seem like reading SPI_DR should pop the data from the RX Buffer, but we don't actually have an RX Buffer to pop from.
This change seems like an improvement on currently dropping the read data, but ideally we would have a buffer to copy to/from.
Quoted 13 lines
Return the response saved by the write and clear RXNE without starting
another transfer. Repeated reads then leave the peripheral state alone.Add a qtest using the upstream netduino2 machine and an M25P80 flash. Check the JEDEC ID, RXNE clearing, and repeated DR reads. The test requires
no board changes and reproduces the unwanted extra transfers before the
fix.
Quoted 2 lines
Fixes 5ae74402d1f ("STM32F2xx: Add the SPI device")Signed-off-by Manuele Conti <[email protected]>
Thanks for the fix!
Alistair
Quoted 110 lines
---
MAINTAINERS | 1 +
hw/ssi/stm32f2xx_spi.c | 1 -
tests/qtest/meson.build | 2 ++
tests/qtest/stm32f2xx-spi-test.c | 50
++++++++++++++++++++++++++++++++4 files changed, 53 insertions(+), 1 deletion(-)create mode 100644
tests/qtest/stm32f2xx-spi-test.cMAINTAINERS +0
-0@@ -1201,6 +1201,7 @@ F: hw/char/stm32f2xx_usart.cF:
hw/timer/stm32f2xx_timer.c
F: hw/adc/*
F:hw/ssi/stm32f2xx_spi.c
+F:tests/qtest/stm32f2xx-spi-test.c
F:include/hw/*/stm32*.h
STM32F405hw/ssi/stm32f2xx_spi.c +0
-0@@ -82,7 +82,6 @@ static uint64_t stm32f2xx_spi_read(void *opaque, hwaddr addr,case
STM_SPI_SR:
returns->spi_sr;
caseSTM_SPI_DR:
-stm32f2xx_spi_transfer(s);
s->spi_sr&= ~STM_SPI_SR_RXNE;
returns->spi_dr;
caseSTM_SPI_CRCPR:tests/qtest/meson.build +0
-0@@ -238,6 +238,8 @@ qtests_stm32l4x5 = \'stm32l4x5_usart-test']
qtests_arm = \
+ (config_all_devices.has_key('CONFIG_NETDUINO2')and
+config_all_devices.has_key('CONFIG_SSI_M25P80')? ['stm32f2xx- spi-test'] : []) + \
(config_all_devices.has_key('CONFIG_MPS2')? ['sse-timer-test'] :
[]) + \
(config_all_devices.has_key('CONFIG_CMSDK_APB_DUALTIMER')? ['cmsdk-apb-dualtimer-test'] : []) + \
(config_all_devices.has_key('CONFIG_CMSDK_APB_TIMER')? ['cmsdk- apb-timer-test'] : []) + \tests/qtest/stm32f2xx-spi-test.c new file +33
-0@@ -0,0 +1,50 @@ +/* +* STM32F2XX SPI controller tests +* +* Copyright (c) 2026 Manuele Conti <[email protected]> +* SPDX-License-Identifier: GPL-2.0-or-later +*/ + +#include "qemu/osdep.h" +#include "libqtest.h" + +/* The first SSI bus found by -device belongs to SPI3. */ +#define SPI3_BASE 0x40003c00 +#define SPI_CR1 (SPI3_BASE + 0x00) +#define SPI_SR (SPI3_BASE + 0x08) +#define SPI_DR (SPI3_BASE + 0x0c) +#define SPI_RXNE (1U << 0) + +static void test_jedec_id (void ) +{ + static const uint8_t jedec_id[] = { 0x20, 0x20, 0x14 }; + QTestState *qts = qtest_init("-machine netduino2 " + "-device m25p80,bus=ssi"); + + /* Enable SPI in master mode, with software slave management. */ + qtest_writel(qts, SPI_CR1, (1U << 9) | (1U << 8) | + (1U << 6) | (1U << 2)); + + qtest_writeb(qts, SPI_DR, 0x9f); + qtest_readb(qts, SPI_DR); + + for (unsigned i = 0; i < ARRAY_SIZE(jedec_id); i++) { + qtest_writeb(qts, SPI_DR, 0); + g_assert_cmphex(qtest_readl(qts, SPI_SR) & SPI_RXNE, ==,
SPI_RXNE);
+g_assert_cmphex(qtest_readb(qts, SPI_DR), ==, jedec_id[i]); +g_assert_cmphex(qtest_readl(qts, SPI_SR) & SPI_RXNE, ==, 0); +
+ /* Reading DR again must not clock another byte out of the flash. */
+g_assert_cmphex(qtest_readb(qts, SPI_DR), ==, jedec_id[i]); +g_assert_cmphex(qtest_readl(qts, SPI_SR) & SPI_RXNE, ==, 0); + }tests/qtest/stm32f2xx-spi-test.c new file +10
-0+ + qtest_quit(qts); +} + +int main (int argc,char **argv) +{ + g_test_init(&argc, &argv, NULL); + qtest_add_func("/stm32f2xx/spi/jedec-id", test_jedec_id); + return g_test_run(); +}
Peter Maydell
AppliedQuoted 10 lines · Manuele Conti
Writing
SPI_DRalready performs the synchronous SSI transfer and stores its response inspi_dr. ReadingSPI_DRperforms another transfer, using the previous response as transmit data, and returns the new response. Consequently a normal write/read pair clocks two frames on the bus and can skip bytes in a peripheral's response.Return the response saved by the write and clear RXNE without starting another transfer. Repeated reads then leave the peripheral state alone.
Quoted 7 lines
Add a qtest using the upstream netduino2 machine and an M25P80 flash. Check the JEDEC ID, RXNE clearing, and repeated DR reads. The test requires no board changes and reproduces the unwanted extra transfers before the fix.
Fixes 5ae74402d1f ("STM32F2xx: Add the SPI device")Signed-off-by Manuele Conti <[email protected]>
Applied to target-arm.next, thanks.
-- PMM