Skip to content

cpu/nrf5xcommon/qdec: allow configuration of count threshold for callback - #22708

Open
leandrolanzieri wants to merge 3 commits into
RIOT-OS:masterfrom
leandrolanzieri:pr/cpu/nrf5xcommon/qdec_add_max_count
Open

leandrolanzieri wants to merge 3 commits into
RIOT-OS:masterfrom
leandrolanzieri:pr/cpu/nrf5xcommon/qdec_add_max_count

Conversation

@leandrolanzieri

Copy link
Copy Markdown
Contributor

Contribution description

The current implementation of the QDEC peripheral driver in nrf5x has some limitations.

  1. Unlike the peripheral in the stm32, this driver doesn't allow setting a maximum count for the hardware accumulator to select when the user callback is called. Currently, applications only get a callback if the count has reached -1023 or 1024. Otherwise they need to constantly poll the driver. Which makes it impractical for event-driven applications.

  2. Although the QDEC peripheral API uses int32_t for the count, this driver is capped by its hardware accumulator.

This PR proposes using a software accumulator for the nrf5x driver, which is updated during the sample report interrupt of the peripheral (instead of the overflow interrupt). Given the way the peripheral works, this has certain caveats, which are documented in the extended qdec_cont_t structure. Depending on the use case, users can set their report_period to trade off the amount of interrupts triggered when the encoder moves against the potential overshoot of the configured threshold. The driver allows setting a sample period of QDEC_REPORTPER_REPORTPER_1Smpl in case the user wants to get the callback exactly at that value, and doesn't care about the triggered interrupts.

Unlike the stm32 implementation, the interrupt doesn't clear the accumulator (this is up to the user), so users can tell which direction of movement triggered the count overflow, and (in this case) whether the count surpassed the maximum value due to a fast encoder.

Testing procedure

tests/periph/qdec is a good starting point to test this. You can set in your nrf board different values of maximum count and report period.

Issues/PRs references

The first three commits in this PR belong to #22700, so you can just ignore them for now. I'll rebase once that's in.

Declaration of AI-Tools / LLMs usage:

AI-Tools / LLMs that were used are:

  • Claude code opus 5: discussion and initial revision of code.

@leandrolanzieri leandrolanzieri added Type: new feature The issue requests / The PR implemements a new feature for RIOT AI: Helped PR/Issue uses AI sparingly, e.g. code inline assistant, debugging with AI, etc. labels Sep 15, 2026
@github-actions github-actions Bot added Platform: ARM Platform: This PR/issue effects ARM-based platforms Area: tests Area: tests and testing framework Area: drivers Area: Device drivers Area: cpu Area: CPU/MCU ports labels Sep 15, 2026
@crasbe
crasbe requested a review from mguetschow September 15, 2026 08:26
Comment thread cpu/nrf5x_common/periph/qdec.c
Comment thread cpu/nrf5x_common/include/periph_cpu_common.h Outdated
@leandrolanzieri
leandrolanzieri force-pushed the pr/cpu/nrf5xcommon/qdec_add_max_count branch from 162e27e to 7dc1f0b Compare September 15, 2026 12:03
@leandrolanzieri

Copy link
Copy Markdown
Contributor Author

Now that #22700 is merged I rebased and also used the chance to rename the struct member to callback_threshold

Comment thread cpu/nrf5x_common/include/periph_cpu_common.h Outdated
Comment thread cpu/nrf5x_common/include/periph_cpu_common.h Outdated
Comment thread cpu/nrf5x_common/include/periph_cpu_common.h Outdated
@kfessel kfessel changed the title cpu/nrf5xcommon/qdec: allow configuration of maximum count for callback cpu/nrf5xcommon/qdec: allow configuration of count threshold for callback Sep 15, 2026
@kfessel

kfessel commented Sep 15, 2026

Copy link
Copy Markdown
Contributor

You may Squash

Just to be sure: -since i do not have device at hand-

