Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reprogramming, then quickly WFI-ing, resets Teensy #76

Closed
mciantyre opened this issue Aug 29, 2020 · 6 comments
Closed

Reprogramming, then quickly WFI-ing, resets Teensy #76

mciantyre opened this issue Aug 29, 2020 · 6 comments
Labels
bug Something isn't working documentation Improvements or additions to documentation

Comments

@mciantyre
Copy link
Owner

mciantyre commented Aug 29, 2020

Describe the bug
After you reprogram the Teensy, there's a chance that the chip will reset again when you wait for interrupt (WFI). You'll notice that the Teensy's red, programming LED is on, and your program doesn't run.

To Reproduce
In the pit.rs example, remove the spin-loop:

// Chip might stop running if we hit WFI out of reset too fast.
// So, block for 500ms, then drop into our loop.
timer.start(core::time::Duration::from_millis(500));
while timer.wait().is_err() {}

Then, build and load the example:

cargo objcopy --example pit --release --no-default-features -- -O ihex target/thumbv7em-none-eabihf/release/examples/pit.hex
teensy_loader_cli -w -v --mcu=TEENSY40 target/thumbv7em-none-eabihf/release/examples/pit.hex

Or, use this minimal example:

#![no_std]
#![no_main]

extern crate panic_halt;

use cortex_m::asm::wfi;
use teensy4_bsp::rt::entry;

#[entry]
fn main() -> ! {
    loop {
        wfi();
    }
}

Expected behavior
When main() is called, you can immediately WFI without issues. You can re-program the Teensy with the same example, and it works as expected.

Additional context
Work arounds:

  • Add a wait, as shown in the pit.rs example. A few hundreds of milliseconds seems to be OK. Or, add a busy-loop, as shown in the RTIC examples:

    // If we reach WFI on teensy 4.0 too quickly it seems to halt. Here we wait a short while in `init`
    // to avoid this issue. The issue only appears to occur when rebooting the device (via the button),
    // however there appears to be no issue when power cycling the device.
    //
    // TODO: Investigate exactly why this appears to be necessary.
    fn init_delay() {
    for _ in 0..10_000_000 {
    core::sync::atomic::spin_loop_hint();
    }
    }

  • Power-cycle the Teensy after reprogramming it. There's no issue when booting from a cold start.

You might not notice this when using the Teensy core libraries, since there's a 300ms delay before calling main(). We could introduce a similar workaround in the runtime crate.

@mciantyre mciantyre added bug Something isn't working help wanted Extra attention is needed labels Aug 29, 2020
@mciantyre
Copy link
Owner Author

Previous discussions in #64.

@ergpopler
Copy link

I am having this issue when trying to do the instructions on the readme, i cannot get it to work at all!

mciantyre added a commit to mciantyre/teensy4-rs-template that referenced this issue Sep 24, 2020
The previous example was demonstrating the issues described in
mciantyre/teensy4-rs#76, which results in a poor experience for
newcomers. We now demonstrate a slightly more interesting example
that won't immediately exhibit a bug.
@mciantyre
Copy link
Owner Author

Thanks for the feedback. I updated the example in the template project so it won't be the first thing new users hit.

Did any of the workarounds described in the issue help?

@ergpopler
Copy link

ergpopler commented Sep 25, 2020

Yes, when I put in the workaround and compiled it didn't update in my teensy loader, that was my bad.

@mciantyre
Copy link
Owner Author

No problem! Good to know a manual workaround helped.

I'm exploring a software workaround in #79.

bors bot added a commit that referenced this issue Sep 27, 2020
79: Disable low-power wait mode during startup r=mciantyre a=mciantyre

Set the low-power state to run mode, rather than keeping the default wait mode. Seems to mitigate #76. Tested on the `led.rs` example.

The PR also moves the vector table into DTCM. Now, interrupt handles will be found in memory, and no longer require an address lookup in off-board FLASH.

Co-authored-by: Ian McIntyre <ianpmcintyre@gmail.com>
@mciantyre
Copy link
Owner Author

mciantyre commented Oct 3, 2020

There's a workaround on the main branch. Leaving this open as my reminder to document the behavior in the runtime crate.

@mciantyre mciantyre added documentation Improvements or additions to documentation and removed help wanted Extra attention is needed labels Oct 3, 2020
@bors bors bot closed this as completed in ce71c2f Oct 16, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

2 participants