Skip to content

Commit 457afa5

Browse files
datdenkiknietJohannes Draaijer
authored andcommitted
Make all examples use defmt
Minimize required features for examples Update CI matrix so that it checks all examples for one of each family
1 parent b5a8015 commit 457afa5

File tree

10 files changed

+162
-196
lines changed

10 files changed

+162
-196
lines changed

.github/workflows/build.yml

Lines changed: 16 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ env:
1111
CARGO_TERM_COLOR: always
1212

1313
jobs:
14-
# Run cargo fmt --check, includes macros/
1514
style:
1615
name: style
1716
runs-on: ubuntu-20.04
@@ -117,70 +116,25 @@ jobs:
117116
runs-on: ubuntu-20.04
118117
strategy:
119118
matrix:
120-
target:
121-
- thumbv7m-none-eabi
122119
toolchain:
123120
- stable
124-
steps:
125-
- name: Checkout
126-
uses: actions/checkout@v3
127-
128-
- name: Install Rust ${{ matrix.toolchain }} with target (${{ matrix.target }})
129-
uses: actions-rs/toolchain@v1
130-
with:
131-
toolchain: ${{ matrix.toolchain }}
132-
target: ${{ matrix.target }}
133-
override: true
134-
135-
- name: cargo build f4 example pktgen
136-
uses: actions-rs/cargo@v1
137-
with:
138-
use-cross: false
139-
command: build
140-
args: --release --target=${{ matrix.target }} --example pktgen --features stm32f429
141-
142-
- name: cargo build f4 example ip
143-
uses: actions-rs/cargo@v1
144-
with:
145-
use-cross: false
146-
command: build
147-
args: --release --target=${{ matrix.target }} --example ip --features stm32f429,smoltcp-phy,log,smoltcp/socket-tcp,smoltcp/socket-icmp,smoltcp/log,smoltcp/verbose
148-
149-
- name: cargo build f4 example arp
150-
uses: actions-rs/cargo@v1
151-
with:
152-
use-cross: false
153-
command: build
154-
args: --release --target=${{ matrix.target }} --example arp --features stm32f407
155-
156-
- name: cargo build f4 example arp-smoltcp
157-
uses: actions-rs/cargo@v1
158-
with:
159-
use-cross: false
160-
command: build
161-
args: --release --target=${{ matrix.target }} --example arp-smoltcp --features stm32f407,smoltcp-phy,smoltcp/socket-icmp
162-
163-
- name: cargo build f1 example ip
164-
uses: actions-rs/cargo@v1
165-
with:
166-
use-cross: false
167-
command: build
168-
args: --release --target=${{ matrix.target }} --example ip-f107 --features stm32f107,smoltcp-phy,log,smoltcp/socket-tcp,smoltcp/socket-icmp,smoltcp/log,smoltcp/verbose
169-
170-
rtic-echo-example:
171-
name: rtic-echo example
172-
runs-on: ubuntu-20.04
173-
strategy:
174-
matrix:
175121
target:
176122
- thumbv7m-none-eabi
177-
toolchain:
178-
- stable
179-
features:
123+
mcu:
180124
- stm32f107
181-
- stm32f407
182-
- stm32f765
183-
- nucleo-pins,stm32f765
125+
- stm32f429
126+
- stm32f745
127+
example:
128+
- name: arp-smoltcp
129+
features: defmt,smoltcp-phy,smoltcp/socket-raw
130+
- name: arp
131+
features: defmt
132+
- name: ip
133+
features: defmt,smoltcp-phy,smoltcp/defmt,smoltcp/socket-tcp
134+
- name: pktgen
135+
features: defmt
136+
- name: rtic-echo
137+
features: rtic-echo-example
184138
steps:
185139
- name: Checkout
186140
uses: actions/checkout@v3
@@ -192,12 +146,12 @@ jobs:
192146
target: ${{ matrix.target }}
193147
override: true
194148

195-
- name: build rtic-echo-example with features ${{ matrix.features }}
149+
- name: Build example ${{ matrix.example.name }} for ${{ matrix.mcu }}
196150
uses: actions-rs/cargo@v1
197151
with:
198152
use-cross: false
199153
command: build
200-
args: --release --target=${{ matrix.target }} --example rtic-echo --features rtic-echo-example,${{ matrix.features }}
154+
args: --release --target=${{ matrix.target }} --example ${{ matrix.example.name}} --features ${{ matrix.mcu }},${{ matrix.example.features }}
201155

202156
# Refs: https://github.com/rust-lang/crater/blob/9ab6f9697c901c4a44025cf0a39b73ad5b37d198/.github/workflows/bors.yml#L125-L149
203157
#
@@ -211,7 +165,6 @@ jobs:
211165
- build
212166
- test
213167
- examples
214-
- rtic-echo-example
215168
runs-on: ubuntu-20.04
216169
steps:
217170
- name: Mark the job as a success

Cargo.toml

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,12 @@ stm32f779 = ["stm32f7xx-hal/stm32f779", "device-selected", "fence"]
6363
smoltcp-phy = ["smoltcp"]
6464

6565
# Example features
66-
nucleo-pins = [ ]
66+
example-nucleo-pins = [ ]
6767
rtic-echo-example = [ "ieee802_3_miim", "defmt", "smoltcp-phy", "smoltcp/defmt", "smoltcp/medium-ethernet", "smoltcp/socket-tcp" ]
6868

6969
[dev-dependencies]
7070
cortex-m = "0.7"
7171
cortex-m-rt = "0.7"
72-
panic-itm = "0.4"
73-
cortex-m-semihosting = "0.3"
7472
fugit = "0.3"
7573
cortex-m-rtic = "1.0"
7674
defmt-rtt = "0.3"
@@ -79,22 +77,21 @@ systick-monotonic = "1.0"
7977

8078
[[example]]
8179
name = "pktgen"
82-
required-features = [ ]
80+
required-features = [ "defmt" ]
8381

8482
[[example]]
8583
name = "ip"
8684
required-features = [
87-
"smoltcp-phy", "log", "smoltcp/socket-tcp", "smoltcp/socket-icmp",
88-
"smoltcp/log", "smoltcp/verbose"
89-
]
85+
"defmt", "smoltcp-phy", "smoltcp/defmt", "smoltcp/socket-tcp"
86+
]
9087

9188
[[example]]
9289
name = "arp"
93-
required-features = [ ]
90+
required-features = [ "defmt" ]
9491

9592
[[example]]
9693
name = "arp-smoltcp"
97-
required-features = ["smoltcp-phy", "smoltcp/socket-icmp"]
94+
required-features = ["defmt", "smoltcp-phy", "smoltcp/socket-raw"]
9895

9996
[[example]]
10097
name = "rtic-echo"

README.md

Lines changed: 65 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,6 @@
1010

1111
Please send pull requests.
1212

13-
## Building Examples
14-
15-
```bash
16-
cargo build --example="pktgen" --features="stm32f429"
17-
cargo build --example="ip" --features="stm32f429 smoltcp-phy log smoltcp/socket-tcp smoltcp/socket-icmp smoltcp/log smoltcp/verbose"
18-
cargo build --example="ip-f107" --features="stm32f107 smoltcp-phy log smoltcp/socket-tcp smoltcp/socket-icmp smoltcp/log smoltcp/verbose"
19-
```
20-
2113
## Usage
2214

