BUS FAULT in application code using k_poll

Hi,

I am using an nRF52833dk to run a simple async SPI transceive API using k_poll
My code is as below, for some reason it always returns BUS FAULT error

Code:

void main(void)
{
	struct k_poll_signal async_sig;
	uint8_t data = 40, val =0, sz = 9;
	    struct spi_buf bufs = {
            .buf = &data,
            .len = sizeof(data)
    };
    struct spi_buf_set tx = {
        .buffers = &bufs
    };

    tx.count = 1;

	struct spi_buf rbufs = {
            .buf = &val,
            .len = sizeof(val)
    };
    struct spi_buf_set rx = {
        .buffers = &rbufs
    };

 spi_config spi_cfg;
spi_cs_control cs_ctrl;
    cs_ctrl.gpio_dev = device_get_binding("GPIO_0");
    cs_ctrl.gpio_pin = 11;
    cs_ctrl.delay = 0;
    spi_cfg.operation = SPI_WORD_SET(8) | SPI_OP_MODE_MASTER 
    //                    | SPI_MODE_CPOL | SPI_MODE_CPHA 
                        | SPI_LINES_SINGLE; 
    spi_cfg.frequency = 1000000;
	spi_cfg.cs = &cs_ctrl;

	const device * sp;
	sp = device_get_binding("SPI_0");

		data++;
			struct k_poll_event  async_evt =
		K_POLL_EVENT_INITIALIZER(K_POLL_TYPE_SIGNAL,
					 K_POLL_MODE_NOTIFY_ONLY,
					 &async_sig);

			printk("transmitted data: %u \n", data);
			int error = spi_transceive_async(sp,&spi_cfg,&tx, &rx,&async_sig);
			int ret = k_poll(&async_evt, 1, K_MSEC(1000));
			printk("received data: %u \n", val);

}

Error:


Kindly help to solve this.

Thanks,

Ubaid

Parents
  • It does not look like you have initialized your signal

    static struct k_poll_signal async_sig = K_POLL_SIGNAL_INITIALIZER(async_sig);
  • Hello ,

    Thank you so much, 

    It does not look like you have initialized your signal

    I was able to solve that bus fault issue.

    My updated code is:

    #include <string.h>
    #include <errno.h>
    #include <zephyr.h>
    #include <sys/printk.h>
    #include <device.h>
    #include <drivers/spi.h>
    
    #define STACKSIZE 1024
    #define PRIORITY 99
    
    void main(void)
    {
    	static struct k_poll_signal async_sig = K_POLL_SIGNAL_INITIALIZER(async_sig);
    	uint8_t data = 40, val =0, sz = 9;
    	    struct spi_buf bufs = {
                .buf = &data,
                .len = sizeof(data)
        };
        struct spi_buf_set tx = {
            .buffers = &bufs
        };
    
        tx.count = 1;
    
    	struct spi_buf rbufs = {
                .buf = &val,
                .len = sizeof(val)
        };
        struct spi_buf_set rx = {
            .buffers = &rbufs
        };
    	struct spi_config spi_cfg;
    	struct spi_cs_control cs_ctrl;
    
    	cs_ctrl.gpio_dev = device_get_binding("GPIO_0");
        cs_ctrl.gpio_pin = 11;
        cs_ctrl.delay = 0;
    
    	spi_cfg.operation = SPI_WORD_SET(8) | SPI_OP_MODE_MASTER | SPI_LINES_SINGLE; 
        spi_cfg.frequency = 1000000;
    	spi_cfg.cs = &cs_ctrl;
    
    	const struct device * sp;
    	sp = device_get_binding("SPI_0");
    
    			data++;
    			struct k_poll_event  async_evt =
    		K_POLL_EVENT_INITIALIZER(K_POLL_TYPE_SIGNAL,
    					 K_POLL_MODE_NOTIFY_ONLY,
    					 &async_sig);
    
    			printk("transmitted data: %u \n", data);
    			int error = spi_transceive_async(sp,&spi_cfg,&tx, &rx,&async_sig);
    			int ret = k_poll(&async_evt, 10, K_MSEC(1000));
    			printk("received data: %u \n", val);
    }

    And the output is:

     The execution just exists after printing 'r' from the last print statement

    printk("received data: %u \n", val);

    Why isn't it polling at the specified frequency of 1000ms and rather prematurely exiting after one execution.

    Kindly suggest.

    Thanks,

Reply
  • Hello ,

    Thank you so much, 

    It does not look like you have initialized your signal

    I was able to solve that bus fault issue.

    My updated code is:

    #include <string.h>
    #include <errno.h>
    #include <zephyr.h>
    #include <sys/printk.h>
    #include <device.h>
    #include <drivers/spi.h>
    
    #define STACKSIZE 1024
    #define PRIORITY 99
    
    void main(void)
    {
    	static struct k_poll_signal async_sig = K_POLL_SIGNAL_INITIALIZER(async_sig);
    	uint8_t data = 40, val =0, sz = 9;
    	    struct spi_buf bufs = {
                .buf = &data,
                .len = sizeof(data)
        };
        struct spi_buf_set tx = {
            .buffers = &bufs
        };
    
        tx.count = 1;
    
    	struct spi_buf rbufs = {
                .buf = &val,
                .len = sizeof(val)
        };
        struct spi_buf_set rx = {
            .buffers = &rbufs
        };
    	struct spi_config spi_cfg;
    	struct spi_cs_control cs_ctrl;
    
    	cs_ctrl.gpio_dev = device_get_binding("GPIO_0");
        cs_ctrl.gpio_pin = 11;
        cs_ctrl.delay = 0;
    
    	spi_cfg.operation = SPI_WORD_SET(8) | SPI_OP_MODE_MASTER | SPI_LINES_SINGLE; 
        spi_cfg.frequency = 1000000;
    	spi_cfg.cs = &cs_ctrl;
    
    	const struct device * sp;
    	sp = device_get_binding("SPI_0");
    
    			data++;
    			struct k_poll_event  async_evt =
    		K_POLL_EVENT_INITIALIZER(K_POLL_TYPE_SIGNAL,
    					 K_POLL_MODE_NOTIFY_ONLY,
    					 &async_sig);
    
    			printk("transmitted data: %u \n", data);
    			int error = spi_transceive_async(sp,&spi_cfg,&tx, &rx,&async_sig);
    			int ret = k_poll(&async_evt, 10, K_MSEC(1000));
    			printk("received data: %u \n", val);
    }

    And the output is:

     The execution just exists after printing 'r' from the last print statement

    printk("received data: %u \n", val);

    Why isn't it polling at the specified frequency of 1000ms and rather prematurely exiting after one execution.

    Kindly suggest.

    Thanks,

Children
No Data
Related