rusEFI
The most advanced open source ECU
Loading...
Searching...
No Matches
Functions | Variables
osc_detector.cpp File Reference

Detailed Description

This logic automatically detects the speed of the oscillator or crystal connected to HSE.

Date
12 July 2021

It works by first using the reasonably-precise HSI oscillator (16MHz) to measure LSI (nominally 32khz, but wide tolerance). Then, it switches the system clock source to HSE, and repeats the same measurement. The inaccurate LSI will not drift significantly in the short period of time between these two measurements, so use it as a transfer standard to compare the speed of HSI and HSE. The ratio between the measured speed of LSI when running on HSE vs. HSI will give the ratio of speeds of HSE and HSI themselves. Since we know the value of HSI (16mhz), we can compute the speed of HSE.

Lastly, the PLL is reconfigured to use the correct input divider such that the input frequency is 1MHz (PLLM is set to N for an N-MHz HSE crystal).

Definition in file osc_detector.cpp.

Functions

static uint32_t getOneCapture ()
 
static uint32_t getTimerCounts (size_t count)
 
static void enableTimer ()
 
static void disableTimer ()
 
static void reprogramPll (uint8_t roundedHseMhz)
 
void __late_init ()
 

Variables

float hseFrequencyMhz
 
uint8_t autoDetectedRoundedMhz
 
static const float rtcpreDivider = 63
 

Function Documentation

◆ __late_init()

void __late_init ( void  )

Definition at line 145 of file osc_detector.cpp.

Here is the call graph for this function:

◆ disableTimer()

static void disableTimer ( )
static

Definition at line 68 of file osc_detector.cpp.

68 {
69 RCC->APB2ENR &= RCC_APB2ENR_TIM17EN;
70}

Referenced by __late_init().

Here is the caller graph for this function:

◆ enableTimer()

static void enableTimer ( )
static

Definition at line 64 of file osc_detector.cpp.

64 {
65 RCC->APB2ENR |= RCC_APB2ENR_TIM17EN;
66}

Referenced by __late_init().

Here is the caller graph for this function:

◆ getOneCapture()

static uint32_t getOneCapture ( )
static

Definition at line 32 of file osc_detector.cpp.

32 {
33 // wait for input capture
34 while ((TIMER->SR & TIM_SR_CC1IF) == 0);
35
36 // Return captured count
37 return TIMER->CCR1;
38}

Referenced by getTimerCounts().

Here is the caller graph for this function:

◆ getTimerCounts()

static uint32_t getTimerCounts ( size_t  count)
static

Definition at line 40 of file osc_detector.cpp.

40 {
41 // Burn one count
43
44 uint32_t firstCapture = getOneCapture();
45 uint32_t lastCapture = 0;
46
47 for (size_t i = 0; i < count; i++)
48 {
49 lastCapture = getOneCapture();
50 }
51
52 return lastCapture - firstCapture;
53}
static uint32_t getOneCapture()
uint16_t count
Definition tunerstudio.h:1

Referenced by __late_init().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ reprogramPll()

static void reprogramPll ( uint8_t  roundedHseMhz)
static

Definition at line 72 of file osc_detector.cpp.

72 {
73 // Switch system clock to HSI to configure PLL (SW = 0)
74 RCC->CFGR &= ~RCC_CFGR_SW;
75
76 // Stop all 3 PLLs
77 RCC->CR &= ~(RCC_CR_PLL1ON | RCC_CR_PLL2ON | RCC_CR_PLL3ON);
78
79 // H7 is configured for 2MHz input to PLL
80 auto pllm = roundedHseMhz / 2;
81
82 // Set PLLM for all 3 PLLs to the new value, and select HSE as the clock source
83 RCC->PLLCKSELR =
84 pllm << RCC_PLLCKSELR_DIVM1_Pos |
85 pllm << RCC_PLLCKSELR_DIVM2_Pos |
86 pllm << RCC_PLLCKSELR_DIVM3_Pos |
87 RCC_PLLCKSELR_PLLSRC_HSE;
88
89 // Enable PLLs
90 RCC->CR |= RCC_CR_PLL1ON | RCC_CR_PLL2ON | RCC_CR_PLL3ON;
91
92 // Wait for PLLs to lock
93 auto readyMask = RCC_CR_PLL1RDY | RCC_CR_PLL2RDY | RCC_CR_PLL3RDY;
94 while ((RCC->CR & readyMask) != readyMask) ;
95
96 // Switch system clock source back to PLL
97 RCC->CFGR |= RCC_CFGR_SW_PLL1;
98}

Referenced by __late_init().

Here is the caller graph for this function:

Variable Documentation

◆ autoDetectedRoundedMhz

uint8_t autoDetectedRoundedMhz

Definition at line 24 of file osc_detector.cpp.

Referenced by __late_init(), and sayHello().

◆ hseFrequencyMhz

float hseFrequencyMhz

Definition at line 23 of file osc_detector.cpp.

Referenced by __late_init(), and sayHello().

◆ rtcpreDivider

static const float rtcpreDivider = 63
static

Definition at line 62 of file osc_detector.cpp.

Referenced by __late_init().

Go to the source code of this file.