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

Basic SPI reads and writes

Hi,

I have a SPI peripheral connected to the nRF960 DK. In order to access this correctly, I need to be able to write a byte to a specific address first and then read back from specific addresses. 

I can run the basic SPI example

https://github.com/Rallare/fw-nrfconnect-nrf/tree/nrf9160_samples/samples/nrf9160/spi

which carries out a basic loop back. This all works fine but I can't see how to write a specific byte to a specific address or read a specific address.

So, ideally I would like to write 0xD2 to address 0x80, wait 1 second and then read data from address 0x00.

Can you point me to a simple example please?

Ta,
Rod

Parents
  • Hello Rod, 

    The SPI example you are using is the only one we have at the moment. The example is using the built-in SPI functions of the Zephyr project, perhaps you are able to find what you are looking for in the Zephyr samples? Also, have a look at the Zephyr SPI API and the SPI header file.

    Ref. similar answer given by my colleague Jan Tore here

    Kind regards,
    Øyvind

  • Hi Øyvind,

    I went back to the reply from Jan Tore on the ticket here. I realised that the BME280 has both an I2C and an SPI interface. So, I thought if I can get this sensor working over SPI and I can then try and migrate it to my own SPI sensor.

    I have successfully connected and ran the BME280 sensor over I2C. I am now trying to access this over SPI. 

    Im using the sample given here, 

    https://github.com/zephyrproject-rtos/zephyr/blob/master/samples/sensor/bme280/src/main.c

    I have set up SPI in my overlay file as suggested by Jan in the other ticket.

    /*
     *
     * Copyright (c) 2018 Linaro Limited
     *
     * SPDX-License-Identifier: Apache-2.0
     */
    
    
    &i2c2 {
            status = "ok";
            sda-pin = <1>;
            scl-pin = <0>;
            clock-frequency = <I2C_BITRATE_FAST>;
            };
    
    &spi3 {
            status = "ok";
    	sck-pin = <10>;
    	mosi-pin = <11>;
    	miso-pin = <12>;
    	ss-pin = <13>;
    	spi-max-frequency = <8000000>;
    	bme280@0 {
    		compatible = "bosch,bme280";
    		reg = <0>;
    		spi-max-frequency = <8000000>;
    		label = "BME280";
    	};
    
    };
    

    and also I have enabled SPI in my conf file

    CONFIG_CONSOLE_SUBSYS=y
    CONFIG_CONSOLE_GETCHAR=y
    CONFIG_GPIO=y
    CONFIG_STDOUT_CONSOLE=y
    
    CONFIG_SERIAL=y
    CONFIG_STDOUT_CONSOLE=y
    CONFIG_UART_INTERRUPT_DRIVEN=y
    CONFIG_TEST_RANDOM_GENERATOR=y
    
    CONFIG_I2C=y
    CONFIG_SENSOR=y
    
    CONFIG_NET_BUF_USER_DATA_SIZE=1
    
    CONFIG_LOG=n
    CONFIG_LOG_DEFAULT_LEVEL=4
    CONFIG_HEAP_MEM_POOL_SIZE=1024
    CONFIG_MAIN_STACK_SIZE=4096
    CONFIG_NEWLIB_LIBC=y
    
    # SPI
    CONFIG_SPI=y
    CONFIG_SPI_3=y
    CONFIG_SPI_3_NRF_SPIM=y
    CONFIG_BME280=y
    CONFIG_SPI_NRFX=y

    However, I get an error

    Secure Boot: MSP_NS 200216b0

    Secure Boot: prepare to jump to Non-Secure image

    ***** Booting Zephyr OS v1.13.99-ncs1-5561-gde69d2df908f *****

    Exception occurred in Secure State

    ***** HARD FAULT *****

      Fault escalation (see below)

    ***** BUS FAULT *****

      Precise data bus error

      BFAR Address: 0x50008120

    ***** Hardware exception *****

    Current thread ID = 0x20020154

    Faulting instruction address = 0x41f98

    Fatal fault in ISR! Spinning...

    Any thoughts?

  • Hi Jan,

    Just wanted to nudge you on this again :-) I have now written a simple SPI program which I wish to test. When I run this, I am reading zeros back. However, I am still using "secure_boot" and have not yet migrated to "spm". I am hoping that a move to SPM will help.

    Please can you clarify the steps to build my test program, with SPM merged and flash my board. Is it as simple as the code below?

    west build -b nrf9160_pca10090ns -d build
    west flash -d build

    Thanks,
    Rod 

  • Hi Rod, 

    Please can you clarify the steps to build my test program, with SPM merged and flash my board. Is it as simple as the code below?

    Using the latest version of NCS, (v0.4.0), just remember to set zephyr environment first. After that, you should be able to run these commands and program your board.
    Edit: See more information here.

    Kind regards, 
    Øyvind

     

      

  • Hi Øyvind,

    Sorry, I just want to check I her the latest tools. Im running everyone from a command line 

    So, to update to the latest version of NCS, is it just this....

    cd ncs
    git fetch ncs
    west update 

    You say I need to set the zephyr environment first, is this necessary? This should be set already?

    Rod

  • Please read "Updating the repositories"

    Your NCS folder should be similar to this:

    ncs
     |___ .west
     |___ mcuboot
     |___ nrf
     |___ nrfxlib
     |___ zephyr

    Enter the following in command line:

    cd ncs/nrf
    git checkout v0.4.0
    west update

     

    RodWatt said:
    You say I need to set the zephyr environment first, is this necessary? This should be set already?

    If you have added this path to your environmental variables, then ignore that message. 

  • Ok, I have updated NCS as above and build my code using

    west build -b nrf9160_pca10090ns -d build
    west flash -d build

    I was expecting this to create a merged hex file but is only seems to create the hex for my application. 

    I can confirm this by looking at the memory map.

     What am I missing here?

