Start on HSI clock (#2957)
* chibios uses HSI * set pll to use HSE * simplify capture * even better logging * string Co-authored-by: Matthew Kennedy <makenne@microsoft.com>
This commit is contained in:
parent
c36fdc15a5
commit
d475b4721f
|
@ -43,8 +43,9 @@ static void sayHello(void) {
|
||||||
efiPrintf(PROTOCOL_HELLO_PREFIX " COMPILER=%s", __VERSION__);
|
efiPrintf(PROTOCOL_HELLO_PREFIX " COMPILER=%s", __VERSION__);
|
||||||
|
|
||||||
#ifdef ENABLE_AUTO_DETECT_HSE
|
#ifdef ENABLE_AUTO_DETECT_HSE
|
||||||
|
extern float hseFrequencyMhz;
|
||||||
extern uint8_t autoDetectedPllMValue;
|
extern uint8_t autoDetectedPllMValue;
|
||||||
efiPrintf(PROTOCOL_HELLO_PREFIX " autoDetectedPllMValue=%d", autoDetectedPllMValue);
|
efiPrintf(PROTOCOL_HELLO_PREFIX " detected HSE clock %.2f MHz, PLLM = %d", hseFrequencyMhz, autoDetectedPllMValue);
|
||||||
#endif /* ENABLE_AUTO_DETECT_HSE */
|
#endif /* ENABLE_AUTO_DETECT_HSE */
|
||||||
|
|
||||||
#if defined(STM32F4) || defined(STM32F7)
|
#if defined(STM32F4) || defined(STM32F7)
|
||||||
|
|
|
@ -324,11 +324,9 @@
|
||||||
*/
|
*/
|
||||||
#define STM32_WDG_USE_IWDG FALSE
|
#define STM32_WDG_USE_IWDG FALSE
|
||||||
|
|
||||||
// We auto detect the value of HSE, so set the default PLLM value to the maximum,
|
// Pretend we have a 25MHz external crystal. This value isn't actually used since we
|
||||||
// so we don't accidentially overclock to processor before we know how fast HSE is
|
// configure the PLL to start on the HSI oscillator, then compute HSE's speed at runtime
|
||||||
#define STM32_PLLM_VALUE 25
|
// and reconfigure the PLL appropriately.
|
||||||
|
|
||||||
// This also means we have to pretend (for now) we have a 25MHz HSE fitted
|
|
||||||
#define STM32_HSECLK 25000000
|
#define STM32_HSECLK 25000000
|
||||||
|
|
||||||
// After boot, we will detect the real frequency, and adjust the PLL M value to suit
|
// After boot, we will detect the real frequency, and adjust the PLL M value to suit
|
||||||
|
|
|
@ -19,6 +19,7 @@
|
||||||
|
|
||||||
#ifdef ENABLE_AUTO_DETECT_HSE
|
#ifdef ENABLE_AUTO_DETECT_HSE
|
||||||
|
|
||||||
|
float hseFrequencyMhz;
|
||||||
uint8_t autoDetectedPllMValue;
|
uint8_t autoDetectedPllMValue;
|
||||||
|
|
||||||
static void useHsi() {
|
static void useHsi() {
|
||||||
|
@ -50,17 +51,15 @@ static uint32_t getAverageLsiCounts() {
|
||||||
// Burn one count
|
// Burn one count
|
||||||
getOneCapture();
|
getOneCapture();
|
||||||
|
|
||||||
uint32_t lastCapture = getOneCapture();
|
uint32_t firstCapture = getOneCapture();
|
||||||
uint32_t sum = 0;
|
uint32_t lastCapture;
|
||||||
|
|
||||||
for (size_t i = 0; i < 20; i++)
|
for (size_t i = 0; i < 20; i++)
|
||||||
{
|
{
|
||||||
auto capture = getOneCapture();
|
lastCapture = getOneCapture();
|
||||||
sum += (capture - lastCapture);
|
|
||||||
lastCapture = capture;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return sum;
|
return lastCapture - firstCapture;
|
||||||
}
|
}
|
||||||
|
|
||||||
// This only works if you're using the PLL as the configured clock source!
|
// This only works if you're using the PLL as the configured clock source!
|
||||||
|
@ -78,11 +77,13 @@ static void reprogramPll(uint8_t pllM) {
|
||||||
// Stop the PLL
|
// Stop the PLL
|
||||||
RCC->CR &= ~RCC_CR_PLLON;
|
RCC->CR &= ~RCC_CR_PLLON;
|
||||||
|
|
||||||
// Mask out the old PLLM val
|
// Mask out the old PLLM and PLLSRC
|
||||||
RCC->PLLCFGR &= ~RCC_PLLCFGR_PLLM_Msk;
|
RCC->PLLCFGR &= ~(RCC_PLLCFGR_PLLM_Msk | RCC_PLLCFGR_PLLSRC_Msk);
|
||||||
|
|
||||||
// Stick in the new PLLM value
|
// Stick in the new PLLM value
|
||||||
RCC->PLLCFGR |= (pllM << RCC_PLLCFGR_PLLM_Pos) & RCC_PLLCFGR_PLLM_Msk;
|
RCC->PLLCFGR |= (pllM << RCC_PLLCFGR_PLLM_Pos) & RCC_PLLCFGR_PLLM_Msk;
|
||||||
|
// Set PLLSRC to HSE
|
||||||
|
RCC->PLLCFGR |= RCC_PLLCFGR_PLLSRC_HSE;
|
||||||
|
|
||||||
// Reenable PLL, wait for lock
|
// Reenable PLL, wait for lock
|
||||||
RCC->CR |= RCC_CR_PLLON;
|
RCC->CR |= RCC_CR_PLLON;
|
||||||
|
@ -122,8 +123,9 @@ extern "C" void __late_init() {
|
||||||
RCC->APB1ENR &= ~RCC_APB1ENR_TIM5EN;
|
RCC->APB1ENR &= ~RCC_APB1ENR_TIM5EN;
|
||||||
|
|
||||||
// The external clocks's frequency is the ratio of the measured LSI speed, times HSI's speed (16MHz)
|
// The external clocks's frequency is the ratio of the measured LSI speed, times HSI's speed (16MHz)
|
||||||
float hseFrequencyMhz = 16.0f * hseCounts / hsiCounts;
|
constexpr float hsiMhz = STM32_HSICLK * 1e-6;
|
||||||
|
|
||||||
|
hseFrequencyMhz = hsiMhz * hseCounts / hsiCounts;
|
||||||
autoDetectedPllMValue = efiRound(hseFrequencyMhz, 1);
|
autoDetectedPllMValue = efiRound(hseFrequencyMhz, 1);
|
||||||
|
|
||||||
reprogramPll(autoDetectedPllMValue);
|
reprogramPll(autoDetectedPllMValue);
|
||||||
|
|
|
@ -51,8 +51,8 @@
|
||||||
|
|
||||||
#define STM32_CLOCK48_REQUIRED TRUE
|
#define STM32_CLOCK48_REQUIRED TRUE
|
||||||
#define STM32_SW STM32_SW_PLL
|
#define STM32_SW STM32_SW_PLL
|
||||||
#define STM32_PLLSRC STM32_PLLSRC_HSE
|
#define STM32_PLLSRC STM32_PLLSRC_HSI
|
||||||
|
#define STM32_PLLM_VALUE 16
|
||||||
#define STM32_PLLN_VALUE 336
|
#define STM32_PLLN_VALUE 336
|
||||||
#define STM32_PLLP_VALUE 2
|
#define STM32_PLLP_VALUE 2
|
||||||
#define STM32_PLLQ_VALUE 7
|
#define STM32_PLLQ_VALUE 7
|
||||||
|
|
|
@ -72,7 +72,8 @@
|
||||||
#define STM32_LSE_ENABLED FALSE
|
#define STM32_LSE_ENABLED FALSE
|
||||||
#define STM32_CLOCK48_REQUIRED TRUE
|
#define STM32_CLOCK48_REQUIRED TRUE
|
||||||
#define STM32_SW STM32_SW_PLL
|
#define STM32_SW STM32_SW_PLL
|
||||||
#define STM32_PLLSRC STM32_PLLSRC_HSE
|
#define STM32_PLLSRC STM32_PLLSRC_HSI
|
||||||
|
#define STM32_PLLM_VALUE 16
|
||||||
#define STM32_PLLN_VALUE 432
|
#define STM32_PLLN_VALUE 432
|
||||||
#define STM32_PLLP_VALUE 2
|
#define STM32_PLLP_VALUE 2
|
||||||
#define STM32_PLLQ_VALUE 9
|
#define STM32_PLLQ_VALUE 9
|
||||||
|
|
Loading…
Reference in New Issue