<?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>Cannot retrieve data from BMI270 using SPI on nRF52840</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/108388/cannot-retrieve-data-from-bmi270-using-spi-on-nrf52840</link><description>Hi, 
 I have the following problem. 
 I have a custom board where I have BMI270 connected with SPI. 
 I use the nrfx_spim library. 
 I get error codes as a success when I try to write and read to SPI, however, I always get 0x00. 
 So I assume the problem</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Tue, 20 Feb 2024 12:56:42 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/108388/cannot-retrieve-data-from-bmi270-using-spi-on-nrf52840" /><item><title>RE: Cannot retrieve data from BMI270 using SPI on nRF52840</title><link>https://devzone.nordicsemi.com/thread/469799?ContentTypeID=1</link><pubDate>Tue, 20 Feb 2024 12:56:42 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:b1fd4fc2-3fb5-4d28-9035-6c3b8c69bd84</guid><dc:creator>J&amp;#248;rgen Holmefjord</dc:creator><description>&lt;p&gt;According to the comment in your code, and the &lt;a href="https://www.bosch-sensortec.com/media/boschsensortec/downloads/datasheets/bst-bmi270-ds000.pdf"&gt;BMI270 datasheet&lt;/a&gt;, a read operation should have the 8th bit in the first byte set when reading a register, however, it looks like you have set this to 0x00:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;uint8_t tx_buf[] = {0x00, 0x00}; // 0x80 for read, followed by dummy byte&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;I also think&amp;nbsp;rx_buf[1] is the incorrect location of the response. According to datasheet, there is one dummy byte output on SDO before the response, and as stated previously, rx_buf will also be filled while tx_buf is clocked out. Please check rx_buf[2].&lt;/p&gt;
&lt;p&gt;You might reference the &lt;a href="https://github.com/nrfconnect/sdk-zephyr/blob/v3.4.99-ncs1-2/drivers/sensor/bmi270/bmi270_spi.c#L22-L58"&gt;Zephyr driver&lt;/a&gt; for interfacing this sensor.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Cannot retrieve data from BMI270 using SPI on nRF52840</title><link>https://devzone.nordicsemi.com/thread/469792?ContentTypeID=1</link><pubDate>Tue, 20 Feb 2024 12:31:35 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:342570a0-8e34-4824-8500-89a0178c207d</guid><dc:creator>MatiM</dc:creator><description>&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;#include &amp;lt;zephyr/kernel.h&amp;gt;
#include &amp;quot;SEGGER_RTT.h&amp;quot;
#include &amp;quot;SEGGER_RTT_printf.c&amp;quot;
#include &amp;quot;nrfx_spim.h&amp;quot;

#define SPI_INSTANCE_ID 0

#define SPI_CS_PIN 32

// Create an SPI configuration structure
nrfx_spim_config_t spi_config = NRFX_SPIM_DEFAULT_CONFIG(
    NRF_GPIO_PIN_MAP(0, 24),  // SCK pin
    NRF_GPIO_PIN_MAP(0, 22),  // MOSI pin
    NRF_GPIO_PIN_MAP(0, 20),  // MISO pin
    NRF_GPIO_PIN_MAP(1, 0)    // CS pin
);

nrfx_spim_t spi_instance = NRFX_SPIM_INSTANCE(SPI_INSTANCE_ID);

static uint8_t bmi270_read_register(uint8_t reg_addr) {
    // Assuming the sensor uses single-byte addresses
    uint8_t tx_buf[] = {0x00, 0x00}; // 0x80 for read, followed by dummy byte
    uint8_t rx_buf[4] = {};

    // Prepare transfer
    nrfx_spim_xfer_desc_t xfer_desc_trx = NRFX_SPIM_XFER_TRX(tx_buf, sizeof(tx_buf), rx_buf, sizeof(rx_buf));

    // Transfer
    nrfx_err_t err_code = nrfx_spim_xfer(&amp;amp;spi_instance, &amp;amp;xfer_desc_trx, 0);

    if (err_code != NRFX_SUCCESS) 
    {
        // Initialization failed
        SEGGER_RTT_WriteString(0, &amp;quot;Sending failed!\n&amp;quot;);
        return 1;
    }
    else
    {
        SEGGER_RTT_WriteString(0, &amp;quot;Sending has been done successfully!\n&amp;quot;);

    }

    if (err_code != NRFX_SUCCESS) 
    {
        // Initialization failed
        SEGGER_RTT_WriteString(0, &amp;quot;Reading failed!\n&amp;quot;);
        return -1;
    }
    else
    {
        SEGGER_RTT_WriteString(0, &amp;quot;Reading has been done successfully!\n&amp;quot;);

    }

    // The received data is in rx_buf[1]
    return rx_buf[1];
}

