Skip to content

Commit 0da0f43

Browse files
Merge #308
308: Add shareable instances of config.toml and openocd.gdb r=eldruin a=winksaville Instead of every chapter having its own copy of the cargo configuration and openocd.gdb files this change creates a shared set. This will make it much easier for the user of the discovery book to handle the situation where their Arm gdb is NOT arm-none-eabi-gdb. As only the shared copy of .cargo/config.toml will have to be modified. Also src/05-led-roulette is updated to take advantage of this and I will go through each of the other chapters changing them to use the shared set. I also needed to comment out the rustflags variable in all of the config files as I'd get an error executing ci/script.sh: ry.x:5: region 'FLASH' already defined >>> FLASH : ORIGIN = 0x08000000, LENGTH = 256K >>> Co-authored-by: Wink Saville <wink@saville.com>
2 parents f9491d5 + 4d57c9a commit 0da0f43

File tree

14 files changed

+199
-94
lines changed

14 files changed

+199
-94
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
[target.thumbv7em-none-eabihf]
22
runner = "arm-none-eabi-gdb -q"
3+
# runner = "gdb-multiarch -q"
4+
# runner = "gdb -q"
35
rustflags = [
46
"-C", "link-arg=-Tlink.x",
57
]
8+
9+
[build]
10+
target = "thumbv7em-none-eabihf"
11+

src/05-led-roulette/debug-it.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -253,10 +253,11 @@ mode enter one of the following commands in the GDB shell:
253253
> **NOTE** Apologies to Windows users, the GDB shipped with the GNU ARM Embedded Toolchain
254254
> may not support this TUI mode `:-(`.
255255
256-
Below is an example of setting up for a `layout split` by executing the follow commands:
256+
Below is an example of setting up for a `layout split` by executing the follow commands.
257+
As you can see we've dropped passing the `--target` parameter:
257258
258259
``` console
259-
$ cargo run --target thumbv7em-none-eabihf
260+
$ cargo run
260261
(gdb) target remote :3333
261262
(gdb) load
262263
(gdb) set print asm-demangle on
@@ -265,10 +266,10 @@ $ cargo run --target thumbv7em-none-eabihf
265266
(gdb) continue
266267
```
267268

268-
Here is a command line with the above commands as `-ex` parameters to save you some typing:
269-
269+
Here is a command line with the above commands as `-ex` parameters to save you some typing,
270+
shortly we'll be providing an easier way to execute the initial set of commands:
270271
```
271-
cargo run --target thumbv7em-none-eabihf -- -q -ex 'target remote :3333' -ex 'load' -ex 'set print asm-demangle on' -ex 'set style sources off' -ex 'b main' -ex 'c' target/thumbv7em-none-eabihf/debug/led-roulette
272+
cargo run -- -q -ex 'target remote :3333' -ex 'load' -ex 'set print asm-demangle on' -ex 'set style sources off' -ex 'b main' -ex 'c' target/thumbv7em-none-eabihf/debug/led-roulette
272273
```
273274

274275
And below is the result:

src/05-led-roulette/flash-it.md

Lines changed: 49 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Onto the actual flashing. First thing we need is to do is launch OpenOCD. We did
1111
previous section but this time we'll run the command inside a temporary directory (`/tmp` on \*nix;
1212
`%TEMP%` on Windows).
1313

14-
Make sure the F3 is connected to your computer and run the following commands on a **new terminal**.
14+
Make sure the F3 is connected to your computer and run the following commands in a **new terminal**.
1515

1616
## For *nix & MacOS:
1717
``` console
@@ -85,7 +85,7 @@ I mentioned that OpenOCD provides a GDB server so let's connect to that right no
8585

8686
## Execute GDB
8787

88-
First we need to determine what version of GDB you have that is capable of debugging ARM binaries.
88+
First, we need to determine what version of `gdb` you have that is capable of debugging ARM binaries.
8989

9090
This could be any one of the commands below, try each one:
9191
``` console
@@ -140,74 +140,87 @@ In both failing and successful cases you should see new output in the **OpenOCD
140140
By default OpenOCD's GDB server listens on TCP port 3333 (localhost). This command is connecting to
141141
that port.
142142

143-
## Update .cargo/config
143+
## Update ../.cargo/config.toml
144144

