Skip to main content
Associate
March 13, 2025
Question

QDEC on STM32H563RG with Zephyr

  • March 13, 2025
  • 1 reply
  • 709 views

Hi all,

I was able to make the qdec on timer1 run and read pulses from an external encoder. Now I wanna generate an interrupt after let's say 100 pulses. That interrupt should toggle a led

I mean the timer has interrupts, but i don't know how to use them in Zephyr.

That is my timerconfig in the devicetree:

tim: &timers1 {
 compatible = "st,stm32-timers";
 reg = <0x40012c00 0x400>;
 clocks = <&rcc STM32_CLOCK(APB2, 11U)>;
 resets = <&rctl STM32_RESET(APB2, 11U)>;
 interrupts = <44 0>;
 interrupt-names = "cc";
 st,prescaler = <0>;
 status = "okay";

 qdec: qdec {
 compatible = "st,stm32-qdec";
 status = "okay";
 pinctrl-0 = <&tim1_ch1_pe9 &tim1_ch2_pe11>;
 pinctrl-names = "default";
 st,input-polarity-inverted;
 st,input-filter-level = <15>; // FDIV32_N8-->15: Fs = F_clk/32, N=8
 st,counts-per-revolution = <700>;
 st,encoder-mode = <1>; //???????
 };
};
 
Code to read the pulses:
 
const struct device *const dev = DEVICE_DT_GET(DT_ALIAS(qdec0));
const struct qdec_stm32_dev_cfg *dev_cfg = (const struct qdec_stm32_dev_cfg *)dev->config;
position = LL_TIM_GetCounter(dev_cfg->timer_inst);
printk("Number of pulses: %" PRIi32 "\n", position);
 
Is cc interrupt the correct one or should I use the "update interrupt"?
 
Do I have to use the counter-module? Zephyr API Documentation: Counter Interface
 
Thank you for your help

Best regards and greetings from Austria
Lukas

1 reply

ST Employee
March 26, 2025

Hello @Lukas4

The update interrupt is triggered when the counter reaches the ARR value, which you can set to the number of pulses you want to count before the interrupt occurs. 

 

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.
Lukas4Author
Associate
March 31, 2025

Hi @Sarra.S 

 

Sory for my late response. I was busy with other tasks. THank you for your help. 

That means I should use the "Update interrupt" of timer 1. That is pos 42 (prio 50) in the IR vector table.

Can I use the IR-API to init the update interrupt?

IRQ_CONNECT(MY_DEV_IRQ, MY_DEV_PRIO, my_isr, MY_ISR_ARG, MY_IRQ_FLAGS); irq_enable(MY_DEV_IRQ);

Do I have to use the counter_ll_stm32_timer.c lib (zephyr/drivers/counter/counter_ll_stm32_timer.c at main · zephyrproject-rtos/zephyr) to set the ARR register??

 

Best regards

Lukas