STM32F4x HSE initializaton at startup - Page 1
Forum section: electronics / general

author: kaadam
author_rank: Contributor
timestamp: May 12, 2016, 07:50:55 am
content: Hello all! I want to use the external oscillator with STM32, and I have to initialize it before the main program starts, because I have to use external SDRAM to some date storage. Unfortunately, my code does not work, and I have no idea why. Here is my current code:http://pastebin.com/BQWNibWAHave anyone experience with this kind of use the STM32F4 family? Thank you!

author: HackedFridgeMagnet
author_rank: Super Contributor
timestamp: May 12, 2016, 08:29:16 am
content: What hardware? Board? Schematic? crystal?You dont get past this line?Code:while(!(RCC->CR & (uint32_t)0x00030000));

author: kaadam
author_rank: Contributor
timestamp: May 12, 2016, 08:31:57 am
content: STM32F427 MCU, crystal oscillator, 25 MHz. It works well with the HAL initialization functions, so there is no hardware issue.

author: ealex
author_rank: Frequent Contributor
timestamp: May 12, 2016, 09:35:40 am
content: can you step trough the code and see where it actually stops ?as HackedFridgeMagnet said, does it stop where you are waiting for the HSERDY bit to get high ?

author: hans
author_rank: Super Contributor
timestamp: May 12, 2016, 10:38:59 am
content: Maybe check the PLL_M and other settings.I believe some demo boards may use a 8MHz oscillator. If you connect it to a 25MHz crystal it will obviously try to reach way too high PLL frequency and not stabilize.

author: kaadam
author_rank: Contributor
timestamp: May 12, 2016, 01:40:59 pm
content: The crystal starts up well, but after my code for some reason the default SystemInit() function runs again, and resets the RCC registers.... When i run it step-by-step, all my changes are written in registers...

author: Sal Ammoniac
author_rank: Super Contributor
timestamp: May 12, 2016, 03:41:58 pm
content: Here's my code that does HSE and PLL initialization. This is for an STM32F429. Perhaps you can glean something useful from it.Code:/** Perform oscillator/PLL setup for 180 MHz Sysclock speed, 90 MHz APB2 speed, 45 MHz APB1 speed*/FLASH->ACR &= ~0b111;FLASH->ACR |= 0b101; // set 5 wait states for FLASH accessRCC->CR |= RCC_CR_HSEON; // turn on HSE (high speed external) oscillatorwhile(!(RCC->CR & RCC_CR_HSERDY)); // wait for HSE oscillator readyRCC->PLLCFGR &= ~RCC_PLLCFGR_PLLP; // clear PLL PLLP bitsRCC->PLLCFGR |= (0 << 16); // set PLLP to 0 (VCO / 2)RCC->PLLCFGR &= ~RCC_PLLCFGR_PLLN; // clear PLL PLLN bitsRCC->PLLCFGR |= (360 << 6); // set PLLN to 360 (multiply VCO up to 360 MHz)RCC->PLLCFGR &= ~RCC_PLLCFGR_PLLM; // clear PLL PLLM bitsRCC->PLLCFGR |= 8; // divide HSE oscillator by 8 (8/8 = 1 MHz)RCC->PLLCFGR |= RCC_PLLCFGR_PLLSRC_HSE; // set PLL source to the HSE (crystal)RCC->CR |= RCC_CR_PLLON; // enable PLLwhile(!(RCC->CR & (1 << 25))); // wait for PLL lockRCC->CFGR &= ~RCC_CFGR_PPRE2; // clear APB2 divisor bitsRCC->CFGR |= 0b100 << 13; // set APB2 to 90 MHz (180 MHz / 2)RCC->CFGR &= ~RCC_CFGR_PPRE1; // clear APB2 divisor bitsRCC->CFGR |= 0b101 << 10; // set APB1 to 45 MHz (180 MHz / 4)RCC->CFGR |= 0b10; // set PLL as system clockFLASH->ACR |= FLASH_ACR_ICEN; // enable instruction cacheFLASH->ACR |= FLASH_ACR_DCEN; // enable data cache

author: richardman
author_rank: Frequent Contributor
timestamp: May 17, 2016, 09:06:33 am
content: Did you set the wait state? Anyway, the trick is to calculate PLLP, PLLN, and PLLM, just do them according to the formulas in the reference manual.