NRF Connect read from accelerometer using SPI driver directly

Hi, I'm trying to port my NRF SDK15.3 application to NRF Connect. I made a custom board for my PCB and I can control the LEDs with PWM etc. Now I'd like to read data from my SPI connected accelerometer.

It's an LIS2DW12. In the Developer academy there's an example of how to use the twi driver directly (https://academy.nordicsemi.com/topic/i2c-driver/)

I'd like to do the same but with SPI.

I've added this to my custom board.dts file

&spi0 {
	compatible = "nordic,nrf-spim";
	status = "okay";
	// pinctrl-0 = <&spi1>					//do I need this?
	// pinctrl-names = "default";
	sck-pin  = <3>;
	miso-pin = <6>;
	mosi-pin = <8>;
	cs-gpios = <&gpio0 5 GPIO_ACTIVE_LOW>;

	lis2dw12: lis2dw12@0 {
		compatible = "spi-device";
		reg = <0>;							//what is this?
		label = "MYSENSOR";
		spi-max-frequency = < 0x80000000UL >;
	};
};

And this code to my lis2dw12.c file

#define SPI0_NODE    DT_NODELABEL(lis2dw12)


void spi_init()
{
     static const struct spi_dt_spec dev_spi = SPI_DT_SPEC_GET(SPI0_NODE, SPI_MODE_MASTER, 0);
}

This results in lots of build failures.

In file included from C:\ncs\v2.0.0\zephyr\include\zephyr\arch\arm\aarch32\arch.h:20,
                 from C:\ncs\v2.0.0\zephyr\include\zephyr\arch\cpu.h:19,
                 from C:\ncs\v2.0.0\zephyr\include\zephyr\kernel_includes.h:33,
                 from C:\ncs\v2.0.0\zephyr\include\zephyr\kernel.h:17,
                 from C:\ncs\v2.0.0\zephyr\include\zephyr\zephyr.h:18,
                 from c:\Vigglab\Projects\Doodoody\BTSender_ClipPair\NCS_Firmware\ClipPair\src\LIS2DW12.c:44:
../src/LIS2DW12.c: In function 'spi_init':
c:\Vigglab\Projects\Doodoody\BTSender_ClipPair\NCS_Firmware\ClipPair\build_3\zephyr\include\generated\devicetree_unfixed.h:10035:33: error: 'DT_N_S_soc_S_spi_40003000_S_lis2dw12_0_P_spi_max_frequency' undeclared (first use in this function); did you mean 'DT_N_S_soc_S_spi_40003000_S_lis2dw12_0_P_compatible_LEN'?
10035 | #define DT_N_NODELABEL_lis2dw12 DT_N_S_soc_S_spi_40003000_S_lis2dw12_0
      |                                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
C:\ncs\v2.0.0\zephyr\include\zephyr\devicetree.h:2991:24: note: in definition of macro 'DT_CAT'
 2991 | #define DT_CAT(a1, a2) a1 ## a2
      |                        ^~
C:\ncs\v2.0.0\zephyr\include\zephyr\drivers\spi.h:341:16: note: in expansion of macro 'DT_PROP'
  341 |   .frequency = DT_PROP(node_id, spi_max_frequency), \
      |                ^~~~~~~
C:\ncs\v2.0.0\zephyr\include\zephyr\drivers\spi.h:403:13: note: in expansion of macro 'SPI_CONFIG_DT'
  403 |   .config = SPI_CONFIG_DT(node_id, operation_, delay_) \
      |             ^~~~~~~~~~~~~
c:\Vigglab\Projects\Doodoody\BTSender_ClipPair\NCS_Firmware\ClipPair\src\LIS2DW12.c:91:48: note: in expansion of macro 'SPI_DT_SPEC_GET'
   91 |      static const struct spi_dt_spec dev_spi = SPI_DT_SPEC_GET(SPI0_NODE, SPI_MODE_MASTER, 0);
      |                                                ^~~~~~~~~~~~~~~
C:\ncs\v2.0.0\zephyr\include\zephyr\devicetree.h:2991:24: note: in expansion of macro 'DT_N_NODELABEL_lis2dw12'
 2991 | #define DT_CAT(a1, a2) a1 ## a2
      |                        ^~
C:\ncs\v2.0.0\zephyr\include\zephyr\devicetree.h:177:29: note: in expansion of macro 'DT_CAT'
  177 | #define DT_NODELABEL(label) DT_CAT(DT_N_NODELABEL_, label)
      |                             ^~~~~~

I've been trying to search on google for examples but without success. 

My plan was to use the SPI functions such as spi_transceive_dt, to replace my old calls to nrf_drv_spi_transfer

described in (https://docs.zephyrproject.org/latest/hardware/peripherals/spi.html)

https://docs.zephyrproject.org/latest/hardware/peripherals/spi.html#c.spi_transceive_dt

Any help is appreciated. 

Parents
  • Hello Jason,

    #define SPI0_NODE    DT_NODELABEL(lis2dw12)
    
    void spi_init()
    {
         static const struct spi_dt_spec dev_spi = SPI_DT_SPEC_GET(SPI0_NODE, SPI_MODE_MASTER, 0);
    }

    SPI_MODE_MASTER is supposed to be SPI_OP_MODE_MASTER. 

    // pinctrl-0 = <&spi1>					//do I need this?
    // pinctrl-names = "default";

    As a matter of fact, from NCS v2.x.x you do. Referring to the Pin control transition chapter of the nRF Connect SDK v2.0.0 migration notes. 

    reg = <0>;							//what is this?

    Assuming you are configuring the peripheral as a master, this line represents the chip-select / numbering of the slaves, see also here.

    compatible = "spi-device";

    This is not correct and should look like this instead: 

    compatible = "st,lis2dw12";

    If of interest, you can have a look at this SPI sample for your reference.

    Cheers, 

    Markus 

Reply
  • Hello Jason,

    #define SPI0_NODE    DT_NODELABEL(lis2dw12)
    
    void spi_init()
    {
         static const struct spi_dt_spec dev_spi = SPI_DT_SPEC_GET(SPI0_NODE, SPI_MODE_MASTER, 0);
    }

    SPI_MODE_MASTER is supposed to be SPI_OP_MODE_MASTER. 

    // pinctrl-0 = <&spi1>					//do I need this?
    // pinctrl-names = "default";

    As a matter of fact, from NCS v2.x.x you do. Referring to the Pin control transition chapter of the nRF Connect SDK v2.0.0 migration notes. 

    reg = <0>;							//what is this?

    Assuming you are configuring the peripheral as a master, this line represents the chip-select / numbering of the slaves, see also here.

    compatible = "spi-device";

    This is not correct and should look like this instead: 

    compatible = "st,lis2dw12";

    If of interest, you can have a look at this SPI sample for your reference.

    Cheers, 

    Markus 

Children
  • Thanks Markus!

    I would never have spotted the SPI_OP_MODE_MASTER mistake!

    I've changed my code to use the pinctrl THANKS again! 

    I prefer not to use the sensor/lis2dw12 supplied with zephyr because I already wrote a driver that I'm familiar with. 

    I've been able to read the WHO_AM_I registter by adapting the "NCS-SPI-Master-Slave-Example" that you linked. So that's great. I'll do the rest today I hope,

    It's a useful example but it needs updated to the latest NCS. There's deprecation warnings for gpio_dev for example

    c:\Vigglab\Projects\Doodoody\BTSender_ClipPair\NCS_Firmware\ClipPair\src\LIS2DW12.c:170:3: warning: 'gpio_dev' is deprecated [-Wdeprecated-declarations]
      170 |   spim_cs.gpio_dev = device_get_binding(MY_SPI_MASTER_CS_PORT);

    I've also noticed a discrepancy in the example overlay files which might be a bug.

    Thanks again for your help

    -Jason

Related