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

rpi uses spi to communicate with nrf52840 and missed transmission problem

I am using raspberry pi to communicate with nrf52840
My rpi is used as msater, nrf52840 is used as slave
My nrf52840 is implemented as SPI slave under zephyr OS
At present, I found that when rpi uses spi to transfer data to nrf52840, there will be a problem that the transfer is not successful
I wonder if the frequency of my nrf52840 SPI and rpi SPI is not the same
I want to ask what is the SPI default frequency of nrf52840?

I can't find how much it is.
I only see the initialization function, but there is no place to set the frequency

nrfx_spis_init(&spis_t,&spis_config_t,spis_event_handler_t,NULL);

So I want to change the frequency of my RPi SPI

The current settings of my rpi are as follows

spi=spidev.SpiDev()
spi.open()
spi.max_speed_hz=976000

Or how can I set the SPI slave frequency of my nrf52840 to 976000?

My nrf52840 SPI slave on Zephyr OS is set as follows

#include <zephyr.h>
#include <sys/printk.h>
#include "nrfx_spis.h"

#define PIN_SCK 29
#define PIN_MOSI 31
#define PIN_MISO 30
#define PIN_CSN 28
#define SPIS_NR 0

#include <devicetree.h>
#include <drivers/gpio.h>
#define LED0_NODE DT_ALIAS(led2)
#define LED0	DT_GPIO_LABEL(LED0_NODE, gpios)
#define PIN	3
#define FLAGS	DT_GPIO_FLAGS(LED0_NODE, gpios)
const struct device *led;

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 readend=0;

uint8_t rx_buffer[8];
uint8_t tx_buffer[8];
uint8_t tmpbuffer[8] = {0};
void spis_event_handler_t(nrfx_spis_evt_t const *p_event, void *p_context){
    //printk("received\n");
    int err;

    switch(p_event->evt_type){
        case NRFX_SPIS_XFER_DONE:
		if(rx_buffer[0] && rx_buffer[0]!= 255)
		{	
			printk("read");
			for(int i=0;i<8;i++){
				printk("%d ",rx_buffer[i]);
			}
			printk("\r\n");	
		}
            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");
            }
		readend=1;

            break;

        default:
            ;
    }
	
}

static void manual_isr_setup()
{
    IRQ_DIRECT_CONNECT(SPIM0_SPIS0_TWIM0_TWIS0_SPI0_TWI0_IRQn, 0, nrfx_spis_0_irq_handler, 0);
    irq_enable(SPIM0_SPIS0_TWIM0_TWIS0_SPI0_TWI0_IRQn);
}

void init_spis(){
    int err;
    err = nrfx_spis_init(&spis_t,&spis_config_t,spis_event_handler_t,NULL);
    if(err != NRFX_SUCCESS){
        printk("Error with init.\n");
    } else {
        printk("SPIS started.\n");
    }
}

void main(void)
{
    int err,k=0,ret;
    init_spis();

    bool low = true;
    bool high =! low;
	led = device_get_binding(LED0);
	if (led == NULL) {
		return;
	}

	ret = gpio_pin_configure(led, PIN, GPIO_OUTPUT_ACTIVE | FLAGS);
	if (ret < 0) {
		return;
	}
        gpio_pin_set(led, PIN, (int)low);

    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("Error with setting.\n");
    }
	

	while(true){

		if(readend==1){
			gpio_pin_set(led, PIN, (int)low);
			tx_buffer[0] = 5;
			tx_buffer[1] = 24;//message from

			printk("write ");
			for(int i = 0 ; i< 8 ; i++)
				tx_buffer[i] = k+i;
			k++;
			
			for(int i = 0 ; i< 8 ; i++)
				printk("%d ",tx_buffer[i]);
			printk("\r\n");

			gpio_pin_set(led, PIN, (int)high);	
			gpio_pin_set(led, PIN, (int)low);

			readend=0;	


		}
	



	}


}

Thank,

Po-I

Related