Skip to content

Commit 0ce1175

Browse files
author
Jorge Aparicio
committed
initialize the FPU
closes #2
1 parent bb0828b commit 0ce1175

File tree

5 files changed

+64
-0
lines changed

5 files changed

+64
-0
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
77

88
## [Unreleased]
99

10+
### Added
11+
12+
- Initialize the FPU before main
13+
1014
## [v0.1.0] - 2016-10-04
1115

1216
### Added

examples/fpu.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//! You can do fast, single precision, floating point math as well
2+
3+
#![feature(asm)]
4+
#![no_main]
5+
#![no_std]
6+
7+
#[macro_use]
8+
extern crate f3;
9+
10+
#[export_name = "main"]
11+
pub extern "C" fn main() -> ! {
12+
let x = black_box(2_f32);
13+
let y = black_box(3_f32);
14+
let z = black_box(x * y);
15+
16+
iprintln!("{} * {} = {}", x, y, z);
17+
18+
loop {}
19+
}
20+
21+
// Magic to prevent optimizations like "constant folding" which is when LLVM
22+
// performs arithmetic at compile time. This is a copy of `test::black_box`.
23+
fn black_box<T>(x: T) -> T {
24+
unsafe {
25+
asm!("" :: "r"(&x));
26+
}
27+
28+
x
29+
}

src/fpu.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//! Floating Point Unit (FPU)
2+
3+
use cortex_m;
4+
5+
/// Initializes the FPU
6+
///
7+
/// # Safety
8+
///
9+
/// - Must be called once
10+
/// - Must be called in an interrupt-free environment
11+
pub unsafe fn init() {
12+
let scb = cortex_m::peripheral::scb_mut();
13+
14+
// Enable the FPU co-processor
15+
let cpacr = scb.cpacr.read();
16+
scb.cpacr.write({
17+
// Enable privileged access to the co-processor 10
18+
const CP10: u32 = 0b01 << 20;
19+
// Enable privileged access to the co-processor 11
20+
const CP11: u32 = 0b01 << 22;
21+
22+
cpacr | CP10 | CP11
23+
});
24+
}

src/itm.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ impl Write for Port {
2323
}
2424

2525
/// Initializes the ITM port
26+
///
27+
/// # Safety
28+
///
29+
/// - Must be called once
30+
/// - Must be called in an interrupt-free environment
2631
pub unsafe fn init() {
2732
let dbgmcu = peripheral::dbgmcu_mut();
2833
let dcb = cortex_m::peripheral::dcb_mut();

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ mod lang_items;
142142
pub mod delay;
143143
pub mod examples;
144144
pub mod exception;
145+
pub mod fpu;
145146
pub mod itm;
146147
pub mod led;
147148
pub mod peripheral;
@@ -152,6 +153,7 @@ pub mod peripheral;
152153
#[linkage = "weak"]
153154
pub unsafe extern "C" fn init() {
154155
delay::init();
156+
fpu::init();
155157
itm::init();
156158
led::init();
157159
}

0 commit comments

Comments
 (0)