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

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

问题描述

我正在用 C (gcc) 编写 STM32F4,它是 ARM Cortex M4,我看到所有示例都以无限循环完成它们的 main() 函数,即使程序的其余部分将从中断中执行.如果我尝试从我的程序中删除循环,中断也会停止.

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

推荐答案

查看在您的项目中 main 之前运行的设置代码.它可能是一些精简的汇编代码或更复杂的东西,但总的来说,它非常接近初始化堆栈和启动 C 运行时所需的最少处理器设置.

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.

如果您从 main 返回,您的处理器应该做什么?重启?悬挂?没有一个好的答案,因此您必须查看与您的程序链接的运行时支持代码,以了解其设计者的决定.在您的情况下,听起来他们没有允许 main 返回,因此处理器只是崩溃/发生异常并且您的程序停止工作.

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.

看起来您实际上正在寻找的是一种在空闲循环期间进入低功耗状态的方法.这当然是可能的 - 因为您的处理器是 ARM Cortex-M4,所以有一条简单的指令可以做到这一点:

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");
}

如果您正在使用 CMSIS(鉴于您使用了 SystemInit,看起来您正在使用),则程序集可能已经为您完成:

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();
}

此链接中了解更多详情.

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

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