<?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>How to use spi_write_dt</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/106798/how-to-use-spi_write_dt</link><description>Hello, 
 I am attempting to use the function spi_write_dt to write data (configuration files and lots of data) to a specific register. is there a better function for SPI that would do what I need it to? 
 This is my read function and I think it is working</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Mon, 08 Jan 2024 15:36:33 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/106798/how-to-use-spi_write_dt" /><item><title>RE: How to use spi_write_dt</title><link>https://devzone.nordicsemi.com/thread/463196?ContentTypeID=1</link><pubDate>Mon, 08 Jan 2024 15:36:33 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:037839d8-7cd4-462c-8dec-56f0acf5766e</guid><dc:creator>jbeh444</dc:creator><description>&lt;p&gt;Thank you for all the help!! I think I understand this function well enough to use it for our needs!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to use spi_write_dt</title><link>https://devzone.nordicsemi.com/thread/462844?ContentTypeID=1</link><pubDate>Fri, 05 Jan 2024 09:42:31 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:2b8ccdcd-596d-49a8-aa6a-7a660c8593fe</guid><dc:creator>Einar Thorsrud</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;This looks sensible to me. If it does not wok, I would consider modifying your custom board to get the signals out so that you can look at them (or use a DK and connect the sensor to it and test your code there if that is easier).&lt;/p&gt;
&lt;p&gt;Regarding reviewing hardware, you can make a private ticket &lt;a href="https://devzone.nordicsemi.com/support/add"&gt;here&lt;/a&gt;&amp;nbsp;(select &amp;quot;PRICATE Tech Support&amp;quot;) and upload your schematic and layout files (in gerber format) for review.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to use spi_write_dt</title><link>https://devzone.nordicsemi.com/thread/462569?ContentTypeID=1</link><pubDate>Wed, 03 Jan 2024 16:19:11 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:f5747a6e-00be-4ec8-ae90-a2b58e88a962</guid><dc:creator>jbeh444</dc:creator><description>&lt;p&gt;Hello,&lt;/p&gt;
&lt;p&gt;This was very helpful! I have been trying to wrap my head around buffers and you have help me understand them better! The SPI MOSI on this custom board is impossible to access as there were no connections made to access the pins (sorry, I know you asked about it twice and I forgot to mention it).&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Here is the updated code:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;static uint8_t buffer[5025]; //declared globaly

