How to measure high pulse (pin) duration using iMX7d Linux

Hi,
I want to measure one GPIO (high) pulse time. GPIO is connected to device which gives measurement results by pulling this pin HIGH in micro seconds.
Could you please help me on this if there is any better way other than keep polling?

Thank you
Neeraj

Hi @neeraj.verma

Which Gpio (SODIMM Pin) are you using?
What is the minimum and maximum switching frequency?
What is the minimum and maximum pulse duration?

Best regards,
Jaski

Hi @jaski.tx

  1. I am ok to use any GPIO if this functionality is given, for now I am using GPIO1_IO0 & 1 (pin 43, 45)
  2. switching frequency/interval 250 milli-seconds (pin 43)
  3. Pulse duration from 0-100 milli-seconds (pin 45)

Thank you
Neeraj

I would suggest using a pin with PWM functionality.
I don’t have experience wiht Linux, but I think that the PWM driver should have a way to configure the pin and get the desired duration.

Hi @neeraj.verma

Sorry I forgot to ask you what resolution do you want for your measurement? Bigger or smaller than 100us?

Best regards,
Jaski

Hi @jaski.tx ,
Will prefer/expect if I can get smaller than 100us.

Thank you
Neeraj

Most likely the only way to achieve this in an accurate fashion is either using a real-time Linux flavor or run such measuring on the M4 e.g. running FreeRTOS or Zephyr.

Thank you for your suggestion… but we have lots of interfaces up and running, changing OS is almost impossible at this stage.
Let’s see is @jaski.tx has any idea…

Thank you
Neeraj

iMX7 has two flex Timer (FTM) modules supporting input capture. Please check this documents - https://www.nxp.com/docs/en/application-note/AN5142.pdf

However you have to write a driver or directly access timer registers.

Please also check https://community.nxp.com/thread/503536

Hi @alex.tx
I read both links info but didn’t get some simple implementation which I can make use of.
Another approach I was thinking to set GPIO as Rising interrupt and start time, then set GPIO as falling INT and stop time, calculate the time for high pulse?
Will it be accurate enough? or do you have any example/sample code which I can use?

Thank you
Neeraj

Yes you can use an interrupt, but be aware that reaction time is not guarantied under a Linux. So I’d recommend do it using M4 core. Unfortunately I do not have any example/sample code.

Hi @neeraj.verma

I agree with the solutions proposed by @marcel.tx and @alex.tx.
So you have the choice using an interrupt based solution on Realtime Linux or FreeRTOS or write a kernel driver for using Flextimer. Interrupt solution on non real-time Linux will not guarantee you the reaction time and the measurement will have a jitter higher than 100us.

Best regards,
Jaski

ok, thank you for sharing various options.

You are welcome.
Best regards,
Jaski