cancel
Showing results for 
Search instead for 
Did you mean: 

STM32L412 Wake from Low Power Sleep when Encoder is turned

Lanceh
Associate III

Hi, what is the best way to wake from Low Power Sleep when an Encoder is rotated?

I have TIM2 setup in encoder mode and it works when in Low Power Run. I just want to switch back from LP Sleep to LP Run when it's turned. Looking in stm32l4xx_hal_tim.c I have not been able get a solution.

This did not work:

//Wake up from Encoder

void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)

{

if (htim->Instance == TIM2)

{

LowPowerSleepToRun();

}

}

I have TIM2 global interrupt set in CubeMX.

I have buttons that do this via EXTI, so the LowPowerSleepToRun function works.

If you can keep the reply to HAL and CubeMX solutions I would appreciate it.

Thanks in advance, Lance.

3 REPLIES 3
waclawek.jan
Super User

EXTI on the encoder pins?

And no, Cube/HAL authors didn't deem this typical enough to support, so I guess for you the answer is"there's no way".

Some users hacked around this by configuring the pin twice, once as EXTI input then as AF needed for the timer to work, but this while may work is against the grain of Cube/HAL so it may for example cease to work with some future Cube update, if you are into the things. 

JW

Thanks JW, that's disappointing.

So there's no 'on change' interrupt for encoder mode?

How do you configure a pin twice in CubeMX? I must not be devious enough.

I did define an unused pin, PC1 as EXTI hoping interrupt system would also catch a change on PA1 which is connected to the encoder, but that did not work either.

Then I thought why not connect one of the Encoder pins to PC1 as well as the encoder via a resistor. Bingo! that worked.

So now I think I have a system that will wake up from LP Sleep when the encoder is turned. I'll have to test it a bit more, but I can't see anything wrong with this approach at the moment.

Thanks again for your help.

Lance.

waclawek.jan
Super User

> So there's no 'on change' interrupt for encoder mode?

You of course can enable capture on CH1 and/or CH2 in timer, and interrupt upon them. I just have no idea how would you do it in Cube/CubeMX or if it's even possible, I don't use Cube/CubeMX.

> How do you configure a pin twice in CubeMX?

Not in CubeMX, but using Cube/HAL (as the "library"), you just call the pin initialization function twice with different content of input "config" structure.

> Then I thought why not connect one of the Encoder pins to PC1 as well as the encoder via a resistor. Bingo! that worked.

I see no problem with that either, except that you've spent one extra pin.

JW