-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
26 changed files
with
1,244 additions
and
199 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
doc/develop/embedded/0.Preparing the development environment.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Preparing the development environment | ||
|
||
## |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# 开发环境准备 | ||
|
||
## 依赖 | ||
- Ubuntu18.04或以上版本. | ||
`gdb-multiarch`是调试`Arm Cortex-M`程序的`GDB` | ||
``` | ||
sudo apt install gdb-multiarch openocd qemu-system-arm | ||
``` | ||
- 添加对Arm Cortex-M 架构交叉编译的支持. | ||
```bash | ||
rustup target add thumbv8m.main-none-eabi | ||
cargo install cargo-binutils | ||
``` | ||
|
||
## udev规则 | ||
这个规则可以在不使用超级用户的情况下,使用`OpenOCD`和开发板. | ||
|
||
`/etc/udev/rules.d/m-st-link.rules` | ||
|
||
`F4xx`: | ||
``` | ||
# STM32F4D rev A/B - ST-LINK/V2 | ||
ATTRS{idVendor}=="0483", ATTRS{idProduct}=="3748", TAG+="uaccess" | ||
# STM32F4 rev C+ - ST-LINK/V2-1 | ||
ATTRS{idVendor}=="0483", ATTRS{idProduct}=="374b", TAG+="uaccess" | ||
``` |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# 第一个嵌入式工程 | ||
|
||
## 使用cargo-generate创建. | ||
```bash | ||
cargo generate --git https://github.com/rust-embedded/cortex-m-quickstart | ||
|
||
- Project Name: demo | ||
``` | ||
|
||
生成的项目目录树: | ||
```bash | ||
├── build.rs | ||
├── Cargo.toml | ||
├── examples | ||
│ ├── allocator.rs | ||
│ ├── crash.rs | ||
│ ├── device.rs | ||
│ ├── exception.rs | ||
│ ├── hello.rs | ||
│ ├── itm.rs | ||
│ ├── panic.rs | ||
│ └── test_on_host.rs | ||
├── memory.x | ||
├── openocd.cfg | ||
├── openocd.gdb | ||
├── README.md | ||
└── src | ||
└── main.rs | ||
``` | ||
其中`src/main.rs`的内容如下: | ||
```rust | ||
#![no_std] | ||
#![no_main] | ||
|
||
use panic_halt as _; | ||
|
||
use cortex_m_rt::entry; | ||
|
||
#[entry] | ||
fn main() -> ! { | ||
loop { | ||
// your code goes here | ||
} | ||
} | ||
``` | ||
`#![no_std]`指出这个程序不会链接标准`crate std`, 将会链接到它的子集`core` crate. | ||
`#![no_main]`指出这个程序将不会使用标准的`main`接口. | ||
|
||
## 交叉编译. | ||
```bash | ||
cargo build --target thumbv7m-none-eabi | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[build] | ||
target-dir = "target" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
[package] | ||
name = "embedded" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
cortex-m = "0.6.0" | ||
cortex-m-rt = "0.6.10" | ||
cortex-m-semihosting = "0.3.3" | ||
panic-halt = "0.2.0" | ||
|
||
# Uncomment for the panic example. | ||
# panic-itm = "0.4.1" | ||
|
||
# Uncomment for the allocator example. | ||
# alloc-cortex-m = "0.4.0" | ||
|
||
# Uncomment for the device example. | ||
# Update `memory.x`, set target to `thumbv7em-none-eabihf` in `.cargo/config`, | ||
# and then use `cargo build --examples device` to build it. | ||
# [dependencies.stm32f3] | ||
# features = ["stm32f303", "rt"] | ||
# version = "0.7.1" | ||
|
||
# this lets you use `cargo fix`! | ||
# [[bin]] | ||
# name = "embedded" | ||
# test = false | ||
# bench = false | ||
|
||
[profile.release] | ||
codegen-units = 1 # better optimizations | ||
debug = false # symbols are nice and they don't increase the size on Flash | ||
lto = true # better optimizations |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
# `cortex-m-quickstart` | ||
|
||
> A template for building applications for ARM Cortex-M microcontrollers | ||
This project is developed and maintained by the [Cortex-M team][team]. | ||
|
||
## Dependencies | ||
|
||
To build embedded programs using this template you'll need: | ||
|
||
- Rust 1.31, 1.30-beta, nightly-2018-09-13 or a newer toolchain. e.g. `rustup | ||
default beta` | ||
|
||
- The `cargo generate` subcommand. [Installation | ||
instructions](https://github.com/ashleygwilliams/cargo-generate#installation). | ||
|
||
- `rust-std` components (pre-compiled `core` crate) for the ARM Cortex-M | ||
targets. Run: | ||
|
||
``` console | ||
$ rustup target add thumbv6m-none-eabi thumbv7m-none-eabi thumbv7em-none-eabi thumbv7em-none-eabihf | ||
``` | ||
|
||
## Using this template | ||
|
||
**NOTE**: This is the very short version that only covers building programs. For | ||
the long version, which additionally covers flashing, running and debugging | ||
programs, check [the embedded Rust book][book]. | ||
|
||
[book]: https://rust-embedded.github.io/book | ||
|
||
0. Before we begin you need to identify some characteristics of the target | ||
device as these will be used to configure the project: | ||
|
||
- The ARM core. e.g. Cortex-M3. | ||
|
||
- Does the ARM core include an FPU? Cortex-M4**F** and Cortex-M7**F** cores do. | ||
|
||
- How much Flash memory and RAM does the target device has? e.g. 256 KiB of | ||
Flash and 32 KiB of RAM. | ||
|
||
- Where are Flash memory and RAM mapped in the address space? e.g. RAM is | ||
commonly located at address `0x2000_0000`. | ||
|
||
You can find this information in the data sheet or the reference manual of your | ||
device. | ||
|
||
In this example we'll be using the STM32F3DISCOVERY. This board contains an | ||
STM32F303VCT6 microcontroller. This microcontroller has: | ||
|
||
- A Cortex-M4F core that includes a single precision FPU | ||
|
||
- 256 KiB of Flash located at address 0x0800_0000. | ||
|
||
- 40 KiB of RAM located at address 0x2000_0000. (There's another RAM region but | ||
for simplicity we'll ignore it). | ||
|
||
1. Instantiate the template. | ||
|
||
``` console | ||
$ cargo generate --git https://github.com/rust-embedded/cortex-m-quickstart | ||
Project Name: app | ||
Creating project called `app`... | ||
Done! New project created /tmp/app | ||
|
||
$ cd app | ||
``` | ||
|
||
2. Set a default compilation target. There are four options as mentioned at the | ||
bottom of `.cargo/config`. For the STM32F303VCT6, which has a Cortex-M4F | ||
core, we'll pick the `thumbv7em-none-eabihf` target. | ||
|
||
``` console | ||
$ tail -n9 .cargo/config.toml | ||
``` | ||
|
||
``` toml | ||
[build] | ||
# Pick ONE of these compilation targets | ||
# 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 = "thumbv8m.base-none-eabi" # Cortex-M23 | ||
# target = "thumbv8m.main-none-eabi" # Cortex-M33 (no FPU) | ||
# target = "thumbv8m.main-none-eabihf" # Cortex-M33 (with FPU) | ||
``` | ||
|
||
3. Enter the memory region information into the `memory.x` file. | ||
|
||
``` console | ||
$ cat memory.x | ||
/* Linker script for the STM32F303VCT6 */ | ||
MEMORY | ||
{ | ||
/* NOTE 1 K = 1 KiBi = 1024 bytes */ | ||
FLASH : ORIGIN = 0x08000000, LENGTH = 256K | ||
RAM : ORIGIN = 0x20000000, LENGTH = 40K | ||
} | ||
``` | ||
|
||
4. Build the template application or one of the examples. | ||
|
||
``` console | ||
$ cargo build | ||
``` | ||
|
||
## VS Code | ||
|
||
This template includes launch configurations for debugging CortexM programs with Visual Studio Code located in the `.vscode/` directory. | ||
See [.vscode/README.md](./.vscode/README.md) for more information. | ||
If you're not using VS Code, you can safely delete the directory from the generated project. | ||
|
||
# License | ||
|
||
This template is 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 | ||
dual licensed as above, without any additional terms or conditions. | ||
|
||
## Code of Conduct | ||
|
||
Contribution to this crate is organized under the terms of the [Rust Code of | ||
Conduct][CoC], the maintainer of this crate, the [Cortex-M team][team], promises | ||
to intervene to uphold that code of conduct. | ||
|
||
[CoC]: https://www.rust-lang.org/policies/code-of-conduct | ||
[team]: https://github.com/rust-embedded/wg#the-cortex-m-team |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
//! 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. | ||
//! | ||
//! The build script also sets the linker flags to tell it which link script to use. | ||
|
||
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"); | ||
|
||
// Specify linker arguments. | ||
|
||
// `--nmagic` is required if memory section addresses are not aligned to 0x10000, | ||
// for example the FLASH and RAM sections in your `memory.x`. | ||
// See https://github.com/rust-embedded/cortex-m-quickstart/pull/95 | ||
println!("cargo:rustc-link-arg=--nmagic"); | ||
|
||
// Set the linker script to the one provided by cortex-m-rt. | ||
println!("cargo:rustc-link-arg=-Tlink.x"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
//! How to use the heap and a dynamic memory allocator | ||
//! | ||
//! This example depends on the alloc-cortex-m crate so you'll have to add it to your Cargo.toml: | ||
//! | ||
//! ``` text | ||
//! # or edit the Cargo.toml file manually | ||
//! $ cargo add alloc-cortex-m | ||
//! ``` | ||
//! | ||
//! --- | ||
|
||
#![feature(alloc_error_handler)] | ||
#![no_main] | ||
#![no_std] | ||
|
||
extern crate alloc; | ||
use panic_halt as _; | ||
|
||
use self::alloc::vec; | ||
use core::alloc::Layout; | ||
|
||
use alloc_cortex_m::CortexMHeap; | ||
use cortex_m::asm; | ||
use cortex_m_rt::entry; | ||
use cortex_m_semihosting::{hprintln, debug}; | ||
|
||
// this is the allocator the application will use | ||
#[global_allocator] | ||
static ALLOCATOR: CortexMHeap = CortexMHeap::empty(); | ||
|
||
const HEAP_SIZE: usize = 1024; // in bytes | ||
|
||
#[entry] | ||
fn main() -> ! { | ||
// Initialize the allocator BEFORE you use it | ||
unsafe { ALLOCATOR.init(cortex_m_rt::heap_start() as usize, HEAP_SIZE) } | ||
|
||
// Growable array allocated on the heap | ||
let xs = vec![0, 1, 2]; | ||
|
||
hprintln!("{:?}", xs).unwrap(); | ||
|
||
// exit QEMU | ||
// NOTE do not run this on hardware; it can corrupt OpenOCD state | ||
debug::exit(debug::EXIT_SUCCESS); | ||
|
||
loop {} | ||
} | ||
|
||
// define what happens in an Out Of Memory (OOM) condition | ||
#[alloc_error_handler] | ||
fn alloc_error(_layout: Layout) -> ! { | ||
asm::bkpt(); | ||
|
||
loop {} | ||
} |
Oops, something went wrong.