Hi,
I'm trying to write a SPI driver base that I can use to interface with different sensors, using the nRF52840DK and NCS v2.0.0.
To test, I'm using a Bosch BMI270. I'm trying to read the Chip_ID from the BMI270 but the responses I get are either 0x00 or 0xFF.
From the BMI270 datasheet:

When reading data from BMI270 using SPI, the first byte read is a dummy byte (0x00) and the next byte should be the Chip_ID (0x24). To get n bytes of meaningful data, you need to read (n + 1) bytes from the device and discard the dummy byte you read.
This is the the SPI driver code I'm using:
//Using SPI1
/*
nRF52840 - BMI270
MOSI (P0.30) - SDI
MISO (P1.08) - SDO
SCK (P0.31) - SCK
CS (P1.07) - CS
*/
#include <zephyr/kernel.h>
#include <zephyr/drivers/gpio.h>
#include <zephyr/drivers/spi.h>
#include <ncs_version.h>
#define CHIP_ID_REG 0x00
#define READ_CMD 0x80
static const struct spi_config spi_cfg = {
.operation = SPI_OP_MODE_MASTER | SPI_WORD_SET(8) | SPI_TRANSFER_MSB |
SPI_MODE_CPOL | SPI_MODE_CPHA,
.frequency = 4000000,
.slave = 0,
};
#define SPI1_NODE DT_NODELABEL(spi1)
static const struct device *spi_dev = DEVICE_DT_GET(SPI1_NODE);
#define MY_GPIO1 DT_NODELABEL(gpio1) //CS
#define GPIO_1_CS 7
const struct device *gpio1_dev = DEVICE_DT_GET(MY_GPIO1);
void spi_test_send(uint8_t reg, uint8_t size)
{
int err;
uint8_t tx_buffer[1];
tx_buffer[0] = READ_CMD | reg;
static uint8_t rx_buffer[2];
for (int i=0; i<sizeof(tx_buffer);i++)
{
rx_buffer[i] = 0x00;
}
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 = size,
};
const struct spi_buf_set rx = {
.buffers = &rx_buf,
.count = 1
};
gpio_pin_set(gpio1_dev, GPIO_1_CS, 0);
err = spi_transceive(spi_dev, &spi_cfg, &tx, &rx);
gpio_pin_set(gpio1_dev, GPIO_1_CS, 1);
if (err) {
printk("SPI error: %d\n", err);
} else {
printk("\r\nTX sent: ");
printk("0x%02x", tx_buffer[0]);
printk("\r\nRX recv 1: ");
printk("0x%02x", rx_buffer[0]);
printk("\r\n");
printk("\r\nRX recv 2: ");
printk("0x%02x", rx_buffer[1]);
printk("\r\n");
}
}
void main(void)
{
gpio_pin_configure(gpio1_dev, GPIO_1_CS, GPIO_OUTPUT);
gpio_pin_set(gpio1_dev, GPIO_1_CS, 1);
spi_test_send(CHIP_ID_REG, 2);
k_sleep(K_MSEC(1000));
}
I've checked the wiring and confirmed the BMI270 is working by testing with a previously written I2C driver, so I believe the problem is with the SPI driver above.
Any help would be really appreciated.
Edit: I updated the code above using the SPI Master example from this answer (https://devzone.nordicsemi.com/f/nordic-q-a/67686/nrf-connect-sdk-spi-communication-is-not-working-on-nrf52840-dev-kit) and did a loopback test on the nRF52840DK, where I can successfully transmit and receive the hex data. I still don't get a response from the BMI270 though.