This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Writing Register problem on Thingy91

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!

Parents
  • I solved it by my own with the help of the timing diagram:

    I have a working code for the read area of the diagram and I need a code that works for the write area of the diagram. I need to expand the buffer and send 3 bytes instead of 2. The working code is:

    void spi_write_reg(void)
    {
      u8_t cmd =      0x0A;
      u8_t reg_addr = 0x20; // register, that you want to write
      u8_t value = 0x46; // value, that you want to set
      void *data = &value;
      int length = 1;
    	u8_t access[3] = { cmd, reg_addr, value };
    
      gpio_pin_write(cs_gpio, cd_pin, 0);
    
    	const struct spi_buf buf[2] = {
    		{
    			.buf = access,
    			.len = 3
    		},
    		{
    			.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);
    
    }

Reply
  • I solved it by my own with the help of the timing diagram:

    I have a working code for the read area of the diagram and I need a code that works for the write area of the diagram. I need to expand the buffer and send 3 bytes instead of 2. The working code is:

    void spi_write_reg(void)
    {
      u8_t cmd =      0x0A;
      u8_t reg_addr = 0x20; // register, that you want to write
      u8_t value = 0x46; // value, that you want to set
      void *data = &value;
      int length = 1;
    	u8_t access[3] = { cmd, reg_addr, value };
    
      gpio_pin_write(cs_gpio, cd_pin, 0);
    
    	const struct spi_buf buf[2] = {
    		{
    			.buf = access,
    			.len = 3
    		},
    		{
    			.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);
    
    }

Children
No Data
Related