nRF5 SDK is not maintained anymore
More Info: Consider nRF Connect SDK for new designs

issues with implementing SPI slave on nrf9160DK

using nrf connect sdk 1.4.0 and nrf9160 DK

build: nrf9160dk_nrf9160ns

SES version: V5.10d

Windows 10 Pro 64bit

1. Please note I used https_client project to implement spi slave(but have removed all the https code.. ignore the cert file)

2. Used some details from tickets this and this.. Please trust me when I say i have searched the devzone and documents as much as I could

proj.conf looks like this

# SPI
CONFIG_SPI=y
## for slave only
CONFIG_SPI_SLAVE=y
CONFIG_SPI_3_OP_MODES=3

#commented these out as per some devzone discussions
#CONFIG_SPI_3=y
#CONFIG_SPI_NRFX=y
#CONFIG_SPI_3_NRF_SPIS=y #this gives an error while opening a project if uncommented


CONFIG_RESET_ON_FATAL_ERROR=n

overlay file

&spi3 {
compatible = "nordic,nrf-spis";
status = "okay";
mosi-pin = <10>;
miso-pin = <11>;
sck-pin = <12>;
csn-pin = <13>;
def-char = <0xde>;
};

main.c

/*
 * Copyright (c) 2020 Nordic Semiconductor ASA
 *
 * SPDX-License-Identifier: LicenseRef-BSD-5-Clause-Nordic
 */

#include <string.h>
#include <zephyr.h>
#include <stdlib.h>
#include <drivers/spi.h>
#include <nrfx.h>
#include <nrfx_uarte.h>
#include "nrfx_spis.h"

#define PIN_SCK 10
#define PIN_MOSI 11
#define PIN_MISO 12
#define PIN_CSN 13
#define SPIS_NR 3

nrfx_spis_t spis_t = NRFX_SPIS_INSTANCE(SPIS_NR);
nrfx_spis_config_t spis_config_t = NRFX_SPIS_DEFAULT_CONFIG(PIN_SCK,PIN_MOSI,PIN_MISO,PIN_CSN);

uint8_t rx_buffer[2] = {0x0};
uint8_t tx_buffer[2] = {0xa5};

void spis_event_handler_t(nrfx_spis_evt_t const *p_event, void *p_context){
    printk("handler\n");
    int err;
    switch(p_event->evt_type){
        case NRFX_SPIS_XFER_DONE:
                printk("received %x, %x\n",rx_buffer[0], rx_buffer[1]);
            err = nrfx_spis_buffers_set(&spis_t, tx_buffer, sizeof(tx_buffer), rx_buffer, sizeof(rx_buffer));
            if(err != NRFX_SUCCESS){
                printk("Error with setting.\n");
            }
            break;
        case NRFX_SPIS_BUFFERS_SET_DONE:
            printk("buffers set\n");
            break;
        case NRFX_SPIS_EVT_TYPE_MAX:
        
            break;
        default:
            ;
    }
}

static void manual_isr_setup()
{
    //IRQ_DIRECT_CONNECT(SPIM3_SPIS3_TWIM3_TWIS3_UARTE3_IRQn, 0, nrfx_spis_3_irq_handler, 0);
    //irq_enable(SPIM3_SPIS3_TWIM3_TWIS3_UARTE3_IRQn);
    IRQ_DIRECT_CONNECT(UARTE3_SPIM3_SPIS3_TWIM3_TWIS3_IRQn, 0, nrfx_spis_3_irq_handler, 0);
    irq_enable(UARTE3_SPIM3_SPIS3_TWIM3_TWIS3_IRQn);    
}

void init_spis(){
    int err;

    //change mode
    spis_config_t.mode = NRF_SPIS_MODE_3;
    err = nrfx_spis_init(&spis_t,&spis_config_t,spis_event_handler_t,NULL);
    if(err != NRFX_SUCCESS){
        printk("nrfx_spis_init - Error. %x\n",err);
    } else {
        printk("SPIS started.\n");
    }
}


void spi_slave_test(void) {
    int err;
    init_spis();
    manual_isr_setup();

    err = nrfx_spis_buffers_set(&spis_t, tx_buffer, sizeof(tx_buffer), rx_buffer, sizeof(rx_buffer));
    if(err != NRFX_SUCCESS){
        printk("nrfx_spis_buffers_set - Error. %x\n", err);
    }
}


void main(void)
{
	printk("SPIS sample started \n\r");
        spi_slave_test();
        while (1) {
          //printk("alive \n");
          k_sleep(Z_TIMEOUT_TICKS(50000));
        }
}


Notes:

I have closed the solution and opened after changing proj.conf. I am using SPIS3

Issue is that nrfx_spis_init fails with error 0xbad0005.. and the reason for that is this line in nrfx_spi.c fails

    if (p_cb->state != NRFX_DRV_STATE_UNINITIALIZED)

I am just confused to see that p_cb->state is set to NRFX_DRV_STATE for some reason
m_cb[p_instance->drv_inst_idx] has valid data... that array has ONLY ONE element (given only spi3 is available)  and drv_inst_idx=0;

It is clear that I a missing some configuration.. I was told to make some spis related changes using the SES ->project->configure nrf sdk project.. I tried to enable spis2 instance and spis3 driver and that made no difference.. I am sure you have some document in there somewhere in your ocean of documents explaining this but unfortunately I couldn't find any details

3. The SPI master worked without any issue.. Not that it helps but just FYI

4. attached the entire project in zip file

Parents Reply Children
No Data
Related