You've already forked comprehensive-rust
mirror of
https://github.com/google/comprehensive-rust.git
synced 2025-11-29 16:57:35 +02:00
Add pages with PAC and HAL examples.
This commit is contained in:
@@ -239,9 +239,9 @@
|
|||||||
- [`no_std`](bare-metal/no_std.md)
|
- [`no_std`](bare-metal/no_std.md)
|
||||||
- [A minimal example](bare-metal/minimal.md)
|
- [A minimal example](bare-metal/minimal.md)
|
||||||
- [`alloc`](bare-metal/alloc.md)
|
- [`alloc`](bare-metal/alloc.md)
|
||||||
- [Microcontrollers]()
|
- [Microcontrollers](bare-metal/microcontrollers.md)
|
||||||
- [PACs]()
|
- [PACs](bare-metal/microcontrollers/pacs.md)
|
||||||
- [HAL crates]()
|
- [HAL crates](bare-metal/microcontrollers/hals.md)
|
||||||
- [The type state pattern]()
|
- [The type state pattern]()
|
||||||
|
|
||||||
# Day 5A: Afternoon
|
# Day 5A: Afternoon
|
||||||
|
|||||||
1
src/bare-metal/microcontrollers.md
Normal file
1
src/bare-metal/microcontrollers.md
Normal file
@@ -0,0 +1 @@
|
|||||||
|
# Microcontrollers
|
||||||
@@ -121,11 +121,12 @@ dependencies = [
|
|||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "microcontroller-example"
|
name = "microcontroller-examples"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"cortex-m-rt",
|
"cortex-m-rt",
|
||||||
"embedded-hal",
|
"embedded-hal",
|
||||||
|
"gd32f1",
|
||||||
"gd32f1x0-hal",
|
"gd32f1x0-hal",
|
||||||
"panic-halt",
|
"panic-halt",
|
||||||
]
|
]
|
||||||
@@ -1,10 +1,17 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "microcontroller-example"
|
name = "microcontroller-examples"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
cortex-m-rt = "0.7.0"
|
cortex-m-rt = "0.7.0"
|
||||||
embedded-hal = "0.2.6"
|
embedded-hal = "0.2.6"
|
||||||
|
gd32f1 = { version = "0.6.0", features = ["gd32f130", "rt"] }
|
||||||
gd32f1x0-hal = { version = "0.7.1", features = ["rt", "gd32f130x8"] }
|
gd32f1x0-hal = { version = "0.7.1", features = ["rt", "gd32f130x8"] }
|
||||||
panic-halt = "0.2.0"
|
panic-halt = "0.2.0"
|
||||||
|
|
||||||
|
[[bin]]
|
||||||
|
name = "pac"
|
||||||
|
|
||||||
|
[[bin]]
|
||||||
|
name = "hal"
|
||||||
@@ -19,17 +19,20 @@
|
|||||||
extern crate panic_halt as _;
|
extern crate panic_halt as _;
|
||||||
|
|
||||||
use cortex_m_rt::entry;
|
use cortex_m_rt::entry;
|
||||||
use embedded_hal::digital::v2::OutputPin;
|
|
||||||
use gd32f1x0_hal::{pac::Peripherals, prelude::*};
|
use gd32f1x0_hal::{pac::Peripherals, prelude::*};
|
||||||
|
|
||||||
#[entry]
|
#[entry]
|
||||||
fn main() -> ! {
|
fn main() -> ! {
|
||||||
let p = Peripherals::take().unwrap();
|
let p = Peripherals::take().unwrap();
|
||||||
let mut rcu = p.RCU.constrain();
|
let mut rcu = p.RCU.constrain();
|
||||||
|
|
||||||
|
// Enable GPIOC.
|
||||||
let mut gpioc = p.GPIOC.split(&mut rcu.ahb);
|
let mut gpioc = p.GPIOC.split(&mut rcu.ahb);
|
||||||
|
|
||||||
|
// Configure PC9 as a push-pull output.
|
||||||
let mut led = gpioc.pc9.into_push_pull_output(&mut gpioc.config);
|
let mut led = gpioc.pc9.into_push_pull_output(&mut gpioc.config);
|
||||||
|
|
||||||
|
// Set PC9 high to turn the LED on.
|
||||||
led.set_high().unwrap();
|
led.set_high().unwrap();
|
||||||
|
|
||||||
loop {}
|
loop {}
|
||||||
41
src/bare-metal/microcontrollers/examples/src/bin/pac.rs
Normal file
41
src/bare-metal/microcontrollers/examples/src/bin/pac.rs
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
// Copyright 2023 Google LLC
|
||||||
|
//
|
||||||
|
// 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.
|
||||||
|
|
||||||
|
// ANCHOR: Example
|
||||||
|
#![no_main]
|
||||||
|
#![no_std]
|
||||||
|
|
||||||
|
extern crate panic_halt as _;
|
||||||
|
|
||||||
|
use cortex_m_rt::entry;
|
||||||
|
use gd32f1::gd32f130::Peripherals;
|
||||||
|
|
||||||
|
#[entry]
|
||||||
|
fn main() -> ! {
|
||||||
|
let p = Peripherals::take().unwrap();
|
||||||
|
let gpioc = p.GPIOC;
|
||||||
|
|
||||||
|
// Enable GPIOC.
|
||||||
|
p.RCU.ahben.modify(|_, w| w.pcen().enabled());
|
||||||
|
|
||||||
|
// Configure PC9 as a push-pull output.
|
||||||
|
gpioc.pud.modify(|_, w| w.pud9().floating());
|
||||||
|
gpioc.omode.modify(|_, w| w.om9().push_pull());
|
||||||
|
gpioc.ctl.modify(|_, w| w.ctl9().output());
|
||||||
|
|
||||||
|
// Set PC9 high to turn the LED on.
|
||||||
|
gpioc.bop.write(|w| w.bop9().set());
|
||||||
|
|
||||||
|
loop {}
|
||||||
|
}
|
||||||
5
src/bare-metal/microcontrollers/hals.md
Normal file
5
src/bare-metal/microcontrollers/hals.md
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
# HAL crates
|
||||||
|
|
||||||
|
```rust,editable,compile_fail
|
||||||
|
{{#include examples/src/bin/hal.rs:Example}}
|
||||||
|
```
|
||||||
13
src/bare-metal/microcontrollers/pacs.md
Normal file
13
src/bare-metal/microcontrollers/pacs.md
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
# PACs
|
||||||
|
|
||||||
|
```rust,editable,compile_fail
|
||||||
|
{{#include examples/src/bin/pac.rs:Example}}
|
||||||
|
```
|
||||||
|
|
||||||
|
<details>
|
||||||
|
|
||||||
|
* `cortex-m-rt` provides the vector table, among other things.
|
||||||
|
* If you `cargo install cargo-binutils` then you can run
|
||||||
|
`cargo objdump --bin pac -- -d --no-show-raw-insn` to see the resulting binary.
|
||||||
|
|
||||||
|
</details>
|
||||||
Reference in New Issue
Block a user