cancel
Showing results for 
Search instead for 
Did you mean: 

STM32 - HSE OSC 16 MHz fails to start

slatybor
Associate II
Posted on December 31, 2014 at 17:20

Hi, I have a problem with proper main clock configuration in STM32F030K6T6.

I've connected an external crystal 16 MHz with 2x15 pF caps, but the HSE is not starting. Variable HSEStatus remains 0, so the PLL config doesn't execute. And yes, I've set the HSE_VALUE to ((uint32_t)16000000 in header file. Here's my startup code:

void
SystemInit (
void
)
{ 
/* Set HSION bit */
RCC->CR |= (uint32_t)0x00000001;
#if defined (STM32F0XX_MD) || defined (STM32F030X8)
/* Reset SW[1:0], HPRE[3:0], PPRE[2:0], ADCPRE and MCOSEL[2:0] bits */
RCC->CFGR &= (uint32_t)0xF8FFB80C;
#else
/* Reset SW[1:0], HPRE[3:0], PPRE[2:0], ADCPRE, MCOSEL[2:0], MCOPRE[2:0] and PLLNODIV bits */
RCC->CFGR &= (uint32_t)0x08FFB80C;
#endif /* STM32F0XX_MD or STM32F030X8 */
/* Reset HSEON, CSSON and PLLON bits */
RCC->CR &= (uint32_t)0xFEF6FFFF;
/* Reset HSEBYP bit */
RCC->CR &= (uint32_t)0xFFFBFFFF;
/* Reset PLLSRC, PLLXTPRE and PLLMUL[3:0] bits */
RCC->CFGR &= (uint32_t)0xFFC0FFFF;
/* Reset PREDIV1[3:0] bits */
RCC->CFGR2 &= (uint32_t)0xFFFFFFF0;
/* Reset USARTSW[1:0], I2CSW, CECSW and ADCSW bits */
RCC->CFGR3 &= (uint32_t)0xFFFFFEAC;
/* Reset HSI14 bit */
RCC->CR2 &= (uint32_t)0xFFFFFFFE;
/* Disable all interrupts */
RCC->CIR = 0x00000000;
/* Configure the System clock frequency, AHB/APBx prescalers and Flash settings */
SetSysClock();
}
static
void
SetSysClock(
void
)
{
__IO uint32_t StartUpCounter = 0, HSEStatus = 0;
/* SYSCLK, HCLK, PCLK configuration ----------------------------------------*/
/* Enable HSE */
RCC->CR |= ((uint32_t)RCC_CR_HSEON);
/* Wait till HSE is ready and if Time out is reached exit */
do
{
HSEStatus = RCC->CR & RCC_CR_HSERDY;
StartUpCounter++; 
} 
while
((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
if
((RCC->CR & RCC_CR_HSERDY) != RESET)
{
HSEStatus = (uint32_t)0x01;
}
else
{
HSEStatus = (uint32_t)0x00;
} 
if
(HSEStatus == (uint32_t)0x01)
{
/* Enable Prefetch Buffer and set Flash Latency */
FLASH->ACR = FLASH_ACR_PRFTBE | FLASH_ACR_LATENCY;
/* HCLK = SYSCLK */
RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;
/* PCLK = HCLK */
RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE_DIV1;
/* PLL configuration = HSE * 3 = 48 MHz */
RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLMULL));
RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_PREDIV1 | RCC_CFGR_PLLXTPRE_PREDIV1 | RCC_CFGR_PLLMULL3);
/* Enable PLL */
RCC->CR |= RCC_CR_PLLON;
/* Wait till PLL is ready */
while
((RCC->CR & RCC_CR_PLLRDY) == 0)
{
}
/* Select PLL as system clock source */
RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL; 
/* Wait till PLL is used as system clock source */
while
((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)RCC_CFGR_SWS_PLL)
{
}
}
else
{ 
/* If HSE fails to start-up, the application will have wrong clock 
configuration. User can add here some code to deal with this error */
//I turn on here my debug LED
}

7 REPLIES 7
slatybor
Associate II
Posted on January 01, 2015 at 12:35

The marking on my crystal is ''M16.000E7D''. Unfortunately I couldn't find any datasheet for it. Maybe I'll try to get another crystal with known parameters.

ST's code isn't that elegant in it's failure path - yeah, that's completely true. 

slatybor
Associate II
Posted on January 05, 2015 at 18:30

I replaced the crystal with a new 8 MHz one (known parameters) and it worked. The MCO shows 48 MHz signal at PLL x6.

Thanks for your advices, clive.

Tamas Novak
Associate III
Posted on January 06, 2015 at 22:05

There is an Application Note at ST, check for AN2867.

Posted on January 07, 2015 at 03:28

There is an Application Note at ST, check for AN2867.

Which was cited n the first response.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on December 31, 2014 at 18:10

Some random crystal, or one of known/chosen electrical characteristics?

http://www.st.com/web/en/resource/technical/document/application_note/CD00221665.pdf

Probably want to view the output indirectly via the MCO (PA8) pin rather than put a scope on the crystal itself and distorting the characteristics of the circuit it's in.

You know if you code more robustly you can have it fail-over to the HSI and run the PLL from that too.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
slatybor
Associate II
Posted on December 31, 2014 at 18:33

It's a random crystal, I've soldered another 16 MHz and it doesn't work with my code too. Maybe it's the PCB layout problem? Or should I try to solder a parallel load resistor?

I will try to see the MCO pin on the scope.

I know I can run PLL from HSI, but I want to use an external crystal.

Posted on December 31, 2014 at 18:43

The oscillator circuits on the STM32 are a bit fussy. You'd want to evaluate the capacitors and the need for serial or parallel resistance. Perhaps there are adequate markings on your current selection to be able to pull a specification or data sheet?

I was pointing out that ST's code isn't that elegant in it's failure path.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..