<?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 read/write  SPI</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/32720/how-to-read-write-spi</link><description>hello Nordic team 
 I am using nrf52DK.how to use spi to read /write data .I have seen sdk14.2.0 in folder nRF5_SDK_14.2.0_17b948a\examples\peripheral\spi.</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Tue, 31 Jul 2018 08:19:31 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/32720/how-to-read-write-spi" /><item><title>RE: how to read/write  SPI</title><link>https://devzone.nordicsemi.com/thread/142082?ContentTypeID=1</link><pubDate>Tue, 31 Jul 2018 08:19:31 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:1c6bc469-8adc-468a-ae1b-dfd089c4bf9c</guid><dc:creator>Bj&amp;#248;rn Kvaale</dc:creator><description>&lt;p&gt;&lt;a href="https://devzone.nordicsemi.com/members/hjkim"&gt;HJkim&lt;/a&gt; We have an &lt;a href="http://infocenter.nordicsemi.com/topic/com.nordic.infocenter.sdk5.v15.0.0/spi_master_example.html?cp=4_0_0_4_5_36"&gt;SPI Maste&lt;/a&gt;r &amp;amp; &lt;a href="http://infocenter.nordicsemi.com/topic/com.nordic.infocenter.sdk5.v15.0.0/spi_slave_example.html?cp=4_0_0_4_5_38"&gt;Slave example&lt;/a&gt; in our SDK &amp;amp; good documentation to show how to run the examples. If you have not taken a look at this, it might be useful to start there.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: how to read/write  SPI</title><link>https://devzone.nordicsemi.com/thread/141446?ContentTypeID=1</link><pubDate>Thu, 26 Jul 2018 05:58:35 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:afd957c9-6967-4378-b429-655bef1998fa</guid><dc:creator>HJkim</dc:creator><description>&lt;p&gt;Hi, suprtigr&lt;/p&gt;
&lt;p&gt;I also have LIS3DH and built your code. but I got following msg&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;lt;info&amp;gt; app: Transfer completed.&lt;br /&gt;&amp;lt;info&amp;gt; app: Transfer completed.&lt;br /&gt;&amp;lt;info&amp;gt; app: Received:&lt;br /&gt;&amp;lt;info&amp;gt; app: 80 |. &lt;br /&gt;&amp;lt;info&amp;gt; app: | &lt;br /&gt;&amp;lt;info&amp;gt; app: |&lt;/p&gt;
&lt;p&gt;\&amp;lt;info&amp;gt; app: Transfer completed.&lt;br /&gt;&amp;lt;info&amp;gt; app: Transfer completed.&lt;br /&gt;&amp;lt;info&amp;gt; app: Received:&lt;br /&gt;&amp;lt;info&amp;gt; app: 80 |. &lt;br /&gt;&amp;lt;info&amp;gt; app: | &lt;br /&gt;&amp;lt;info&amp;gt; app: |&lt;/p&gt;
&lt;p&gt;I think the master (nrt52832) can&amp;#39;t read from LIS3DH in your code. Do these logs mean success or error ? It seems there should be y, z, values in a couple of blanks. &amp;lt;info&amp;gt; app: |&amp;nbsp;&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: how to read/write  SPI</title><link>https://devzone.nordicsemi.com/thread/125847?ContentTypeID=1</link><pubDate>Sat, 24 Mar 2018 15:15:06 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:80e2d812-8730-4156-ac43-8394cd019d75</guid><dc:creator>suprtigr</dc:creator><description>&lt;p&gt;Kirt,&lt;/p&gt;
&lt;p&gt;If I understand you question, you&amp;#39;re asking for more information on how to utilize SPI to read and write with a peripheral device. Firstly you should look into the pca10040.h (supplement your own board number in there) to find the pins to use for SPI, They are labeled SPIM0_MISO_PIN and others of the like. I have just completed this task for my LIS3DH Accelerometer. I was able to write and read from registers. Here is the code I utilized in my main.c file. Note that it is far from the most efficient and elegant solution, but it may help.&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;/**
 * Copyright (c) 2015 - 2017, Nordic Semiconductor ASA
 * 
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 * 
 * 1. Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer.
 * 
 * 2. Redistributions in binary form, except as embedded into a Nordic
 *    Semiconductor ASA integrated circuit in a product or a software update for
 *    such product, must reproduce the above copyright notice, this list of
 *    conditions and the following disclaimer in the documentation and/or other
 *    materials provided with the distribution.
 * 
 * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
 *    contributors may be used to endorse or promote products derived from this
 *    software without specific prior written permission.
 * 
 * 4. This software, with or without modification, must only be used with a
 *    Nordic Semiconductor ASA integrated circuit.
 * 
 * 5. Any software provided in binary form under this license must not be reverse
 *    engineered, decompiled, modified and/or disassembled.
 * 
 * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA &amp;quot;AS IS&amp;quot; AND ANY EXPRESS
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 */
#include &amp;quot;nrf_drv_spi.h&amp;quot;
#include &amp;quot;app_util_platform.h&amp;quot;
#include &amp;quot;nrf_gpio.h&amp;quot;
#include &amp;quot;nrf_delay.h&amp;quot;
#include &amp;quot;boards.h&amp;quot;
#include &amp;quot;app_error.h&amp;quot;
#include &amp;lt;string.h&amp;gt;
#include &amp;quot;nrf_log.h&amp;quot;
#include &amp;quot;nrf_log_ctrl.h&amp;quot;
#include &amp;quot;nrf_log_default_backends.h&amp;quot;

