This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Using nRF5340-PDK with zephyr adxl372 sensor sample project

I'm new to the nRF5340 and zephyr and ncs, but I have gone through the tutorial series https://devzone.nordicsemi.com/nordic/nrf-connect-sdk-guides/b/getting-started/posts/nrf-connect-sdk-tutorial 

We're developing a product that will use the SPIS peripheral on the nRF5340 and I have a few nRF5340-PDK boards

Since I haven't found any SPIS sample projects, I started with the zephyr adxl372 sensor sample project.

I wanted to get it to a point where the SPIM peripheral was working (wiggling pins as expected) then create my own sensor and change it to SPIS.

I re-mapped the SPI pins to pins on port 1.

I started by using the non-secure build, but couldn't get any SPI activity on the pins.

It looked like the chip select pin was being handled as a GPIO pin (rather than as part of the SPIM peripheral) and when I tried to manually change registers in the GPIO1 peripheral through the SES through the registers view, all the SPIM1 registers showed as 0 and I couldn't change them.

I wondered if GPIO1 was not under the application processor's control, because the NRF_P1 is missing from the peripheral domain status printed at bootup.

Peripheral Domain Status
00 NRF_P0 Non-Secure OK
01 NRF_CLOCK Non-Secure OK
02 NRF_RTC0 Non-Secure OK
03 NRF_RTC1 Non-Secure OK
04 NRF_NVMC Non-Secure OK
05 NRF_UARTE1 Non-Secure OK
06 NRF_UARTE2 Secure SKIP
07 NRF_TWIM2 Non-Secure OK
08 NRF_SPIM3 Non-Secure OK
09 NRF_TIMER0 Non-Secure OK
10 NRF_TIMER1 Non-Secure OK
11 NRF_TIMER2 Non-Secure OK
12 NRF_SAADC Non-Secure OK
13 NRF_PWM0 Non-Secure OK
14 NRF_PWM1 Non-Secure OK
15 NRF_PWM2 Non-Secure OK
16 NRF_PWM3 Non-Secure OK
17 NRF_IPC Non-Secure OK
18 NRF_VMC Non-Secure OK
19 NRF_FPU Non-Secure OK
20 NRF_EGU1 Non-Secure OK
21 NRF_EGU2 Non-Secure OK
22 NRF_DPPIC Non-Secure OK
23 NRF_GPIOTE1 Non-Secure OK
24 NRF_REGULATORS Non-Secure OK

When I choose Project > Configure nRF Connect SDK Project (in the non-secure project) it shows two options - menuconfig and spm_menuconfig

I tried the secure build, and then I saw SPI activity.

But when I try to view the configuration with Project > Configure nRF Connect SDK Project (in the secure project) it does not show me two options.  It just takes me to a config.

Q1:  Why doesn't the non-secure project work to control the SPI pins?

