rust-embedded / discovery

Discover the world of microcontrollers through Rust!
https://docs.rust-embedded.org/discovery/
Apache License 2.0
1.48k stars 513 forks source link

the-led-and-delay-abstraction.md `set half_period` is broken. #280

Open vringar opened 3 years ago

vringar commented 3 years ago

https://github.com/rust-embedded/discovery/blob/b2b16247cdedef3fb6ce10b8132832b95ba4e91a/src/05-led-roulette/the-led-and-delay-abstractions.md#L207-L220

Doesn't work anymore on rustc 1.48.0 (7eac88abb 2020-11-16).

Full asm output (built with RUSTFLAGS="--emit asm" cargo build --target thumbv7em-none-eabi ```asm .text .syntax unified .eabi_attribute 67, "2.09" .eabi_attribute 6, 13 .eabi_attribute 7, 77 .eabi_attribute 8, 0 .eabi_attribute 9, 2 .eabi_attribute 34, 1 .eabi_attribute 17, 1 .eabi_attribute 20, 1 .eabi_attribute 21, 0 .eabi_attribute 23, 3 .eabi_attribute 24, 1 .eabi_attribute 25, 1 .eabi_attribute 38, 1 .eabi_attribute 14, 0 .file "33ne5uerfjn3u8oj" .section .text.main,"ax",%progbits .globl main .p2align 1 .type main,%function .code 16 .thumb_func main: .Lfunc_begin0: .file 1 "/home/stefan/projects/discovery/src/05-led-roulette/src/main.rs" .loc 1 7 0 .fnstart .cfi_sections .debug_frame .cfi_startproc .save {r7, lr} push {r7, lr} .cfi_def_cfa_offset 8 .cfi_offset lr, -4 .cfi_offset r7, -8 .setfp r7, sp mov r7, sp .cfi_def_cfa_register r7 .Ltmp0: .loc 1 7 1 prologue_end bl _ZN12led_roulette18__cortex_m_rt_main17h0b2223cb0e38e500E .inst.n 0xdefe .Ltmp1: .Lfunc_end0: .size main, .Lfunc_end0-main .cfi_endproc .cantunwind .fnend .section .text._ZN12led_roulette18__cortex_m_rt_main17h0b2223cb0e38e500E,"ax",%progbits .p2align 1 .type _ZN12led_roulette18__cortex_m_rt_main17h0b2223cb0e38e500E,%function .code 16 .thumb_func _ZN12led_roulette18__cortex_m_rt_main17h0b2223cb0e38e500E: .Lfunc_begin1: .loc 1 8 0 .fnstart .cfi_startproc .save {r7, lr} push {r7, lr} .cfi_def_cfa_offset 8 .cfi_offset lr, -4 .cfi_offset r7, -8 .setfp r7, sp mov r7, sp .cfi_def_cfa_register r7 .pad #72 sub sp, #72 add r0, sp, #40 .Ltmp2: .loc 1 9 48 prologue_end bl _ZN4aux54init17ha90da604cbc87758E b .LBB1_1 .LBB1_1: .loc 1 0 48 is_stmt 0 add r0, sp, #40 add r1, sp, #12 .loc 1 9 10 ldm.w r0, {r2, r3, r4, r12, lr} stm.w r1, {r2, r3, r4, r12, lr} .loc 1 9 21 ldr r0, [sp, #60] ldr r1, [sp, #64] str r1, [sp, #36] str r0, [sp, #32] mov.w r0, #500 .Ltmp3: .loc 1 11 23 is_stmt 1 strh r0, [r7, #-2] .Ltmp4: .loc 1 13 5 b .LBB1_2 .LBB1_2: .loc 1 14 9 movw r2, :lower16:.L__unnamed_1 movt r2, :upper16:.L__unnamed_1 add r0, sp, #32 movs r1, #0 bl _ZN73_$LT$f3..led..Leds$u20$as$u20$core..ops..index..IndexMut$LT$usize$GT$$GT$9index_mut17h381d5f4e0c621798E str r0, [sp, #8] b .LBB1_3 .LBB1_3: ldr r0, [sp, #8] bl _ZN2f33led3Led2on17hd271365517c0b11cE b .LBB1_4 .LBB1_4: .loc 1 0 9 is_stmt 0 add r0, sp, #12 mov.w r1, #500 .loc 1 15 9 is_stmt 1 bl _ZN97_$LT$stm32f30x_hal..delay..Delay$u20$as$u20$embedded_hal..blocking..delay..DelayMs$LT$u16$GT$$GT$8delay_ms17h5400aa5c3c87f438E b .LBB1_5 .LBB1_5: .loc 1 17 9 movw r2, :lower16:.L__unnamed_2 movt r2, :upper16:.L__unnamed_2 add r0, sp, #32 movs r1, #0 bl _ZN73_$LT$f3..led..Leds$u20$as$u20$core..ops..index..IndexMut$LT$usize$GT$$GT$9index_mut17h381d5f4e0c621798E str r0, [sp, #4] b .LBB1_6 .LBB1_6: ldr r0, [sp, #4] bl _ZN2f33led3Led3off17hef007257bef9f451E b .LBB1_7 .LBB1_7: .loc 1 0 9 is_stmt 0 add r0, sp, #12 mov.w r1, #500 .loc 1 18 9 is_stmt 1 bl _ZN97_$LT$stm32f30x_hal..delay..Delay$u20$as$u20$embedded_hal..blocking..delay..DelayMs$LT$u16$GT$$GT$8delay_ms17h5400aa5c3c87f438E b .LBB1_8 .LBB1_8: .loc 1 13 5 b .LBB1_2 .Ltmp5: .Lfunc_end1: .size _ZN12led_roulette18__cortex_m_rt_main17h0b2223cb0e38e500E, .Lfunc_end1-_ZN12led_roulette18__cortex_m_rt_main17h0b2223cb0e38e500E .cfi_endproc .cantunwind .fnend .type .L__unnamed_3,%object .section .rodata..L__unnamed_3,"a",%progbits .L__unnamed_3: .ascii "src/05-led-roulette/src/main.rs" .size .L__unnamed_3, 31 .type .L__unnamed_1,%object .section .rodata..L__unnamed_1,"a",%progbits .p2align 2 .L__unnamed_1: .long .L__unnamed_3 .asciz "\037\000\000\000\016\000\000\000\t\000\000" .size .L__unnamed_1, 16 .type .L__unnamed_2,%object .section .rodata..L__unnamed_2,"a",%progbits .p2align 2 .L__unnamed_2: .long .L__unnamed_3 .asciz "\037\000\000\000\021\000\000\000\t\000\000" .size .L__unnamed_2, 16 .section .debug_abbrev,"",%progbits .byte 1 .byte 17 .byte 1 .byte 37 .byte 14 .byte 19 .byte 5 .byte 3 .byte 14 .byte 16 .byte 23 .byte 27 .byte 14 .ascii "\264B" .byte 25 .byte 17 .byte 1 .byte 85 .byte 23 .byte 0 .byte 0 .byte 2 .byte 57 .byte 1 .byte 3 .byte 14 .byte 0 .byte 0 .byte 3 .byte 46 .byte 0 .byte 17 .byte 1 .byte 18 .byte 6 .byte 64 .byte 24 .byte 110 .byte 14 .byte 3 .byte 14 .byte 58 .byte 11 .byte 59 .byte 11 .byte 63 .byte 25 .byte 0 .byte 0 .byte 4 .byte 46 .byte 1 .byte 17 .byte 1 .byte 18 .byte 6 .byte 64 .byte 24 .byte 110 .byte 14 .byte 3 .byte 14 .byte 58 .byte 11 .byte 59 .byte 11 .ascii "\207\001" .byte 25 .byte 0 .byte 0 .byte 5 .byte 11 .byte 1 .byte 17 .byte 1 .byte 18 .byte 6 .byte 0 .byte 0 .byte 6 .byte 52 .byte 0 .byte 2 .byte 24 .byte 3 .byte 14 .byte 58 .byte 11 .byte 59 .byte 11 .byte 73 .byte 19 .byte 0 .byte 0 .byte 7 .byte 19 .byte 1 .byte 3 .byte 14 .byte 11 .byte 11 .ascii "\210\001" .byte 15 .byte 0 .byte 0 .byte 8 .byte 13 .byte 0 .byte 3 .byte 14 .byte 73 .byte 19 .ascii "\210\001" .byte 15 .byte 56 .byte 11 .byte 0 .byte 0 .byte 9 .byte 47 .byte 0 .byte 73 .byte 19 .byte 3 .byte 14 .byte 0 .byte 0 .byte 10 .byte 19 .byte 0 .byte 3 .byte 14 .byte 11 .byte 11 .ascii "\210\001" .byte 15 .byte 0 .byte 0 .byte 11 .byte 36 .byte 0 .byte 3 .byte 14 .byte 62 .byte 11 .byte 11 .byte 11 .byte 0 .byte 0 .byte 12 .byte 15 .byte 0 .byte 73 .byte 19 .byte 3 .byte 14 .byte 51 .byte 6 .byte 0 .byte 0 .byte 13 .byte 1 .byte 1 .byte 73 .byte 19 .byte 0 .byte 0 .byte 14 .byte 33 .byte 0 .byte 73 .byte 19 .byte 34 .byte 13 .byte 55 .byte 11 .byte 0 .byte 0 .byte 15 .byte 36 .byte 0 .byte 3 .byte 14 .byte 11 .byte 11 .byte 62 .byte 11 .byte 0 .byte 0 .byte 0 .section .debug_info,"",%progbits .Lcu_begin0: .long .Ldebug_info_end0-.Ldebug_info_start0 .Ldebug_info_start0: .short 4 .long .debug_abbrev .byte 4 .byte 1 .long .Linfo_string0 .short 28 .long .Linfo_string1 .long .Lline_table_start0 .long .Linfo_string2 .long 0 .long .Ldebug_ranges0 .byte 2 .long .Linfo_string3 .byte 3 .long .Lfunc_begin0 .long .Lfunc_end0-.Lfunc_begin0 .byte 1 .byte 87 .long .Linfo_string4 .long .Linfo_string5 .byte 1 .byte 7 .byte 4 .long .Lfunc_begin1 .long .Lfunc_end1-.Lfunc_begin1 .byte 1 .byte 87 .long .Linfo_string6 .long .Linfo_string7 .byte 1 .byte 8 .byte 5 .long .Ltmp3 .long .Ltmp5-.Ltmp3 .byte 6 .byte 2 .byte 125 .byte 12 .long .Linfo_string8 .byte 1 .byte 9 .long 159 .byte 6 .byte 2 .byte 125 .byte 32 .long .Linfo_string36 .byte 1 .byte 9 .long 520 .byte 5 .long .Ltmp4 .long .Ltmp5-.Ltmp4 .byte 6 .byte 2 .byte 145 .byte 126 .long .Linfo_string53 .byte 1 .byte 11 .long 580 .byte 0 .byte 0 .byte 0 .byte 0 .byte 2 .long .Linfo_string9 .byte 2 .long .Linfo_string8 .byte 7 .long .Linfo_string35 .byte 20 .byte 4 .byte 8 .long .Linfo_string10 .long 195 .byte 4 .byte 0 .byte 8 .long .Linfo_string24 .long 406 .byte 1 .byte 0 .byte 0 .byte 0 .byte 2 .long .Linfo_string11 .byte 7 .long .Linfo_string23 .byte 20 .byte 4 .byte 8 .long .Linfo_string12 .long 275 .byte 4 .byte 0 .byte 8 .long .Linfo_string17 .long 275 .byte 4 .byte 4 .byte 8 .long .Linfo_string18 .long 275 .byte 4 .byte 8 .byte 8 .long .Linfo_string19 .long 389 .byte 1 .byte 16 .byte 8 .long .Linfo_string21 .long 389 .byte 1 .byte 17 .byte 8 .long .Linfo_string22 .long 275 .byte 4 .byte 12 .byte 0 .byte 0 .byte 2 .long .Linfo_string13 .byte 7 .long .Linfo_string16 .byte 4 .byte 4 .byte 8 .long .Linfo_string14 .long 382 .byte 4 .byte 0 .byte 0 .byte 0 .byte 2 .long .Linfo_string40 .byte 2 .long .Linfo_string41 .byte 7 .long .Linfo_string49 .byte 1 .byte 1 .byte 8 .long .Linfo_string42 .long 389 .byte 1 .byte 0 .byte 8 .long .Linfo_string43 .long 454 .byte 1 .byte 0 .byte 9 .long 345 .long .Linfo_string46 .byte 0 .byte 0 .byte 7 .long .Linfo_string47 .byte 0 .byte 1 .byte 8 .long .Linfo_string43 .long 471 .byte 1 .byte 0 .byte 9 .long 373 .long .Linfo_string46 .byte 0 .byte 10 .long .Linfo_string44 .byte 0 .byte 1 .byte 0 .byte 0 .byte 11 .long .Linfo_string15 .byte 7 .byte 4 .byte 11 .long .Linfo_string20 .byte 7 .byte 1 .byte 2 .long .Linfo_string25 .byte 2 .long .Linfo_string26 .byte 7 .long .Linfo_string34 .byte 0 .byte 1 .byte 8 .long .Linfo_string27 .long 437 .byte 1 .byte 0 .byte 0 .byte 0 .byte 0 .byte 2 .long .Linfo_string28 .byte 2 .long .Linfo_string29 .byte 7 .long .Linfo_string33 .byte 0 .byte 1 .byte 9 .long 490 .long .Linfo_string32 .byte 0 .byte 7 .long .Linfo_string48 .byte 0 .byte 1 .byte 9 .long 345 .long .Linfo_string32 .byte 0 .byte 7 .long .Linfo_string45 .byte 0 .byte 1 .byte 9 .long 373 .long .Linfo_string32 .byte 0 .byte 0 .byte 0 .byte 12 .long 503 .long .Linfo_string31 .long 0 .byte 11 .long .Linfo_string30 .byte 7 .byte 0 .byte 2 .long .Linfo_string37 .byte 2 .long .Linfo_string38 .byte 7 .long .Linfo_string52 .byte 8 .byte 1 .byte 8 .long .Linfo_string36 .long 560 .byte 1 .byte 0 .byte 0 .byte 7 .long .Linfo_string50 .byte 1 .byte 1 .byte 8 .long .Linfo_string39 .long 305 .byte 1 .byte 0 .byte 0 .byte 0 .byte 0 .byte 13 .long 539 .byte 14 .long 573 .byte 0 .byte 8 .byte 0 .byte 15 .long .Linfo_string51 .byte 8 .byte 7 .byte 11 .long .Linfo_string54 .byte 7 .byte 2 .byte 0 .Ldebug_info_end0: .section .text.main,"ax",%progbits .Lsec_end0: .section .text._ZN12led_roulette18__cortex_m_rt_main17h0b2223cb0e38e500E,"ax",%progbits .Lsec_end1: .section .debug_aranges,"",%progbits .long 36 .short 2 .long .Lcu_begin0 .byte 4 .byte 0 .zero 4,255 .long .Lfunc_begin0 .long .Lsec_end0-.Lfunc_begin0 .long .Lfunc_begin1 .long .Lsec_end1-.Lfunc_begin1 .long 0 .long 0 .section .debug_ranges,"",%progbits .Ldebug_ranges0: .long .Lfunc_begin0 .long .Lfunc_end0 .long .Lfunc_begin1 .long .Lfunc_end1 .long 0 .long 0 .section .debug_str,"MS",%progbits,1 .Linfo_string0: .asciz "clang LLVM (rustc version 1.48.0 (7eac88abb 2020-11-16))" .Linfo_string1: .asciz "src/05-led-roulette/src/main.rs" .Linfo_string2: .asciz "/home/stefan/projects/discovery" .Linfo_string3: .asciz "led_roulette" .Linfo_string4: .asciz "main" .Linfo_string5: .asciz "__cortex_m_rt_main_trampoline" .Linfo_string6: .asciz "_ZN12led_roulette18__cortex_m_rt_main17h0b2223cb0e38e500E" .Linfo_string7: .asciz "__cortex_m_rt_main" .Linfo_string8: .asciz "delay" .Linfo_string9: .asciz "stm32f30x_hal" .Linfo_string10: .asciz "clocks" .Linfo_string11: .asciz "rcc" .Linfo_string12: .asciz "hclk" .Linfo_string13: .asciz "time" .Linfo_string14: .asciz "__0" .Linfo_string15: .asciz "u32" .Linfo_string16: .asciz "Hertz" .Linfo_string17: .asciz "pclk1" .Linfo_string18: .asciz "pclk2" .Linfo_string19: .asciz "ppre1" .Linfo_string20: .asciz "u8" .Linfo_string21: .asciz "ppre2" .Linfo_string22: .asciz "sysclk" .Linfo_string23: .asciz "Clocks" .Linfo_string24: .asciz "syst" .Linfo_string25: .asciz "cortex_m" .Linfo_string26: .asciz "peripheral" .Linfo_string27: .asciz "_marker" .Linfo_string28: .asciz "core" .Linfo_string29: .asciz "marker" .Linfo_string30: .asciz "()" .Linfo_string31: .asciz "*const ()" .Linfo_string32: .asciz "T" .Linfo_string33: .asciz "PhantomData<*const ()>" .Linfo_string34: .asciz "SYST" .Linfo_string35: .asciz "Delay" .Linfo_string36: .asciz "leds" .Linfo_string37: .asciz "f3" .Linfo_string38: .asciz "led" .Linfo_string39: .asciz "pex" .Linfo_string40: .asciz "gpio" .Linfo_string41: .asciz "gpioe" .Linfo_string42: .asciz "i" .Linfo_string43: .asciz "_mode" .Linfo_string44: .asciz "PushPull" .Linfo_string45: .asciz "PhantomData" .Linfo_string46: .asciz "MODE" .Linfo_string47: .asciz "Output" .Linfo_string48: .asciz "PhantomData>" .Linfo_string49: .asciz "PEx>" .Linfo_string50: .asciz "Led" .Linfo_string51: .asciz "__ARRAY_SIZE_TYPE__" .Linfo_string52: .asciz "Leds" .Linfo_string53: .asciz "half_period" .Linfo_string54: .asciz "u16" .section .debug_pubnames,"",%progbits .long .LpubNames_end0-.LpubNames_begin0 .LpubNames_begin0: .short 2 .long .Lcu_begin0 .long 588 .long 396 .asciz "cortex_m" .long 43 .asciz "__cortex_m_rt_main_trampoline" .long 432 .asciz "marker" .long 38 .asciz "led_roulette" .long 64 .asciz "__cortex_m_rt_main" .long 427 .asciz "core" .long 401 .asciz "peripheral" .long 149 .asciz "stm32f30x_hal" .long 270 .asciz "time" .long 154 .asciz "delay" .long 295 .asciz "gpio" .long 300 .asciz "gpioe" .long 515 .asciz "led" .long 190 .asciz "rcc" .long 510 .asciz "f3" .long 0 .LpubNames_end0: .section .debug_pubtypes,"",%progbits .long .LpubTypes_end0-.LpubTypes_begin0 .LpubTypes_begin0: .short 2 .long .Lcu_begin0 .long 588 .long 490 .asciz "*const ()" .long 580 .asciz "u16" .long 520 .asciz "Leds" .long 437 .asciz "PhantomData<*const ()>" .long 275 .asciz "Hertz" .long 159 .asciz "Delay" .long 389 .asciz "u8" .long 503 .asciz "()" .long 406 .asciz "SYST" .long 539 .asciz "Led" .long 345 .asciz "Output" .long 305 .asciz "PEx>" .long 382 .asciz "u32" .long 471 .asciz "PhantomData" .long 454 .asciz "PhantomData>" .long 373 .asciz "PushPull" .long 195 .asciz "Clocks" .long 0 .LpubTypes_end0: .section ".note.GNU-stack","",%progbits .eabi_attribute 30, 5 .section .debug_line,"",%progbits .Lline_table_start0: ```

I think the relevant part of this output is section .LBB1_4 where we see that the constant 500 gets moved into r1 before calling the delay function instead of loading the variable from the stack.

pevandenburie commented 3 years ago

Explanation is given in issue #236 , although this does not resolve it. Maybe simply remove this gdb example from the book?