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

SPI instance taking up a lot of power when enabled.

SDK version: 15.3

Soft-device version: 6.1.1

Hi,

I've been working on a low power application where every 10's of uA is important. My application requires SPI as Master on nRF52832 but as soon as I enable my SPI my current shoots up to mA range from uA. 

I used "SPI"example present in "..\nRF5_SDK_15.3.0_59ac345\examples\peripheral" to understand the configuration of SPI. Could you please tell my why would this happen?

I read a couple of articles where people faced similar current consumption issue. Or do I have to do some disabling of SPI to save power?

Parents
  • Hi, the SPI example is not power optimized. nrf_delay_ms() is just using a loop of NOP instructions. You should start the SPI transfer from an RTC interrupt or similar, and keep the CPU in a WFE loop.

    I modified the SPI example to include an app_timer which uses the RTC to give you an interrupt every 100ms. The power consumption is 2uA between the transfers. Also remember to disable logging in sdk_config.h.

    #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"
    #include "app_timer.h"
    
    #define SPI_INSTANCE  0 /**< SPI instance index. */
    static const nrf_drv_spi_t spi = NRF_DRV_SPI_INSTANCE(SPI_INSTANCE);  /**< SPI instance. */
    
    #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)
    {
        NRF_LOG_INFO("Transfer completed.");
        if (m_rx_buf[0] != 0)
        {
            NRF_LOG_INFO(" Received:");
            NRF_LOG_HEXDUMP_INFO(m_rx_buf, strlen((const char *)m_rx_buf));
        }
    }
    
    static void spi_transfer_handler(void * p_context){
            memset(m_rx_buf, 0, m_length);
            APP_ERROR_CHECK(nrf_drv_spi_transfer(&spi, m_tx_buf, m_length, m_rx_buf, m_length));
    }
    
    int main(void)
    {
        bsp_board_init(BSP_INIT_LEDS);
    
        APP_ERROR_CHECK(NRF_LOG_INIT(NULL));
        NRF_LOG_DEFAULT_BACKENDS_INIT();
    
        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));
    
        NRF_CLOCK->TASKS_LFCLKSTART = 1;
        while(NRF_CLOCK->EVENTS_LFCLKSTARTED == 0){
        }
        NRF_CLOCK->EVENTS_LFCLKSTARTED = 0;
        APP_ERROR_CHECK(app_timer_init());
        APP_TIMER_DEF(spi_timer_id);
        APP_ERROR_CHECK(app_timer_create(&spi_timer_id, APP_TIMER_MODE_REPEATED, spi_transfer_handler));
        APP_ERROR_CHECK(app_timer_start(spi_timer_id, APP_TIMER_TICKS(100), NULL));
    
        NRF_LOG_INFO("SPI example started.");
    
        while (1)
        {
            NRF_LOG_FLUSH();
            __WFE();
        }
    }

Reply
  • Hi, the SPI example is not power optimized. nrf_delay_ms() is just using a loop of NOP instructions. You should start the SPI transfer from an RTC interrupt or similar, and keep the CPU in a WFE loop.

    I modified the SPI example to include an app_timer which uses the RTC to give you an interrupt every 100ms. The power consumption is 2uA between the transfers. Also remember to disable logging in sdk_config.h.

    #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"
    #include "app_timer.h"
    
    #define SPI_INSTANCE  0 /**< SPI instance index. */
    static const nrf_drv_spi_t spi = NRF_DRV_SPI_INSTANCE(SPI_INSTANCE);  /**< SPI instance. */
    
    #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)
    {
        NRF_LOG_INFO("Transfer completed.");
        if (m_rx_buf[0] != 0)
        {
            NRF_LOG_INFO(" Received:");
            NRF_LOG_HEXDUMP_INFO(m_rx_buf, strlen((const char *)m_rx_buf));
        }
    }
    
    static void spi_transfer_handler(void * p_context){
            memset(m_rx_buf, 0, m_length);
            APP_ERROR_CHECK(nrf_drv_spi_transfer(&spi, m_tx_buf, m_length, m_rx_buf, m_length));
    }
    
    int main(void)
    {
        bsp_board_init(BSP_INIT_LEDS);
    
        APP_ERROR_CHECK(NRF_LOG_INIT(NULL));
        NRF_LOG_DEFAULT_BACKENDS_INIT();
    
        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));
    
        NRF_CLOCK->TASKS_LFCLKSTART = 1;
        while(NRF_CLOCK->EVENTS_LFCLKSTARTED == 0){
        }
        NRF_CLOCK->EVENTS_LFCLKSTARTED = 0;
        APP_ERROR_CHECK(app_timer_init());
        APP_TIMER_DEF(spi_timer_id);
        APP_ERROR_CHECK(app_timer_create(&spi_timer_id, APP_TIMER_MODE_REPEATED, spi_transfer_handler));
        APP_ERROR_CHECK(app_timer_start(spi_timer_id, APP_TIMER_TICKS(100), NULL));
    
        NRF_LOG_INFO("SPI example started.");
    
        while (1)
        {
            NRF_LOG_FLUSH();
            __WFE();
        }
    }

Children
No Data
Related