BMA4_INTF_RET_TYPE bma4_spi_write(uint8_t reg_addr, const uint8_t *reg_data, uint32_t len, void *intf_ptr)
{    
    
    buffer[0] = reg_addr;
    memcpy(&amp;amp;buffer[1], &amp;amp;reg_data, len);    
   
    /*This SPI attempts to put the register address in front of the data buffer */
    struct spi_buf tx = {
    .buf = buffer,.len = len+1        
    };

    const struct spi_buf_set tx_bufs = {
    .buffers = &amp;amp;tx,.count = 1
    };

    rslt= spi_write_dt(&amp;amp;dev_spi, &amp;amp;tx_bufs);          
    return rslt;
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;On a side note, I was hoping to review a custom board design with Nordic. We are using the NPM1100 and the nRF52832-CIAA. Do I just ask about it on a private chat over the DevZone?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to use spi_write_dt</title><link>https://devzone.nordicsemi.com/thread/462552?ContentTypeID=1</link><pubDate>Wed, 03 Jan 2024 15:03:21 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:098c807e-2961-419e-ade4-88659af0bfcb</guid><dc:creator>Einar Thorsrud</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;Yes, this is more or less what I was thinking. With one important exception that your declare a buffer of variable length, and it is also on the stack. So the first line here should probably be something like this instead:&lt;/p&gt;
&lt;p&gt;&lt;code&gt;static uint8_t buffer[BMA4_MAX_LEN]; //&amp;nbsp;&lt;span&gt;BMA4_MAX_LEN&amp;nbsp;should have a value that is larger than you will ever need&lt;/span&gt;&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Does this not work? If not, why? How does the Tx (MOSI) signal look and how is it compared to what you would expect?&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to use spi_write_dt</title><link>https://devzone.nordicsemi.com/thread/462381?ContentTypeID=1</link><pubDate>Tue, 02 Jan 2024 23:04:01 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:4fa868c7-22de-42dd-9894-07fcaaf92999</guid><dc:creator>jbeh444</dc:creator><description>&lt;p&gt;Hello,&lt;/p&gt;
&lt;p&gt;Thank you for getting back to me. Your insight and help is much appreciated. My biggest issue is trying to understand how to use the buffers in the way you described. Here is another attempt and any help would be very appreciated!&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;  uint8_t buffer[len +1];
    buffer[0] = reg_addr;
    memcpy(&amp;amp;buffer[1], &amp;amp;reg_data, len);    
   
    /*This SPI attempts to put the register address in front of the data buffer */
    struct spi_buf tx[] = {
        {.buf = buffer,.len = len+1}, 
        //{.buf = (uint8_t *)reg_data,.len = len}, 
    };

    const struct spi_buf_set tx_bufs = {
    .buffers = tx,.count = 1
    };

    rslt= spi_write_dt(&amp;amp;dev_spi, &amp;amp;tx_bufs);          
    return rslt;&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to use spi_write_dt</title><link>https://devzone.nordicsemi.com/thread/462318?ContentTypeID=1</link><pubDate>Tue, 02 Jan 2024 14:09:20 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:ead0a63f-8972-4200-aff4-f4beac1acdf8</guid><dc:creator>Einar Thorsrud</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;I see burst write is supported by the&amp;nbsp;&lt;span&gt;BMA456&amp;nbsp;for the initialization data, then you write the 1 byte address and the multi byte data. I am not sure why yo split this up in two Tx buffers, though. This is one transaction, so I would make a single buffer and copy the data in to the first possition (index 0), and the data to the next position (index 1 and so on in case you have more data for a burst write). Other than that I don&amp;#39;t see any problems.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;In what way does it not work? If you look at the data on the SPI line with a logic analyzer, how does it differ from what you would have expected?&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to use spi_write_dt</title><link>https://devzone.nordicsemi.com/thread/462159?ContentTypeID=1</link><pubDate>Sun, 31 Dec 2023 22:35:41 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:6ad016c7-4316-4f1d-9c0a-aeb39afe26b8</guid><dc:creator>jbeh444</dc:creator><description>&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;BMA4_INTF_RET_TYPE bma4_spi_write(uint8_t reg_addr, const uint8_t *reg_data, uint32_t len, void *intf_ptr)
{    
    struct spi_buf tx[2];
    tx[0].buf = &amp;amp;reg_addr;
    tx[0].len = 1;  
    tx[1].buf = (uint8_t *)reg_data;
    tx[1].len = len;        
   
    const struct spi_buf_set tx_bufs = {
        .buffers = tx,
        .count = ARRAY_SIZE(tx)
    };     
    rslt= spi_write_dt(&amp;amp;dev_spi, &amp;amp;tx_bufs);       
    return rslt;
}&lt;/pre&gt;This seems to almost do what I want it to. I think I may not be addressing the write properly. I would love to just have register address in front of the data buffer, but I can&amp;#39;t seem to wrap my head around the steps to do that.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to use spi_write_dt</title><link>https://devzone.nordicsemi.com/thread/461756?ContentTypeID=1</link><pubDate>Wed, 27 Dec 2023 09:59:16 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:a693ddc3-52f8-4607-809c-62d702198830</guid><dc:creator>&amp;#216;yvind</dc:creator><description>&lt;p&gt;&lt;span&gt;&lt;span dir="ltr"&gt;Hello,&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;span dir="ltr"&gt;We are severely understaffed this week because of the Christmas holidays, and Einar will have to get back to you on this next week. Sorry for the inconvenience.&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;span dir="ltr"&gt;Kind regards,&lt;br /&gt;Øyvind&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to use spi_write_dt</title><link>https://devzone.nordicsemi.com/thread/461641?ContentTypeID=1</link><pubDate>Sat, 23 Dec 2023 23:00:10 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:c018372c-f258-44db-ad0d-b54eacf723d6</guid><dc:creator>jbeh444</dc:creator><description>&lt;p&gt;Hello Einar,&lt;/p&gt;
&lt;p&gt;Thank you for the update. I look forward to hearing back from you after the holidays.&lt;/p&gt;
&lt;p&gt;Here is some additional info for the time being.&lt;/p&gt;
&lt;p&gt;I need the SPI function to write a configuration file approximately 5016 bytes which can be broken up into smaller pieces. This is passed to the write function as a pointer to the file.I can&amp;#39;t wrap my head around how to do that with the spi_buf and spi_buf_set structs.&lt;/p&gt;
&lt;p&gt;I am having trouble understanding the zephyr version spi_write_dt and having changeable sizes of transactions they seem to want the same size buffers for every&amp;nbsp;SPI transaction.&lt;/p&gt;
&lt;p&gt;I have tried to get a different version of SPI to work using the spim examples in the ncs folder, but it ultimately wouldn&amp;#39;t initialize the SPI and seemed like it needed a ton of different files (header and .c) to get working.&lt;/p&gt;
&lt;p&gt;Thanks again,&lt;/p&gt;
&lt;p&gt;Jared&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to use spi_write_dt</title><link>https://devzone.nordicsemi.com/thread/461594?ContentTypeID=1</link><pubDate>Fri, 22 Dec 2023 14:16:56 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:32f5b763-081c-4a5a-9f7e-eef854ea1b8d</guid><dc:creator>Einar Thorsrud</dc:creator><description>&lt;p&gt;Hi Jared,&lt;/p&gt;
&lt;p&gt;I have not had a chance to look at this unfortuantely, and I will be OoO next week. DevZone will also have reduced staffing. That said, I se Bosch has a &lt;a href="https://github.com/boschsensortec/BMA456-Sensor-API"&gt;driver &lt;/a&gt;that you may look at.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to use spi_write_dt</title><link>https://devzone.nordicsemi.com/thread/461490?ContentTypeID=1</link><pubDate>Thu, 21 Dec 2023 16:53:41 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:11d3e2e2-aeaf-411c-9d49-e31ee00e4825</guid><dc:creator>jbeh444</dc:creator><description>&lt;p&gt;Thank you for the great insight! The I2c version worked on this sensor on the dev kit. We moved to a custom board which the pins were switch to an SPI. I am now attempting to correct this by getting the SPI working. The BMA456 uses a burst write to write a config file. To change settings, it does a burst read on the same register. I am trying to understand how to write a file of 5016 bytes and what the limits are on the SPI TX.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;#1&amp;nbsp; I need the read function to be a modifiable buffer size because the number of bytes it reads can vary. The read must be saved to reg_data in the function call as this is what the other functions read to see if the chip_id and register data is correct.&lt;/p&gt;
&lt;p&gt;#2 The write function needs to be a modifiable buffer of various sizes as well.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;I apologize for my lack of understanding about the SPI buffers and pointers to data.&lt;/p&gt;
&lt;p&gt;Here is my latest attempt to modify the code to attempt to initialize the sensor.&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;BMA4_INTF_RET_TYPE bma4_spi_read(uint8_t reg_addr, uint8_t *reg_data, uint32_t len, void *intf_ptr)
{  
    static struct spi_buf rx;
    static struct spi_buf_set rx_bufs = {
    .buffers = &amp;amp;rx,
    .count = 1,
    };

    static struct spi_buf tx;
    static struct spi_buf_set tx_bufs = {
    .buffers = &amp;amp;tx,
    .count = 1,
    }; 
    
    tx.buf = &amp;amp;reg_addr;       
    tx.len = 1;
    rx.buf = reg_data;       
    rx.len = len;   
    rslt= spi_transceive_dt(&amp;amp;dev_spi, &amp;amp;tx_bufs, &amp;amp;rx_bufs);

    return rslt;
}

BMA4_INTF_RET_TYPE bma4_spi_write(uint8_t reg_addr, const uint8_t *reg_data, uint32_t len, void *intf_ptr)
{    
    struct spi_buf tx[len+1];
            
    tx[0].buf = &amp;amp;reg_addr;
    tx[0].len = 1; 
    for(int i = 0; i &amp;lt; len; i++)
    {
        tx[i+1].buf = (uint8_t*)reg_data+i; 
        tx[i+1].len = 1;       
    }      
    struct spi_buf_set tx_bufs = {
        .buffers = tx,
        .count = 1,
    };     
    rslt= spi_write_dt(&amp;amp;dev_spi, &amp;amp;tx_bufs);       
    return rslt;
}&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to use spi_write_dt</title><link>https://devzone.nordicsemi.com/thread/461370?ContentTypeID=1</link><pubDate>Thu, 21 Dec 2023 08:09:48 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:11f827df-8eab-4051-901d-258b98c861b2</guid><dc:creator>Einar Thorsrud</dc:creator><description>&lt;p&gt;Hi Jared,&lt;/p&gt;
[quote user="jbeh444"]I was hoping you could take a look at SPI code in the post and give me some pointers on best practices of how to make sure my data is in the proper format and length. When I create the buffer, it seems to cut off the rest of the data and only shows eight bits.[/quote]
&lt;p&gt;I can take a look. I guess the relevant part is this function;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;BMA4_INTF_RET_TYPE bma4_spi_write(uint8_t reg_addr, const uint8_t *reg_data, uint32_t len, void *intf_ptr)
{    
    uint8_t txbuffer[2] = {reg_addr, *reg_data};
    struct spi_buf tx = {
        .buf = txbuffer,  .len = sizeof(txbuffer),};
    struct spi_buf_set tx_bufs = {
        .buffers = &amp;amp;tx, .count = 1,};
    rslt= spi_write_dt(&amp;amp;dev_spi, &amp;amp;tx_bufs);       
    return rslt;
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Here I see you ignore the len parameter of your function, and always set len in the&amp;nbsp;spi_buf instante to the size of the array, which is 2. And that matches what I saw in the datasheet when I briefely looked yesterday. I did not read it carefully so I might be wrong, but it seemd to me like you always write a 1 byte address and 1 byte data. Then you could add on with another 1 byte address and 1 byte data, and so on. The latter is not supported by your function here.&lt;/p&gt;
&lt;p&gt;If you wanted to do that you would have to buidl a larger array of every second byte addres an data. There should not be anything preventing you from doing that though. Essentially make your&amp;nbsp;&lt;code&gt;txbuffer&lt;/code&gt; larger, populate it properly, and that should be it. (I have no knowledge of the sensor though and did not carefully read the datasheet, so there may be details I am missing).&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to use spi_write_dt</title><link>https://devzone.nordicsemi.com/thread/461329?ContentTypeID=1</link><pubDate>Wed, 20 Dec 2023 16:38:48 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:cfca2d5f-7646-4cec-87ca-95c35742a847</guid><dc:creator>jbeh444</dc:creator><description>&lt;p&gt;Hello Einar,&lt;/p&gt;
&lt;p&gt;Thank you for the quick response!&amp;nbsp;&lt;/p&gt;
&lt;p&gt;I was hoping you could take a look at SPI code in the post and give me some pointers on best practices of how to make sure my data is in the proper format and length. When I create the buffer, it seems to cut off the rest of the data and only shows eight bits.&lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;Jared&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to use spi_write_dt</title><link>https://devzone.nordicsemi.com/thread/461287?ContentTypeID=1</link><pubDate>Wed, 20 Dec 2023 13:31:56 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:0552ced9-7632-42e3-ba4a-311d8c85d95c</guid><dc:creator>Einar Thorsrud</dc:creator><description>&lt;p&gt;Hi Jared,&lt;/p&gt;
&lt;p&gt;SPI just transfers data, and if you are writing to a specific register on the slave device it just still just data seen from the nRF. From what I can see from the BMA400 datasheet under &amp;quot;SPI Interface and protocol&amp;quot;, the address should just be the first byte when you write (then you can add another register and data byte if you like, and so on).&lt;/p&gt;
&lt;p&gt;Einar&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>