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

Enabling SPI in nRF9160 using overlay file

Hello,

I have followed the tutorial series of nRF Connect SDK Tutorial and now I have basic understanding on how the ncs works.

I'm trying to enable SPI0 in the nRF9160 DK using overlay file (project attached in the end)

My overlay file: nrf9160_pca10090ns.overlay

&spi0 {
status = "okay";
cs-gpios = <13>;
sck-pin = <10>;
mosi-pin = <11>;
miso-pin = <12>;
label = "CAN_SPI_0";
};

My CMakeLists.txt

cmake_minimum_required(VERSION 3.13.1)
set(SPI_DTC_OVERLAY_FILE
${CMAKE_CURRENT_SOURCE_DIR}/nrf9160_pca10090ns.overlay
)
include($ENV{ZEPHYR_BASE}/../nrf/cmake/boilerplate.cmake)
include($ENV{ZEPHYR_BASE}/cmake/app/boilerplate.cmake NO_POLICY_SCOPE)
project(NONE)

target_sources(app PRIVATE src/main.c)


My main.c

#include <zephyr.h>
#include <sys/printk.h>
#include <drivers/spi.h>

#define SPI_DEVICE_NAME DT_NORDIC_NRF_SPIM_SPI_0_LABEL

void main(void)
{
struct device *spi_dev = device_get_binding(SPI_DEVICE_NAME);
}

When running west build -b nrf9160_pca10090ns -d build_nrf9160_pca10090ns

I get the following error

.

./src/main.c: In function 'main':
../src/main.c:5:25: error: 'DT_NORDIC_NRF_SPIM_SPI_0_LABEL' undeclared (first use in this function); did you mean 'DT_NORDIC_NRF_SPIM_SPI_3_LABEL'?
#define SPI_DEVICE_NAME DT_NORDIC_NRF_SPIM_SPI_0_LABEL
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
../src/main.c:9:46: note: in expansion of macro 'SPI_DEVICE_NAME'
struct device *spi_dev = device_get_binding(SPI_DEVICE_NAME);
^~~~~~~~~~~~~~~
../src/main.c:5:25: note: each undeclared identifier is reported only once for each function it appears in
#define SPI_DEVICE_NAME DT_NORDIC_NRF_SPIM_SPI_0_LABEL
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
../src/main.c:9:46: note: in expansion of macro 'SPI_DEVICE_NAME'
struct device *spi_dev = device_get_binding(SPI_DEVICE_NAME);
^~~~~~~~~~~~~~~
../src/main.c:9:17: warning: unused variable 'spi_dev' [-Wunused-variable]
struct device *spi_dev = device_get_binding(SPI_DEVICE_NAME);
^~~~~~~
[125/136] Building C object zephyr/CMakeFiles/zephyr.dir/drivers/timer/nrf_rtc_timer.c.obj
ninja: build stopped: subcommand failed.
ERROR: command exited with status 1: 'C:\Program Files\CMake\bin\cmake.EXE' --build 'D:\DEV\Phantomtracker\software\my_projects\project_hello\hello_world\build_nrf9160_pca10090ns'

To find a solution I tried the following :

  • I changed SPI_DEVICE_NAME to SPI3
    #define SPI_DEVICE_NAME DT_NORDIC_NRF_SPIM_SPI_3_LABEL

This will buil the project with success. This is make sense because SPI3 is already enabled in ncs\v1.2.0\zephyr\boards\arm\nrf9160_pca10090\Kconfig.defconfig

  • I tried to enable SPI0 using Segger Embedded studio as shown in the image below :

I got the following error

-- Configuring done
CMake Error at D:/DEV/nRF/ncs/v1.2.0/zephyr/cmake/extensions.cmake:372 (add_library):
No SOURCES given to target: drivers__serial
Call Stack (most recent call first):
D:/DEV/nRF/ncs/v1.2.0/zephyr/cmake/extensions.cmake:349 (zephyr_library_named)
D:/DEV/nRF/ncs/v1.2.0/zephyr/drivers/serial/CMakeLists.txt:3 (zephyr_library)


CMake Generate step failed. Build files cannot be regenerated correctly.
Project load failed
Reported error: solution load command failed (1)

Cleaning the project didn't help.

Q1: How can I fix configuration over Segger?

  • I tried using ninja menuconfig, but again got the following error

ninja: error: loading 'build.ninja': The system cannot find the file specified

Q2 : Ninja is working since I can build projects, am I missing paths ?

  • I thought that I can enable it in the pjr.conf file but I'm not sure what's the string to add. something like CONFIG_SPI_0_NRFX_SPIM = y ?

Q3 : is there any documention that helps defining it ?

  • Checking the build folder I can see that  SPI0 was set correctly in build_nrf9160_pca10090ns\zephyr\zephyr.dts

			spi0: spi@8000 {
				#address-cells = < 0x1 >;
				#size-cells = < 0x0 >;
				reg = < 0x8000 0x1000 >;
				interrupts = < 0x8 0x1 >;
				status = "okay";
				label = "CAN_SPI_0";
				cs-gpios = < 0xd >;
				sck-pin = < 0xa >;
				mosi-pin = < 0xb >;
				miso-pin = < 0xc >;
			};

Q4 : most important question  : can enable SPI in this project  Slight smile ?

Project attached here hello_world.zip

Regards,

hicham

  • Hi!

    Thank you for your patience, it took longer than I expected to figure that out. 

    There are a few minor errors with your code that I have cleared up, an incorrect GPIO definition and the name for SPI_0 is just "SPI_0". The main issue was that SPI0 and UART0 use the same resources so you have to disable UART0 for SPI0 to work, so I removed CONFIG_UART_0_NRF_UARTE=y. Since Zephyr's logging uses UART 0, you have to disable logging in your sample (CONFIG_SERIAL=n) as well. And since you're building for the non-secure board and therefore using SPM, you need to add this to the SPM sample too. 

    So when trying out the .zip, make sure to go into ncs/nrf/samples/nrf9160/spm/prj.conf and add CONFIG_SERIAL=n first.

    hello_world_248016_DZ.zip

    Best regards,

    Heidi

Related