Q2: Why doesn't the secure project show both menuconfig and spm_menuconfig?

  • Since SPIM2 does not work on nRF5340 yet I updated prj.conf as follows:

    CONFIG_STDOUT_CONSOLE=y
    CONFIG_LOG=y
    CONFIG_SPI=y
    CONFIG_SPI_2=n
    CONFIG_SPI_1=y
    CONFIG_SPI_1_OP_MODES=1
    CONFIG_SPI_1_NRF_SPIM=y
    CONFIG_SPI_1_NRF_ORC=0xff
    CONFIG_SENSOR=y
    CONFIG_ADXL372=y
    CONFIG_ADXL372_SPI=y
    CONFIG_SENSOR_LOG_LEVEL_WRN=y
    

    And I added nrf5340_dk_nrf5340_cpuapp.overlay and nrf5340_dk_nrf5340_cpuappns.overlay both as follows:

    &spi1 {
    	status = "okay";
    	compatible = "nordic,nrf-spim";
    	sck-pin = < 0x26 >;
    	mosi-pin = < 0x28 >;
    	miso-pin = < 0x27 >;
    	cs-gpios = <&gpio1 5 0>;
    
    	adxl372@0 {
    		compatible = "adi,adxl372";
    		reg = <0>;
    		spi-max-frequency = <1000000>;
    		label = "ADXL372";
    		int1-gpios = <&gpio1 5 0>;
    	};
    };
    &spi2 {
    	status = "disabled";
    };
    

  • Hi Douglas,

    Q1:

    Looking at the pins you have selected, 26,27 and 28 should be OK, but you have selected the same pin (p1.05) for both CS and int1. ( They are not the same )

    One other thing you should notice is that to use the adxl372 driver you need to connect to a adxl372. Since it is a init function that runs in the beginning and check if the device is connected. (if not the "device_get_binding()" will fail)

    Q2:

    When you build a regular application, you build it for the non-secure area as described in the document "working with nrf9160".
    But you also build "Secure Partition Manager" at the same time, which is why both the "menuconfig" for your application and "spm_menuconfig" shows when you try to configure something building an application in the "non-secure" area.

  • Q1:

    I understand that if I want to fully use the adxl372 example program (get accelerometer readings, etc.) I would need to connect an adxl372.

    But I'm just trying to get the SPI port operating.

    Once I get the SPI port operating, I'm going to change the example code to not use the adxl372 but to accomodate what we're doing on our board.

    I'm looking at the SPI signals (cs, sck, mosi, miso) with an oscilloscope and looking for data to be sent over the SPI bus.

    I understand that without an adxl372 connected, device_get_binding() will fail, but it should attempt to read a few registers over the SPI bus before concluding that the adxl372 is not connected.  I should see those register reads on the oscilloscope.

    When I build and run a non-secure application

    This is what I see on the console:

    [2020-05-18 15:29:26.692] *** Booting Zephyr OS build v2.1.99-ncs1  ***
    [2020-05-18 15:29:26.697] Flash region		Domain		Permissions
    [2020-05-18 15:29:26.700] 00 0x00000 0x08000 	Secure		rwxl
    [2020-05-18 15:29:26.702] 01 0x08000 0x10000 	Non-Secure	rwxl
    [2020-05-18 15:29:26.706] 02 0x10000 0x18000 	Non-Secure	rwxl
    [2020-05-18 15:29:26.709] 03 0x18000 0x20000 	Non-Secure	rwxl
    [2020-05-18 15:29:26.714] 04 0x20000 0x28000 	Non-Secure	rwxl
    [2020-05-18 15:29:26.716] 05 0x28000 0x30000 	Non-Secure	rwxl
    [2020-05-18 15:29:26.719] 06 0x30000 0x38000 	Non-Secure	rwxl
    [2020-05-18 15:29:26.723] 07 0x38000 0x40000 	Non-Secure	rwxl
    [2020-05-18 15:29:26.726] 08 0x40000 0x48000 	Non-Secure	rwxl
    [2020-05-18 15:29:26.729] 09 0x48000 0x50000 	Non-Secure	rwxl
    [2020-05-18 15:29:26.733] 10 0x50000 0x58000 	Non-Secure	rwxl
    [2020-05-18 15:29:26.736] 11 0x58000 0x60000 	Non-Secure	rwxl
    [2020-05-18 15:29:26.739] 12 0x60000 0x68000 	Non-Secure	rwxl
    [2020-05-18 15:29:26.742] 13 0x68000 0x70000 	Non-Secure	rwxl
    [2020-05-18 15:29:26.746] 14 0x70000 0x78000 	Non-Secure	rwxl
    [2020-05-18 15:29:26.749] 15 0x78000 0x80000 	Non-Secure	rwxl
    [2020-05-18 15:29:26.752] 16 0x80000 0x88000 	Non-Secure	rwxl
    [2020-05-18 15:29:26.756] 17 0x88000 0x90000 	Non-Secure	rwxl
    [2020-05-18 15:29:26.759] 18 0x90000 0x98000 	Non-Secure	rwxl
    [2020-05-18 15:29:26.763] 19 0x98000 0xa0000 	Non-Secure	rwxl
    [2020-05-18 15:29:26.765] 20 0xa0000 0xa8000 	Non-Secure	rwxl
    [2020-05-18 15:29:26.769] 21 0xa8000 0xb0000 	Non-Secure	rwxl
    [2020-05-18 15:29:26.772] 22 0xb0000 0xb8000 	Non-Secure	rwxl
    [2020-05-18 15:29:26.775] 23 0xb8000 0xc0000 	Non-Secure	rwxl
    [2020-05-18 15:29:26.779] 24 0xc0000 0xc8000 	Non-Secure	rwxl
    [2020-05-18 15:29:26.781] 25 0xc8000 0xd0000 	Non-Secure	rwxl
    [2020-05-18 15:29:26.785] 26 0xd0000 0xd8000 	Non-Secure	rwxl
    [2020-05-18 15:29:26.788] 27 0xd8000 0xe0000 	Non-Secure	rwxl
    [2020-05-18 15:29:26.792] 28 0xe0000 0xe8000 	Non-Secure	rwxl
    [2020-05-18 15:29:26.795] 29 0xe8000 0xf0000 	Non-Secure	rwxl
    [2020-05-18 15:29:26.798] 30 0xf0000 0xf8000 	Non-Secure	rwxl
    [2020-05-18 15:29:26.801] 31 0xf8000 0x100000 	Non-Secure	rwxl
    [2020-05-18 15:29:26.805] Non-secure callable region 0 placed in flash region 0 with size 32.
    [2020-05-18 15:29:26.811] 
    [2020-05-18 15:29:26.811] 
    [2020-05-18 15:29:26.811] SRAM region		Domain		Permissions
    [2020-05-18 15:29:26.814] 00 0x00000 0x02000	Secure		rwxl
    [2020-05-18 15:29:26.816] 01 0x02000 0x04000	Secure		rwxl
    [2020-05-18 15:29:26.819] 02 0x04000 0x06000	Secure		rwxl
    [2020-05-18 15:29:26.823] 03 0x06000 0x08000	Secure		rwxl
    [2020-05-18 15:29:26.826] 04 0x08000 0x0a000	Secure		rwxl
    [2020-05-18 15:29:26.828] 05 0x0a000 0x0c000	Secure		rwxl
    [2020-05-18 15:29:26.832] 06 0x0c000 0x0e000	Secure		rwxl
    [2020-05-18 15:29:26.835] 07 0x0e000 0x10000	Secure		rwxl
    [2020-05-18 15:29:26.837] 08 0x10000 0x12000	Non-Secure	rwxl
    [2020-05-18 15:29:26.841] 09 0x12000 0x14000	Non-Secure	rwxl
    [2020-05-18 15:29:26.843] 10 0x14000 0x16000	Non-Secure	rwxl
    [2020-05-18 15:29:26.850] 11 0x16000 0x18000	Non-Secure	rwxl
    [2020-05-18 15:29:26.850] 12 0x18000 0x1a000	Non-Secure	rwxl
    [2020-05-18 15:29:26.855] 13 0x1a000 0x1c000	Non-Secure	rwxl
    [2020-05-18 15:29:26.856] 14 0x1c000 0x1e000	Non-Secure	rwxl
    [2020-05-18 15:29:26.865] 15 0x1e000 0x20000	Non-Secure	rwxl
    [2020-05-18 15:29:26.865] 16 0x20000 0x22000	Non-Secure	rwxl
    [2020-05-18 15:29:26.866] 17 0x22000 0x24000	Non-Secure	rwxl
    [2020-05-18 15:29:26.875] 18 0x24000 0x26000	Non-Secure	rwxl
    [2020-05-18 15:29:26.875] 19 0x26000 0x28000	Non-Secure	rwxl
    [2020-05-18 15:29:26.876] 20 0x28000 0x2a000	Non-Secure	rwxl
    [2020-05-18 15:29:26.886] 21 0x2a000 0x2c000	Non-Secure	rwxl
    [2020-05-18 15:29:26.886] 22 0x2c000 0x2e000	Non-Secure	rwxl
    [2020-05-18 15:29:26.886] 23 0x2e000 0x30000	Non-Secure	rwxl
    [2020-05-18 15:29:26.892] 24 0x30000 0x32000	Non-Secure	rwxl
    [2020-05-18 15:29:26.892] 25 0x32000 0x34000	Non-Secure	rwxl
    [2020-05-18 15:29:26.902] 26 0x34000 0x36000	Non-Secure	rwxl
    [2020-05-18 15:29:26.902] 27 0x36000 0x38000	Non-Secure	rwxl
    [2020-05-18 15:29:26.902] 28 0x38000 0x3a000	Non-Secure	rwxl
    [2020-05-18 15:29:26.908] 29 0x3a000 0x3c000	Non-Secure	rwxl
    [2020-05-18 15:29:26.909] 30 0x3c000 0x3e000	Non-Secure	rwxl
    [2020-05-18 15:29:26.918] 31 0x3e000 0x40000	Non-Secure	rwxl
    [2020-05-18 15:29:26.920] 
    [2020-05-18 15:29:26.920] Peripheral		Domain		Status
    [2020-05-18 15:29:26.920] 00 NRF_P0               Non-Secure	OK
    [2020-05-18 15:29:26.920] 01 NRF_CLOCK            Non-Secure	OK
    [2020-05-18 15:29:26.929] 02 NRF_RTC0             Non-Secure	OK
    [2020-05-18 15:29:26.929] 03 NRF_RTC1             Non-Secure	OK
    [2020-05-18 15:29:26.934] 04 NRF_NVMC             Non-Secure	OK
    [2020-05-18 15:29:26.936] 05 NRF_UARTE1           Non-Secure	OK
    [2020-05-18 15:29:26.944] 06 NRF_UARTE2           Secure		SKIP
    [2020-05-18 15:29:26.944] 07 NRF_TWIM2            Non-Secure	OK
    [2020-05-18 15:29:26.944] 08 NRF_SPIM3            Non-Secure	OK
    [2020-05-18 15:29:26.951] 09 NRF_TIMER0           Non-Secure	OK
    [2020-05-18 15:29:26.951] 10 NRF_TIMER1           Non-Secure	OK
    [2020-05-18 15:29:26.957] 11 NRF_TIMER2           Non-Secure	OK
    [2020-05-18 15:29:26.958] 12 NRF_SAADC            Non-Secure	OK
    [2020-05-18 15:29:26.967] 13 NRF_PWM0             Non-Secure	OK
    [2020-05-18 15:29:26.967] 14 NRF_PWM1             Non-Secure	OK
    [2020-05-18 15:29:26.976] 15 NRF_PWM2             Non-Secure	OK
    [2020-05-18 15:29:26.977] 16 NRF_PWM3             Non-Secure	OK
    [2020-05-18 15:29:26.977] 17 NRF_IPC              Non-Secure	OK
    [2020-05-18 15:29:26.986] 18 NRF_VMC              Non-Secure	OK
    [2020-05-18 15:29:26.987] 19 NRF_FPU              Non-Secure	OK
    [2020-05-18 15:29:26.987] 20 NRF_EGU1             Non-Secure	OK
    [2020-05-18 15:29:26.988] 21 NRF_EGU2             Non-Secure	OK
    [2020-05-18 15:29:26.997] 22 NRF_DPPIC            Non-Secure	OK
    [2020-05-18 15:29:26.997] 23 NRF_GPIOTE1          Non-Secure	OK
    [2020-05-18 15:29:27.002] 24 NRF_REGULATORS       Non-Secure	OK
    [2020-05-18 15:29:27.005] 
    [2020-05-18 15:29:27.005] SPM: NS image at 0x8000
    [2020-05-18 15:29:27.012] SPM: NS MSP at 0x200109f0
    [2020-05-18 15:29:27.014] SPM: NS reset vector at 0xa235
    [2020-05-18 15:29:27.014] SPM: prepare to jump to Non-Secure image.
    

    Using the debugger in SES, I can see that the code execution is getting to adxl372_init() and running adxl372_probe() and adxl372_reg_read().  It appears to get stuck in z_impl_spi_transceive().

    On the oscilloscope, I've tried triggering on CS going low, SCK going low, and MOSI going low.  None of these signals are changing state while running the non-secure app.

    HOWEVER, if I build and run the app as secure:

    This is what I see on the console:

    [2020-05-18 16:00:20.338] *** Booting Zephyr OS build v2.1.99-ncs1  ***
    [2020-05-18 16:00:20.342] Could not get ADXL372 device
    [2020-05-18 16:00:21.335] [00:00:00.000,000] <err> ADXL372: failed to read id (0x0:0x0)
    

    and the oscilloscope triggers.  I see the nRF5340 send 0x01 0xFF 0x05 0xFF over the SPI bus (on MOSI).

    So I'm trying to understand why the secure app is actually sending SPI bytes but the non-secure app is not.

    It's not preventing me from moving on, but since I'm new to the nRF Connect SDK and zephyr and the nRF5340 I'm trying to better understand the issues with using them.

    By the way, I've changed the overlay files to separate the CS and interrupt pins.  This doesn't seem to affect the behavior described above.

    Here's my updated project

    adxl372-20200518.zip

  • Hi,
    If you are trying out just the SPI, I would rather recommend you checking out a simple SPI sample --> https://github.com/Rallare/fw-nrfconnect-nrf/tree/nrf9160_samples/samples/nrf9160/spi that shows how you can implement and use it.

    Just change the .overlay file to suit your nrf5340 and your chosen pins.

    The reason it's working when building the application in the Secure area, is because then you have the SPI 1 available to be used. If you look at the "Secure Partition Manager" when building the application for the non-secure area you see that you only have enabled SPI 3 to be used in the non-secure area:

    Not all peripherals have been set to new available by default in the non-secure area.

    However, there is a Pull request to fix that:https://github.com/NordicPlayground/fw-nrfconnect-nrf/pull/1896

    So either change the device tree file (.overlay file) + prj.conf (configuration file) file to use SPI 3 or

    change the nrf/subsys/spm files "Kconfig" and "spm.c" with these additional lines so you get access to the SPI_1 in the non-secure area.

    Kconfig

    spm.c

    Br,

    Martin L.

Related