gpt4 book ai didi

c - 使用 avr-gcc 在 AVR XMEGA 上的时序循环中不满足内联汇编器约束

转载 作者:太空宇宙 更新时间:2023-11-03 23:31:07 26 4
gpt4 key购买 nike

我在以 AVR 为目标的 C 中遇到毫秒延迟循环的问题。我在从 Ubuntu 存储库获得的 Linux 上使用 avr-gcc 4.7.0,我也有尝试了新编译的 4.7.2。目标硬件是 XMEGA128A1,目前主频为 2 MHz。

下面的函数经常(但不总是——我在遇到麻烦之前调用了几次这个函数)提示约束:

../common/spin_delay.h:64:9: warning: asm operand 3 probably doesn’t match constraints [enabled by default]
../common/spin_delay.h:64:9: error: impossible constraint in ‘asm’

如果我删除停止编译并出现此错误的特定调用,它也会提示操作数 0、1 和 2。

#define LOOPS_PER_MS ((CPU_CLK_HZ/1000)/4)

static inline void ms_spin(unsigned short ms) {
if (ms) {
__asm__ __volatile__ (
" ldi r24, %1 \n"
" ldi r25, %0 \n"
"1: ldi r26, %3 \n"
" ldi r27, %2 \n"
"2: sbiw r26, 1 \n"
" brne 2b \n"
" sbiw r24, 1 \n"
" brne 1b \n"
:
: "M" (ms >> 8),
"M" (ms & 0xff),
"M" (LOOPS_PER_MS >> 8),
"M" (LOOPS_PER_MS & 0xff)
: "r24", "r25", "r26", "r27"
);
}
}

相同的代码在使用 WinAVR 的 avr-gcc 4.3.3 的 Windows 上编译得很好,但是,这让我认为这是内联汇编程序自那时以来的一些变化。

在我看来,这一切看起来都是正确的,因为 16 位 short 被分解为高字节和低字节并受“M”(8 位常量)约束,而它工作几次的事实规则解决硬定义的 CPU_CLK_HZ 派生常量的问题。问题操作数是 0-3 中的任何一个这一事实表明它不是特定的失败的 asm 操作数。

我也尝试按照推荐使用 n 约束 here ,但错误仍然存​​在。

最佳答案


编辑:

最近的 gcc 版本支持内置的周期精确延迟:

void __builtin_avr_delay_cycles (unsigned long ticks)

(但请注意:“ticks 必须是编译时整数常量;不支持具有可变周期数的延迟。”)


我认为问题在于您将 ms 变量声明为汇编程序的常量 输入。我相信,这甚至不应该首先编译。

如果当它确实编译时,可能只是因为编译器优化使 gcc “识别”声明为变量 (ms) 的内容实际上是常量并且在编译时已知 -时间。然而,不能指望这些优化总是以相同的方式发生。

因此,我的建议是不要ms 使用“M” 约束,因为它在这里基本上不合适。

通过使用“正确的”约束,您可能会获得更健壮和正确的代码,甚至可能性能更高,如:

#define LOOPS_PER_MS (((CPU_CLK_HZ/1000)-(1+1+2+2))/(2+2)) // accounting for the overhead of 6 (1+1+2+2) cycles per ms and the 4 (2+2) cycles per inner loop iteration

static inline void ms_spin(unsigned short ms) {
if (ms) {
unsigned short dummy;
__asm__ __volatile__ (
"ms_spin_outer_loop_%=: \n"

" ldi %A[loopcnt], lo8(%[loops]) \n"
" ldi %B[loopcnt], hi8(%[loops]) \n"

"ms_spin_inner_loop_%=: \n"

" sbiw %A[loopcnt], 1 \n"
" brne ms_spin_inner_loop_%= \n"

" sbiw %A[ms], 1 \n"
" brne ms_spin_outer_loop_%= \n"

: [ms] "+w" (ms),
[loopcnt] "=&w" (dummy)
: [loops] "i" (LOOPS_PER_MS)
: // none.
);
}
}

使用“正确的”约束还会将特定寄存器集的选择留给 gcc 使用,这可能会更好地优化您的内联汇编程序语句。

为了让事情变得更加可预测,您可能需要考虑强制 gcc 始终通过 __attribute__((always_inline)) 修饰内联函数。

有关 avr-gcc 中内联汇编器约束的更多详细信息,请参阅 avr-libc documentation 示例.此列表并非详尽无遗,因此请查看一般 gcc documentation有时可能会有所帮助。

关于c - 使用 avr-gcc 在 AVR XMEGA 上的时序循环中不满足内联汇编器约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15158823/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com