-
Notifications
You must be signed in to change notification settings - Fork 5
ALT mux
https://forum.pjrc.com/printthread.php?t=51727&pp=25&page=1 PaulStoffregen
First you need to understand every pin has a 8-way mux which connects it to 1 of 8 possible functions. This is described in chapter 10 of the reference manual, where you can find a huge table that shows all 8 mux possibilities for every pin. Use the schematic to translate Arduino pin numbers to Freescale's native names in that table.
I'm going to talk about pin 5. From the schematic, you can see that's "PTD7". You can find it on the very last line of the huge table, on page 209 of the MK20DX256 reference manual.
This pin has 5 of the 8 mux channels implemented. So when the pin's mux is configured to 1 (called "ALT1"), the pin is controlled by the GPIO module. Then it's set to ALT4, the pin is controlled by FTM0_CH7.
So, to answer your original question, yes you can switch between FTM input capture and FTM output compare. But both of those require the mux to be set to ALT4. Then to change how the FTM timer uses FTM0_CH7, you would write to the FTM0_C7SC register. The 4 bits MSB,MSA,ELSB,ELSA documented on pages 783-784 directly control whether the pin is PWM, input capture, output compare or other modes. Changing those 4 bits causes the pin to become an input or output, depending on which way the FTM uses it.
However, the FTM0 timer only controls pin 5 (PTD7) when its mux is set to ALT4. If you use pinMode(5, OUTPUT), the pinMode function will set the mux to ALT1, so the pin becomes controlled by the GPIO hardware. When GPIO has control of the pin, it becomes input vs output depending on the GPIOD_PDDR register.
Likewise, when the pin's mux is configured to the other settings, the pin is effectively disconnected from GPIO & FTM and fully controlled by whatever module you selected. If you use ALT2, then the pin is fully controlled by the CMT timer (eg, used by the IrRemote library). Using ALT0 (the default) put the pin's hardware into a low power disabled state, even if the FTM and GPIO are running.
As long as the mux is configured for anything other than ALT4, that pin is effectively disconnected from the FTM0 timer and you can change from input capture to output compare or any other feature of the FTM timer by writing to FTM0_C7SC and the other FTM registers. But if you start mixing pinMode and other non-FTM usage, you must be aware of the pin mux. Only 1 of the 8 possible functions fully controls the pin at a time. The other 7 have no effect, so if you switch the mux to ALT1 for GPIO, the FTM no longer has access to the pin.
While this doesn't pertain directly to your situation, for the sake of completeness (for anyone who later finds this by searching), the very similar TPM timers (which we sometimes mistakenly call FTM for the sake of software compatibility, especially on Teensy LC) have a few minor differences. Their "SC" registers that configure the modes like input capture vs output compare can not directly change modes like FTM can. The register bits and everything else looks virtually identical, but the TPM's SC register must first be written to zero, and then you must wait a brief delay until the write takes effect (if the TPM is running from a slow clock). The TPM ignores any changes to its SC channel config registers if you don't first clear them to zero, but it defaults to zero from hardware reset, so your first write to the SC register will always succeed (and the Teensyduino startup code initializes them all to the mode used for PWM by analogWrite). Simple if you know about this little gotcha, but incredibly frustrating if you try porting complex FTM code to the TPM timers and don't know you have to clear them to zero to get any config changes to take effect!
Teensy is a PJRC trademark. Notes here are for reference and will typically refer to the ARM variants unless noted.