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
  • Jason said:
    I've changed my code to use the pinctrl THANKS again! 

    My pleasure, Jason! I’m glad to hear that you are making progress.

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

    Please note that this is not an official NCS sample and as stated in its requirements, it currently only supports NCS v1.9.1 (-:

    Requirements 
    
    SDK: 
    
        nRF Connect SDK v1.9.1 
    
    Supported boards: 
    
        nrf52dk_nrf52832 
        nrf52840dk_nrf52840 
        nrf5340dk_nrf5340_cpuapp 

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

    This actually is not a bug. Referring to the &spi1 devicetree of e.g. the nRF52840DK:

    spi1: spi@40004000 {
    	compatible = "nordic,nrf-spi";
    	#address-cells = < 0x1 >;
    	#size-cells = < 0x0 >;
    	reg = < 0x40004000 0x1000 >;
    	interrupts = < 0x4 0x1 >;
    	status = "okay";
    	label = "SPI_1";
    	pinctrl-0 = < &spi1_default >;
    	pinctrl-1 = < &spi1_sleep >;
    	pinctrl-names = "default", "sleep";
    };
     

    The nRF53/nRF91 & nRF52 series are using different SPI drivers. 

    Cheers, 

    Markus 

Reply
  • Jason said:
    I've changed my code to use the pinctrl THANKS again! 

    My pleasure, Jason! I’m glad to hear that you are making progress.

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

    Please note that this is not an official NCS sample and as stated in its requirements, it currently only supports NCS v1.9.1 (-:

    Requirements 
    
    SDK: 
    
        nRF Connect SDK v1.9.1 
    
    Supported boards: 
    
        nrf52dk_nrf52832 
        nrf52840dk_nrf52840 
        nrf5340dk_nrf5340_cpuapp 

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

    This actually is not a bug. Referring to the &spi1 devicetree of e.g. the nRF52840DK:

    spi1: spi@40004000 {
    	compatible = "nordic,nrf-spi";
    	#address-cells = < 0x1 >;
    	#size-cells = < 0x0 >;
    	reg = < 0x40004000 0x1000 >;
    	interrupts = < 0x4 0x1 >;
    	status = "okay";
    	label = "SPI_1";
    	pinctrl-0 = < &spi1_default >;
    	pinctrl-1 = < &spi1_sleep >;
    	pinctrl-names = "default", "sleep";
    };
     

    The nRF53/nRF91 & nRF52 series are using different SPI drivers. 

    Cheers, 

    Markus 

Children
No Data
Related