2015-09-19 08:55 AM
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 Freya2015-09-19 09:38 AM
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.2015-09-23 12:42 AM
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 Freya2015-09-23 04:06 AM
Make sure you have interrupt handling function in your program.
2015-09-23 04:13 AM
2015-09-23 07:49 AM
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.
2015-10-19 12:42 AM
Excuse me for the delay.
Now it works! Thank you Freya