Migrating to SDK 2.0.0 caused my CS pin to stop working on my SPI

Was using SDK 1.9.1 up till now, but I migrated to SDK 2.0.0 and I changed my overlay file to add the pin control stuff.  I have a nrf5340 and use the app_ns board config.

This is the overlay file


&pinctrl {

    spi2_default: spi2_default {
        group1 {
            psels = <NRF_PSEL(SPIM_SCK, 0, 26)>,
                <NRF_PSEL(SPIM_MISO, 0, 10)>,
                <NRF_PSEL(SPIM_MOSI, 1, 5)>;
        };
    };

    spi2_sleep: spi2_sleep {
        group1 {
            psels = <NRF_PSEL(SPIM_SCK, 0, 26)>,
                <NRF_PSEL(SPIM_MISO, 0, 10)>,
                <NRF_PSEL(SPIM_MOSI, 1, 5)>;
            low-power-enable;
        };
    };

};

&spi2 {
    compatible = "nordic,nrf-spim";
    status = "okay";
    pinctrl-0 = <&spi2_default>;
    pinctrl-1 = <&spi2_sleep>;
    pinctrl-names = "default", "sleep";
    cs-gpios = <&gpio0 25 GPIO_ACTIVE_LOW>;
};

This is a C file

static const struct device * spi_dev;

static struct spi_cs_control spi_cs = {
    .gpio_dev = NULL,
    .gpio_pin = 25,
    .gpio_dt_flags = GPIO_ACTIVE_LOW,
    .delay = 0,
  };

static struct spi_config spi_cfg = {
  .operation = SPI_WORD_SET(8) | SPI_TRANSFER_MSB,// |
        // SPI_MODE_CPOL | SPI_MODE_CPHA,
  .frequency = 8000000,
  .slave = 0,
  .cs = &spi_cs,
};
spi_dev = device_get_binding("SPI_2");

    int err;
    static uint8_t tx_buffer[3];
    static uint8_t rx_buffer[3];
    const struct spi_buf tx_buf = {
        .buf = tx_buffer,
        .len = sizeof(tx_buffer)
    };
    const struct spi_buf_set tx = {
        .buffers = &tx_buf,
        .count = 1
    };

    struct spi_buf rx_buf = {
        .buf = rx_buffer,
        .len = sizeof(rx_buffer),
    };
    const struct spi_buf_set rx = {
        .buffers = &rx_buf,
        .count = 1
    };

    tx_buffer[0] = 0x00;
    tx_buffer[1] = 0x80; // write
    rx_buffer[2] = 0x00;
err = spi_transceive(spi_dev, &spi_cfg, &tx, &rx);

So that code used to make the CS line work during communication.  The 1.9.1 overlay file used to look like this

&spi2 {
    compatible = "nordic,nrf-spim";
    status = "okay";
    sck-pin = <26>;
    mosi-pin = <37>;
    miso-pin = <10>;
    cs-gpios = <&gpio0 25 GPIO_ACTIVE_LOW>;
};

And I took away the pin config lines for the new pin control stuff. 

Any idea what I'm doing wrong or what I'm missing?  I watch the pins on a logic analyzer and the CS line gets configured and goes high and then never goes low. 

Also

I was looking through here

https://developer.nordicsemi.com/nRF_Connect_SDK/doc/2.0.0/zephyr/hardware/peripherals/spi.html

 

And saw this

struct spi_cs_controlÁ

#include <spi.h>

SPI Chip Select control structure.

This can be used to control a CS line via a GPIO line, instead of using the controller inner CS logic.

 

So I can use the controller inner CS logic?  How do I do that?  Was I always using the GPIO line?  

Parents
  • Here is some other weirdness.  My FAE has also been thinking about this and he emailed me:

    But you have 

    .gpio dev set to NULL

    I think it needs to.point to a structure set in the binding of the gpio port

    Creating a structure with the binding of the node label "GPIO_0"  

    so I think he was referring to

    static struct spi_cs_control spi_cs = {
        .gpio_dev = NULL,
        .gpio_pin = 25,
        .gpio_dt_flags = GPIO_ACTIVE_LOW,
        .delay = 0,
      };

    now all the examples I have seen with SPI had .gpio_dev set to NULL.  And it worked in SDK 1.9.1.  But since I'm in a "I'll try anything" I did this:

    const struct device *gpioPort0;

      gpioPort0 = device_get_binding("GPIO_0");

      spi_cs.gpio_dev = gpioPort0;

    By setting .gpio_dev to that, I get the compiler message that .gpio_dev is deprecated:

    But that made the CS line work.

    Before setting .gpio_dev

    After setting that

    So is .gpio_dev deprecated?  Does that mean I should stop using it?  Why was this not needed in 1.9.1 and it's now needed in 2.0.0?  And if it's deprecated is there something else I should be doing to make the CS line work with SPI in SDK 2.0.0?

  • I tried to investigate this.

    I figured out that the CS pin set by DTS would eventually end up in dev->data->ctx->cs_gpios->pin and the CS pin set through the API would eventually end up in dev->data->ctx->config->cs->gpio->pin (this is used when asserting/deasserting CS).

    So it seems like setting CS through DTS does nothing, if my findings are correct. I will continue to investigate and also look at v1.9.1 and how it was done there

    Best regards,

    Simon

Reply Children
No Data
Related