Răsfoiți Sursa

First Version OK.

ZRY 1 an în urmă
părinte
comite
cc42662771
15 a modificat fișierele cu 628 adăugiri și 0 ștergeri
  1. 29 0
      .cargo/config.toml
  2. 5 0
      .gitignore
  3. 8 0
      .idea/.gitignore
  4. 12 0
      .idea/hello_nsagp_with_rust_2022_0718_1907.iml
  5. 8 0
      .idea/modules.xml
  6. 6 0
      .idea/vcs.xml
  7. 1 0
      CODE_OF_CONDUCT.md
  8. 70 0
      Cargo.toml
  9. 39 0
      Embed.toml
  10. 36 0
      README.md
  11. 267 0
      Template_README.md
  12. 31 0
      build.rs
  13. 36 0
      debug_probes.md
  14. 15 0
      memory.x
  15. 65 0
      src/main.rs

+ 29 - 0
.cargo/config.toml

@@ -0,0 +1,29 @@
+[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
+# - cargo embed offers flashing, rtt, defmt and a gdb server via a hardware debugger
+#     it is configured via the Embed.toml in the root of this project
+# - elf2uf2-rs loads firmware over USB when the rp2040 is in boot mode
+# runner = "probe-run --chip RP2040"
+# runner = "cargo embed"
+runner = "elf2uf2-rs -d"
+
+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"

+ 5 - 0
.gitignore

@@ -11,3 +11,8 @@
 # Generated by Cargo
 /target/
 
+.gdb_history
+Cargo.lock
+
+# UF2 Files
+*.uf2

+ 8 - 0
.idea/.gitignore

@@ -0,0 +1,8 @@
+# Default ignored files
+/shelf/
+/workspace.xml
+# Editor-based HTTP Client requests
+/httpRequests/
+# Datasource local storage ignored files
+/dataSources/
+/dataSources.local.xml

+ 12 - 0
.idea/hello_nsagp_with_rust_2022_0718_1907.iml

@@ -0,0 +1,12 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<module type="WEB_MODULE" version="4">
+  <component name="Go" enabled="true" />
+  <component name="NewModuleRootManager">
+    <content url="file://$MODULE_DIR$">
+      <sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
+      <excludeFolder url="file://$MODULE_DIR$/target" />
+    </content>
+    <orderEntry type="inheritedJdk" />
+    <orderEntry type="sourceFolder" forTests="false" />
+  </component>
+</module>

+ 8 - 0
.idea/modules.xml

@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="ProjectModuleManager">
+    <modules>
+      <module fileurl="file://$PROJECT_DIR$/.idea/hello_nsagp_with_rust_2022_0718_1907.iml" filepath="$PROJECT_DIR$/.idea/hello_nsagp_with_rust_2022_0718_1907.iml" />
+    </modules>
+  </component>
+</project>

+ 6 - 0
.idea/vcs.xml

@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="VcsDirectoryMappings">
+    <mapping directory="$PROJECT_DIR$" vcs="Git" />
+  </component>
+</project>

+ 1 - 0
CODE_OF_CONDUCT.md

@@ -0,0 +1 @@
+Contribution to these projects is organized under the terms of the [Rust Code of Conduct](https://www.rust-lang.org/policies/code-of-conduct)

+ 70 - 0
Cargo.toml

@@ -0,0 +1,70 @@
+#cargo-features = ["edition2021"]
+
+[package]
+edition = "2021"
+name = "hello_nsagp_with_rust_2022_0718_1907"
+version = "0.1.0"
+
+[dependencies]
+cortex-m = "0.7"
+cortex-m-rt = "0.7"
+embedded-hal = { version = "0.2.5", features = ["unproven"] }
+embedded-time = "0.12"
+rp2040-hal = "0.5.0"
+rp2040-boot2 = { version = "0.2" }
+
+defmt = "0.3"
+defmt-rtt = "0.3"
+panic-probe = { version = "0.3", features = ["print-defmt"] }
+
+# 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 - 0
Embed.toml

@@ -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"

+ 36 - 0
README.md

@@ -1,2 +1,38 @@
 # hello_nsagp_with_rust_2022_0718_1907
 
+## 使用说明
+
+### 准备工作
+
+```bash
+# 安装编译目标
+rustup target install thumbv6m-none-eabi
+# 安装flip-link
+cargo install flip-link
+# 如果想生成uf2文件,安装elf2uf2-rs
+cargo install elf2uf2-rs --locked
+```
+
+### 编译
+
+```bash
+cargo build --releases
+```
+
+### 烧写
+
+#### 通过UF2烧写
+
+1. 让RP2040进入`USB Bootloader Mode`,一般来说是按住`BOOTSEL`按钮上电。
+
+2. 烧写
+
+```bash
+cargo run --release
+```
+
+#### 生成UF2但不烧写
+
+```bash
+elf2uf2-rs target\thumbv6m-none-eabi\release\hello_nsagp_with_rust_2022_0718_1907 hello_nsagp_with_rust_2022_0718_1907.uf2
+```

+ 267 - 0
Template_README.md

@@ -0,0 +1,267 @@
+# 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="#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 by installing the following firmware on it:
+  https://github.com/majbthrd/DapperMime/releases/download/20210225/raspberry_pi_pico-DapperMime.uf2
+
+  More details on 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
+# 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/cargo-embed):
+
+  ```console
+  $ cargo install cargo-embed
+  ```
+
+  *Step 2* - Make sure your .cargo/config contains the following
+
+  ```toml
+  [target.thumbv6m-none-eabi]
+  runner = "cargo embed"
+  ```
+
+  *Step 3* - 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/cargo-embed/blob/master/src/config/default.toml)
+
+  *Step 4* - 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
+  ```
+
+* **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)
+
+* **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>
+
+<!-- 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 - 0
build.rs

@@ -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");
+}

+ 36 - 0
debug_probes.md

@@ -0,0 +1,36 @@
+# Compatible CMSIS-DAP debug probes
+
+## Raspberry Pi Pico
+
+  You can use a second Pico as your debugger.
+
+  -  Download this file: https://github.com/majbthrd/DapperMime/releases/download/20210225/raspberry_pi_pico-DapperMime.uf2
+  - Boot the Pico in bootloader mode by holding the bootset button while plugging it in
+  - Open the drive RPI-RP2 when prompted
+  - Copy raspberry_pi_pico-DapperMime.uf2 from Downloads into RPI-RP2
+  - 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 - 0
memory.x

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

+ 65 - 0
src/main.rs

@@ -0,0 +1,65 @@
+#![no_std]
+#![no_main]
+
+use cortex_m_rt::entry;
+use defmt::*;
+use defmt_rtt as _;
+use embedded_hal::digital::v2::OutputPin;
+use embedded_time::fixed_point::FixedPoint;
+use panic_probe as _;
+use rp2040_hal as hal;
+use hal::{
+    clocks::{init_clocks_and_plls, Clock},
+    pac,
+    sio::Sio,
+    watchdog::Watchdog,
+};
+
+#[link_section = ".boot2"]
+#[used]
+pub static BOOT2: [u8; 256] = rp2040_boot2::BOOT_LOADER_AT25SF128A;
+
+#[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().integer());
+
+    let pins = hal::gpio::Pins::new(
+        pac.IO_BANK0,
+        pac.PADS_BANK0,
+        sio.gpio_bank0,
+        &mut pac.RESETS,
+    );
+
+    let mut led_pin = pins.gpio25.into_push_pull_output();
+
+    loop {
+        info!("on!");
+        led_pin.set_high().unwrap();
+        delay.delay_ms(500);
+        info!("off!");
+        led_pin.set_low().unwrap();
+        delay.delay_ms(500);
+    }
+}
+
+// End of file