aboutsummaryrefslogtreecommitdiff
path: root/cortex-m-rt/examples
diff options
context:
space:
mode:
Diffstat (limited to 'cortex-m-rt/examples')
-rw-r--r--cortex-m-rt/examples/alignment.rs33
-rw-r--r--cortex-m-rt/examples/cfg-static.rs25
-rw-r--r--cortex-m-rt/examples/data_overflow.rs30
-rw-r--r--cortex-m-rt/examples/device.rs36
-rw-r--r--cortex-m-rt/examples/divergent-default-handler.rs18
-rw-r--r--cortex-m-rt/examples/divergent-exception.rs18
-rw-r--r--cortex-m-rt/examples/entry-static.rs20
-rw-r--r--cortex-m-rt/examples/main.rs13
-rw-r--r--cortex-m-rt/examples/minimal.rs17
-rw-r--r--cortex-m-rt/examples/override-exception.rs29
-rw-r--r--cortex-m-rt/examples/pre_init.rs20
-rw-r--r--cortex-m-rt/examples/qemu.rs28
-rw-r--r--cortex-m-rt/examples/state.rs24
-rw-r--r--cortex-m-rt/examples/unsafe-default-handler.rs16
-rw-r--r--cortex-m-rt/examples/unsafe-entry.rs13
-rw-r--r--cortex-m-rt/examples/unsafe-exception.rs16
-rw-r--r--cortex-m-rt/examples/unsafe-hard-fault.rs18
-rw-r--r--cortex-m-rt/examples/unsafety.rs36
-rw-r--r--cortex-m-rt/examples/warnings.rs50
19 files changed, 460 insertions, 0 deletions
diff --git a/cortex-m-rt/examples/alignment.rs b/cortex-m-rt/examples/alignment.rs
new file mode 100644
index 0000000..4421e69
--- /dev/null
+++ b/cortex-m-rt/examples/alignment.rs
@@ -0,0 +1,33 @@
+//! This is not an example; this is a link-pass test
+
+#![deny(warnings)]
+#![no_main]
+#![no_std]
+
+extern crate cortex_m_rt as rt;
+extern crate panic_halt;
+
+use core::ptr;
+
+use rt::entry;
+
+static mut BSS1: u16 = 0;
+static mut BSS2: u8 = 0;
+static mut DATA1: u8 = 1;
+static mut DATA2: u16 = 1;
+static RODATA1: &[u8; 3] = b"012";
+static RODATA2: &[u8; 2] = b"34";
+
+#[entry]
+fn main() -> ! {
+ unsafe {
+ let _bss1 = ptr::read_volatile(&BSS1);
+ let _bss2 = ptr::read_volatile(&BSS2);
+ let _data1 = ptr::read_volatile(&DATA1);
+ let _data2 = ptr::read_volatile(&DATA2);
+ let _rodata1 = ptr::read_volatile(&RODATA1);
+ let _rodata2 = ptr::read_volatile(&RODATA2);
+ }
+
+ loop {}
+}
diff --git a/cortex-m-rt/examples/cfg-static.rs b/cortex-m-rt/examples/cfg-static.rs
new file mode 100644
index 0000000..2ffee13
--- /dev/null
+++ b/cortex-m-rt/examples/cfg-static.rs
@@ -0,0 +1,25 @@
+//! using `#[cfg]` on `static` shouldn't cause compile errors
+
+#![deny(unsafe_code)]
+#![deny(warnings)]
+#![no_main]
+#![no_std]
+
+extern crate cortex_m_rt as rt;
+extern crate panic_halt;
+
+use rt::{entry, exception};
+
+#[entry]
+fn main() -> ! {
+ #[cfg(never)]
+ static mut COUNT: u32 = 0;
+
+ loop {}
+}
+
+#[exception]
+fn SysTick() {
+ #[cfg(never)]
+ static mut FOO: u32 = 0;
+}
diff --git a/cortex-m-rt/examples/data_overflow.rs b/cortex-m-rt/examples/data_overflow.rs
new file mode 100644
index 0000000..a84ec12
--- /dev/null
+++ b/cortex-m-rt/examples/data_overflow.rs
@@ -0,0 +1,30 @@
+//! This is not an example; this is a linker overflow detection test
+//! which should fail to link due to .data overflowing FLASH.
+
+#![deny(warnings)]
+#![no_main]
+#![no_std]
+
+extern crate cortex_m_rt as rt;
+extern crate panic_halt;
+
+use core::ptr;
+
+use rt::entry;
+
+// This large static array uses most of .rodata
+static RODATA: [u8; 48 * 1024] = [1u8; 48 * 1024];
+
+// This large mutable array causes .data to use the rest of FLASH
+// without also overflowing RAM.
+static mut DATA: [u8; 16 * 1024] = [1u8; 16 * 1024];
+
+#[entry]
+fn main() -> ! {
+ unsafe {
+ let _bigdata = ptr::read_volatile(&RODATA as *const u8);
+ let _bigdata = ptr::read_volatile(&DATA as *const u8);
+ }
+
+ loop {}
+}
diff --git a/cortex-m-rt/examples/device.rs b/cortex-m-rt/examples/device.rs
new file mode 100644
index 0000000..c18b569
--- /dev/null
+++ b/cortex-m-rt/examples/device.rs
@@ -0,0 +1,36 @@
+//! Manually create the interrupts portion of the vector table
+
+#![deny(unsafe_code)]
+#![deny(warnings)]
+#![no_main]
+#![no_std]
+
+extern crate cortex_m_rt as rt;
+extern crate panic_halt;
+
+use rt::entry;
+
+#[entry]
+fn main() -> ! {
+ loop {}
+}
+
+// interrupts portion of the vector table
+pub union Vector {
+ handler: unsafe extern "C" fn(),
+ reserved: usize,
+}
+
+extern "C" {
+ fn WWDG();
+ fn PVD();
+}
+
+#[allow(unsafe_code)]
+#[link_section = ".vector_table.interrupts"]
+#[no_mangle]
+pub static __INTERRUPTS: [Vector; 3] = [
+ Vector { handler: WWDG },
+ Vector { reserved: 0 },
+ Vector { handler: PVD },
+];
diff --git a/cortex-m-rt/examples/divergent-default-handler.rs b/cortex-m-rt/examples/divergent-default-handler.rs
new file mode 100644
index 0000000..3290254
--- /dev/null
+++ b/cortex-m-rt/examples/divergent-default-handler.rs
@@ -0,0 +1,18 @@
+#![deny(warnings)]
+#![no_main]
+#![no_std]
+
+extern crate cortex_m_rt;
+extern crate panic_halt;
+
+use cortex_m_rt::{entry, exception};
+
+#[entry]
+fn foo() -> ! {
+ loop {}
+}
+
+#[exception]
+unsafe fn DefaultHandler(_irqn: i16) -> ! {
+ loop {}
+}
diff --git a/cortex-m-rt/examples/divergent-exception.rs b/cortex-m-rt/examples/divergent-exception.rs
new file mode 100644
index 0000000..cb2247b
--- /dev/null
+++ b/cortex-m-rt/examples/divergent-exception.rs
@@ -0,0 +1,18 @@
+#![deny(warnings)]
+#![no_main]
+#![no_std]
+
+extern crate cortex_m_rt;
+extern crate panic_halt;
+
+use cortex_m_rt::{entry, exception};
+
+#[entry]
+fn foo() -> ! {
+ loop {}
+}
+
+#[exception]
+fn SysTick() -> ! {
+ loop {}
+}
diff --git a/cortex-m-rt/examples/entry-static.rs b/cortex-m-rt/examples/entry-static.rs
new file mode 100644
index 0000000..55e7a89
--- /dev/null
+++ b/cortex-m-rt/examples/entry-static.rs
@@ -0,0 +1,20 @@
+//! `static mut` variables local to the entry point are safe to use
+
+#![deny(unsafe_code)]
+#![deny(warnings)]
+#![no_main]
+#![no_std]
+
+extern crate cortex_m_rt as rt;
+extern crate panic_halt;
+
+use rt::entry;
+
+#[entry]
+fn main() -> ! {
+ static mut COUNT: u32 = 0;
+
+ loop {
+ *COUNT += 1;
+ }
+}
diff --git a/cortex-m-rt/examples/main.rs b/cortex-m-rt/examples/main.rs
new file mode 100644
index 0000000..58e3cb4
--- /dev/null
+++ b/cortex-m-rt/examples/main.rs
@@ -0,0 +1,13 @@
+//! Directly plug a `main` symbol instead of using `#[entry]`
+
+#![deny(warnings)]
+#![no_main]
+#![no_std]
+
+extern crate cortex_m_rt as rt;
+extern crate panic_halt;
+
+#[no_mangle]
+pub unsafe extern "C" fn main() -> ! {
+ loop {}
+}
diff --git a/cortex-m-rt/examples/minimal.rs b/cortex-m-rt/examples/minimal.rs
new file mode 100644
index 0000000..bd0a6ad
--- /dev/null
+++ b/cortex-m-rt/examples/minimal.rs
@@ -0,0 +1,17 @@
+//! Minimal `cortex-m-rt` based program
+
+#![deny(unsafe_code)]
+#![deny(warnings)]
+#![no_main]
+#![no_std]
+
+extern crate cortex_m_rt as rt;
+extern crate panic_halt;
+
+use rt::entry;
+
+// the program entry point
+#[entry]
+fn main() -> ! {
+ loop {}
+}
diff --git a/cortex-m-rt/examples/override-exception.rs b/cortex-m-rt/examples/override-exception.rs
new file mode 100644
index 0000000..3190b77
--- /dev/null
+++ b/cortex-m-rt/examples/override-exception.rs
@@ -0,0 +1,29 @@
+//! How to override the hard fault exception handler and the default exception handler
+
+#![deny(warnings)]
+#![no_main]
+#![no_std]
+
+extern crate cortex_m;
+extern crate cortex_m_rt as rt;
+extern crate panic_halt;
+
+use cortex_m::asm;
+use rt::{entry, exception, ExceptionFrame};
+
+#[entry]
+fn main() -> ! {
+ loop {}
+}
+
+#[exception]
+unsafe fn DefaultHandler(_irqn: i16) {
+ asm::bkpt();
+}
+
+#[exception]
+unsafe fn HardFault(_ef: &ExceptionFrame) -> ! {
+ asm::bkpt();
+
+ loop {}
+}
diff --git a/cortex-m-rt/examples/pre_init.rs b/cortex-m-rt/examples/pre_init.rs
new file mode 100644
index 0000000..2c931bb
--- /dev/null
+++ b/cortex-m-rt/examples/pre_init.rs
@@ -0,0 +1,20 @@
+//! `cortex-m-rt` based program with a function run before RAM is initialized.
+
+#![deny(warnings)]
+#![no_main]
+#![no_std]
+
+extern crate cortex_m_rt as rt;
+extern crate panic_halt;
+
+use rt::{entry, pre_init};
+
+#[pre_init]
+unsafe fn disable_watchdog() {
+ // Do what you need to disable the watchdog.
+}
+
+#[entry]
+fn main() -> ! {
+ loop {}
+}
diff --git a/cortex-m-rt/examples/qemu.rs b/cortex-m-rt/examples/qemu.rs
new file mode 100644
index 0000000..e903404
--- /dev/null
+++ b/cortex-m-rt/examples/qemu.rs
@@ -0,0 +1,28 @@
+// #![feature(stdsimd)]
+#![no_main]
+#![no_std]
+
+extern crate cortex_m;
+extern crate cortex_m_rt as rt;
+extern crate cortex_m_semihosting as semihosting;
+
+extern crate panic_halt;
+
+use cortex_m::asm;
+use rt::entry;
+
+#[entry]
+fn main() -> ! {
+ use core::fmt::Write;
+ let x = 42;
+
+ loop {
+ asm::nop();
+
+ // write something through semihosting interface
+ let mut hstdout = semihosting::hio::hstdout().unwrap();
+ write!(hstdout, "x = {}\n", x).unwrap();
+ // exit from qemu
+ semihosting::debug::exit(semihosting::debug::EXIT_SUCCESS);
+ }
+}
diff --git a/cortex-m-rt/examples/state.rs b/cortex-m-rt/examples/state.rs
new file mode 100644
index 0000000..ee6224d
--- /dev/null
+++ b/cortex-m-rt/examples/state.rs
@@ -0,0 +1,24 @@
+//! Preserving state across executions of an exception handler
+
+#![deny(unsafe_code)]
+#![deny(warnings)]
+#![no_main]
+#![no_std]
+
+extern crate cortex_m_rt as rt;
+extern crate panic_halt;
+
+use rt::{entry, exception};
+
+#[entry]
+fn main() -> ! {
+ loop {}
+}
+
+// exception handler with state
+#[exception]
+fn SysTick() {
+ static mut STATE: u32 = 0;
+
+ *STATE += 1;
+}
diff --git a/cortex-m-rt/examples/unsafe-default-handler.rs b/cortex-m-rt/examples/unsafe-default-handler.rs
new file mode 100644
index 0000000..a805c12
--- /dev/null
+++ b/cortex-m-rt/examples/unsafe-default-handler.rs
@@ -0,0 +1,16 @@
+#![deny(warnings)]
+#![no_main]
+#![no_std]
+
+extern crate cortex_m_rt;
+extern crate panic_halt;
+
+use cortex_m_rt::{entry, exception};
+
+#[entry]
+fn foo() -> ! {
+ loop {}
+}
+
+#[exception]
+unsafe fn DefaultHandler(_irqn: i16) {}
diff --git a/cortex-m-rt/examples/unsafe-entry.rs b/cortex-m-rt/examples/unsafe-entry.rs
new file mode 100644
index 0000000..9dcbf33
--- /dev/null
+++ b/cortex-m-rt/examples/unsafe-entry.rs
@@ -0,0 +1,13 @@
+#![deny(warnings)]
+#![no_main]
+#![no_std]
+
+extern crate cortex_m_rt;
+extern crate panic_halt;
+
+use cortex_m_rt::entry;
+
+#[entry]
+unsafe fn foo() -> ! {
+ loop {}
+}
diff --git a/cortex-m-rt/examples/unsafe-exception.rs b/cortex-m-rt/examples/unsafe-exception.rs
new file mode 100644
index 0000000..4212610
--- /dev/null
+++ b/cortex-m-rt/examples/unsafe-exception.rs
@@ -0,0 +1,16 @@
+#![deny(warnings)]
+#![no_main]
+#![no_std]
+
+extern crate cortex_m_rt;
+extern crate panic_halt;
+
+use cortex_m_rt::{entry, exception};
+
+#[entry]
+fn foo() -> ! {
+ loop {}
+}
+
+#[exception]
+unsafe fn SysTick() {}
diff --git a/cortex-m-rt/examples/unsafe-hard-fault.rs b/cortex-m-rt/examples/unsafe-hard-fault.rs
new file mode 100644
index 0000000..b1d48f3
--- /dev/null
+++ b/cortex-m-rt/examples/unsafe-hard-fault.rs
@@ -0,0 +1,18 @@
+#![deny(warnings)]
+#![no_main]
+#![no_std]
+
+extern crate cortex_m_rt;
+extern crate panic_halt;
+
+use cortex_m_rt::{entry, exception, ExceptionFrame};
+
+#[entry]
+fn foo() -> ! {
+ loop {}
+}
+
+#[exception]
+unsafe fn HardFault(_ef: &ExceptionFrame) -> ! {
+ loop {}
+}
diff --git a/cortex-m-rt/examples/unsafety.rs b/cortex-m-rt/examples/unsafety.rs
new file mode 100644
index 0000000..cdb5aca
--- /dev/null
+++ b/cortex-m-rt/examples/unsafety.rs
@@ -0,0 +1,36 @@
+//! Checks that the declared unsafety is respected by the attributes
+
+#![deny(warnings)]
+#![no_main]
+#![no_std]
+
+extern crate cortex_m_rt;
+extern crate panic_halt;
+
+use cortex_m_rt::{entry, exception, ExceptionFrame};
+
+#[entry]
+unsafe fn main() -> ! {
+ foo();
+
+ loop {}
+}
+
+#[exception]
+unsafe fn DefaultHandler(_irqn: i16) {
+ foo();
+}
+
+#[exception]
+unsafe fn HardFault(_ef: &ExceptionFrame) -> ! {
+ foo();
+
+ loop {}
+}
+
+#[exception]
+unsafe fn SysTick() {
+ foo();
+}
+
+unsafe fn foo() {}
diff --git a/cortex-m-rt/examples/warnings.rs b/cortex-m-rt/examples/warnings.rs
new file mode 100644
index 0000000..3372003
--- /dev/null
+++ b/cortex-m-rt/examples/warnings.rs
@@ -0,0 +1,50 @@
+//! Tests that a crate can still build with all warnings enabled.
+//!
+//! The code generated by the `cortex-m-rt` macros might need to manually
+//! `#[allow]` some of them (even though Rust does that by default for a few
+//! warnings too).
+
+#![no_std]
+#![no_main]
+#![deny(warnings, missing_docs, rust_2018_idioms)]
+
+extern crate cortex_m_rt;
+extern crate panic_halt;
+
+use cortex_m_rt::{entry, exception, interrupt, pre_init, ExceptionFrame};
+
+#[allow(non_camel_case_types)]
+enum interrupt {
+ INT,
+}
+
+extern "C" {
+ fn INT();
+}
+
+union Vector {
+ #[allow(dead_code)]
+ handler: unsafe extern "C" fn(),
+}
+
+#[link_section = ".vector_table.interrupts"]
+#[no_mangle]
+#[used]
+static __INTERRUPTS: [Vector; 1] = [Vector { handler: INT }];
+
+/// Dummy interrupt.
+#[interrupt]
+fn INT() {}
+
+#[exception]
+fn HardFault(_eh: &ExceptionFrame) -> ! {
+ loop {}
+}
+
+#[entry]
+fn main() -> ! {
+ loop {}
+}
+
+#[pre_init]
+unsafe fn pre_init() {}