Frage

I'm programing a STM32F4 in C (gcc), it's a ARM Cortex M4, I see all examples finish their main() function with an infinite loop, even when the rest of the program will be executed from interruptions. If I try to remove the loop from my program, the interruptions stop being fired too.

Why can't I just remove this loop and exit the main thread?

here is the assembly (I guess it's thumb, but I can't read that, even with the doc):

LoopFillZerobss:
ldr  r3, = _ebss
cmp  r2, r3
bcc  FillZerobss

/* Call the clock system intitialization function.*/
bl  SystemInit 
/* Call the application's entry point.*/
bl  main
bx  lr    
.size  Reset_Handler, .-Reset_Handler
War es hilfreich?

Lösung

Take a look at the setup code that runs before main in your project. It might be some slim assembly code or something more complicated, but in general it's pretty close to the bare minimum amount of processor setup required to initialize a stack and get the C runtime going.

If you were return from main, what is your processor supposed to do? Reset? Hang? There's no one good answer, so you'll have to look at the runtime support code being linked with your program to see what its designers decided. In your case, it sounds like they didn't make any allowances for main to return, so the processor just crashes/takes an exception and your program stops working.

Edit: It seems like what you're actually looking for is a way to enter a low power state during the idle loop. That's certainly possible - since your processor is an ARM Cortex-M4, there's a simple instruction to do just that:

while (1)
{
    asm("wfi");
}

If you're using CMSIS (and it looks like you are, given your use of SystemInit), the assembly is probably already done for you:

while(1)
{
   __WFI();
}

More details at this link.

Andere Tipps

you are not running on an operating system. main() is just a function like any other, it returns from where it was called. With a bare metal system like this though that is not an operating system. So if your software is all interrupt driven and main() is simply for setup, then you need to keep the processor in a controlled state either in an infinite loop or in a low power mode. you can do that at the end of main() or whatever your setup function is called, or the assembly that calls main:

bl main
b . ;@ an infinite loop

or if you want wfi in there:

bl main
xyz:
wfi
b xyz
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top