I use PIT/DMA3/TPM to make sounds accurate to the nano-second (Never underestimate the stupidity of politicians writing technical regulations that become laws). In the MKL27 that I'm using I ran into these things.
What it takes to clear:
ATOMIC_BLOCK( ATOMIC_RESTORESTATE )
{
TPM_SC = 0UL; /* PWM Off */
PIT_TCTRL0 = 0UL; /* PWM Duration timer off */
PIT_LDVAL0 = 0UL;
DMA_DSR_BCR3 = DMA_DSR_BCR_DONE_MASK; /* Clear all DMA status and error bits */
DMA_DSR_BCR3 = 0UL; /* 'Done' must be cleared to configure the DMA */
DMA_DCR3 = 0UL;
PIT_TFLG0 = PIT_TFLG_TIF_MASK; /* Clear flag so there is no immediate IRQ when PIT is enabled */
}
The order that PIT_MCR is set is significant.
/* Periodic Interrupt Timer (PIT) configuration. Used for TPM tone duration control: */
/*
* Errata mask 1N71K e7914: PIT: After enabling the Periodic
* Interrupt Timer (PIT) clock gate, an attempt to immediately
* enable the PIT module may not be successful.
*
* Description: If a write to the PIT module enable bit
* (PIT_MCR[MDIS]) occurs within two bus clock cycles of enabling
* the PIT clock gate in the SIM_CG register, the write will be
* ignored and the PIT will fail to enable.
*
* Workaround: Insert a read of the PIT_MCR register before writing
* to the PIT_MCR register. This guarantees a minimum delay of two
* bus clocks to guarantee the write is not ignored.
*
/*
/* Do three times to make sure it really happens, as once was not getting it done: [There is an internal timing race] */
(volatile uint32_t) PIT_MCR; /* Force read as errata workaround */
PIT_MCR = PIT_MCR_FRZ_MASK; /* Enable timer and freeze timer when debug mode, This field must be enabled before any other setup is done */
(volatile uint32_t) PIT_MCR; /* Force read as errata workaround */
PIT_MCR = PIT_MCR_FRZ_MASK; /* Enable timer and freeze timer when debug mode, This field must be enabled before any other setup is done */
(volatile uint32_t) PIT_MCR; /* Force read as errata workaround */
PIT_MCR = PIT_MCR_FRZ_MASK; /* Enable timer and freeze timer when debug mode, This field must be enabled before any other setup is done */
Also do this right before enabling the PIT as configuring it may set the flag, you do mention this:
PIT_TFLG0 = PIT_TFLG_TIF_MASK; /* Clear flag so there is no immediate IRQ when PIT is enabled */
Also the DMA must be loaded and configured before the PIT.
Someday I'll post some code based this DMA/PIT/TPM the that plays Nokia Ring Tones (RTTTL).
I like to play 'Mission Impossible' on changes of schedule or yet new hardware bugs with the chips... :-/