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

problems with SPI on nRF9160 and Zephyr

I am trying to develop a SPI interface to a CANBUS controller for the nRF9160 for the zephyr platform.

The issue I am having at this point is is that I am unable to see any activity on the SPI lines from the nRF9160. 

My first test of the SPI is to connect to an FLASH EEPROM that I have jjust to test the SPI connectivity. 

for now I am running without secure_boot and the cellular connection as it simplified testing with the uart.

I have added the lines 

&spi2 {
status = "ok";
sck-pin = <26>;
mosi-pin = <27>;
miso-pin = <28>;
spi-max-frequency = <4000000>;
}; 

to the nrf9160_pca10090.overlay file and 

# General config
CONFIG_NEWLIB_LIBC=y


CONFIG_SERIAL=y
CONFIG_UART_INTERRUPT_DRIVEN=y

CONFIG_GPIO=y

CONFIG_SPI=y
CONFIG_SPI_NRFX=y
CONFIG_SPI_2=y
CONFIG_SPI_2_NRF_SPIM=y

CONFIG_PRINTK=y
CONFIG_HEAP_MEM_POOL_SIZE=256
CONFIG_ASSERT=y

is my prj.conf file

I do get a device back when I device_get_binding("SPI_2").

I have copied and modified some SPI code from the Zephyr samples.  but have not been able to get any response from the SPI pins.

Do you have any SPI samples specifically for the nRF9160