#define SPI_INSTANCE  0 /**&amp;lt; SPI instance index. */
static const nrf_drv_spi_t spi = NRF_DRV_SPI_INSTANCE(SPI_INSTANCE);  /**&amp;lt; SPI instance. */
static volatile bool spi_xfer_done;  /**&amp;lt; Flag used to indicate that SPI instance completed the transfer. */
static volatile bool config_done = false;

#define TEST_STRING &amp;quot;Nordic&amp;quot;
//add new definition
//static uint8_t WHO_AM_I_REG = ((uint8_t)143); //10001111 ((uint8_t)0x10) 143 int 0x8F


//MOD BLOCK FOR NEW REG
/**static uint8_t       m_tx_buf[] = TEST_STRING;           /**&amp;lt; TX buffer. */
/**static uint8_t       m_rx_buf[sizeof(TEST_STRING) + 1];    /**&amp;lt; RX buffer. */
/**static const uint8_t m_length = sizeof(m_tx_buf);        /**&amp;lt; Transfer length. */

static uint8_t       m_config_buf1[] = {((uint8_t)0x20),((uint8_t)0x1F)};           /**&amp;lt; TX buffer. */ //,((uint8_t)0x24),((uint8_t)0x40),((uint8_t)0x2E),((uint8_t)0x40)
static uint8_t       m_config_buf2[] = {((uint8_t)0x24),((uint8_t)0x40)}; 
static uint8_t       m_config_buf3[] = {((uint8_t)0x2E),((uint8_t)0x00)}; 

static uint8_t       m_tx_rx_buf1[] = {((uint8_t)0xA0)}; //,((uint8_t)0xA4),((uint8_t)0xAE)
static uint8_t       m_tx_rx_buf2[] = {((uint8_t)0xA4)};
static uint8_t       m_tx_rx_buf3[] = {((uint8_t)0xAE)};

static uint8_t       m_rx_buf1[sizeof(((uint8_t)0xA4)) + 1];    /**&amp;lt; RX buffer. */
static uint8_t       m_rx_buf2[sizeof(((uint8_t)0xA4)) + 1];
static uint8_t       m_rx_buf3[sizeof(((uint8_t)0xA4)) + 1];

//Buffers for accelerometer data
static uint8_t       m_x_axis_buf1[] = {((uint8_t)0xA9)};
static uint8_t       m_x_rx_buf1[sizeof(((uint8_t)0xA9)) + 1]; 

//static const uint8_t m_length_config = sizeof(m_config_buf1);        /**&amp;lt; Transfer length. */
static const uint8_t m_length_rx = sizeof(m_tx_rx_buf1);        /**&amp;lt; Transfer length. */




/**
 * @brief SPI user event handler.
 * @param event
 */
