cancel
Showing results for 
Search instead for 
Did you mean: 

Blink program not working

freya17365
Associate II
Posted on September 19, 2015 at 17:55

Hi to all,

I am trying to write my first program for STM32F0 discovery board.

Of course the first step is to make a LED blink, but, till now, I failed.

I created the program using CubeMX and of course it worked. Then I tried to write it by myself and there is no way to make it work.

Hereafter there is my system clock init function:

------------------------------------------

void SystemInit()

{

    /* Clock init */

    if(!(RCC->CR & 0x02))    // Check if HSI is on

    {

        RCC->CR |= 0x01;    // if not, just enable it

        while((RCC->CR & 0x02) == 0);    // Wait for HSI being ready

    }

    if((RCC->CFGR & 0x0C)) RCC->CFGR &= 0xFFFFFFFC;    // if HSI is not SystemClock, set HSI as SystemClock

    RCC->CFGR &= 0xFFFEF80F;                    // Set prescler PPRE and HPRE to no division

    RCC->CR &= 0xFEF2FFFF;                        // Reset HSEON, CSSON, HSEBYP and PLLON bits

    RCC->CFGR2 &= (uint32_t)0xFFFFFFF0;            // Reset PREDIV1[3:0] bits

    RCC->CFGR3 &= (uint32_t)0xFFFFFEAC;            // Reset USARTSW[1:0], I2CSW, CECSW and ADCSW bits

    RCC->CR2 &= (uint32_t)0xFFFFFFFE;            // Reset HSI14 bit

    RCC->CIR = 0x00000000;                        // Disable all interrupts

}

Could it be right?

How can I debug my program?

Thank you

Freya

6 REPLIES 6
Posted on September 19, 2015 at 18:38

How can I debug my program?

Well, here we'd uncheck the debuggers ''Run to main()'' option, start the thing up and step through an instruction at a time to see where it gets and what it's doing.

The chip should start from a clock that's sufficiently functional to run the processor, so SystemInit() could do nothing and things should function.

If you turn on clocks and PLLs, then you should probably wait in a loop for them to actually start, as this may take hundreds or thousands of machine cycles.

Counters in software delay loops should probably be volatile to stop the compiler optimizing them away.

Maybe you should shelve the C++/class stuff for a while and just have a linear run of code that initializes the clocks, programs the GPIO pins, and then sets some specific states. Then step through that front-to-back and confirm it's doing what you're telling it to do.

You'll have to dig into the startup.s file to understand how the constructors are being initialized and called.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
freya17365
Associate II
Posted on September 23, 2015 at 09:42

Thank you,

now the program works :)

Only one more program remains. If I add:

    SysTick_Config((uint32_t) 8000);    //HSI = 8MHz

    NVIC_SetPriority(SysTick_IRQn, 0);

program stops (crashes?).

Comparing my program and the one generated with CubeMX, it seems to me that SysTick initialization is the same, but mine is not working.

What can I do?

Thank you

Freya

svjatoslav
Associate II
Posted on September 23, 2015 at 13:06

Make sure you have interrupt handling function in your program.

freya17365
Associate II
Posted on September 23, 2015 at 13:13

void SysTick_Handler()

{

    myled.LedOn(LED3);

    return;

}

Posted on September 23, 2015 at 16:49

In C++ (.cpp) you'd surely need to address the name mangling.

extern ''C'' void SysTick_Handler()
{
myled.LedOn(LED3);
return;
}

If it crashes, you need to identify where it ends up. Perhaps the Hard Fault Handler, or Default Handler, with a while(1) loop.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
freya17365
Associate II
Posted on October 19, 2015 at 09:42

Excuse me for the delay.

Now it works!

Thank you

Freya