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

High current consumption on nrf52 dk

Hi,

I have a beacon code for nrf51 that I wanted to port to nrf52. It uses app_timer, gpiote and adc. The power consumption in idle is ~30uA. Now I wanted to port this code to nrf52 but have problems with current consumption. What I see is that current is on const around 1mA level. I thought maybe I have done something wrong with SAADC so I removed it from code but result is about the same. I was stripping components one by one as I get to point where I have only for loop with __WFE() inside and I see 200uA !(softdevice present but not enabled) . Any idea what is wrong? Now I think that maybe some errata code is needed or maybe it is the errata code in startup causing troubles?

For measurment I used giant gecko with simplicty studio energy profiler.

Regards,

Michal

  • Hello,

    To verify that there is nothing wrong with the board itself, could you try to go to system-off? (Just copy the code below Relaxed) This should bring the consumption down to ~ 300 nA. Consumption around 200uA can indicate that the high frequency clock is running. Maybe I could see more of your code to verify it?

    int main(void)
    {
        NRF_POWER->SYSTEMOFF = 1;
    
        while (true)
        {
        }
    }

  • #include <stdbool.h>
    #include <stdint.h>
    #include "ble_advdata.h"
    #include "nordic_common.h"
    #include "softdevice_handler.h"
    #include "bsp.h"
    #include "app_timer.h"
    #include "nrf_log.h"
    #include "nrf_log_ctrl.h"
    #include "nrf_drv_gpiote.h"
    #include "nrf_delay.h"
    #include "nrf_drv_saadc.h"
    #include "nrf52.h"
    
    #define BAT_MEASURE_PERIOD_MINUTES      2
    #define BAT_ANALOG_INPUT                NRF_SAADC_INPUT_AIN0
    #define ADC_MEASURE_PERIOD_MINUTES      20
    #define ADC_ANALOG_INPUT                NRF_SAADC_INPUT_AIN1
    #define ADV_PERIOD_MS                   5000                              // advertising period
    #define WAKE_GPIO                       13                                // wake up gpio
    #define ADV_INTERVAL_MS                 1000                              // advertising interval
    #define TX_POWER_LEVEL                  (-4)                                // tx power level
    #define DEVICE_NAME                     "XYZ"                               // device name
    #define APP_MAJOR_VALUE                 0x01, 0x02                        /**< Major value used to identify Beacons. */
    #define APP_MINOR_VALUE                 0x03, 0x04                        /**< Minor value used to identify Beacons. */
    #define VBAT_MAX_IN_MV                  3000
    
    #define CENTRAL_LINK_COUNT              0                                 /**< Number of central links used by the application. When changing this number remember to adjust the RAM settings*/
    #define PERIPHERAL_LINK_COUNT           0                                 /**< Number of peripheral links used by the application. When changing this number remember to adjust the RAM settings*/
    
    #define IS_SRVC_CHANGED_CHARACT_PRESENT 0                                 /**< Include or not the service_changed characteristic. if not enabled, the server's database cannot be changed for the lifetime of the device*/
    
    #define APP_CFG_NON_CONN_ADV_TIMEOUT    0                                 /**< Time for which the device must be advertising in non-connectable mode (in seconds). 0 disables timeout. */
    #define NON_CONNECTABLE_ADV_INTERVAL    MSEC_TO_UNITS(ADV_INTERVAL_MS, UNIT_0_625_MS) 
                                                                              /**< The advertising interval for non-connectable advertisement (100 ms). This value can vary between 100ms to 10.24s). */
    
    #define APP_BEACON_INFO_LENGTH          0x17                              /**< Total length of information advertised by the Beacon. */
    #define APP_ADV_DATA_LENGTH             0x15                              /**< Length of manufacturer specific data in the advertisement. */
    #define APP_DEVICE_TYPE                 0x02                              /**< 0x02 refers to Beacon. */
    #define APP_MEASURED_RSSI               0xC3                              /**< The Beacon's measured RSSI at 1 meter distance in dBm. */
    #define APP_COMPANY_IDENTIFIER          0x0059                            /**< Company identifier for Nordic Semiconductor ASA. as per www.bluetooth.org. */
    
    #define APP_BEACON_UUID                 0x01, 0x12, 0x23, 0x34, \
                                            0x45, 0x56, 0x67, 0x78, \
                                            0x89, 0x9a, 0xab, 0xbc, \
                                            0xcd, 0xde, 0xef, 0xf0            /**< Proprietary UUID for Beacon. */
    
    #define DEAD_BEEF                       0xDEADBEEF                        /**< Value used as error code on stack dump, can be used to identify stack location on stack unwind. */
    
    #define APP_TIMER_PRESCALER             15                                /**< Value of the RTC1 PRESCALER register. */
    #define APP_TIMER_OP_QUEUE_SIZE         4                                 /**< Size of timer operation queues. */
    
    // #define USE_UICR_FOR_MAJ_MIN_VALUES
    
    // #if defined(USE_UICR_FOR_MAJ_MIN_VALUES)
    #define MAJ_VAL_OFFSET_IN_BEACON_INFO   0x12                                /**< Position of the MSB of the Major Value in m_beacon_info array. */
    #define UICR_ADDRESS                    0x10001080                        /**< Address of the UICR register used by this example. The major and minor versions to be encoded into the advertising data will be picked up from this location. */
    // #endif
    
    #define TOGGLE                          0
    #define DEBUG_TOGGLE(pin)               //nrf_gpio_pin_toggle(pin-4)
    
    APP_TIMER_DEF(sleep_timer);
    APP_TIMER_DEF(bas_timer);
    APP_TIMER_DEF(adc_timer);
    
    #define SAMPLES_IN_BUFFER 1
    #define NUMBE_OF_ADC_CH   2
    
    static bool module_advertising = false;
    static ble_gap_adv_params_t m_adv_params;                                 /**< Parameters to be passed to the stack when starting advertising. */
    static uint8_t m_beacon_info[APP_BEACON_INFO_LENGTH] =                    /**< Information advertised by the Beacon. */
    {
        APP_DEVICE_TYPE,
        APP_ADV_DATA_LENGTH, // Manufacturer specific information. Specifies the length of the
                             // manufacturer specific data in this implementation.
        APP_BEACON_UUID,
        APP_MAJOR_VALUE,     // Major arbitrary value that can be used to distinguish between Beacons.
        APP_MINOR_VALUE,     // Minor arbitrary value that can be used to distinguish between Beacons.
    };
    
    /**@brief Callback function for asserts in the SoftDevice.
     *
     * @details This function will be called in case of an assert in the SoftDevice.
     *
     * @warning This handler is an example only and does not fit a final product. You need to analyze
     *          how your product is supposed to react in case of Assert.
     * @warning On assert from the SoftDevice, the system can only recover on reset.
     *
     * @param[in]   line_num   Line number of the failing ASSERT call.
     * @param[in]   file_name  File name of the failing ASSERT call.
     */
    void assert_nrf_callback(uint16_t line_num, const uint8_t * p_file_name)
    {
        app_error_handler(DEAD_BEEF, line_num, p_file_name);
    }
    
    /**@brief Function for initializing the Advertising functionality.
     *
     * @details Encodes the required advertising data and passes it to the stack.
     *          Also builds a structure to be passed to the stack when starting advertising.
     */
    static void advertising_init(int8_t * p_tx_power_level, uint8_t * p_battery_level, uint8_t * p_adc_level)
    {
        uint32_t      err_code;
        ble_advdata_t advdata;
        // uint8_t       flags = BLE_GAP_ADV_FLAG_BR_EDR_NOT_SUPPORTED;
    
        ble_advdata_manuf_data_t manuf_specific_data;
    
        manuf_specific_data.company_identifier = APP_COMPANY_IDENTIFIER;
    
    #if defined(USE_UICR_FOR_MAJ_MIN_VALUES)
        uint8_t index = MAJ_VAL_OFFSET_IN_BEACON_INFO;
        // If USE_UICR_FOR_MAJ_MIN_VALUES is defined, the major and minor values will be read from the
        // UICR instead of using the default values. The major and minor values obtained from the UICR
        // are encoded into advertising data in big endian order (MSB First).
        // To set the UICR used by this example to a desired value, write to the address 0x10001080
        // using the nrfjprog tool. The command to be used is as follows.
        // nrfjprog --snr <Segger-chip-Serial-Number> --memwr 0x10001080 --val <your major/minor value>
        // For example, for a major value and minor value of 0xabcd and 0x0102 respectively, the
        // the following command should be used.
        // nrfjprog --snr <Segger-chip-Serial-Number> --memwr 0x10001080 --val 0xabcd0102
        uint16_t major_value = ((*(uint32_t *)UICR_ADDRESS) & 0xFFFF0000) >> 16;
        uint16_t minor_value = ((*(uint32_t *)UICR_ADDRESS) & 0x0000FFFF);
    
    
        m_beacon_info[index++] = MSB_16(major_value);
        m_beacon_info[index++] = LSB_16(major_value);
    
        m_beacon_info[index++] = MSB_16(minor_value);
        m_beacon_info[index++] = LSB_16(minor_value);
    #endif
        if (p_battery_level != NULL)
        {
          m_beacon_info[APP_BEACON_INFO_LENGTH -1] = *p_battery_level;
        }
    
        if (p_adc_level != NULL)
        {
          m_beacon_info[MAJ_VAL_OFFSET_IN_BEACON_INFO] = *p_adc_level;
        }
    
        manuf_specific_data.data.p_data = (uint8_t *) m_beacon_info;
        manuf_specific_data.data.size   = APP_BEACON_INFO_LENGTH;
    
        // Build and set advertising data.
        memset(&advdata, 0, sizeof(advdata));
    
        advdata.name_type             = BLE_ADVDATA_FULL_NAME;
        // advdata.flags                 = flags;
        if (p_tx_power_level != NULL)
        {
            advdata.p_tx_power_level = p_tx_power_level;
        }
        advdata.p_manuf_specific_data = &manuf_specific_data;
        err_code = ble_advdata_set(&advdata, NULL);
        APP_ERROR_CHECK(err_code);
    
        // Initialize advertising parameters (used when starting advertising).
        memset(&m_adv_params, 0, sizeof(m_adv_params));
    
        m_adv_params.type        = BLE_GAP_ADV_TYPE_ADV_NONCONN_IND;
        m_adv_params.p_peer_addr = NULL;                             // Undirected advertisement.
        m_adv_params.fp          = BLE_GAP_ADV_FP_ANY;
        m_adv_params.interval    = NON_CONNECTABLE_ADV_INTERVAL;
        m_adv_params.timeout     = APP_CFG_NON_CONN_ADV_TIMEOUT;
    }
    
    
    /**@brief Function for starting advertising.
     */
    static void advertising_start(void)
    {
        uint32_t err_code;
    
        err_code = sd_ble_gap_adv_start(&m_adv_params);
        APP_ERROR_CHECK(err_code);
    }
    
    
    /**@brief Function for initializing the BLE stack.
     *
     * @details Initializes the SoftDevice and the BLE event interrupt.
     */
    static void ble_stack_init(void)
    {
        uint32_t err_code;
    
        nrf_clock_lf_cfg_t clock_lf_cfg = NRF_CLOCK_LFCLKSRC;
    
        // Initialize the SoftDevice handler module.
        SOFTDEVICE_HANDLER_INIT(&clock_lf_cfg, NULL);
    
        ble_enable_params_t ble_enable_params;
        err_code = softdevice_enable_get_default_config(CENTRAL_LINK_COUNT,
                                                        PERIPHERAL_LINK_COUNT,
                                                        &ble_enable_params);
        APP_ERROR_CHECK(err_code);
    
        //Check the ram settings against the used number of links
        CHECK_RAM_START_ADDR(CENTRAL_LINK_COUNT,PERIPHERAL_LINK_COUNT);
    
        // Enable BLE stack.
        err_code = softdevice_enable(&ble_enable_params);
        APP_ERROR_CHECK(err_code);
    }
    
    static void device_name_init(void)
    {
        uint32_t                err_code;
        ble_gap_conn_sec_mode_t sec_mode;
    
        BLE_GAP_CONN_SEC_MODE_SET_OPEN(&sec_mode);
    
        err_code = sd_ble_gap_device_name_set(&sec_mode,
                                              (const uint8_t *)DEVICE_NAME,
                                              strlen(DEVICE_NAME));
        APP_ERROR_CHECK(err_code);
    }
    
    /**@brief Function for doing power management.
     */
    static void power_manage(void)
    {
        uint32_t err_code = sd_app_evt_wait();
        APP_ERROR_CHECK(err_code);
    }
    
    void advertise(uint8_t * p_battery_level, uint8_t * p_adc_level)
    {
      if (module_advertising == false)
      {
        module_advertising = true;
        app_timer_start(sleep_timer, APP_TIMER_TICKS(ADV_PERIOD_MS, APP_TIMER_PRESCALER), NULL);
        advertising_init(NULL, p_battery_level, p_adc_level);
        advertising_start();
      }
    }
    
    void saadc_callback(nrf_drv_saadc_evt_t const * p_event)
    {
    
    }
    
    void saadc_init(void)
    {
        ret_code_t err_code;
        nrf_saadc_channel_config_t channel_config =
            NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(NRF_SAADC_INPUT_AIN2);
    
        nrf_drv_saadc_config_t saadc_config = NRF_DRV_SAADC_DEFAULT_CONFIG;
        saadc_config.resolution = NRF_SAADC_RESOLUTION_8BIT;
        err_code = nrf_drv_saadc_init(&saadc_config, saadc_callback);
        APP_ERROR_CHECK(err_code);
    
        err_code = nrf_drv_saadc_channel_init(0, &channel_config);
        APP_ERROR_CHECK(err_code);
    
        channel_config.pin_p = NRF_SAADC_INPUT_AIN1;
        err_code = nrf_drv_saadc_channel_init(1, &channel_config);
        APP_ERROR_CHECK(err_code);
    }
    
    uint8_t adc_level_get(uint8_t channel)
    {
      saadc_init();
      static nrf_saadc_value_t adc_lvl = 0;
      nrf_drv_saadc_sample_convert(channel, &adc_lvl);
      // APP_ERROR_CHECK(err_code);
      nrf_drv_saadc_uninit();
      NVIC_ClearPendingIRQ(SAADC_IRQn);
      return adc_lvl;
    }
    
    
    void sleep_timer_handler(void * p_context)
    {
      DEBUG_TOGGLE(23);
      module_advertising = false;
      uint32_t err_code = sd_ble_gap_adv_stop();
      APP_ERROR_CHECK(err_code);
      __DSB();
      __NOP();
    }
    
    static uint8_t result;
    void bas_timer_handler(void * p_context)
    {
      uint8_t lvl = adc_level_get(0);
      uint16_t vbg_in_mv = 600;
      uint8_t adc_max = 255;
      uint16_t vbat_current_in_mv = (lvl * 6 * vbg_in_mv) / adc_max;
    
      if (vbat_current_in_mv > 2800)
      {
        result = 0;
      }
      else if (vbat_current_in_mv > 2700)
      {
        result = 1;
      }
      else if (vbat_current_in_mv > 2600)
      {
        result = 2;
      }
      else
      {
        result = 3;
      }
    
    
      advertise(&result, NULL);
    }
    
    static uint8_t lvl;
    void adc_timer_handler(void * p_context)
    {
      DEBUG_TOGGLE(22);
      lvl = adc_level_get(1);
      advertise(NULL, &lvl);
    }
    
    void init_timer(void)
    {
        APP_TIMER_INIT(APP_TIMER_PRESCALER, APP_TIMER_OP_QUEUE_SIZE, false);
        uint32_t err_code = app_timer_create(&sleep_timer,
                                            APP_TIMER_MODE_SINGLE_SHOT,
                                            sleep_timer_handler);
        err_code = app_timer_create(&bas_timer,
                                    APP_TIMER_MODE_REPEATED,
                                    bas_timer_handler);
        err_code = app_timer_create(&adc_timer,
                                    APP_TIMER_MODE_REPEATED,
                                    adc_timer_handler);
        APP_ERROR_CHECK(err_code);
    }
    
    void in_pin_handler(nrf_drv_gpiote_pin_t pin, nrf_gpiote_polarity_t action)
    {
      DEBUG_TOGGLE(22);
      // uint8_t lvl = battery_level_get();
      advertise(NULL, NULL);
    }
    
    static void gpio_init(void)
    {
        ret_code_t err_code;
    
        err_code = nrf_drv_gpiote_init();
        APP_ERROR_CHECK(err_code);
    
        nrf_drv_gpiote_in_config_t in_config = GPIOTE_CONFIG_IN_SENSE_HITOLO(false);
        in_config.pull = NRF_GPIO_PIN_PULLUP;
    
        err_code = nrf_drv_gpiote_in_init(WAKE_GPIO, &in_config, in_pin_handler);
        APP_ERROR_CHECK(err_code);
    
        nrf_drv_gpiote_in_event_enable(WAKE_GPIO, true);
    }
    
    /**
     * @brief Function for application main entry.
     */
    int main(void)
    {
        static int8_t tx_power_level = TX_POWER_LEVEL;
    
    #if TOGGLE
        // nrf_gpio_cfg_output(17);
        // nrf_gpio_cfg_output(18);
        // nrf_gpio_cfg_output(19);
    #endif
    
        // NRF_POWER->DCDCEN = 1;  
        init_timer();    
        app_timer_start(bas_timer, APP_TIMER_TICKS((BAT_MEASURE_PERIOD_MINUTES * 60 * 1000), APP_TIMER_PRESCALER), NULL);
        nrf_delay_ms(100);
        app_timer_start(adc_timer, APP_TIMER_TICKS((ADC_MEASURE_PERIOD_MINUTES  * 1000), APP_TIMER_PRESCALER), NULL);
        gpio_init();
        // Enter main loop.
        ble_stack_init();
        sd_ble_gap_tx_power_set(tx_power_level);
        device_name_init();
    
        for (;; )
        {
          power_manage();
          // DEBUG_TOGGLE(21);
        }
    }
    
    
    /**
     * @}
     */
    

    Here is code for my nrf52 port. In the test where I got 200uA it was only WFE in main loop without any initilization. Also I was trying to trigger hfclk stop task but without any change in consumption. I will test system off later.

  • Ok, I will stay tuned for when you find time to try system off.

    In the meantime, I tried the beacon example in SDK 14.2

    I removed all code from the main except __WFE(); loop like you described, and the power consumption was around 1,3µA as expected.

  • I am using SDK 12.3. I will try to flash with SDK 14.2 example today. Also note that I had softdevice in my application, even though it was not enabled I think it may have impact on consumption. 

  • I tired your code with systemOff and the result of consumption is around 200uA. Why would be hfclk running if I did not use it ? Any other ideas what can be wrong ?

Related