Reply Children
  • Yes, it should look like this:

    I think that you may have disabled Automatic Building of SPM, please check you prj.conf that you have:

    CONFIG_SPM=y

    Let me know if that helps. 

    -Øyvind

  • Hi Øyvind

    Yes, I saw that I thought that may be it but when I add this to my prj.conf I get an error

    Watts-MacBook-Pro:build Watt$ cmake -GNinja -DBOARD=nrf9160_pca10090ns  ..
    Zephyr version: 1.14.0
    -- Selected BOARD nrf9160_pca10090ns
    -- Found west: /usr/local/bin/west (found suitable version "0.5.7", minimum required is "0.5.4")
    -- Loading /Users/Watt/zephyrproject/ncs/zephyr/boards/arm/nrf9160_pca10090/nrf9160_pca10090ns.dts as base
    -- Overlaying /Users/Watt/zephyrproject/ncs/zephyr/dts/common/common.dts
    -- Overlaying /Users/Watt/Documents/Switch_That/Nordic_Semi_Code/SPI_Test/nrf9160_pca10090ns.overlay
    nrf9160_pca10090ns.dts.pre.tmp:252.19-258.3: Warning (unique_unit_address_if_enabled): /soc/peripheral@40000000/clock@5000: duplicate unit-address (also used in node /soc/peripheral@40000000/power@5000)
    Parsing Kconfig tree in /Users/Watt/zephyrproject/ncs/zephyr/Kconfig
    Loading /Users/Watt/zephyrproject/ncs/zephyr/boards/arm/nrf9160_pca10090/nrf9160_pca10090ns_defconfig as base
    Merging prj.conf
    
    warning: LOG_DEFAULT_LEVEL (defined at subsys/logging/Kconfig:126) was assigned the value '4' but
    got the value ''. You can check symbol information (including dependencies) in the 'menuconfig'
    interface (see the Application Development Primer section of the manual), or in the Kconfig
    reference at http://docs.zephyrproject.org/latest/reference/kconfig/CONFIG_LOG_DEFAULT_LEVEL.html
    (which is updated regularly from the master branch). See the 'Setting configuration values' section
    of the Board Porting Guide as well.
    
    prj.conf:2: warning: attempt to assign the value 'y' to the undefined symbol SPM
    
    warning: LOG_RUNTIME_FILTERING (defined at subsys/logging/Kconfig:120) has direct dependencies LOG with value n, but is currently being y-selected by the following symbols:
     - SHELL (defined at subsys/shell/Kconfig:13), with value y, direct dependencies "y" (value: y)
    
    Error: Aborting due to non-whitelisted Kconfig warning 'prj.conf:2: warning: attempt to assign the
    value 'y' to the undefined symbol SPM'. Note: If this warning doesn't point to an actual problem,
    you can add it to the whitelist at the top of
    /Users/Watt/zephyrproject/ncs/zephyr/scripts/kconfig/kconfig.py.
    
    CMake Error at /Users/Watt/zephyrproject/ncs/zephyr/cmake/kconfig.cmake:191 (message):
      command failed with return code: 1
    Call Stack (most recent call first):
      /Users/Watt/zephyrproject/ncs/zephyr/cmake/app/boilerplate.cmake:397 (include)
      CMakeLists.txt:3 (include)
    
    
    -- Configuring incomplete, errors occurred!
    Watts-MacBook-Pro:build Watt$ 
    

  • OK, there is something missing here. 

    Please do the following:

    cd ncs/nrf
    git checkout master
    git pull

    Now verify that you have the correct tag:

    git tag
    
    Outputs:
    v0.1.0
    v0.1.0-rc1
    v0.3.0
    v0.3.0-rc1
    v0.3.0-rc2
    v0.4.0
    v0.4.0-rc1
    v0.4.0-rc2

    From the output, we see that tag v0.4.0 is available. We can now check out this tag:

    git checkout v0.4.0
    
    Outputs:
    Note: checking out 'v0.4.0'.
    
    You are in 'detached HEAD' state. You can look around, make experimental
    changes and commit them, and you can discard any commits you make in this
    state without impacting any branches by performing another checkout.
    
    If you want to create a new branch to retain commits you create, you may
    do so (now or later) by using -b with the checkout command again. Example:
    
      git checkout -b <new-branch-name>
    
    HEAD is now at f925102 manifest: Updated manifest to use D4 release tags.

    Then we can update using west:

    west update

    This should fix the issues you have seen, building SPM and the sample project in one. Can't remember if I wrote this before, but SES v4.16 does not support debugging a multi-image .hex file i.e. merged.hex. You have to program it manually. The workaround is explained here. We are working on a fix.

  • Hi Øyvind,

    Ok, I will try this later when I have some time, although I think I may have done this previously, I will go through the steps as you have detailed just to check. If this doesn't work, I will carry out  clean install. What's the easiest way of doing a complete clean install from a command line?

  • RodWatt said:
    Ok, I will try this later when I have some time, although I think I may have done this previously, I will go through the steps as you have detailed just to check

    Yes, please do. I'm concerned that you do not have the latest version.

    RodWatt said:
    What's the easiest way of doing a complete clean install from a command line?

    The easiest would be to do the following: 

    mkdir NCS && cd NCS
    west init -m https://github.com/NordicPlayground/fw-nrfconnect-nrf --mr v0.4.0
    west update
    

Related