<?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>What are memset() and nrf_driv_spis_buffers_set() doing in the spi_slave example?</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/59310/what-are-memset-and-nrf_driv_spis_buffers_set-doing-in-the-spi_slave-example</link><description>I have two NRF52840 dev kits that are running what are essentially the spi master and spi slave examples. They both work and I&amp;#39;ve altered them slightly. I&amp;#39;m just kind of confused by this block of code in the spi slave example: 
 
 
 Why do I need to reset</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Thu, 26 Mar 2020 19:43:15 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/59310/what-are-memset-and-nrf_driv_spis_buffers_set-doing-in-the-spi_slave-example" /><item><title>RE: What are memset() and nrf_driv_spis_buffers_set() doing in the spi_slave example?</title><link>https://devzone.nordicsemi.com/thread/241968?ContentTypeID=1</link><pubDate>Thu, 26 Mar 2020 19:43:15 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:5d00368d-2595-43e9-8e57-9e24dd0ee6b5</guid><dc:creator>ryerye120</dc:creator><description>&lt;p&gt;So I was hoping to have all of the i/o handling organized neatly in the interrupts so that I wouldn&amp;#39;t need to worry about timing issues while taking SPI packets and sending them over USB. Since I hope to build this into a wireless link (Peripheral&amp;lt;-SPI-&amp;gt; Dev kit &amp;lt;-BLE-&amp;gt; Dev kit &amp;lt;-USB-&amp;gt; host PC), I figured this interrupt-based organization would make my life a little easier. Unfortunately, it seems that may be a bad idea and that I&amp;#39;ll have to leave&amp;nbsp;&lt;em&gt;nrf_drv_spis_buffers_set &lt;/em&gt;in the main&amp;nbsp;inside the &lt;em&gt;if(spi_transfer_done)&lt;/em&gt; scope.&lt;/p&gt;
