<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Writing Register problem on Thingy91</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/55697/writing-register-problem-on-thingy91</link><description>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: 
 
 Now I wanted to try</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Mon, 30 Dec 2019 15:26:56 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/55697/writing-register-problem-on-thingy91" /><item><title>RE: Writing Register problem on Thingy91</title><link>https://devzone.nordicsemi.com/thread/227091?ContentTypeID=1</link><pubDate>Mon, 30 Dec 2019 15:26:56 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:0657d4b1-a2c0-44c6-9f84-8ef5781abd8d</guid><dc:creator>Bergmann</dc:creator><description>&lt;p&gt;I solved it by my own with the help of the timing diagram:&lt;/p&gt;
&lt;p&gt;&lt;img src="https://devzone.nordicsemi.com/resized-image/__size/320x240/__key/communityserver-discussions-components-files/4/pastedimage1577719322588v1.png" alt=" " /&gt;&lt;/p&gt;
&lt;p&gt;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:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;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 = &amp;amp;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, &amp;amp;spi_cfg, &amp;amp;tx, &amp;amp;rx);

 // printk(&amp;quot;SPI result: %d\n&amp;quot;,spi_result);
  printk(&amp;quot;Value: 0x%02X\n&amp;quot;, value);

  gpio_pin_write(cs_gpio, cd_pin, 1);

}&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>