This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

how to read/write SPI

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.

Parents
  • Kirt,

    If I understand you question, you'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.

    /**
     * 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 "AS IS" 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 "nrf_drv_spi.h"
    #include "app_util_platform.h"
    #include "nrf_gpio.h"
    #include "nrf_delay.h"
    #include "boards.h"
    #include "app_error.h"
    #include <string.h>
    #include "nrf_log.h"
    #include "nrf_log_ctrl.h"
    #include "nrf_log_default_backends.h"
    
    #define SPI_INSTANCE  0 /**< SPI instance index. */
    static const nrf_drv_spi_t spi = NRF_DRV_SPI_INSTANCE(SPI_INSTANCE);  /**< SPI instance. */
    static volatile bool spi_xfer_done;  /**< Flag used to indicate that SPI instance completed the transfer. */
    static volatile bool config_done = false;
    
    #define TEST_STRING "Nordic"
    //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;           /**< TX buffer. */
    /**static uint8_t       m_rx_buf[sizeof(TEST_STRING) + 1];    /**< RX buffer. */
    /**static const uint8_t m_length = sizeof(m_tx_buf);        /**< Transfer length. */
    
    static uint8_t       m_config_buf1[] = {((uint8_t)0x20),((uint8_t)0x1F)};           /**< 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];    /**< 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);        /**< Transfer length. */
    static const uint8_t m_length_rx = sizeof(m_tx_rx_buf1);        /**< 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("Transfer completed.");
        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(" Received:");
            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("SPI example.");
    
        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(&spi, &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&&!verified){
              APP_ERROR_CHECK(nrf_drv_spi_transfer(&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(&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(&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(&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(&spi, m_config_buf1, sizeof(m_config_buf1), NULL, NULL));//change 3rd param from m_rx_buf to NULL & 4th from m_length to NULL 
        nrf_delay_ms(20);
        APP_ERROR_CHECK(nrf_drv_spi_transfer(&spi, m_config_buf2, sizeof(m_config_buf2), NULL, NULL));
        nrf_delay_ms(20);
        APP_ERROR_CHECK(nrf_drv_spi_transfer(&spi, m_config_buf3, sizeof(m_config_buf3), NULL, NULL));
        nrf_delay_ms(20);
    }

    I hope this helps.

  • Hi, suprtigr

    I also have LIS3DH and built your code. but I got following msg 

    <info> app: Transfer completed.
    <info> app: Transfer completed.
    <info> app: Received:
    <info> app: 80 |.
    <info> app: |
    <info> app: |

    \<info> app: Transfer completed.
    <info> app: Transfer completed.
    <info> app: Received:
    <info> app: 80 |.
    <info> app: |
    <info> app: |

    I think the master (nrt52832) can'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. <info> app: |  

  • We have an SPI Master & Slave example in our SDK & 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.

Reply Children
No Data
Related