1
0
mirror of https://github.com/google/comprehensive-rust.git synced 2025-04-25 16:54:32 +02:00

Add raw MMIO example.

This commit is contained in:
Andrew Walbran 2023-02-16 03:16:49 +00:00
parent 2545e40d21
commit 0fa73286b9
5 changed files with 95 additions and 4 deletions

View File

@ -240,7 +240,7 @@
- [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](bare-metal/microcontrollers.md) - [Microcontrollers](bare-metal/microcontrollers.md)
- [Raw MMIO]() - [Raw MMIO](bare-metal/microcontrollers/mmio.md)
- [PACs](bare-metal/microcontrollers/pacs.md) - [PACs](bare-metal/microcontrollers/pacs.md)
- [HAL crates](bare-metal/microcontrollers/hals.md) - [HAL crates](bare-metal/microcontrollers/hals.md)
- [Board support crates](bare-metal/microcontrollers/board-support.md) - [Board support crates](bare-metal/microcontrollers/board-support.md)

View File

@ -16,11 +16,14 @@ panic-halt = "0.2.0"
[[bin]] [[bin]]
name = "board_support" name = "board_support"
[[bin]]
name = "pac"
[[bin]] [[bin]]
name = "hal" name = "hal"
[[bin]]
name = "mmio"
[[bin]]
name = "pac"
[[bin]] [[bin]]
name = "typestate" name = "typestate"

View File

@ -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];

View 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 {}
}

View File

@ -0,0 +1,5 @@
# Raw MMIO
```rust,editable,compile_fail
{{#include examples/src/bin/mmio.rs:Example}}
```