embarto
Posts: 3
Joined: Mon May 17, 2021 11:58 am

µs Timing with Pi 4

Mon May 17, 2021 12:22 pm

Hello everyone,
i'm trying to create a quite accurate delay between 2 pins going high.
I've found the following solution viewtopic.php?t=52393 and i'm trying to implement that for my Raspberry Pi 4. The solution polls the free-running-counter and disables all interrupts in order to get an accuracy of 0.1-1µs.
So far i've looked up the registers in Datasheet for the interrupts(p.124 and following). Since i'm not certain which core my Raspi OS is using i planned to check for pending interrupts, if there are none, disable all interrupts for a period of 200-400µs. Afterwards turn them back on of course.
I know that i need to store the values of the interrupt registers in order to turn them back on. I'm also aware that the OS might not like that.
Since i already experienced that offsets in the datasheets are wrong, I'm now in need for the correct base-offset for the interrupt register.
Can sb help me with this? I'd appreciate your help a lot since i'm stuck with this for quite some time now.

drtechno
Posts: 246
Joined: Fri Apr 09, 2021 6:33 pm

Re: µs Timing with Pi 4

Tue May 18, 2021 2:12 am

Which Kernel?
There might be some timing tweaks that might be solvable in the kernel level. Because the Jiffy could be turn down to say, 100 for 100pS.

Because some are set to 1000 if they didn't compile to 2.6.13 specs which is set to 250. I haven't looked that far yet on this platform.

petzval
Posts: 40
Joined: Sat Aug 10, 2013 12:15 pm

Re: µs Timing with Pi 4

Tue May 18, 2021 11:39 am

Disabling interrupts on the Pi4 will work most of the time, but occasionally crashes the system for no apparent reason. I would use pigpio for this.

LdB
Posts: 1692
Joined: Wed Dec 07, 2016 2:29 pm

Re: µs Timing with Pi 4

Thu May 20, 2021 3:28 am

The best layman way was given here but you will ocassionally get caught with interrupt delays
https://raspberrypi.stackexchange.com/q ... icrosecond

The only way to do it with turning off the interrupts is via a kernel driver ... discussed and with driver code here
https://embetronicx.com/tutorials/linux ... ce-driver/

User avatar
MikeDB
Posts: 511
Joined: Sun Oct 12, 2014 8:27 am
Contact: Website

Re: µs Timing with Pi 4

Fri May 21, 2021 11:49 pm

Wouldn't it be better to force your code to run on a single core and use isolcpu command to stop anything else running on it. Then provided the OS isn't writing to the same port as your code you shouldn't need to disable any interrupts.
Always interested in innovative audio startups needing help and investment. Look for me on ModWiggler or other sites that have PMs.

LdB
Posts: 1692
Joined: Wed Dec 07, 2016 2:29 pm

Re: µs Timing with Pi 4

Sat May 22, 2021 1:52 am

It's been discussed many times isolcpu doesn't stop the scheduler interrupts on that core that requires you to remove the core from the scheduler ... to get a totally free core you have to recompile the source (All discussed here https://lwn.net/Articles/816298/). At best it makes the interrupt problem less frequent.

It's not trivial and a lot more work than just making a kernel driver and there are no free lunches or easy ways to pull a core from the system when it was compiled to use all cores. The deeper question to ponder is when you do get it free how do you sync it's hardware use with the other cores running linux ... think race conditions.

Now for bonus points your challenge is to convince yourself it's not easier to just couple a raspberry pi pico to the Pi4 :-)

cleverca22
Posts: 3802
Joined: Sat Aug 18, 2012 2:33 pm

Re: µs Timing with Pi 4

Sat May 22, 2021 11:07 am

what exactly does the code need to do?
there are at least 3 other options, dpi, vpu, and pico

which one you can use depends on what you need to do

embarto
Posts: 3
Joined: Mon May 17, 2021 11:58 am

Re: µs Timing with Pi 4

Wed May 26, 2021 8:28 am

Thanks for your replies.
I've found the following post: viewtopic.php?t=228727 which helped me achieve my goal...

Return to “Bare metal, Assembly language”