Skip to content

Commit b283bc8

Browse files
authored
Merge pull request #281 from Sh3Rm4n/adc-instance
Adc instance
2 parents 31f34d2 + a8e4afd commit b283bc8

File tree

23 files changed

+3709
-587
lines changed

23 files changed

+3709
-587
lines changed

.cargo/config.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,6 @@ rustflags = [
2020

2121
[build]
2222
target = "thumbv7em-none-eabihf"
23+
24+
[env]
25+
DEFMT_LOG = "trace"

CHANGELOG.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,18 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
2121
- `RccBus`, `Enable`, `Reset` traits and implementations for peripherals ([#299])
2222
- Support cortex-m-rt `v0.7.0` but still allow `v0.6.13` ([#283])
2323
- Make timer `InterruptTypes` fields public to be useful. ([#304])
24+
- Add support for the internal **signature** peripheral ([#281])
25+
- Add common derives to `Toggle`. ([#281])
26+
- Add `is_interrupt_configured` and `configured_interrupts` function to
27+
`serial::Serial` and `timer::Timer`. ([#281])
2428

2529
### Fixed
2630

2731
- Fix `can` support. Can would not build for `stm32f302x6` for example.
2832
Also support `can` for every chip other than `stm32f301` and `stm32f318`.
2933
([#283])
34+
- Fix wrong ADC votlage regulator startup time calculation effecting
35+
calibration. ([#281])
3036

3137
### Breaking Changes
3238

@@ -42,6 +48,34 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
4248
the dependencies where using the MIT / Apache 2.0 dual licensing already,
4349
which resulted into the situtation, that BSD 0-clause was not affectivly
4450
0-clause as MIT conditions had to be met anyways. (🧂 IANAL). ([#309])
51+
- Renamed `Serial::raw_read` to `Serial::read_data_register`. ([#281])
52+
- Timer error type `AlreadyCancled` is no longer public constructable. ([#281])
53+
- Seal `FlashExt` and `RccExt` traits. These are no longer implementable by
54+
a user of this crate. ([#281])
55+
- Move ADC from a macro to a generic implementation, meaning that
56+
it is possible to obtain an ADC instance via `Adc::new` instead of
57+
`Adc::adc1`. ([#281])
58+
- Remove `adc::ClockMode` and the clock configuration of the ADC.
59+
- Clock configuration effects the common ADC which inturn controls e.g. ADC1
60+
and ADC2, which the old API did not resemble.
61+
- The API provides no clock configuration for ADC peripherals yet, but
62+
correctly acts upon changes done through the `pac`. ([#281])
63+
- Major rework of the ADC implementation: ([#281])
64+
- Add `CommonAdc` support.
65+
- Add internal sensor peripheral support like `TemeperaturSensor` and similar.
66+
- Lift restriction of the ADC implementation for `stm32f303`, though
67+
`stm32f373` is still not supported.
68+
- Enable manual configuration of the Adc peripheral of most important features
69+
- `ConversionMode`
70+
- `Sequence` length
71+
- `OverrunMode`
72+
- etc.
73+
- Leverage type states to:
74+
- Allow a `Disabled` ADC, which can be manually calibrated.
75+
- Allow a `OneShot` ADC implementation through `into_oneshot` with an
76+
optimal configuration for the `OneShot` embedded-hal trait.
77+
- Support every possible ADC channel.
78+
- Add interrupt support.
4579

4680
## [v0.8.2] - 2021-12-14
4781

@@ -522,6 +556,7 @@ let clocks = rcc
522556
[#291]: https://github.com/stm32-rs/stm32f3xx-hal/pull/291
523557
[#283]: https://github.com/stm32-rs/stm32f3xx-hal/pull/283
524558
[#282]: https://github.com/stm32-rs/stm32f3xx-hal/pull/282
559+
[#281]: https://github.com/stm32-rs/stm32f3xx-hal/pull/281
525560
[#278]: https://github.com/stm32-rs/stm32f3xx-hal/pull/278
526561
[#277]: https://github.com/stm32-rs/stm32f3xx-hal/pull/277
527562
[#273]: https://github.com/stm32-rs/stm32f3xx-hal/pull/273

Cargo.toml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ bare-metal = "1.0.0"
5656

5757
[dev-dependencies]
5858
cortex-m-semihosting = "0.3.7"
59+
defmt = "0.3.0"
5960
defmt-rtt = "0.3.0"
6061
defmt-test = "0.3.0"
6162
panic-probe = "0.3.0"
@@ -148,7 +149,8 @@ defmt-error = ["defmt"]
148149
# cargo build/run
149150
[profile.dev]
150151
debug = 2
151-
lto = true
152+
# Disabled until https://github.com/knurling-rs/defmt/issues/649 is fixed
153+
# lto = true
152154

153155
# cargo test
154156
[profile.test]
@@ -158,7 +160,8 @@ opt-level = 3 # <-
158160
# cargo build/run --release
159161
[profile.release]
160162
debug = 2
161-
lto = true
163+
# Disabled until https://github.com/knurling-rs/defmt/issues/649 is fixed
164+
# lto = true
162165
opt-level = "s"
163166

164167
# cargo test --release

codegen/tools/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
gen.py
2+
__pycache__

codegen/tools/NOTES.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# ADC Channels
2+
3+
The ADC channel approach did not use the current codegen code, as
4+
it was more convenient to query the xml database with a commandline tool,
5+
to get an idea how it was structured.
6+
7+
This code **should** be integrated into the codegen rust source in the future.
8+
But to make this happen codegen has to be refactored, as it is heavily
9+
tailored to the `IP` resolution right now.
10+
11+
What really should happen though, is to integrate all those features into
12+
[**`cube-parse`**](https://github.com/stm32-rs/cube-parse/)
13+
and only have a shim of processing code, which tailors the output to our
14+
macros.

codegen/tools/adc_channel.py

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
#!/usr/bin/env python3
2+
3+
"""
4+
This script generates the ADC channel! macro with arguents for all pins
5+
of all chip subfamilies.
6+
7+
The strategy is, to get the maximum possible support for the most
8+
finest currently possible chip subfamily distinction.
9+
10+
That means, that some impls for some chips are more than the actual
11+
chip is actually capable of.
12+
"""
13+
14+
import re
15+
16+
import subprocess
17+
18+
# Hacky way to create a dict variable with a bash script
19+
# which is then saved to a file which then can be directly imported.
20+
channel = subprocess.check_output(['bash', 'adc_channel.sh'])
21+
with open("gen.py", "wb") as f:
22+
f.write(channel)
23+
24+
import gen
25+
26+
CHIP_SELECT = {
27+
"stm32f301x6": r".*STM32F301.(6|8|\(6-8\)).*\.xml",
28+
"stm32f302x6": r".*STM32F302.(6|8|\(6-8\)).*\.xml",
29+
"stm32f303x6": r".*STM32F303.(6|8|\(6-8\)).*\.xml",
30+
"stm32f302xb": r".*STM32F302.(B|C|\(B-C\)|\(8-B-C\)).*\.xml",
31+
"stm32f303xb": r".*STM32F303.(B|C|\(B-C\)|\(8-B-C\)).*\.xml",
32+
"stm32f302xd": r".*STM32F302.(D|E|\(D-E\)).*\.xml",
33+
"stm32f303xd": r".*STM32F303.(D|E).*\.xml",
34+
"stm32f328": r".*STM32F328.*\.xml",
35+
"stm32f358": r".*STM32F358.*\.xml",
36+
"stm32f398": r".*STM32F398.*\.xml",
37+
"stm32f373": r".*STM32F37(3|8).*\.xml",
38+
"stm32f334": r".*STM32F334.*\.xml",
39+
}
40+
41+
42+
def main():
43+
chip_post = {}
44+
45+
# Split ADC into adc and channel
46+
for chip_name, pin_list in gen.CHANNELS.items():
47+
pins = []
48+
for pin in pin_list:
49+
adc_list = []
50+
for i in [1, 2]:
51+
try:
52+
(adc, channel) = pin[i].split("_")
53+
except IndexError:
54+
continue
55+
adc_list.append((adc, int(channel)))
56+
pins.append((pin[0], adc_list))
57+
chip_post[chip_name] = pins
58+
59+
chip_results = {}
60+
61+
# Group into chip categories and select chip with max entries
62+
# Choosing the maximum list is a compromise right now:
63+
# 1. The stm32cubemx database is much more fine-grained about
64+
# the types of chips than our current feature is
65+
# 2. Only allowing the minimal subset of all chip-subfamilies
66+
# is can be pretty annoying and would severly restrict the
67+
# channel implementation for the stm32f303xc and st32f303xd
68+
# for example.
69+
# 3. This goes a little against the current impl strategy,
70+
# that is, allowing more than a chip might be possible to use.
71+
# But the alternative of finer feature selection is a pretty
72+
# big change and gives no real benefit, other than annoying
73+
# the user.
74+
for chip, regex in CHIP_SELECT.items():
75+
grouping = []
76+
for chip_name, _ in chip_post.items():
77+
if re.search(regex, chip_name):
78+
grouping.append(chip_post[chip_name])
79+
grouping = list(max(grouping))
80+
chip_results[chip] = grouping
81+
82+
# Print the resulintg dictionary in a format, compatible with the
83+
# `channel!` macro.
84+
for type, result in chip_results.items():
85+
type_variant_map = {
86+
'x6': 'x8',
87+
'xb': 'xc',
88+
'xd': 'xe',
89+
}
90+
for key, value in type_variant_map.items():
91+
if type[-2:] == key:
92+
type_variant = type[:-2] + value
93+
print(f'}} else if #[cfg(any(feature = "{type}", feature = "{type_variant}"))] {{')
94+
break
95+
else:
96+
print(f'}} else if #[cfg(feature = "{type}")] {{')
97+
print(" channel!([")
98+
for elem in result:
99+
line = f" ({', '.join([str(e) for e in elem])})"
100+
line = str(elem).replace("'", "")
101+
print(" " + line + ",")
102+
print(" ]);")
103+
104+
105+
if __name__ == "__main__":
106+
main()

codegen/tools/adc_channel.sh

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/usr/bin/env bash
2+
# Using xq to query the stm32cubemx database for adc channels
3+
# xq beeing a wrapper of jq converting xml to a json representation internally.
4+
#
5+
# This is the source of the CHANNELS dict in adc_channel.py
6+
# TODO(Sh3Rm4n): This should probably be rewritten in the codegen part.
7+
# Though therefor the codegen should be refactored.
8+
9+
set -euo pipefail
10+
11+
echo "CHANNELS = {"
12+
for xml in /opt/stm32cubemx/db/mcu/STM32F3*.xml; do
13+
echo '"'$xml'": ['
14+
xq '.Mcu.Pin[]
15+
| select(.Signal[]?."@Name"? | startswith("ADC"))
16+
| [."@Name", (.Signal[]? | select(."@Name" | startswith("ADC")))."@Name"]
17+
| @csv' --raw-output < $xml \
18+
| rg -v EXTI \
19+
| rg -v OSC32 \
20+
| rg -v 'jq:' \
21+
| sed 's/IN//g' \
22+
| sort -u \
23+
| awk '{print "("$0"),"}'
24+
echo '],'
25+
done
26+
echo "}"

0 commit comments

Comments
 (0)