From 3fe13f96a191df4ee6a3f746b8bdd080744526d7 Mon Sep 17 00:00:00 2001 From: Tilman Sauerbeck Date: Sun, 16 Jun 2019 21:10:48 +0200 Subject: [PATCH] common: Add the nvic module. Allows enabling and disabling of interrupts as well as configuring their priority. --- SConscript.libcommon | 1 + src/common/lib.rs | 1 + src/common/nvic.rs | 64 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 66 insertions(+) create mode 100644 src/common/nvic.rs diff --git a/SConscript.libcommon b/SConscript.libcommon index 7482ee3..a23c79b 100644 --- a/SConscript.libcommon +++ b/SConscript.libcommon @@ -4,6 +4,7 @@ env = env.Clone() source_files_rs = [ 'src/common/lib.rs', # Must be listed first (see below). 'src/common/register.rs', + 'src/common/nvic.rs', ] source_files_c = [ diff --git a/src/common/lib.rs b/src/common/lib.rs index f2feb14..0fa0f6b 100644 --- a/src/common/lib.rs +++ b/src/common/lib.rs @@ -27,6 +27,7 @@ #![feature(lang_items)] pub mod register; +pub mod nvic; #[lang="eh_personality"] extern fn eh_personality() { diff --git a/src/common/nvic.rs b/src/common/nvic.rs new file mode 100644 index 0000000..38aece3 --- /dev/null +++ b/src/common/nvic.rs @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2019 Tilman Sauerbeck (tilman at code-monkey de) + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +use register; + +type Reg32 = register::Register; + +const NVIC_BASE: u32 = 0xe000e100; + +const NVIC_ISER: u32 = NVIC_BASE + 0x000; +const NVIC_ICER: u32 = NVIC_BASE + 0x080; +const NVIC_IP : u32 = NVIC_BASE + 0x300; + +pub fn enable_irq(irq_number: u32) { + let mut iser = Reg32::new(NVIC_ISER); + + iser.write(1 << irq_number); +} + +pub fn disable_irq(irq_number: u32) { + let mut icer = Reg32::new(NVIC_ICER); + + icer.write(1 << irq_number); +} + +fn ip_offset(irq_number: u32) -> u32 { + irq_number & !3 +} + +fn ip_shift(irq_number: u32) -> u32 { + (irq_number & 3) * 8 +} + +pub fn set_priority(irq_number: u32, priority: u32) { + let mut ip = Reg32::new(NVIC_IP + ip_offset(irq_number)); + + ip.modify(|v| { + let mut m = v; + + m &= !(0xff << ip_shift(irq_number)); + m |= priority << ip_shift(irq_number); + m + }); +} -- 2.30.2