TTimer problem on Raspian lazarus / 1-Linux ---------------------------------------------------------------------- soldeersmurf · 2016-08-15 22:18 In another topic a TTimer problem was described and solved.  Updating the statusbar caused the timer to become slower. I experienced something similar, although I have no statusbar in my application.  I have a main thread that starts 3 instances of a class (with Application.CreateForm).  The class controls some hardware.  Communication with the hardware is via a serial handler that uses a single RS485 line to all the hardware components, hence the single communication handler. Each instance contains a TTimer (250 msec) to control and monitor "its own" hardware component at regular intervals and update its own window on the GUI.  Also Main- and Serial-task have a timer running at 50 ms. On a PC running Windows 7, I see no strange behaviour.  On the Raspi, the 250 ms timers are running slow (66%) and the processor load is 100%. I left out a lot of other info, but I hope someone can put me in the right direction to attack this problem.  I have not done any tests yet (such as start with one timer and observe its behaviour). I am running Lazarus 1.2.4. (because it served was precompiled and worked for me). Help is appreciated! regards, Michael ---------------------------------------------------------------------- DonAlfredo · 2016-08-16 05:39 What I don't know: your code ! What I do know: many people (including me) run timers on RPi with extra hardware comm's without poblem. So this problem is very likely to be solvable. On thing I always do on a small board like RPi: make the timer-proc reentrant safe ! The timer proc can be time consuming and another entry of this proc while the proc is still running can cause havoc. It may seem a bit overdone, but better be sure than sorry. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i TForm1...private  Timer1Busy:integer;end; procedure TForm1.OnCreate(...)begin  Timer1Busy:=integer(false);  ...end; procedure TForm1.Timer1Timer(Sender: TObject);begin    if InterLockedExchange(Timer1Busy, integer(True))<>integer(True) then    try        //your timer code here         //...    finally        InterLockedExchange(Timer1Busy, integer(False));    end;end; The other thread about timer problems mentions a StatusBar update problem. On my RPi's, statusbars are updated by timers without any problem. ---------------------------------------------------------------------- Thaddy · 2016-08-16 06:00 What kind of RPi (1,2,3) are we talking about? The single core one  or a multi-core? [edit] PC's have hardware timers and a stable clock. Pi's don't have a clock. A multi-core Pi can run a timer on a specific core. (I can write code for that) It came to mind that for high precision timing (much more precise than 250 or 50 ms) we could even use the videocore somehow. (I will investigate and ask Dom) A single core Pi needs an add on hardware timer. A 2 euro investment so it may be out of reach for most... ---------------------------------------------------------------------- soldeersmurf · 2016-08-16 12:21 Thanks for the hints.  The code (which is too big to include) runs on a single core Rpi. I have enough RTC chips laying around and toyed with the idea to use on of those to generate an interrupt instead of a Ttimer.  What means 'Pi's have no clock'? No RTC, but even without access to a time-server the system time seems pretty accurate after settting it once, so there must be some timing facility. How is a TTimer implemented? Does it constantly check the system clock to see if an action is due?  I can imagine such a mechanism could cause my CPU load being so high (and eventually causes being 'late'). About re-entrant safe timer-routines.  I'll check if this is an issue (my comm-routine is a candidate). Thanks for the code-example.  Can't I simply disable the timer in the timer-routine to prevent re- entrance? Tonight I have some homework to do! ---------------------------------------------------------------------- Thaddy · 2016-08-16 12:54 Did you see this thread? I can pascalize it for you. https://www.raspberrypi.org/forums/viewtopic.php?f=29&t=52393 ---------------------------------------------------------------------- DonAlfredo · 2016-08-16 13:47 @soldeersmurf Its ok to switch of the timer. But this makes the timer a sort of one-shot. I like my timers to be timers with more or less accurate timer- interrupt-intervals. ---------------------------------------------------------------------- soldeersmurf · 2016-08-16 14:14 @Don, Good point, I didn't think about that.  On the other hand, in my Comm- routine exact time is not needed.  The only purpose is to set an interval during which an answer should come in. So the one-shot would work.  Yet, I'll check if the routine is re-entered. @Thaddy, I didn't see it before. but it could be the solution to get rid of the ttimers altogether. I haven't used interrupts by GPIO's before, so a snippet with a simple example would be very helpful.  How are interrupt handled in general by Lazarus? Does the processor actually jump (via a vector) to my code or must my code keep on checking if some pin change has occurred?