145145
Now that you've successfully determined which debugger you need to use
146-
we need to change `.cargo/config` so that `cargo run` command can succeed.
146+
we need to change `../cargo/config.toml` so that `cargo run` command will succeed.
147+
Note: `cargo` is the rust package manager and you can read about it
148+
[here](https://doc.rust-lang.org/cargo/).
147149

148-
Get back to the terminal prompt and looking at `.cargo/config`:
150+
Get back to the terminal prompt and look at `../cargo/config.toml`:
149151
``` console
150-
$ cat .cargo/config
152+
~/embedded-discovery/src/05-led-roulette
153+
$ cat ../.cargo/config.toml
151154
[target.thumbv7em-none-eabihf]
152155
runner = "arm-none-eabi-gdb -q"
156+
# runner = "gdb-multiarch -q"
157+
# runner = "gdb -q"
153158
rustflags = [
154159
"-C", "link-arg=-Tlink.x",
155160
]
156161

162+
[build]
163+
target = "thumbv7em-none-eabihf"
164+
157165
```
158-
Use your favorite editor to edit `.cargo/config` so that the
159-
runner line contains the name of that debugger:
166+
Use your favorite editor to edit `../.cargo/config.toml` so that the
167+
`runner` line contains the correct name of that debugger:
160168
``` console
161-
nano .cargo/config
169+
nano ../.cargo/config.toml
162170
```
163171
For example, if your debugger was `gdb-multiarch` then after
164-
editing you should have:
165-
``` console
166-
$ cat .cargo/config
167-
[target.thumbv7em-none-eabihf]
168-
runner = "gdb-mulitarch -q"
169-
rustflags = [
170-
"-C", "link-arg=-Tlink.x",
171-
]
172-
```
173-
And `git diff` should be:
172+
editing the `git diff` should be:
174173
``` diff
175-
$ git diff .cargo/config
176-
diff --git a/src/05-led-roulette/.cargo/config b/src/05-led-roulette/.cargo/config
177-
index 01d25c8..c23dc80 100644
178-
--- a/src/05-led-roulette/.cargo/config
179-
+++ b/src/05-led-roulette/.cargo/config
180-
@@ -1,5 +1,5 @@
174+
$ git diff ../.cargo/config.toml
175+
diff --git a/src/.cargo/config.toml b/src/.cargo/config.toml
176+
index ddff17f..8512cfe 100644
177+
--- a/src/.cargo/config.toml
178+
+++ b/src/.cargo/config.toml
179+
@@ -1,6 +1,6 @@
181180
[target.thumbv7em-none-eabihf]
182181
-runner = "arm-none-eabi-gdb -q"
182+
-# runner = "gdb-multiarch -q"
183+
+# runner = "arm-none-eabi-gdb -q"
183184
+runner = "gdb-multiarch -q"
185+
# runner = "gdb -q"
184186
rustflags = [
185187
"-C", "link-arg=-Tlink.x",
186-
]
187188
```
188189

189-
Now that you have `.cargo/config` setup to let's test it and use `cargo run` to
190-
start the debug session:
190+
Now that you have `../.cargo/config.toml` setup let's test it using `cargo run` to
191+
start the debug session.
192+
193+
> Note the `--target thumbv7em-none-eabihf` defines which architecture
194+
> to build and run. In our `../.cargo/config.toml` file we have
195+
> `target = "thumbv7em-none-eabihf"` so it is actually not necessary
196+
> to specify `--target` we do it here just so you know that parameters on
197+
> the command line can be used and they override those in `config.toml` files
198+
199+
```
200+
cargo run --target thumbv7em-none-eabihf
201+
```
202+
Results in:
191203
```
192204
~/embedded-discovery/src/05-led-roulette
193205
$ cargo run --target thumbv7em-none-eabihf
194206
Finished dev [unoptimized + debuginfo] target(s) in 0.01s
195207
Running `arm-none-eabi-gdb -q ~/embedded-discovery/target/thumbv7em-none-eabihf/debug/led-roulette`
196208
Reading symbols from ~/embedded-discovery/target/thumbv7em-none-eabihf/debug/led-roulette...
209+
```
197210

211+
Now issue the `target remote :3333` to connect to the OpenOCD server
212+
and connect to the F3:
213+
```
198214
(gdb) target remote :3333
199215
Remote debugging using :3333
200216
0x00000000 in ?? ()
201-
202-
(gdb)
203217
```
204218

205-
Bravo, you'll be making additional changes to `.cargo/config` in future
206-
sections to make building and debugging easier.
207-
208-
> **Note** the default `.cargo/config` in every chapter assumes
209-
> the debugger is `arm-none-eabi-gdb`. So the first thing you should
210-
> do when you start a new chapter is edit `.cargo/config`!
219+
Bravo, we will be modifying `../.cargo/config.toml` in future. **But**, since
220+
this file is shared with all of the chapters those changes should be made with
221+
that in mind. If you want or we need to make changes that only pertain to
222+
a particular chapter then create a `.cargo/config.toml` local to that chapter
223+
directory.
211224

212225
## Flash the device
213226

src/05-led-roulette/the-challenge.md

Lines changed: 70 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,42 +26,88 @@ entering the same commands at the beginning. We can use a `.gdb` file to execute
2626
right after GDB is started. This way you can save yourself the effort of having to enter them
2727
manually on each GDB session.
2828

29-
Using an editor create `openocd.gdb` in the root of the Cargo project, right next to the `Cargo.toml`:
29+
As it turns out we've already created `../openocd.gdb` and you can see it's doing
30+
pretty much what we did in the previous section plus a few other commands. Look at
31+
the comments for additional information:
3032

3133
``` console
32-
nano openocd.gdb
33-
```
34-
35-
And add the following text:
36-
37-
``` text
34+
$ cat ../openocd.gdb
35+
# Connect to gdb remote server
3836
target remote :3333
37+
38+
# Load will flash the code
3939
load
40+
41+
# Eanble demangling asm names on disassembly
42+
set print asm-demangle on
43+
44+
# Enable pretty printing
45+
set print pretty on
46+
47+
# Disable style sources as the default colors can be hard to read
48+
set style sources off
49+
50+
# Initialize monitoring so iprintln! macro output
51+
# is sent from the itm port to itm.txt
52+
monitor tpiu config internal itm.txt uart off 8000000
53+
54+
# Turn on the itm port
55+
monitor itm port 0 on
56+
57+
# Set a breakpoint at main, aka entry
4058
break main
59+
60+
# Set a breakpiont at DefaultHandler
61+
break DefaultHandler
62+
63+
# Set a breakpiont at HardFault
64+
break HardFault
65+
66+
# Continue running and unill we hit the main breakpoint
4167
continue
68+
69+
# Step from the trampoline code in entry into main
4270
step
4371

4472
```
4573

46-
Next modify the `.cargo/config` file to execute openocd.gdb and we'll
47-
also add a `[build]` section with `thumbv7em-none-eabihf` so we don't
48-
have to specify the `--target` when using `cargo build` or `cargo run`:
49-
74+
Now we need to modify the `../.cargo/config.toml` file to execute `../openocd.gdb`
5075
``` console
51-
nano .cargo/config
76+
nano ../openocd.gdb
5277
```
5378

54-
Replacing the contents with the text below. This adds `-x openocd.gdb` to
55-
the `runner =` line and appends `[build]` and `target = "thumbv7em-none-eabihf` lines:
79+
Edit your `runner` command ` -x ../openocd.gdb`.
80+
Assuming you're using `arm-none-eabi-gdb` the diff is:
81+
``` diff
82+
~/embedded-discovery/src/05-led-roulette
83+
$ git diff ../.cargo/config.toml
84+
diff --git a/src/.cargo/config.toml b/src/.cargo/config.toml
85+
index ddff17f..02ac952 100644
86+
--- a/src/.cargo/config.toml
87+
+++ b/src/.cargo/config.toml
88+
@@ -1,5 +1,5 @@
89+
[target.thumbv7em-none-eabihf]
90+
-runner = "arm-none-eabi-gdb -q"
91+
+runner = "arm-none-eabi-gdb -q -x ../openocd.gdb"
92+
# runner = "gdb-multiarch -q"
93+
# runner = "gdb -q"
94+
rustflags = [
95+
```
96+
97+
And the full contents of `../.cargo/config.toml`, again
98+
assuming `arm-none-eabi-gdb`, is:
5699
``` toml
57100
[target.thumbv7em-none-eabihf]
58-
runner = "arm-none-eabi-gdb -q -x openocd.gdb"
101+
runner = "arm-none-eabi-gdb -q -x ../openocd.gdb"
102+
# runner = "gdb-multiarch -q"
103+
# runner = "gdb -q"
59104
rustflags = [
60105
"-C", "link-arg=-Tlink.x",
61106
]
62107

63108
[build]
64109
target = "thumbv7em-none-eabihf"
110+
65111
```
66112

67113
With that in place, you can now use a simple `cargo run` command which will build
@@ -95,3 +141,12 @@ Breakpoint 1, led_roulette::__cortex_m_rt_main_trampoline ()
95141
led_roulette::__cortex_m_rt_main () at ~/embedded-discovery/src/05-led-roulette/src/main.rs:9
96142
9 let (mut delay, mut leds): (Delay, LedArray) = aux5::init();
97143
```
144+
145+
## Fork the discovery book
146+
147+
If you haven't already ready, it's probably a good idea to fork
148+
the [embedded discovery book](https://github.com/rust-embedded/discovery) so you
149+
can save your changes in your own branch of your fork. We suggest creating
150+
your own branch and leaving the `master` branch alone so the `master` branch
151+
of your fork can stay in sync with the upstream repo. Also, it allows you to
152+
more easily create PR's and improve this book, **thank you in advance**!

src/05-led-roulette/the-led-and-delay-abstractions.md

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,25 +38,19 @@ fn main() -> ! {
3838
```
3939

4040
Now build it:
41-
4241
``` console
43-
cargo build --target thumbv7em-none-eabihf
42+
cargo build
4443
```
4544

4645
> **NOTE** It's possible to forget to rebuild the program *before* starting a GDB session; this
4746
> omission can lead to very confusing debug sessions. To avoid this problem you can call just `cargo run`
4847
> instead of `cargo build`; `cargo run`. The `cargo run` command will build *and* start a debug
4948
> session ensuring you never forget to recompile your program.
5049
51-
Now, we'll repeat the flashing procedure that we did in the previous section:
52-
50+
Now we'll run and repeat the flashing procedure as we did in the previous section
51+
but with the new program. I'll let you type in the `cargo run`, *this will get easier shortly* :)
5352
``` console
54-
cargo run --target thumbv7em-none-eabihf
55-
```
56-
57-
Which results in something like:
58-
``` console
59-
$ cargo run --target thumbv7em-none-eabihf
53+
$ cargo run
6054
Finished dev [unoptimized + debuginfo] target(s) in 0.01s
6155
Running `arm-none-eabi-gdb -q ~/embedded-discovery/target/thumbv7em-none-eabihf/debug/led-roulette`
6256
Reading symbols from ~/embedded-discovery/target/thumbv7em-none-eabihf/debug/led-roulette...
@@ -93,7 +87,6 @@ led_roulette::__cortex_m_rt_main () at ~/embedded-discovery/src/05-led-roulette/
9387

9488
OK. Let's step through the code. This time, we'll use the `next` command instead of `step`. The
9589
difference is that the `next` command will step *over* function calls instead of going inside them.
96-
9790
```
9891
(gdb) next
9992
11 let half_period = 500_u16;

src/06-hello-world/.cargo/config

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
[target.thumbv7em-none-eabihf]
22
runner = "arm-none-eabi-gdb -q -x openocd.gdb"
3-
rustflags = [
4-
"-C", "link-arg=-Tlink.x",
5-
]
3+
#rustflags = [
4+
# "-C", "link-arg=-Tlink.x",
5+
#]
66

77
[build]
88
target = "thumbv7em-none-eabihf"

src/07-registers/.cargo/config

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
[target.thumbv7em-none-eabihf]
22
runner = "arm-none-eabi-gdb -q -x openocd.gdb"
3-
rustflags = [
4-
"-C", "link-arg=-Tlink.x",
5-
]
3+
#rustflags = [
4+
# "-C", "link-arg=-Tlink.x",
5+
#]
66

77
[build]
8-
target = "thumbv7em-none-eabihf"
8+
target = "thumbv7em-none-eabihf"

src/08-leds-again/.cargo/config

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
[target.thumbv7em-none-eabihf]
22
runner = "arm-none-eabi-gdb -q -x openocd.gdb"
3-
rustflags = [
4-
"-C", "link-arg=-Tlink.x",
5-
]
3+
#rustflags = [
4+
# "-C", "link-arg=-Tlink.x",
5+
#]
66

77
[build]
8-
target = "thumbv7em-none-eabihf"
8+
target = "thumbv7em-none-eabihf"
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
[target.thumbv7em-none-eabihf]
22
runner = "arm-none-eabi-gdb -q -x openocd.gdb"
3-
rustflags = [
4-
"-C", "link-arg=-Tlink.x",
5-
]
3+
#rustflags = [
4+
# "-C", "link-arg=-Tlink.x",
5+
#]
66

77
[build]
8-
target = "thumbv7em-none-eabihf"
8+
target = "thumbv7em-none-eabihf"

0 commit comments

Comments
 (0)