This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

NRF9160 interfacing with LR1110 over SPI3

Hi,

I'm trying to interface LR1110 (Dev Board) with NRF9160 DK using SPI. I have been working on this for a couple of weeks, explored almost the whole devzone, learned a lot, you guys are wonderful.

Semtech provides the driver for LR1110 here.

I am trying to write lr1110_hal.c for SPI communication but not getting a response from LR1110, when I tried to connect LR1110 board with Arduino, Arduino is getting response. 

I observed the SPI signals on oscilloscope, SCK is giving perfect clock, MOSI is sending Data, but Chip Select is not toggling with SPI sends. Please check my codes and configurations for some Error, secondly I'm getting ERROR: RFX_ERROR_INVALID_STATE in nrfx_spim_init() call.

Below is my nrf9160dk_nrf9160ns.overlay file

&spi3 {
	compatible = "nordic,nrf-spim";
	status = "okay";
	sck-pin = <13>;
	mosi-pin = <11>;
	miso-pin = <12>;
	cs-gpios = <&gpio0 21 1>;
	clock-frequency = <4000000>;
};

This is my prj.conf

CONFIG_TRUSTED_EXECUTION_NONSECURE=y

# General config
CONFIG_NEWLIB_LIBC=y
CONFIG_NEWLIB_LIBC_FLOAT_PRINTF=y
CONFIG_ASSERT=y
CONFIG_REBOOT=y
CONFIG_RESET_ON_FATAL_ERROR=n

# Logging
CONFIG_UART_CONSOLE=y
CONFIG_LOG=y
CONFIG_LOG_IMMEDIATE=y

# DK - Used for buttons and LEDs in UI module.
CONFIG_DK_LIBRARY_INVERT_LEDS=n

CONFIG_SHELL=y

CONFIG_SERIAL=y
CONFIG_GPIO=y

# SPI
CONFIG_SPI=y
CONFIG_SPI_3=y
CONFIG_SPI_NRFX=y
CONFIG_NRFX_SPIM=y
CONFIG_NRFX_SPIM3=y

Below is my lr1110_hal.c file

#include <zephyr.h>
#include <device.h>
#include <logging/log.h>
#include <nrfx_spim.h>
#include <drivers/nrfx_errors.h>
#include "lr1110_hal.h"

#define LR1110_NODE			DT_ALIAS(lr1110alias)
#define SPIM_DRV_NAME			"SPI_3"

#define NRFX_CUSTOM_ERROR_CODES 0 //used in nrfx_errors.h

#define NRFX_SPIM_SCK_PIN  (13)
#define NRFX_SPIM_MOSI_PIN (11)
#define NRFX_SPIM_MISO_PIN (12)
#define NRFX_SPIM_SS_PIN   (21)

static uint8_t       m_tx_buf[10] = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 };           /**< TX buffer. */
static uint8_t       m_rx_buf[100];  /**< RX buffer. */
static const uint8_t m_length = sizeof(m_tx_buf);        /**< Transfer length. */

#define SPI_INSTANCE  3 
static const nrfx_spim_t spi = NRFX_SPIM_INSTANCE(SPI_INSTANCE);
#define SPIM1_IRQn UARTE3_SPIM3_SPIS3_TWIM3_TWIS3_IRQn
#define ISR_PRIORITY 5

static volatile bool spi_xfer_done = true;

void spim_event_handler(nrfx_spim_evt_t const * p_event, void * p_context)
{
   spi_xfer_done = true;
   if (m_rx_buf[0] != 0)
   {
       printk("Received in hex: ");
		for (int i = 0; i < strlen((const char *)m_rx_buf); i++)
			printk("%x ", m_rx_buf[i]);
		printk("\n");
		printk("ascii: %s\n", m_rx_buf);
   }	
}

void setup_spim(void)
{
   	nrfx_spim_config_t spi_config = 	NRFX_SPIM_DEFAULT_CONFIG(
		NRFX_SPIM_SCK_PIN, 
		NRFX_SPIM_MOSI_PIN,
		NRFX_SPIM_MISO_PIN,
		NRFX_SPIM_SS_PIN);
   	printk("Setting SPI\n");
	//uint32_t err = nrfx_spim_init(&spi, &spi_config, spim_event_handler, NULL);
	uint32_t err = nrfx_spim_init(&spi, &spi_config, NULL, NULL);
	if (err == NRFX_ERROR_INVALID_STATE) {
		printk("nrfx INIT NRFX_ERROR_INVALID_STATE\n");
		return;
	}
	if (err == NRFX_ERROR_BUSY) {
		printk("nrfx INIT NRFX_ERROR_BUSY\n");
		return;
	}
	if (err == NRFX_ERROR_NOT_SUPPORTED) {
		printk("nrfx INIT NRFX_ERROR_NOT_SUPPORTED\n");
		return;
	}
	if (err != NRFX_SUCCESS) {
		printk("nrfx INIT err: %d\n", err);
		return;
	}
   	printk("SPI SET\n");
	/* Zephyr IRQ wrapper, corresponds to NVIC_* functions */
	//IRQ_CONNECT(SPIM1_IRQn, ISR_PRIORITY, nrfx_spim_3_irq_handler, NULL, 0);
}

