/*
Blink
Turns on an LED on for one second, then off for one second, repeatedly.
This example code is in the public domain.
*/
#include "clock_functions.h"
#include "Audio_pll.h"
extern "C" uint32_t set_arm_clock(uint32_t frequency);
static uint32_t t0;
// Pin 13 has an LED connected on most Arduino boards.
// Pin 11 has the LED on Teensy 2.0
// Pin 6 has the LED on Teensy++ 2.0
// Pin 13 has the LED on Teensy 3.0
// give it a name:
int led = 13;
// the setup routine runs once when you press reset:
void setup() {
while ( !Serial && millis() < 4000 ) ;
delay(2000);
/*set osc clock crystal freq */
CLOCK_SetXtalFreq(24000000UL);
Serial.println();
/* Set the core frequency into 62.5MHz. */
//CLOCK_SetAHBDiv(0x07);
//Serial.printf("System Clock 62.5: %d\r\n", CLOCK_GetAhbFreq());
//delay(4000);
/* Set the core frequency into 500MHz. */
//CLOCK_SetAHBDiv(0x0);
//Serial.printf("System Clock 500: %d\r\n", CLOCK_GetAhbFreq());
//delay(4000);
/* Set the core frequency into 600MHz. */
//CLOCK_SetAHBDiv(0x1);
//Serial.printf("System Clock 600: %d\r\n", CLOCK_GetAhbFreq());
CCM_ANALOG_PLL_AUDIO = 0;
//CCM_ANALOG_PLL_AUDIO |= CCM_ANALOG_PLL_AUDIO_BYPASS;
CCM_ANALOG_PLL_AUDIO |= CCM_ANALOG_PLL_AUDIO_ENABLE;
CCM_ANALOG_PLL_AUDIO |= CCM_ANALOG_PLL_AUDIO_POST_DIV_SELECT(2); // 0: 1/4; 1: 1/2; 0: 1/1
CCM_ANALOG_PLL_AUDIO |= CCM_ANALOG_PLL_AUDIO_DIV_SELECT(32);
CCM_ANALOG_PLL_AUDIO_NUM = 0 & CCM_ANALOG_PLL_AUDIO_NUM_MASK;
CCM_ANALOG_PLL_AUDIO_DENOM = 1 & CCM_ANALOG_PLL_AUDIO_DENOM_MASK;
//--------------------------------------------------------
/*
* AUDIO PLL setting: Frequency = Fref * (DIV_SELECT + NUM / DENOM)
* = 24 * (32 + 77/100)
* = 786.48 MHz
*/
const clock_audio_pll_config_t audioPllConfig = {
.loopDivider = 32, /* PLL loop divider. Valid range for DIV_SELECT divider value: 27~54. */
.postDivider = 1, /* Divider after the PLL, should only be 1, 2, 4, 8, 16. */
.numerator = 77, /* 30 bit numerator of fractional loop divider. */
.denominator = 100, /* 30 bit denominator of fractional loop divider */
};
CLOCK_InitAudioPll(&audioPllConfig);
CCM_ANALOG_MISC2 &= ~(CCM_ANALOG_MISC2_DIV_MSB | CCM_ANALOG_MISC2_DIV_LSB);
CCM_CCGR5 |= CCM_CCGR5_SAI1(CCM_CCGR_ON);
// clear SAI1_CLK register locations
CCM_CSCMR1 &= ~(CCM_CSCMR1_SAI1_CLK_SEL_MASK);
CCM_CS1CDR &= ~(CCM_CS1CDR_SAI1_CLK_PRED_MASK | CCM_CS1CDR_SAI1_CLK_PODF_MASK);
//
CCM_CSCMR1 |= CCM_CSCMR1_SAI1_CLK_SEL(2); // &0x03 // (0,1,2): PLL3PFD0, PLL5, PLL4,
CCM_CS1CDR |= CCM_CS1CDR_SAI1_CLK_PRED(4 - 1); // &0x07
CCM_CS1CDR |= CCM_CS1CDR_SAI1_CLK_PODF(8 - 1); // &0x3f
CORE_PIN23_CONFIG = 3; //1:MCLK
IOMUXC_GPR_GPR1 &= ~(IOMUXC_GPR_GPR1_SAI1_MCLK1_SEL_MASK);
IOMUXC_GPR_GPR1 |= (IOMUXC_GPR_GPR1_SAI1_MCLK_DIR | IOMUXC_GPR_GPR1_SAI1_MCLK1_SEL(0));
printClocks();
// initialize the digital pin as an output.
pinMode(led, OUTPUT);
#define MHZ_MHZ 1000000
TimeDiff();
set_arm_clock(240 * MHZ_MHZ); // comment here to get full speed
TimeDiff();
set_arm_clock(110 * MHZ_MHZ); // comment here to get full speed
TimeDiff();
set_arm_clock(600 * MHZ_MHZ); // comment here to get full speed
TimeDiff();
set_arm_clock(130 * MHZ_MHZ); // comment here to get full speed
TimeDiff();
set_arm_clock(110 * MHZ_MHZ); // comment here to get full speed
TimeDiff();
set_arm_clock(24 * MHZ_MHZ); // comment here to get full speed
TimeDiff();
set_arm_clock(600 * MHZ_MHZ); // comment here to get full speed
TimeDiff();
set_arm_clock(24 * MHZ_MHZ); // comment here to get full speed
TimeDiff();
}
// the loop routine runs over and over again forever:
void loop() {
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
void printClocks() {
Serial.printf("System Clock: %d\r\n", CLOCK_GetAhbFreq());
Serial.printf("IPG Clock: %d\r\n", CLOCK_GetFreq(kCLOCK_IpgClk));
Serial.printf("Semc Clock: %d\r\n", CLOCK_GetFreq(kCLOCK_SemcClk));
Serial.printf("RTC Clock: %d\r\n", CLOCK_GetFreq(kCLOCK_RtcClk));
Serial.printf("USB1pll Clock: %d\r\n", CLOCK_GetFreq(kCLOCK_Usb1PllClk));
Serial.printf("Peripheral Clock: %d\r\n", CLOCK_GetFreq(kCLOCK_PerClk));
Serial.printf("Osc Clock: %d\r\n", CLOCK_GetFreq(kCLOCK_OscClk));
Serial.printf("Arm Clock: %d\r\n", CLOCK_GetFreq(kCLOCK_ArmPllClk));
Serial.printf("Usb1PllPfd0 Clock: %d\r\n", CLOCK_GetFreq(kCLOCK_Usb1PllPfd0Clk));
Serial.printf("Usb1PllPfd1 Clock: %d\r\n", CLOCK_GetFreq(kCLOCK_Usb1PllPfd1Clk));
Serial.printf("Usb1PllPfd2 Clock: %d\r\n", CLOCK_GetFreq(kCLOCK_Usb1PllPfd2Clk));
Serial.printf("Usb1PllPfd3 Clock: %d\r\n", CLOCK_GetFreq(kCLOCK_Usb1PllPfd3Clk));
Serial.printf("Usb2Pll Clock: %d\r\n", CLOCK_GetFreq(kCLOCK_Usb2PllClk));
Serial.printf("SysPll Clock: %d\r\n", CLOCK_GetFreq(kCLOCK_SysPllClk));
Serial.printf("SysPllPfd0 Clock: %d\r\n", CLOCK_GetFreq(kCLOCK_SysPllPfd0Clk));
Serial.printf("SysPllPfd1 Clock: %d\r\n", CLOCK_GetFreq(kCLOCK_SysPllPfd1Clk));
Serial.printf("SysPllPfd2 Clock: %d\r\n", CLOCK_GetFreq(kCLOCK_SysPllPfd2Clk));
Serial.printf("SysPllPfd3 Clock: %d\r\n", CLOCK_GetFreq(kCLOCK_SysPllPfd3Clk));
Serial.printf("EnetPll0 Clock: %d\r\n", CLOCK_GetFreq(kCLOCK_EnetPll0Clk));
Serial.printf("EnetPll1 Clock: %d\r\n", CLOCK_GetFreq(kCLOCK_EnetPll1Clk));
Serial.printf("AudioPll Clock: %d\r\n", CLOCK_GetFreq(kCLOCK_AudioPllClk));
Serial.printf("VideoPll Clock: %d\r\n", CLOCK_GetFreq(kCLOCK_VideoPllClk));
}
void TimeDiff() {
Serial.printf("\n F_CPU=%u", F_CPU_ACTUAL );
[COLOR="#FF0000"]t0 = millis(); // opps - this is needed here[/COLOR]
while (!(millis() > (t0 + 1000)));
t0 = millis();
uint32_t t1 = micros(), t2 = millis();
delay(50);
uint32_t foo = millis() - t2;
Serial.printf("\t50ms delay:: %u us and %u ms\n", micros() - t1, foo);
printClocks();
if ( foo < 47 ) {
Serial.printf("\n >>>>>>>>>>>>>>>>>>>> BUGBUG @%u MHz <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<", F_CPU_ACTUAL/MHZ_MHZ );
Serial.printf("\n F_CPU=%u\n", F_CPU_ACTUAL );
}
}