nRF5340 SPI example code

Hi,

could you provide me an example codes for SPI (serial peripheral communication), SPIM(serial peripheral communication master) and SPIS(serial peripheral communication slave) for nRF5340 SoC.

Thanks & Regards,

Srinivas.

  • Hi,

    You can find SPI master and slave examples in this post. I have tested the master example on nRF5340, and it needed a small change to build, see this post. I assume a similar change would be needed for the slave example.

    Best regards,
    Jørgen

  • I tried to compile code nrfx_spi_master.zip . but I got compilation errors as shown below

    C:\Nordic>west build -d C:\sample\spi_master\build_nrf5340dk_nrf5340_cpuapp --board nrf5340dk_nrf5340_cpuapp C:\sample\spi_master
    -- west build: generating a build system
    -- Application: C:/sample/spi_master
    -- Using NCS Toolchain 1.5.0 for building. (C:/Nordic/v1.5.0/toolchain/cmake)
    -- Zephyr version: 2.4.99 (C:/Nordic/v1.5.0/zephyr)
    -- Found Python3: C:/Nordic/v1.5.0/toolchain/opt/bin/python.exe (found suitable exact version "3.8.2") found components: Interpreter
    -- Found west (found suitable version "0.9.0", minimum required is "0.7.1")
    -- Board: nrf5340dk_nrf5340_cpuapp
    -- Cache files will be written to: C:/Nordic/v1.5.0/zephyr/.cache
    -- Found dtc: C:/Nordic/v1.5.0/toolchain/opt/bin/dtc.exe (found suitable version "1.4.7", minimum required is "1.4.6")
    -- Found toolchain: gnuarmemb (C:/Nordic/v1.5.0/toolchain/opt)
    -- Found BOARD.dts: C:/Nordic/v1.5.0/zephyr/boards/arm/nrf5340dk_nrf5340/nrf5340dk_nrf5340_cpuapp.dts
    -- Generated zephyr.dts: C:/sample/spi_master/build_nrf5340dk_nrf5340_cpuapp/zephyr/zephyr.dts
    -- Generated devicetree_unfixed.h: C:/sample/spi_master/build_nrf5340dk_nrf5340_cpuapp/zephyr/include/generated/devicetree_unfixed.h
    -- Generated device_extern.h: C:/sample/spi_master/build_nrf5340dk_nrf5340_cpuapp/zephyr/include/generated/device_extern.h
    Parsing C:/Nordic/v1.5.0/zephyr/Kconfig
    Loaded configuration 'C:/Nordic/v1.5.0/zephyr/boards/arm/nrf5340dk_nrf5340/nrf5340dk_nrf5340_cpuapp_defconfig'
    Merged configuration 'C:/sample/spi_master/prj.conf'
    Configuration saved to 'C:/sample/spi_master/build_nrf5340dk_nrf5340_cpuapp/zephyr/.config'
    Kconfig header saved to 'C:/sample/spi_master/build_nrf5340dk_nrf5340_cpuapp/zephyr/include/generated/autoconf.h'
    -- The C compiler identification is GNU 9.2.1
    -- The CXX compiler identification is GNU 9.2.1
    -- The ASM compiler identification is GNU
    -- Found assembler: C:/Nordic/v1.5.0/toolchain/opt/bin/arm-none-eabi-gcc.exe
    -- Configuring done
    -- Generating done
    -- Build files have been written to: C:/sample/spi_master/build_nrf5340dk_nrf5340_cpuapp
    -- west build: building application
    [7/148] Building C object CMakeFiles/app.dir/src/main.c.obj
    FAILED: CMakeFiles/app.dir/src/main.c.obj
    C:\Nordic\v1.5.0\toolchain\opt\bin\arm-none-eabi-gcc.exe -DBUILD_VERSION=v2.4.99-ncs1 -DKERNEL -DNRF5340_XXAA_APPLICATION -DUSE_PARTITION_MANAGER=0 -D_FORTIFY_SOURCE=2 -D__PROGRAM_START -D__ZEPHYR__=1 -IC:/Nordic/v1.5.0/zephyr/include -Izephyr/include/generated -IC:/Nordic/v1.5.0/zephyr/soc/arm/nordic_nrf/nrf53 -IC:/Nordic/v1.5.0/nrf/include -IC:/Nordic/v1.5.0/modules/hal/cmsis/CMSIS/Core/Include -IC:/Nordic/v1.5.0/modules/hal/nordic/nrfx -IC:/Nordic/v1.5.0/modules/hal/nordic/nrfx/drivers/include -IC:/Nordic/v1.5.0/modules/hal/nordic/nrfx/mdk -IC:/Nordic/v1.5.0/zephyr/modules/hal_nordic/nrfx/. -isystem C:/Nordic/v1.5.0/zephyr/lib/libc/minimal/include -isystem c:/nordic/v1.5.0/toolchain/opt/bin/../lib/gcc/arm-none-eabi/9.2.1/include -isystem c:/nordic/v1.5.0/toolchain/opt/bin/../lib/gcc/arm-none-eabi/9.2.1/include-fixed -isystem C:/Nordic/v1.5.0/nrfxlib/crypto/nrf_cc312_platform/include -Os -imacros C:/sample/spi_master/build_nrf5340dk_nrf5340_cpuapp/zephyr/include/generated/autoconf.h -ffreestanding -fno-common -g -mcpu=cortex-m33 -mthumb -mabi=aapcs -imacros C:/Nordic/v1.5.0/zephyr/include/toolchain/zephyr_stdint.h -Wall -Wformat -Wformat-security -Wno-format-zero-length -Wno-main -Wno-pointer-sign -Wpointer-arith -Wno-address-of-packed-member -Wno-unused-but-set-variable -Werror=implicit-int -fno-asynchronous-unwind-tables -fno-pie -fno-pic -fno-strict-overflow -fno-reorder-functions -fno-defer-pop -fmacro-prefix-map=C:/sample/spi_master=CMAKE_SOURCE_DIR -fmacro-prefix-map=C:/Nordic/v1.5.0/zephyr=ZEPHYR_BASE -fmacro-prefix-map=C:/Nordic/v1.5.0=WEST_TOPDIR -ffunction-sections -fdata-sections -std=c99 -nostdinc -MD -MT CMakeFiles/app.dir/src/main.c.obj -MF CMakeFiles\app.dir\src\main.c.obj.d -o CMakeFiles/app.dir/src/main.c.obj -c ../src/main.c
    In file included from C:/Nordic/v1.5.0/zephyr/include/arch/arm/aarch32/irq.h:20,
    from C:/Nordic/v1.5.0/zephyr/include/arch/arm/aarch32/arch.h:27,
    from C:/Nordic/v1.5.0/zephyr/include/arch/cpu.h:19,
    from C:/Nordic/v1.5.0/zephyr/include/kernel_includes.h:33,
    from C:/Nordic/v1.5.0/zephyr/include/kernel.h:17,
    from C:/Nordic/v1.5.0/zephyr/include/zephyr.h:18,
    from ../src/main.c:2:
    ../src/main.c: In function 'manual_isr_setup':
    ../src/main.c:39:21: error: 'SPIM1_SPIS1_TWIM1_TWIS1_SPI1_TWI1_IRQn' undeclared (first use in this function); did you mean 'SPIM1_SPIS1_TWIM1_TWIS1_UARTE1_IRQn'?
    39 | IRQ_DIRECT_CONNECT(SPIM1_SPIS1_TWIM1_TWIS1_SPI1_TWI1_IRQn, 0,
    | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    C:/Nordic/v1.5.0/zephyr/include/sw_isr_table.h:71:5: note: in definition of macro 'Z_ISR_DECLARE'
    71 | {irq, flags, (void *)&func, (const void *)param}
    | ^~~
    C:/Nordic/v1.5.0/zephyr/include/irq.h:112:2: note: in expansion of macro 'ARCH_IRQ_DIRECT_CONNECT'
    112 | ARCH_IRQ_DIRECT_CONNECT(irq_p, priority_p, isr_p, flags_p)
    | ^~~~~~~~~~~~~~~~~~~~~~~
    ../src/main.c:39:2: note: in expansion of macro 'IRQ_DIRECT_CONNECT'
    39 | IRQ_DIRECT_CONNECT(SPIM1_SPIS1_TWIM1_TWIS1_SPI1_TWI1_IRQn, 0,
    | ^~~~~~~~~~~~~~~~~~
    ../src/main.c:39:21: note: each undeclared identifier is reported only once for each function it appears in
    39 | IRQ_DIRECT_CONNECT(SPIM1_SPIS1_TWIM1_TWIS1_SPI1_TWI1_IRQn, 0,
    | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    C:/Nordic/v1.5.0/zephyr/include/sw_isr_table.h:71:5: note: in definition of macro 'Z_ISR_DECLARE'
    71 | {irq, flags, (void *)&func, (const void *)param}
    | ^~~
    C:/Nordic/v1.5.0/zephyr/include/irq.h:112:2: note: in expansion of macro 'ARCH_IRQ_DIRECT_CONNECT'
    112 | ARCH_IRQ_DIRECT_CONNECT(irq_p, priority_p, isr_p, flags_p)
    | ^~~~~~~~~~~~~~~~~~~~~~~
    ../src/main.c:39:2: note: in expansion of macro 'IRQ_DIRECT_CONNECT'
    39 | IRQ_DIRECT_CONNECT(SPIM1_SPIS1_TWIM1_TWIS1_SPI1_TWI1_IRQn, 0,
    | ^~~~~~~~~~~~~~~~~~
    [16/148] Building C object zephyr/arch/arch/arm/core/aarch32/CMakeFiles/arch__arm__core__aarch32.dir/thread.c.obj
    ninja: build stopped: subcommand failed.
    FATAL ERROR: command exited with status 1: 'C:\Nordic\v1.5.0\toolchain\opt\bin\cmake.EXE' --build 'C:\sample\spi_master\build_nrf5340dk_nrf5340_cpuapp'

  • You get that error because you did not add the changes mentioned in the second thread I linked.

  • As per your mentioned , I have added changes . now it's compiled . i have a one doubt why we need to add separately manual isr setup function. is it implemented in the SPI driver? 

  • No, the driver will only enable the interrupt, you need to connect the handler to the interrupt.

    Note that there is also a built-in SPI driver in Zephyr that you can use, if you do not want to use the nrfx_spim/spis drivers directly.

Related