cortex-m icon indicating copy to clipboard operation
cortex-m copied to clipboard

'get_ticks_per_10ms' works like 'get_ticks_per_1ms' on QEMU

Open JOE1994 opened this issue 5 years ago • 1 comments

Hello, Using the Cargo config as below,

...
[target.thumbv7m-none-eabi]
runner = "qemu-system-arm -cpu cortex-m3 -machine lm3s6965evb -nographic -semihosting-config enable=on,target=native -kernel"
[build]
target = "thumbv7m-none-eabi"    # Cortex-M3
...

I tested running the below program using QEMU.

#![no_std]
#![no_main]
extern crate panic_halt;
use cortex_m::asm;
use cortex_m_rt::entry;
use cortex_m::peripheral::syst::SystClkSource;
use cortex_m::peripheral::SYST;
use cortex_m_semihosting::{debug, hprintln};
#[entry]
fn main() -> ! {
    let p = cortex_m::Peripherals::take().unwrap();
    let mut syst = p.SYST;

    syst.set_clock_source(SystClkSource::Core);
    syst.set_reload(SYST::get_ticks_per_10ms() * 1000); // Works strange in QEMU?
    syst.enable_counter();
    hprintln!(
        "{} ticks = 10 millisecond", SYST::get_ticks_per_10ms()
    ).unwrap();
    let mut seconds: usize = 0;
    loop {
        // busy wait until the timer wraps around
        while !syst.has_wrapped() {}
        seconds += 1;
        hprintln!("{} seconds passed", seconds).unwrap(); // prints every second!
    }
}

I checked that fn get_ticks_per_10ms always returns 10_000 in my program. Since I set called the set_reload function with an argument of get_ticks_per_10ms() * 1_000, I expected the program to print out to the console every 10 seconds (since ticks_per_10ms * 1_000 == ticks_per_10seconds). However, the program prints out to the console every 1 second.

I tried changing the argument fed to set_reload(), and it seems like the get_ticks_per_10ms() is behaving like get_ticks_per_1ms().

Unfortunately, I do not have an actual cortex-m device to test this behavior. I'm currently not sure whether this is an issue with the api of the cortex-m crate, or QEMU. If this is an issue with the cortex-m crate, maybe the function name of fn get_ticks_per_10ms should be changed to fn get_ticks_per_1ms.

Thank you for reading 😄

JOE1994 avatar Jan 05 '20 21:01 JOE1994

My bet is the qemu model has just implemented the calib register wrong. Could you check is_precise? perhaps qemu agrees that the value returned by calib is wrong

TDHolmes avatar Dec 17 '21 05:12 TDHolmes