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

Thread: How to reset Teensy4.0 timers for phase syncing PWM signals

  1. #1

    How to reset Teensy4.0 timers for phase syncing PWM signals

    The Teensy4.0 has far more timers than the 3.6 which enables some neat use cases I'm pursuing. However, I need to be able to resync the PWM phase (with respect to an external source) by restarting the timers for each pin. I've seen one other thread addressing this for 3.x. Based on that thread it seems at least one way to do this is to call pinmode with the same settings that pin already had and then call analogwrite again to reset the timer for that pin. Can anyone confirm how this will behave?

    I need to be able to sync as many pins as possible as closely as possible, so if there are ways to directly manipulate registers etc to speed the process it would help out a lot. Are there reliable ways to do this easily? Any chance a bank of timers can be reset with one command?

  2. #2
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    22,082
    Quote Originally Posted by kdharbert View Post
    Based on that thread it seems at least one way to do this is to call pinmode with the same settings that pin already had and then call analogwrite again to reset the timer for that pin. Can anyone confirm how this will behave?
    That will not work. You'll get the same PWM waveforms when the hardware switches back from GPIO to timers controlling those pins.


    so if there are ways to directly manipulate registers etc to speed the process it would help out a lot. Are there reliable ways to do this easily? Any chance a bank of timers can be reset with one command?
    Yes, there are ways, but I don't believe many people would use the word "easily" regarding reading the FlexPWM chapter in the reference manual.

    https://www.pjrc.com/teensy/datasheets.html

    The 16 FlexPWM timers are arranged in 4 groups of 4. So you want to use PWM pins controlled by the same group of 4. See this page for details:

    https://www.pjrc.com/teensy/td_pulse.html

    Scroll down to "PWM Frequency" for the info about which pins are controlled by which timers. You probably want to use pins from those first 4 lines, so you're using all the FlexPWM1 timers.

  3. #3
    I already found the Teensy PWM page and chose my pins specifically to have different timers. I was able to measure perfect behavior, so I'm over pin\timer conflicts. I just need to phase-correct the waveforms with respect to an external input.

    As you suggested, the FlexPWM documentation is definitely too much to bench, but maybe you can help me on the basics:

    It seems like resetting the PWM phase would be a one-liner...its just figuring out what that one-liner is. Is it more complicated than this?

    You suggested using the same group of four FlexPWM timers, I thought there might be some use to that but I don't understand why. Can you elaborate?

  4. #4
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    22,082
    In the reference manual, turn to "55.4.2.2 Register Reload Logic" on page 3047.

    Click image for larger version. 

Name:	sc.png 
Views:	1 
Size:	62.2 KB 
ID:	20811

    Also on the next page is a similar diagram about synchronization. The point is each group of 4 "submodule" timers can be configured to act independently, which is the way we normally use them, or you can program that group of 4 submodule timers to all sync to the first timer in the group.

    Also pay close attention to the MCTRL register documented starting on page 3134. The 4 bit LDOK field is particularly important. Each submodule timer is controller by 6 double buffered registers. These 4 bits control the PWM setting can load. Again, normally we use these 4 submodules are independent timers, so when writing to this register we use only values 1, 2, 4, 8 with LDOK, to update exactly 1 of the submodules at a time (but all the PWM values for that submodule take effect at that moment, regardless of the code's speed to write to those 6 registers). You will probably use LDOK differently.

    I can't tell you exactly how to use the many advanced but complex FlexPWM features. In fact, I don't even have a perfectly clear idea of what you're really trying to accomplish. The best I can do is help point you to the specific parts of the FlexPWM documentation that are most likely the features you need. Hopefully this can at least help give you a foothold in the mountainous FlexPWM chapter.

  5. #5
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    22,082
    Also, as you read the FlexPWM chapter you'll see info about fractional cycle stuff. Teensy 4.0 & 4.1 do *NOT* have that feature. You can ignore all that fractional cycle info. All those fractional value registers with "FRAC" in the middle of their names are not actually implemented.

    NXP could have made this clearer, or they could have left all that irrelevant info out of the manual, but they didn't. At least knowing this now can save you some time. Ignore all that fractional value documentation.

  6. #6
    Thanks for the replies. My use case is simple: I need to have a pin output a square wave at a particular frequency and be able to restart the duty cycle so I can sync its phase based on external input. I already used AnalogWriteFrequency to set the frequency, I just need a command to handle the reset. Nothing fancy past that.

    So far we've only discussed the FlexTimers. Is there a quick explanation of the difference between the FlexTimers and QuadTimers? Any chance they might be easier to use?

Posting Permissions

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