void test_SPIM(void)
{
	memset(m_rx_buf, 0, m_length);
	memset(m_tx_buf, 0, m_length);
	m_tx_buf[0] = 0x01;
	m_tx_buf[1] = 0x01;
	m_tx_buf[2] = 0x00;
	m_tx_buf[3] = 0x00;
	m_tx_buf[4] = 0x00;
	m_tx_buf[5] = 0x00;
	m_tx_buf[6] = 0x00;


	nrfx_spim_xfer_desc_t xfer_desc = NRFX_SPIM_XFER_TRX(m_tx_buf, 7, m_rx_buf, 7);
	memset(m_rx_buf, 0, m_length);
	uint32_t ret = nrfx_spim_xfer(&spi, &xfer_desc, 0);
	for (int i = 0; i < 7; i++)
		printk("%x ", m_rx_buf[i]);
	printk("\n");
}

lr1110_hal_status_t lr1110_hal_reset( const void* context ) 
{
	return LR1110_HAL_STATUS_OK;
}

lr1110_hal_status_t lr1110_hal_wakeup( const void* context ) 
{
	return LR1110_HAL_STATUS_OK;
}

lr1110_hal_status_t lr1110_hal_write( const void* context, const uint8_t* command, const uint16_t command_length,
                                      const uint8_t* data, const uint16_t data_length )
{
	nrfx_spim_xfer_desc_t xfer_desc1 = NRFX_SPIM_XFER_TRX(command, command_length, m_rx_buf, command_length);
	uint32_t ret1 = nrfx_spim_xfer(&spi, &xfer_desc1, 0);
    nrfx_spim_xfer_desc_t xfer_desc2 = NRFX_SPIM_XFER_TRX(data, data_length, m_rx_buf, data_length);
	uint32_t ret2 = nrfx_spim_xfer(&spi, &xfer_desc2, 0);
	return LR1110_HAL_STATUS_OK;
}

lr1110_hal_status_t lr1110_hal_read( const void* context, const uint8_t* command, const uint16_t command_length,
                                     uint8_t* data, const uint16_t data_length )
{
	nrfx_spim_xfer_desc_t xfer_desc1 = NRFX_SPIM_XFER_TRX(command, command_length, m_rx_buf, command_length);
	uint32_t ret1 = nrfx_spim_xfer(&spi, &xfer_desc1, 0);
	// if (ret1 != NRFX_ERROR_BUSY) --> To be added using while
    nrfx_spim_xfer_desc_t xfer_desc2 = NRFX_SPIM_XFER_TRX(LR1110_NOP , 1, m_rx_buf, 1);
	uint32_t ret2 = nrfx_spim_xfer(&spi, &xfer_desc2, 0);

	nrfx_spim_xfer_desc_t xfer_desc3 = NRFX_SPIM_XFER_TRX(LR1110_NOP , data_length, data, data_length);
	uint32_t ret3 = nrfx_spim_xfer(&spi, &xfer_desc3, 0);
	return LR1110_HAL_STATUS_OK;
}

lr1110_hal_status_t lr1110_hal_direct_read( const void* context, uint8_t* data, const uint16_t data_length )
{
	// if (ret1 != NRFX_ERROR_BUSY) --> To be added using while
	nrfx_spim_xfer_desc_t xfer_desc1 = NRFX_SPIM_XFER_TRX(LR1110_NOP , data_length, data, data_length);
	uint32_t ret1 = nrfx_spim_xfer(&spi, &xfer_desc1, 0);
	return LR1110_HAL_STATUS_OK;
}

Below is my main.c

#include <zephyr.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/printk.h>
#include <devicetree.h>
#include <drivers/gpio.h>
#include "lr1110_hal.h"
#include "lr1110_wifi.h"


lr1110_hal_status_t lr_status;

void main(void)
{
    printk("SPIM Example\n");	
	
	setup_spim();
		
	while (1) {
		test_SPIM();
		k_sleep(K_MSEC(1000));
	}
}

Any help would be really appreciated. Thanks in advance

--

Regards

Parents Reply Children
Related