Parents
  • Hi Jeff,

    When working with nrf9160 samples, it has to be built as a non-secure firmware image.
    So that means that you need to add:

    CONFIG_TRUSTED_EXECUTION_NONSECURE=y

    in the prj.conf file for your application. I also recommend that you copy your .overlay file and add it in the secure_boot sample and build/flash that again.

    (I assume that you are already using the nrfx_spi driver)

    We do not have a sample for this yet, but I can see if I can make something for you if the pointers aren't enough.

  • Have you tried anything with the SPI.  I also tryed the SPI under Zephyr on the nRF52840 with no results.

  • Hi Jeff,


    It would be useful to see what's going on in your main.c files. That would make it easiuer to reproduce your setup locally.

  • Hi Jan

    I just took one of the Zephyr samples and started to adapt to see how things work.  Once I got some activity my plan was to make it work with my chip.

    /*
     * Copyright (c) 2016 Intel Corporation
     *
     * SPDX-License-Identifier: Apache-2.0
     */
    
    #include <errno.h>
    #include <zephyr.h>
    #include <misc/printk.h>
    #include <device.h>
    #include <spi.h>
    
    /**
     * @file Sample app using the Fujitsu MB85RS64V FRAM through SPI.
     */
    
    #define MB85RS64V_MANUFACTURER_ID_CMD 0x9f
    #define MB85RS64V_WRITE_ENABLE_CMD 0x06
    #define MB85RS64V_READ_CMD 0x03
    #define MB85RS64V_WRITE_CMD 0x02
    #define MAX_USER_DATA_LENGTH 1024
    
    static u8_t data[MAX_USER_DATA_LENGTH], cmp_data[MAX_USER_DATA_LENGTH];
    
    static int mb85rs64v_access(struct device *spi, struct spi_config *spi_cfg,
    			    u8_t cmd, u16_t addr, void *data, size_t len)
    {
    	u8_t access[3];
    	struct spi_buf bufs[] = {
    		{
    			.buf = access,
    		},
    		{
    			.buf = data,
    			.len = len
    		}
    	};
    	struct spi_buf_set tx = {
    		.buffers = bufs
    	};
    
    	access[0] = cmd;
    
    	if (cmd == MB85RS64V_WRITE_CMD || cmd == MB85RS64V_READ_CMD) {
    		access[1] = (addr >> 8) & 0xFF;
    		access[2] = addr & 0xFF;
    
    		bufs[0].len = 3;
    		tx.count = 2;
    
    		if (cmd == MB85RS64V_READ_CMD) {
    			struct spi_buf_set rx = {
    				.buffers = bufs,
    				.count = 2
    			};
    
    			return spi_transceive(spi, spi_cfg, &tx, &rx);
    		}
    	} else {
    		tx.count = 1;
    	}
    
    	return spi_write(spi, spi_cfg, &tx);
    }
    
    
    static int mb85rs64v_read_id(struct device *spi, struct spi_config *spi_cfg)
    {
    	u8_t id[4];
    	int err;
    
    	err = mb85rs64v_access(spi, spi_cfg,
    			       MB85RS64V_MANUFACTURER_ID_CMD, 0, &id, 4);
    	if (err) {
    		printk("Error during ID read\n");
    		return -EIO;
    	}
    
    	if (id[0] != 0x04) {
    		return -EIO;
    	}
    
    	if (id[1] != 0x7f) {
    		return -EIO;
    	}
    
    	if (id[2] != 0x03) {
    		return -EIO;
    	}
    
    	if (id[3] != 0x02) {
    		return -EIO;
    	}
    
    	return 0;
    }
    
    static int write_bytes(struct device *spi, struct spi_config *spi_cfg,
    		       u16_t addr, u8_t *data, u32_t num_bytes)
    {
    	int err;
    
    	/* disable write protect */
    	err = mb85rs64v_access(spi, spi_cfg,
    			       MB85RS64V_WRITE_ENABLE_CMD, 0, NULL, 0);
    	if (err) {
    		printk("unable to disable write protect\n");
    		return -EIO;
    	}
    
    	/* write cmd */
    	err = mb85rs64v_access(spi, spi_cfg,
    			       MB85RS64V_WRITE_CMD, addr, data, num_bytes);
    	if (err) {
    		printk("Error during SPI write\n");
    		return -EIO;
    	}
    
    	return 0;
    }
    
    static int read_bytes(struct device *spi, struct spi_config *spi_cfg,
    		      u16_t addr, u8_t *data, u32_t num_bytes)
    {
    	int err;
    
    	/* read cmd */
    	err = mb85rs64v_access(spi, spi_cfg,
    			       MB85RS64V_READ_CMD, addr, data, num_bytes);
    	if (err) {
    		printk("Error during SPI read\n");
    		return -EIO;
    	}
    
    	return 0;
    }
    
    void main(void)
    {
    	struct device *spi;
            struct device *gpio_dev;
    	struct spi_config spi_cfg;
            struct spi_cs_control cs_control;
    	int err;
    
    	printk("fujitsu FRAM example application\n");
    
    	spi = device_get_binding("SPI_3");
    	if (!spi) {
    		printk("Could not find SPI 3 driver\n");
    		return;
    	}else{
                    printk("SPI_3 %x\n",spi);
            }
    
            gpio_dev = device_get_binding("GPIO_0");
            if(!gpio_dev) {
                    printk("could not find GPIO device\n");
                    return;
            }
            cs_control.delay = 0;
            cs_control.gpio_dev = gpio_dev;
            cs_control.gpio_pin = 22;
    	spi_cfg.operation = SPI_WORD_SET(8);
    	spi_cfg.frequency = 256000;
            spi_cfg.cs = &cs_control; 
    
    while(1){
    	err = mb85rs64v_read_id(spi, &spi_cfg);
    	if (err) {
    		printk("Could not verify FRAM ID\n");
    		
    	}
               }
    
    	
    }
    

    If it would help I can email you the entire project.  Nothing private in it.

  • Hi Jeff,

    Did you manage to get this working? I am also trying to get the SPI interface up and running. I can run the basic SPI loopback test but am unable to get access to and SPI peripheral. I am trying to get the the BME280 code working, works with I2C but I can't get it to work with SPI. Anyway, any tips you can share would be appreciated. Thanks, Rod 

Reply Children
Related