Multiple device with different GPIO chip selects on SPI2


armand@...
 

Good day,

I have decided to dive head first into Zephyr with a project of mine.

I have a LIS2DH and a W25Q128JV Flash on the same SPI bus, with two GPIOs to control the chip selects. I have a STM32F413VGT6 microcontroller, and the Zephyr version is

However, I am having some trouble. When the FLASH SPI is initialized (in function spi_nor_read_id called by  spi_not_configure) to read the JEDEC ID, the LIS2DH chip select is still not initialised, ie its low, so I have two devices selected at the same time. This results in an incorrect JEDEC ID.

Later when I try to use the LIS2DH, no chip selects are being controlled, leading to a problem in getting data from the LIS2DH.

I cannot find any place in the code where the cs-gpios from the dts file is used. In fact I have been able to ascertain that the chip select list is not initialized with the following test:

static inline void spi_context_cs_control(struct spi_context *ctx, bool on)
{
if (ctx)
{
printf("Test0 %d\n",(int)on);

if (ctx->config)
{
printf("Test1 %p\n",ctx->config);

if (ctx->config->cs)
{
printf("Test2 %p\n",ctx->config->cs);

if (ctx->config->cs->gpio_dev)
{
printf("Test3 %p,%s,%08X\n",ctx->config->cs->gpio_dev,
ctx->config->cs->gpio_dev->config->name,
ctx->config->cs->gpio_dev->config->init);
}
}
}
}

_spi_context_cs_control(ctx, on, false);
printf("Setting CS End");
}

At the time the SPI NOR flash is initialised the output from the above function is:
Test0 1
Test1 0x20000f78
Test2 0x20000f84
Test3 0x200050fc,GPIOD
Setting CS End
Test0 0
Test1 0x20000f78
Test2 0x20000f84
Test3 0x200050fc,GPIOD
Setting CS End
(And I can see the NOR Flash chip select activated)

but by the time I want to use the LISD2H, the output is:
Test0 1
Test1 0x20000f5c
Setting CS End
Test0 0
Test1 0x20000f5c
Setting CS End
(No chip select activity since the chip select GPIO list ctx->config->cs is NULL)

I have seen the debate on where chip select control is to be done here:
https://github.com/zephyrproject-rtos/zephyr/issues/12698 and
https://github.com/zephyrproject-rtos/zephyr/issues/12226
but none of these really helped my to solve my problem.

Here is an extract from my dts file:

&spi2 {
status = "okay";

cs-gpios = <&gpiob 7 0>, <&gpiod 11 0>;

lis2dh@0 {
compatible = "st,lis2dh";
spi-max-frequency = <1000000>;
reg = <0>;
irq-gpios = <&gpiob 6 0>, <&gpiod 7 0>;
label = "LIS2DH";
};


flash1: flash@1 {
compatible = "jedec,spi-nor";
reg = <1>;
size = <16777216>;
spi-max-frequency = <2000000>;
label = "W25Q128JV";
jedec-id = [ef 40 18];
};
};


Does anyone know of a good working example with multiple Chip selects on a SPI bus where the CS lines are controlled with GPIOs, or can someone point me in the right direction to solve this problem?

Thanks in advance, any help is appreciated.

Greeting from South Africa