From 25e971278f1433ea161fc69b2648ad3bbef390af Mon Sep 17 00:00:00 2001 From: Tilman Sauerbeck Date: Sat, 6 Jul 2019 18:17:43 +0200 Subject: [PATCH] common: Add the gpio module. --- SConscript.libcommon | 1 + src/common/gpio.rs | 74 ++++++++++++++++++++++++++++++++++++++++++++ src/common/lib.rs | 1 + 3 files changed, 76 insertions(+) create mode 100644 src/common/gpio.rs diff --git a/SConscript.libcommon b/SConscript.libcommon index 158205f..a8534c8 100644 --- a/SConscript.libcommon +++ b/SConscript.libcommon @@ -8,6 +8,7 @@ source_files_rs = [ 'src/common/clock.rs', 'src/common/systick.rs', 'src/common/port.rs', + 'src/common/gpio.rs', 'src/common/crc32.rs', 'src/common/buffer.rs', 'src/common/usb_serial.rs', diff --git a/src/common/gpio.rs b/src/common/gpio.rs new file mode 100644 index 0000000..6f6dd3d --- /dev/null +++ b/src/common/gpio.rs @@ -0,0 +1,74 @@ +/* + * 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 GPIO_BASE: u32 = 0x400ff000; + +pub const GPIOA: u32 = GPIO_BASE + 0x000; +pub const GPIOB: u32 = GPIO_BASE + 0x040; +pub const GPIOC: u32 = GPIO_BASE + 0x080; +pub const GPIOD: u32 = GPIO_BASE + 0x0c0; +pub const GPIOE: u32 = GPIO_BASE + 0x100; + +pub enum Direction { + Input, + Output +} + +const GPIO_PSOR: u32 = 0x04; +const GPIO_PCOR: u32 = 0x08; +const GPIO_PTOR: u32 = 0x0c; +const GPIO_PDIR: u32 = 0x10; +const GPIO_PDDR: u32 = 0x14; + +pub fn set(gpio: u32, pin_mask: u32) { + Reg32::new(gpio + GPIO_PSOR).write(pin_mask); +} + +pub fn clear(gpio: u32, pin_mask: u32) { + Reg32::new(gpio + GPIO_PCOR).write(pin_mask); +} + +pub fn toggle(gpio: u32, pin_mask: u32) { + Reg32::new(gpio + GPIO_PTOR).write(pin_mask); +} + +pub fn set_direction(gpio: u32, pin_mask: u32, direction: Direction) { + let mut pddr = Reg32::new(gpio + GPIO_PDDR); + + match direction { + Direction::Output => { + pddr.modify(|v| v | pin_mask); + }, + Direction::Input => { + pddr.modify(|v| v & !pin_mask); + } + } +} + +pub fn get(gpio: u32) -> u32 { + Reg32::new(gpio + GPIO_PDIR).read() +} diff --git a/src/common/lib.rs b/src/common/lib.rs index 17a3587..7828fea 100644 --- a/src/common/lib.rs +++ b/src/common/lib.rs @@ -30,6 +30,7 @@ pub mod nvic; pub mod clock; pub mod systick; pub mod port; +pub mod gpio; pub mod crc32; pub mod buffer; pub mod usb_serial; -- 2.30.2