LED Timing Issue in Timer Peripheral Example when Adjusting time_ms Value

Hi,

I am currently developing a project using the nRF52810 as my controller, with SEGGER Embedded Studio (version 5.42a) and SDK version 17.0.2 as my development environment. During the development phase, I am testing the timer peripheral example project from the directory nRF5_SDK_17.0.2_d674dde\examples\peripheral\timer\pca10040\blank\ses. I flashed the code onto the nRF52832 PCA10040 development board, and the four LEDs on the board turn on and off based on the time_ms value.

The issue is that when I change the time_ms value from 1000 (1 second) to 300000 (5 minutes), the LED indications are incorrect. The LEDs turn on and off within 30 seconds instead of 5 minutes. However, when I change the time_ms value to up to 4 minutes, the LEDs behave as expected, with the correct 4-minute delay. What could be causing this issue?

Studio version 5.42a

SDK: nRF5 SDK 17.0.2

  • The following is the current example project that we are working on

    /**
     * Copyright (c) 2014 - 2020, 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.
     *
     */
    /** @file
     * @defgroup nrf_dev_timer_example_main main.c
     * @{
     * @ingroup nrf_dev_timer_example
     * @brief Timer Example Application main file.
     *
     * This file contains the source code for a sample application using Timer0.
     *
     */
    
    #include <stdbool.h>
    #include <stdint.h>
    #include "nrf.h"
    #include "nrf_drv_timer.h"
    #include "bsp.h"
    #include "app_error.h"
    
    
    
    
    const nrf_drv_timer_t TIMER_LED = NRF_DRV_TIMER_INSTANCE(0);
    
    /**
     * @brief Handler for timer events.
     */
    void timer_led_event_handler(nrf_timer_event_t event_type, void* p_context)
    {
        static uint32_t i;
        int j=0;
        uint32_t led_to_invert = ((i++) % LEDS_NUMBER);
    
        switch (event_type)
        {
            case NRF_TIMER_EVENT_COMPARE0:
                bsp_board_led_invert(led_to_invert);
                //nrf_gpio_pin_toggle(12);
                
                
                break;
    
            default:
                //Do nothing.
                break;
        }
    }
    
    
    /**
     * @brief Function for main application entry.
     */
    int main(void)
    {
        uint32_t time_ms = 300000; //Time(in miliseconds) between consecutive compare events.
        uint32_t time_ticks;
        uint32_t err_code = NRF_SUCCESS;
    
        //Configure all leds on board.
        bsp_board_init(BSP_INIT_LEDS);
        //nrf_gpio_cfg_output(12);
        //nrf_gpio_cfg_output(11);
    
        //Configure TIMER_LED for generating simple light effect - leds on board will invert his state one after the other.
        nrf_drv_timer_config_t timer_cfg = NRF_DRV_TIMER_DEFAULT_CONFIG;
        err_code = nrf_drv_timer_init(&TIMER_LED, &timer_cfg, timer_led_event_handler);
        APP_ERROR_CHECK(err_code);
    
        time_ticks = nrf_drv_timer_ms_to_ticks(&TIMER_LED, time_ms);
    
        nrf_drv_timer_extended_compare(
             &TIMER_LED, NRF_TIMER_CC_CHANNEL0, time_ticks, NRF_TIMER_SHORT_COMPARE0_CLEAR_MASK, true);
    
        nrf_drv_timer_enable(&TIMER_LED);
    
        while (1)
        {
            __WFI();
        }
    }
    
    /** @} */
    

  • Hi,

    When I test your code I get an assert at line 708 in modules/nrfx/hal/nrf_timer.h, which is because the number of ticks calculated from 300000 us is higher than UINT32_MAX (it does not fit in a 32 bit variable). You could solve this by changing the prescaler value so that the timer frequency is lower. For instance, add this line right before the call to  nrf_drv_timer_init():

    timer_cfg.frequency = NRF_TIMER_FREQ_125kHz;

    As you did not get this I assume you have disabled asserts? In that case, it is expected that you see the LED toggling at the wrong frequency, as the number of ticks overflowex the 32 bit, so the upper bits (above 32 bit) disappeared, so the number that is being used is lower than what you intended.

  • Thanks a lot for the valuable response. I understand the mistake and have changed timer_cfg.frequency = NRF_TIMER_FREQ_31250Hz; before the nrf_drv_timer_init, and I am now able to increase the timer value. The LED indicates as per the given timer values. Currently, I am configuring the frequency to the minimum, which is 31250 Hz. Is there any need to modify it to 125 kHz ?.

  • Hi,

    I am glad to hear you got it working. There is no need to modify to 125 kHz, I just picked a value that was low enough.

Related