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 Reply Children
  • 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,

  • Hello Susheel Nuguru & Hello ,

    It seems even now the code is throwing faults:
    Please find the sequence of code execution leading to faults as in below:

    kernel.h

    poll.c

    poll.c

    fault_s.S

    fault_s.S

    fault.c

    fault.c

    fault.c

    My 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)
    {
    	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);
    }

    Kindly help to solve this.

    Thanks,

  • Could you upload the whole project (main.c, prj.conf, CMakeLists.txt, .overlay files and so on...) in zipped format.

    Best regards,

    Simon

  • Hello ,

    Could you upload the whole project (main.c, prj.conf, CMakeLists.txt, .overlay files and so on...) in zipped format.

    SPI_Sample.7z

    Please suggest 

    It seems even now the code is throwing faults:
    Please find the sequence of code execution leading to faults as in below:
  • I'm not too familiar with spi_transceive_async(), so I decided to run the sample zephyr\tests\drivers\spi\spi_loopback, which demonstrates how to properly use spi_transceive_async(). I don't have an nRF52833 DK at hand at the moment, but I built it for the nRF52840 DK. Then I connected P1.03 and P1.04 together and it worked fine. I also added a conf file for the nRF5232 and that also worked.

    Could you add a file nrf52833dk_nrf52833.conf to zephyr\tests\drivers\spi\spi_loopback\boards with the following content:

    CONFIG_SPI_3_NRF_ORC=0x00
    CONFIG_SPI_3_NRF_RX_DELAY=1
    
    CONFIG_SPI_LOOPBACK_DRV_NAME="SPI_1"
    CONFIG_SPI_LOOPBACK_CS_GPIO=y
    CONFIG_SPI_LOOPBACK_CS_CTRL_GPIO_DRV_NAME="GPIO_0"
    CONFIG_SPI_LOOPBACK_CS_CTRL_GPIO_PIN=28
    

    Then connect together P0.30 and P1.08 (doublecheck \zephyr\tests\drivers\spi\spi_loopback\build\zephyr\zephyr.dts to see that this is the pins "SPI_1" uses), build and flash zephyr\tests\drivers\spi\spi_loopback and see if you get the following output:

    *** Booting Zephyr OS build v2.7.0-ncs1  ***
    Running test suite test_spi
    ===================================================================
    START - test_spi_loopback
    I: SPI test on buffers TX/RX 0x200001ad/0x2000096a
    I: SPI test slow config
    I: Start complete multiple
    I: Passed
    I: Start complete loop
    I: Passed
    I: Start null tx
    I: Passed
    I: Start half start
    I: Passed
    I: Start half end
    I: Passed
    I: Start every 4
    I: Passed
    I: Start async call
    I: Passed
    I: SPI test fast config
    I: Start complete multiple
    I: Passed
    I: Start complete loop
    I: Passed
    I: Start null tx
    I: Passed
    I: Start half start
    I: Passed
    I: Start half end
    I: Passed
    I: Start every 4
    I: Passed
    I: Start async call
    I: Passed
    I: Start complete loop
    I: Passed
    I: Start complete loop
    I: Passed
    I: All tx/rx passed
     PASS - test_spi_loopback in 0.60 seconds
    ===================================================================
    Test suite test_spi succeeded
    ===================================================================
    PROJECT EXECUTION SUCCESSFUL
    

    If you see these lines printed, it means the spi_transceive_async() call worked:

    I: Start async call
    I: Passed

    If you get that to work, you can strip down the test sample and remove everything you don't need. If you don't get it to work, please tell me and I will get a hold of an nRF52833 and test it myself.

    Best regards,

    Simon

Related