cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F103C6 HSE oscillator Hardfault

holcsik2
Associate II
Posted on September 04, 2012 at 00:42

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

#hse-hardfault
3 REPLIES 3
Posted on September 04, 2012 at 02:44

You should look at what the chip is running at via the MCO pin to look at the SYSCLK

If you're using CMSIS, confirm whether or not it is calling SystemInit() and setting up some alternate clocking.

Confirm HSE_VALUE matches you external oscillator.

Examine the fault state to understand the instruction that causes the failure. Get a debugger and us it to analyze the problem.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
holcsik2
Associate II
Posted on September 04, 2012 at 13:00

I was curious, and started the PLL.

I've set the DIV to 2, and the MUL to 2, so it makes 12MHz, the PLL source is HSE.

Now it works like a charm.

I'm not so happy with this now, because it's a workaround not a fix.

///

At startup, it calls the SystemInit, and sets to 24MHz from HSI, and at the hwinit() I switch to HSE or (PLL in this case).

When I'm using the define to run the SetSysClockToHSE(void) function, It has the same freezing, and hardfault problem.

I think i will try this on my other bigger ST MCU, and see what will happen if I'm using only from HSE without PLL.

I have a debugger (openocd+ homemade ft2232 based) but i'm not very statisfied with it. Not very stable especially at startups. I'm planning to use a ST-link with some other debugger software....

 

holcsik2
Associate II
Posted on September 04, 2012 at 13:38

Some progress....

If I comment out all of my oscillator setting stuff from HWinit, and define to use the HSE at systeminit, it starts but only runs from HSI, somehow didn't switch to HSE or it fails. (because tampering with a 10nF cap at the Xtal pins didn't stop the mcu)

When I'm using the PLL, I can stop the HSE with the cap, and it switches to HSI.

Now I need to figure out /measure  why it couldn't start at it's normal SetSysClockToHSE(void) function.

But the hardfault thing is a mistery to me 🙂 but at least it doesn't occur this time...