Hello,
I am new to freescale controllers and I am playing around with making delays using TPM0 module. I am not using PE, just doing baremetal coding. I would like to use the MCGIRCCLK to be the source clock for the TPM0 module bit I can't seem to get it to kick in. I am aware of the different FEI, BLPI, etc... clock state modes but am unsure how to manipulate them. I am writing a small piece of code to create a delay for the Blue LED. Here is my code. Thank you for any help.
P.S. I am running on CodeWarrior 10.6
#include "derivative.h" /* include peripheral declarations */
#define BLUE_LED_LOC (1<<1)
#define MCG_IRC_CLK 0x03000000
#define BLUE_LED_ON GPIOD_PCOR = BLUE_LED_LOC
#define BLUE_LED_OFF GPIOD_PSOR = BLUE_LED_LOC
#define BLUE_LED_TOGGLE GPIOD_PTOR = BLUE_LED_LOC
void Blue_LED_Init(void);
void TPM0_Init(void);
int main(void)
{
Blue_LED_Init();
TPM0_Init();
for(;;) {
while((TPM0_SC & 0x80) == 0) {}
TPM0_SC |= 0x80;
BLUE_LED_TOGGLE;
}
return 0;
}
/*
* Initialise the Blue LED (PTD1)
*
*/
void Blue_LED_Init(void)
{
SIM_SCGC5 |= SIM_SCGC5_PORTD_MASK; /* Turn on clock to PortD module */
PORTD_PCR1 = PORT_PCR_MUX(1); /* Set the PTD1 pin multiplexer to GPIO mode */
GPIOD_PDDR |= BLUE_LED_LOC; /* Set the pins direction to output */
GPIOD_PSOR = BLUE_LED_LOC; /*Turn off Blue LED*/
}
/*
* Initialise TPM0
*
*/
void TPM0_Init(void)
{
SIM_SCGC6 |= SIM_SCGC6_TPM0_MASK; /* Enable clock to TPM0 module */
SIM_SOPT2 |= 0x03000000; /* Source clock to TPM0 module PLL select, MCGPLLCLK/2 */
TPM0_SC |= 0x00; /* Disable timer/counter during configuration, set up for up-counter */
TPM0_SC |= 0x02; /* Set Prescalar to DIV by 128 */
TPM0_MOD = 40960 - 1; /* Max modulous */
TPM0_SC |= 0x80; /* Clear TOF flag */
TPM0_SC |= 0x08; /* Enable counter free running mode */
}
Solved! Go to Solution.
Hi,
Regarding which MCG mode you are running (PEE, FBI, FEE,.....) to use the MCGIRCLK clock you need to enable it by writing to the MCG_C1 and MCG_C2 registers.
Hope this information can help you
Best Regards,
Adrian Sanchez Cano
-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------
Hi Stan,
To select the MCGIRCLK as the source clock for the TPM module you need to enable it first, this is done writing register MCG_C1[IRCLKEN]:
//Enables Internal Reference Clock
MCG_C1 |= MCG_C1_IRCLKEN_MASK;
You can add this configuration in the TPM0_Init function:
#include "derivative.h" /* include peripheral declarations */
#define BLUE_LED_LOC (1<<1)
#define MCG_IRC_CLK 0x03000000
#define BLUE_LED_ON GPIOD_PCOR = BLUE_LED_LOC
#define BLUE_LED_OFF GPIOD_PSOR = BLUE_LED_LOC
#define BLUE_LED_TOGGLE GPIOD_PTOR = BLUE_LED_LOC
void Blue_LED_Init(void);
void TPM0_Init(void);
int main(void)
{
Blue_LED_Init();
TPM0_Init();
for(;;) {
while((TPM0_SC & 0x80) == 0) {}
TPM0_SC |= 0x80;
BLUE_LED_TOGGLE;
}
return 0;
}
/*
* Initialise the Blue LED (PTD1)
*
*/
void Blue_LED_Init(void)
{
SIM_SCGC5 |= SIM_SCGC5_PORTD_MASK; /* Turn on clock to PortD module */
PORTD_PCR1 = PORT_PCR_MUX(1); /* Set the PTD1 pin multiplexer to GPIO mode */
GPIOD_PDDR |= BLUE_LED_LOC; /* Set the pins direction to output */
GPIOD_PSOR = BLUE_LED_LOC; /*Turn off Blue LED*/
}
/*
* Initialise TPM0
*
*/
void TPM0_Init(void)
{
//Enables Internal Reference Clock
MCG_C1 |= MCG_C1_IRCLKEN_MASK;
SIM_SCGC6 |= SIM_SCGC6_TPM0_MASK; /* Enable clock to TPM0 module */
SIM_SOPT2 |= 0x03000000; /* Source clock to TPM0 module PLL select, MCGPLLCLK/2 */
TPM0_SC |= 0x00; /* Disable timer/counter during configuration, set up for up-counter */
TPM0_SC |= 0x02; /* Set Prescalar to DIV by 128 */
TPM0_MOD = 40960 - 1; /* Max modulous */
TPM0_SC |= 0x80; /* Clear TOF flag */
TPM0_SC |= 0x08; /* Enable counter free running mode */
}
And this will keep the LED toggling.
Best regards,
Earl.
Works, thanks for the help guys.
Hi,
Regarding which MCG mode you are running (PEE, FBI, FEE,.....) to use the MCGIRCLK clock you need to enable it by writing to the MCG_C1 and MCG_C2 registers.
Hope this information can help you
Best Regards,
Adrian Sanchez Cano
-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------