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

SPI nrf52832

hi,

I am trying to add SPI (master) codes into the ble_app_uart.

My setting for SPI is as follows:

#define SPI1_ENABLED 1 //
#define SPI_INSTANCE 1 /**< SPI instance index. */
//#define SPI0_ENABLED 0
#define SPIM_IN_USE 0
#define SPI_IN_USE 1

I have included the nrf_drv_spi.h and nrf_drv_config.h in main.c

yet when I am trying to compile the project. One error occurred in nrf_drv_spi.c(which I added into the project) indicting "wrong configuration"

And even if I assigned the value of SPIM_IN_USE and SPI_IN_USE, the error still occurs. After compiling, a window shows saying "browse information of one or more  files is not available"

I am wondering what's wrong with it?

Parents
  • Hey maslooo,

    You should add the following lines of code to your sdk_config.h file:

    #ifndef SPI_SCK_PIN
    #define SPI_SCK_PIN YOUR_PIN_NUMBER
    #endif
    
    #ifndef SPI_MISO_PIN
    #define SPI_MISO_PIN YOUR_PIN_NUMBER
    #endif
    
    #ifndef SPI_MOSI_PIN
    #define SPI_MOSI_PIN YOUR_PIN_NUMBER
    #endif
    
    #ifndef SPI_SS_PIN
    #define SPI_SS_PIN YOUR_PIN_NUMBER
    #endif
    
    
    // <e> SPI_ENABLED - nrf_drv_spi - SPI/SPIM peripheral driver
    //==========================================================
    #ifndef SPI_ENABLED
    #define SPI_ENABLED 1
    #endif
    // <o> SPI_DEFAULT_CONFIG_IRQ_PRIORITY  - Interrupt priority
     
    
    // <i> Priorities 0,2 (nRF51) and 0,1,4,5 (nRF52) are reserved for SoftDevice
    // <0=> 0 (highest) 
    // <1=> 1 
    // <2=> 2 
    // <3=> 3 
    // <4=> 4 
    // <5=> 5 
    // <6=> 6 
    // <7=> 7 
    
    #ifndef SPI_DEFAULT_CONFIG_IRQ_PRIORITY
    #define SPI_DEFAULT_CONFIG_IRQ_PRIORITY 7
    #endif
    
    // <o> SPI_DEFAULT_FREQUENCY  - SPI frequency
     
    // <33554432=> 125 kHz 
    // <67108864=> 250 kHz 
    // <134217728=> 500 kHz 
    // <268435456=> 1 MHz 
    // <536870912=> 2 MHz 
    // <1073741824=> 4 MHz 
    // <2147483648=> 8 MHz 
    
    #ifndef SPI_DEFAULT_FREQUENCY
    #define SPI_DEFAULT_FREQUENCY 1073741824
    #endif
    
    // <o> NRF_SPI_DRV_MISO_PULLUP_CFG  - MISO PIN pull-up configuration.
     
    // <0=> NRF_GPIO_PIN_NOPULL 
    // <1=> NRF_GPIO_PIN_PULLDOWN 
    // <3=> NRF_GPIO_PIN_PULLUP 
    
    #ifndef NRF_SPI_DRV_MISO_PULLUP_CFG
    #define NRF_SPI_DRV_MISO_PULLUP_CFG 1
    #endif
    
    // <e> SPI0_ENABLED - Enable SPI0 instance
    //==========================================================
    #ifndef SPI0_ENABLED
    #define SPI0_ENABLED 1
    #endif
    // <q> SPI0_USE_EASY_DMA  - Use EasyDMA
     
    
    #ifndef SPI0_USE_EASY_DMA
    #define SPI0_USE_EASY_DMA 1
    #endif

    main.c: 

    #include "nrf_drv_spi.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. */
    
    #define TEST_STRING "Nordic"
    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. */
    
    /**
     * @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;
    }
    
    void spi_init(void)
    {
        nrf_drv_spi_config_t spi_config = NRF_DRV_SPI_DEFAULT_CONFIG;
        spi_config.ss_pin   = SPI_SS_PIN;
        spi_config.miso_pin = SPI_MISO_PIN;
        spi_config.mosi_pin = SPI_MOSI_PIN;
        spi_config.sck_pin  = SPI_SCK_PIN;
        APP_ERROR_CHECK(nrf_drv_spi_init(&spi, &spi_config, spi_event_handler, NULL));
    }
    
    int main(void)
    {
       spi_init();
       
        while (1)
        {   /*** transfer the test string every 200ms***/
        
            // Reset rx buffer and transfer done flag
            memset(m_rx_buf, 0, m_length);
            spi_xfer_done = false;
    
            APP_ERROR_CHECK(nrf_drv_spi_transfer(&spi, m_tx_buf, m_length, m_rx_buf, m_length));
    
            while (!spi_xfer_done)
            {
                __WFE();
            }
    
            nrf_delay_ms(200);
        }
    }

    Cheers,

    Håkon.

  • Hi Haakonsh,

    i am trying this, i am getting error

    undefined reference to `nrf_drv_spi_init'

Reply Children
Related