-
Couldn't load subscription status.
- Fork 8.1k
Description
When using spi conf <device> <frequency> [<settings>] followed by spi transceive from the shell, SPI controller drivers that are using spi_context_configured (such as spi_ll_stm32.c) do not allow for subsequent reconfiguration with spi conf. This means that it is not possible to change clock frequencies, polarity, phase, etc. without a reboot and there is no indication that the new settings did not take effect (besides verifying with a logic analyzer). Found on a STM32 Nucleo board, but appears to be applicable to other platforms as well.
This issue is due to spi_shell.c using a static struct spi_config whose address never changes, and the spi_context_configured API is simply checking to see if the address of the last used struct spi_config changed. Since the address never changes, even if the contents change, spi_context_configured returns true and the new configuration is never applied.
I have not investigated the best way of fixing this, but here are a few ideas:
- Have
spi_shell.cperform a dummy / 0 byte SPI transaction with a differentstruct spi_configstructure whenspi confis called. This will forcespi_context_configuredto returnfalsesince the address is different and force a reconfiguration on the next transaction. However, this may not work properly with all SPI controller drivers and would require testing. - Add a new
SPI_OP_RECONFIGflag tospi_operation_t(used inside ofstruct spi_config) to forcespi_context_configuredto return false and always force reconfiguration when set. This would be set byspi confand optionally cleared afterspi transceive. - Enhance
spi_context_configuredto perform a better (albeit slower) check, such as hashing the structure. - Enhance spi shell to double buffer the
struct spi_configstructure, and swap which structure is used on aspi conf. This would be fairly complicated to implement correctly though, as the last used config would need to be tracked on a per-controller basis. - Utilize
spi_releaseto clear the cachedstruct spi_configpointer in the context. This is not supported by all controller drivers, would change the semantics ofspi_release, and would unnecessarily force reconfiguration in some cases. - Add a new API / syscall to clear the cached
struct spi_config,
To Reproduce
spi conf spi@44002000 80000000spi transceive aa<- transceives at ~80MHz, mode 0, MSB first as requestedspi conf spi@44002000 100000 olspi transceive aa<- transceives at ~80MHz, mode 0, MSB first; should be ~100kHz, mode 2, LSB first
Expected behavior
SPI transaction with spi transceive is made with requested settings from spi conf.
Impact
Since the spi shell is mainly for debugging and hardware bringup, this is minor. Given that there is no indication of a potential issue to the user though, they may be inclined to believe that their SPI peripheral is not working instead of the controller settings being incorrect.
Environment (please complete the following information):
- OS: Linux
- Toolchain: Zephyr SDK 16.9
- Commit SHA or Version used: v3.7.1