Skip to content

Commit

Permalink
added set_psram_clock
Browse files Browse the repository at this point in the history
  • Loading branch information
mjs513 committed Aug 24, 2024
1 parent 2b69ca9 commit 2d1fd0e
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions teensy4/clockspeed.c
Original file line number Diff line number Diff line change
Expand Up @@ -176,3 +176,37 @@ uint32_t set_arm_clock(uint32_t frequency)
return frequency;
}

#ifdef ARDUINO_TEENSY41
void set_psram_clock(int speed_mhz) {
// What clocks exist:
static const int flexspio2_clock_speeds[] = { 396, 720, 665, 528 };

// See what the closest setting might be:
uint8_t clk_save = 0;
uint8_t divider_save =0;
int min_delta = speed_mhz;
for (uint8_t clk = 0; clk < 4; clk++) {
uint8_t divider = (flexspio2_clock_speeds[clk] + (speed_mhz / 2)) / speed_mhz;
int delta = abs(speed_mhz - flexspio2_clock_speeds[clk] / divider);
if ((delta < min_delta) && (divider < 8)) {
min_delta = delta;
clk_save = clk;
divider_save = divider;
}
}

// first turn off FLEXSPI2
CCM_CCGR7 &= ~CCM_CCGR7_FLEXSPI2(CCM_CCGR_ON);

divider_save--; // 0 biased.
//Serial.printf("Update FLEXSPI2 speed: %u clk:%u div:%u Actual:%u\n", speed_mhz, clk_save, divider_save,
// flexspio2_clock_speeds[clk_save]/ (divider_save + 1));

// Set the clock settings.
CCM_CBCMR = (CCM_CBCMR & ~(CCM_CBCMR_FLEXSPI2_PODF_MASK | CCM_CBCMR_FLEXSPI2_CLK_SEL_MASK))
| CCM_CBCMR_FLEXSPI2_PODF(divider_save) | CCM_CBCMR_FLEXSPI2_CLK_SEL(clk_save);

// Turn FlexSPI2 clock back on
CCM_CCGR7 |= CCM_CCGR7_FLEXSPI2(CCM_CCGR_ON);
}
#endif

1 comment on commit 2d1fd0e

@Defragster
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be nice to see a comment on the possible speed range?
88 MHz is the default? And note likely possible faster speeds up to expected max 132 MHz?

Please sign in to comment.