Timer

    Here’s an example of this API.

    The first thing you notice is that we need to create an instance of Timer tobe able to generate delays. Timer::new will create a unique instance of thetimer and thus will return Some only the first time is called; subsequentcalls of this constructor will return None. We’ll return to this requirementof uniqueness later on.

    Periodic also happens to be an “infinite stream” because it can yield aninfinite number of Async::Ready values.

    Timer provides another method, oneshot. Which can be used to emulate the olddelay module. In fact, timer.oneshot(100).wait() is equivalent to thedelay::ms function because it also uses busy waiting to block for 100milliseconds.

    Back to the issue of uniqueness. Timer uses the TIM7 peripheral under thehood. If we allowed creation of multiple instances of it, that would make codelike this compile:

    1. let mut timer1 = Timer::new();
    2. let delay2 = timer.oneshot(200);
    3. delay1.wait(); // this actually blocks for 200 milliseconds!

    But this won’t work as expected because the second oneshot call will“overwrite” the previous call as both of these methods would endup writing to the same registers.