@@ -5,12 +5,12 @@ Now we're going to take a brief look into delay abstractions provided by `embedd
5
5
before combining this with the GPIO abstractions from the previous chapter in order to
6
6
finally make an LED blink.
7
7
8
- ` embedded-hal ` provides us with two abstractions to delay the execution of our program:
9
- [ ` DelayUs ` ] and [ ` DelayMs ` ] . Both of them essentially work the exact same way except
10
- that they accept different units for their delay function.
8
+ ` embedded-hal ` provides us with an abstractions to delay the execution of our program:
9
+ [ ` DelayNs ` ] . This abstraction provides three functions ` delay_ns ` , ` delay_us ` and ` delay_ms `
10
+ that delays execution for nano, micro or mili seconds respectively. They essentially work
11
+ the exact same way except that they accept different units for their delay function.
11
12
12
- [ `DelayUs` ] : https://docs.rs/embedded-hal/0.2.6/embedded_hal/blocking/delay/trait.DelayUs.html
13
- [ `DelayMs` ] : https://docs.rs/embedded-hal/0.2.6/embedded_hal/blocking/delay/trait.DelayMs.html
13
+ [ `DelayNs` ] : https://docs.rs/embedded-hal/1.0.0/embedded_hal/blocking/delay/trait.DelayNs.html
14
14
15
15
Inside our MCU, several so-called "timers" exist. They can do various things regarding time for us,
16
16
including simply pausing the execution of our program for a fixed amount of time. A very
@@ -22,21 +22,25 @@ simple delay-based program that prints something every second might for example
22
22
#![no_std]
23
23
24
24
use cortex_m_rt :: entry;
25
- use rtt_target :: {rtt_init_print, rprintln};
25
+ use embedded_hal :: delay :: DelayNS ;
26
+ use rtt_target :: {
27
+ rtt_init_print,
28
+ rprintln,
29
+ };
26
30
use panic_rtt_target as _;
27
31
use microbit :: board :: Board ;
28
32
use microbit :: hal :: timer :: Timer ;
29
- use microbit :: hal :: prelude :: * ;
30
33
31
34
#[entry]
32
35
fn main () -> ! {
33
36
rtt_init_print! ();
34
- let mut board = Board :: take (). unwrap ();
37
+
38
+ let board = Board :: take (). unwrap ();
35
39
36
40
let mut timer = Timer :: new (board . TIMER0 );
37
41
38
42
loop {
39
- timer . delay_ms (1000 u16 );
43
+ timer . delay_ms (1_000 u32 );
40
44
rprintln! (" 1000 ms passed" );
41
45
}
42
46
}
@@ -47,20 +51,20 @@ Note that we changed our panic implementation from `panic_halt` to
47
51
RTT lines from ` Cargo.toml ` and comment the ` panic-halt ` one out,
48
52
since Rust only allows one panic implementation at a time.
49
53
50
- In order to actually see the prints we have to change ` Embed.toml ` like this :
54
+ In order to actually see the prints we have to change ` Embed.toml ` like shown on the marked lines ( ` <--- Here ` ) :
51
55
```
52
56
[default.general]
53
57
# chip = "nrf52833_xxAA" # uncomment this line for micro:bit V2
54
58
# chip = "nrf51822_xxAA" # uncomment this line for micro:bit V1
55
59
56
60
[default.reset]
57
- halt_afterwards = false
61
+ halt_afterwards = false <--- Here
58
62
59
63
[default.rtt]
60
- enabled = true
64
+ enabled = true <--- Here
61
65
62
66
[default.gdb]
63
- enabled = false
67
+ enabled = false <--- Here
64
68
```
65
69
66
70
And now after putting the code into ` src/main.rs ` and another quick ` cargo embed ` (again with the same flags you used before)
@@ -78,15 +82,22 @@ a mash-up of the one above and the one that turned an LED on in the last section
78
82
#![no_std]
79
83
80
84
use cortex_m_rt :: entry;
81
- use rtt_target :: {rtt_init_print, rprintln};
85
+ use rtt_target :: {
86
+ rtt_init_print,
87
+ rprintln,
88
+ };
82
89
use panic_rtt_target as _;
90
+ use embedded_hal :: {
91
+ delay :: DelayNS ,
92
+ digital :: OutputPin ,
93
+ };
83
94
use microbit :: board :: Board ;
84
95
use microbit :: hal :: timer :: Timer ;
85
- use microbit :: hal :: prelude :: * ;
86
96
87
97
#[entry]
88
98
fn main () -> ! {
89
99
rtt_init_print! ();
100
+
90
101
let mut board = Board :: take (). unwrap ();
91
102
92
103
let mut timer = Timer :: new (board . TIMER0 );
@@ -98,6 +109,7 @@ fn main() -> ! {
98
109
row1 . set_low (). unwrap ();
99
110
rprintln! (" Dark!" );
100
111
timer . delay_ms (1_000_u16 );
112
+
101
113
row1 . set_high (). unwrap ();
102
114
rprintln! (" Light!" );
103
115
timer . delay_ms (1_000_u16 );
0 commit comments