SPI with Mesh Sensor Example

Hello!

I have added a UART and I2C to the Mesh Sensor Server example and on a custom NRF52840 board everything works fine.

Today I tried to add SPI without success.

When I downloaded and built the ncs-spi-master-slave-example I get similar issues. The program crashes with the output shown in the screen shots. It seems to be interrupt related. I would be happy to use a SPI Master async or blocking function but can not seem to get that right either.

Please provide an example or suggestions for sending a few bytes to a SPI sensor and getting the result back in a NRF Connect / Zephyr project. I have a lot of SPI experience but I seem to be missing something here.

Thank you!

Regards,

David Carlin

From ncs-spi-master example

From my Mesh Sensor project:

Parents
  • Hi,

    The sample you're using:

    https://github.com/too1/ncs-spi-master-slave-example/tree/master

    Is based on NCS v2.3.0, and does not use _DT for fetching the cs-gpios.

    Try applying this patch:

    diff --git a/src/main.c b/src/main.c
    index d3fc0b5..b3163f8 100644
    --- a/src/main.c
    +++ b/src/main.c
    @@ -24,20 +24,12 @@
     const struct device *spi_dev;
     static struct k_poll_signal spi_done_sig = K_POLL_SIGNAL_INITIALIZER(spi_done_sig);
     
    -struct spi_cs_control spim_cs = {
    -       .gpio = SPI_CS_GPIOS_DT_SPEC_GET(DT_NODELABEL(reg_my_spi_master)),
    -       .delay = 0,
    -};
    -
     static void spi_init(void)
     {
            spi_dev = DEVICE_DT_GET(MY_SPI_MASTER);
            if(!device_is_ready(spi_dev)) {
                    printk("SPI master device not ready!\n");
            }
    -       if(!device_is_ready(spim_cs.gpio.port)){
    -               printk("SPI master chip select device not ready!\n");
    -       }
     }
     
     static const struct spi_config spi_cfg = {
    @@ -45,7 +37,9 @@ static const struct spi_config spi_cfg = {
                                     SPI_MODE_CPOL | SPI_MODE_CPHA,
            .frequency = 4000000,
            .slave = 0,
    -       .cs = &spim_cs,
    +       .cs = {
    +                       .gpio = GPIO_DT_SPEC_GET(MY_SPI_MASTER, cs_gpios),
    +       },
     };
     
     static int spi_write_test_msg(void)
    
    

     

    In this log:

    From my Mesh Sensor project:

    You are asserting in sem.c:128, this one specifically:

    https://github.com/nrfconnect/sdk-zephyr/blob/v3.4.99-ncs1-1/kernel/sem.c#L128

    This could be a side-effect introduced due to the .cs_gpio not properly initialized, but the assert occurs when taking a semaphore from interrupt context. Are you triggering a transfer directly in a GPIO interrupt callback?

     

    Kind regards,

    Håkon

Reply
  • Hi,

    The sample you're using:

    https://github.com/too1/ncs-spi-master-slave-example/tree/master

    Is based on NCS v2.3.0, and does not use _DT for fetching the cs-gpios.

    Try applying this patch:

    diff --git a/src/main.c b/src/main.c
    index d3fc0b5..b3163f8 100644
    --- a/src/main.c
    +++ b/src/main.c
    @@ -24,20 +24,12 @@
     const struct device *spi_dev;
     static struct k_poll_signal spi_done_sig = K_POLL_SIGNAL_INITIALIZER(spi_done_sig);
     
    -struct spi_cs_control spim_cs = {
    -       .gpio = SPI_CS_GPIOS_DT_SPEC_GET(DT_NODELABEL(reg_my_spi_master)),
    -       .delay = 0,
    -};
    -
     static void spi_init(void)
     {
            spi_dev = DEVICE_DT_GET(MY_SPI_MASTER);
            if(!device_is_ready(spi_dev)) {
                    printk("SPI master device not ready!\n");
            }
    -       if(!device_is_ready(spim_cs.gpio.port)){
    -               printk("SPI master chip select device not ready!\n");
    -       }
     }
     
     static const struct spi_config spi_cfg = {
    @@ -45,7 +37,9 @@ static const struct spi_config spi_cfg = {
                                     SPI_MODE_CPOL | SPI_MODE_CPHA,
            .frequency = 4000000,
            .slave = 0,
    -       .cs = &spim_cs,
    +       .cs = {
    +                       .gpio = GPIO_DT_SPEC_GET(MY_SPI_MASTER, cs_gpios),
    +       },
     };
     
     static int spi_write_test_msg(void)
    
    

     

    In this log:

    From my Mesh Sensor project:

    You are asserting in sem.c:128, this one specifically:

    https://github.com/nrfconnect/sdk-zephyr/blob/v3.4.99-ncs1-1/kernel/sem.c#L128

    This could be a side-effect introduced due to the .cs_gpio not properly initialized, but the assert occurs when taking a semaphore from interrupt context. Are you triggering a transfer directly in a GPIO interrupt callback?

     

    Kind regards,

    Håkon

Children
Related