Because TIM10 and TIM11 lack the 2nd compare register the ICU driver normally uses,
I've also had to add support for an additional input mode:
Code: Select all
/**
* @brief ICU driver mode.
*/
typedef enum {
ICU_INPUT_ACTIVE_HIGH, /**< Trigger on rising edge. */
ICU_INPUT_ACTIVE_LOW, /**< Trigger on falling edge. */
ICU_INPUT_EDGE, /**< Trigger on either edge. */
ICU_INPUT_MODES
} icumode_t;
The new INPUT_EDGE mode configures the counter to generate events on both edges of the input PWM signal.
It uses only one IC register and so is compatible with TIM10 and TIM11
This technique is documented in a couple ST app notes.
It works well so long as the minimum pulse width is longer than the interrupt response time.
Note that the ACTIVE_HIGH and ACTIVE_LOW modes require only that the period of the PWM be longer than the interrupt response time. Thus, very low or high duty cycles may cause a capture overflow when using INPUT_EDGE mode.
When using ICU_INPUT_EDGE, only the period callback is supported. The width callback should be configured as NULL.
The period callback will be called at each edge of the input signal.
It will typically have to check the signal's polarity to determine whether it is current servicing a rising or falling edge.
You may also find the revised driver in my git repo's esp3g-2.6.10 branch:
https://github.com/brentr/ChibiOS-RT/tree/esp3g-2.6.10
All relevant changes are here:
https://github.com/brentr/ChibiOS-RT/blob/esp3g-2.6.10/os/hal/platforms/STM32/TIMv1/icu_lld.c
https://github.com/brentr/ChibiOS-RT/blob/esp3g-2.6.10/os/hal/platforms/STM32/TIMv1/icu_lld.h
Note:
I would really like the API to support both signal "high" and "low" callbacks, but I cannot find a way to check the current state of a counter's input. No bit in the STM32's TIMx registers seem to return this
Without a generic way to check the input state, one must resort to different solutions depending on the specific signal routing.
In my test case, I have the output of COMP2 routed to TIM10. In the period_cb, I must check the COMP2 output rather than the TIM10 input.