int main(void)
{
        nrfx_err_t err_code = nrfx_spim_init(&amp;amp;spi_instance, &amp;amp;spi_config, NULL, NULL);
        if (err_code != NRFX_SUCCESS) 
        {
            SEGGER_RTT_WriteString(0, &amp;quot;SPI init failed!\n&amp;quot;);
            return -1;
        }
        else
        {
            SEGGER_RTT_WriteString(0, &amp;quot;SPI init performed successfully!\n&amp;quot;);
        }
        
        uint8_t data_buffer_from_SPI = bmi270_read_register(0x00);
        SEGGER_RTT_printf(0, &amp;quot;BMI270 data0: 0x%02X\n&amp;quot;, data_buffer_from_SPI);     
}
&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;spi_read() is not used here, I am now trying just to run a simple request/response, here is the simplified version of my code, just config, and trx usage&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Cannot retrieve data from BMI270 using SPI on nRF52840</title><link>https://devzone.nordicsemi.com/thread/469789?ContentTypeID=1</link><pubDate>Tue, 20 Feb 2024 12:27:24 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:29314837-8555-42c9-9f86-32db40c96dcd</guid><dc:creator>J&amp;#248;rgen Holmefjord</dc:creator><description>[quote user="MatiM"]The TRX option handle completely the CS pin right?[/quote]
&lt;p&gt;Yes, it should handle the toggling of CS pin as long as this is set in the config.&lt;/p&gt;
[quote user="MatiM"]In my case when using it instead of reading and writing separately I get 0x255 all the time[/quote]
&lt;p&gt;I can&amp;#39;t see how the function is used in the attached code. What is the input to&amp;nbsp;spi_read()?&amp;nbsp;NRFX_SPIM_SINGLE_XFER() should not be used, as &lt;a href="https://developer.nordicsemi.com/nRF_Connect_SDK/doc/2.5.1/nrfx/drivers/spim/driver.html#c.NRFX_SPIM_SINGLE_XFER"&gt;stated in the documentation&lt;/a&gt;, this is for internal use only.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Cannot retrieve data from BMI270 using SPI on nRF52840</title><link>https://devzone.nordicsemi.com/thread/469776?ContentTypeID=1</link><pubDate>Tue, 20 Feb 2024 11:57:48 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:118f7e19-d8e6-4b60-97a4-1764d6feb2d3</guid><dc:creator>MatiM</dc:creator><description>&lt;p&gt;The TRX option handle completely the CS pin right?&lt;/p&gt;
&lt;p&gt;In my case when using it instead of reading and writing separately I get 0x255 all the time&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;#include &amp;lt;zephyr/kernel.h&amp;gt;
#include &amp;quot;SEGGER_RTT.h&amp;quot;
#include &amp;quot;SEGGER_RTT_printf.c&amp;quot;
#include &amp;quot;nrfx_spim.h&amp;quot;

//#include &amp;quot;remote_service/remote.h&amp;quot;

// SPI instance index. You can have multiple instances for different peripherals.
#define SPI_INSTANCE_ID 0

#define SPI_CS_PIN 32

// Create an SPI configuration structure
nrfx_spim_config_t spi_config = NRFX_SPIM_DEFAULT_CONFIG(
    NRF_GPIO_PIN_MAP(0, 24),  // SCK pin
    NRF_GPIO_PIN_MAP(0, 22),  // MOSI pin
    NRF_GPIO_PIN_MAP(0, 20),  // MISO pin
    NRF_GPIO_PIN_MAP(1, 0)    // CS pin
);

nrfx_spim_t spi_instance = NRFX_SPIM_INSTANCE(SPI_INSTANCE_ID);

void spi_write(uint8_t *data, size_t length) {
    nrfx_spim_xfer_desc_t xfer_desc = NRFX_SPIM_XFER_TX(data, length);
    nrfx_spim_xfer(&amp;amp;spi_instance, &amp;amp;xfer_desc, 0);
}

void spi_read(uint8_t *command, size_t command_length, uint8_t *response, size_t response_length) {
    nrfx_spim_xfer_desc_t xfer_desc = NRFX_SPIM_XFER_TRX(command, command_length, response, response_length);
    nrfx_spim_xfer(&amp;amp;spi_instance, &amp;amp;xfer_desc, 0);
}

