AnsweredAssumed Answered

STM32F103C6 HSE oscillator Hardfault

Question asked by holcsik.tamas on Sep 4, 2012
Latest reply on Sep 4, 2012 by holcsik.tamas
Hello!

Sorry for the new thread
I'm new here, and I ran into a strange problem...
I have a board with an STM32F103C6 micro, and I'm trying to switch the uC to use the on board  HSE crystal (12MHz) instead of the inner 8MHz.
When it's using the HSI, there are no problems, but when I switch to HSE, it randomly crashes,  doesn't start up, or most of all, throws a HardFault....

I've measured the clock on the pins of the Xtal with an oscilloscope, it seems to be stable.

This is the code...

int main(void)
{
    inithw();

    while(1)
    {
        LED3_on();
        delay();
        LED3_off(); //theese are defined at the beginning GPIOA->BSRR|=(1<<6)
        delay();
    }
}

void HardFault_Handler(void)
{
    while(1)
    {
        LED4_on(); //RED led
    }
}

void delay(void) //some soft delay
{
    volatile uint32_t loop;
    for(loop=0; loop<100000; loop++);
}

//and the inithw function with the problematic part
void inithw(void)
{
    RCC_DeInit(); //reset registers->8MHz LSI
    RCC_HSEConfig(RCC_HSE_ON); //start HSE
    HSEStartUpStatus = RCC_WaitForHSEStartUp();//wait while  hse starts up
    if(HSEStartUpStatus==SUCCESS)
    {
            //No need to fiddle with the flash becasuse fck<24MHz
            //FLASH->ACR|=FLASH_ACR_PRFTBE; //prefetch buffer enable
            //FLASH->ACR|=FLASH_ACR_LATENCY_0; //
       
           //set divisions to 1 ->Run everything from12MHz
           RCC_HCLKConfig(RCC_SYSCLK_Div1); //HCLK=SYSCLK
            RCC_PCLK2Config(RCC_HCLK_Div1); //PCLK2=HCLK
            RCC_PCLK1Config(RCC_HCLK_Div1); //PCLK=HCLK,

            RCC_SYSCLKConfig(RCC_SYSCLKSource_HSE); //switch to HSE

            //wait till HSE is the clock source
            while (RCC_GetSYSCLKSource() != 0x04);
    }
    else
    { //ERROR
           led1_on(); //->there are no errors
    }
}

Strange facts:
-When I press the reset button, it some times starts and flashes the LED for  a few hundred milisecs, and then it goes to hardfault.
- sometime it's in hardfault and flashes the led for a few time and then freezes

It look like there are some problem when it's calling a function but I'm not sure.
I have another board with a different STM32 MCU, but there were no problem when I switched to HSE and PLL, it runs happily at 48MHz.

I looked at the examples, at the CMSIS archive, but I don't know what I'm doing wrong...

I'm using Sourcery G++ compiler, & linkerscript from the Atollic IDE, with corrected memory sizes

Outcomes