[quote userid="87869" url="~/f/nordic-q-a/59310/what-are-memset-and-nrf_driv_spis_buffers_set-doing-in-the-spi_slave-example/241378"]Furthermore, I would advice you to use the &lt;em&gt;__WFE()&amp;nbsp;&lt;/em&gt;wait-for-event function, rather than wasting the full cycles in the while(1) loop. Using __WFE will give you considerably lower power consumption.[/quote]
&lt;p&gt;I didn&amp;#39;t realize that! That will be very helpful for the future. Thank you!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: What are memset() and nrf_driv_spis_buffers_set() doing in the spi_slave example?</title><link>https://devzone.nordicsemi.com/thread/241378?ContentTypeID=1</link><pubDate>Tue, 24 Mar 2020 12:59:00 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:c0d94952-1903-449c-9cdf-8748f8e203dc</guid><dc:creator>Karl Ylvisaker</dc:creator><description>&lt;p&gt;Hi Ryan,&lt;br /&gt;&lt;br /&gt;Looking at the SPI code you have provided I dont immediately see where it goes wrong.&lt;br /&gt;You are doing all the right things, but I suspect the order in which you do them is the cause of your problems. In particular, I think&amp;nbsp;&lt;em&gt;&lt;/em&gt;moving&amp;nbsp;&lt;em&gt;nrf_drv_spis_buffers_set&amp;nbsp;&lt;/em&gt;to the event handler might be were it goes wrong, since the callback function can be called before returning from the function.&lt;br /&gt;Is there a particular reason why you would like to do this in your event handler?&lt;br /&gt;&lt;br /&gt;Here is the relevant exempt from the SPIS API Reference documentation:&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&lt;em&gt;When either the memory buffer configuration or the SPI transaction has been completed, the event callback function will be called with the appropriate event&amp;nbsp;&lt;a href="https://infocenter.nordicsemi.com/topic/sdk_nrf5_v16.0.0/group__nrfx__spis.html#ga4cea1060ac3baef70e45d47ed318b0e9"&gt;nrfx_spis_evt_type_t&lt;/a&gt;. The callback function can be called before returning from this function, because it is called from the SPI slave interrupt context.&lt;br /&gt;&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Furthermore, I would advice you to use the &lt;em&gt;__WFE()&amp;nbsp;&lt;/em&gt;wait-for-event function, rather than wasting the full cycles in the while(1) loop. Using __WFE will give you considerably lower power consumption.&lt;br /&gt;&lt;br /&gt;&lt;/p&gt;
[quote user="ryerye120"]As always, thank you. I&amp;#39;ve really appreciated your help.[/quote]
&lt;p&gt;Thank you for saying that! It is no problem at all, I am happy to help! :)&lt;br /&gt;&lt;br /&gt;Best regards,&lt;br /&gt;Karl&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: What are memset() and nrf_driv_spis_buffers_set() doing in the spi_slave example?</title><link>https://devzone.nordicsemi.com/thread/241224?ContentTypeID=1</link><pubDate>Mon, 23 Mar 2020 17:37:21 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:b1880ed9-aad6-4c57-8375-d9772d4a17a6</guid><dc:creator>ryerye120</dc:creator><description>&lt;p&gt;Hi Karl,&lt;/p&gt;
&lt;p&gt;Sorry for not being clear!&lt;/p&gt;
[quote userid="87869" url="~/f/nordic-q-a/59310/what-are-memset-and-nrf_driv_spis_buffers_set-doing-in-the-spi_slave-example/241113"]By this, do you mean that the whole string is empty, or that just the section where spi_packet_buffer should have been is empty?[/quote]
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Just the section where spi_packet_buffer should have been is empty! Everything else I wanted to print was there.&lt;/p&gt;
&lt;p&gt;So I only made minor adjustments to the event handler and the main.c, copied below:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;void spis_event_handler(nrf_drv_spis_event_t event)
{
    if (event.evt_type == NRF_DRV_SPIS_XFER_DONE)
    {
        spis_xfer_done = true;
        spi_packet_buffer = (uint32_t)m_rx_buf;
        NRF_LOG_INFO(&amp;quot; Transfer completed. Received: %s&amp;quot;,spi_packet_buffer);
        bsp_board_led_invert(BSP_BOARD_LED_0);

        memset(m_rx_buf, 0, m_length);
        APP_ERROR_CHECK(nrf_drv_spis_buffers_set(&amp;amp;spis, m_tx_buf, m_length, m_rx_buf, m_length));
    }
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;    while (1)
    {
        if (spis_xfer_done)
        {
        // If you&amp;#39;re in this loop then that means spi interrupt was fired and flag has been set. We&amp;#39;re okay to reset everything and wait for the next spi transfer
        NRF_LOG_FLUSH();
        bsp_board_led_invert(BSP_BOARD_LED_1);
        //memset(m_rx_buf, 0, m_length);
        spis_xfer_done = false;
       //APP_ERROR_CHECK(nrf_drv_spis_buffers_set(&amp;amp;spis, m_tx_buf, m_length, m_rx_buf, m_length));
        }
    }&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;As always, thank you. I&amp;#39;ve really appreciated your help.&lt;/p&gt;
&lt;p&gt;Ryan&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: What are memset() and nrf_driv_spis_buffers_set() doing in the spi_slave example?</title><link>https://devzone.nordicsemi.com/thread/241113?ContentTypeID=1</link><pubDate>Mon, 23 Mar 2020 12:03:30 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:ac9f4885-1bff-4499-b38e-bd707ae2e22e</guid><dc:creator>Karl Ylvisaker</dc:creator><description>&lt;p&gt;Hello Ryan,&lt;/p&gt;
[quote user="ryerye120"]Karl,&amp;nbsp;thank you!!!&amp;nbsp;Also thank you for the links, they are very helpful.[/quote]
&lt;p&gt;No problem at all, I am happy to help! :)&lt;br /&gt;&lt;br /&gt;Looking over your initial post, I see that I failed to explicitly explain memset(), which is good for you to know about.&lt;br /&gt;memset here is used to initialize the buffers to zero. memset directly accesses the specified memory addresses, and sets them to the given values.&lt;/p&gt;
[quote user="ryerye120"]The result is that&amp;nbsp;the slave will print out an empty string over UART.&amp;nbsp;[/quote]
&lt;p&gt;By this, do you mean that the whole string is empty, or that just the section where spi_packet_buffer should have been is empty?&lt;br /&gt;Could you perhaps share your entire source code? or, is the code snippet you sent all the modifications that you have made to the example? If so, you do not need to share the source code.&lt;br /&gt;&lt;br /&gt;Best regards,&lt;br /&gt;Karl&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: What are memset() and nrf_driv_spis_buffers_set() doing in the spi_slave example?</title><link>https://devzone.nordicsemi.com/thread/240946?ContentTypeID=1</link><pubDate>Fri, 20 Mar 2020 17:44:56 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:5b2db737-faa9-46d9-b9ed-013b21931859</guid><dc:creator>ryerye120</dc:creator><description>&lt;p&gt;Karl,&amp;nbsp;thank you!!!&amp;nbsp;Also thank you for the links, they are very helpful.&lt;/p&gt;
&lt;p&gt;I have another question now hahah. When I place memset() and nrf_driv_spis_buffers_set() inside the spi_event_handler, I properly reset the SPI buffers for the next transfer (the master will receive the proper data), but I also somehow delete the contents of the rx_buffer before I can read it out. My SPI event handler is below:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;void spis_event_handler(nrf_drv_spis_event_t event)
{
    if (event.evt_type == NRF_DRV_SPIS_XFER_DONE)
    {
        spis_xfer_done = true;
        spi_packet_buffer = (uint32_t)m_rx_buf;
        NRF_LOG_INFO(&amp;quot; Transfer completed. Received: %s&amp;quot;,spi_packet_buffer);
        bsp_board_led_invert(BSP_BOARD_LED_0);

        memset(m_rx_buf, 0, m_length);
        APP_ERROR_CHECK(nrf_drv_spis_buffers_set(&amp;amp;spis, m_tx_buf, m_length, m_rx_buf, m_length));
    }
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;The result is that&amp;nbsp;the slave will print out an empty string over UART.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Kindest regards,&lt;/p&gt;
&lt;p&gt;Ryan&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: What are memset() and nrf_driv_spis_buffers_set() doing in the spi_slave example?</title><link>https://devzone.nordicsemi.com/thread/240847?ContentTypeID=1</link><pubDate>Fri, 20 Mar 2020 11:57:00 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:7b902269-68c9-4102-a66a-4947dcd8cce2</guid><dc:creator>Karl Ylvisaker</dc:creator><description>&lt;p&gt;Hello,&lt;/p&gt;
[quote user=""]Sorry if these are dumb questions, I appreciate all/any help someone can give me.[/quote]
&lt;p&gt;No need to apologize, these are not dumb questions at all! I am happy to help.&lt;/p&gt;
[quote user=""]Why do I need to reset the rx_buf&amp;nbsp;after each transaction? Why can&amp;#39;t I have the master keep shifting in data without the slave running memset() and nrf_driv_spis_buffers_set()?[/quote]
&lt;p&gt;As you can see in the SPI Slave example documentation, it uses the &lt;a href="https://infocenter.nordicsemi.com/topic/sdk_nrf5_v16.0.0/group__nrf__drv__spis.html"&gt;SPIS Legacy driver&lt;/a&gt;. If we look at the&amp;nbsp;&lt;em&gt;nrf_drv_spis_buffers_set()&lt;/em&gt; function description in the API reference, you will see that it is a macro forwarding to the &lt;a href="https://infocenter.nordicsemi.com/index.jsp?topic=%2Fsdk_nrf5_v16.0.0%2Fgroup__nrfx__spis.html&amp;amp;anchor=ga785f8f780f59f36a5a62b24edb3a0c8f"&gt;nrfx_spis_buffers_set()&lt;/a&gt;&amp;nbsp;function. Reading the function documentation, we see that it prepares the SPIS for the next transfer.&lt;br /&gt;&lt;br /&gt;&lt;/p&gt;
[quote user=""]Also just to confirm my understanding of how this works,&amp;nbsp; when a spi transaction comes in, CS goes low, data gets shifted into some buffer in memory. Then CS goes high and an interrupt is triggered (in the slave). When this interrupt is triggered, a handler will read out the data and do whatever I tell it to do with it. Is this using the easyDMA?[/quote]
&lt;p&gt;In this particular case, CS is active low. This is the usual case, and valid for nRF slave devices, but be aware that there also exists CS active high devices.&lt;br /&gt;You are correct in your understanding of an SPI transaction. I am not sure what you mean by handler will read out the data, but the important thing is that the data is handled during the interrupt service routine, in which you could do whatever you want with said data.&lt;br /&gt;You can read more about SPIS with easyDMA and its usage &lt;a href="https://infocenter.nordicsemi.com/index.jsp?topic=%2Fps_nrf52840%2Fspis.html"&gt;here&lt;/a&gt;&amp;nbsp;and &lt;a href="https://infocenter.nordicsemi.com/index.jsp?topic=%2Fps_nrf52840%2Feasydma.html"&gt;here&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;Do not hesitate to come back if you should have any more questions! :)&lt;br /&gt;&lt;br /&gt;Best regards,&lt;br /&gt;Karl&lt;br /&gt;&lt;br /&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>