static uint8_t bmi270_read_register(uint8_t reg_addr) {
    // Assuming the sensor uses single-byte addresses
    uint8_t tx_buf[] = {0x00, 0x00}; // 0x80 for read, followed by dummy byte
    uint8_t rx_buf[4] = {};

    // Prepare transfer
    nrfx_spim_xfer_desc_t xfer_desc_tx = NRFX_SPIM_SINGLE_XFER(tx_buf, sizeof(tx_buf), rx_buf, sizeof(rx_buf));
    nrfx_spim_xfer_desc_t xfer_desc_rx = NRFX_SPIM_XFER_RX(rx_buf, sizeof(rx_buf));

    // Transfer
    nrfx_err_t err_code = nrfx_spim_xfer(&amp;amp;spi_instance, &amp;amp;xfer_desc_tx, 0);

    if (err_code != NRFX_SUCCESS) 
    {
        // Initialization failed
        SEGGER_RTT_WriteString(0, &amp;quot;Sending failed!\n&amp;quot;);
        return 1;
    }
    else
    {
        SEGGER_RTT_WriteString(0, &amp;quot;Sending has been done successfully!\n&amp;quot;);

    }

    //k_sleep(K_MSEC(2000));

    //err_code = nrfx_spim_xfer(&amp;amp;spi_instance, &amp;amp;xfer_desc_rx, 0);


    if (err_code != NRFX_SUCCESS) 
    {
        // Initialization failed
        SEGGER_RTT_WriteString(0, &amp;quot;Reading failed!\n&amp;quot;);
        return -1;
    }
    else
    {
        SEGGER_RTT_WriteString(0, &amp;quot;Reading has been done successfully!\n&amp;quot;);

    }

    // The received data is in rx_buf[1]
    return rx_buf[3];
}

int main(void)
{
        nrfx_err_t err_code = nrfx_spim_init(&amp;amp;spi_instance, &amp;amp;spi_config, NULL, NULL);
        if (err_code != NRFX_SUCCESS) 
        {
            // Initialization failed
            SEGGER_RTT_WriteString(0, &amp;quot;SPI init failed!\n&amp;quot;);
            return -1;
        }
        else
        {
            SEGGER_RTT_WriteString(0, &amp;quot;SPI init performed successfully!\n&amp;quot;);

        }
        
        uint8_t data_buffer_from_SPI = bmi270_read_register(0x00);
        //for(int i = 0; i &amp;lt; sizeof(data_buffer_from_SPI); i++)
        //{
            SEGGER_RTT_printf(0, &amp;quot;BMI270 data0: 0x%02X\n&amp;quot;, data_buffer_from_SPI);
        //}
        
}&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Cannot retrieve data from BMI270 using SPI on nRF52840</title><link>https://devzone.nordicsemi.com/thread/469522?ContentTypeID=1</link><pubDate>Mon, 19 Feb 2024 13:09:15 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:96ac2578-2ec5-4f5c-bc60-f641c527cb87</guid><dc:creator>J&amp;#248;rgen Holmefjord</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;I do not know how the sensor you are using works, but note that the CS pin will go high between the TX and RX operations when the transfers are split into two separate operations. You should check the datasheet of the sensor to see if it will discard any received command when CS goes high, or if it can accept TX and RX to occur in separate transfers.&lt;/p&gt;
&lt;p&gt;You can either use &lt;a href="https://developer.nordicsemi.com/nRF_Connect_SDK/doc/2.5.2/nrfx/drivers/spim/driver.html#c.NRFX_SPIM_XFER_TRX"&gt;NRFX_SPIM_XFER_TRX&lt;/a&gt;&lt;span&gt;() to create one transfer for both TX and RX, or control the CS pin manually in application by setting &lt;a href="https://developer.nordicsemi.com/nRF_Connect_SDK/doc/2.5.2/nrfx/drivers/spim/driver.html#c.nrfx_spim_config_t.ss_pin"&gt;ss_pin&lt;/a&gt;&amp;nbsp;to&amp;nbsp;&lt;a href="https://developer.nordicsemi.com/nRF_Connect_SDK/doc/2.5.2/nrfx/drivers/spim/hal.html#group__nrf__spim__hal_1ga818eb2c4164cd8f9422c3a8a0a626e2a"&gt;&lt;span&gt;NRF_SPIM_PIN_NOT_CONNECTED&lt;/span&gt;&lt;/a&gt;&amp;nbsp;in the driver config. Remember that if you use TRX, you should set the RX buffer size to TX length + RX length if you expect response from slave after commands have been written, since SPI RX buffers starts to get filled when TX data is shifted out.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Best regards,&lt;br /&gt;Jørgen&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Cannot retrieve data from BMI270 using SPI on nRF52840</title><link>https://devzone.nordicsemi.com/thread/469429?ContentTypeID=1</link><pubDate>Mon, 19 Feb 2024 09:48:00 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:9e3e3d27-57e2-4cdf-a4ba-6620ed6096ee</guid><dc:creator>MatiM</dc:creator><description>&lt;p&gt;&lt;img height="157" src="https://devzone.nordicsemi.com/resized-image/__size/314x314/__key/communityserver-discussions-components-files/4/pastedimage1708336001907v1.png" width="157" alt=" " /&gt;&lt;/p&gt;
&lt;p&gt;with or without MSB added in the send buffer, this is how the response look like&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>