0

I am going through this discovery book and I have successfully completed it till chapter 8 Leds Again. Now in chapter 9 Clock and timers i was going through these for loop delays using function delay but I am not getting what actually we want to achieve and what this loop is doing in function delay. Can someone please help me with this? I know we are not using delay as we were using it in the previous chapter we have to work with it differently. This is what I want to understand.

#![no_main]
#![no_std]

use aux9::{entry, switch_hal::OutputSwitch, tim6};

#[inline(never)]
fn delay(_tim6: &tim6::RegisterBlock, ms: u16) {
    const K: u16 = 3; // this value needs to be tweaked
    for _ in 0..(K * ms) {
        aux9::nop()
    }
}
#[entry]
fn main() -> ! {
    let (leds, rcc, tim6) = aux9::init();
    let mut leds = leds.into_array();

    // TODO initialize TIM6

    let ms = 50;
    loop {
        for curr in 0..8 {
            let next = (curr + 1) % 8;

            leds[next].on().unwrap();
            delay(tim6, ms);
            leds[curr].off().unwrap();
            delay(tim6, ms);
        }
    }
}

Okay so I know why we are using nop here I just want to understand the working of constant k & for loop

Jmb
  • 18,893
  • 2
  • 28
  • 55
  • 2
    `K` looks to be the constant for the NOP-retiring throughput aka how many NOPs can be processed per millisecond. Then if you need to wait `x`ms, you send `K*x` NOPs and that should be about right. – Masklinn Jul 08 '21 at 07:08
  • `this discovery book` ? What "discovery book"? Citing chapters is not useful information without citing the book (title/author at least. Edition, ISBN, link also all useful to disambiguate). – Clifford Jul 08 '21 at 17:56
  • Surely if the book does not explain it, it tells you something about the quality if the book? – Clifford Jul 08 '21 at 18:03
  • I guess you are referring to [this](https://docs.rust-embedded.org/discovery/09-clocks-and-timers/index.html)? The loop makes sense in the context of the previous section 9.1. I don't know Rust (and this does not make me want to bother), but it is a bad implementation, but I think that is the point - the chapter leads on to using a hardware timer instead (it is a counter-example mostly to illustrate why it is a bad idea). I am not sure the book is as clear about that as it should be however. It is also labouring a point that it could get past much quicker. – Clifford Jul 08 '21 at 18:14

0 Answers0