mirror of
https://github.com/google/comprehensive-rust.git
synced 2025-04-25 08:53:01 +02:00
Add raw MMIO example.
This commit is contained in:
parent
2545e40d21
commit
0fa73286b9
@ -240,7 +240,7 @@
|
||||
- [A minimal example](bare-metal/minimal.md)
|
||||
- [alloc](bare-metal/alloc.md)
|
||||
- [Microcontrollers](bare-metal/microcontrollers.md)
|
||||
- [Raw MMIO]()
|
||||
- [Raw MMIO](bare-metal/microcontrollers/mmio.md)
|
||||
- [PACs](bare-metal/microcontrollers/pacs.md)
|
||||
- [HAL crates](bare-metal/microcontrollers/hals.md)
|
||||
- [Board support crates](bare-metal/microcontrollers/board-support.md)
|
||||
|
@ -16,11 +16,14 @@ panic-halt = "0.2.0"
|
||||
[[bin]]
|
||||
name = "board_support"
|
||||
|
||||
[[bin]]
|
||||
name = "pac"
|
||||
|
||||
[[bin]]
|
||||
name = "hal"
|
||||
|
||||
[[bin]]
|
||||
name = "mmio"
|
||||
|
||||
[[bin]]
|
||||
name = "pac"
|
||||
|
||||
[[bin]]
|
||||
name = "typestate"
|
||||
|
@ -0,0 +1,17 @@
|
||||
// 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.
|
||||
|
||||
#[link_section = ".vector_table.interrupts"]
|
||||
#[no_mangle]
|
||||
pub static __INTERRUPTS: [usize; 1] = [0];
|
66
src/bare-metal/microcontrollers/examples/src/bin/mmio.rs
Normal file
66
src/bare-metal/microcontrollers/examples/src/bin/mmio.rs
Normal file
@ -0,0 +1,66 @@
|
||||
// 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 _;
|
||||
|
||||
mod interrupts;
|
||||
|
||||
use core::mem::size_of;
|
||||
use cortex_m_rt::entry;
|
||||
|
||||
/// GPIO port 0 peripheral address
|
||||
const GPIO_P0: usize = 0x5000_0000;
|
||||
|
||||
// GPIO peripheral offsets
|
||||
const PIN_CNF: usize = 0x700;
|
||||
const OUTSET: usize = 0x508;
|
||||
const OUTCLR: usize = 0x50c;
|
||||
|
||||
// PIN_CNF fields
|
||||
const DIR_OUTPUT: u32 = 0x1;
|
||||
const INPUT_DISCONNECT: u32 = 0x1 << 1;
|
||||
const PULL_DISABLED: u32 = 0x0 << 2;
|
||||
const DRIVE_S0S1: u32 = 0x0 << 8;
|
||||
const SENSE_DISABLED: u32 = 0x0 << 16;
|
||||
|
||||
#[entry]
|
||||
fn main() -> ! {
|
||||
// Configure GPIO 0 pins 21 and 28 as push-pull outputs.
|
||||
let pin_cnf_21 = (GPIO_P0 + PIN_CNF + 21 * size_of::<u32>()) as *mut u32;
|
||||
let pin_cnf_28 = (GPIO_P0 + PIN_CNF + 28 * size_of::<u32>()) as *mut u32;
|
||||
// Safe because the pointers are to valid peripheral control registers, and no aliases exist.
|
||||
unsafe {
|
||||
pin_cnf_21.write_volatile(
|
||||
DIR_OUTPUT | INPUT_DISCONNECT | PULL_DISABLED | DRIVE_S0S1 | SENSE_DISABLED,
|
||||
);
|
||||
pin_cnf_28.write_volatile(
|
||||
DIR_OUTPUT | INPUT_DISCONNECT | PULL_DISABLED | DRIVE_S0S1 | SENSE_DISABLED,
|
||||
);
|
||||
}
|
||||
|
||||
// Set pin 28 low and pin 21 high to turn the LED on.
|
||||
let gpio0_outset = (GPIO_P0 + OUTSET) as *mut u32;
|
||||
let gpio0_outclr = (GPIO_P0 + OUTCLR) as *mut u32;
|
||||
// Safe because the pointers are to valid peripheral control registers, and no aliases exist.
|
||||
unsafe {
|
||||
gpio0_outclr.write_volatile(1 << 28);
|
||||
gpio0_outset.write_volatile(1 << 21);
|
||||
}
|
||||
|
||||
loop {}
|
||||
}
|
5
src/bare-metal/microcontrollers/mmio.md
Normal file
5
src/bare-metal/microcontrollers/mmio.md
Normal file
@ -0,0 +1,5 @@
|
||||
# Raw MMIO
|
||||
|
||||
```rust,editable,compile_fail
|
||||
{{#include examples/src/bin/mmio.rs:Example}}
|
||||
```
|
Loading…
x
Reference in New Issue
Block a user