zephyr: drivers: spi: spi_mcux_lpspi: inconsistent chip select behaviour
Describe the bug The MCUX LPSPI SPI driver handles SPI chip selects inconsistently when comparing GPIO CS and “native” controller CS handling over multipart transfers.
With GPIO CS enabled (where the LPSPI controller does not control the CS line), the CS line is kept asserted through the entire transfer. This is opposed to what happens when the CS line is controlled by the LPSPI controller itself; then the CS line is deasserted between the different parts of the transfer.
To Reproduce
#include <zephyr.h>
#include <misc/printk.h>
#include <spi.h>
#define SPI_LABEL "SPI_1"
#define GPIO_CS_LABEL "GPIO_0"
#define GPIO_CS_PIN 16
void main(void)
{
struct spi_config spi_cfg;
struct device *spi_dev;
struct spi_cs_control spi_cs;
u8_t cmd[2] = { 0xaa, 0x55 }; /* 2 bytes command */
u8_t response[2]; /* 2 bytes response */
const struct spi_buf tx_bufs[] = {
{
.buf = cmd,
.len = sizeof(cmd),
},
};
const struct spi_buf_set tx = {
.buffers = tx_bufs,
.count = ARRAY_SIZE(tx_bufs),
};
const struct spi_buf rx_bufs[] = {
{
.buf = NULL,
.len = sizeof(cmd), /* 2 dummy bytes */
},
{
.buf = response,
.len = sizeof(response),
},
};
const struct spi_buf_set rx = {
.buffers = rx_bufs,
.count = ARRAY_SIZE(rx_bufs),
};
spi_dev = device_get_binding(SPI_LABEL);
if (!spi_dev) {
printk("SPI device not found\n");
return;
}
spi_cfg.operation = SPI_OP_MODE_MASTER | SPI_TRANSFER_MSB |
SPI_WORD_SET(8);
spi_cfg.frequency = 1000000;
spi_cfg.slave = 2;
#if 0
spi_cs.gpio_dev = device_get_binding(GPIO_CS_LABEL);
if (!spi_cs.gpio_dev) {
printk("SPI GPIO CS device not found\n");
return;
}
spi_cs.gpio_pin = GPIO_CS_PIN;
spi_cs.delay = 1; /* us */
spi_cfg.cs = &spi_cs;
#else
spi_cfg.cs = NULL;
#endif
spi_transceive(spi_dev, &spi_cfg, &tx, &rx);
}
Expected behavior The CS line remains asserted through all the parts of the multipart transfer.
Impact Deasserting the CS line in the middle of a transfer causes problem e.g. when communicating with SPI EEPROMs.
Screenshots or console output
When using GPIO CS:

When using LPSPI CS:

Environment (please complete the following information):
- OS: GNU/Linux
- Toolchain Zephyr SDK
- Commit SHA or Version used: c88c91942d984e4c2ad2b3484576d39c2538858d
Additional context This was spotted when trying to use a SPI EEPROM with the TWR-KE18F board, but it is not limited to that board nor to the KE1xF SoC series, as far as I can tell.
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Comments: 35 (30 by maintainers)
after 6 years, we may have a solution finally to the oldest nxp platform issue in zephyr…
can anyone following this issue please try out the version of the driver in #82877 to see if it is working for your use case? as far as I can tell the native CS works with that now and the testing is all passing, but would like someone to see if it works when someone actually tries using it.
I am concerned if there is some race conditions that I have not found that the test doesn’t catch or something. I found it difficult to support this API properly for the LPSPI due to the lpspi control fifo scheme being the CS control along with the stalling TX behavior. And I had to basically rewrite the entire driver to get this to work properly.
I am working on it now.
NXP is planning to contribute some rework of the lpspi driver soon, there is no quick fix, the existing driver just doesn’t meet the zephyr api and is broken in a lot of ways. This issue is actually boiling over lately on a lot of fronts so it will be addressed soon
As @henrikbrixandersen mentioned, CS line has to be asserted for the whole transaction. The buffers given to spi_transceive() are scatter-gather type, so the overall anyway represent one and only one transaction.
(The data frame size name in the documentation relates to the word size. This is mandatory to know how to interpret the buffers and how to r/w to/from the controller. The SPI_HOLD_ON_CS is a specific configuration bit where you can request the CS line to stay asserted after the spi_transceive() call. There were a few use case supporting that feature back then.)
I would expect the chip select to be deasserted only after all buffers in a transaction has been clocked in/out.
… sorry about this, I just returned to this in the spirit of LTS bug fixing and realized I accidentally left the pin muxed to the native CS before when I tested the GPIO (both mux options were in the overlay), now I see the difference, woops