Hello, I want to use the nRF52820 as a slave device. I've left my code, prj.conf, and DTS files below, but the spi_transceive function seems to be getting stuck. What could be wrong? Is my main code correct? Thank you in advance.
CONFIG_CLOCK_CONTROL=y CONFIG_CLOCK_CONTROL_NRF_K32SRC_SYNTH=y CONFIG_CLOCK_CONTROL_NRF_K32SRC_XTAL=n CONFIG_CLOCK_CONTROL_NRF_K32SRC_RC=n CONFIG_GPIO=y CONFIG_SPI=y CONFIG_SPI_SLAVE=y CONFIG_LOG=y CONFIG_USE_SEGGER_RTT=y CONFIG_LOG_BACKEND_RTT=y CONFIG_LOG_BACKEND_UART=n CONFIG_LOG_DEFAULT_LEVEL=2 CONFIG_LOG_PRINTK=y CONFIG_RTT_CONSOLE=y CONFIG_UART_CONSOLE=n
#include <zephyr/kernel.h>
#include <zephyr/sys/printk.h>
#include <zephyr/drivers/spi.h>
#include <string.h> // memset için
#define SPI_NODE DT_NODELABEL(spi0)
static const struct spi_config spi_cfg = {
.operation = SPI_WORD_SET(8) | SPI_TRANSFER_MSB | SPI_OP_MODE_SLAVE
| SPI_MODE_CPOL | SPI_MODE_CPHA,
.frequency = 800000, // slave'de çoğu zaman dikkate alınmaz ama doldurulmalı
.slave = 0,
};
const struct device *spi_dev;
static void spi_init(void)
{
spi_dev = DEVICE_DT_GET(SPI_NODE);
if (!device_is_ready(spi_dev)) {
printk("SPI device %s not ready!\n", spi_dev->name);
return;
}
printk("SPI device %s ready.\n", spi_dev->name);
}
void spi_test_send(void)
{
int err;
static uint8_t tx_buffer[] = "Hello from AliveCor!";
static uint8_t rx_buffer[sizeof(tx_buffer)] = {0};
// Her transferde RX buffer sıfırlansın
memset(rx_buffer, 0, sizeof(rx_buffer));
const struct spi_buf tx_buf = {
.buf = tx_buffer,
.len = sizeof(tx_buffer)
};
const struct spi_buf_set tx = {
.buffers = &tx_buf,
.count = 1
};
struct spi_buf rx_buf = {
.buf = rx_buffer,
.len = sizeof(rx_buffer),
};
const struct spi_buf_set rx = {
.buffers = &rx_buf,
.count = 1
};
printk("Waiting SPI master...\n");
err = spi_transceive(spi_dev, &spi_cfg, &tx, &rx);
if (err) {
printk("SPI error: %d\n", err);
} else {
printk("TX sent: [");
for (size_t i = 0; i < sizeof(tx_buffer); i++) {
printk("%02X ", tx_buffer[i]);
}
printk("]\n");
printk("RX recv: [");
for (size_t i = 0; i < sizeof(rx_buffer); i++) {
printk("%02X ", rx_buffer[i]);
}
printk("]\n");
}
}
void main(void)
{
printk("SPI SLAVE test, board: %s\n", CONFIG_BOARD);
spi_init();
while (1) {
spi_test_send();
k_sleep(K_MSEC(1000));
}
}
/dts-v1/;
#include <nordic/nrf52820_qdaa.dtsi>
#include "TST-pinctrl.dtsi"
/ {
model = "testing";
compatible = "BY,TST";
chosen {
zephyr,sram = &sram0;
zephyr,flash = &flash0;
zephyr,code-partition = &slot0_partition;
zephyr,console = &cdc_acm_uart0;
zephyr,shell-uart = &cdc_acm_uart0;
zephyr,uart-mcumgr = &cdc_acm_uart0;
};
leds {
compatible = "gpio-leds";
led0: led_0 {
gpios = <&gpio0 28 GPIO_ACTIVE_HIGH>;
label = "Green LED 0";
};
led1: led_1 {
gpios = <&gpio0 30 GPIO_ACTIVE_HIGH>;
label = "Green LED 1";
};
};
aliases {
led0 = &led0;
led1 = &led1;
};
};
&flash0 {
partitions {
compatible = "fixed-partitions";
#address-cells = <1>;
#size-cells = <1>;
boot_partition: partition@0 {
label = "mcuboot";
reg = <0x00000000 DT_SIZE_K(48)>;
};
slot0_partition: partition@c000 {
label = "image-0";
reg = <0x0000c000 DT_SIZE_K(92)>;
};
slot1_partition: partition@23000 {
label = "image-1";
reg = <0x00023000 DT_SIZE_K(92)>;
};
storage_partition: partition@3a000 {
label = "storage";
reg = <0x0003a000 DT_SIZE_K(24)>;
};
};
};
&gpiote {
status = "okay";
};
&gpio0 {
status = "okay";
};
&clock {
status = "okay";
};
zephyr_udc0: &usbd {
compatible = "nordic,nrf-usbd";
status = "okay";
};
&usbd {
cdc_acm_uart0: cdc_acm_uart0 {
compatible = "zephyr,cdc-acm-uart";
label = "CDC_ACM_0";
};
};
&spi1 {
compatible = "nordic,nrf-spi";
status = "okay";
pinctrl-0 = <&spi1_default>;
pinctrl-1 = <&spi1_sleep>;
pinctrl-names = "default", "sleep";
cs-gpios = <&gpio0 2 GPIO_ACTIVE_LOW>;
};
&radio {
status = "okay";
};
&spi0 {
status = "okay";
compatible = "nordic,nrf-spis";
pinctrl-0 = <&spi0_default>;
pinctrl-1 = <&spi0_sleep>;
pinctrl-names = "default", "sleep";
cs-gpios = <&gpio0 14 GPIO_ACTIVE_LOW>;
def-char = <0xFF>;
};
&pinctrl{
spi1_default: spi1_default {
group1 {
psels = <NRF_PSEL(SPIM_SCK, 0, 0)>,
<NRF_PSEL(SPIM_MOSI, 0, 4)>,
<NRF_PSEL(SPIM_MISO, 0, 1)>;
};
};
spi1_sleep: spi1_sleep {
group1 {
psels = <NRF_PSEL(SPIM_SCK, 0, 0)>,
<NRF_PSEL(SPIM_MOSI, 0, 4)>,
<NRF_PSEL(SPIM_MISO, 0, 1)>;
low-power-enable;
};
};
spi0_default: spi0_default {
group1 {
psels = <NRF_PSEL(SPIS_SCK, 0, 5)>,
<NRF_PSEL(SPIS_MOSI, 0, 7)>,
<NRF_PSEL(SPIS_MISO, 0, 6)>;
};
};
spi0_sleep: spi0_sleep {
group1 {
psels = <NRF_PSEL(SPIS_SCK, 0, 5)>,
<NRF_PSEL(SPIS_MOSI, 0, 7)>,
<NRF_PSEL(SPIS_MISO, 0, 6)>;
low-power-enable;
};
};
};