my master is outputting all the right signals on the SPI lines. The slave though, although it appears ot recognize the CSN pin and runs the spi_tranceieve function. It does not actually read the MOSI line. I am using 2x nRF52850DK boards. and using the nrf52840dk_nrf52840.dts file and wrapping it with an overlay. i got 2 print command that show it waits at the spi_transceive line until it sees a csn trigger. but it never actually fills the rx buffer.
master overlay
&pinctrl {
spi1_default: spi1_default {
group1 {
psels = <NRF_PSEL(SPIM_SCK, 0, 31)>,
<NRF_PSEL(SPIM_MOSI, 0, 30)>,
<NRF_PSEL(SPIM_MISO, 0, 10)>;
};
};
spi1_sleep: spi1_sleep {
group1 {
psels = <NRF_PSEL(SPIM_SCK, 0, 31)>,
<NRF_PSEL(SPIM_MOSI, 0, 30)>,
<NRF_PSEL(SPIM_MISO, 0, 10)>;
low-power-enable;
};
};
};
&spi1 {
compatible = "nordic,nrf-spim";
pinctrl-0 = <&spi1_default>;
pinctrl-1 = <&spi1_sleep>;
pinctrl-names = "default", "sleep";
cs-gpios = <&gpio0 4 GPIO_ACTIVE_LOW>;
};
master SPI output command
int spi_write_cmd(uint8_t cmd){
int err;
int incr = 0;
tx_buffer[0] = cmd;
tx_buffer[1] = 0x33;
tx_buffer[2] = 0xff;
err = spi_transceive(spi_dev, &spi_cfg, &tx, &rx);
if(err > 0){
while(incr < MAX_BUFFER_SIZE){
printk("0x%X", rx_buffer[incr]);
incr++;
}
printk("\n");
}
return err;
}
slave overlay
&pinctrl {
spi2_default: spi2_default {
group1 {
psels = <NRF_PSEL(SPIS_SCK, 0, 10)>,
<NRF_PSEL(SPIS_MOSI, 0, 21)>,
<NRF_PSEL(SPIS_MISO, 0, 23)>,
<NRF_PSEL(SPIS_CSN, 0, 16)>;
};
};
spi2_sleep: spi2_sleep {
group1 {
psels = <NRF_PSEL(SPIS_SCK, 0, 10)>,
<NRF_PSEL(SPIS_MOSI, 0, 21)>,
<NRF_PSEL(SPIS_MISO, 0, 23)>,
<NRF_PSEL(SPIS_CSN, 0, 16)>;
low-power-enable;
};
};
};
&spi2 {
compatible = "nordic,nrf-spis";
status = "okay";
pinctrl-0 = <&spi2_default>;
/delete-property/ pinctrl-1;
pinctrl-names = "default";
def-char = <0x33>;
};
slave read function
int spis_transceive(void){
int cnt = 0;
uint8_t incr = 0;
printk("pre trans_spi\n");
cnt = spi_transceive(spis_dev, &spis_cfg, &spis_tx, &spis_rx);
printk("post spi_trans cnt: %d\n", cnt);
if(cnt > 0){
while(incr < SPI_BUFFER_SIZE){
printk("0x%X", spis_rx_buffer[incr]);
incr++;
}
printk("\n");
}
return cnt;
slave output: every time i send a command over SPI it registers the the CSN pin activity, but the spi_transceieve function does not do anything or fill the buffer.
post spi_trans cnt: 0 pre trans_spi post spi_trans cnt: 0 pre trans_spi post spi_trans cnt: 0 pre trans_spi
complete slave code
#include "spis.h"
#include <zephyr.h>
#include <sys/printk.h>
#include <drivers/spi.h>
#include <device.h>
#define SPIS_NODE DT_NODELABEL(spi2)
extern void spis_thread(void *, void *, void *);
K_THREAD_DEFINE(spis_tid, SPIS_STACK_SIZE, spis_thread, NULL, NULL, NULL, SPIS_PRIORITY, 0, 0);
struct spis_cb_t spi_cb;
static uint8_t spis_tx_buffer[SPI_BUFFER_SIZE] = "<*>slave out default message buf";
static uint8_t spis_rx_buffer[SPI_BUFFER_SIZE];
static struct spi_config spis_cfg = {
.operation = SPI_WORD_SET(8) | SPI_TRANSFER_MSB |
SPI_OP_MODE_SLAVE
| SPI_MODE_CPOL | SPI_MODE_CPHA,
.frequency = 125000,
.slave = 1,
//.cs = &spi_cs,
};
const struct spi_buf spis_tx_buf = {
.buf = spis_tx_buffer,
.len = sizeof(spis_tx_buffer)
};
const struct spi_buf_set spis_tx = {
.buffers = &spis_tx_buf,
.count = 1
};
const struct spi_buf spis_rx_buf = {
.buf = spis_rx_buffer,
.len = sizeof(spis_rx_buffer),
};
const struct spi_buf_set spis_rx = {
.buffers = &spis_rx_buf,
.count = 1
};
const struct device *spis_dev;
uint8_t spis_init(void)
{
int err;
spis_tx_buffer[0] = 0xff;
spis_tx_buffer[1] = 0x35;
spis_dev = device_get_binding(DT_LABEL(SPIS_NODE));
if (spis_dev == NULL) {
printk("Could not get %s device\n", DT_LABEL(SPIS_NODE));
return 1;
}
printk("SPI Device: %s\n", DT_LABEL(SPIS_NODE));
return 0;
}
int spis_transceive(void){
int cnt = 0;
uint8_t incr = 0;
printk("pre trans_spi\n");
cnt = spi_transceive(spis_dev, &spis_cfg, &spis_tx, &spis_rx);
printk("post spi_trans cnt: %d\n", cnt);
if(cnt > 0){
while(incr < SPI_BUFFER_SIZE){
printk("0x%X", spis_rx_buffer[incr]);
incr++;
}
printk("\n");
}
return cnt;
}
extern void spis_thread(void *unused1, void *unused2, void *unused3){
int err;
err = spis_init();
if(err) return;
if(!device_is_ready(spis_dev)) return;
printk("SPIS thread begin\n");
while (1) {
err = spis_transceive();
if(err < 0) break;
}
printk("SPIS thread end\n");
}