<?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>SPI communication on Thingy:91 board</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/58078/spi-communication-on-thingy-91-board</link><description>Hello guys! 
 We want to configure ADXL362 accelerometer on Thingy:91 board to wake up nRF9160 SiP via INT1 external interrupt. For that purpose, we need to properly configure ADXL362. Consequently SPI driver is needed to access ADXL362 internal registers</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Tue, 25 Feb 2020 08:05:18 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/58078/spi-communication-on-thingy-91-board" /><item><title>RE: SPI communication on Thingy:91 board</title><link>https://devzone.nordicsemi.com/thread/236146?ContentTypeID=1</link><pubDate>Tue, 25 Feb 2020 08:05:18 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:bf0d1af0-10af-4575-94a1-dc182b4ec474</guid><dc:creator>bojan</dc:creator><description>&lt;p&gt;OK, guys, I think I figured out how to send/receive data over SPIM. It is indeed necessary to have a single transmit buffer (&lt;span style="background-color:#ff00ff;"&gt;&lt;strong&gt;&lt;em&gt;tx_buf&lt;/em&gt;&lt;/strong&gt;&lt;/span&gt;) that will contain command byte(s), address byte(s) as well as data to write (if there are some) as well as &lt;span style="color:#ff0000;"&gt;&lt;strong&gt;a single&lt;/strong&gt;&lt;/span&gt; receive buffer (&lt;span style="background-color:#ff00ff;"&gt;&lt;strong&gt;&lt;em&gt;rx_buf&lt;/em&gt;&lt;/strong&gt;&lt;/span&gt;) that will contain data read from SPI device. Here is how I accessed single register of ADXL362 device:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;int read_adxl362(struct device *spi, const struct spi_config *spi_cfg,
                 u8_t addr, u8_t *data, u32_t num_bytes){
    
    int err = 0;

    /* read cmd */
    err = adxl362_access(spi, spi_cfg,
                           ADXL362_READ_CMD, addr, data, num_bytes);
    if (err) {
            printk(&amp;quot;Error during SPI read\n&amp;quot;);
            return -EIO;
    }

    return 0;       
}

int write_adxl362(struct device *spi, const struct spi_config *spi_cfg,
                  u8_t addr, u8_t *data, u32_t num_bytes){

    int err = 0;
    /* write cmd */
    err = adxl362_access(spi, spi_cfg,
                           ADXL362_WRITE_CMD, addr, data, num_bytes);
    if (err) {
            printk(&amp;quot;Error during SPI write\n&amp;quot;);
            return -ENXIO;
    }

    return 0;
}

int adxl362_access(struct device *spi, const struct spi_config *spi_cfg,
			    u8_t cmd, u8_t addr, void *data, size_t len)
{

	int err;
	static u8_t tx_buffer[2];
	//static u8_t rx_buffer[1];
        tx_buffer[0] = cmd;
        tx_buffer[1] = addr;

	const struct spi_buf tx_buf = {
		.buf = tx_buffer,
		.len = sizeof(tx_buffer)
	};
	const struct spi_buf_set tx = {
		.buffers = &amp;amp;tx_buf,
		.count = 1
	};

	struct spi_buf rx_buf = {
		.buf = data,
		.len = len + 2,
	};
	const struct spi_buf_set rx = {
		.buffers = &amp;amp;rx_buf,
		.count = 1
	};

	err = spi_transceive(spi_dev, spi_cfg, &amp;amp;tx, &amp;amp;rx);
	if (err) {
		printk(&amp;quot;SPI error: %d\n&amp;quot;, err);
	}

        return err;
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;According to Nordic&amp;#39;s documentation, SPI master is a synchronous interface and for every byte that is sent, a different byte will be received at the same time. That is why we should look the content of the register read into &lt;strong&gt;&lt;em&gt;rx_buf[2]&lt;/em&gt;&lt;/strong&gt;. &lt;strong&gt;&lt;em&gt;rx_buf[0]&lt;/em&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;em&gt;rx_buf[1]&lt;/em&gt;&lt;/strong&gt; store some data received while sending command and address bytes (&lt;strong&gt;&lt;em&gt;tx_buf[0]&lt;/em&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;em&gt;tx_buf[1]&lt;/em&gt;&lt;/strong&gt;, respectively).&lt;/p&gt;
&lt;p&gt;Cheers!&lt;/p&gt;
&lt;p&gt;Bojan.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>