Hi, I am working with the adxl362 sensor and the nrf9160 SPI example (https://github.com/Rallare/fw-nrfconnect-nrf/blob/nrf9160_samples/samples/nrf9160/spi/src/main.c) and I am already able to read the register with this code:
void spi_read_reg(void) { u8_t cmd = 0x0B; u8_t reg_addr = 0x20; u8_t value = 0x00; void *data = &value; int length = 1; u8_t access[2] = { cmd, reg_addr }; gpio_pin_write(cs_gpio, cd_pin, 0); const struct spi_buf buf[2] = { { .buf = access, .len = 2 }, { .buf = data, .len = length } }; struct spi_buf_set tx = { .buffers = buf, }; const struct spi_buf_set rx = { .buffers = buf, .count = 2 }; tx.count = 1; int spi_result = spi_transceive(spi_dev, &spi_cfg, &tx, &rx); // printk("SPI result: %d\n",spi_result); printk("Value: 0x%02X\n", value); gpio_pin_write(cs_gpio, cd_pin, 1); }
Now I wanted to try to write the 0x20 (Lower Threshold) with the value 0x0F with the code below.
void spi_test_send(void) { int err; static u8_t tx_buffer[1] = {0x0F}; static u8_t rx_buffer[2]= {0x0A, 0x20}; gpio_pin_write(cs_gpio, cd_pin, 0); 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 }; err = spi_transceive(spi_dev, &spi_cfg, &tx, &rx); if (err) { printk("SPI error: %d\n", err); } else { /* Connect MISO to MOSI for loopback */ printk("TX sent: %x\n", tx_buffer[0]); printk("RX recv: %x\n", rx_buffer[0]); } gpio_pin_write(cs_gpio, cd_pin, 1); }
When I read the register after writing, I get a different value than 0x0F.
I think that these two lines are not correct:
static u8_t tx_buffer[1] = {0x0F};
static u8_t rx_buffer[2]= {0x0A, 0x20};
But I don't know how to fix them.
Do you have any idea what could be the problem or do you have an example code?
Thanks for your help and your answers!