为什么我需要在STM32编程无限循环? [英] Why do I need an infinite loop in STM32 programming?

查看:1433
本文介绍了为什么我需要在STM32编程无限循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在编程C(GCC)一STM32F4,这是一个的ARM Cortex M4,我看到所有的例子完成他们的main()函数有一个无限循环,即使程序的其余部分将中断执行。如果我尝试从我的程序删除循环中,中断停止在被炒得。

为什么我不能只是删除该循环并退出主线程?

下面是汇编(我想这是拇指,但我不能阅读,甚至与DOC):

  LoopF​​illZerobss:
LDR R3 = _ebss
CMP R2,R3
BCC FillZerobss/ *调用时钟系统intitialization功能。* /
BL SystemInit
/ *调用应用程序的入口点。* /
BL主
BX LR
.size Reset_Handler。-Reset_Handler


解决方案

看看设置code,以前运行主在您的项目。这可能是有些渺茫组装code或更复杂的东西,但总的来说它是pretty接近初始化协议栈,并获得C运行时将所需的处理器设置最低限度的金额。

如果你是从返回主,什么是你的处理器应该做的?复位?挂?有没有人很好的答案,所以你必须在运行时支持code看起来正与你的程序,看看它的设计师决定联系起来。对你来说,这听起来像他们没有做任何津贴为返回,因此处理器刚刚崩溃/接受一个例外,你的程序停止工作。

编辑:好像你实际上寻找的是空闲循环期间进入低功耗状态的一种方式。这当然可能的 - 因为你的处理器是ARM​​的Cortex-M4,有一个简单的指令,只是做到这一点:

 而(1)
{
    ASM(WFI);
}

如果你使用CMSIS(它看起来像你,鉴于你使用 SystemInit ),大会可能已经为你做了:

 而(1)
{
   __WFI();
}

此链接更多细节。

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

解决方案

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.

这篇关于为什么我需要在STM32编程无限循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