Compare commits
31 Commits
c60f8db3c8
...
v1.1.0
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
416fdb0e7f | ||
|
|
a514098d00 | ||
|
|
ec72b2dc8f | ||
|
|
8c95b3343d | ||
|
|
ddef3db5fb | ||
|
|
6334c95e05 | ||
|
|
92d69e875a | ||
|
|
6dae8c727a | ||
|
|
cb99c9b89b | ||
|
|
c7d790f474 | ||
|
|
a93965eccf | ||
|
|
22b3b2307d | ||
|
|
5399983dfb | ||
|
|
34b10426c2 | ||
|
|
89ad23b3a0 | ||
|
|
d0fd820434 | ||
|
|
8cbd43bc3a | ||
|
|
86032fc821 | ||
|
|
6992b46ed5 | ||
|
|
7bd4d6acac | ||
|
|
9e90cc7295 | ||
|
|
8d8d3b3341 | ||
|
|
bdb46a89c1 | ||
|
|
07c0e9501a | ||
|
|
9feffc8152 | ||
|
|
3ba1a3afae | ||
|
|
cfc31e428e | ||
|
|
fae99273c2 | ||
|
|
adc4d6f29e | ||
|
|
88f2ec3ae9 | ||
|
|
1b4ddcaf6c |
15
2sem_i2c/.cargo/config.toml
Normal file
15
2sem_i2c/.cargo/config.toml
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
[build]
|
||||||
|
target = "thumbv8m.main-none-eabihf"
|
||||||
|
|
||||||
|
[target.thumbv8m.main-none-eabihf]
|
||||||
|
runner = "probe-rs run --chip STM32U575ZITxQ"
|
||||||
|
|
||||||
|
rustflags = [
|
||||||
|
"-C", "linker=rust-lld",
|
||||||
|
"-C", "link-arg=-Tlink.x",
|
||||||
|
"-C", "link-arg=-Tdefmt.x",
|
||||||
|
"-C", "link-arg=--nmagic",
|
||||||
|
]
|
||||||
|
|
||||||
|
[package.metadata.cargo-flash]
|
||||||
|
chip = "STM32U575ZIT"
|
||||||
1
2sem_i2c/.gitignore
vendored
Normal file
1
2sem_i2c/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
/target
|
||||||
1088
2sem_i2c/Cargo.lock
generated
Normal file
1088
2sem_i2c/Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
30
2sem_i2c/Cargo.toml
Normal file
30
2sem_i2c/Cargo.toml
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
[package]
|
||||||
|
authors = ["Priec <filippriec@gmail.com>"]
|
||||||
|
name = "hal_rng"
|
||||||
|
edition = "2024"
|
||||||
|
version = "0.1.0"
|
||||||
|
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
cortex-m = { version = "0.7.7", features = ["inline-asm", "critical-section-single-core"] }
|
||||||
|
cortex-m-rt = "0.7.5"
|
||||||
|
panic-halt = "1.0.0"
|
||||||
|
|
||||||
|
embassy-executor = { version = "0.9.1", features = ["arch-cortex-m", "executor-thread"] }
|
||||||
|
embassy-futures = "0.1.2"
|
||||||
|
embassy-stm32 = { version = "0.4.0", features = ["unstable-pac", "stm32u575zi", "time-driver-any", "memory-x", "defmt"] }
|
||||||
|
embassy-sync = "0.7.2"
|
||||||
|
embassy-time = { version = "0.5.0", features = ["tick-hz-32_768"] }
|
||||||
|
embassy-usb = "0.5.1"
|
||||||
|
|
||||||
|
embedded-hal = "1.0.0"
|
||||||
|
embedded-graphics = "0.8.1"
|
||||||
|
heapless = { version = "0.9.1", default-features = false }
|
||||||
|
micromath = "2.1.0"
|
||||||
|
tinybmp = "0.6.0"
|
||||||
|
panic-probe = { version = "1.0.0", features = ["defmt"] }
|
||||||
|
defmt-rtt = "1.1.0"
|
||||||
|
defmt = "1.0.1"
|
||||||
|
static_cell = "2.1.1"
|
||||||
|
embedded-io-async = "0.6.0"
|
||||||
|
embedded-io = "0.6.1"
|
||||||
201
2sem_i2c/LICENSE-APACHE
Normal file
201
2sem_i2c/LICENSE-APACHE
Normal file
@@ -0,0 +1,201 @@
|
|||||||
|
Apache License
|
||||||
|
Version 2.0, January 2004
|
||||||
|
http://www.apache.org/licenses/
|
||||||
|
|
||||||
|
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||||
|
|
||||||
|
1. Definitions.
|
||||||
|
|
||||||
|
"License" shall mean the terms and conditions for use, reproduction,
|
||||||
|
and distribution as defined by Sections 1 through 9 of this document.
|
||||||
|
|
||||||
|
"Licensor" shall mean the copyright owner or entity authorized by
|
||||||
|
the copyright owner that is granting the License.
|
||||||
|
|
||||||
|
"Legal Entity" shall mean the union of the acting entity and all
|
||||||
|
other entities that control, are controlled by, or are under common
|
||||||
|
control with that entity. For the purposes of this definition,
|
||||||
|
"control" means (i) the power, direct or indirect, to cause the
|
||||||
|
direction or management of such entity, whether by contract or
|
||||||
|
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||||
|
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||||
|
|
||||||
|
"You" (or "Your") shall mean an individual or Legal Entity
|
||||||
|
exercising permissions granted by this License.
|
||||||
|
|
||||||
|
"Source" form shall mean the preferred form for making modifications,
|
||||||
|
including but not limited to software source code, documentation
|
||||||
|
source, and configuration files.
|
||||||
|
|
||||||
|
"Object" form shall mean any form resulting from mechanical
|
||||||
|
transformation or translation of a Source form, including but
|
||||||
|
not limited to compiled object code, generated documentation,
|
||||||
|
and conversions to other media types.
|
||||||
|
|
||||||
|
"Work" shall mean the work of authorship, whether in Source or
|
||||||
|
Object form, made available under the License, as indicated by a
|
||||||
|
copyright notice that is included in or attached to the work
|
||||||
|
(an example is provided in the Appendix below).
|
||||||
|
|
||||||
|
"Derivative Works" shall mean any work, whether in Source or Object
|
||||||
|
form, that is based on (or derived from) the Work and for which the
|
||||||
|
editorial revisions, annotations, elaborations, or other modifications
|
||||||
|
represent, as a whole, an original work of authorship. For the purposes
|
||||||
|
of this License, Derivative Works shall not include works that remain
|
||||||
|
separable from, or merely link (or bind by name) to the interfaces of,
|
||||||
|
the Work and Derivative Works thereof.
|
||||||
|
|
||||||
|
"Contribution" shall mean any work of authorship, including
|
||||||
|
the original version of the Work and any modifications or additions
|
||||||
|
to that Work or Derivative Works thereof, that is intentionally
|
||||||
|
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||||
|
or by an individual or Legal Entity authorized to submit on behalf of
|
||||||
|
the copyright owner. For the purposes of this definition, "submitted"
|
||||||
|
means any form of electronic, verbal, or written communication sent
|
||||||
|
to the Licensor or its representatives, including but not limited to
|
||||||
|
communication on electronic mailing lists, source code control systems,
|
||||||
|
and issue tracking systems that are managed by, or on behalf of, the
|
||||||
|
Licensor for the purpose of discussing and improving the Work, but
|
||||||
|
excluding communication that is conspicuously marked or otherwise
|
||||||
|
designated in writing by the copyright owner as "Not a Contribution."
|
||||||
|
|
||||||
|
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||||
|
on behalf of whom a Contribution has been received by Licensor and
|
||||||
|
subsequently incorporated within the Work.
|
||||||
|
|
||||||
|
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||||
|
this License, each Contributor hereby grants to You a perpetual,
|
||||||
|
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||||
|
copyright license to reproduce, prepare Derivative Works of,
|
||||||
|
publicly display, publicly perform, sublicense, and distribute the
|
||||||
|
Work and such Derivative Works in Source or Object form.
|
||||||
|
|
||||||
|
3. Grant of Patent License. Subject to the terms and conditions of
|
||||||
|
this License, each Contributor hereby grants to You a perpetual,
|
||||||
|
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||||
|
(except as stated in this section) patent license to make, have made,
|
||||||
|
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||||
|
where such license applies only to those patent claims licensable
|
||||||
|
by such Contributor that are necessarily infringed by their
|
||||||
|
Contribution(s) alone or by combination of their Contribution(s)
|
||||||
|
with the Work to which such Contribution(s) was submitted. If You
|
||||||
|
institute patent litigation against any entity (including a
|
||||||
|
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||||
|
or a Contribution incorporated within the Work constitutes direct
|
||||||
|
or contributory patent infringement, then any patent licenses
|
||||||
|
granted to You under this License for that Work shall terminate
|
||||||
|
as of the date such litigation is filed.
|
||||||
|
|
||||||
|
4. Redistribution. You may reproduce and distribute copies of the
|
||||||
|
Work or Derivative Works thereof in any medium, with or without
|
||||||
|
modifications, and in Source or Object form, provided that You
|
||||||
|
meet the following conditions:
|
||||||
|
|
||||||
|
(a) You must give any other recipients of the Work or
|
||||||
|
Derivative Works a copy of this License; and
|
||||||
|
|
||||||
|
(b) You must cause any modified files to carry prominent notices
|
||||||
|
stating that You changed the files; and
|
||||||
|
|
||||||
|
(c) You must retain, in the Source form of any Derivative Works
|
||||||
|
that You distribute, all copyright, patent, trademark, and
|
||||||
|
attribution notices from the Source form of the Work,
|
||||||
|
excluding those notices that do not pertain to any part of
|
||||||
|
the Derivative Works; and
|
||||||
|
|
||||||
|
(d) If the Work includes a "NOTICE" text file as part of its
|
||||||
|
distribution, then any Derivative Works that You distribute must
|
||||||
|
include a readable copy of the attribution notices contained
|
||||||
|
within such NOTICE file, excluding those notices that do not
|
||||||
|
pertain to any part of the Derivative Works, in at least one
|
||||||
|
of the following places: within a NOTICE text file distributed
|
||||||
|
as part of the Derivative Works; within the Source form or
|
||||||
|
documentation, if provided along with the Derivative Works; or,
|
||||||
|
within a display generated by the Derivative Works, if and
|
||||||
|
wherever such third-party notices normally appear. The contents
|
||||||
|
of the NOTICE file are for informational purposes only and
|
||||||
|
do not modify the License. You may add Your own attribution
|
||||||
|
notices within Derivative Works that You distribute, alongside
|
||||||
|
or as an addendum to the NOTICE text from the Work, provided
|
||||||
|
that such additional attribution notices cannot be construed
|
||||||
|
as modifying the License.
|
||||||
|
|
||||||
|
You may add Your own copyright statement to Your modifications and
|
||||||
|
may provide additional or different license terms and conditions
|
||||||
|
for use, reproduction, or distribution of Your modifications, or
|
||||||
|
for any such Derivative Works as a whole, provided Your use,
|
||||||
|
reproduction, and distribution of the Work otherwise complies with
|
||||||
|
the conditions stated in this License.
|
||||||
|
|
||||||
|
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||||
|
any Contribution intentionally submitted for inclusion in the Work
|
||||||
|
by You to the Licensor shall be under the terms and conditions of
|
||||||
|
this License, without any additional terms or conditions.
|
||||||
|
Notwithstanding the above, nothing herein shall supersede or modify
|
||||||
|
the terms of any separate license agreement you may have executed
|
||||||
|
with Licensor regarding such Contributions.
|
||||||
|
|
||||||
|
6. Trademarks. This License does not grant permission to use the trade
|
||||||
|
names, trademarks, service marks, or product names of the Licensor,
|
||||||
|
except as required for reasonable and customary use in describing the
|
||||||
|
origin of the Work and reproducing the content of the NOTICE file.
|
||||||
|
|
||||||
|
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||||
|
agreed to in writing, Licensor provides the Work (and each
|
||||||
|
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||||
|
implied, including, without limitation, any warranties or conditions
|
||||||
|
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||||
|
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||||
|
appropriateness of using or redistributing the Work and assume any
|
||||||
|
risks associated with Your exercise of permissions under this License.
|
||||||
|
|
||||||
|
8. Limitation of Liability. In no event and under no legal theory,
|
||||||
|
whether in tort (including negligence), contract, or otherwise,
|
||||||
|
unless required by applicable law (such as deliberate and grossly
|
||||||
|
negligent acts) or agreed to in writing, shall any Contributor be
|
||||||
|
liable to You for damages, including any direct, indirect, special,
|
||||||
|
incidental, or consequential damages of any character arising as a
|
||||||
|
result of this License or out of the use or inability to use the
|
||||||
|
Work (including but not limited to damages for loss of goodwill,
|
||||||
|
work stoppage, computer failure or malfunction, or any and all
|
||||||
|
other commercial damages or losses), even if such Contributor
|
||||||
|
has been advised of the possibility of such damages.
|
||||||
|
|
||||||
|
9. Accepting Warranty or Additional Liability. While redistributing
|
||||||
|
the Work or Derivative Works thereof, You may choose to offer,
|
||||||
|
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||||
|
or other liability obligations and/or rights consistent with this
|
||||||
|
License. However, in accepting such obligations, You may act only
|
||||||
|
on Your own behalf and on Your sole responsibility, not on behalf
|
||||||
|
of any other Contributor, and only if You agree to indemnify,
|
||||||
|
defend, and hold each Contributor harmless for any liability
|
||||||
|
incurred by, or claims asserted against, such Contributor by reason
|
||||||
|
of your accepting any such warranty or additional liability.
|
||||||
|
|
||||||
|
END OF TERMS AND CONDITIONS
|
||||||
|
|
||||||
|
APPENDIX: How to apply the Apache License to your work.
|
||||||
|
|
||||||
|
To apply the Apache License to your work, attach the following
|
||||||
|
boilerplate notice, with the fields enclosed by brackets "[]"
|
||||||
|
replaced with your own identifying information. (Don't include
|
||||||
|
the brackets!) The text should be enclosed in the appropriate
|
||||||
|
comment syntax for the file format. We also recommend that a
|
||||||
|
file or class name and description of purpose be included on the
|
||||||
|
same "printed page" as the copyright notice for easier
|
||||||
|
identification within third-party archives.
|
||||||
|
|
||||||
|
Copyright [yyyy] [name of copyright owner]
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
23
2sem_i2c/LICENSE-MIT
Normal file
23
2sem_i2c/LICENSE-MIT
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
Permission is hereby granted, free of charge, to any
|
||||||
|
person obtaining a copy of this software and associated
|
||||||
|
documentation files (the "Software"), to deal in the
|
||||||
|
Software without restriction, including without
|
||||||
|
limitation the rights to use, copy, modify, merge,
|
||||||
|
publish, distribute, sublicense, and/or sell copies of
|
||||||
|
the Software, and to permit persons to whom the Software
|
||||||
|
is furnished to do so, subject to the following
|
||||||
|
conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice
|
||||||
|
shall be included in all copies or substantial portions
|
||||||
|
of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
|
||||||
|
ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
|
||||||
|
TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
|
||||||
|
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
|
||||||
|
SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
||||||
|
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||||
|
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
|
||||||
|
IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||||
|
DEALINGS IN THE SOFTWARE.
|
||||||
23
2sem_i2c/Makefile
Normal file
23
2sem_i2c/Makefile
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
TARGET = thumbv8m.main-none-eabihf
|
||||||
|
CHIP = STM32U575ZI
|
||||||
|
BIN = stm32u5-blinky
|
||||||
|
MODE ?= release
|
||||||
|
TARGET_DIR = target/$(TARGET)/$(MODE)
|
||||||
|
ELF = $(TARGET_DIR)/$(BIN)
|
||||||
|
PROBE = probe-rs
|
||||||
|
|
||||||
|
.PHONY: all build flash clean empty
|
||||||
|
|
||||||
|
all: build
|
||||||
|
|
||||||
|
build:
|
||||||
|
cargo build --$(MODE)
|
||||||
|
|
||||||
|
flash: build
|
||||||
|
$(PROBE) run --chip $(CHIP) $(ELF)
|
||||||
|
|
||||||
|
empty:
|
||||||
|
$(PROBE) erase --chip $(CHIP)
|
||||||
|
|
||||||
|
clean:
|
||||||
|
cargo clean
|
||||||
232
2sem_i2c/README.md
Normal file
232
2sem_i2c/README.md
Normal file
@@ -0,0 +1,232 @@
|
|||||||
|
# `app-template`
|
||||||
|
|
||||||
|
> Quickly set up a [`probe-rs`] + [`defmt`] + [`flip-link`] embedded project
|
||||||
|
|
||||||
|
[`probe-rs`]: https://crates.io/crates/probe-rs
|
||||||
|
[`defmt`]: https://github.com/knurling-rs/defmt
|
||||||
|
[`flip-link`]: https://github.com/knurling-rs/flip-link
|
||||||
|
|
||||||
|
## Dependencies
|
||||||
|
|
||||||
|
### 1. `flip-link`:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
cargo install flip-link
|
||||||
|
```
|
||||||
|
|
||||||
|
### 2. `probe-rs`:
|
||||||
|
|
||||||
|
Install probe-rs by following the instructions at <https://probe.rs/docs/getting-started/installation/>.
|
||||||
|
|
||||||
|
### 3. [`cargo-generate`]:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
cargo install cargo-generate
|
||||||
|
```
|
||||||
|
|
||||||
|
[`cargo-generate`]: https://crates.io/crates/cargo-generate
|
||||||
|
|
||||||
|
> *Note:* You can also just clone this repository instead of using `cargo-generate`, but this involves additional manual adjustments.
|
||||||
|
|
||||||
|
## Setup
|
||||||
|
|
||||||
|
### 1. Initialize the project template
|
||||||
|
|
||||||
|
```bash
|
||||||
|
cargo generate \
|
||||||
|
--git https://github.com/knurling-rs/app-template \
|
||||||
|
--branch main \
|
||||||
|
--name my-app
|
||||||
|
```
|
||||||
|
|
||||||
|
If you look into your new `my-app` folder, you'll find that there are a few `TODO`s in the files marking the properties you need to set.
|
||||||
|
|
||||||
|
Let's walk through them together now.
|
||||||
|
|
||||||
|
### 2. Set `probe-rs` chip
|
||||||
|
|
||||||
|
Pick a chip from ` probe-rs chip list` and enter it into `.cargo/config.toml`.
|
||||||
|
|
||||||
|
If, for example, you have a nRF52840 Development Kit as used in one of [our exercises], replace `{{chip}}` with `nRF52840_xxAA`.
|
||||||
|
|
||||||
|
[our workshops]: https://rust-exercises.ferrous-systems.com
|
||||||
|
|
||||||
|
```diff
|
||||||
|
# .cargo/config.toml
|
||||||
|
-runner = ["probe-rs", "run", "--chip", "$CHIP", "--log-format=oneline"]
|
||||||
|
+runner = ["probe-rs", "run", "--chip", "nRF52840_xxAA", "--log-format=oneline"]
|
||||||
|
```
|
||||||
|
|
||||||
|
### 3. Adjust the compilation target
|
||||||
|
|
||||||
|
In `.cargo/config.toml`, pick the right compilation target for your board.
|
||||||
|
|
||||||
|
```diff
|
||||||
|
# .cargo/config.toml
|
||||||
|
[build]
|
||||||
|
-target = "thumbv6m-none-eabi" # Cortex-M0 and Cortex-M0+
|
||||||
|
-# target = "thumbv7m-none-eabi" # Cortex-M3
|
||||||
|
-# target = "thumbv7em-none-eabi" # Cortex-M4 and Cortex-M7 (no FPU)
|
||||||
|
-# target = "thumbv7em-none-eabihf" # Cortex-M4F and Cortex-M7F (with FPU)
|
||||||
|
+target = "thumbv7em-none-eabihf" # Cortex-M4F (with FPU)
|
||||||
|
```
|
||||||
|
|
||||||
|
Add the target with `rustup`.
|
||||||
|
|
||||||
|
```bash
|
||||||
|
rustup target add thumbv7em-none-eabihf
|
||||||
|
```
|
||||||
|
|
||||||
|
### 4. Add a HAL as a dependency
|
||||||
|
|
||||||
|
In `Cargo.toml`, list the Hardware Abstraction Layer (HAL) for your board as a dependency.
|
||||||
|
|
||||||
|
For the nRF52840 you'll want to use the [`nrf52840-hal`].
|
||||||
|
|
||||||
|
[`nrf52840-hal`]: https://crates.io/crates/nrf52840-hal
|
||||||
|
|
||||||
|
```diff
|
||||||
|
# Cargo.toml
|
||||||
|
[dependencies]
|
||||||
|
-# some-hal = "1.2.3"
|
||||||
|
+nrf52840-hal = "0.14.0"
|
||||||
|
```
|
||||||
|
|
||||||
|
⚠️ Note for RP2040 users ⚠️
|
||||||
|
|
||||||
|
You will need to not just specify the `rp-hal` HAL, but a BSP (board support crate) which includes a second stage bootloader. Please find a list of available BSPs [here](https://github.com/rp-rs/rp-hal-boards#packages).
|
||||||
|
|
||||||
|
### 5. Import your HAL
|
||||||
|
|
||||||
|
Now that you have selected a HAL, fix the HAL import in `src/lib.rs`
|
||||||
|
|
||||||
|
```diff
|
||||||
|
// my-app/src/lib.rs
|
||||||
|
-// use some_hal as _; // memory layout
|
||||||
|
+use nrf52840_hal as _; // memory layout
|
||||||
|
```
|
||||||
|
|
||||||
|
### (6. Get a linker script)
|
||||||
|
|
||||||
|
Some HAL crates require that you manually copy over a file called `memory.x` from the HAL to the root of your project. For nrf52840-hal, this is done automatically so no action is needed. For other HAL crates, see their documentation on where to find an example file.
|
||||||
|
|
||||||
|
The `memory.x` file should look something like:
|
||||||
|
|
||||||
|
```text
|
||||||
|
MEMORY
|
||||||
|
{
|
||||||
|
FLASH : ORIGIN = 0x00000000, LENGTH = 1024K
|
||||||
|
RAM : ORIGIN = 0x20000000, LENGTH = 256K
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
The `memory.x` file is included in the `cortex-m-rt` linker script `link.x`, and so `link.x` is the one you should tell `rustc` to use (see the `.cargo/config.toml` file where we do that).
|
||||||
|
|
||||||
|
### 7. Run!
|
||||||
|
|
||||||
|
You are now all set to `cargo-run` your first `defmt`-powered application!
|
||||||
|
There are some examples in the `src/bin` directory.
|
||||||
|
|
||||||
|
Start by `cargo run`-ning `my-app/src/bin/hello.rs`:
|
||||||
|
|
||||||
|
```console
|
||||||
|
$ # `rb` is an alias for `run --bin`
|
||||||
|
$ cargo rb hello
|
||||||
|
Finished `dev` profile [optimized + debuginfo] target(s) in 0.01s
|
||||||
|
Running `probe-rs run --chip nrf52840_xxaa --log-format=oneline target/thumbv6m-none-eabi/debug/hello`
|
||||||
|
Erasing ✔ 100% [####################] 8.00 KiB @ 15.79 KiB/s (took 1s)
|
||||||
|
Programming ✔ 100% [####################] 8.00 KiB @ 13.19 KiB/s (took 1s) Finished in 1.11s
|
||||||
|
Hello, world!
|
||||||
|
|
||||||
|
$ echo $?
|
||||||
|
0
|
||||||
|
```
|
||||||
|
|
||||||
|
If you're running out of memory (`flip-link` bails with an overflow error), you can decrease the size of the device memory buffer by setting the `DEFMT_RTT_BUFFER_SIZE` environment variable. The default value is 1024 bytes, and powers of two should be used for optimal performance:
|
||||||
|
|
||||||
|
```console
|
||||||
|
$ DEFMT_RTT_BUFFER_SIZE=64 cargo rb hello
|
||||||
|
```
|
||||||
|
|
||||||
|
### (8. Set `rust-analyzer.linkedProjects`)
|
||||||
|
|
||||||
|
If you are using [rust-analyzer] with VS Code for IDE-like features you can add following configuration to your `.vscode/settings.json` to make it work transparently across workspaces. Find the details of this option in the [RA docs].
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"rust-analyzer.linkedProjects": [
|
||||||
|
"Cargo.toml",
|
||||||
|
"firmware/Cargo.toml",
|
||||||
|
]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
[RA docs]: https://rust-analyzer.github.io/manual.html#configuration
|
||||||
|
[rust-analyzer]: https://rust-analyzer.github.io/
|
||||||
|
|
||||||
|
## Running tests
|
||||||
|
|
||||||
|
The template comes configured for running unit tests and integration tests on the target.
|
||||||
|
|
||||||
|
Unit tests reside in the library crate and can test private API; the initial set of unit tests are in `src/lib.rs`.
|
||||||
|
`cargo test --lib` will run those unit tests.
|
||||||
|
|
||||||
|
```console
|
||||||
|
$ cargo test --lib
|
||||||
|
Compiling example v0.1.0 (./knurling-rs/example)
|
||||||
|
Finished `test` profile [optimized + debuginfo] target(s) in 0.15s
|
||||||
|
Running unittests src/lib.rs (target/thumbv6m-none-eabi/debug/deps/example-2b0d0e25d141bf57)
|
||||||
|
Erasing ✔ 100% [####################] 8.00 KiB @ 15.99 KiB/s (took 1s)
|
||||||
|
Programming ✔ 100% [####################] 8.00 KiB @ 13.33 KiB/s (took 1s) Finished in 1.10s
|
||||||
|
(1/1) running `it_works`...
|
||||||
|
all tests passed!
|
||||||
|
```
|
||||||
|
|
||||||
|
Integration tests reside in the `tests` directory; the initial set of integration tests are in `tests/integration.rs`.
|
||||||
|
`cargo test --test integration` will run those integration tests.
|
||||||
|
Note that the argument of the `--test` flag must match the name of the test file in the `tests` directory.
|
||||||
|
|
||||||
|
```console
|
||||||
|
$ cargo test --test integration
|
||||||
|
Compiling example v0.1.0 (./knurling-rs/example)
|
||||||
|
Finished `test` profile [optimized + debuginfo] target(s) in 0.10s
|
||||||
|
Running tests/integration.rs (target/thumbv6m-none-eabi/debug/deps/integration-aaaff41151f6a722)
|
||||||
|
Erasing ✔ 100% [####################] 8.00 KiB @ 16.03 KiB/s (took 0s)
|
||||||
|
Programming ✔ 100% [####################] 8.00 KiB @ 13.19 KiB/s (took 1s) Finished in 1.11s
|
||||||
|
(1/1) running `it_works`...
|
||||||
|
all tests passed!
|
||||||
|
```
|
||||||
|
|
||||||
|
Note that to add a new test file to the `tests` directory you also need to add a new `[[test]]` section to `Cargo.toml`.
|
||||||
|
|
||||||
|
To run all the tests via `cargo test` the tests need to be explicitly disabled for all the existing binary targets.
|
||||||
|
See `Cargo.toml` for details on how to do this.
|
||||||
|
|
||||||
|
## Support
|
||||||
|
|
||||||
|
`app-template` is part of the [Knurling] project, [Ferrous Systems]' effort at
|
||||||
|
improving tooling used to develop for embedded systems.
|
||||||
|
|
||||||
|
If you think that our work is useful, consider sponsoring it via [GitHub
|
||||||
|
Sponsors].
|
||||||
|
|
||||||
|
## License
|
||||||
|
|
||||||
|
Licensed under either of
|
||||||
|
|
||||||
|
- Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0)
|
||||||
|
|
||||||
|
- MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)
|
||||||
|
|
||||||
|
at your option.
|
||||||
|
|
||||||
|
### Contribution
|
||||||
|
|
||||||
|
Unless you explicitly state otherwise, any contribution intentionally submitted
|
||||||
|
for inclusion in the work by you, as defined in the Apache-2.0 license, shall be
|
||||||
|
licensed as above, without any additional terms or conditions.
|
||||||
|
|
||||||
|
[Knurling]: https://knurling.ferrous-systems.com
|
||||||
|
[Ferrous Systems]: https://ferrous-systems.com/
|
||||||
|
[GitHub Sponsors]: https://github.com/sponsors/knurling-rs
|
||||||
46
2sem_i2c/src/bin/main.rs
Normal file
46
2sem_i2c/src/bin/main.rs
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
// src/bin/main.rs
|
||||||
|
#![no_std]
|
||||||
|
#![no_main]
|
||||||
|
|
||||||
|
use defmt::*;
|
||||||
|
use embassy_executor::Spawner;
|
||||||
|
use embassy_stm32::i2c::{Config, I2c};
|
||||||
|
use embassy_time::{Duration, Timer};
|
||||||
|
use {defmt_rtt as _, panic_probe as _};
|
||||||
|
|
||||||
|
#[embassy_executor::main]
|
||||||
|
async fn main(_spawner: Spawner) {
|
||||||
|
info!("tititi");
|
||||||
|
|
||||||
|
let p = embassy_stm32::init(Default::default());
|
||||||
|
|
||||||
|
let mut i2c_cfg = Config::default();
|
||||||
|
i2c_cfg.sda_pullup = true;
|
||||||
|
i2c_cfg.scl_pullup = true;
|
||||||
|
i2c_cfg.timeout = Duration::from_millis(200);
|
||||||
|
|
||||||
|
let mut i2c = I2c::new_blocking(p.I2C3, p.PC0, p.PC1, i2c_cfg);
|
||||||
|
let addr = 0x23;
|
||||||
|
|
||||||
|
if let Err(e) = i2c.blocking_write(addr, &[0x01, 0x79]) {
|
||||||
|
error!("Nepodarilo sa aktivovať CMM: {:?}", e);
|
||||||
|
}
|
||||||
|
|
||||||
|
loop {
|
||||||
|
let mut raw = [0u8; 9];
|
||||||
|
match i2c.blocking_write_read(addr, &[0x24], &mut raw) {
|
||||||
|
Ok(_) => {
|
||||||
|
let x = i32::from_be_bytes([raw[0], raw[1], raw[2], 0]) >> 8;
|
||||||
|
let y = i32::from_be_bytes([raw[3], raw[4], raw[5], 0]) >> 8;
|
||||||
|
let z = i32::from_be_bytes([raw[6], raw[7], raw[8], 0]) >> 8;
|
||||||
|
info!("X: {}, Y: {}, Z: {}", x, y, z);
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
error!("Chyba pri čítaní: {:?}", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Čítajte napríklad každých 100ms. Dáta v registroch budú vždy najaktuálnejšie dokončené meranie.
|
||||||
|
Timer::after(Duration::from_millis(100)).await;
|
||||||
|
}
|
||||||
|
}
|
||||||
1
2sem_i2c/src/lib.rs
Normal file
1
2sem_i2c/src/lib.rs
Normal file
@@ -0,0 +1 @@
|
|||||||
|
#![no_std]
|
||||||
16
2sem_i2c/tests/integration.rs
Normal file
16
2sem_i2c/tests/integration.rs
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
#![no_std]
|
||||||
|
#![no_main]
|
||||||
|
|
||||||
|
use stm32u5_blinky as _; // memory layout + panic handler
|
||||||
|
|
||||||
|
// See https://crates.io/crates/defmt-test/0.3.0 for more documentation (e.g. about the 'state'
|
||||||
|
// feature)
|
||||||
|
#[defmt_test::tests]
|
||||||
|
mod tests {
|
||||||
|
use defmt::assert;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn it_works() {
|
||||||
|
assert!(true)
|
||||||
|
}
|
||||||
|
}
|
||||||
15
2sem_sem2/.cargo/config.toml
Normal file
15
2sem_sem2/.cargo/config.toml
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
[build]
|
||||||
|
target = "thumbv8m.main-none-eabihf"
|
||||||
|
|
||||||
|
[target.thumbv8m.main-none-eabihf]
|
||||||
|
runner = "probe-rs run --chip STM32U575ZITxQ"
|
||||||
|
|
||||||
|
rustflags = [
|
||||||
|
"-C", "linker=rust-lld",
|
||||||
|
"-C", "link-arg=-Tlink.x",
|
||||||
|
"-C", "link-arg=-Tdefmt.x",
|
||||||
|
"-C", "link-arg=--nmagic",
|
||||||
|
]
|
||||||
|
|
||||||
|
[package.metadata.cargo-flash]
|
||||||
|
chip = "STM32U575ZIT"
|
||||||
2
2sem_sem2/.gitignore
vendored
Normal file
2
2sem_sem2/.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
/target
|
||||||
|
x.txt
|
||||||
1089
2sem_sem2/Cargo.lock
generated
Normal file
1089
2sem_sem2/Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
30
2sem_sem2/Cargo.toml
Normal file
30
2sem_sem2/Cargo.toml
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
[package]
|
||||||
|
authors = ["Priec <filippriec@gmail.com>"]
|
||||||
|
name = "semestralka2"
|
||||||
|
edition = "2024"
|
||||||
|
version = "0.1.0"
|
||||||
|
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
cortex-m = { version = "0.7.7", features = ["inline-asm", "critical-section-single-core"] }
|
||||||
|
cortex-m-rt = "0.7.5"
|
||||||
|
panic-halt = "1.0.0"
|
||||||
|
|
||||||
|
embassy-executor = { version = "0.9.1", features = ["arch-cortex-m", "executor-thread"] }
|
||||||
|
embassy-futures = "0.1.2"
|
||||||
|
embassy-stm32 = { version = "0.4.0", features = ["unstable-pac", "stm32u575zi", "time-driver-any", "memory-x", "defmt", "exti"] }
|
||||||
|
embassy-sync = "0.7.2"
|
||||||
|
embassy-time = { version = "0.5.0", features = ["tick-hz-32_768"] }
|
||||||
|
embassy-usb = "0.5.1"
|
||||||
|
|
||||||
|
embedded-hal = "1.0.0"
|
||||||
|
embedded-graphics = "0.8.1"
|
||||||
|
heapless = { version = "0.9.1", default-features = false, features = ["defmt"] }
|
||||||
|
micromath = "2.1.0"
|
||||||
|
tinybmp = "0.6.0"
|
||||||
|
panic-probe = { version = "1.0.0", features = ["defmt"] }
|
||||||
|
defmt-rtt = "1.1.0"
|
||||||
|
defmt = "1.0.1"
|
||||||
|
static_cell = "2.1.1"
|
||||||
|
embedded-io-async = "0.6.0"
|
||||||
|
embedded-io = "0.6.1"
|
||||||
201
2sem_sem2/LICENSE-APACHE
Normal file
201
2sem_sem2/LICENSE-APACHE
Normal file
@@ -0,0 +1,201 @@
|
|||||||
|
Apache License
|
||||||
|
Version 2.0, January 2004
|
||||||
|
http://www.apache.org/licenses/
|
||||||
|
|
||||||
|
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||||
|
|
||||||
|
1. Definitions.
|
||||||
|
|
||||||
|
"License" shall mean the terms and conditions for use, reproduction,
|
||||||
|
and distribution as defined by Sections 1 through 9 of this document.
|
||||||
|
|
||||||
|
"Licensor" shall mean the copyright owner or entity authorized by
|
||||||
|
the copyright owner that is granting the License.
|
||||||
|
|
||||||
|
"Legal Entity" shall mean the union of the acting entity and all
|
||||||
|
other entities that control, are controlled by, or are under common
|
||||||
|
control with that entity. For the purposes of this definition,
|
||||||
|
"control" means (i) the power, direct or indirect, to cause the
|
||||||
|
direction or management of such entity, whether by contract or
|
||||||
|
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||||
|
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||||
|
|
||||||
|
"You" (or "Your") shall mean an individual or Legal Entity
|
||||||
|
exercising permissions granted by this License.
|
||||||
|
|
||||||
|
"Source" form shall mean the preferred form for making modifications,
|
||||||
|
including but not limited to software source code, documentation
|
||||||
|
source, and configuration files.
|
||||||
|
|
||||||
|
"Object" form shall mean any form resulting from mechanical
|
||||||
|
transformation or translation of a Source form, including but
|
||||||
|
not limited to compiled object code, generated documentation,
|
||||||
|
and conversions to other media types.
|
||||||
|
|
||||||
|
"Work" shall mean the work of authorship, whether in Source or
|
||||||
|
Object form, made available under the License, as indicated by a
|
||||||
|
copyright notice that is included in or attached to the work
|
||||||
|
(an example is provided in the Appendix below).
|
||||||
|
|
||||||
|
"Derivative Works" shall mean any work, whether in Source or Object
|
||||||
|
form, that is based on (or derived from) the Work and for which the
|
||||||
|
editorial revisions, annotations, elaborations, or other modifications
|
||||||
|
represent, as a whole, an original work of authorship. For the purposes
|
||||||
|
of this License, Derivative Works shall not include works that remain
|
||||||
|
separable from, or merely link (or bind by name) to the interfaces of,
|
||||||
|
the Work and Derivative Works thereof.
|
||||||
|
|
||||||
|
"Contribution" shall mean any work of authorship, including
|
||||||
|
the original version of the Work and any modifications or additions
|
||||||
|
to that Work or Derivative Works thereof, that is intentionally
|
||||||
|
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||||
|
or by an individual or Legal Entity authorized to submit on behalf of
|
||||||
|
the copyright owner. For the purposes of this definition, "submitted"
|
||||||
|
means any form of electronic, verbal, or written communication sent
|
||||||
|
to the Licensor or its representatives, including but not limited to
|
||||||
|
communication on electronic mailing lists, source code control systems,
|
||||||
|
and issue tracking systems that are managed by, or on behalf of, the
|
||||||
|
Licensor for the purpose of discussing and improving the Work, but
|
||||||
|
excluding communication that is conspicuously marked or otherwise
|
||||||
|
designated in writing by the copyright owner as "Not a Contribution."
|
||||||
|
|
||||||
|
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||||
|
on behalf of whom a Contribution has been received by Licensor and
|
||||||
|
subsequently incorporated within the Work.
|
||||||
|
|
||||||
|
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||||
|
this License, each Contributor hereby grants to You a perpetual,
|
||||||
|
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||||
|
copyright license to reproduce, prepare Derivative Works of,
|
||||||
|
publicly display, publicly perform, sublicense, and distribute the
|
||||||
|
Work and such Derivative Works in Source or Object form.
|
||||||
|
|
||||||
|
3. Grant of Patent License. Subject to the terms and conditions of
|
||||||
|
this License, each Contributor hereby grants to You a perpetual,
|
||||||
|
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||||
|
(except as stated in this section) patent license to make, have made,
|
||||||
|
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||||
|
where such license applies only to those patent claims licensable
|
||||||
|
by such Contributor that are necessarily infringed by their
|
||||||
|
Contribution(s) alone or by combination of their Contribution(s)
|
||||||
|
with the Work to which such Contribution(s) was submitted. If You
|
||||||
|
institute patent litigation against any entity (including a
|
||||||
|
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||||
|
or a Contribution incorporated within the Work constitutes direct
|
||||||
|
or contributory patent infringement, then any patent licenses
|
||||||
|
granted to You under this License for that Work shall terminate
|
||||||
|
as of the date such litigation is filed.
|
||||||
|
|
||||||
|
4. Redistribution. You may reproduce and distribute copies of the
|
||||||
|
Work or Derivative Works thereof in any medium, with or without
|
||||||
|
modifications, and in Source or Object form, provided that You
|
||||||
|
meet the following conditions:
|
||||||
|
|
||||||
|
(a) You must give any other recipients of the Work or
|
||||||
|
Derivative Works a copy of this License; and
|
||||||
|
|
||||||
|
(b) You must cause any modified files to carry prominent notices
|
||||||
|
stating that You changed the files; and
|
||||||
|
|
||||||
|
(c) You must retain, in the Source form of any Derivative Works
|
||||||
|
that You distribute, all copyright, patent, trademark, and
|
||||||
|
attribution notices from the Source form of the Work,
|
||||||
|
excluding those notices that do not pertain to any part of
|
||||||
|
the Derivative Works; and
|
||||||
|
|
||||||
|
(d) If the Work includes a "NOTICE" text file as part of its
|
||||||
|
distribution, then any Derivative Works that You distribute must
|
||||||
|
include a readable copy of the attribution notices contained
|
||||||
|
within such NOTICE file, excluding those notices that do not
|
||||||
|
pertain to any part of the Derivative Works, in at least one
|
||||||
|
of the following places: within a NOTICE text file distributed
|
||||||
|
as part of the Derivative Works; within the Source form or
|
||||||
|
documentation, if provided along with the Derivative Works; or,
|
||||||
|
within a display generated by the Derivative Works, if and
|
||||||
|
wherever such third-party notices normally appear. The contents
|
||||||
|
of the NOTICE file are for informational purposes only and
|
||||||
|
do not modify the License. You may add Your own attribution
|
||||||
|
notices within Derivative Works that You distribute, alongside
|
||||||
|
or as an addendum to the NOTICE text from the Work, provided
|
||||||
|
that such additional attribution notices cannot be construed
|
||||||
|
as modifying the License.
|
||||||
|
|
||||||
|
You may add Your own copyright statement to Your modifications and
|
||||||
|
may provide additional or different license terms and conditions
|
||||||
|
for use, reproduction, or distribution of Your modifications, or
|
||||||
|
for any such Derivative Works as a whole, provided Your use,
|
||||||
|
reproduction, and distribution of the Work otherwise complies with
|
||||||
|
the conditions stated in this License.
|
||||||
|
|
||||||
|
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||||
|
any Contribution intentionally submitted for inclusion in the Work
|
||||||
|
by You to the Licensor shall be under the terms and conditions of
|
||||||
|
this License, without any additional terms or conditions.
|
||||||
|
Notwithstanding the above, nothing herein shall supersede or modify
|
||||||
|
the terms of any separate license agreement you may have executed
|
||||||
|
with Licensor regarding such Contributions.
|
||||||
|
|
||||||
|
6. Trademarks. This License does not grant permission to use the trade
|
||||||
|
names, trademarks, service marks, or product names of the Licensor,
|
||||||
|
except as required for reasonable and customary use in describing the
|
||||||
|
origin of the Work and reproducing the content of the NOTICE file.
|
||||||
|
|
||||||
|
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||||
|
agreed to in writing, Licensor provides the Work (and each
|
||||||
|
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||||
|
implied, including, without limitation, any warranties or conditions
|
||||||
|
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||||
|
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||||
|
appropriateness of using or redistributing the Work and assume any
|
||||||
|
risks associated with Your exercise of permissions under this License.
|
||||||
|
|
||||||
|
8. Limitation of Liability. In no event and under no legal theory,
|
||||||
|
whether in tort (including negligence), contract, or otherwise,
|
||||||
|
unless required by applicable law (such as deliberate and grossly
|
||||||
|
negligent acts) or agreed to in writing, shall any Contributor be
|
||||||
|
liable to You for damages, including any direct, indirect, special,
|
||||||
|
incidental, or consequential damages of any character arising as a
|
||||||
|
result of this License or out of the use or inability to use the
|
||||||
|
Work (including but not limited to damages for loss of goodwill,
|
||||||
|
work stoppage, computer failure or malfunction, or any and all
|
||||||
|
other commercial damages or losses), even if such Contributor
|
||||||
|
has been advised of the possibility of such damages.
|
||||||
|
|
||||||
|
9. Accepting Warranty or Additional Liability. While redistributing
|
||||||
|
the Work or Derivative Works thereof, You may choose to offer,
|
||||||
|
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||||
|
or other liability obligations and/or rights consistent with this
|
||||||
|
License. However, in accepting such obligations, You may act only
|
||||||
|
on Your own behalf and on Your sole responsibility, not on behalf
|
||||||
|
of any other Contributor, and only if You agree to indemnify,
|
||||||
|
defend, and hold each Contributor harmless for any liability
|
||||||
|
incurred by, or claims asserted against, such Contributor by reason
|
||||||
|
of your accepting any such warranty or additional liability.
|
||||||
|
|
||||||
|
END OF TERMS AND CONDITIONS
|
||||||
|
|
||||||
|
APPENDIX: How to apply the Apache License to your work.
|
||||||
|
|
||||||
|
To apply the Apache License to your work, attach the following
|
||||||
|
boilerplate notice, with the fields enclosed by brackets "[]"
|
||||||
|
replaced with your own identifying information. (Don't include
|
||||||
|
the brackets!) The text should be enclosed in the appropriate
|
||||||
|
comment syntax for the file format. We also recommend that a
|
||||||
|
file or class name and description of purpose be included on the
|
||||||
|
same "printed page" as the copyright notice for easier
|
||||||
|
identification within third-party archives.
|
||||||
|
|
||||||
|
Copyright [yyyy] [name of copyright owner]
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
23
2sem_sem2/LICENSE-MIT
Normal file
23
2sem_sem2/LICENSE-MIT
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
Permission is hereby granted, free of charge, to any
|
||||||
|
person obtaining a copy of this software and associated
|
||||||
|
documentation files (the "Software"), to deal in the
|
||||||
|
Software without restriction, including without
|
||||||
|
limitation the rights to use, copy, modify, merge,
|
||||||
|
publish, distribute, sublicense, and/or sell copies of
|
||||||
|
the Software, and to permit persons to whom the Software
|
||||||
|
is furnished to do so, subject to the following
|
||||||
|
conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice
|
||||||
|
shall be included in all copies or substantial portions
|
||||||
|
of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
|
||||||
|
ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
|
||||||
|
TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
|
||||||
|
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
|
||||||
|
SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
||||||
|
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||||
|
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
|
||||||
|
IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||||
|
DEALINGS IN THE SOFTWARE.
|
||||||
23
2sem_sem2/Makefile
Normal file
23
2sem_sem2/Makefile
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
TARGET = thumbv8m.main-none-eabihf
|
||||||
|
CHIP = STM32U575ZI
|
||||||
|
BIN = stm32u5-blinky
|
||||||
|
MODE ?= release
|
||||||
|
TARGET_DIR = target/$(TARGET)/$(MODE)
|
||||||
|
ELF = $(TARGET_DIR)/$(BIN)
|
||||||
|
PROBE = probe-rs
|
||||||
|
|
||||||
|
.PHONY: all build flash clean empty
|
||||||
|
|
||||||
|
all: build
|
||||||
|
|
||||||
|
build:
|
||||||
|
cargo build --$(MODE)
|
||||||
|
|
||||||
|
flash: build
|
||||||
|
$(PROBE) run --chip $(CHIP) $(ELF)
|
||||||
|
|
||||||
|
empty:
|
||||||
|
$(PROBE) erase --chip $(CHIP)
|
||||||
|
|
||||||
|
clean:
|
||||||
|
cargo clean
|
||||||
232
2sem_sem2/README.md
Normal file
232
2sem_sem2/README.md
Normal file
@@ -0,0 +1,232 @@
|
|||||||
|
# `app-template`
|
||||||
|
|
||||||
|
> Quickly set up a [`probe-rs`] + [`defmt`] + [`flip-link`] embedded project
|
||||||
|
|
||||||
|
[`probe-rs`]: https://crates.io/crates/probe-rs
|
||||||
|
[`defmt`]: https://github.com/knurling-rs/defmt
|
||||||
|
[`flip-link`]: https://github.com/knurling-rs/flip-link
|
||||||
|
|
||||||
|
## Dependencies
|
||||||
|
|
||||||
|
### 1. `flip-link`:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
cargo install flip-link
|
||||||
|
```
|
||||||
|
|
||||||
|
### 2. `probe-rs`:
|
||||||
|
|
||||||
|
Install probe-rs by following the instructions at <https://probe.rs/docs/getting-started/installation/>.
|
||||||
|
|
||||||
|
### 3. [`cargo-generate`]:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
cargo install cargo-generate
|
||||||
|
```
|
||||||
|
|
||||||
|
[`cargo-generate`]: https://crates.io/crates/cargo-generate
|
||||||
|
|
||||||
|
> *Note:* You can also just clone this repository instead of using `cargo-generate`, but this involves additional manual adjustments.
|
||||||
|
|
||||||
|
## Setup
|
||||||
|
|
||||||
|
### 1. Initialize the project template
|
||||||
|
|
||||||
|
```bash
|
||||||
|
cargo generate \
|
||||||
|
--git https://github.com/knurling-rs/app-template \
|
||||||
|
--branch main \
|
||||||
|
--name my-app
|
||||||
|
```
|
||||||
|
|
||||||
|
If you look into your new `my-app` folder, you'll find that there are a few `TODO`s in the files marking the properties you need to set.
|
||||||
|
|
||||||
|
Let's walk through them together now.
|
||||||
|
|
||||||
|
### 2. Set `probe-rs` chip
|
||||||
|
|
||||||
|
Pick a chip from ` probe-rs chip list` and enter it into `.cargo/config.toml`.
|
||||||
|
|
||||||
|
If, for example, you have a nRF52840 Development Kit as used in one of [our exercises], replace `{{chip}}` with `nRF52840_xxAA`.
|
||||||
|
|
||||||
|
[our workshops]: https://rust-exercises.ferrous-systems.com
|
||||||
|
|
||||||
|
```diff
|
||||||
|
# .cargo/config.toml
|
||||||
|
-runner = ["probe-rs", "run", "--chip", "$CHIP", "--log-format=oneline"]
|
||||||
|
+runner = ["probe-rs", "run", "--chip", "nRF52840_xxAA", "--log-format=oneline"]
|
||||||
|
```
|
||||||
|
|
||||||
|
### 3. Adjust the compilation target
|
||||||
|
|
||||||
|
In `.cargo/config.toml`, pick the right compilation target for your board.
|
||||||
|
|
||||||
|
```diff
|
||||||
|
# .cargo/config.toml
|
||||||
|
[build]
|
||||||
|
-target = "thumbv6m-none-eabi" # Cortex-M0 and Cortex-M0+
|
||||||
|
-# target = "thumbv7m-none-eabi" # Cortex-M3
|
||||||
|
-# target = "thumbv7em-none-eabi" # Cortex-M4 and Cortex-M7 (no FPU)
|
||||||
|
-# target = "thumbv7em-none-eabihf" # Cortex-M4F and Cortex-M7F (with FPU)
|
||||||
|
+target = "thumbv7em-none-eabihf" # Cortex-M4F (with FPU)
|
||||||
|
```
|
||||||
|
|
||||||
|
Add the target with `rustup`.
|
||||||
|
|
||||||
|
```bash
|
||||||
|
rustup target add thumbv7em-none-eabihf
|
||||||
|
```
|
||||||
|
|
||||||
|
### 4. Add a HAL as a dependency
|
||||||
|
|
||||||
|
In `Cargo.toml`, list the Hardware Abstraction Layer (HAL) for your board as a dependency.
|
||||||
|
|
||||||
|
For the nRF52840 you'll want to use the [`nrf52840-hal`].
|
||||||
|
|
||||||
|
[`nrf52840-hal`]: https://crates.io/crates/nrf52840-hal
|
||||||
|
|
||||||
|
```diff
|
||||||
|
# Cargo.toml
|
||||||
|
[dependencies]
|
||||||
|
-# some-hal = "1.2.3"
|
||||||
|
+nrf52840-hal = "0.14.0"
|
||||||
|
```
|
||||||
|
|
||||||
|
⚠️ Note for RP2040 users ⚠️
|
||||||
|
|
||||||
|
You will need to not just specify the `rp-hal` HAL, but a BSP (board support crate) which includes a second stage bootloader. Please find a list of available BSPs [here](https://github.com/rp-rs/rp-hal-boards#packages).
|
||||||
|
|
||||||
|
### 5. Import your HAL
|
||||||
|
|
||||||
|
Now that you have selected a HAL, fix the HAL import in `src/lib.rs`
|
||||||
|
|
||||||
|
```diff
|
||||||
|
// my-app/src/lib.rs
|
||||||
|
-// use some_hal as _; // memory layout
|
||||||
|
+use nrf52840_hal as _; // memory layout
|
||||||
|
```
|
||||||
|
|
||||||
|
### (6. Get a linker script)
|
||||||
|
|
||||||
|
Some HAL crates require that you manually copy over a file called `memory.x` from the HAL to the root of your project. For nrf52840-hal, this is done automatically so no action is needed. For other HAL crates, see their documentation on where to find an example file.
|
||||||
|
|
||||||
|
The `memory.x` file should look something like:
|
||||||
|
|
||||||
|
```text
|
||||||
|
MEMORY
|
||||||
|
{
|
||||||
|
FLASH : ORIGIN = 0x00000000, LENGTH = 1024K
|
||||||
|
RAM : ORIGIN = 0x20000000, LENGTH = 256K
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
The `memory.x` file is included in the `cortex-m-rt` linker script `link.x`, and so `link.x` is the one you should tell `rustc` to use (see the `.cargo/config.toml` file where we do that).
|
||||||
|
|
||||||
|
### 7. Run!
|
||||||
|
|
||||||
|
You are now all set to `cargo-run` your first `defmt`-powered application!
|
||||||
|
There are some examples in the `src/bin` directory.
|
||||||
|
|
||||||
|
Start by `cargo run`-ning `my-app/src/bin/hello.rs`:
|
||||||
|
|
||||||
|
```console
|
||||||
|
$ # `rb` is an alias for `run --bin`
|
||||||
|
$ cargo rb hello
|
||||||
|
Finished `dev` profile [optimized + debuginfo] target(s) in 0.01s
|
||||||
|
Running `probe-rs run --chip nrf52840_xxaa --log-format=oneline target/thumbv6m-none-eabi/debug/hello`
|
||||||
|
Erasing ✔ 100% [####################] 8.00 KiB @ 15.79 KiB/s (took 1s)
|
||||||
|
Programming ✔ 100% [####################] 8.00 KiB @ 13.19 KiB/s (took 1s) Finished in 1.11s
|
||||||
|
Hello, world!
|
||||||
|
|
||||||
|
$ echo $?
|
||||||
|
0
|
||||||
|
```
|
||||||
|
|
||||||
|
If you're running out of memory (`flip-link` bails with an overflow error), you can decrease the size of the device memory buffer by setting the `DEFMT_RTT_BUFFER_SIZE` environment variable. The default value is 1024 bytes, and powers of two should be used for optimal performance:
|
||||||
|
|
||||||
|
```console
|
||||||
|
$ DEFMT_RTT_BUFFER_SIZE=64 cargo rb hello
|
||||||
|
```
|
||||||
|
|
||||||
|
### (8. Set `rust-analyzer.linkedProjects`)
|
||||||
|
|
||||||
|
If you are using [rust-analyzer] with VS Code for IDE-like features you can add following configuration to your `.vscode/settings.json` to make it work transparently across workspaces. Find the details of this option in the [RA docs].
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"rust-analyzer.linkedProjects": [
|
||||||
|
"Cargo.toml",
|
||||||
|
"firmware/Cargo.toml",
|
||||||
|
]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
[RA docs]: https://rust-analyzer.github.io/manual.html#configuration
|
||||||
|
[rust-analyzer]: https://rust-analyzer.github.io/
|
||||||
|
|
||||||
|
## Running tests
|
||||||
|
|
||||||
|
The template comes configured for running unit tests and integration tests on the target.
|
||||||
|
|
||||||
|
Unit tests reside in the library crate and can test private API; the initial set of unit tests are in `src/lib.rs`.
|
||||||
|
`cargo test --lib` will run those unit tests.
|
||||||
|
|
||||||
|
```console
|
||||||
|
$ cargo test --lib
|
||||||
|
Compiling example v0.1.0 (./knurling-rs/example)
|
||||||
|
Finished `test` profile [optimized + debuginfo] target(s) in 0.15s
|
||||||
|
Running unittests src/lib.rs (target/thumbv6m-none-eabi/debug/deps/example-2b0d0e25d141bf57)
|
||||||
|
Erasing ✔ 100% [####################] 8.00 KiB @ 15.99 KiB/s (took 1s)
|
||||||
|
Programming ✔ 100% [####################] 8.00 KiB @ 13.33 KiB/s (took 1s) Finished in 1.10s
|
||||||
|
(1/1) running `it_works`...
|
||||||
|
all tests passed!
|
||||||
|
```
|
||||||
|
|
||||||
|
Integration tests reside in the `tests` directory; the initial set of integration tests are in `tests/integration.rs`.
|
||||||
|
`cargo test --test integration` will run those integration tests.
|
||||||
|
Note that the argument of the `--test` flag must match the name of the test file in the `tests` directory.
|
||||||
|
|
||||||
|
```console
|
||||||
|
$ cargo test --test integration
|
||||||
|
Compiling example v0.1.0 (./knurling-rs/example)
|
||||||
|
Finished `test` profile [optimized + debuginfo] target(s) in 0.10s
|
||||||
|
Running tests/integration.rs (target/thumbv6m-none-eabi/debug/deps/integration-aaaff41151f6a722)
|
||||||
|
Erasing ✔ 100% [####################] 8.00 KiB @ 16.03 KiB/s (took 0s)
|
||||||
|
Programming ✔ 100% [####################] 8.00 KiB @ 13.19 KiB/s (took 1s) Finished in 1.11s
|
||||||
|
(1/1) running `it_works`...
|
||||||
|
all tests passed!
|
||||||
|
```
|
||||||
|
|
||||||
|
Note that to add a new test file to the `tests` directory you also need to add a new `[[test]]` section to `Cargo.toml`.
|
||||||
|
|
||||||
|
To run all the tests via `cargo test` the tests need to be explicitly disabled for all the existing binary targets.
|
||||||
|
See `Cargo.toml` for details on how to do this.
|
||||||
|
|
||||||
|
## Support
|
||||||
|
|
||||||
|
`app-template` is part of the [Knurling] project, [Ferrous Systems]' effort at
|
||||||
|
improving tooling used to develop for embedded systems.
|
||||||
|
|
||||||
|
If you think that our work is useful, consider sponsoring it via [GitHub
|
||||||
|
Sponsors].
|
||||||
|
|
||||||
|
## License
|
||||||
|
|
||||||
|
Licensed under either of
|
||||||
|
|
||||||
|
- Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0)
|
||||||
|
|
||||||
|
- MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)
|
||||||
|
|
||||||
|
at your option.
|
||||||
|
|
||||||
|
### Contribution
|
||||||
|
|
||||||
|
Unless you explicitly state otherwise, any contribution intentionally submitted
|
||||||
|
for inclusion in the work by you, as defined in the Apache-2.0 license, shall be
|
||||||
|
licensed as above, without any additional terms or conditions.
|
||||||
|
|
||||||
|
[Knurling]: https://knurling.ferrous-systems.com
|
||||||
|
[Ferrous Systems]: https://ferrous-systems.com/
|
||||||
|
[GitHub Sponsors]: https://github.com/sponsors/knurling-rs
|
||||||
57
2sem_sem2/src/bin/main.rs
Normal file
57
2sem_sem2/src/bin/main.rs
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
// src/bin/main.rs
|
||||||
|
#![no_std]
|
||||||
|
#![no_main]
|
||||||
|
|
||||||
|
use defmt::info;
|
||||||
|
use embassy_executor::Spawner;
|
||||||
|
use embassy_stm32::exti::ExtiInput;
|
||||||
|
use embassy_stm32::gpio;
|
||||||
|
use embassy_stm32::gpio::{Output, Pull};
|
||||||
|
use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex;
|
||||||
|
use embassy_sync::channel::Channel;
|
||||||
|
use embassy_time::Duration;
|
||||||
|
use semestralka2::level_for_bit;
|
||||||
|
use semestralka2::receive::bit_receive_and_decode;
|
||||||
|
use semestralka2::send::{bit_send, msg_encode};
|
||||||
|
|
||||||
|
use {defmt_rtt as _, panic_probe as _};
|
||||||
|
|
||||||
|
static PIPE_SEND: Channel<CriticalSectionRawMutex, u8, 128> = Channel::new();
|
||||||
|
// static PIPE_REC: Channel<CriticalSectionRawMutex, u8, 128> = Channel::new();
|
||||||
|
// static REC_ALLOWED: AtomicBool = AtomicBool::new(true);
|
||||||
|
|
||||||
|
#[embassy_executor::main]
|
||||||
|
async fn main(spawner: Spawner) {
|
||||||
|
info!("tititititi");
|
||||||
|
|
||||||
|
let p = embassy_stm32::init(Default::default());
|
||||||
|
let tx = Output::new(
|
||||||
|
p.PF2,
|
||||||
|
if level_for_bit(1) {
|
||||||
|
gpio::Level::High
|
||||||
|
} else {
|
||||||
|
gpio::Level::Low
|
||||||
|
},
|
||||||
|
gpio::Speed::VeryHigh,
|
||||||
|
);
|
||||||
|
let rx = ExtiInput::new(p.PA3, p.EXTI3, Pull::Up);
|
||||||
|
|
||||||
|
let sender = PIPE_SEND.sender();
|
||||||
|
let receiver = PIPE_SEND.receiver();
|
||||||
|
|
||||||
|
let e_nrz = semestralka2::Encoding::Nrz;
|
||||||
|
spawner.spawn(bit_send(tx, receiver, e_nrz)).unwrap();
|
||||||
|
spawner.spawn(bit_receive_and_decode(rx, e_nrz)).unwrap();
|
||||||
|
|
||||||
|
info!("starting loop");
|
||||||
|
loop {
|
||||||
|
let _ = msg_encode(b"ahoj", &sender).await;
|
||||||
|
let _ = msg_encode(b"hello", &sender).await;
|
||||||
|
|
||||||
|
let data = [0x01, 0x02, 0x03, 0x04];
|
||||||
|
let _ = msg_encode(&data, &sender).await;
|
||||||
|
|
||||||
|
info!("frame sent");
|
||||||
|
embassy_time::Timer::after(Duration::from_secs(1)).await;
|
||||||
|
}
|
||||||
|
}
|
||||||
29
2sem_sem2/src/lib.rs
Normal file
29
2sem_sem2/src/lib.rs
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
#![no_std]
|
||||||
|
|
||||||
|
pub mod receive;
|
||||||
|
pub mod send;
|
||||||
|
|
||||||
|
#[derive(Copy, Clone, PartialEq)]
|
||||||
|
pub enum Encoding {
|
||||||
|
Nrz,
|
||||||
|
Nrzi,
|
||||||
|
Manchester,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Copy, Clone, PartialEq)]
|
||||||
|
pub enum LogicLevel {
|
||||||
|
Low,
|
||||||
|
High,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub const LOGIC_ZERO: LogicLevel = LogicLevel::Low;
|
||||||
|
pub const LOGIC_ONE: LogicLevel = LogicLevel::High;
|
||||||
|
|
||||||
|
pub fn level_for_bit(bit: u8) -> bool {
|
||||||
|
let level = if bit == 0 { LOGIC_ZERO } else { LOGIC_ONE };
|
||||||
|
level == LogicLevel::High
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn bit_for_level(level: bool) -> u8 {
|
||||||
|
if level_for_bit(1) == level { 1 } else { 0 }
|
||||||
|
}
|
||||||
211
2sem_sem2/src/receive.rs
Normal file
211
2sem_sem2/src/receive.rs
Normal file
@@ -0,0 +1,211 @@
|
|||||||
|
use defmt::info;
|
||||||
|
use embassy_stm32::exti::ExtiInput;
|
||||||
|
use embassy_time::{Ticker, Timer};
|
||||||
|
|
||||||
|
use crate::{Encoding, bit_for_level, level_for_bit};
|
||||||
|
|
||||||
|
const START: u8 = 0x7E;
|
||||||
|
const STOP: u8 = 0x81;
|
||||||
|
const MAX_PAYLOAD: usize = 64;
|
||||||
|
|
||||||
|
fn crc8(crc: u8, byte: u8) -> u8 {
|
||||||
|
let mut c = crc ^ byte;
|
||||||
|
for _ in 0..8 {
|
||||||
|
c = if c & 0x80 != 0 {
|
||||||
|
(c << 1) ^ 0x07
|
||||||
|
} else {
|
||||||
|
c << 1
|
||||||
|
};
|
||||||
|
}
|
||||||
|
c
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn sample_byte(
|
||||||
|
pin: &mut ExtiInput<'static>,
|
||||||
|
encoding: Encoding,
|
||||||
|
ticker: &mut Ticker,
|
||||||
|
last_physical_level: &mut bool,
|
||||||
|
bit_time: embassy_time::Duration,
|
||||||
|
) -> u8 {
|
||||||
|
let mut byte = 0u8;
|
||||||
|
for _ in 0..8 {
|
||||||
|
let current_level = pin.is_high();
|
||||||
|
let bit = match encoding {
|
||||||
|
Encoding::Nrz => {
|
||||||
|
let bit = bit_for_level(pin.is_high());
|
||||||
|
ticker.next().await;
|
||||||
|
bit
|
||||||
|
}
|
||||||
|
Encoding::Nrzi => {
|
||||||
|
let bit = if current_level != *last_physical_level {
|
||||||
|
1
|
||||||
|
} else {
|
||||||
|
0
|
||||||
|
};
|
||||||
|
*last_physical_level = current_level;
|
||||||
|
ticker.next().await;
|
||||||
|
bit
|
||||||
|
}
|
||||||
|
Encoding::Manchester => {
|
||||||
|
Timer::after(bit_time / 4).await;
|
||||||
|
let first_half = pin.is_high();
|
||||||
|
|
||||||
|
Timer::after(bit_time / 2).await;
|
||||||
|
let second_half = pin.is_high();
|
||||||
|
|
||||||
|
Timer::after(bit_time / 4).await;
|
||||||
|
|
||||||
|
if first_half == level_for_bit(1) && second_half == level_for_bit(0) {
|
||||||
|
1
|
||||||
|
} else {
|
||||||
|
0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
byte = (byte << 1) | bit;
|
||||||
|
}
|
||||||
|
byte
|
||||||
|
}
|
||||||
|
|
||||||
|
#[embassy_executor::task]
|
||||||
|
pub async fn bit_receive_and_decode(mut pin: ExtiInput<'static>, encoding: Encoding) {
|
||||||
|
loop {
|
||||||
|
if level_for_bit(1) {
|
||||||
|
pin.wait_for_falling_edge().await;
|
||||||
|
} else {
|
||||||
|
pin.wait_for_rising_edge().await;
|
||||||
|
}
|
||||||
|
let t1 = embassy_time::Instant::now();
|
||||||
|
|
||||||
|
if level_for_bit(1) {
|
||||||
|
pin.wait_for_rising_edge().await;
|
||||||
|
} else {
|
||||||
|
pin.wait_for_falling_edge().await;
|
||||||
|
}
|
||||||
|
let bit_time = embassy_time::Instant::now().duration_since(t1);
|
||||||
|
|
||||||
|
// stred data bitu
|
||||||
|
if encoding != Encoding::Manchester {
|
||||||
|
Timer::after(bit_time / 2).await;
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut ticker = Ticker::every(bit_time);
|
||||||
|
let mut last_level = level_for_bit(1);
|
||||||
|
|
||||||
|
// First START bit is the already detected logical 0, sample the remaining 7 bits.
|
||||||
|
let mut start_byte = 0u8;
|
||||||
|
for _ in 0..7 {
|
||||||
|
let bit = match encoding {
|
||||||
|
Encoding::Nrz => {
|
||||||
|
let bit = bit_for_level(pin.is_high());
|
||||||
|
ticker.next().await;
|
||||||
|
bit
|
||||||
|
}
|
||||||
|
Encoding::Nrzi => {
|
||||||
|
let current_level = pin.is_high();
|
||||||
|
let b = if current_level != last_level { 1 } else { 0 };
|
||||||
|
last_level = current_level;
|
||||||
|
ticker.next().await;
|
||||||
|
b
|
||||||
|
}
|
||||||
|
Encoding::Manchester => {
|
||||||
|
Timer::after(bit_time / 4).await;
|
||||||
|
let first_pol = pin.is_high();
|
||||||
|
Timer::after(bit_time / 2).await;
|
||||||
|
let second_pol = pin.is_high();
|
||||||
|
Timer::after(bit_time / 4).await;
|
||||||
|
if first_pol == level_for_bit(1) && second_pol == level_for_bit(0) {
|
||||||
|
1
|
||||||
|
} else {
|
||||||
|
0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
start_byte = (start_byte << 1) | bit;
|
||||||
|
}
|
||||||
|
|
||||||
|
if start_byte != START {
|
||||||
|
info!("start fail: 0x{:02X}", start_byte);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// len
|
||||||
|
let len = sample_byte(&mut pin, encoding, &mut ticker, &mut last_level, bit_time).await;
|
||||||
|
if len as usize > MAX_PAYLOAD {
|
||||||
|
info!("bad len {}", len);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// payload
|
||||||
|
let mut payload = [0u8; MAX_PAYLOAD];
|
||||||
|
for i in 0..(len as usize) {
|
||||||
|
payload[i] =
|
||||||
|
sample_byte(&mut pin, encoding, &mut ticker, &mut last_level, bit_time).await;
|
||||||
|
}
|
||||||
|
|
||||||
|
// crc
|
||||||
|
let crc_recv =
|
||||||
|
sample_byte(&mut pin, encoding, &mut ticker, &mut last_level, bit_time).await;
|
||||||
|
|
||||||
|
// parita
|
||||||
|
let parity_recv = match encoding {
|
||||||
|
Encoding::Nrz => {
|
||||||
|
let bit = bit_for_level(pin.is_high());
|
||||||
|
ticker.next().await;
|
||||||
|
bit
|
||||||
|
}
|
||||||
|
Encoding::Nrzi => {
|
||||||
|
let current_p = pin.is_high();
|
||||||
|
let b = if current_p != last_level { 1 } else { 0 };
|
||||||
|
last_level = current_p;
|
||||||
|
ticker.next().await;
|
||||||
|
b
|
||||||
|
}
|
||||||
|
Encoding::Manchester => {
|
||||||
|
Timer::after(bit_time / 4).await;
|
||||||
|
let f = pin.is_high();
|
||||||
|
Timer::after(bit_time / 2).await;
|
||||||
|
let s = pin.is_high();
|
||||||
|
Timer::after(bit_time / 4).await;
|
||||||
|
if f == level_for_bit(1) && s == level_for_bit(0) {
|
||||||
|
1
|
||||||
|
} else {
|
||||||
|
0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// stop
|
||||||
|
let stop = sample_byte(&mut pin, encoding, &mut ticker, &mut last_level, bit_time).await;
|
||||||
|
if stop != STOP {
|
||||||
|
info!("bad stop 0x{:02X}", stop);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// verify CRC
|
||||||
|
let mut crc_calc = crc8(0, len);
|
||||||
|
for i in 0..(len as usize) {
|
||||||
|
crc_calc = crc8(crc_calc, payload[i]);
|
||||||
|
}
|
||||||
|
if crc_calc != crc_recv {
|
||||||
|
info!("crc nesedi 0x{:02X} ma byt 0x{:02X}", crc_recv, crc_calc);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// verify parity
|
||||||
|
let mut ones = len.count_ones() + crc_recv.count_ones();
|
||||||
|
for i in 0..(len as usize) {
|
||||||
|
ones += payload[i].count_ones();
|
||||||
|
}
|
||||||
|
if (ones as u8 & 1) != parity_recv {
|
||||||
|
info!("nesedi parita");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Ok(s) = core::str::from_utf8(&payload[..len as usize]) {
|
||||||
|
info!("RX OK: \"{}\"", s);
|
||||||
|
} else {
|
||||||
|
info!("RX OK: {} bytes", len);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
126
2sem_sem2/src/send.rs
Normal file
126
2sem_sem2/src/send.rs
Normal file
@@ -0,0 +1,126 @@
|
|||||||
|
// src/send.rs
|
||||||
|
|
||||||
|
use embassy_stm32::gpio::{Level, Output};
|
||||||
|
use embassy_sync::{
|
||||||
|
blocking_mutex::raw::CriticalSectionRawMutex,
|
||||||
|
channel::{Receiver, Sender},
|
||||||
|
};
|
||||||
|
use embassy_time::{Duration, Ticker};
|
||||||
|
|
||||||
|
use crate::{Encoding, level_for_bit};
|
||||||
|
|
||||||
|
const START: u8 = 0x7E;
|
||||||
|
const STOP: u8 = 0x81;
|
||||||
|
const MAX_PAYLOAD: usize = 64;
|
||||||
|
const BIT_PERIOD: Duration = Duration::from_millis(10);
|
||||||
|
|
||||||
|
pub type Tx = Sender<'static, CriticalSectionRawMutex, u8, 128>;
|
||||||
|
|
||||||
|
fn set_level(pin: &mut Output<'static>, is_high: bool) {
|
||||||
|
pin.set_level(if is_high { Level::High } else { Level::Low });
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn send_byte(byte: u8, tx: &Tx) {
|
||||||
|
for i in (0..8).rev() {
|
||||||
|
tx.send((byte >> i) & 1).await;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// [START] [LEN] [PAYLOAD ...] [CRC] [P] [STOP]
|
||||||
|
pub async fn msg_encode(payload: &[u8], tx: &Tx) -> Result<(), ()> {
|
||||||
|
if payload.len() > MAX_PAYLOAD {
|
||||||
|
return Err(());
|
||||||
|
}
|
||||||
|
|
||||||
|
// start
|
||||||
|
send_byte(START, tx).await;
|
||||||
|
|
||||||
|
// len
|
||||||
|
let len = payload.len() as u8;
|
||||||
|
send_byte(len, tx).await;
|
||||||
|
for &b in payload {
|
||||||
|
send_byte(b, tx).await;
|
||||||
|
}
|
||||||
|
|
||||||
|
// crc
|
||||||
|
let mut crc = crc8(0, len);
|
||||||
|
for &b in payload {
|
||||||
|
crc = crc8(crc, b);
|
||||||
|
}
|
||||||
|
send_byte(crc, tx).await;
|
||||||
|
|
||||||
|
let mut ones = len.count_ones() + crc.count_ones();
|
||||||
|
for &b in payload {
|
||||||
|
ones += b.count_ones();
|
||||||
|
}
|
||||||
|
tx.send((ones as u8) & 1).await;
|
||||||
|
|
||||||
|
send_byte(STOP, tx).await;
|
||||||
|
|
||||||
|
// idle
|
||||||
|
tx.send(1).await;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn crc8(crc: u8, byte: u8) -> u8 {
|
||||||
|
let mut c = crc ^ byte;
|
||||||
|
for _ in 0..8 {
|
||||||
|
c = if c & 0x80 != 0 {
|
||||||
|
(c << 1) ^ 0x07
|
||||||
|
} else {
|
||||||
|
c << 1
|
||||||
|
};
|
||||||
|
}
|
||||||
|
c
|
||||||
|
}
|
||||||
|
|
||||||
|
#[embassy_executor::task]
|
||||||
|
pub async fn bit_send(
|
||||||
|
mut pin: Output<'static>,
|
||||||
|
rx: Receiver<'static, CriticalSectionRawMutex, u8, 128>,
|
||||||
|
encoding: Encoding,
|
||||||
|
) {
|
||||||
|
let mut ticker = Ticker::every(BIT_PERIOD / 2);
|
||||||
|
let mut is_high = level_for_bit(1);
|
||||||
|
set_level(&mut pin, is_high);
|
||||||
|
|
||||||
|
loop {
|
||||||
|
let bit = rx.receive().await;
|
||||||
|
match encoding {
|
||||||
|
Encoding::Nrz => {
|
||||||
|
is_high = level_for_bit(bit);
|
||||||
|
set_level(&mut pin, is_high);
|
||||||
|
ticker.next().await;
|
||||||
|
}
|
||||||
|
// toggle, ak sme v 1, inac sa nedeje nic
|
||||||
|
Encoding::Nrzi => {
|
||||||
|
if bit == 1 {
|
||||||
|
is_high = !is_high;
|
||||||
|
if is_high {
|
||||||
|
pin.set_high();
|
||||||
|
} else {
|
||||||
|
pin.set_low();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ticker.next().await;
|
||||||
|
}
|
||||||
|
// manchester xor tabulka
|
||||||
|
Encoding::Manchester => {
|
||||||
|
if bit == 1 {
|
||||||
|
// high -> low
|
||||||
|
set_level(&mut pin, level_for_bit(1));
|
||||||
|
ticker.next().await;
|
||||||
|
set_level(&mut pin, level_for_bit(0));
|
||||||
|
ticker.next().await;
|
||||||
|
} else {
|
||||||
|
// low -> high
|
||||||
|
set_level(&mut pin, level_for_bit(0));
|
||||||
|
ticker.next().await;
|
||||||
|
set_level(&mut pin, level_for_bit(1));
|
||||||
|
ticker.next().await;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
16
2sem_sem2/tests/integration.rs
Normal file
16
2sem_sem2/tests/integration.rs
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
#![no_std]
|
||||||
|
#![no_main]
|
||||||
|
|
||||||
|
use stm32u5_blinky as _; // memory layout + panic handler
|
||||||
|
|
||||||
|
// See https://crates.io/crates/defmt-test/0.3.0 for more documentation (e.g. about the 'state'
|
||||||
|
// feature)
|
||||||
|
#[defmt_test::tests]
|
||||||
|
mod tests {
|
||||||
|
use defmt::assert;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn it_works() {
|
||||||
|
assert!(true)
|
||||||
|
}
|
||||||
|
}
|
||||||
15
2sem_spi/.cargo/config.toml
Normal file
15
2sem_spi/.cargo/config.toml
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
[build]
|
||||||
|
target = "thumbv8m.main-none-eabihf"
|
||||||
|
|
||||||
|
[target.thumbv8m.main-none-eabihf]
|
||||||
|
runner = "probe-rs run --chip STM32U575ZITxQ"
|
||||||
|
|
||||||
|
rustflags = [
|
||||||
|
"-C", "linker=rust-lld",
|
||||||
|
"-C", "link-arg=-Tlink.x",
|
||||||
|
"-C", "link-arg=-Tdefmt.x",
|
||||||
|
"-C", "link-arg=--nmagic",
|
||||||
|
]
|
||||||
|
|
||||||
|
[package.metadata.cargo-flash]
|
||||||
|
chip = "STM32U575ZIT"
|
||||||
1
2sem_spi/.gitignore
vendored
Normal file
1
2sem_spi/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
/target
|
||||||
1088
2sem_spi/Cargo.lock
generated
Normal file
1088
2sem_spi/Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
30
2sem_spi/Cargo.toml
Normal file
30
2sem_spi/Cargo.toml
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
[package]
|
||||||
|
authors = ["Priec <filippriec@gmail.com>"]
|
||||||
|
name = "hal_rng"
|
||||||
|
edition = "2024"
|
||||||
|
version = "0.1.0"
|
||||||
|
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
cortex-m = { version = "0.7.7", features = ["inline-asm", "critical-section-single-core"] }
|
||||||
|
cortex-m-rt = "0.7.5"
|
||||||
|
panic-halt = "1.0.0"
|
||||||
|
|
||||||
|
embassy-executor = { version = "0.9.1", features = ["arch-cortex-m", "executor-thread"] }
|
||||||
|
embassy-futures = "0.1.2"
|
||||||
|
embassy-stm32 = { version = "0.4.0", features = ["unstable-pac", "stm32u575zi", "time-driver-any", "memory-x", "defmt"] }
|
||||||
|
embassy-sync = "0.7.2"
|
||||||
|
embassy-time = { version = "0.5.0", features = ["tick-hz-32_768"] }
|
||||||
|
embassy-usb = "0.5.1"
|
||||||
|
|
||||||
|
embedded-hal = "1.0.0"
|
||||||
|
embedded-graphics = "0.8.1"
|
||||||
|
heapless = { version = "0.9.1", default-features = false }
|
||||||
|
micromath = "2.1.0"
|
||||||
|
tinybmp = "0.6.0"
|
||||||
|
panic-probe = { version = "1.0.0", features = ["defmt"] }
|
||||||
|
defmt-rtt = "1.1.0"
|
||||||
|
defmt = "1.0.1"
|
||||||
|
static_cell = "2.1.1"
|
||||||
|
embedded-io-async = "0.6.0"
|
||||||
|
embedded-io = "0.6.1"
|
||||||
201
2sem_spi/LICENSE-APACHE
Normal file
201
2sem_spi/LICENSE-APACHE
Normal file
@@ -0,0 +1,201 @@
|
|||||||
|
Apache License
|
||||||
|
Version 2.0, January 2004
|
||||||
|
http://www.apache.org/licenses/
|
||||||
|
|
||||||
|
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||||
|
|
||||||
|
1. Definitions.
|
||||||
|
|
||||||
|
"License" shall mean the terms and conditions for use, reproduction,
|
||||||
|
and distribution as defined by Sections 1 through 9 of this document.
|
||||||
|
|
||||||
|
"Licensor" shall mean the copyright owner or entity authorized by
|
||||||
|
the copyright owner that is granting the License.
|
||||||
|
|
||||||
|
"Legal Entity" shall mean the union of the acting entity and all
|
||||||
|
other entities that control, are controlled by, or are under common
|
||||||
|
control with that entity. For the purposes of this definition,
|
||||||
|
"control" means (i) the power, direct or indirect, to cause the
|
||||||
|
direction or management of such entity, whether by contract or
|
||||||
|
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||||
|
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||||
|
|
||||||
|
"You" (or "Your") shall mean an individual or Legal Entity
|
||||||
|
exercising permissions granted by this License.
|
||||||
|
|
||||||
|
"Source" form shall mean the preferred form for making modifications,
|
||||||
|
including but not limited to software source code, documentation
|
||||||
|
source, and configuration files.
|
||||||
|
|
||||||
|
"Object" form shall mean any form resulting from mechanical
|
||||||
|
transformation or translation of a Source form, including but
|
||||||
|
not limited to compiled object code, generated documentation,
|
||||||
|
and conversions to other media types.
|
||||||
|
|
||||||
|
"Work" shall mean the work of authorship, whether in Source or
|
||||||
|
Object form, made available under the License, as indicated by a
|
||||||
|
copyright notice that is included in or attached to the work
|
||||||
|
(an example is provided in the Appendix below).
|
||||||
|
|
||||||
|
"Derivative Works" shall mean any work, whether in Source or Object
|
||||||
|
form, that is based on (or derived from) the Work and for which the
|
||||||
|
editorial revisions, annotations, elaborations, or other modifications
|
||||||
|
represent, as a whole, an original work of authorship. For the purposes
|
||||||
|
of this License, Derivative Works shall not include works that remain
|
||||||
|
separable from, or merely link (or bind by name) to the interfaces of,
|
||||||
|
the Work and Derivative Works thereof.
|
||||||
|
|
||||||
|
"Contribution" shall mean any work of authorship, including
|
||||||
|
the original version of the Work and any modifications or additions
|
||||||
|
to that Work or Derivative Works thereof, that is intentionally
|
||||||
|
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||||
|
or by an individual or Legal Entity authorized to submit on behalf of
|
||||||
|
the copyright owner. For the purposes of this definition, "submitted"
|
||||||
|
means any form of electronic, verbal, or written communication sent
|
||||||
|
to the Licensor or its representatives, including but not limited to
|
||||||
|
communication on electronic mailing lists, source code control systems,
|
||||||
|
and issue tracking systems that are managed by, or on behalf of, the
|
||||||
|
Licensor for the purpose of discussing and improving the Work, but
|
||||||
|
excluding communication that is conspicuously marked or otherwise
|
||||||
|
designated in writing by the copyright owner as "Not a Contribution."
|
||||||
|
|
||||||
|
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||||
|
on behalf of whom a Contribution has been received by Licensor and
|
||||||
|
subsequently incorporated within the Work.
|
||||||
|
|
||||||
|
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||||
|
this License, each Contributor hereby grants to You a perpetual,
|
||||||
|
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||||
|
copyright license to reproduce, prepare Derivative Works of,
|
||||||
|
publicly display, publicly perform, sublicense, and distribute the
|
||||||
|
Work and such Derivative Works in Source or Object form.
|
||||||
|
|
||||||
|
3. Grant of Patent License. Subject to the terms and conditions of
|
||||||
|
this License, each Contributor hereby grants to You a perpetual,
|
||||||
|
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||||
|
(except as stated in this section) patent license to make, have made,
|
||||||
|
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||||
|
where such license applies only to those patent claims licensable
|
||||||
|
by such Contributor that are necessarily infringed by their
|
||||||
|
Contribution(s) alone or by combination of their Contribution(s)
|
||||||
|
with the Work to which such Contribution(s) was submitted. If You
|
||||||
|
institute patent litigation against any entity (including a
|
||||||
|
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||||
|
or a Contribution incorporated within the Work constitutes direct
|
||||||
|
or contributory patent infringement, then any patent licenses
|
||||||
|
granted to You under this License for that Work shall terminate
|
||||||
|
as of the date such litigation is filed.
|
||||||
|
|
||||||
|
4. Redistribution. You may reproduce and distribute copies of the
|
||||||
|
Work or Derivative Works thereof in any medium, with or without
|
||||||
|
modifications, and in Source or Object form, provided that You
|
||||||
|
meet the following conditions:
|
||||||
|
|
||||||
|
(a) You must give any other recipients of the Work or
|
||||||
|
Derivative Works a copy of this License; and
|
||||||
|
|
||||||
|
(b) You must cause any modified files to carry prominent notices
|
||||||
|
stating that You changed the files; and
|
||||||
|
|
||||||
|
(c) You must retain, in the Source form of any Derivative Works
|
||||||
|
that You distribute, all copyright, patent, trademark, and
|
||||||
|
attribution notices from the Source form of the Work,
|
||||||
|
excluding those notices that do not pertain to any part of
|
||||||
|
the Derivative Works; and
|
||||||
|
|
||||||
|
(d) If the Work includes a "NOTICE" text file as part of its
|
||||||
|
distribution, then any Derivative Works that You distribute must
|
||||||
|
include a readable copy of the attribution notices contained
|
||||||
|
within such NOTICE file, excluding those notices that do not
|
||||||
|
pertain to any part of the Derivative Works, in at least one
|
||||||
|
of the following places: within a NOTICE text file distributed
|
||||||
|
as part of the Derivative Works; within the Source form or
|
||||||
|
documentation, if provided along with the Derivative Works; or,
|
||||||
|
within a display generated by the Derivative Works, if and
|
||||||
|
wherever such third-party notices normally appear. The contents
|
||||||
|
of the NOTICE file are for informational purposes only and
|
||||||
|
do not modify the License. You may add Your own attribution
|
||||||
|
notices within Derivative Works that You distribute, alongside
|
||||||
|
or as an addendum to the NOTICE text from the Work, provided
|
||||||
|
that such additional attribution notices cannot be construed
|
||||||
|
as modifying the License.
|
||||||
|
|
||||||
|
You may add Your own copyright statement to Your modifications and
|
||||||
|
may provide additional or different license terms and conditions
|
||||||
|
for use, reproduction, or distribution of Your modifications, or
|
||||||
|
for any such Derivative Works as a whole, provided Your use,
|
||||||
|
reproduction, and distribution of the Work otherwise complies with
|
||||||
|
the conditions stated in this License.
|
||||||
|
|
||||||
|
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||||
|
any Contribution intentionally submitted for inclusion in the Work
|
||||||
|
by You to the Licensor shall be under the terms and conditions of
|
||||||
|
this License, without any additional terms or conditions.
|
||||||
|
Notwithstanding the above, nothing herein shall supersede or modify
|
||||||
|
the terms of any separate license agreement you may have executed
|
||||||
|
with Licensor regarding such Contributions.
|
||||||
|
|
||||||
|
6. Trademarks. This License does not grant permission to use the trade
|
||||||
|
names, trademarks, service marks, or product names of the Licensor,
|
||||||
|
except as required for reasonable and customary use in describing the
|
||||||
|
origin of the Work and reproducing the content of the NOTICE file.
|
||||||
|
|
||||||
|
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||||
|
agreed to in writing, Licensor provides the Work (and each
|
||||||
|
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||||
|
implied, including, without limitation, any warranties or conditions
|
||||||
|
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||||
|
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||||
|
appropriateness of using or redistributing the Work and assume any
|
||||||
|
risks associated with Your exercise of permissions under this License.
|
||||||
|
|
||||||
|
8. Limitation of Liability. In no event and under no legal theory,
|
||||||
|
whether in tort (including negligence), contract, or otherwise,
|
||||||
|
unless required by applicable law (such as deliberate and grossly
|
||||||
|
negligent acts) or agreed to in writing, shall any Contributor be
|
||||||
|
liable to You for damages, including any direct, indirect, special,
|
||||||
|
incidental, or consequential damages of any character arising as a
|
||||||
|
result of this License or out of the use or inability to use the
|
||||||
|
Work (including but not limited to damages for loss of goodwill,
|
||||||
|
work stoppage, computer failure or malfunction, or any and all
|
||||||
|
other commercial damages or losses), even if such Contributor
|
||||||
|
has been advised of the possibility of such damages.
|
||||||
|
|
||||||
|
9. Accepting Warranty or Additional Liability. While redistributing
|
||||||
|
the Work or Derivative Works thereof, You may choose to offer,
|
||||||
|
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||||
|
or other liability obligations and/or rights consistent with this
|
||||||
|
License. However, in accepting such obligations, You may act only
|
||||||
|
on Your own behalf and on Your sole responsibility, not on behalf
|
||||||
|
of any other Contributor, and only if You agree to indemnify,
|
||||||
|
defend, and hold each Contributor harmless for any liability
|
||||||
|
incurred by, or claims asserted against, such Contributor by reason
|
||||||
|
of your accepting any such warranty or additional liability.
|
||||||
|
|
||||||
|
END OF TERMS AND CONDITIONS
|
||||||
|
|
||||||
|
APPENDIX: How to apply the Apache License to your work.
|
||||||
|
|
||||||
|
To apply the Apache License to your work, attach the following
|
||||||
|
boilerplate notice, with the fields enclosed by brackets "[]"
|
||||||
|
replaced with your own identifying information. (Don't include
|
||||||
|
the brackets!) The text should be enclosed in the appropriate
|
||||||
|
comment syntax for the file format. We also recommend that a
|
||||||
|
file or class name and description of purpose be included on the
|
||||||
|
same "printed page" as the copyright notice for easier
|
||||||
|
identification within third-party archives.
|
||||||
|
|
||||||
|
Copyright [yyyy] [name of copyright owner]
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
23
2sem_spi/LICENSE-MIT
Normal file
23
2sem_spi/LICENSE-MIT
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
Permission is hereby granted, free of charge, to any
|
||||||
|
person obtaining a copy of this software and associated
|
||||||
|
documentation files (the "Software"), to deal in the
|
||||||
|
Software without restriction, including without
|
||||||
|
limitation the rights to use, copy, modify, merge,
|
||||||
|
publish, distribute, sublicense, and/or sell copies of
|
||||||
|
the Software, and to permit persons to whom the Software
|
||||||
|
is furnished to do so, subject to the following
|
||||||
|
conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice
|
||||||
|
shall be included in all copies or substantial portions
|
||||||
|
of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
|
||||||
|
ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
|
||||||
|
TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
|
||||||
|
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
|
||||||
|
SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
||||||
|
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||||
|
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
|
||||||
|
IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||||
|
DEALINGS IN THE SOFTWARE.
|
||||||
23
2sem_spi/Makefile
Normal file
23
2sem_spi/Makefile
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
TARGET = thumbv8m.main-none-eabihf
|
||||||
|
CHIP = STM32U575ZI
|
||||||
|
BIN = stm32u5-blinky
|
||||||
|
MODE ?= release
|
||||||
|
TARGET_DIR = target/$(TARGET)/$(MODE)
|
||||||
|
ELF = $(TARGET_DIR)/$(BIN)
|
||||||
|
PROBE = probe-rs
|
||||||
|
|
||||||
|
.PHONY: all build flash clean empty
|
||||||
|
|
||||||
|
all: build
|
||||||
|
|
||||||
|
build:
|
||||||
|
cargo build --$(MODE)
|
||||||
|
|
||||||
|
flash: build
|
||||||
|
$(PROBE) run --chip $(CHIP) $(ELF)
|
||||||
|
|
||||||
|
empty:
|
||||||
|
$(PROBE) erase --chip $(CHIP)
|
||||||
|
|
||||||
|
clean:
|
||||||
|
cargo clean
|
||||||
232
2sem_spi/README.md
Normal file
232
2sem_spi/README.md
Normal file
@@ -0,0 +1,232 @@
|
|||||||
|
# `app-template`
|
||||||
|
|
||||||
|
> Quickly set up a [`probe-rs`] + [`defmt`] + [`flip-link`] embedded project
|
||||||
|
|
||||||
|
[`probe-rs`]: https://crates.io/crates/probe-rs
|
||||||
|
[`defmt`]: https://github.com/knurling-rs/defmt
|
||||||
|
[`flip-link`]: https://github.com/knurling-rs/flip-link
|
||||||
|
|
||||||
|
## Dependencies
|
||||||
|
|
||||||
|
### 1. `flip-link`:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
cargo install flip-link
|
||||||
|
```
|
||||||
|
|
||||||
|
### 2. `probe-rs`:
|
||||||
|
|
||||||
|
Install probe-rs by following the instructions at <https://probe.rs/docs/getting-started/installation/>.
|
||||||
|
|
||||||
|
### 3. [`cargo-generate`]:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
cargo install cargo-generate
|
||||||
|
```
|
||||||
|
|
||||||
|
[`cargo-generate`]: https://crates.io/crates/cargo-generate
|
||||||
|
|
||||||
|
> *Note:* You can also just clone this repository instead of using `cargo-generate`, but this involves additional manual adjustments.
|
||||||
|
|
||||||
|
## Setup
|
||||||
|
|
||||||
|
### 1. Initialize the project template
|
||||||
|
|
||||||
|
```bash
|
||||||
|
cargo generate \
|
||||||
|
--git https://github.com/knurling-rs/app-template \
|
||||||
|
--branch main \
|
||||||
|
--name my-app
|
||||||
|
```
|
||||||
|
|
||||||
|
If you look into your new `my-app` folder, you'll find that there are a few `TODO`s in the files marking the properties you need to set.
|
||||||
|
|
||||||
|
Let's walk through them together now.
|
||||||
|
|
||||||
|
### 2. Set `probe-rs` chip
|
||||||
|
|
||||||
|
Pick a chip from ` probe-rs chip list` and enter it into `.cargo/config.toml`.
|
||||||
|
|
||||||
|
If, for example, you have a nRF52840 Development Kit as used in one of [our exercises], replace `{{chip}}` with `nRF52840_xxAA`.
|
||||||
|
|
||||||
|
[our workshops]: https://rust-exercises.ferrous-systems.com
|
||||||
|
|
||||||
|
```diff
|
||||||
|
# .cargo/config.toml
|
||||||
|
-runner = ["probe-rs", "run", "--chip", "$CHIP", "--log-format=oneline"]
|
||||||
|
+runner = ["probe-rs", "run", "--chip", "nRF52840_xxAA", "--log-format=oneline"]
|
||||||
|
```
|
||||||
|
|
||||||
|
### 3. Adjust the compilation target
|
||||||
|
|
||||||
|
In `.cargo/config.toml`, pick the right compilation target for your board.
|
||||||
|
|
||||||
|
```diff
|
||||||
|
# .cargo/config.toml
|
||||||
|
[build]
|
||||||
|
-target = "thumbv6m-none-eabi" # Cortex-M0 and Cortex-M0+
|
||||||
|
-# target = "thumbv7m-none-eabi" # Cortex-M3
|
||||||
|
-# target = "thumbv7em-none-eabi" # Cortex-M4 and Cortex-M7 (no FPU)
|
||||||
|
-# target = "thumbv7em-none-eabihf" # Cortex-M4F and Cortex-M7F (with FPU)
|
||||||
|
+target = "thumbv7em-none-eabihf" # Cortex-M4F (with FPU)
|
||||||
|
```
|
||||||
|
|
||||||
|
Add the target with `rustup`.
|
||||||
|
|
||||||
|
```bash
|
||||||
|
rustup target add thumbv7em-none-eabihf
|
||||||
|
```
|
||||||
|
|
||||||
|
### 4. Add a HAL as a dependency
|
||||||
|
|
||||||
|
In `Cargo.toml`, list the Hardware Abstraction Layer (HAL) for your board as a dependency.
|
||||||
|
|
||||||
|
For the nRF52840 you'll want to use the [`nrf52840-hal`].
|
||||||
|
|
||||||
|
[`nrf52840-hal`]: https://crates.io/crates/nrf52840-hal
|
||||||
|
|
||||||
|
```diff
|
||||||
|
# Cargo.toml
|
||||||
|
[dependencies]
|
||||||
|
-# some-hal = "1.2.3"
|
||||||
|
+nrf52840-hal = "0.14.0"
|
||||||
|
```
|
||||||
|
|
||||||
|
⚠️ Note for RP2040 users ⚠️
|
||||||
|
|
||||||
|
You will need to not just specify the `rp-hal` HAL, but a BSP (board support crate) which includes a second stage bootloader. Please find a list of available BSPs [here](https://github.com/rp-rs/rp-hal-boards#packages).
|
||||||
|
|
||||||
|
### 5. Import your HAL
|
||||||
|
|
||||||
|
Now that you have selected a HAL, fix the HAL import in `src/lib.rs`
|
||||||
|
|
||||||
|
```diff
|
||||||
|
// my-app/src/lib.rs
|
||||||
|
-// use some_hal as _; // memory layout
|
||||||
|
+use nrf52840_hal as _; // memory layout
|
||||||
|
```
|
||||||
|
|
||||||
|
### (6. Get a linker script)
|
||||||
|
|
||||||
|
Some HAL crates require that you manually copy over a file called `memory.x` from the HAL to the root of your project. For nrf52840-hal, this is done automatically so no action is needed. For other HAL crates, see their documentation on where to find an example file.
|
||||||
|
|
||||||
|
The `memory.x` file should look something like:
|
||||||
|
|
||||||
|
```text
|
||||||
|
MEMORY
|
||||||
|
{
|
||||||
|
FLASH : ORIGIN = 0x00000000, LENGTH = 1024K
|
||||||
|
RAM : ORIGIN = 0x20000000, LENGTH = 256K
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
The `memory.x` file is included in the `cortex-m-rt` linker script `link.x`, and so `link.x` is the one you should tell `rustc` to use (see the `.cargo/config.toml` file where we do that).
|
||||||
|
|
||||||
|
### 7. Run!
|
||||||
|
|
||||||
|
You are now all set to `cargo-run` your first `defmt`-powered application!
|
||||||
|
There are some examples in the `src/bin` directory.
|
||||||
|
|
||||||
|
Start by `cargo run`-ning `my-app/src/bin/hello.rs`:
|
||||||
|
|
||||||
|
```console
|
||||||
|
$ # `rb` is an alias for `run --bin`
|
||||||
|
$ cargo rb hello
|
||||||
|
Finished `dev` profile [optimized + debuginfo] target(s) in 0.01s
|
||||||
|
Running `probe-rs run --chip nrf52840_xxaa --log-format=oneline target/thumbv6m-none-eabi/debug/hello`
|
||||||
|
Erasing ✔ 100% [####################] 8.00 KiB @ 15.79 KiB/s (took 1s)
|
||||||
|
Programming ✔ 100% [####################] 8.00 KiB @ 13.19 KiB/s (took 1s) Finished in 1.11s
|
||||||
|
Hello, world!
|
||||||
|
|
||||||
|
$ echo $?
|
||||||
|
0
|
||||||
|
```
|
||||||
|
|
||||||
|
If you're running out of memory (`flip-link` bails with an overflow error), you can decrease the size of the device memory buffer by setting the `DEFMT_RTT_BUFFER_SIZE` environment variable. The default value is 1024 bytes, and powers of two should be used for optimal performance:
|
||||||
|
|
||||||
|
```console
|
||||||
|
$ DEFMT_RTT_BUFFER_SIZE=64 cargo rb hello
|
||||||
|
```
|
||||||
|
|
||||||
|
### (8. Set `rust-analyzer.linkedProjects`)
|
||||||
|
|
||||||
|
If you are using [rust-analyzer] with VS Code for IDE-like features you can add following configuration to your `.vscode/settings.json` to make it work transparently across workspaces. Find the details of this option in the [RA docs].
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"rust-analyzer.linkedProjects": [
|
||||||
|
"Cargo.toml",
|
||||||
|
"firmware/Cargo.toml",
|
||||||
|
]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
[RA docs]: https://rust-analyzer.github.io/manual.html#configuration
|
||||||
|
[rust-analyzer]: https://rust-analyzer.github.io/
|
||||||
|
|
||||||
|
## Running tests
|
||||||
|
|
||||||
|
The template comes configured for running unit tests and integration tests on the target.
|
||||||
|
|
||||||
|
Unit tests reside in the library crate and can test private API; the initial set of unit tests are in `src/lib.rs`.
|
||||||
|
`cargo test --lib` will run those unit tests.
|
||||||
|
|
||||||
|
```console
|
||||||
|
$ cargo test --lib
|
||||||
|
Compiling example v0.1.0 (./knurling-rs/example)
|
||||||
|
Finished `test` profile [optimized + debuginfo] target(s) in 0.15s
|
||||||
|
Running unittests src/lib.rs (target/thumbv6m-none-eabi/debug/deps/example-2b0d0e25d141bf57)
|
||||||
|
Erasing ✔ 100% [####################] 8.00 KiB @ 15.99 KiB/s (took 1s)
|
||||||
|
Programming ✔ 100% [####################] 8.00 KiB @ 13.33 KiB/s (took 1s) Finished in 1.10s
|
||||||
|
(1/1) running `it_works`...
|
||||||
|
all tests passed!
|
||||||
|
```
|
||||||
|
|
||||||
|
Integration tests reside in the `tests` directory; the initial set of integration tests are in `tests/integration.rs`.
|
||||||
|
`cargo test --test integration` will run those integration tests.
|
||||||
|
Note that the argument of the `--test` flag must match the name of the test file in the `tests` directory.
|
||||||
|
|
||||||
|
```console
|
||||||
|
$ cargo test --test integration
|
||||||
|
Compiling example v0.1.0 (./knurling-rs/example)
|
||||||
|
Finished `test` profile [optimized + debuginfo] target(s) in 0.10s
|
||||||
|
Running tests/integration.rs (target/thumbv6m-none-eabi/debug/deps/integration-aaaff41151f6a722)
|
||||||
|
Erasing ✔ 100% [####################] 8.00 KiB @ 16.03 KiB/s (took 0s)
|
||||||
|
Programming ✔ 100% [####################] 8.00 KiB @ 13.19 KiB/s (took 1s) Finished in 1.11s
|
||||||
|
(1/1) running `it_works`...
|
||||||
|
all tests passed!
|
||||||
|
```
|
||||||
|
|
||||||
|
Note that to add a new test file to the `tests` directory you also need to add a new `[[test]]` section to `Cargo.toml`.
|
||||||
|
|
||||||
|
To run all the tests via `cargo test` the tests need to be explicitly disabled for all the existing binary targets.
|
||||||
|
See `Cargo.toml` for details on how to do this.
|
||||||
|
|
||||||
|
## Support
|
||||||
|
|
||||||
|
`app-template` is part of the [Knurling] project, [Ferrous Systems]' effort at
|
||||||
|
improving tooling used to develop for embedded systems.
|
||||||
|
|
||||||
|
If you think that our work is useful, consider sponsoring it via [GitHub
|
||||||
|
Sponsors].
|
||||||
|
|
||||||
|
## License
|
||||||
|
|
||||||
|
Licensed under either of
|
||||||
|
|
||||||
|
- Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0)
|
||||||
|
|
||||||
|
- MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)
|
||||||
|
|
||||||
|
at your option.
|
||||||
|
|
||||||
|
### Contribution
|
||||||
|
|
||||||
|
Unless you explicitly state otherwise, any contribution intentionally submitted
|
||||||
|
for inclusion in the work by you, as defined in the Apache-2.0 license, shall be
|
||||||
|
licensed as above, without any additional terms or conditions.
|
||||||
|
|
||||||
|
[Knurling]: https://knurling.ferrous-systems.com
|
||||||
|
[Ferrous Systems]: https://ferrous-systems.com/
|
||||||
|
[GitHub Sponsors]: https://github.com/sponsors/knurling-rs
|
||||||
50
2sem_spi/src/bin/main.rs
Normal file
50
2sem_spi/src/bin/main.rs
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
// src/bin/main.rs
|
||||||
|
#![no_std]
|
||||||
|
#![no_main]
|
||||||
|
|
||||||
|
use defmt::*;
|
||||||
|
use embassy_executor::Spawner;
|
||||||
|
use embassy_stm32::spi::{Config, Spi};
|
||||||
|
use embassy_stm32::gpio::{Level, Speed, Output};
|
||||||
|
use embassy_time::{Duration, Timer};
|
||||||
|
use {defmt_rtt as _, panic_probe as _};
|
||||||
|
|
||||||
|
#[embassy_executor::main]
|
||||||
|
async fn main(_spawner: Spawner) {
|
||||||
|
info!("tititi");
|
||||||
|
|
||||||
|
let p = embassy_stm32::init(Default::default());
|
||||||
|
|
||||||
|
let mut spi = Spi::new_blocking(p.SPI1, p.PB3, p.PB5, p.PB4, Config::default());
|
||||||
|
let mut cs = Output::new(p.PA4, Level::High, Speed::VeryHigh);
|
||||||
|
|
||||||
|
cs.set_low();
|
||||||
|
let _ = spi.blocking_write(&[0x01u8, 0x79u8]);
|
||||||
|
cs.set_high();
|
||||||
|
|
||||||
|
loop {
|
||||||
|
let mut data = [0u8; 10];
|
||||||
|
|
||||||
|
data[0] = 0xA4;
|
||||||
|
cs.set_low();
|
||||||
|
let result = spi.blocking_transfer_in_place(&mut data);
|
||||||
|
cs.set_high();
|
||||||
|
|
||||||
|
match result {
|
||||||
|
Ok(_) => {
|
||||||
|
let raw = &data[1..];
|
||||||
|
|
||||||
|
// 1. Correct 24-bit Signed Conversion (Minimal change)
|
||||||
|
let x = (((raw[0] as i32) << 24) | ((raw[1] as i32) << 16) | ((raw[2] as i32) << 8)) >> 8;
|
||||||
|
let y = (((raw[3] as i32) << 24) | ((raw[4] as i32) << 16) | ((raw[5] as i32) << 8)) >> 8;
|
||||||
|
let z = (((raw[6] as i32) << 24) | ((raw[7] as i32) << 16) | ((raw[8] as i32) << 8)) >> 8;
|
||||||
|
|
||||||
|
// 2. Display in Relevant Decimal Format
|
||||||
|
info!("X: {} Y: {} Z: {}", x, y, z);
|
||||||
|
}
|
||||||
|
Err(e) => error!("SPI error: {:?}", e),
|
||||||
|
}
|
||||||
|
|
||||||
|
Timer::after(Duration::from_millis(10)).await;
|
||||||
|
}
|
||||||
|
}
|
||||||
1
2sem_spi/src/lib.rs
Normal file
1
2sem_spi/src/lib.rs
Normal file
@@ -0,0 +1 @@
|
|||||||
|
#![no_std]
|
||||||
16
2sem_spi/tests/integration.rs
Normal file
16
2sem_spi/tests/integration.rs
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
#![no_std]
|
||||||
|
#![no_main]
|
||||||
|
|
||||||
|
use stm32u5_blinky as _; // memory layout + panic handler
|
||||||
|
|
||||||
|
// See https://crates.io/crates/defmt-test/0.3.0 for more documentation (e.g. about the 'state'
|
||||||
|
// feature)
|
||||||
|
#[defmt_test::tests]
|
||||||
|
mod tests {
|
||||||
|
use defmt::assert;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn it_works() {
|
||||||
|
assert!(true)
|
||||||
|
}
|
||||||
|
}
|
||||||
15
2sem_uart_flow_ctrl/.cargo/config.toml
Normal file
15
2sem_uart_flow_ctrl/.cargo/config.toml
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
[build]
|
||||||
|
target = "thumbv8m.main-none-eabihf"
|
||||||
|
|
||||||
|
[target.thumbv8m.main-none-eabihf]
|
||||||
|
runner = "probe-rs run --chip STM32U575ZITxQ"
|
||||||
|
|
||||||
|
rustflags = [
|
||||||
|
"-C", "linker=rust-lld",
|
||||||
|
"-C", "link-arg=-Tlink.x",
|
||||||
|
"-C", "link-arg=-Tdefmt.x",
|
||||||
|
"-C", "link-arg=--nmagic",
|
||||||
|
]
|
||||||
|
|
||||||
|
[package.metadata.cargo-flash]
|
||||||
|
chip = "STM32U575ZIT"
|
||||||
1
2sem_uart_flow_ctrl/.gitignore
vendored
Normal file
1
2sem_uart_flow_ctrl/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
/target
|
||||||
1088
2sem_uart_flow_ctrl/Cargo.lock
generated
Normal file
1088
2sem_uart_flow_ctrl/Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
30
2sem_uart_flow_ctrl/Cargo.toml
Normal file
30
2sem_uart_flow_ctrl/Cargo.toml
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
[package]
|
||||||
|
authors = ["Priec <filippriec@gmail.com>"]
|
||||||
|
name = "hal_rng"
|
||||||
|
edition = "2024"
|
||||||
|
version = "0.1.0"
|
||||||
|
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
cortex-m = { version = "0.7.7", features = ["inline-asm", "critical-section-single-core"] }
|
||||||
|
cortex-m-rt = "0.7.5"
|
||||||
|
panic-halt = "1.0.0"
|
||||||
|
|
||||||
|
embassy-executor = { version = "0.9.1", features = ["arch-cortex-m", "executor-thread"] }
|
||||||
|
embassy-futures = "0.1.2"
|
||||||
|
embassy-stm32 = { version = "0.4.0", features = ["unstable-pac", "stm32u575zi", "time-driver-any", "memory-x", "defmt"] }
|
||||||
|
embassy-sync = "0.7.2"
|
||||||
|
embassy-time = { version = "0.5.0", features = ["tick-hz-32_768"] }
|
||||||
|
embassy-usb = "0.5.1"
|
||||||
|
|
||||||
|
embedded-hal = "1.0.0"
|
||||||
|
embedded-graphics = "0.8.1"
|
||||||
|
heapless = { version = "0.9.1", default-features = false }
|
||||||
|
micromath = "2.1.0"
|
||||||
|
tinybmp = "0.6.0"
|
||||||
|
panic-probe = { version = "1.0.0", features = ["defmt"] }
|
||||||
|
defmt-rtt = "1.1.0"
|
||||||
|
defmt = "1.0.1"
|
||||||
|
static_cell = "2.1.1"
|
||||||
|
embedded-io-async = "0.6.0"
|
||||||
|
embedded-io = "0.6.1"
|
||||||
201
2sem_uart_flow_ctrl/LICENSE-APACHE
Normal file
201
2sem_uart_flow_ctrl/LICENSE-APACHE
Normal file
@@ -0,0 +1,201 @@
|
|||||||
|
Apache License
|
||||||
|
Version 2.0, January 2004
|
||||||
|
http://www.apache.org/licenses/
|
||||||
|
|
||||||
|
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||||
|
|
||||||
|
1. Definitions.
|
||||||
|
|
||||||
|
"License" shall mean the terms and conditions for use, reproduction,
|
||||||
|
and distribution as defined by Sections 1 through 9 of this document.
|
||||||
|
|
||||||
|
"Licensor" shall mean the copyright owner or entity authorized by
|
||||||
|
the copyright owner that is granting the License.
|
||||||
|
|
||||||
|
"Legal Entity" shall mean the union of the acting entity and all
|
||||||
|
other entities that control, are controlled by, or are under common
|
||||||
|
control with that entity. For the purposes of this definition,
|
||||||
|
"control" means (i) the power, direct or indirect, to cause the
|
||||||
|
direction or management of such entity, whether by contract or
|
||||||
|
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||||
|
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||||
|
|
||||||
|
"You" (or "Your") shall mean an individual or Legal Entity
|
||||||
|
exercising permissions granted by this License.
|
||||||
|
|
||||||
|
"Source" form shall mean the preferred form for making modifications,
|
||||||
|
including but not limited to software source code, documentation
|
||||||
|
source, and configuration files.
|
||||||
|
|
||||||
|
"Object" form shall mean any form resulting from mechanical
|
||||||
|
transformation or translation of a Source form, including but
|
||||||
|
not limited to compiled object code, generated documentation,
|
||||||
|
and conversions to other media types.
|
||||||
|
|
||||||
|
"Work" shall mean the work of authorship, whether in Source or
|
||||||
|
Object form, made available under the License, as indicated by a
|
||||||
|
copyright notice that is included in or attached to the work
|
||||||
|
(an example is provided in the Appendix below).
|
||||||
|
|
||||||
|
"Derivative Works" shall mean any work, whether in Source or Object
|
||||||
|
form, that is based on (or derived from) the Work and for which the
|
||||||
|
editorial revisions, annotations, elaborations, or other modifications
|
||||||
|
represent, as a whole, an original work of authorship. For the purposes
|
||||||
|
of this License, Derivative Works shall not include works that remain
|
||||||
|
separable from, or merely link (or bind by name) to the interfaces of,
|
||||||
|
the Work and Derivative Works thereof.
|
||||||
|
|
||||||
|
"Contribution" shall mean any work of authorship, including
|
||||||
|
the original version of the Work and any modifications or additions
|
||||||
|
to that Work or Derivative Works thereof, that is intentionally
|
||||||
|
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||||
|
or by an individual or Legal Entity authorized to submit on behalf of
|
||||||
|
the copyright owner. For the purposes of this definition, "submitted"
|
||||||
|
means any form of electronic, verbal, or written communication sent
|
||||||
|
to the Licensor or its representatives, including but not limited to
|
||||||
|
communication on electronic mailing lists, source code control systems,
|
||||||
|
and issue tracking systems that are managed by, or on behalf of, the
|
||||||
|
Licensor for the purpose of discussing and improving the Work, but
|
||||||
|
excluding communication that is conspicuously marked or otherwise
|
||||||
|
designated in writing by the copyright owner as "Not a Contribution."
|
||||||
|
|
||||||
|
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||||
|
on behalf of whom a Contribution has been received by Licensor and
|
||||||
|
subsequently incorporated within the Work.
|
||||||
|
|
||||||
|
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||||
|
this License, each Contributor hereby grants to You a perpetual,
|
||||||
|
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||||
|
copyright license to reproduce, prepare Derivative Works of,
|
||||||
|
publicly display, publicly perform, sublicense, and distribute the
|
||||||
|
Work and such Derivative Works in Source or Object form.
|
||||||
|
|
||||||
|
3. Grant of Patent License. Subject to the terms and conditions of
|
||||||
|
this License, each Contributor hereby grants to You a perpetual,
|
||||||
|
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||||
|
(except as stated in this section) patent license to make, have made,
|
||||||
|
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||||
|
where such license applies only to those patent claims licensable
|
||||||
|
by such Contributor that are necessarily infringed by their
|
||||||
|
Contribution(s) alone or by combination of their Contribution(s)
|
||||||
|
with the Work to which such Contribution(s) was submitted. If You
|
||||||
|
institute patent litigation against any entity (including a
|
||||||
|
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||||
|
or a Contribution incorporated within the Work constitutes direct
|
||||||
|
or contributory patent infringement, then any patent licenses
|
||||||
|
granted to You under this License for that Work shall terminate
|
||||||
|
as of the date such litigation is filed.
|
||||||
|
|
||||||
|
4. Redistribution. You may reproduce and distribute copies of the
|
||||||
|
Work or Derivative Works thereof in any medium, with or without
|
||||||
|
modifications, and in Source or Object form, provided that You
|
||||||
|
meet the following conditions:
|
||||||
|
|
||||||
|
(a) You must give any other recipients of the Work or
|
||||||
|
Derivative Works a copy of this License; and
|
||||||
|
|
||||||
|
(b) You must cause any modified files to carry prominent notices
|
||||||
|
stating that You changed the files; and
|
||||||
|
|
||||||
|
(c) You must retain, in the Source form of any Derivative Works
|
||||||
|
that You distribute, all copyright, patent, trademark, and
|
||||||
|
attribution notices from the Source form of the Work,
|
||||||
|
excluding those notices that do not pertain to any part of
|
||||||
|
the Derivative Works; and
|
||||||
|
|
||||||
|
(d) If the Work includes a "NOTICE" text file as part of its
|
||||||
|
distribution, then any Derivative Works that You distribute must
|
||||||
|
include a readable copy of the attribution notices contained
|
||||||
|
within such NOTICE file, excluding those notices that do not
|
||||||
|
pertain to any part of the Derivative Works, in at least one
|
||||||
|
of the following places: within a NOTICE text file distributed
|
||||||
|
as part of the Derivative Works; within the Source form or
|
||||||
|
documentation, if provided along with the Derivative Works; or,
|
||||||
|
within a display generated by the Derivative Works, if and
|
||||||
|
wherever such third-party notices normally appear. The contents
|
||||||
|
of the NOTICE file are for informational purposes only and
|
||||||
|
do not modify the License. You may add Your own attribution
|
||||||
|
notices within Derivative Works that You distribute, alongside
|
||||||
|
or as an addendum to the NOTICE text from the Work, provided
|
||||||
|
that such additional attribution notices cannot be construed
|
||||||
|
as modifying the License.
|
||||||
|
|
||||||
|
You may add Your own copyright statement to Your modifications and
|
||||||
|
may provide additional or different license terms and conditions
|
||||||
|
for use, reproduction, or distribution of Your modifications, or
|
||||||
|
for any such Derivative Works as a whole, provided Your use,
|
||||||
|
reproduction, and distribution of the Work otherwise complies with
|
||||||
|
the conditions stated in this License.
|
||||||
|
|
||||||
|
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||||
|
any Contribution intentionally submitted for inclusion in the Work
|
||||||
|
by You to the Licensor shall be under the terms and conditions of
|
||||||
|
this License, without any additional terms or conditions.
|
||||||
|
Notwithstanding the above, nothing herein shall supersede or modify
|
||||||
|
the terms of any separate license agreement you may have executed
|
||||||
|
with Licensor regarding such Contributions.
|
||||||
|
|
||||||
|
6. Trademarks. This License does not grant permission to use the trade
|
||||||
|
names, trademarks, service marks, or product names of the Licensor,
|
||||||
|
except as required for reasonable and customary use in describing the
|
||||||
|
origin of the Work and reproducing the content of the NOTICE file.
|
||||||
|
|
||||||
|
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||||
|
agreed to in writing, Licensor provides the Work (and each
|
||||||
|
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||||
|
implied, including, without limitation, any warranties or conditions
|
||||||
|
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||||
|
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||||
|
appropriateness of using or redistributing the Work and assume any
|
||||||
|
risks associated with Your exercise of permissions under this License.
|
||||||
|
|
||||||
|
8. Limitation of Liability. In no event and under no legal theory,
|
||||||
|
whether in tort (including negligence), contract, or otherwise,
|
||||||
|
unless required by applicable law (such as deliberate and grossly
|
||||||
|
negligent acts) or agreed to in writing, shall any Contributor be
|
||||||
|
liable to You for damages, including any direct, indirect, special,
|
||||||
|
incidental, or consequential damages of any character arising as a
|
||||||
|
result of this License or out of the use or inability to use the
|
||||||
|
Work (including but not limited to damages for loss of goodwill,
|
||||||
|
work stoppage, computer failure or malfunction, or any and all
|
||||||
|
other commercial damages or losses), even if such Contributor
|
||||||
|
has been advised of the possibility of such damages.
|
||||||
|
|
||||||
|
9. Accepting Warranty or Additional Liability. While redistributing
|
||||||
|
the Work or Derivative Works thereof, You may choose to offer,
|
||||||
|
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||||
|
or other liability obligations and/or rights consistent with this
|
||||||
|
License. However, in accepting such obligations, You may act only
|
||||||
|
on Your own behalf and on Your sole responsibility, not on behalf
|
||||||
|
of any other Contributor, and only if You agree to indemnify,
|
||||||
|
defend, and hold each Contributor harmless for any liability
|
||||||
|
incurred by, or claims asserted against, such Contributor by reason
|
||||||
|
of your accepting any such warranty or additional liability.
|
||||||
|
|
||||||
|
END OF TERMS AND CONDITIONS
|
||||||
|
|
||||||
|
APPENDIX: How to apply the Apache License to your work.
|
||||||
|
|
||||||
|
To apply the Apache License to your work, attach the following
|
||||||
|
boilerplate notice, with the fields enclosed by brackets "[]"
|
||||||
|
replaced with your own identifying information. (Don't include
|
||||||
|
the brackets!) The text should be enclosed in the appropriate
|
||||||
|
comment syntax for the file format. We also recommend that a
|
||||||
|
file or class name and description of purpose be included on the
|
||||||
|
same "printed page" as the copyright notice for easier
|
||||||
|
identification within third-party archives.
|
||||||
|
|
||||||
|
Copyright [yyyy] [name of copyright owner]
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
23
2sem_uart_flow_ctrl/LICENSE-MIT
Normal file
23
2sem_uart_flow_ctrl/LICENSE-MIT
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
Permission is hereby granted, free of charge, to any
|
||||||
|
person obtaining a copy of this software and associated
|
||||||
|
documentation files (the "Software"), to deal in the
|
||||||
|
Software without restriction, including without
|
||||||
|
limitation the rights to use, copy, modify, merge,
|
||||||
|
publish, distribute, sublicense, and/or sell copies of
|
||||||
|
the Software, and to permit persons to whom the Software
|
||||||
|
is furnished to do so, subject to the following
|
||||||
|
conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice
|
||||||
|
shall be included in all copies or substantial portions
|
||||||
|
of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
|
||||||
|
ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
|
||||||
|
TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
|
||||||
|
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
|
||||||
|
SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
||||||
|
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||||
|
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
|
||||||
|
IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||||
|
DEALINGS IN THE SOFTWARE.
|
||||||
23
2sem_uart_flow_ctrl/Makefile
Normal file
23
2sem_uart_flow_ctrl/Makefile
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
TARGET = thumbv8m.main-none-eabihf
|
||||||
|
CHIP = STM32U575ZI
|
||||||
|
BIN = stm32u5-blinky
|
||||||
|
MODE ?= release
|
||||||
|
TARGET_DIR = target/$(TARGET)/$(MODE)
|
||||||
|
ELF = $(TARGET_DIR)/$(BIN)
|
||||||
|
PROBE = probe-rs
|
||||||
|
|
||||||
|
.PHONY: all build flash clean empty
|
||||||
|
|
||||||
|
all: build
|
||||||
|
|
||||||
|
build:
|
||||||
|
cargo build --$(MODE)
|
||||||
|
|
||||||
|
flash: build
|
||||||
|
$(PROBE) run --chip $(CHIP) $(ELF)
|
||||||
|
|
||||||
|
empty:
|
||||||
|
$(PROBE) erase --chip $(CHIP)
|
||||||
|
|
||||||
|
clean:
|
||||||
|
cargo clean
|
||||||
232
2sem_uart_flow_ctrl/README.md
Normal file
232
2sem_uart_flow_ctrl/README.md
Normal file
@@ -0,0 +1,232 @@
|
|||||||
|
# `app-template`
|
||||||
|
|
||||||
|
> Quickly set up a [`probe-rs`] + [`defmt`] + [`flip-link`] embedded project
|
||||||
|
|
||||||
|
[`probe-rs`]: https://crates.io/crates/probe-rs
|
||||||
|
[`defmt`]: https://github.com/knurling-rs/defmt
|
||||||
|
[`flip-link`]: https://github.com/knurling-rs/flip-link
|
||||||
|
|
||||||
|
## Dependencies
|
||||||
|
|
||||||
|
### 1. `flip-link`:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
cargo install flip-link
|
||||||
|
```
|
||||||
|
|
||||||
|
### 2. `probe-rs`:
|
||||||
|
|
||||||
|
Install probe-rs by following the instructions at <https://probe.rs/docs/getting-started/installation/>.
|
||||||
|
|
||||||
|
### 3. [`cargo-generate`]:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
cargo install cargo-generate
|
||||||
|
```
|
||||||
|
|
||||||
|
[`cargo-generate`]: https://crates.io/crates/cargo-generate
|
||||||
|
|
||||||
|
> *Note:* You can also just clone this repository instead of using `cargo-generate`, but this involves additional manual adjustments.
|
||||||
|
|
||||||
|
## Setup
|
||||||
|
|
||||||
|
### 1. Initialize the project template
|
||||||
|
|
||||||
|
```bash
|
||||||
|
cargo generate \
|
||||||
|
--git https://github.com/knurling-rs/app-template \
|
||||||
|
--branch main \
|
||||||
|
--name my-app
|
||||||
|
```
|
||||||
|
|
||||||
|
If you look into your new `my-app` folder, you'll find that there are a few `TODO`s in the files marking the properties you need to set.
|
||||||
|
|
||||||
|
Let's walk through them together now.
|
||||||
|
|
||||||
|
### 2. Set `probe-rs` chip
|
||||||
|
|
||||||
|
Pick a chip from ` probe-rs chip list` and enter it into `.cargo/config.toml`.
|
||||||
|
|
||||||
|
If, for example, you have a nRF52840 Development Kit as used in one of [our exercises], replace `{{chip}}` with `nRF52840_xxAA`.
|
||||||
|
|
||||||
|
[our workshops]: https://rust-exercises.ferrous-systems.com
|
||||||
|
|
||||||
|
```diff
|
||||||
|
# .cargo/config.toml
|
||||||
|
-runner = ["probe-rs", "run", "--chip", "$CHIP", "--log-format=oneline"]
|
||||||
|
+runner = ["probe-rs", "run", "--chip", "nRF52840_xxAA", "--log-format=oneline"]
|
||||||
|
```
|
||||||
|
|
||||||
|
### 3. Adjust the compilation target
|
||||||
|
|
||||||
|
In `.cargo/config.toml`, pick the right compilation target for your board.
|
||||||
|
|
||||||
|
```diff
|
||||||
|
# .cargo/config.toml
|
||||||
|
[build]
|
||||||
|
-target = "thumbv6m-none-eabi" # Cortex-M0 and Cortex-M0+
|
||||||
|
-# target = "thumbv7m-none-eabi" # Cortex-M3
|
||||||
|
-# target = "thumbv7em-none-eabi" # Cortex-M4 and Cortex-M7 (no FPU)
|
||||||
|
-# target = "thumbv7em-none-eabihf" # Cortex-M4F and Cortex-M7F (with FPU)
|
||||||
|
+target = "thumbv7em-none-eabihf" # Cortex-M4F (with FPU)
|
||||||
|
```
|
||||||
|
|
||||||
|
Add the target with `rustup`.
|
||||||
|
|
||||||
|
```bash
|
||||||
|
rustup target add thumbv7em-none-eabihf
|
||||||
|
```
|
||||||
|
|
||||||
|
### 4. Add a HAL as a dependency
|
||||||
|
|
||||||
|
In `Cargo.toml`, list the Hardware Abstraction Layer (HAL) for your board as a dependency.
|
||||||
|
|
||||||
|
For the nRF52840 you'll want to use the [`nrf52840-hal`].
|
||||||
|
|
||||||
|
[`nrf52840-hal`]: https://crates.io/crates/nrf52840-hal
|
||||||
|
|
||||||
|
```diff
|
||||||
|
# Cargo.toml
|
||||||
|
[dependencies]
|
||||||
|
-# some-hal = "1.2.3"
|
||||||
|
+nrf52840-hal = "0.14.0"
|
||||||
|
```
|
||||||
|
|
||||||
|
⚠️ Note for RP2040 users ⚠️
|
||||||
|
|
||||||
|
You will need to not just specify the `rp-hal` HAL, but a BSP (board support crate) which includes a second stage bootloader. Please find a list of available BSPs [here](https://github.com/rp-rs/rp-hal-boards#packages).
|
||||||
|
|
||||||
|
### 5. Import your HAL
|
||||||
|
|
||||||
|
Now that you have selected a HAL, fix the HAL import in `src/lib.rs`
|
||||||
|
|
||||||
|
```diff
|
||||||
|
// my-app/src/lib.rs
|
||||||
|
-// use some_hal as _; // memory layout
|
||||||
|
+use nrf52840_hal as _; // memory layout
|
||||||
|
```
|
||||||
|
|
||||||
|
### (6. Get a linker script)
|
||||||
|
|
||||||
|
Some HAL crates require that you manually copy over a file called `memory.x` from the HAL to the root of your project. For nrf52840-hal, this is done automatically so no action is needed. For other HAL crates, see their documentation on where to find an example file.
|
||||||
|
|
||||||
|
The `memory.x` file should look something like:
|
||||||
|
|
||||||
|
```text
|
||||||
|
MEMORY
|
||||||
|
{
|
||||||
|
FLASH : ORIGIN = 0x00000000, LENGTH = 1024K
|
||||||
|
RAM : ORIGIN = 0x20000000, LENGTH = 256K
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
The `memory.x` file is included in the `cortex-m-rt` linker script `link.x`, and so `link.x` is the one you should tell `rustc` to use (see the `.cargo/config.toml` file where we do that).
|
||||||
|
|
||||||
|
### 7. Run!
|
||||||
|
|
||||||
|
You are now all set to `cargo-run` your first `defmt`-powered application!
|
||||||
|
There are some examples in the `src/bin` directory.
|
||||||
|
|
||||||
|
Start by `cargo run`-ning `my-app/src/bin/hello.rs`:
|
||||||
|
|
||||||
|
```console
|
||||||
|
$ # `rb` is an alias for `run --bin`
|
||||||
|
$ cargo rb hello
|
||||||
|
Finished `dev` profile [optimized + debuginfo] target(s) in 0.01s
|
||||||
|
Running `probe-rs run --chip nrf52840_xxaa --log-format=oneline target/thumbv6m-none-eabi/debug/hello`
|
||||||
|
Erasing ✔ 100% [####################] 8.00 KiB @ 15.79 KiB/s (took 1s)
|
||||||
|
Programming ✔ 100% [####################] 8.00 KiB @ 13.19 KiB/s (took 1s) Finished in 1.11s
|
||||||
|
Hello, world!
|
||||||
|
|
||||||
|
$ echo $?
|
||||||
|
0
|
||||||
|
```
|
||||||
|
|
||||||
|
If you're running out of memory (`flip-link` bails with an overflow error), you can decrease the size of the device memory buffer by setting the `DEFMT_RTT_BUFFER_SIZE` environment variable. The default value is 1024 bytes, and powers of two should be used for optimal performance:
|
||||||
|
|
||||||
|
```console
|
||||||
|
$ DEFMT_RTT_BUFFER_SIZE=64 cargo rb hello
|
||||||
|
```
|
||||||
|
|
||||||
|
### (8. Set `rust-analyzer.linkedProjects`)
|
||||||
|
|
||||||
|
If you are using [rust-analyzer] with VS Code for IDE-like features you can add following configuration to your `.vscode/settings.json` to make it work transparently across workspaces. Find the details of this option in the [RA docs].
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"rust-analyzer.linkedProjects": [
|
||||||
|
"Cargo.toml",
|
||||||
|
"firmware/Cargo.toml",
|
||||||
|
]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
[RA docs]: https://rust-analyzer.github.io/manual.html#configuration
|
||||||
|
[rust-analyzer]: https://rust-analyzer.github.io/
|
||||||
|
|
||||||
|
## Running tests
|
||||||
|
|
||||||
|
The template comes configured for running unit tests and integration tests on the target.
|
||||||
|
|
||||||
|
Unit tests reside in the library crate and can test private API; the initial set of unit tests are in `src/lib.rs`.
|
||||||
|
`cargo test --lib` will run those unit tests.
|
||||||
|
|
||||||
|
```console
|
||||||
|
$ cargo test --lib
|
||||||
|
Compiling example v0.1.0 (./knurling-rs/example)
|
||||||
|
Finished `test` profile [optimized + debuginfo] target(s) in 0.15s
|
||||||
|
Running unittests src/lib.rs (target/thumbv6m-none-eabi/debug/deps/example-2b0d0e25d141bf57)
|
||||||
|
Erasing ✔ 100% [####################] 8.00 KiB @ 15.99 KiB/s (took 1s)
|
||||||
|
Programming ✔ 100% [####################] 8.00 KiB @ 13.33 KiB/s (took 1s) Finished in 1.10s
|
||||||
|
(1/1) running `it_works`...
|
||||||
|
all tests passed!
|
||||||
|
```
|
||||||
|
|
||||||
|
Integration tests reside in the `tests` directory; the initial set of integration tests are in `tests/integration.rs`.
|
||||||
|
`cargo test --test integration` will run those integration tests.
|
||||||
|
Note that the argument of the `--test` flag must match the name of the test file in the `tests` directory.
|
||||||
|
|
||||||
|
```console
|
||||||
|
$ cargo test --test integration
|
||||||
|
Compiling example v0.1.0 (./knurling-rs/example)
|
||||||
|
Finished `test` profile [optimized + debuginfo] target(s) in 0.10s
|
||||||
|
Running tests/integration.rs (target/thumbv6m-none-eabi/debug/deps/integration-aaaff41151f6a722)
|
||||||
|
Erasing ✔ 100% [####################] 8.00 KiB @ 16.03 KiB/s (took 0s)
|
||||||
|
Programming ✔ 100% [####################] 8.00 KiB @ 13.19 KiB/s (took 1s) Finished in 1.11s
|
||||||
|
(1/1) running `it_works`...
|
||||||
|
all tests passed!
|
||||||
|
```
|
||||||
|
|
||||||
|
Note that to add a new test file to the `tests` directory you also need to add a new `[[test]]` section to `Cargo.toml`.
|
||||||
|
|
||||||
|
To run all the tests via `cargo test` the tests need to be explicitly disabled for all the existing binary targets.
|
||||||
|
See `Cargo.toml` for details on how to do this.
|
||||||
|
|
||||||
|
## Support
|
||||||
|
|
||||||
|
`app-template` is part of the [Knurling] project, [Ferrous Systems]' effort at
|
||||||
|
improving tooling used to develop for embedded systems.
|
||||||
|
|
||||||
|
If you think that our work is useful, consider sponsoring it via [GitHub
|
||||||
|
Sponsors].
|
||||||
|
|
||||||
|
## License
|
||||||
|
|
||||||
|
Licensed under either of
|
||||||
|
|
||||||
|
- Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0)
|
||||||
|
|
||||||
|
- MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)
|
||||||
|
|
||||||
|
at your option.
|
||||||
|
|
||||||
|
### Contribution
|
||||||
|
|
||||||
|
Unless you explicitly state otherwise, any contribution intentionally submitted
|
||||||
|
for inclusion in the work by you, as defined in the Apache-2.0 license, shall be
|
||||||
|
licensed as above, without any additional terms or conditions.
|
||||||
|
|
||||||
|
[Knurling]: https://knurling.ferrous-systems.com
|
||||||
|
[Ferrous Systems]: https://ferrous-systems.com/
|
||||||
|
[GitHub Sponsors]: https://github.com/sponsors/knurling-rs
|
||||||
126
2sem_uart_flow_ctrl/src/bin/main.rs
Normal file
126
2sem_uart_flow_ctrl/src/bin/main.rs
Normal file
@@ -0,0 +1,126 @@
|
|||||||
|
// src/bin/main.rs
|
||||||
|
#![no_std]
|
||||||
|
#![no_main]
|
||||||
|
|
||||||
|
use defmt::*;
|
||||||
|
use core::sync::atomic::{AtomicBool, Ordering};
|
||||||
|
use embassy_executor::Spawner;
|
||||||
|
use embassy_stm32::usart::{Config, Uart, UartRx, UartTx, InterruptHandler};
|
||||||
|
use embassy_stm32::peripherals::{USART1, USART3};
|
||||||
|
use embassy_stm32::bind_interrupts;
|
||||||
|
use embassy_time::{Duration, Timer};
|
||||||
|
use embassy_sync::channel::Channel;
|
||||||
|
use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex;
|
||||||
|
|
||||||
|
use {defmt_rtt as _, panic_probe as _};
|
||||||
|
|
||||||
|
bind_interrupts!(struct Irqs {
|
||||||
|
USART1 => InterruptHandler<USART1>;
|
||||||
|
USART3 => InterruptHandler<USART3>;
|
||||||
|
});
|
||||||
|
|
||||||
|
static PIPE: Channel<CriticalSectionRawMutex, u8, 64> = Channel::new();
|
||||||
|
static SEND_ALLOWED: AtomicBool = AtomicBool::new(true);
|
||||||
|
static REC_ALLOWED: AtomicBool = AtomicBool::new(true);
|
||||||
|
|
||||||
|
#[embassy_executor::main]
|
||||||
|
async fn main(spawner: Spawner) {
|
||||||
|
info!("tititititi");
|
||||||
|
|
||||||
|
let p = embassy_stm32::init(Default::default());
|
||||||
|
|
||||||
|
let config = Config::default();
|
||||||
|
let dev_usart = Uart::new(
|
||||||
|
p.USART1,
|
||||||
|
p.PA10,
|
||||||
|
p.PA9,
|
||||||
|
Irqs,
|
||||||
|
p.GPDMA1_CH0, // TX DMA
|
||||||
|
p.GPDMA1_CH1, // RX DMA
|
||||||
|
config,
|
||||||
|
).unwrap();
|
||||||
|
|
||||||
|
let uart = Uart::new(
|
||||||
|
p.USART3,
|
||||||
|
p.PD9, // RX zo zariadenia
|
||||||
|
p.PD8, // TX do zariadenia
|
||||||
|
Irqs,
|
||||||
|
p.GPDMA1_CH2,
|
||||||
|
p.GPDMA1_CH3,
|
||||||
|
config,
|
||||||
|
).unwrap();
|
||||||
|
let (mut tx, rx) = uart.split();
|
||||||
|
let (dev_tx, dev_rx) = dev_usart.split();
|
||||||
|
spawner.spawn(rx_task(rx, dev_tx)).unwrap();
|
||||||
|
spawner.spawn(toggle_rec_task()).unwrap();
|
||||||
|
|
||||||
|
let sender = PIPE.sender();
|
||||||
|
let receiver = PIPE.receiver();
|
||||||
|
|
||||||
|
info!("starting echo");
|
||||||
|
|
||||||
|
let data = b"filip\r\n";
|
||||||
|
let idle = &[0x13u8];
|
||||||
|
|
||||||
|
loop {
|
||||||
|
// rec_allowed = (start_time.elapsed().as_secs() / 2) % 2 == 0;
|
||||||
|
let current_send_allowed = SEND_ALLOWED.load(Ordering::Relaxed);
|
||||||
|
let current_rec_allowed = REC_ALLOWED.load(Ordering::Relaxed);
|
||||||
|
if current_rec_allowed == true {
|
||||||
|
if current_send_allowed {
|
||||||
|
if let Ok(stored_byte) = receiver.try_receive() {
|
||||||
|
let _ = tx.write(&[stored_byte]).await;
|
||||||
|
} else {
|
||||||
|
let _ = tx.write(data).await;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
for b in data {
|
||||||
|
let _ = sender.try_send(*b);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
let _ = tx.write(idle).await;
|
||||||
|
}
|
||||||
|
let _ = Timer::after(Duration::from_millis(100));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[embassy_executor::task]
|
||||||
|
async fn rx_task(
|
||||||
|
mut rx: UartRx<'static, embassy_stm32::mode::Async>,
|
||||||
|
mut pc_tx: UartTx<'static, embassy_stm32::mode::Async>
|
||||||
|
) {
|
||||||
|
let mut buf = [0u8; 1];
|
||||||
|
loop {
|
||||||
|
if rx.read(&mut buf).await.is_ok() {
|
||||||
|
match buf[0] {
|
||||||
|
0x11 => {
|
||||||
|
SEND_ALLOWED.store(true, Ordering::Relaxed);
|
||||||
|
info!("Vysielanie povolené");
|
||||||
|
}
|
||||||
|
0x13 => {
|
||||||
|
SEND_ALLOWED.store(false, Ordering::Relaxed);
|
||||||
|
info!("Vysielanie zakázané");
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
let _ = pc_tx.write(&buf).await;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[embassy_executor::task]
|
||||||
|
async fn toggle_rec_task() {
|
||||||
|
loop {
|
||||||
|
Timer::after(Duration::from_secs(2)).await;
|
||||||
|
// Načítame aktuálnu hodnotu a uložíme jej opak
|
||||||
|
let current = REC_ALLOWED.load(Ordering::Relaxed);
|
||||||
|
REC_ALLOWED.store(!current, Ordering::Relaxed);
|
||||||
|
|
||||||
|
if !current {
|
||||||
|
info!("REC_ALLOWED: true");
|
||||||
|
} else {
|
||||||
|
info!("REC_ALLOWED: false");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
1
2sem_uart_flow_ctrl/src/lib.rs
Normal file
1
2sem_uart_flow_ctrl/src/lib.rs
Normal file
@@ -0,0 +1 @@
|
|||||||
|
#![no_std]
|
||||||
16
2sem_uart_flow_ctrl/tests/integration.rs
Normal file
16
2sem_uart_flow_ctrl/tests/integration.rs
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
#![no_std]
|
||||||
|
#![no_main]
|
||||||
|
|
||||||
|
use stm32u5_blinky as _; // memory layout + panic handler
|
||||||
|
|
||||||
|
// See https://crates.io/crates/defmt-test/0.3.0 for more documentation (e.g. about the 'state'
|
||||||
|
// feature)
|
||||||
|
#[defmt_test::tests]
|
||||||
|
mod tests {
|
||||||
|
use defmt::assert;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn it_works() {
|
||||||
|
assert!(true)
|
||||||
|
}
|
||||||
|
}
|
||||||
37
flake.lock
generated
37
flake.lock
generated
@@ -16,9 +16,44 @@
|
|||||||
"type": "github"
|
"type": "github"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"nixpkgs_2": {
|
||||||
|
"locked": {
|
||||||
|
"lastModified": 1744536153,
|
||||||
|
"narHash": "sha256-awS2zRgF4uTwrOKwwiJcByDzDOdo3Q1rPZbiHQg/N38=",
|
||||||
|
"owner": "NixOS",
|
||||||
|
"repo": "nixpkgs",
|
||||||
|
"rev": "18dd725c29603f582cf1900e0d25f9f1063dbf11",
|
||||||
|
"type": "github"
|
||||||
|
},
|
||||||
|
"original": {
|
||||||
|
"owner": "NixOS",
|
||||||
|
"ref": "nixpkgs-unstable",
|
||||||
|
"repo": "nixpkgs",
|
||||||
|
"type": "github"
|
||||||
|
}
|
||||||
|
},
|
||||||
"root": {
|
"root": {
|
||||||
"inputs": {
|
"inputs": {
|
||||||
"nixpkgs": "nixpkgs"
|
"nixpkgs": "nixpkgs",
|
||||||
|
"rust-overlay": "rust-overlay"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"rust-overlay": {
|
||||||
|
"inputs": {
|
||||||
|
"nixpkgs": "nixpkgs_2"
|
||||||
|
},
|
||||||
|
"locked": {
|
||||||
|
"lastModified": 1777346187,
|
||||||
|
"narHash": "sha256-oVxyGjpiIsrXhWTJVUOs38fZQkLjd0nZGOY9K7Kfot8=",
|
||||||
|
"owner": "oxalica",
|
||||||
|
"repo": "rust-overlay",
|
||||||
|
"rev": "146e7bf7569b8288f24d41d806b9f584f7cfd5b5",
|
||||||
|
"type": "github"
|
||||||
|
},
|
||||||
|
"original": {
|
||||||
|
"owner": "oxalica",
|
||||||
|
"repo": "rust-overlay",
|
||||||
|
"type": "github"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|||||||
23
flake.nix
23
flake.nix
@@ -1,20 +1,26 @@
|
|||||||
{
|
{
|
||||||
description = "STM32U575ZI-Q Rust Embedded Development Environment";
|
description = "STM32U575ZI-Q Rust Embedded Development Environment";
|
||||||
|
|
||||||
inputs = { nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; };
|
inputs = {
|
||||||
|
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
|
||||||
|
rust-overlay.url = "github:oxalica/rust-overlay";
|
||||||
|
};
|
||||||
|
|
||||||
outputs = { self, nixpkgs }: let
|
outputs = { self, nixpkgs, rust-overlay}: let
|
||||||
system = "x86_64-linux";
|
system = "x86_64-linux";
|
||||||
pkgs = import nixpkgs { inherit system; };
|
pkgs = import nixpkgs {
|
||||||
|
inherit system;
|
||||||
|
overlays = [ (import rust-overlay) ];
|
||||||
|
};
|
||||||
rust = pkgs.rust-bin.stable.latest.default.override {
|
rust = pkgs.rust-bin.stable.latest.default.override {
|
||||||
targets = [ "thumbv8m.main-none-eabihf" ];
|
targets = [ "thumbv8m.main-none-eabihf" ];
|
||||||
extensions = [ "rust-src" "rustfmt" "clippy" ];
|
extensions = [ "rust-src" "rustfmt" "clippy" "rust-analyzer" ];
|
||||||
};
|
};
|
||||||
in {
|
in {
|
||||||
devShells.${system}.default = pkgs.mkShell {
|
devShells.${system}.default = pkgs.mkShell {
|
||||||
packages = with pkgs; [
|
packages = with pkgs; [
|
||||||
# Base toolchain
|
# Base toolchain
|
||||||
rustup
|
rust
|
||||||
llvm
|
llvm
|
||||||
gcc
|
gcc
|
||||||
gnumake
|
gnumake
|
||||||
@@ -40,13 +46,6 @@
|
|||||||
shellHook = ''
|
shellHook = ''
|
||||||
echo ">>> STM32 DevShell"
|
echo ">>> STM32 DevShell"
|
||||||
|
|
||||||
export PATH=${pkgs.rust-analyzer}/bin:$PATH
|
|
||||||
export PATH=$HOME/.cargo/bin:$PATH
|
|
||||||
|
|
||||||
rustup default stable
|
|
||||||
rustup component add rust-src rustfmt clippy 2>/dev/null || true
|
|
||||||
rustup target add thumbv8m.main-none-eabihf 2>/dev/null || true
|
|
||||||
|
|
||||||
export RUST_TARGET=thumbv8m.main-none-eabihf
|
export RUST_TARGET=thumbv8m.main-none-eabihf
|
||||||
export CARGO_TARGET_DIR=target
|
export CARGO_TARGET_DIR=target
|
||||||
export DEFMT_LOG=info
|
export DEFMT_LOG=info
|
||||||
|
|||||||
@@ -4,6 +4,13 @@
|
|||||||
|
|
||||||
use defmt::*;
|
use defmt::*;
|
||||||
use embassy_stm32::pac;
|
use embassy_stm32::pac;
|
||||||
|
use embassy_stm32::Config as CPUConfig;
|
||||||
|
use embassy_stm32::rcc::PllDiv;
|
||||||
|
use embassy_stm32::rcc::PllMul;
|
||||||
|
use embassy_stm32::rcc::PllPreDiv;
|
||||||
|
use embassy_stm32::rcc::PllSource;
|
||||||
|
use embassy_stm32::rcc::Pll;
|
||||||
|
use embassy_stm32::rcc::Sysclk;
|
||||||
use embassy_executor::{Spawner, task};
|
use embassy_executor::{Spawner, task};
|
||||||
use embassy_stm32::Config;
|
use embassy_stm32::Config;
|
||||||
use embassy_stm32::gpio::{Output, Level, Speed};
|
use embassy_stm32::gpio::{Output, Level, Speed};
|
||||||
@@ -20,7 +27,24 @@ use {defmt_rtt as _, panic_probe as _};
|
|||||||
#[embassy_executor::main]
|
#[embassy_executor::main]
|
||||||
async fn main(spawner: Spawner) {
|
async fn main(spawner: Spawner) {
|
||||||
info!("boot");
|
info!("boot");
|
||||||
let p = embassy_stm32::init(Config::default());
|
let mut config = CPUConfig::default();
|
||||||
|
config.rcc.hsi = true;
|
||||||
|
config.rcc.sys = Sysclk::PLL1_R;
|
||||||
|
config.rcc.pll1 = Some(Pll {
|
||||||
|
source: PllSource::HSI,
|
||||||
|
// 16 MHz / 1 × 20 / 2 = 160 MHz
|
||||||
|
prediv: PllPreDiv::DIV1,
|
||||||
|
mul: PllMul::MUL20,
|
||||||
|
divp: None,
|
||||||
|
divq: None,
|
||||||
|
divr: Some(PllDiv::DIV2),
|
||||||
|
});
|
||||||
|
config.enable_independent_io_supply = true;
|
||||||
|
config.enable_independent_analog_supply = true;
|
||||||
|
|
||||||
|
let p = embassy_stm32::init(config);
|
||||||
|
info!("boot");
|
||||||
|
// let p = embassy_stm32::init(Config::default());
|
||||||
let mut led = Output::new(p.PA3, Level::Low, Speed::Low);
|
let mut led = Output::new(p.PA3, Level::Low, Speed::Low);
|
||||||
let _led_ground = Output::new(p.PB0, Level::Low, Speed::Low);
|
let _led_ground = Output::new(p.PB0, Level::Low, Speed::Low);
|
||||||
info!("init m8");
|
info!("init m8");
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ pub const BAUD: u32 = 9_600;
|
|||||||
pub const PIPE_HW_TX_SIZE: usize = 1024;
|
pub const PIPE_HW_TX_SIZE: usize = 1024;
|
||||||
pub const PIPE_HW_RX_SIZE: usize = 1024;
|
pub const PIPE_HW_RX_SIZE: usize = 1024;
|
||||||
|
|
||||||
pub const WATCHDOG_TIMEOUT_US: u32 = 2_000_000; // 2 seconds
|
pub const WATCHDOG_TIMEOUT_US: u32 = 9_000_000; // 2 seconds
|
||||||
|
|
||||||
pub static PIPE_HW_TX: Pipe<CriticalSectionRawMutex, PIPE_HW_TX_SIZE> = Pipe::new();
|
pub static PIPE_HW_TX: Pipe<CriticalSectionRawMutex, PIPE_HW_TX_SIZE> = Pipe::new();
|
||||||
pub static PIPE_HW_RX: Pipe<CriticalSectionRawMutex, PIPE_HW_RX_SIZE> = Pipe::new();
|
pub static PIPE_HW_RX: Pipe<CriticalSectionRawMutex, PIPE_HW_RX_SIZE> = Pipe::new();
|
||||||
|
|||||||
Reference in New Issue
Block a user