Did you test this? (short setup description and short results 'd be nice)

Comment thread cpu/nrf5x_common/periph/qdec.c Outdated
Comment thread cpu/nrf5x_common/include/periph_cpu_common.h Outdated
@leandrolanzieri

Copy link
Copy Markdown
Contributor Author

Test setup

I tested these changes on a seeedstudio-xiao-nrf52840-sense board with a KY-040 360 rotary encoder module.

20260916_115012

I modified the periph_conf.h of the board like so:

static const qdec_conf_t qdec_config[] = {
    {
        .a_pin = GPIO_PIN(1, 15),                           /* Xiao D10 */
        .b_pin = GPIO_PIN(1, 14),                           /* Xiao D9 */
        .led_pin = GPIO_PIN(0, 13), /* And the first LED */
        .sample_period = QDEC_SAMPLEPER_SAMPLEPER_128us,
        .debounce_filter = true,
        .led_active_state = false,
        .report_period = QDEC_REPORTPER_REPORTPER_10Smpl,
        .callback_threshold = 1024
    },
};

The test application is a variation of the qdec test that does not clear the counter on the main loop so I can see the count at the moment of the callback:

static void handler(void *arg)
{
    qdec_t qdec = (qdec_t)(uintptr_t)arg;
    int32_t value = qdec_read_and_reset(QDEC_DEV(qdec));
    printf("QDEC %lu counter overflow with = %ld\n", (unsigned long int)qdec, (long int)value);
}

int main(void)
{
    unsigned i;
    int32_t value;

    for (i = 0; i < QDEC_NUMOF; i++) {
        int32_t error = qdec_init(QDEC_DEV(i), QDEC_X4, handler, (void *)(uintptr_t)i);
        if (error) {
            fprintf(stderr, "Mode not supported!\n");
            return error;
        }
    }

    while (1) {
        for (i = 0; i < QDEC_NUMOF; i++) {
            value = qdec_read(QDEC_DEV(i));
            printf("QDEC %lu = %ld\n", (unsigned long int)i, (long int)value);
        }
        xtimer_sleep(1);
    }

    return 0;
}

During the tests I rotated the encoder in both directions at various speeds.

Test Results

Threshold = 4 counts, Report = 1 sample

periph_conf.h

        .report_period = QDEC_REPORTPER_REPORTPER_1Smpl,
        .callback_threshold = 4
Test output

2026-09-16 11:43:13,426 # QDEC 0 = 0
2026-09-16 11:43:14,419 # QDEC 0 = 0
2026-09-16 11:43:15,413 # QDEC 0 = 0
2026-09-16 11:43:16,406 # QDEC 0 = 0
2026-09-16 11:43:17,304 # QDEC 0 counter overflow with = 4
2026-09-16 11:43:17,400 # QDEC 0 = 0
2026-09-16 11:43:17,842 # QDEC 0 counter overflow with = 4
2026-09-16 11:43:18,394 # QDEC 0 = 0
2026-09-16 11:43:18,554 # QDEC 0 counter overflow with = 4
2026-09-16 11:43:19,316 # QDEC 0 counter overflow with = 4
2026-09-16 11:43:19,388 # QDEC 0 = 0
2026-09-16 11:43:20,382 # QDEC 0 = 0
2026-09-16 11:43:20,528 # QDEC 0 counter overflow with = 4
2026-09-16 11:43:20,568 # QDEC 0 counter overflow with = 4
2026-09-16 11:43:20,606 # QDEC 0 counter overflow with = 4
2026-09-16 11:43:20,635 # QDEC 0 counter overflow with = 4
2026-09-16 11:43:20,659 # QDEC 0 counter overflow with = 4
2026-09-16 11:43:20,684 # QDEC 0 counter overflow with = 4
2026-09-16 11:43:20,713 # QDEC 0 counter overflow with = 4
2026-09-16 11:43:20,760 # QDEC 0 counter overflow with = 4
2026-09-16 11:43:20,861 # QDEC 0 counter overflow with = 4
2026-09-16 11:43:21,375 # QDEC 0 = 0
2026-09-16 11:43:22,369 # QDEC 0 = 0
2026-09-16 11:43:23,176 # QDEC 0 counter overflow with = -4
2026-09-16 11:43:23,216 # QDEC 0 counter overflow with = -4
2026-09-16 11:43:23,258 # QDEC 0 counter overflow with = -4
2026-09-16 11:43:23,295 # QDEC 0 counter overflow with = -4
2026-09-16 11:43:23,329 # QDEC 0 counter overflow with = -4
2026-09-16 11:43:23,361 # QDEC 0 counter overflow with = -4
2026-09-16 11:43:23,363 # QDEC 0 = 0
2026-09-16 11:43:23,405 # QDEC 0 counter overflow with = -4
2026-09-16 11:43:24,357 # QDEC 0 = 0
2026-09-16 11:43:25,351 # QDEC 0 = 0
2026-09-16 11:43:26,344 # QDEC 0 = 0

Threshold = 1, Report = 10 samples

        .report_period = QDEC_REPORTPER_REPORTPER_10Smpl,
        .callback_threshold = 1
Test output



2026-09-16 11:47:20,572 # QDEC 0 = 0
2026-09-16 11:47:20,573 # QDEC 0 counter overflow with = -1
2026-09-16 11:47:20,652 # QDEC 0 = 0
2026-09-16 11:47:21,155 # QDEC 0 counter overflow with = 1
2026-09-16 11:47:21,248 # QDEC 0 counter overflow with = 1
2026-09-16 11:47:21,283 # QDEC 0 counter overflow with = 1
2026-09-16 11:47:21,300 # QDEC 0 counter overflow with = 1
2026-09-16 11:47:21,646 # QDEC 0 = 0
2026-09-16 11:47:21,937 # QDEC 0 counter overflow with = 1
2026-09-16 11:47:21,993 # QDEC 0 counter overflow with = 1
2026-09-16 11:47:22,016 # QDEC 0 counter overflow with = 1
2026-09-16 11:47:22,030 # QDEC 0 counter overflow with = 1
2026-09-16 11:47:22,640 # QDEC 0 = 0
2026-09-16 11:47:22,709 # QDEC 0 counter overflow with = -1
2026-09-16 11:47:22,733 # QDEC 0 counter overflow with = -1
2026-09-16 11:47:22,745 # QDEC 0 counter overflow with = -1
2026-09-16 11:47:22,752 # QDEC 0 counter overflow with = -1
2026-09-16 11:47:23,393 # QDEC 0 counter overflow with = -1
2026-09-16 11:47:23,428 # QDEC 0 counter overflow with = -1
2026-09-16 11:47:23,442 # QDEC 0 counter overflow with = -1
2026-09-16 11:47:23,449 # QDEC 0 counter overflow with = -1
2026-09-16 11:47:23,634 # QDEC 0 = 0
2026-09-16 11:47:23,955 # QDEC 0 counter overflow with = 1
2026-09-16 11:47:23,993 # QDEC 0 counter overflow with = 1
2026-09-16 11:47:24,010 # QDEC 0 counter overflow with = 1
2026-09-16 11:47:24,021 # QDEC 0 counter overflow with = 1
2026-09-16 11:47:24,538 # QDEC 0 counter overflow with = 1
2026-09-16 11:47:24,572 # QDEC 0 counter overflow with = 1
2026-09-16 11:47:24,587 # QDEC 0 counter overflow with = 1
2026-09-16 11:47:24,596 # QDEC 0 counter overflow with = 1
2026-09-16 11:47:24,627 # QDEC 0 = 0
2026-09-16 11:47:25,124 # QDEC 0 counter overflow with = -1
2026-09-16 11:47:25,156 # QDEC 0 counter overflow with = -1
2026-09-16 11:47:25,167 # QDEC 0 counter overflow with = -1
2026-09-16 11:47:25,173 # QDEC 0 counter overflow with = -1
2026-09-16 11:47:25,621 # QDEC 0 = 0
2026-09-16 11:47:26,614 # QDEC 0 = 0
2026-09-16 11:47:27,607 # QDEC 0 = 0
2026-09-16 11:47:28,599 # QDEC 0 = 0
2026-09-16 11:47:29,592 # QDEC 0 = 0
2026-09-16 11:47:30,585 # QDEC 0 = 0
2026-09-16 11:47:31,578 # QDEC 0 = 0
2026-09-16 11:47:32,571 # QDEC 0 = 0
2026-09-16 11:47:33,564 # QDEC 0 = 0
2026-09-16 11:47:34,315 # QDEC 0 counter overflow with = 1
2026-09-16 11:47:34,336 # QDEC 0 counter overflow with = 1
2026-09-16 11:47:34,347 # QDEC 0 counter overflow with = 1
2026-09-16 11:47:34,354 # QDEC 0 counter overflow with = 1
2026-09-16 11:47:34,369 # QDEC 0 counter overflow with = 1
2026-09-16 11:47:34,390 # QDEC 0 counter overflow with = 1
2026-09-16 11:47:34,398 # QDEC 0 counter overflow with = 1
2026-09-16 11:47:34,403 # QDEC 0 counter overflow with = 1
2026-09-16 11:47:34,412 # QDEC 0 counter overflow with = 1
2026-09-16 11:47:34,424 # QDEC 0 counter overflow with = 1
2026-09-16 11:47:34,432 # QDEC 0 counter overflow with = 1
2026-09-16 11:47:34,436 # QDEC 0 counter overflow with = 1
2026-09-16 11:47:34,439 # QDEC 0 counter overflow with = 1
2026-09-16 11:47:34,448 # QDEC 0 counter overflow with = 1
2026-09-16 11:47:34,453 # QDEC 0 counter overflow with = 1
2026-09-16 11:47:34,458 # QDEC 0 counter overflow with = 1
2026-09-16 11:47:34,465 # QDEC 0 counter overflow with = 1
2026-09-16 11:47:34,475 # QDEC 0 counter overflow with = 1
2026-09-16 11:47:34,480 # QDEC 0 counter overflow with = 1
2026-09-16 11:47:34,485 # QDEC 0 counter overflow with = 1
2026-09-16 11:47:34,495 # QDEC 0 counter overflow with = 1
2026-09-16 11:47:34,511 # QDEC 0 counter overflow with = 1
2026-09-16 11:47:34,517 # QDEC 0 counter overflow with = 1
2026-09-16 11:47:34,523 # QDEC 0 counter overflow with = 1
2026-09-16 11:47:34,535 # QDEC 0 counter overflow with = 1
2026-09-16 11:47:34,551 # QDEC 0 counter overflow with = 1
2026-09-16 11:47:34,558 # QDEC 0 counter overflow with = 1
2026-09-16 11:47:34,558 # QDEC 0 = 0
2026-09-16 11:47:34,564 # QDEC 0 counter overflow with = 1
2026-09-16 11:47:34,577 # QDEC 0 counter overflow with = 1
2026-09-16 11:47:34,591 # QDEC 0 counter overflow with = 1
2026-09-16 11:47:34,598 # QDEC 0 counter overflow with = 1
2026-09-16 11:47:34,604 # QDEC 0 counter overflow with = 1
2026-09-16 11:47:35,097 # QDEC 0 counter overflow with = -1
2026-09-16 11:47:35,110 # QDEC 0 counter overflow with = -1
2026-09-16 11:47:35,117 # QDEC 0 counter overflow with = -1
2026-09-16 11:47:35,122 # QDEC 0 counter overflow with = -1
2026-09-16 11:47:35,128 # QDEC 0 counter overflow with = -1
2026-09-16 11:47:35,140 # QDEC 0 counter overflow with = -1
2026-09-16 11:47:35,146 # QDEC 0 counter overflow with = -1
2026-09-16 11:47:35,151 # QDEC 0 counter overflow with = -1
2026-09-16 11:47:35,159 # QDEC 0 counter overflow with = -1
2026-09-16 11:47:35,170 # QDEC 0 counter overflow with = -1
2026-09-16 11:47:35,177 # QDEC 0 counter overflow with = -1
2026-09-16 11:47:35,183 # QDEC 0 counter overflow with = -1
2026-09-16 11:47:35,192 # QDEC 0 counter overflow with = -1
2026-09-16 11:47:35,205 # QDEC 0 counter overflow with = -1
2026-09-16 11:47:35,211 # QDEC 0 counter overflow with = -1
2026-09-16 11:47:35,215 # QDEC 0 counter overflow with = -1
2026-09-16 11:47:35,221 # QDEC 0 counter overflow with = -1
2026-09-16 11:47:35,230 # QDEC 0 counter overflow with = -1
2026-09-16 11:47:35,235 # QDEC 0 counter overflow with = -1
2026-09-16 11:47:35,240 # QDEC 0 counter overflow with = -1
2026-09-16 11:47:35,245 # QDEC 0 counter overflow with = -1
2026-09-16 11:47:35,252 # QDEC 0 counter overflow with = -1
2026-09-16 11:47:35,257 # QDEC 0 counter overflow with = -1
2026-09-16 11:47:35,259 # QDEC 0 counter overflow with = -1
2026-09-16 11:47:35,264 # QDEC 0 counter overflow with = -1
2026-09-16 11:47:35,270 # QDEC 0 counter overflow with = -1
2026-09-16 11:47:35,275 # QDEC 0 counter overflow with = -1
2026-09-16 11:47:35,278 # QDEC 0 counter overflow with = -1
2026-09-16 11:47:35,284 # QDEC 0 counter overflow with = -1
2026-09-16 11:47:35,290 # QDEC 0 counter overflow with = -1
2026-09-16 11:47:35,294 # QDEC 0 counter overflow with = -1
2026-09-16 11:47:35,298 # QDEC 0 counter overflow with = -1
2026-09-16 11:47:35,304 # QDEC 0 counter overflow with = -1
2026-09-16 11:47:35,312 # QDEC 0 counter overflow with = -1
2026-09-16 11:47:35,317 # QDEC 0 counter overflow with = -1
2026-09-16 11:47:35,320 # QDEC 0 counter overflow with = -1
2026-09-16 11:47:35,329 # QDEC 0 counter overflow with = -1
2026-09-16 11:47:35,355 # QDEC 0 counter overflow with = -1
2026-09-16 11:47:35,551 # QDEC 0 = 0
2026-09-16 11:47:36,546 # QDEC 0 = 0

Threshold = 1024, Report = 10 samples (same limit as before)

        .report_period = QDEC_REPORTPER_REPORTPER_10Smpl,
        .callback_threshold = 1024
Test output

2026-09-16 11:48:29,690 # QDEC 0 = 0
2026-09-16 11:48:29,783 # QDEC 0 = 0
2026-09-16 11:48:30,776 # QDEC 0 = 0
2026-09-16 11:48:31,770 # QDEC 0 = 0
2026-09-16 11:48:32,763 # QDEC 0 = 62
2026-09-16 11:48:33,756 # QDEC 0 = 114
2026-09-16 11:48:34,750 # QDEC 0 = 170
2026-09-16 11:48:35,742 # QDEC 0 = 247
2026-09-16 11:48:36,736 # QDEC 0 = 329
2026-09-16 11:48:37,729 # QDEC 0 = 416
2026-09-16 11:48:38,722 # QDEC 0 = 498
2026-09-16 11:48:39,715 # QDEC 0 = 566
2026-09-16 11:48:40,709 # QDEC 0 = 642
2026-09-16 11:48:41,702 # QDEC 0 = 735
2026-09-16 11:48:42,695 # QDEC 0 = 810
2026-09-16 11:48:43,688 # QDEC 0 = 870
2026-09-16 11:48:44,681 # QDEC 0 = 942
2026-09-16 11:48:45,675 # QDEC 0 = 1003
2026-09-16 11:48:45,750 # QDEC 0 counter overflow with = 1024
2026-09-16 11:48:46,667 # QDEC 0 = 23
2026-09-16 11:48:47,660 # QDEC 0 = 70
2026-09-16 11:48:48,653 # QDEC 0 = 70
2026-09-16 11:48:49,648 # QDEC 0 = 70
2026-09-16 11:48:50,640 # QDEC 0 = 70
2026-09-16 11:48:51,633 # QDEC 0 = 70
2026-09-16 11:48:52,626 # QDEC 0 = 38
2026-09-16 11:48:53,619 # QDEC 0 = -30
2026-09-16 11:48:54,612 # QDEC 0 = -86
2026-09-16 11:48:55,606 # QDEC 0 = -150
2026-09-16 11:48:56,599 # QDEC 0 = -223
2026-09-16 11:48:57,591 # QDEC 0 = -322
2026-09-16 11:48:58,584 # QDEC 0 = -394
2026-09-16 11:48:59,577 # QDEC 0 = -470
2026-09-16 11:49:00,571 # QDEC 0 = -574
2026-09-16 11:49:01,563 # QDEC 0 = -650
2026-09-16 11:49:02,557 # QDEC 0 = -718
2026-09-16 11:49:03,550 # QDEC 0 = -774
2026-09-16 11:49:04,543 # QDEC 0 = -850
2026-09-16 11:49:05,536 # QDEC 0 = -919
2026-09-16 11:49:06,529 # QDEC 0 = -982
2026-09-16 11:49:07,087 # QDEC 0 counter overflow with = -1024
2026-09-16 11:49:07,522 # QDEC 0 = -30
2026-09-16 11:49:08,516 # QDEC 0 = -52
2026-09-16 11:49:09,508 # QDEC 0 = -52
2026-09-16 11:49:10,501 # QDEC 0 = -52
2026-09-16 11:49:11,495 # QDEC 0 = -52

This extends the QDEC driver for nrf5x platforms to extend the hardware
counter with a software one. In addition to extending the count range,
users can now configure the maximum value at which a callback is called.
@leandrolanzieri
leandrolanzieri force-pushed the pr/cpu/nrf5xcommon/qdec_add_max_count branch from 284acde to d8abab1 Compare September 16, 2026 10:16
@kfessel

kfessel commented Sep 16, 2026

Copy link
Copy Markdown
Contributor

from your test Threshold = 1, Report = 10 samples
for

       .report_period = QDEC_REPORTPER_REPORTPER_10Smpl,
        .callback_threshold = 1

i would have expected

QDEC 0 counter overflow with = 10
or 
QDEC 0 counter overflow with = -10

or at least not always 1 like in you test results

Am i missing somthing?

@leandrolanzieri

leandrolanzieri commented Sep 16, 2026

Copy link
Copy Markdown
Contributor Author

am i missing somthing?

I think so. report_period is a configuration of the peripheral itself. It tells it when an interrupt is generated in amount of samples. So if you have a sample period of 128 us/sample (that is, the peripheral samples the pins every 128 us), and you configure a report period of 10 samples, then the peripheral will only trigger an interrupt every 128 us/sample * 10 samples = 1280 us in case the (hardware) accumulator changed during time (i.e., there was activity). Even if during that period there has been one count.

Samples are different from counts in this context. Does this clarify?

Comment on lines +79 to 87
if (cb && (conf(qdec)->callback_threshold > 0)) {
dev(qdec)->EVENTS_REPORTRDY = 0;
dev(qdec)->INTENSET = QDEC_INTENSET_REPORTRDY_Msk;
NVIC_EnableIRQ(QDEC_IRQn);
dev(qdec)->INTENSET = QDEC_INTENSET_ACCOF_Msk;
}
else {
NVIC_DisableIRQ(QDEC_IRQn);
dev(qdec)->INTENCLR = QDEC_INTENCLR_ACCOF_Msk;
dev(qdec)->INTENCLR = QDEC_INTENCLR_REPORTRDY_Msk;
}

@kfessel kfessel Sep 16, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how about keeping the overflow behaviour when callback_threshold is 0

Suggested change
if (cb && (conf(qdec)->callback_threshold > 0)) {
dev(qdec)->EVENTS_REPORTRDY = 0;
dev(qdec)->INTENSET = QDEC_INTENSET_REPORTRDY_Msk;
NVIC_EnableIRQ(QDEC_IRQn);
dev(qdec)->INTENSET = QDEC_INTENSET_ACCOF_Msk;
}
else {
NVIC_DisableIRQ(QDEC_IRQn);
dev(qdec)->INTENCLR = QDEC_INTENCLR_ACCOF_Msk;
dev(qdec)->INTENCLR = QDEC_INTENCLR_REPORTRDY_Msk;
}
NVIC_DisableIRQ(QDEC_IRQn);
if ((conf(qdec)->callback_threshold > 0)) {
dev(qdec)->EVENTS_REPORTRDY = 0;
dev(qdec)->INTENSET = QDEC_INTENSET_REPORTRDY_Msk;
}
else {
dev(qdec)->INTENSET = QDEC_INTENCLR_ACCOF_Msk;
}
if (cb) {
NVIC_EnableIRQ(QDEC_IRQn);
}

reasoning:
EVENTS_REPORTRDY is time based (and count) -> less sleep

overflow will happen less often (depends on the only on the counted distance) -> has a real advantage

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how about keeping the overflow behaviour when callback_threshold is 0

Do you mean that the user would get a callback when the count reaches 1024 or keep the software counter and call back every multiple of 1024? I'm not sure about how this would work.

Keep in mind that this PR also include the software counter.

We could have callback_threshold = 0 a sort of legacy mode that restricts count to +/- 1024 and only uses the overflow interrupt. Probably not the nicest API but it would cover both cases.

Do you have any thoughts on that?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my thought and reasoning is in the follow up comment

so yes I think it would be nice to have callback_threshold == 0 + callback function to behave like a legacy fallback (overflow only) to avoid the extra interrupts that might wake up the cpu and only have overflow interrupts)

@kfessel

kfessel commented Sep 16, 2026

Copy link
Copy Markdown
Contributor

I think so. report_period is a configuration of the peripheral itself. It tells it when an interrupt is generated in amount of samples. So if you have a sample period of 128 us/sample (that is, the peripheral samples the pins every 128 us), and you configure a report period of 10 samples, then the peripheral will only trigger an interrupt every 128 us/sample * 10 samples = 1280 us in case the (hardware) accumulator changed during time (i.e., there was activity). Even if during that period there has been one count.

Samples are different from counts in this context. Does this clarify?

thanks for the clarification so every little step now creates an irq (not callback) after some time and before that only happen when there where to many steps happend ->

  • before there was one irq after 1024 steps

  • now its an irq if there is one step and some time ( measured in sample period * report_period) went by (this makes for 1024 more irq (if direction stays the same)).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

AI: Helped PR/Issue uses AI sparingly, e.g. code inline assistant, debugging with AI, etc. Area: cpu Area: CPU/MCU ports Area: drivers Area: Device drivers Area: tests Area: tests and testing framework Platform: ARM Platform: This PR/issue effects ARM-based platforms Type: new feature The issue requests / The PR implemements a new feature for RIOT

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants