Skip to content

Commit 18617af

Browse files
committed
Update docs and some fixes
1 parent e50b74c commit 18617af

File tree

6 files changed

+338
-276
lines changed

6 files changed

+338
-276
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ jobs:
1111
runs-on: ubuntu-latest
1212
strategy:
1313
matrix:
14+
# Devices only differ in memory size are commented out
1415
mcu:
1516
- stm32f301x6
1617
- stm32f318x8

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1515
- Enable better GPIO internal resistor configuration ([#189])
1616
- Support for GPIO output slew rate configuration ([#189])
1717
- Support for GPIO interrupts ([#189])
18+
- `ld` feature, which enables the memory.x generation ([#216])
1819

1920
### Changed
2021

@@ -26,6 +27,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
2627

2728
- Delay based on systick no longer panics ([#203]) for to high values
2829
and support longer delays ([#208])
30+
- Long delay during ADC initialization ([#217])
2931

3032
### Breaking Changes
3133

@@ -35,6 +37,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
3537
If the supplied frequency cannot be converted to `Hertz` the code
3638
will `panic`. This will occur if the supplied `Megahertz` frequency
3739
cannot fit into `u32::MAX` when converting to `Hertz` ([#192])
40+
- You always required to select a sub-target for target chips ([#216])
3841

3942
```rust
4043
// The supplied frequencies must be in `MHz`.
@@ -313,6 +316,8 @@ let clocks = rcc
313316
[defmt]: https://github.com/knurling-rs/defmt
314317
[filter]: https://defmt.ferrous-systems.com/filtering.html
315318

319+
[#217]: https://github.com/stm32-rs/stm32f3xx-hal/pull/217
320+
[#216]: https://github.com/stm32-rs/stm32f3xx-hal/pull/216
316321
[#211]: https://github.com/stm32-rs/stm32f3xx-hal/pull/211
317322
[#210]: https://github.com/stm32-rs/stm32f3xx-hal/pull/210
318323
[#208]: https://github.com/stm32-rs/stm32f3xx-hal/pull/208

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ gpio-f303e = []
9090
gpio-f333 = []
9191
gpio-f373 = []
9292

93-
# Any changes here should be mirrored in README.md, src/lib.rs, and
93+
# Any changes here should be mirrored in README.md, build.rs, src/lib.rs, and
9494
# .github/workflows/ci.yml.
9595
stm32f301 = ["svd-f301", "direct-call-deprecated"]
9696
stm32f301x6 = ["stm32f301", "mem-6", "gpio-f302", "device-selected"]

README.md

Lines changed: 62 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -38,20 +38,50 @@ Almost all of the implementation was shamelessly adapted from the
3838
[`stm32f30x-hal`]: https://github.com/japaric/stm32f30x-hal
3939
[`embedded-hal`]: https://github.com/japaric/embedded-hal
4040

41-
## [Changelog](CHANGELOG.md)
41+
## Getting Started
42+
43+
### Adding stm32f3xx-hal and other dependencies
44+
45+
Cargo.toml:
46+
47+
```toml
48+
[dependencies]
49+
cortex-m = "0.7.2"
50+
cortex-m-rt = { version = "0.6.13", features = ["device"] }
51+
# Panic behaviour, see https://crates.io/keywords/panic-impl for alternatives
52+
panic-halt = "0.2.0"
53+
# Replace stm32f303xc with your target chip, see next section for more info
54+
stm32f3xx-hal = { version = "0.7.0", features = ["ld", "rt", "stm32f303xc"] }
55+
```
56+
57+
We also need to tell Rust about target architecture and how to link our
58+
executable by creating `.cargo/config`.
4259

43-
## Selecting the right chip
60+
.cargo/config:
61+
62+
```toml
63+
[target.thumbv7em-none-eabihf]
64+
rustflags = [
65+
"-C", "link-arg=-Tlink.x",
66+
]
67+
68+
[build]
69+
target = "thumbv7em-none-eabihf"
70+
```
71+
72+
### Selecting the right chip
4473

4574
This crate requires you to specify your target chip as a feature.
4675

47-
*Example: The STM32F3Discovery board has a STM32F303VCT6 chip.
48-
So you want to expand your call to `cargo` with `--features stm32f303xc`.*
76+
*Example: The STM32F3Discovery board has a STM32F303VCT6 chip according to the
77+
[user manual][]. So you need to specify `stm32f303xc` in your `Cargo.toml`
78+
(note that VC → xc).*
4979

50-
### Possible chips
80+
#### Possible chips
5181

5282
[comment]: # (Any changes here should be mirrored in src/lib.rs)
5383

54-
Note: `x` denotes any character in [a-z]
84+
Please select one of the following (`x` denotes any character in [a-z]):
5585

5686
* stm32f301x6
5787
* stm32f301x8
@@ -79,51 +109,48 @@ Note: `x` denotes any character in [a-z]
79109
* stm32f334x6
80110
* stm32f334x8
81111

82-
### Background
112+
#### Background
83113

84114
For some of the stm32f3xx chips there are sub-variants that differ in
85115
functionality, peripheral use and hence 'under the hood' implementation. To
86116
allow the full use of all peripherals on certain subvariants without
87117
allowing for code that just doesn't run on other sub-vairants, they are
88118
distinct features that need to be specified.
89119

90-
As this crate is still under fundamental development, expect more
91-
sub-variants replacing the plain variants in the future as we are
92-
implementing more stuff. It is not desired to allow the plain variants to
93-
be used as this leads to confusion.
94-
*Example: the stm32f303xc has a gpio_e bank while the stm32f303x6 does
95-
not. Hence we don't want to expose the gpoio_e bank on all stm32f303 (i.e.
96-
when specifying the feature stm32f303) albeit a stm32f303xc user would
97-
expect it to do so.*
98-
99-
### Detailed steps to select the right chip
100-
101-
1. Get the full name of the chip you are using from your datasheet, user manual
102-
or other source.
120+
[user manual]: https://www.st.com/content/ccc/resource/technical/document/user_manual/8a/56/97/63/8d/56/41/73/DM00063382.pdf/files/DM00063382.pdf/jcr:content/translations/en.DM00063382.pdf
103121

104-
_Example_:
122+
### Basic Usage
105123

106-
We want to use the STM32F3Discovery kit.
107-
The [Usermanual][] tells us it's using a STM32F303VC chip.
124+
```rust
125+
#![no_std]
126+
#![no_main]
108127

109-
2. Find your chip as a feature in the list above.
128+
use cortex_m::asm;
129+
use cortex_m_rt::entry;
130+
use panic_halt as _;
131+
use stm32f3xx_hal::{self as hal, pac, prelude::*};
110132

111-
_Example_:
133+
#[entry]
134+
fn main() -> ! {
135+
let dp = pac::Peripherals::take().unwrap();
112136

113-
Looking for the right feature for our STM32F303VC chip we first find
114-
`stm32f301xb`. This is the wrong chip, as we're not looking for `f301` but
115-
for `f303`.
137+
let mut rcc = dp.RCC.constrain();
138+
let mut gpioe = dp.GPIOE.split(&mut rcc.ahb);
116139

117-
Looking further we find `stm32f303xc`. This matches STM32F303VC
118-
(note that VC → xc).
140+
let mut led = gpioe
141+
.pe13
142+
.into_push_pull_output(&mut gpioe.moder, &mut gpioe.otyper);
119143

120-
3. Add the chip name as a feature to your cargo call.
121-
122-
_Example_:
144+
loop {
145+
led.toggle().unwrap();
146+
asm::delay(8_000_000);
147+
}
148+
}
149+
```
123150

124-
Using the STM32F303VC chip we run `cargo check --features stm32f303xc`.
151+
See the [examples folder](examples) for more example programs.
125152

126-
[Usermanual]: https://www.st.com/content/ccc/resource/technical/document/user_manual/8a/56/97/63/8d/56/41/73/DM00063382.pdf/files/DM00063382.pdf/jcr:content/translations/en.DM00063382.pdf
153+
## [Changelog](CHANGELOG.md)
127154

128155
## Minimum Supported Rust Version (MSRV)
129156

build.rs

Lines changed: 104 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,124 @@
11
use std::{env, fs::File, io::prelude::*, path::PathBuf};
22

33
fn main() {
4+
check_device_feature();
45
if cfg!(feature = "ld") {
56
gen_memory_x();
67
}
78
println!("cargo:rerun-if-changed=build.rs");
89
}
910

11+
/// Check device feature selection
12+
fn check_device_feature() {
13+
if !cfg!(feature = "device-selected") {
14+
if cfg!(feature = "direct-call-deprecated") {
15+
eprintln!(
16+
"The feature you selected is deprecated, because it was split up into sub-devices.
17+
18+
Example: The STM32F3Discovery board has a STM32F303VCT6 chip.
19+
You probably used to use `stm32f303` but now functionalities for the sub-device were added.
20+
In this case replace it with `stm32f303xc` to make your code build again.
21+
22+
For more information, see \x1b]8;;https://github.com/stm32-rs/stm32f3xx-hal#selecting-the-right-chip\x1b\\README -> Selecting the right chip\x1b]8;;\x1b\\."
23+
);
24+
} else {
25+
eprintln!(
26+
"This crate requires you to specify your target chip as a feature.
27+
28+
Please select one of the following (`x` denotes any character in [a-z]):
29+
30+
stm32f301x6 stm32f301x8
31+
stm32f318x8
32+
stm32f302x6 stm32f302x8 stm32f302xb stm32f302xc stm32f302xd stm32f302xe
33+
stm32f303x6 stm32f303x8 stm32f303xb stm32f303xc stm32f303xd stm32f303xe
34+
stm32f328x8
35+
stm32f358xc
36+
stm32f398xe
37+
stm32f373x8 stm32f373xb stm32f373xc
38+
stm32f378xc
39+
stm32f334x4 stm32f334x6 stm32f334x8
40+
41+
Example: The STM32F3Discovery board has a STM32F303VCT6 chip.
42+
So you need to specify stm32f303xc in your Cargo.toml (note that VC → xc).
43+
44+
For more information, see \x1b]8;;https://github.com/stm32-rs/stm32f3xx-hal#selecting-the-right-chip\x1b\\README -> Selecting the right chip\x1b]8;;\x1b\\."
45+
);
46+
}
47+
std::process::exit(1);
48+
}
49+
}
50+
51+
/// Generate `memory.x` for selected device
52+
///
53+
/// Available RAM/CCMRAM/FLASH value is extracted from RM0313/RM0316/RM0364/RM0365/RM0366
1054
fn gen_memory_x() {
11-
#![allow(clippy::unneeded_wildcard_pattern)]
55+
enum Mem {
56+
_4,
57+
_6,
58+
_8,
59+
B,
60+
C,
61+
D,
62+
E,
63+
}
1264

13-
let mem_cfg_set = (
14-
cfg!(feature = "mem-4"),
15-
cfg!(feature = "mem-6"),
16-
cfg!(feature = "mem-8"),
17-
cfg!(feature = "mem-b"),
18-
cfg!(feature = "mem-c"),
19-
cfg!(feature = "mem-d"),
20-
cfg!(feature = "mem-e"),
21-
);
22-
let flash = match mem_cfg_set {
23-
(true, ..) => 16,
24-
(_, true, ..) => 32,
25-
(_, _, true, ..) => 64,
26-
(.., true, _, _, _) => 128,
27-
(.., _, true, _, _) => 256,
28-
(.., _, _, true, _) => 384,
29-
(.., _, _, _, true) => 512,
30-
_ => unreachable!(),
65+
let mem = if cfg!(feature = "mem-4") {
66+
Mem::_4
67+
} else if cfg!(feature = "mem-6") {
68+
Mem::_6
69+
} else if cfg!(feature = "mem-8") {
70+
Mem::_8
71+
} else if cfg!(feature = "mem-b") {
72+
Mem::B
73+
} else if cfg!(feature = "mem-c") {
74+
Mem::C
75+
} else if cfg!(feature = "mem-d") {
76+
Mem::D
77+
} else if cfg!(feature = "mem-e") {
78+
Mem::E
79+
} else {
80+
eprintln!(
81+
"Memory size unknown.
82+
This may be due to incorrect feature configuration in Cargo.toml or stm32f3xx-hal's internal issue."
83+
);
84+
std::process::exit(1);
85+
};
86+
87+
let flash = match mem {
88+
Mem::_4 => 16,
89+
Mem::_6 => 32,
90+
Mem::_8 => 64,
91+
Mem::B => 128,
92+
Mem::C => 256,
93+
Mem::D => 384,
94+
Mem::E => 512,
3195
};
3296
let ccmram = if cfg!(feature = "svd-f303") || cfg!(feature = "svd-f3x4") {
33-
match mem_cfg_set {
34-
(true, ..) | (_, true, ..) | (_, _, true, ..) => 4,
35-
(.., true, _, _, _) | (.., _, true, _, _) => 8,
36-
(.., _, _, true, _) | (.., _, _, _, true) => 16,
37-
_ => unreachable!(),
97+
match mem {
98+
Mem::_4 | Mem::_6 | Mem::_8 => 4,
99+
Mem::B | Mem::C => 8,
100+
Mem::D | Mem::E => 16,
38101
}
39102
} else {
40103
0
41104
};
42-
let ram = match mem_cfg_set {
43-
(true, ..) | (_, true, ..) | (_, _, true, ..) => 16,
44-
(.., true, _, _, _) if cfg!(feature = "svd-f373") => 24,
45-
(.., true, _, _, _) if cfg!(feature = "svd-f302") => 32,
46-
(.., _, true, _, _) if cfg!(feature = "svd-f373") => 32,
47-
(.., true, _, _, _) if cfg!(feature = "svd-f303") => 40,
48-
(.., _, true, _, _) if cfg!(feature = "svd-f302") => 40,
49-
(.., _, true, _, _) if cfg!(feature = "svd-f303") => 48,
50-
(.., _, _, true, _) | (.., _, _, _, true) if cfg!(feature = "svd-f302") => 64,
51-
(.., _, _, true, _) | (.., _, _, _, true) if cfg!(feature = "svd-f303") => 80,
52-
_ => unreachable!(),
105+
let ram = match mem {
106+
Mem::_4 | Mem::_6 | Mem::_8 => 16,
107+
Mem::B if cfg!(feature = "svd-f373") => 24,
108+
Mem::B if cfg!(feature = "svd-f302") => 32,
109+
Mem::C if cfg!(feature = "svd-f373") => 32,
110+
Mem::B if cfg!(feature = "svd-f303") => 40,
111+
Mem::C if cfg!(feature = "svd-f302") => 40,
112+
Mem::C if cfg!(feature = "svd-f303") => 48,
113+
Mem::D | Mem::E if cfg!(feature = "svd-f302") => 64,
114+
Mem::D | Mem::E if cfg!(feature = "svd-f303") => 80,
115+
_ => {
116+
eprintln!(
117+
"Memory size unknown.
118+
This may be due to incorrect feature configuration in Cargo.toml or stm32f3xx-hal's internal issue."
119+
);
120+
std::process::exit(1);
121+
}
53122
} - ccmram;
54123

55124
let out_dir = PathBuf::from(env::var_os("OUT_DIR").unwrap());

0 commit comments

Comments
 (0)