void spi_event_handler(nrf_drv_spi_evt_t const * p_event,
                       void *                    p_context)
{
    spi_xfer_done = true;
    NRF_LOG_INFO(&amp;quot;Transfer completed.&amp;quot;);
    if (m_rx_buf1[0] != 0||m_rx_buf2[0] != 0||m_rx_buf3[0] != 0||m_x_rx_buf1[0] != 0)
    {
        NRF_LOG_INFO(&amp;quot; Received:&amp;quot;);
        NRF_LOG_HEXDUMP_INFO(m_rx_buf1, strlen((const char *)m_rx_buf1));
        NRF_LOG_HEXDUMP_INFO(m_rx_buf2, strlen((const char *)m_rx_buf2));
        NRF_LOG_HEXDUMP_INFO(m_rx_buf3, strlen((const char *)m_rx_buf3));
    }
}

int main(void)
{
    bool verified = false;
    bsp_board_leds_init();

    APP_ERROR_CHECK(NRF_LOG_INIT(NULL));
    NRF_LOG_DEFAULT_BACKENDS_INIT();
    
    NRF_LOG_INFO(&amp;quot;SPI example.&amp;quot;);

    nrf_drv_spi_config_t spi_config = NRF_DRV_SPI_DEFAULT_CONFIG;
    //FROM pca10040.h
    spi_config.ss_pin   = 12; //SPI_SS_PIN
    spi_config.miso_pin = 28; //SPI_MISO_PIN
    spi_config.mosi_pin = 25; //SPI_MOSI_PIN
    spi_config.sck_pin  = 29; //SPI_SCK_PIN
    APP_ERROR_CHECK(nrf_drv_spi_init(&amp;amp;spi, &amp;amp;spi_config, spi_event_handler, NULL));

    while (1)
    {
        // Reset rx buffer and transfer done flag
        memset(m_rx_buf1, 0, m_length_rx);
        memset(m_rx_buf2, 0, m_length_rx);
        memset(m_rx_buf3, 0, m_length_rx);

        spi_xfer_done = false;

        //moved to config_accel()
        if(!config_done){
          config_accel();
        }

        if(config_done&amp;amp;&amp;amp;!verified){
          APP_ERROR_CHECK(nrf_drv_spi_transfer(&amp;amp;spi, m_tx_rx_buf1, m_length_rx, m_rx_buf1, m_length_rx));
          nrf_delay_ms(200);
          APP_ERROR_CHECK(nrf_drv_spi_transfer(&amp;amp;spi, m_tx_rx_buf2, m_length_rx, m_rx_buf2, m_length_rx));
          nrf_delay_ms(200);
          APP_ERROR_CHECK(nrf_drv_spi_transfer(&amp;amp;spi, m_tx_rx_buf3, m_length_rx, m_rx_buf3, m_length_rx));
          nrf_delay_ms(200);
          verified = true;
        }

        if(verified){
          APP_ERROR_CHECK(nrf_drv_spi_transfer(&amp;amp;spi,m_x_axis_buf1, m_length_rx, m_rx_buf1, m_length_rx));
        }


        while (!spi_xfer_done)
        {
            __WFE();
        }

        NRF_LOG_FLUSH();

        bsp_board_led_invert(BSP_BOARD_LED_0);
        nrf_delay_ms(900);//orig 200
        config_done = true;
    }
}

void config_accel(){
    APP_ERROR_CHECK(nrf_drv_spi_transfer(&amp;amp;spi, m_config_buf1, sizeof(m_config_buf1), NULL, NULL));//change 3rd param from m_rx_buf to NULL &amp;amp; 4th from m_length to NULL 
    nrf_delay_ms(20);
    APP_ERROR_CHECK(nrf_drv_spi_transfer(&amp;amp;spi, m_config_buf2, sizeof(m_config_buf2), NULL, NULL));
    nrf_delay_ms(20);
    APP_ERROR_CHECK(nrf_drv_spi_transfer(&amp;amp;spi, m_config_buf3, sizeof(m_config_buf3), NULL, NULL));
    nrf_delay_ms(20);
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;I hope this helps.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>