We are testing SPI communication with the nrf5340 Board as the mater and the nrf52840 board as the slave.
The FW applied to nrf5340 is a FW whose normal operation was confirmed through a communication test with other FWs, so I think there is a problem with nrf52840.
prj.conf is
CONFIG_SPI=y
CONFIG_SPI_SLAVE=y
CONFIG_RTT_CONSOLE=y
CONFIG_PRINTK=y
CONFIG_LOG=y
CONFIG_LOG_BACKEND_RTT=y
CONFIG_MAIN_STACK_SIZE=4096
nrf52840dk_nrf52840.overlay is
&spi1{
status = "disabled";
};
&spi3{
status = "disabled";
};
&qspi{
status = "disabled";
};
my_spi_slave: &spi2 {
compatible = "nordic,nrf-spis";
status = "okay";
pinctrl-0 = <&spi_slave_default>;
pinctrl-1 = <&spi_slave_sleep>;
def-char = 0x00;
pinctrl-names = "default", "sleep";
};
spi_slave_default:&spi2_default {
group1 {
psels = <NRF_PSEL(SPIS_SCK, 0, 19)>,
<NRF_PSEL(SPIS_CSN, 0, 17)>,
<NRF_PSEL(SPIS_MOSI, 0, 20)>,
<NRF_PSEL(SPIS_MISO, 0, 21)>;
};
};
spi_slave_sleep:&spi2_sleep {
group1 {
psels = <NRF_PSEL(SPIS_SCK, 0, 19)>,
<NRF_PSEL(SPIS_CSN, 0, 17)>,
<NRF_PSEL(SPIS_MOSI, 0, 20)>,
<NRF_PSEL(SPIS_MISO, 0, 21)>;
low-power-enable;
};
};
and the operation code is as follows.
#include <zephyr/kernel.h>
#include <zephyr/device.h>
#include <zephyr/devicetree.h>
#include <zephyr/drivers/gpio.h>
#include <zephyr/drivers/spi.h>
#define MY_SPI_SLAVE DT_NODELABEL(my_spi_slave)
// SPI slave functionality
const struct device *spi_slave_dev;
static const struct spi_config spi_slave_cfg = {
.operation = SPI_WORD_SET(8) | SPI_TRANSFER_MSB | SPI_OP_MODE_SLAVE ,
.frequency = 4000000;
.slave = 1;
};
void spi_slave_init(void)
{
spi_slave_dev = DEVICE_DT_GET(MY_SPI_SLAVE);
if(!device_is_ready(spi_slave_dev)) {
printk("SPI slave device not ready!\n");
}
}
static uint8_t slave_rx_buffer[1];
static uint8_t slave_tx_buffer[1] ={2};
int spi_slave_read_test_msg(void)
{
struct spi_buf s_rx_buf = {
.buf = slave_rx_buffer,
.len = sizeof(slave_rx_buffer);
};
const struct spi_buf_set s_rx = {
.buffers = &s_rx_buf,
.count = 1
};
struct spi_buf s_tx_buf = {
.buf = slave_tx_buffer,
.len = sizeof(slave_tx_buffer);
};
const struct spi_buf_set s_tx = {
.buffers = &s_tx_buf,
.count = 1
};
printk("1\n");
int error = spi_read(spi_slave_dev, &spi_slave_cfg, &s_rx);
// int error = spi_transceive(spi_slave_dev, &spi_slave_cfg, &s_tx, &s_rx);
if(error < 0){
printk("SPI slave transceive error: %i\n", error);
return error;
} else {
printk("RX recv: %x\n", slave_rx_buffer[0]);
}
printk("2\n");
printk("RX recv: %d \n", slave_rx_buffer[0]);
return 0;
}
int main(void)
{
spi_slave_init();
while(1)
{
spi_slave_read_test_msg();
k_msleep(10);
}
return 0;
}
At this time, the output is as shown below.
*** Booting nRF Connect SDK v3.5.99-ncs1-1 ***
One
Since "2" is not output, spi_read(spi_slave_dev, &spi_slave_cfg, &s_rx); I think there's a problem with the function, but I'm not sure.
int error = spi_transceive(spi_slave_dev, &spi_slave_cfg, &s_tx, &s_rx); The result is the same even if you use .
For reference, the config of the main function is as follows.
nrfx_spim_config_t config = NRFX_SPIM_DEFAULT_CONFIG(SCK_PIN,
MOSI_PIN,
MISO_PIN;
CS_PIN);