From 0fa73286b90362785d7aa5e78aec318e7d1801a5 Mon Sep 17 00:00:00 2001 From: Andrew Walbran Date: Thu, 16 Feb 2023 03:16:49 +0000 Subject: [PATCH] Add raw MMIO example. --- src/SUMMARY.md | 2 +- .../microcontrollers/examples/Cargo.toml | 9 ++- .../examples/src/bin/interrupts/mod.rs | 17 +++++ .../microcontrollers/examples/src/bin/mmio.rs | 66 +++++++++++++++++++ src/bare-metal/microcontrollers/mmio.md | 5 ++ 5 files changed, 95 insertions(+), 4 deletions(-) create mode 100644 src/bare-metal/microcontrollers/examples/src/bin/interrupts/mod.rs create mode 100644 src/bare-metal/microcontrollers/examples/src/bin/mmio.rs create mode 100644 src/bare-metal/microcontrollers/mmio.md diff --git a/src/SUMMARY.md b/src/SUMMARY.md index b0f9382c..991fdd57 100644 --- a/src/SUMMARY.md +++ b/src/SUMMARY.md @@ -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) diff --git a/src/bare-metal/microcontrollers/examples/Cargo.toml b/src/bare-metal/microcontrollers/examples/Cargo.toml index 57e0cfb3..a3b3231c 100644 --- a/src/bare-metal/microcontrollers/examples/Cargo.toml +++ b/src/bare-metal/microcontrollers/examples/Cargo.toml @@ -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" diff --git a/src/bare-metal/microcontrollers/examples/src/bin/interrupts/mod.rs b/src/bare-metal/microcontrollers/examples/src/bin/interrupts/mod.rs new file mode 100644 index 00000000..2dc2402f --- /dev/null +++ b/src/bare-metal/microcontrollers/examples/src/bin/interrupts/mod.rs @@ -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]; diff --git a/src/bare-metal/microcontrollers/examples/src/bin/mmio.rs b/src/bare-metal/microcontrollers/examples/src/bin/mmio.rs new file mode 100644 index 00000000..32624e6e --- /dev/null +++ b/src/bare-metal/microcontrollers/examples/src/bin/mmio.rs @@ -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::()) as *mut u32; + let pin_cnf_28 = (GPIO_P0 + PIN_CNF + 28 * size_of::()) 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 {} +} diff --git a/src/bare-metal/microcontrollers/mmio.md b/src/bare-metal/microcontrollers/mmio.md new file mode 100644 index 00000000..9f827cf3 --- /dev/null +++ b/src/bare-metal/microcontrollers/mmio.md @@ -0,0 +1,5 @@ +# Raw MMIO + +```rust,editable,compile_fail +{{#include examples/src/bin/mmio.rs:Example}} +```