The alarm clock does not work

Hello.

I am trying to run an alarm clock using the RTC calendar. In the main loop I see the time being printed, but there is no response from the alarm callback, which I set for 10 seconds.

Here is part of my code with alarm and rtc timer

    uint32_t ticks;
    uint64_t microseconds;
    uint32_t seconds;
    uint32_t remainder_us;

    uint32_t current_ticks, alarm_ticks;
    uint32_t diff_seconds, diff_us;
    time_t current_epoch_time, target_epoch_time;

    
    if (!device_is_ready(counter_dev)) {
        printk("Counter device is not ready\n");
        return;
    }

    // starting counter
    counter_start(counter_dev);

    struct tm init_time = {
        .tm_year = 2025 - 1900,
        .tm_mon  = 1 - 1,
        .tm_mday = 1,
        .tm_hour = 0,
        .tm_min  = 0,
        .tm_sec  = 0,
    };

    time_t start_epoch_time = mktime(&init_time);
    current_epoch_time = mktime(&init_time);

    struct tm target_time = {
        .tm_year = 2025 - 1900,
        .tm_mon  = 1 - 1,   
        .tm_mday = 1,
        .tm_hour = 0,
        .tm_min  = 0,
        .tm_sec  = 10,
    };
    target_epoch_time = mktime(&target_time);

        /* Обчислюємо різницю в секундах */
        diff_seconds = (uint32_t)(target_epoch_time - current_epoch_time);
        printk("diff_seconds = %d\n",diff_seconds);
        /* Конвертуємо різницю в мікросекунди */
        diff_us = diff_seconds * 1000000U;
        printk("diff_us =  = %d\n",diff_us);

        /* Отримуємо поточний показник таймера */
        counter_get_value(counter_dev, &current_ticks);
        
        /* Обчислюємо значення тика для спрацювання будильника */
        alarm_ticks = current_ticks + counter_us_to_ticks(counter_dev, diff_us);
        printk("current_ticks = %d\n",current_ticks);
        printk("alarm_ticks = %d\n",alarm_ticks);
        /* Налаштування параметрів будильника */
        struct counter_alarm_cfg alarm_cfg = {
            .ticks    = alarm_ticks,
            .flags    = COUNTER_ALARM_CFG_ABSOLUTE, // Використовуйте цей прапорець, якщо підтримується
            .callback = alarm_callback,
            .user_data = NULL,
        };
        
        /* Встановлюємо будильник на каналі 0 */
        err = counter_set_channel_alarm(counter_dev, 0, &alarm_cfg);
        if (err < 0) {
            LOG_ERR("Failed to configure set_channel_alarm");
            return -1;
        }

    for (;;) {
      counter_get_value(counter_dev, &ticks);

        // Конвертуємо тики в мікросекунди
        microseconds = counter_ticks_to_us(counter_dev, ticks);

        // Визначаємо секунди та залишок мікросекунд
        seconds = (uint32_t)(microseconds / 1000000U);
        remainder_us = (uint32_t)(microseconds % 1000000U);

        // Обчислюємо поточний час
        time_t current_epoch_time = start_epoch_time + seconds;
        struct tm *current_time = gmtime(&current_epoch_time);

        // Виводимо поточний час у форматі YYYY-MM-DD HH:MM:SS
        printk("Поточний час: %04d-%02d-%02d %02d:%02d:%02d.%06u\n",
               current_time->tm_year + 1900,
               current_time->tm_mon + 1,
               current_time->tm_mday,
               current_time->tm_hour,
               current_time->tm_min,
               current_time->tm_sec,
               remainder_us);

        // Затримка на 1 секунду
        k_sleep(K_SECONDS(3));
    }

Here is my prj.conf

# Logger module
CONFIG_LOG=y

# Button and LED library


# Bluetooth LE
CONFIG_BT=y

CONFIG_BT_PERIPHERAL=y
CONFIG_BT_DEVICE_NAME="MY_LBS1"
CONFIG_BT_SMP=y

# Increase stack size for the main thread and System Workqueue
CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE=2048
CONFIG_MAIN_STACK_SIZE=2048

CONFIG_NVS=y



CONFIG_NRFX_TIMER1=y
CONFIG_ADC=y
CONFIG_SPI=y
CONFIG_GPIO=y
CONFIG_PRINTK=y
CONFIG_USE_SEGGER_RTT=y

CONFIG_NCS_SAMPLE_MCUMGR_BT_OTA_DFU=y

CONFIG_RTC=y
CONFIG_COUNTER=y

Also here is my logs from RTT:

diff_seconds = 10
diff_us  = 10000000
current_ticks = 85886
alarm_ticks = 413566

Поточний час: 2025-01-01 00:00:02.620666
Поточний час: 2025-01-01 00:00:05.620819
Поточний час: 2025-01-01 00:00:08.620971

I use NCS 2.8.0

Let me know if you need any more information. I would be very grateful for help.

Parents
  • Hello,

    I went through the code you shared, and I am not sure about the root cause of the issue. Since the time is being printed, the counter device is functioning and initialized properly. Can you log the return value of counter_set_channel_alarm() to ensure that the alarm was successfully configured? I can see that your application is mostly based on the sample application, but are you working with a custom board, or is this a development kit? Which Nordic chip or DK are you using here?

    Kind regards,
    Abhijith

Reply
  • Hello,

    I went through the code you shared, and I am not sure about the root cause of the issue. Since the time is being printed, the counter device is functioning and initialized properly. Can you log the return value of counter_set_channel_alarm() to ensure that the alarm was successfully configured? I can see that your application is mostly based on the sample application, but are you working with a custom board, or is this a development kit? Which Nordic chip or DK are you using here?

    Kind regards,
    Abhijith

Children
No Data
Related