avr-rust / delay

arduino-like delay routines based on busy-wait loops
Apache License 2.0
15 stars 11 forks source link

Doesn't work properly when added as dependency (for Arduino Mega 2560) #11

Closed Flowneee closed 2 years ago

Flowneee commented 3 years ago

Hi! I'm trying to use this crate and encountered problem: delay is TOO short when library is added as dependency. I use this code for blink LED:

#![feature(llvm_asm)]
#![no_std]
#![no_main]

use avrd::current as c;

mod delay; // your source code, copied into file inside project 

#[no_mangle]
pub extern "C" fn main() {
    unsafe {
        *c::DDRB = 0xFF;
        *c::PORTB = 0x00;
    };

    loop {
        unsafe {
            *c::PORTB = 0xFF;
            // here is used local copy of `delay_ms`
            // it works OK, LED is on for about second
            delay::delay_ms(1000); 

            *c::PORTB = 0x00;
            // here is used `delay_ms` from dependency `avr_delay`
            // it is TOO short, blink (off) is barely visible (few milliseconds maybe);
            avr_delay::delay_ms(1000); // h
        };
    }
}

#[panic_handler]
fn panic(_info: &core::panic::PanicInfo) -> ! {
    loop {}
}

Also interesting thing: when I upload program with avrdude, version with avr_delay dependency is much bigger than version with only local copy:

// Only local copy
avrdude: Device signature = 0x1e9801 (probably m2560)
avrdude: reading input file "target/atmega2560/release/blink.elf"
avrdude: writing flash (826 bytes)

// With external dependency
avrdude: Device signature = 0x1e9801 (probably m2560)
avrdude: reading input file "target/atmega2560/release/blink.elf"
avrdude: writing flash (2844 bytes)

I also tested:

but all the same, if I use avr_delay from external dependency, it works way too fast.

I'm using Arduino Mega 2560 (or maybe it is Chinese clone, I'm not sure). My target:

{
    "arch": "avr",
    "atomic-cas": false,
    "cpu": "atmega2560",
    "data-layout": "e-P1-p:16:8-i8:8-i16:8-i32:8-i64:8-f32:8-f64:8-n8-a:8",
    "eh-frame-header": false,
    "exe-suffix": ".elf",
    "executables": true,
    "is-builtin": true,
    "late-link-args": {
        "gcc": [
            "-lc",
            "-lgcc"
        ]
    },
    "linker": "avr-gcc",
    "linker-flavor": "gcc",
    "linker-is-gnu": true,
    "llvm-target": "avr-unknown-unknown",
    "max-atomic-width": 0,
    "os": "unknown",
    "pre-link-args": {
        "gcc": [
            "-mmcu=atmega2560",
            "-Wl,--as-needed"
        ]
    },
    "target-c-int-width": "16",
    "target-endian": "little",
    "target-pointer-width": "16"
}

Let me know if I could provide anything else.

P.S. Local copy only works properly when opt-level set to 2 or 3, maybe this is somehow helpful.

P.P.S. Code is still super useful, it is very easy and intuitive for beginner like me, thanks!

stappersg commented 2 years ago

Let me know if I could provide anything else.

Please try again, because much has be changed & improved last fifteen months.

Flowneee commented 2 years ago

Please try again

Thank you, bu unfortunately I am unable to test it now. I am gonna close this issue, if somehow I will be able to try it and something doesn't work, I wil open a new issue