ICU and timer channel?

ChibiOS public support forum for all topics not covered by a specific support forum.

Moderators: RoccoMarco, lbednarz, tfAteba

kgysmits
Posts: 20
Joined: Tue May 03, 2011 10:16 am

Re: ICU and timer channel?

Post by kgysmits »

Slightly offtopic:
Wouldn't it be nice to have a Counter abstracted driver for rpm counting, quadrature input etc? Right now it seems chibios has:
- virtual timers, based on systick
- ICU, Input capture unit: PWM period and duty cycle measurement
- GPT, general purpose timers with callback

Seems there is some room for a counter-specific driver?
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: ICU and timer channel?

Post by Giovanni »

There is room for even more drivers of course, the possibility you mentioned make sense. It is matter of time and available development bandwidth.

Note that ChibiOS does not forbid access to the hardware, if a specific feature is not found into an existing device driver it can be implemented as a custom device driver or directly at application level.

By "custom device driver" I mean a device driver which is not standard part of the HAL but respects all conventions and code templates of normal drivers, it can also be platform-specific, when writing a custom driver there is no need to plan for portability nor to split it in high and low level layers.

Giovanni
utzig
Posts: 359
Joined: Sat Jan 07, 2012 6:22 pm
Has thanked: 1 time
Been thanked: 20 times

Re: ICU and timer channel?

Post by utzig »

I did an implementation for a quadrature decoder last year using two standard input pins configured to trigger interrupts (up and down edges). The state machine is so simple that it can be implemented in maybe 10 lines for the complete code. I never used a processor before that has hardware support for this although the stm32f1xx seems to have (probably newer stm32 models too). There's not much information on the reference manual though.
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: ICU and timer channel?

Post by Giovanni »

True, the RM does not give details about how to use a timer in that scenario.

Giovanni
kgysmits
Posts: 20
Joined: Tue May 03, 2011 10:16 am

Re: ICU and timer channel?

Post by kgysmits »

Check chapter 14.3.16 in the STM32F101/2/3 reference manual, and 13.3.16 in the STM32F40x RM. This chapter describes "Encoder interface mode".
When using a standard rotary encoder used as a human interface (say 24 count per rotation), input pins with interrupts are fine. I'm looking to implement a rotary encoder with a 16bit resolution per rotation, that's a whole different matter. Especially at momentary high speed movement.
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: ICU and timer channel?

Post by Giovanni »

I missed that, I was searching "quadrature"...

It does not look complex, a simple driver could be created easily, how callbacks would work in this scenario? (assuming it needs callbacks of some kind)

Giovanni
utzig
Posts: 359
Joined: Sat Jan 07, 2012 6:22 pm
Has thanked: 1 time
Been thanked: 20 times

Re: ICU and timer channel?

Post by utzig »

kgysmits,

The peripheral I used is used to measure liquid flow and generates around 100 pulses for each rotation. I didn't know there were encoders with such high precision.

In my specific case, I only need to know the current counter value (after converted to litters value). The counter is incremented/decremented automatically on the "low-level" routines. For me the only callback that I would need is counter overflow/undeflow?, where I would get the value and add to a uint32_t variable. And I would need access to the current counter value. Maybe for some uses it would be important to know when it gets to a certain value (an alarm like functionality).

The encoder interface also works only on TI1 and TI2 too like PWM input used on the ICU driver. It annoys me that the hardware designers didn't enable TI3 and TI4 as inputs for these interfaces.
kgysmits
Posts: 20
Joined: Tue May 03, 2011 10:16 am

Re: ICU and timer channel?

Post by kgysmits »

utzig wrote:kgysmits,

The peripheral I used is used to measure liquid flow and generates around 100 pulses for each rotation. I didn't know there were encoders with such high precision.

[...]

I am using a hall effect linear encoder from Austria Microsystems, the AS5311. by using an off-center 128pole magnetic ring (diameter 4cm), it's possible to reach 16bit resolution per turn with quadrature output. If you interface with the SSI interface, 18bit is also possible.

My application is a delta robot with SMA wire (memory/muscle wire) as actuators for the 3 arms.

A 'position reached' callback would be a nice feature for me. If i have the time to implement a decent driver with some abstraction, I'll send it to Giovanni for review. Don't count on anything though, as the project is running behind schedule already...
hazelnusse
Posts: 77
Joined: Thu May 24, 2012 8:01 am

Re: ICU and timer channel?

Post by hazelnusse »

I use TIM3, TIM4, TIM5 on a STM32F107 to count encoder ticks. This is all done in hardware, and no interrupt handler is needed, if the SMS bits in the SMCR register is set as follows (for up/down encoder counting):

Code: Select all

TIMx->SMCR = 0x0003;


See 15.3.12 and 15.4.3 of RM0008. Input filtering and polarity can be configured with TIMx->CCMR1, I have used 0x3131 for this register with motor encoders with 20000 counts per revolution and it never misses a count.

When the encoder Channel A is connected to TIMx_CH1 and Channel B is connected to TIMx_CH2, encoder transitions will increment and decrement TIMx->CNT appropriately, again all without any interrupts. When you need the angle of the motor, you just read the CNT register.
trunet
Posts: 54
Joined: Fri Apr 06, 2012 7:18 pm

Re: ICU and timer channel?

Post by trunet »

Hi,

Reading datasheet, 14.3.6, 14.3.7, it appears that PWM Input Mode is a particular case of Capture Input Mode.

However the ICU driver is written to measure width and period of transitions. Don't you think that the ICU driver shoud be called PWMICU and ICU being a generic input capture only?

If I want to only measure width of a pulse ICU should do that and I'll be able to read the 4 channels instead to only 2 on all timers?
Post Reply