Closed hacknus closed 1 year ago
if I set the hclk
to half of sysclk
let clocks = rcc
.cfgr
.use_hse(8.MHz())
.sysclk(168.MHz())
.hclk(84.MHz()) // AHB
.require_pll48clk()
.pclk1(21.MHz())
.pclk2(42.MHz())
.freeze();
the code will not hang and continue further - but USB will not show up on my host (macbook) if I am in debug
.
When using the --release
flag, the behavior is the same as stated above, even with different clocks.
In rust stable, the example works with different clocks.
EDIT: After cargo clean
, nightly now shows the same issue as stated above, even for different clocks...
I just tested this in Beta and it's also broken there. I think it's worth filing an issue with rust-lang
.
Edit: filed https://github.com/rust-lang/rust/issues/106830
As @hacknus said, in nightly this line hangs: while read_reg!(otg_global, regs.global(), GRSTCTL, AHBIDL) == 0 {}
The GRSTCTL register is at address 0x50000010, and AHBIDL is bit 31. On my F411 reproducing this issue, that memory address is 0x80000000 (AHBIDL set) as you'd expect, but the disassembly around that region indicates the code's actually reading from 0x500000d8
. No idea why yet.
(gdb) frame
#0 synopsys_usb_otg::bus::{impl#2}::enable::{closure#0}<stm32f4xx_hal::otg_fs::USB> (cs=<optimised out>) at /home/adam/.cargo/registry/src/github.com-1ecc6299db9ec823/synopsys-usb-otg-0.3.1/src/bus.rs:365
365 while read_reg!(otg_global, regs.global(), GRSTCTL, AHBIDL) == 0 {}
(gdb) x 0x50000010
0x50000010: 0x80000000
(gdb) x 0x500000d8
0x500000d8: 0x00000000
(gdb) info reg
R0 0x50000000 1342177280
R1 0x0 0
R2 0x0 0
R3 0x10 16
R4 0x0 0
R5 0x40020000 1073872896
R6 0x2000fd60 536935776
R7 0x2000ffec 536936428
R8 0xffffff80 4294967168
R9 0x0 0
R10 0x2000fbc8 536935368
R11 0xffffff81 4294967169
R12 0x0 0
SP 0x2000fb64 0x2000fb64
LR 0x8001511 134223121
PC 0x800152a 0x800152a <f4usb::__cortex_m_rt_main+1598>
XPSR 0x61005800 1627412480
FPSCR 0x0 0
MSP 0x2000fb64 536935268
PSP 0x0 0
0800151e d0f8d820 ldr r2, [r0, #0xd8]
08001522 002a cmp r2, #0
08001524 5cbf itt pl
08001526 d0f8d820 ldr r2, [r0, #0xd8]
0800152a 002a cmp r2, #0
0800152c 04d4 bmi #0x8001538
I think this is a bug in the RAL included in this crate; the RegisterBlock
structs are not marked repr(C)
(see https://github.com/adamgreig/stm32ral/commit/fc07b98e93c93ed208a8507db0f3cc3d71ecd9f4 and https://github.com/imxrt-rs/imxrt-hal/issues/13) so the compiler is in principle free to rearrange them, which it seems is happening here.
Adding #[repr(C)]
to all the RegisterBlocks and rebuilding seems to resolve the issue.
Not sure what commit in rust-lang exactly triggered this, looks like @antonok-edm has narrowed it down to between nightly-2022-11-19 and nightly-2022-11-24.
Original issue, was redirected here.
I tried both USB-examples on my STM32F405; poll and interrupt and both work with rust stable (rustc 1.66.0 ) but hang with nightly (rustc 1.68.0-nightly).
By using the debugger, I notice that I get stuck in this line:
line 365 in
.cargo/registry/src/github.com-1ecc6299db9ec823/synopsys-usb-otg-0.3.1/src/bus.rs
full output of the debugger:
it seems in
--release
some stuff is optimized out, but it gets stuck at the same point in debug: