Add rp2040 for controlling spindle

This commit is contained in:
Frederik Menke 2023-06-24 19:58:06 +02:00
parent 26218bcbd3
commit 5c0c975a86
14 changed files with 1039 additions and 0 deletions

28
rp2040/.cargo/config.toml Normal file
View file

@ -0,0 +1,28 @@
[target.'cfg(all(target_arch = "arm", target_os = "none"))']
# Choose a default "cargo run" tool:
# - probe-run provides flashing and defmt via a hardware debugger, and stack unwind on panic
# - elf2uf2-rs loads firmware over USB when the rp2040 is in boot mode
# - "probe-rs-cli run" is similar to probe-run but it uses the latest probe-rs lib crate
runner = "probe-run --chip RP2040"
# runner = "elf2uf2-rs -d"
# runner = "probe-rs-cli run --chip RP2040 --protocol swd"
rustflags = [
"-C", "linker=flip-link",
"-C", "link-arg=--nmagic",
"-C", "link-arg=-Tlink.x",
"-C", "link-arg=-Tdefmt.x",
# Code-size optimizations.
# trap unreachable can save a lot of space, but requires nightly compiler.
# uncomment the next line if you wish to enable it
# "-Z", "trap-unreachable=no",
"-C", "inline-threshold=5",
"-C", "no-vectorize-loops",
]
[build]
target = "thumbv6m-none-eabi"
[env]
DEFMT_LOG = "debug"

59
rp2040/.github/workflows/ci_checks.yml vendored Normal file
View file

@ -0,0 +1,59 @@
name: CI Checks
on: [push, pull_request]
env:
CARGO_TERM_COLOR: always
jobs:
building:
name: Building
continue-on-error: ${{ matrix.experimental || false }}
strategy:
matrix:
# All generated code should be running on stable now
rust: [nightly, stable]
include:
# Nightly is only for reference and allowed to fail
- rust: nightly
experimental: true
os:
# Check compilation works on common OSes
# (i.e. no path issues)
- ubuntu-latest
- macOS-latest
- windows-latest
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v3
- uses: dtolnay/rust-toolchain@master
with:
toolchain: ${{ matrix.rust }}
target: thumbv6m-none-eabi
- run: cargo install flip-link
- run: cargo build --all
- run: cargo build --all --release
linting:
name: Linting
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
submodules: true
- uses: dtolnay/rust-toolchain@stable
with:
components: clippy
target: thumbv6m-none-eabi
- run: cargo clippy --all-features -- --deny=warnings
formatting:
name: Formatting
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
submodules: true
- uses: dtolnay/rust-toolchain@stable
with:
components: rustfmt
target: thumbv6m-none-eabi
- run: cargo fmt -- --check

14
rp2040/.gitignore vendored Normal file
View file

@ -0,0 +1,14 @@
**/*.rs.bk
.#*
.gdb_history
Cargo.lock
target/
# editor files
.vscode/*
!.vscode/*.md
!.vscode/*.svd
!.vscode/launch.json
!.vscode/tasks.json
!.vscode/extensions.json
!.vscode/settings.json

45
rp2040/.vscode/launch.json vendored Normal file
View file

@ -0,0 +1,45 @@
// The format of this file is specified in https://probe.rs/docs/tools/vscode/#start-a-debug-session-with-minimum-configuration
{
"version": "0.2.0",
"configurations": [
{
"preLaunchTask": "rust: cargo build",
"type": "probe-rs-debug",
"request": "launch",
"name": "rp2040-project",
"cwd": "${workspaceFolder}",
"chip": "rp2040",
// RP2040 doesn't support connectUnderReset
"connectUnderReset": false,
"speed": 4000,
"runtimeExecutable": "probe-rs-debugger",
"runtimeArgs": [
"debug"
],
"flashingConfig": {
"flashingEnabled": true,
"resetAfterFlashing": true,
"haltAfterReset": true,
},
"coreConfigs": [
{
"coreIndex": 0,
"programBinary": "target/thumbv6m-none-eabi/debug/rp2040-project-template",
"chip": "RP2040",
// Uncomment this if you've downloaded the SVD from
// https://github.com/raspberrypi/pico-sdk/raw/1.3.1/src/rp2040/hardware_regs/rp2040.svd
// and placed it in the .vscode directory
// "svdFile": "./.vscode/rp2040.svd",
"rttEnabled": true,
"options": {
"env": {
"DEFMT_LOG": "debug"
}
},
}
],
"consoleLogLevel": "Info", //Error, Warn, Info, Debug, Trace
"wireProtocol": "Swd"
}
]
}

5
rp2040/.vscode/settings.json vendored Normal file
View file

@ -0,0 +1,5 @@
{
"rust-analyzer.cargo.target": "thumbv6m-none-eabi",
"rust-analyzer.checkOnSave.allTargets": false,
"editor.formatOnSave": true
}

201
rp2040/APACHE2.0 Normal file
View 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 2021 rp-rs organization
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.

76
rp2040/Cargo.toml Normal file
View file

@ -0,0 +1,76 @@
[package]
edition = "2021"
name = "rp2040-project-template"
version = "0.1.0"
[dependencies]
cortex-m = "0.7"
cortex-m-rt = "0.7"
embedded-hal = { version = "0.2.5", features = ["unproven"] }
defmt = "0.3"
defmt-rtt = "0.4"
panic-probe = { version = "0.3", features = ["print-defmt"] }
# We're using a Pico by default on this template
rp-pico = "0.7"
fugit = "0.3.7"
# but you can use any BSP. Uncomment this to use the pro_micro_rp2040 BSP instead
# sparkfun-pro-micro-rp2040 = "0.6"
# If you're not going to use a Board Support Package you'll need these:
# rp2040-hal = { version="0.8", features=["rt", "critical-section-impl"] }
# rp2040-boot2 = "0.2"
# cargo build/run
[profile.dev]
codegen-units = 1
debug = 2
debug-assertions = true
incremental = false
opt-level = 3
overflow-checks = true
# cargo build/run --release
[profile.release]
codegen-units = 1
debug = 2
debug-assertions = false
incremental = false
lto = 'fat'
opt-level = 3
overflow-checks = false
# do not optimize proc-macro crates = faster builds from scratch
[profile.dev.build-override]
codegen-units = 8
debug = false
debug-assertions = false
opt-level = 0
overflow-checks = false
[profile.release.build-override]
codegen-units = 8
debug = false
debug-assertions = false
opt-level = 0
overflow-checks = false
# cargo test
[profile.test]
codegen-units = 1
debug = 2
debug-assertions = true
incremental = false
opt-level = 3
overflow-checks = true
# cargo test --release
[profile.bench]
codegen-units = 1
debug = 2
debug-assertions = false
incremental = false
lto = 'fat'
opt-level = 3

39
rp2040/Embed.toml Normal file
View file

@ -0,0 +1,39 @@
[default.probe]
protocol = "Swd"
speed = 20000
# If you only have one probe cargo embed will pick automatically
# Otherwise: add your probe's VID/PID/serial to filter
## rust-dap
# usb_vid = "6666"
# usb_pid = "4444"
# serial = "test"
[default.flashing]
enabled = true
[default.reset]
enabled = true
halt_afterwards = false
[default.general]
chip = "RP2040"
log_level = "WARN"
# RP2040 does not support connect_under_reset
connect_under_reset = false
[default.rtt]
enabled = true
up_mode = "NoBlockSkip"
channels = [
{ up = 0, down = 0, name = "name", up_mode = "NoBlockSkip", format = "Defmt" },
]
timeout = 3000
show_timestamps = true
log_enabled = false
log_path = "./logs"
[default.gdb]
enabled = false
gdb_connection_string = "127.0.0.1:2345"

21
rp2040/MIT Normal file
View file

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2021 rp-rs organization
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.

300
rp2040/README.md Normal file
View file

@ -0,0 +1,300 @@
# Project template for rp2040-hal
This template is intended as a starting point for developing your own firmware based on the rp2040-hal.
It includes all of the `knurling-rs` tooling as showcased in https://github.com/knurling-rs/app-template (`defmt`, `defmt-rtt`, `panic-probe`, `flip-link`) to make development as easy as possible.
`probe-run` is configured as the default runner, so you can start your program as easy as
```sh
cargo run --release
```
If you aren't using a debugger (or want to use cargo-embed/probe-rs-debugger), check out [alternative runners](#alternative-runners) for other options
<!-- TABLE OF CONTENTS -->
<details open="open">
<summary><h2 style="display: inline-block">Table of Contents</h2></summary>
<ol>
<li><a href="#markdown-header-requirements">Requirements</a></li>
<li><a href="#installation-of-development-dependencies">Installation of development dependencies</a></li>
<li><a href="#running">Running</a></li>
<li><a href="#alternative-runners">Alternative runners</a></li>
<li><a href="#notes-on-using-rp2040_boot2">Notes on using rp2040_boot2</a></li>
<li><a href="#roadmap">Roadmap</a></li>
<li><a href="#contributing">Contributing</a></li>
<li><a href="#code-of-conduct">Code of conduct</a></li>
<li><a href="#license">License</a></li>
<li><a href="#contact">Contact</a></li>
</ol>
</details>
<!-- Requirements -->
<details open="open">
<summary><h2 style="display: inline-block" id="requirements">Requirements</h2></summary>
- The standard Rust tooling (cargo, rustup) which you can install from https://rustup.rs/
- Toolchain support for the cortex-m0+ processors in the rp2040 (thumbv6m-none-eabi)
- flip-link - this allows you to detect stack-overflows on the first core, which is the only supported target for now.
- probe-run. Upstream support for RP2040 was added with version 0.3.1.
- A CMSIS-DAP probe. (J-Link and other probes will not work with probe-run)
You can use a second
[Pico as a CMSIS-DAP debug probe](debug_probes.md#raspberry-pi-pico). Details
on other supported debug probes can be found in
[debug_probes.md](debug_probes.md)
</details>
<!-- Installation of development dependencies -->
<details open="open">
<summary><h2 style="display: inline-block" id="installation-of-development-dependencies">Installation of development dependencies</h2></summary>
```sh
rustup target install thumbv6m-none-eabi
cargo install flip-link
# This is our suggested default 'runner'
cargo install probe-run --locked
# If you want to use elf2uf2-rs instead of probe-run, instead do...
cargo install elf2uf2-rs --locked
```
</details>
<!-- Running -->
<details open="open">
<summary><h2 style="display: inline-block" id="running">Running</h2></summary>
For a debug build
```sh
cargo run
```
For a release build
```sh
cargo run --release
```
If you do not specify a DEFMT_LOG level, it will be set to `debug`.
That means `println!("")`, `info!("")` and `debug!("")` statements will be printed.
If you wish to override this, you can change it in `.cargo/config.toml`
```toml
[env]
DEFMT_LOG = "off"
```
You can also set this inline (on Linux/MacOS)
```sh
DEFMT_LOG=trace cargo run
```
or set the _environment variable_ so that it applies to every `cargo run` call that follows:
#### Linux/MacOS/unix
```sh
export DEFMT_LOG=trace
```
Setting the DEFMT_LOG level for the current session
for bash
```sh
export DEFMT_LOG=trace
```
#### Windows
Windows users can only override DEFMT_LOG through `config.toml`
or by setting the environment variable as a separate step before calling `cargo run`
- cmd
```cmd
set DEFMT_LOG=trace
```
- powershell
```ps1
$Env:DEFMT_LOG = trace
```
```cmd
cargo run
```
</details>
<!-- ALTERNATIVE RUNNERS -->
<details open="open">
<summary><h2 style="display: inline-block" id="alternative-runners">Alternative runners</h2></summary>
If you don't have a debug probe or if you want to do interactive debugging you can set up an alternative runner for cargo.
Some of the options for your `runner` are listed below:
* **cargo embed**
*Step 1* - Install [`cargo embed`](https://github.com/probe-rs/probe-rs/blob/master/cargo-embed):
```console
$ cargo install --locked cargo-embed
```
*Step 2* - Update settings in [Embed.toml](./Embed.toml)
- The defaults are to flash, reset, and start a defmt logging session
You can find all the settings and their meanings [in the cargo-embed repo](https://github.com/probe-rs/probe-rs/blob/master/cargo-embed/src/config/default.toml)
*Step 3* - Use the command `cargo embed`, which will compile the code, flash the device
and start running the configuration specified in Embed.toml
```console
$ cargo embed --release
```
* **probe-rs-debugger**
*Step 1* - Download [`probe-rs-debugger VSCode plugin 0.4.0`](https://github.com/probe-rs/vscode/releases/download/v0.4.0/probe-rs-debugger-0.4.0.vsix)
*Step 2* - Install `probe-rs-debugger VSCode plugin`
```console
$ code --install-extension probe-rs-debugger-0.4.0.vsix
```
*Step 3* - Install `probe-rs-debugger`
```console
$ cargo install probe-rs-debugger
```
*Step 4* - Open this project in VSCode
*Step 5* - Launch a debug session by choosing `Run`>`Start Debugging` (or press F5)
* **probe-rs-cli**
*Step 1* - Install [`probe-rs-cli`](https://crates.io/crates/probe-rs-cli):
```console
$ cargo install probe-rs-cli
```
*Step 2* - Make sure your .cargo/config contains the following
```toml
[target.thumbv6m-none-eabi]
runner = "probe-rs-cli run --chip RP2040 --protocol swd"
```
*Step 3* - Use `cargo run`, which will compile the code and start the
specified 'runner'. As the 'runner' is cargo embed, it will flash the device
and start running immediately
```console
$ cargo run --release
```
* **Loading a UF2 over USB**
*Step 1* - Install [`elf2uf2-rs`](https://github.com/JoNil/elf2uf2-rs):
```console
$ cargo install elf2uf2-rs --locked
```
*Step 2* - Make sure your .cargo/config contains the following
```toml
[target.thumbv6m-none-eabi]
runner = "elf2uf2-rs -d"
```
The `thumbv6m-none-eabi` target may be replaced by the all-Arm wildcard
`'cfg(all(target_arch = "arm", target_os = "none"))'`.
*Step 3* - Boot your RP2040 into "USB Bootloader mode", typically by rebooting
whilst holding some kind of "Boot Select" button. On Linux, you will also need
to 'mount' the device, like you would a USB Thumb Drive.
*Step 4* - Use `cargo run`, which will compile the code and start the
specified 'runner'. As the 'runner' is the elf2uf2-rs tool, it will build a UF2
file and copy it to your RP2040.
```console
$ cargo run --release
```
* **Loading with picotool**
As ELF files produced by compiling Rust code are completely compatible with ELF
files produced by compiling C or C++ code, you can also use the Raspberry Pi
tool [picotool](https://github.com/raspberrypi/picotool). The only thing to be
aware of is that picotool expects your ELF files to have a `.elf` extension, and
by default Rust does not give the ELF files any extension. You can fix this by
simply renaming the file.
This means you can't easily use it as a cargo runner - yet.
Also of note is that the special
[pico-sdk](https://github.com/raspberrypi/pico-sdk) macros which hide
information in the ELF file in a way that `picotool info` can read it out, are
not supported in Rust. An alternative is TBC.
</details>
</details>
<!-- Notes on using rp2040_hal and rp2040_boot2 -->
<details open="open">
<summary><h2 style="display: inline-block" id="notes-on-using-rp2040_boot2">Notes on using rp2040_boot2</h2></summary>
The second-stage boot loader must be written to the .boot2 section. That
is usually handled by the board support package (e.g.`rp-pico`). If you don't use
one, you should initialize the boot loader manually. This can be done by adding the
following to the beginning of main.rs:
```rust
use rp2040_boot2;
#[link_section = ".boot2"]
#[used]
pub static BOOT_LOADER: [u8; 256] = rp2040_boot2::BOOT_LOADER_W25Q080;
```
</details>
<!-- ROADMAP -->
## Roadmap
NOTE These packages are under active development. As such, it is likely to
remain volatile until a 1.0.0 release.
See the [open issues](https://github.com/rp-rs/rp2040-project-template/issues) for a list of
proposed features (and known issues).
## Contributing
Contributions are what make the open source community such an amazing place to be learn, inspire, and create. Any contributions you make are **greatly appreciated**.
The steps are:
1. Fork the Project by clicking the 'Fork' button at the top of the page.
2. Create your Feature Branch (`git checkout -b feature/AmazingFeature`)
3. Make some changes to the code or documentation.
4. Commit your Changes (`git commit -m 'Add some AmazingFeature'`)
5. Push to the Feature Branch (`git push origin feature/AmazingFeature`)
6. Create a [New Pull Request](https://github.com/rp-rs/rp-hal/pulls)
7. An admin will review the Pull Request and discuss any changes that may be required.
8. Once everyone is happy, the Pull Request can be merged by an admin, and your work is part of our project!
## Code of Conduct
Contribution to this crate is organized under the terms of the [Rust Code of
Conduct][CoC], and the maintainer of this crate, the [rp-rs team], promises
to intervene to uphold that code of conduct.
[CoC]: CODE_OF_CONDUCT.md
[rp-rs team]: https://github.com/orgs/rp-rs/teams/rp-rs
## License
The contents of this repository are dual-licensed under the _MIT OR Apache
2.0_ License. That means you can chose either the MIT licence or the
Apache-2.0 licence when you re-use this code. See `MIT` or `APACHE2.0` for more
information on each specific licence.
Any submissions to this project (e.g. as Pull Requests) must be made available
under these terms.
## Contact
Raise an issue: [https://github.com/rp-rs/rp2040-project-template/issues](https://github.com/rp-rs/rp2040-project-template/issues)
Chat to us on Matrix: [#rp-rs:matrix.org](https://matrix.to/#/#rp-rs:matrix.org)

31
rp2040/build.rs Normal file
View file

@ -0,0 +1,31 @@
//! This build script copies the `memory.x` file from the crate root into
//! a directory where the linker can always find it at build time.
//! For many projects this is optional, as the linker always searches the
//! project root directory -- wherever `Cargo.toml` is. However, if you
//! are using a workspace or have a more complicated build setup, this
//! build script becomes required. Additionally, by requesting that
//! Cargo re-run the build script whenever `memory.x` is changed,
//! updating `memory.x` ensures a rebuild of the application with the
//! new memory settings.
use std::env;
use std::fs::File;
use std::io::Write;
use std::path::PathBuf;
fn main() {
// Put `memory.x` in our output directory and ensure it's
// on the linker search path.
let out = &PathBuf::from(env::var_os("OUT_DIR").unwrap());
File::create(out.join("memory.x"))
.unwrap()
.write_all(include_bytes!("memory.x"))
.unwrap();
println!("cargo:rustc-link-search={}", out.display());
// By default, Cargo will re-run a build script whenever
// any file in the project changes. By specifying `memory.x`
// here, we ensure the build script is only re-run when
// `memory.x` is changed.
println!("cargo:rerun-if-changed=memory.x");
}

47
rp2040/debug_probes.md Normal file
View file

@ -0,0 +1,47 @@
# Compatible CMSIS-DAP debug probes
## Raspberry Pi Pico
You can use a second Pico as your debugger.
Download one of these firmware files:
- [picoprobe.uf2](https://github.com/raspberrypi/picoprobe/releases/download/picoprobe-cmsis-v1.02/picoprobe.uf2) -
Official raspberrypi probe firmware supporting CMSIS-DAP. ([Source](https://github.com/raspberrypi/picoprobe))
- [raspberry_pi_pico-DapperMime.uf2](https://github.com/majbthrd/DapperMime/releases/download/20210225/raspberry_pi_pico-DapperMime.uf2) -
Based upon an older version of the CMSIS-DAP sources. ([Source](https://github.com/majbthrd/DapperMime))
- [rust-dap-pico-ramexec-setclock.uf2](https://raw.githubusercontent.com/9names/binary-bits/main/rust-dap-pico-ramexec-setclock.uf2) -
If you have good wiring between your Pico's, this firmware will give faster
programming. (Inofficial build by [@9names](https://github.com/9names/).) ([Source](https://github.com/ciniml/rust-dap))
Then:
1. Put the Pico into USB Mass Storage Mode by holding the BOOTSEL button while connecting it to your computer with a USB cable
2. Open the drive RPI-RP2 when prompted
3. Copy the uf2 firmware file from Downloads into RPI-RP2
4. Connect the debug pins of your CMSIS-DAP Pico to the target one
- Connect GP2 on the Probe to SWCLK on the Target
- Connect GP3 on the Probe to SWDIO on the Target
- Connect a ground line from the CMSIS-DAP Probe to the Target too
## WeAct MiniF4
https://therealprof.github.io/blog/usb-c-pill-part1/
## HS-Probe
https://github.com/probe-rs/hs-probe
## ST-LINK v2 clone
It's getting harder to source these with stm32f103's as time goes on, so you might be better off choosing a stm32f103 dev board
Firmware: https://github.com/devanlai/dap42
## LPC-Link2
https://www.nxp.com/design/microcontrollers-developer-resources/lpc-link2:OM13054
## MCU-Link
https://www.nxp.com/part/MCU-LINK#/
## DAPLink
You can use DAPLink firmware with any of it's supported chips (LPC4322, LPC11U35, K20, K22, KL26). You'll need to use the 'develop' branch to use GCC to build it. You'll need to find a chip with the correct
Firmware source: https://github.com/ARMmbed/DAPLink/tree/develop

15
rp2040/memory.x Normal file
View file

@ -0,0 +1,15 @@
MEMORY {
BOOT2 : ORIGIN = 0x10000000, LENGTH = 0x100
FLASH : ORIGIN = 0x10000100, LENGTH = 2048K - 0x100
RAM : ORIGIN = 0x20000000, LENGTH = 256K
}
EXTERN(BOOT2_FIRMWARE)
SECTIONS {
/* ### Boot loader */
.boot2 ORIGIN(BOOT2) :
{
KEEP(*(.boot2));
} > BOOT2
} INSERT BEFORE .text;

158
rp2040/src/main.rs Normal file
View file

@ -0,0 +1,158 @@
//! Blinks the LED on a Pico board
//!
//! This will blink an LED attached to GP25, which is the pin the Pico uses for the on-board LED.
#![no_std]
#![no_main]
use bsp::{entry, hal::pwm::FreeRunning};
use defmt::*;
use defmt_rtt as _;
use panic_probe as _;
use rp_pico::hal;
// Provide an alias for our BSP so we can switch targets quickly.
// Uncomment the BSP you included in Cargo.toml, the rest of the code does not need to change.
use rp_pico as bsp;
use bsp::hal::{
clocks::{init_clocks_and_plls, Clock},
pac,
pwm::Slices,
sio::Sio,
watchdog::Watchdog,
};
/// Size of the i2c receive FIFO in bytes
const I2C_RECV_FIFO_SIZE: usize = 16;
// Which registers can be written over i2c
mod i2c_registers {
// Spindle speed from 0 to 65535 as 2 bytes
pub const SPINDLE_SPEED: u8 = 0;
}
enum I2CMessage {
SpindleSpeed(u16),
}
#[entry]
fn main() -> ! {
info!("Program start");
let mut pac = pac::Peripherals::take().unwrap();
let core = pac::CorePeripherals::take().unwrap();
let mut watchdog = Watchdog::new(pac.WATCHDOG);
let sio = Sio::new(pac.SIO);
// External high-speed crystal on the pico board is 12Mhz
let external_xtal_freq_hz = 12_000_000u32;
let clocks = init_clocks_and_plls(
external_xtal_freq_hz,
pac.XOSC,
pac.CLOCKS,
pac.PLL_SYS,
pac.PLL_USB,
&mut pac.RESETS,
&mut watchdog,
)
.ok()
.unwrap();
let mut delay = cortex_m::delay::Delay::new(core.SYST, clocks.system_clock.freq().to_Hz());
let pins = bsp::Pins::new(
pac.IO_BANK0,
pac.PADS_BANK0,
sio.gpio_bank0,
&mut pac.RESETS,
);
let mut i2c = hal::I2C::new_peripheral_event_iterator(
pac.I2C1,
pins.gpio18.into_mode(),
pins.gpio19.into_mode(),
&mut pac.RESETS,
25,
);
let pwm_slices = Slices::new(pac.PWM, &mut pac.RESETS);
let mut pwm = pwm_slices.pwm1;
info!("Top: {}", pwm.get_top());
pwm.set_ph_correct();
pwm.enable();
let pwm = pwm.into_mode::<FreeRunning>();
let mut channel_b = pwm.channel_b;
use embedded_hal::PwmPin;
let _channel_pin_b_3 = channel_b.output_to(pins.gpio3);
channel_b.set_duty(u16::MAX);
channel_b.enable();
info!("PWM enabled");
let mut iter_counter = 1;
loop {
use hal::i2c::peripheral::I2CEvent;
let read_bytes = match i2c.next() {
None => None,
Some(I2CEvent::Stop) => {
info!("Stop");
None
}
Some(I2CEvent::Start) => {
info!("Start");
None
}
Some(I2CEvent::TransferWrite) => {
info!("Start");
let mut buf = [0; I2C_RECV_FIFO_SIZE];
let read_count = i2c.read(&mut buf);
if read_count >= buf.len() {
error!(
"Hit i2c receive FIFO limit: read {} bytes of data from I2C, ignoring transfer.",
I2C_RECV_FIFO_SIZE
);
// empty out i2c recv FIFO
while let 1.. = i2c.read(&mut buf) {}
None
} else {
info!("read {} bytes", read_count);
info!("content: {:?}", buf);
Some(buf)
}
}
Some(_) => {
info!("something");
None
}
};
read_bytes
.and_then(|bytes| parse_i2c_message(&bytes).ok())
.map(|message| {
if let I2CMessage::SpindleSpeed(duty) = message {
channel_b.set_duty(duty);
info!("Duty: {}", channel_b.get_duty());
}
});
delay.delay_ms(10);
}
}
fn parse_i2c_message(bytes: &[u8]) -> Result<I2CMessage, ()> {
if bytes.len() == 0 {
error!("Can't parse empty buffer!");
return Err(());
}
if bytes.len() >= 3 && bytes[0] == i2c_registers::SPINDLE_SPEED {
let speed_lower: u16 = bytes[1].into();
let speed_higher: u16 = bytes[2].into();
let speed = (speed_higher << 8) + speed_lower;
Ok(I2CMessage::SpindleSpeed(speed))
} else {
error!("could not parse i2c message: {}", bytes);
Err(())
}
}