2315
Add one of the following to the `[dependencies]` section in your `Cargo.toml` (with the correct MCU specified):
@@ -89,6 +81,70 @@ fn main() {
8981
}
9082
```
9183

92-
## [smoltcp] support
84+
85+
## `smoltcp` support
9386

9487
Use feature-flag `smoltcp-phy`
88+
89+
## Examples
90+
91+
The examples should run and compile on any MCU that has an 802.3 compatible PHY connected to the default RMII pins.
92+
93+
The examples use `defmt` and `defmt_rtt` for logging, and `panic_probe` over `defmt_rtt` for printing panic backtraces.
94+
95+
To run or build them, the following steps should be taken:
96+
97+
1. Determine the correct compilation target for the MCU that you're using. For `stm32f107`, it is `thumbv7m-none-eabi`. For all others, it is `thumbv7em-none-eabihf`.
98+
2. Determine the MCU feature necessary for running on your MCU, e.g. `stm32f745`.
99+
3. Determine the Additional required features (see section below) necessary to build the example.
100+
4. Follow the rest of the instructions in the "Building examples" or "Running examples" subsections.
101+
102+
### Additional required features
103+
Besides the feature selecting the correct MCU to be used when building and/or running an example, the following
104+
additional features are required:
105+
106+
| Example | Additional required features |
107+
| ------------- | ---------------------------------------------------- |
108+
| `arp-smoltcp` | `defmt,smoltcp-phy,smoltcp/socket-raw` |
109+
| `arp` | `defmt` |
110+
| `ip` | `defmt,smoltcp-phy,smoltcp/defmt,smoltcp/socket-tcp` |
111+
| `pktgen` | `defmt` |
112+
| `rtic-echo` | `rtic-echo-example` |
113+
114+
115+
### Building examples
116+
Run the following command:
117+
```bash
118+
cargo build --release --example <example> --features <MCU feature>,<additional required features> --target <MCU compilation target>
119+
```
120+
121+
For example, if we wish to build the `arp-smoltcp` example for an `stm32f429`, we should run the following command:
122+
123+
```bash
124+
cargo build --release --example arp-smoltcp --features stm32f429,smoltcp-phy,smoltcp/socket-tcp,smoltcp/socket-icmp --target thumbv7em-none-eabihf
125+
```
126+
127+
### Running examples
128+
Install `probe-run` (`cargo install probe-run --version '~0.3'`), and ensure that `probe-run` can
129+
attach to your MCU.
130+
131+
Find the correct value for `PROBE_RUN_CHIP` for your MCU from the list provided by `probe-run --list-chips`.
132+
133+
Ensure that `probe-run` can attach to your MCU
134+
135+
Then, run the following command:
136+
```bash
137+
DEFMT_LOG=info PROBE_RUN_CHIP=<probe-run chip> cargo run --release --example <example> --features <MCU feature>,<additional required features> --target <MCU compilation target>
138+
```
139+
140+
For example, if we wish to run the `rtic-echo` example on an `STM32F107RCT6`, we should run the following command:
141+
142+
```bash
143+
DEFMT_LOG=info PROBE_RUN_CHIP=STM32F107RC cargo run --release --example rtic-echo --features stm32f107,rtic-echo-example --target thumbv7m-none-eabi
144+
```
145+
146+
### Pins
147+
148+
For the `stm32-nucleo-f746zg` board, the `example-nucleo-pins` feature can be activated.
149+
150+
If the usage of different pins is required, the types and `setup_pins` function in `examples/common.rs` should be edited. If the pin configuration is for a `nucleo` board, a PR with the changes would be appreciated.

examples/arp-smoltcp.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
1-
//! cargo build --example arp-smoltcp --features=<MCU>,smoltcp-phy,smoltcp/socket-tcp,smoltcp/socket-icmp
2-
//! This example should work on any MCU with an 802.3 compatible, on-by-default PHY connected to the default
3-
//! ethernet pins of that MCU.
1+
//! For build and run instructions, see [`README.md`](../README.md#examples)
2+
//!
43
//! With Wireshark, you can see the ARP packets, which should look like this:
54
//! No. Time Source Destination Protocol Length Info
65
//! 1 0.000000000 Cetia_ad:be:ef Broadcast ARP 60 Who has 10.0.0.2? Tell 10.0.0.10
76
87
#![no_std]
98
#![no_main]
109

11-
extern crate panic_itm;
10+
use defmt_rtt as _;
11+
use panic_probe as _;
1212

1313
use core::cell::RefCell;
1414
use core::default::Default;
1515

1616
use cortex_m::asm;
1717
use cortex_m::interrupt::Mutex;
1818
use cortex_m_rt::{entry, exception};
19-
use cortex_m_semihosting::hprintln;
2019
use smoltcp::wire::{
2120
ArpOperation, ArpPacket, ArpRepr, EthernetAddress, EthernetFrame, EthernetProtocol,
2221
EthernetRepr, Ipv4Address,
@@ -43,7 +42,8 @@ fn main() -> ! {
4342

4443
setup_systick(&mut cp.SYST);
4544

46-
hprintln!("Enabling ethernet...").unwrap();
45+
defmt::info!("Enabling ethernet...");
46+
4747
let (eth_pins, mdio, mdc) = common::setup_pins(gpio);
4848

4949
let mut rx_ring: [RingEntry<_>; 16] = Default::default();
@@ -69,9 +69,9 @@ fn main() -> ! {
6969

7070
if link_up != last_link_up {
7171
if link_up {
72-
hprintln!("Ethernet: link detected").unwrap();
72+
defmt::info!("Ethernet: link detected");
7373
} else {
74-
hprintln!("Ethernet: no link detected").unwrap();
74+
defmt::info!("Ethernet: no link detected");
7575
}
7676
last_link_up = link_up;
7777
}
@@ -110,12 +110,12 @@ fn main() -> ! {
110110

111111
match r {
112112
Ok(()) => {
113-
hprintln!("ARP-smoltcp sent").unwrap();
113+
defmt::info!("ARP-smoltcp sent");
114114
}
115-
Err(TxError::WouldBlock) => hprintln!("ARP failed").unwrap(),
115+
Err(TxError::WouldBlock) => defmt::info!("ARP failed"),
116116
}
117117
} else {
118-
hprintln!("Down").unwrap();
118+
defmt::info!("Down");
119119
}
120120

121121
cortex_m::interrupt::free(|cs| {

examples/arp.rs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
//! cargo build --example arp --features=<MCU>
2-
//! This example should work on any MCU with an 802.3 compatible, on-by-default PHY connected to the
3-
//! default ethernet pins of that MCU.
1+
//! For build and run instructions, see [`README.md`](../README.md#examples)
2+
//!
43
//! With Wireshark, you can see the ARP packets, which should look like this:
54
//! No. Time Source Destination Protocol Length Info
65
//! 1 0.000000000 Cetia_ad:be:ef Broadcast ARP 60 Who has 10.0.0.2? Tell 10.0.0.10
76
87
#![no_std]
98
#![no_main]
109

11-
extern crate panic_itm;
10+
use defmt_rtt as _;
11+
use panic_probe as _;
1212

1313
use core::cell::RefCell;
1414
use core::default::Default;
@@ -21,8 +21,6 @@ use stm32_eth::{
2121
stm32::{interrupt, CorePeripherals, Peripherals, SYST},
2222
};
2323

24-
use cortex_m_semihosting::hprintln;
25-
2624
pub mod common;
2725

2826
use stm32_eth::{RingEntry, TxError};
@@ -41,7 +39,7 @@ fn main() -> ! {
4139

4240
setup_systick(&mut cp.SYST);
4341

44-
hprintln!("Enabling ethernet...").unwrap();
42+
defmt::info!("Enabling ethernet...");
4543

4644
let (eth_pins, mdio, mdc) = common::setup_pins(gpio);
4745

@@ -68,9 +66,9 @@ fn main() -> ! {
6866

6967
if link_up != last_link_up {
7068
if link_up {
71-
hprintln!("Ethernet: link detected").unwrap();
69+
defmt::info!("Ethernet: link detected");
7270
} else {
73-
hprintln!("Ethernet: no link detected").unwrap();
71+
defmt::info!("Ethernet: no link detected");
7472
}
7573
last_link_up = link_up;
7674
}
@@ -109,12 +107,12 @@ fn main() -> ! {
109107

110108
match r {
111109
Ok(()) => {
112-
hprintln!("ARP sent").unwrap();
110+
defmt::info!("ARP sent");
113111
}
114-
Err(TxError::WouldBlock) => hprintln!("ARP failed").unwrap(),
112+
Err(TxError::WouldBlock) => defmt::info!("ARP failed"),
115113
}
116114
} else {
117-
hprintln!("Down").unwrap();
115+
defmt::info!("Down");
118116
}
119117

120118
cortex_m::interrupt::free(|cs| {

0 commit comments

Comments
 (0)