Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 7 of 7

Thread: Relation between PIT and GPT timer on Teensy 4.0

  1. #1

    Relation between PIT and GPT timer on Teensy 4.0

    Hello,
    I have a quite complicated program with a PIT timer and a GPT timer. I would like to know if there is no relation between this two timers, if one timer can disturb the other one. My PIT timer is around 50kHz, and the GPT timer is ok with a speed of less than 100Hz, above it seems than it is slower than expected. Perhaps, it will be more efficient to use 2 channels of the PIT timer, but I can't find any example of that use.
    Thanks
    Michel

  2. #2
    Senior Member
    Join Date
    Apr 2014
    Location
    Germany
    Posts
    9,279
    Hi,

    no these are independent hardware units. There is no relation.

    Frank

  3. #3
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    9,561
    The only relationship I know of is they share the same setup in Clock Controller Module (CCM) if you look at the Clock Tree configuration: Page 1016 in PDF file.
    By default they run at 24mhz.

    But there is a configurable option within the library Teensytimertool that allows you to change these from the fixed 24mhz OSC clock to use the other configuration that typically runs at 150mhz.
    So if you have defined one timer using the normal stuff, and then later setup a timer using the tmertool, it might throw the timings of the existing timers.

  4. #4
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    25,045
    Quote Originally Posted by michastro View Post
    if one timer can disturb the other one.
    As Frank and Kurt said, they're independent hardware.


    My PIT timer is around 50kHz, and the GPT timer is ok with a speed of less than 100Hz, above it seems than it is slower than expected.
    Perhaps assigning interrupt priority levels might help? Really, that's just a blind guess, since I have not idea what you're actually observing which "seems than it is slower than expected". In fact, I really don't even know if you're using interrupts or employing the timers some other way.


    Perhaps, it will be more efficient to use 2 channels of the PIT timer, but I can't find any example of that use.
    Just use 2 instances of IntervalTimer. Easy peasy!

  5. #5
    Ok thank you, so I don't understand where is the problem. Here is a little part of my code. The TimerMoteur is used to make a ramp to increase and decrease the speed of motor and to make a precise number of steps. The value of the timer is starting at about 4000Hz, increase up to 50000Hz and decrease to 4000Hz then stop. The TimerCompensation is used to inject more steps during the process because the system is used to follow the stars, and the sky continue to turn during the process. The rate of TimerCompensation can be between 50 and 300Hz. As you can see the TimerCompensation interrupt is very short ans at a slow rate compare to the other timer and the clock of Teensy. What I don't understand is the fact that time for the process increase quite strongly if I increase the speed of TimerCompensation, that is normal that it increases, but just a little bit because the difference of steps small. In fact between 50Hz and 100Hz, it's ok, after it becomes longer and longer.
    If you have an idea, I am interested.
    Thanks
    Code:
    #include "TeensyTimerTool.h"
    using namespace TeensyTimerTool;
    #define RS232 Serial
    PeriodicTimer TimerMoteur(PIT);
    PeriodicTimer TimerCompensation(PIT);
    int Num_Pointage;
    byte Accu_Add;
    
    void step_moteur(void) {
       digitalWrite(Out_DIR, HIGH);
       digitalWrite(Out_DIR, HIGH);
       digitalWrite(Out_DIR, HIGH);
       digitalWrite(Out_DIR, HIGH);
       digitalWrite(Out_DIR, HIGH);
       digitalWrite(Out_HorlogeMoteur, LOW);
    }
    
    /****************************************************************
             Function Name:  MoveNumOfUSteps
             Return Value:   none
             Parameters:     none
             Description:    Move motor x steps in dir Out_DIR
     ****************************************************************/
    
    void MoveNumOfUSteps(void) {
      int t = micros();
    
      while (Num_Pointage > 1) {          
        // Calculation of a new _delay to accelerate the stepbystep motor
        TimerMoteur.stop();
        PIT_LDVAL0 = _delay;      // Valeur du delai transferee dans le registre comparateur
        PIT_TCTRL0 = PIT_TCTRL_TEN; // start Timer0 without interruption
        Num_Pointage--;
    
        Num_Pointage += Pointage_Corr;  // Adjust Num_Pointage
        step_moteur();  //one micro step
    
        while (PIT_TFLG0 == 0) {  // Waiting the end of timermoteur
        }
        PIT_TFLG0 = 1; //clear flag of timer0
      }
    
      t = micros() - t;
      RS232.printf("time in micro seconde=%d\r\n", t);
    }
    
    void setup() {
      pinMode(Out_HorlogeMoteur, OUTPUT_OPENDRAIN);
      pinMode(Out_DIR, OUTPUT_OPENDRAIN);
    
      RS232.begin(19200);
      while (RS232);
    
      //    PIT Initialisation for the motor interrupt
      TimerMoteur.begin(IntTimerMoteur, 400ms, true);
      NVIC_SET_PRIORITY(IRQ_PIT, 20);   //priority
      TimerMoteur.start();
    
      TimerCompensation.begin(IntTimerCompensation, 250ms, true);
      TimerMoteur.stop();
      Num_Pointage=10000;
      MoveNumOfUSteps();
      exit(0);
    }
    
    void loop() {
    }
    
    void IntTimerCompensation() {
      //
      // Timer PIT1 interrupt handler
      // is used only during slewing to
      //
      // compensate for sidereal speed during the acceleration phase
      //
        if (!Accu_Add) {     // If slewing is in anti-sidereal dir (one of SPD_*_MINUS) //
          if (Num_Pointage > 1) {
            Pointage_Corr--;        
          }
        }
        else {
          Pointage_Corr++; // If slewing is in sidereal dir (one of SPD_*_PLUS) //
        }
    }
    
    void IntTimerMoteur() {
      step_moteur();
    }

  6. #6
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    9,561
    Sorry I have not done the math.

    But if it were me, I would look at the resolution of the timers. That is again they run at 24mhz, so for example if you are running at 4mhz, that
    would imply like 6 cycles of the clock. Note Interval timer I don't believe will allow you to set this short off a time... But probably the Teensy Timer tool does.

    But then to go up to around 5mhz, 24/5 = 4 cycles. which is 6mhz or, it is 5 or 4.8mhz...

    Now again I believe that the timer tool has the ability to run them at 150mhz... Which would give you more granularity, but it will effect both types of timers.

    Hope that makes sense?

    Again I don't know if that is what you are running into, but that is what I would look at next if it were me.

  7. #7
    Hi,
    The resolution of my timers is 24MHz. Perhaps I can try with 150MHz.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •