From 16590c93c81432eeb42f349801673a6ac1c017c9 Mon Sep 17 00:00:00 2001 From: Tilman Sauerbeck Date: Sat, 6 Jul 2019 18:16:11 +0200 Subject: [PATCH] common: Add the port module. --- SConscript.libcommon | 1 + src/common/lib.rs | 1 + src/common/port.rs | 80 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 82 insertions(+) create mode 100644 src/common/port.rs diff --git a/SConscript.libcommon b/SConscript.libcommon index 227c80a..158205f 100644 --- a/SConscript.libcommon +++ b/SConscript.libcommon @@ -7,6 +7,7 @@ source_files_rs = [ 'src/common/nvic.rs', 'src/common/clock.rs', 'src/common/systick.rs', + 'src/common/port.rs', 'src/common/crc32.rs', 'src/common/buffer.rs', 'src/common/usb_serial.rs', diff --git a/src/common/lib.rs b/src/common/lib.rs index e93861e..17a3587 100644 --- a/src/common/lib.rs +++ b/src/common/lib.rs @@ -29,6 +29,7 @@ pub mod register; pub mod nvic; pub mod clock; pub mod systick; +pub mod port; pub mod crc32; pub mod buffer; pub mod usb_serial; diff --git a/src/common/port.rs b/src/common/port.rs new file mode 100644 index 0000000..0dc95a8 --- /dev/null +++ b/src/common/port.rs @@ -0,0 +1,80 @@ +/* + * 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 PORT_BASE: u32 = 0x40049000; + +pub const PORTA: u32 = PORT_BASE + 0x0000; +pub const PORTB: u32 = PORT_BASE + 0x1000; +pub const PORTC: u32 = PORT_BASE + 0x2000; +pub const PORTD: u32 = PORT_BASE + 0x3000; +pub const PORTE: u32 = PORT_BASE + 0x4000; + +const PORT_PCR_MUX_SHIFT: u32 = 8; +const PORT_PCR_MUX_MASK: u32 = 3 << PORT_PCR_MUX_SHIFT; + +const PORT_PCR_PE: u32 = 1 << 1; +const PORT_PCR_PS: u32 = 1 << 0; + +pub enum Pull { + None, + Up, + Down, +} + +fn pcr_offset(pin: u32) -> u32 { + pin * 4 +} + +pub fn set_af(port: u32, pin: u32, af: u32) { + let mut pcr = Reg32::new(port + pcr_offset(pin)); + + pcr.modify(|v| { + (v & !PORT_PCR_MUX_MASK) | (af << PORT_PCR_MUX_SHIFT) + }); +} + +pub fn set_pull(port: u32, pin: u32, pull: Pull) { + let mut pcr = Reg32::new(port + pcr_offset(pin)); + + match pull { + Pull::None => { + pcr.modify(|v| { + v & !PORT_PCR_PE + }); + }, + Pull::Up => { + pcr.modify(|v| { + v | PORT_PCR_PE | PORT_PCR_PS + }); + }, + Pull::Down => { + pcr.modify(|v| { + (v | PORT_PCR_PE) & !PORT_PCR_PS + }); + }, + } +} -- 2.30.2