我想把一些最初用C++编写的Arduino代码翻译成Rust,但是这一行内联程序集给我带来了麻烦。
asm volatile(
" lds r16, %[timer0] \n\t" //
#if defined(__AVR_ATmega2560__)
" add r16, %[toffset] \n\t" //
#endif
" subi r16, %[tsync] \n\t" //
" andi r16, 7 \n\t" //
" call TL \n\t" //
"TL: \n\t" //
#if defined(__AVR_ATmega2560__)
" pop r17 \n\t" //ATMEGA2560 has a 22bit PC!
#endif
" pop r31 \n\t" //
" pop r30 \n\t" //
" adiw r30, (LW-TL-5) \n\t" //
" add r30, r16 \n\t" //
//" adc r31, __zero_reg__ \n\t" //
" ijmp \n\t" //
"LW: \n\t" //
" nop \n\t" //
" nop \n\t" //
" nop \n\t" //
" nop \n\t" //
" nop \n\t" //
" nop \n\t" //
" nop \n\t" //
//" nop \n\t" //
"LBEND: \n\t" //
:
: [timer0] "i" (&TCNT0),
[toffset] "i" ((uint8_t)DEJITTER_OFFSET),
[tsync] "i" ((uint8_t)DEJITTER_SYNC)
: "r30", "r31", "r16", "r17");我最好的尝试是:
const TCNT0: *mut u8 = 70 as *mut u8;
const DEJITTER_OFFSET: u8 = 1;
const DEJITTER_SYNC: i8 = -2;
asm!(
" lds r16, %[timer0]
\t subi r16, %[tsync]
\t andi r16, 7
\t call TL
\tTL:
\t pop r31
\t pop r30
\t adiw r30, (LW-TL-5)
\t add r30, r16
\t ijmp
\tLW:
\t nop
\t nop
\t nop
\t nop
\t nop
\t nop
\t nop
\tLBEND:
\t"
:
: "{timer0}"(&TCNT0),
"{toffset}"(DEJITTER_OFFSET),
"{tsync}"(DEJITTER_SYNC)
: "r30", "r31", "r16": "volatile");我还远不能编译。当我试图编译时所显示的错误是:
error: couldn't allocate input reg for constraint '{timer0}'
--> /home/kirbylife/Proyectos/rvgax/src/lib.rs:53:9
|
53 | / asm!(
54 | | r" lds r16, ${timer0}
55 | | subi r16, ${tsync}
56 | | andi r16, 7
... |
77 | | "{tsync}"(DEJITTER_SYNC)
78 | | : "r30", "r31", "r16": "volatile");
| |_______________________________________^我用的是货物和铁锈1.38.0。
发布于 2020-06-22 01:57:31
错误的直接含义是没有名为timer0、toffset和tsync的寄存器。根本原因是Rust中的"{}"语法表示约束的寄存器名,而不是符号名称。换句话说,它对应于GCC的""材料,而不是[]的东西。我看不出用符号名称的方法,所以改用数字名称。另外,它使用$代替%来代替from约束。试一试:
const TCNT0: *mut u8 = 70 as *mut u8;
const DEJITTER_OFFSET: u8 = 1;
const DEJITTER_SYNC: i8 = -2;
asm!(
" lds r16, $0
\t subi r16, $2
\t andi r16, 7
\t call TL
\tTL:
\t pop r31
\t pop r30
\t adiw r30, (LW-TL-5)
\t add r30, r16
\t ijmp
\tLW:
\t nop
\t nop
\t nop
\t nop
\t nop
\t nop
\t nop
\tLBEND:
\t"
:
: "i"(&TCNT0),
"i"(DEJITTER_OFFSET),
"i"(DEJITTER_SYNC)
: "r30", "r31", "r16": "volatile");(注:未经测试,因为我目前还没有安装AVR)
https://stackoverflow.com/questions/62398031
复制相似问题