Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add gpiohs support #9

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -13,7 +13,7 @@ edition = "2018"
targets = ["riscv64gc-unknown-none-elf"]

[dependencies]
embedded-hal = "1.0.0-alpha.1"
nb = "0.1.1"
embedded-hal = { version = "0.2.4", features = ["unproven"] }
nb = "1.0.0"
k210-pac = "0.2.0"
bitflags = "1.2.1"
38 changes: 19 additions & 19 deletions src/gpio.rs
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@ use crate::pac;
use crate::sysctl::{self, APB0};
use crate::fpioa::{IoPin, Pull, Mode};
use crate::bit_utils::{u32_set_bit, u32_toggle_bit, u32_bit_is_set, u32_bit_is_clear};
use embedded_hal::digital::{OutputPin, StatefulOutputPin, InputPin, ToggleableOutputPin};
use embedded_hal::digital::v2::{OutputPin, StatefulOutputPin, InputPin, ToggleableOutputPin};

/// Extension trait to split a GPIO peripheral into independent pins
pub trait GpioExt {
@@ -15,15 +15,15 @@ pub trait GpioExt {

macro_rules! def_gpio_pins {
($($GPIOX: ident: ($num: expr, $gpiox: ident, $func: ident);)+) => {

impl GpioExt for pac::GPIO {
fn split(self, apb0: &mut APB0) -> Parts {
// enable APB0 bus
apb0.enable();
// enable sysctl peripheral
sysctl::clk_en_peri().modify(|_r, w| w.gpio_clk_en().set_bit());
// return ownership
Parts {
Parts {
$( $gpiox: $GPIOX { _ownership: () }, )+
}
}
@@ -151,15 +151,15 @@ impl<GPIO: GpioIndex, PIN: IoPin, MODE: Active> Gpio<GPIO, PIN, MODE> {

#[inline]
fn direction_in(&mut self) {
unsafe {
unsafe {
let p = &(*pac::GPIO::ptr()).direction as *const _ as *mut _;
u32_set_bit(p, false, GPIO::INDEX as usize);
}
}

#[inline]
fn direction_out(&mut self) {
unsafe {
unsafe {
let p = &(*pac::GPIO::ptr()).direction as *const _ as *mut _;
u32_set_bit(p, true, GPIO::INDEX as usize);
}
@@ -169,15 +169,15 @@ impl<GPIO: GpioIndex, PIN: IoPin, MODE: Active> Gpio<GPIO, PIN, MODE> {
impl<GPIO: GpioIndex, PIN, MODE> InputPin for Gpio<GPIO, PIN, Input<MODE>> {
type Error = core::convert::Infallible;

fn try_is_high(&self) -> Result<bool, Self::Error> {
Ok(unsafe {
fn is_high(&self) -> Result<bool, Self::Error> {
Ok(unsafe {
let p = &(*pac::GPIO::ptr()).data_input as *const _ as *const _;
u32_bit_is_set(p, GPIO::INDEX as usize)
})
}

fn try_is_low(&self) -> Result<bool, Self::Error> {
Ok(unsafe {
fn is_low(&self) -> Result<bool, Self::Error> {
Ok(unsafe {
let p = &(*pac::GPIO::ptr()).data_input as *const _ as *const _;
u32_bit_is_clear(p, GPIO::INDEX as usize)
})
@@ -187,16 +187,16 @@ impl<GPIO: GpioIndex, PIN, MODE> InputPin for Gpio<GPIO, PIN, Input<MODE>> {
impl<GPIO: GpioIndex, PIN> OutputPin for Gpio<GPIO, PIN, Output> {
type Error = core::convert::Infallible;

fn try_set_high(&mut self) -> Result<(), Self::Error> {
unsafe {
fn set_high(&mut self) -> Result<(), Self::Error> {
unsafe {
let p = &(*pac::GPIO::ptr()).data_output as *const _ as *mut _;
u32_set_bit(p, true, GPIO::INDEX as usize);
}
Ok(())
}

fn try_set_low(&mut self) -> Result<(), Self::Error> {
unsafe {
fn set_low(&mut self) -> Result<(), Self::Error> {
unsafe {
let p = &(*pac::GPIO::ptr()).data_output as *const _ as *mut _;
u32_set_bit(p, false, GPIO::INDEX as usize);
}
@@ -205,15 +205,15 @@ impl<GPIO: GpioIndex, PIN> OutputPin for Gpio<GPIO, PIN, Output> {
}

impl<GPIO: GpioIndex, PIN> StatefulOutputPin for Gpio<GPIO, PIN, Output> {
fn try_is_set_high(&self) -> Result<bool, Self::Error> {
Ok(unsafe {
fn is_set_high(&self) -> Result<bool, Self::Error> {
Ok(unsafe {
let p = &(*pac::GPIO::ptr()).data_output as *const _ as *const _;
u32_bit_is_set(p, GPIO::INDEX as usize)
})
}

fn try_is_set_low(&self) -> Result<bool, Self::Error> {
Ok(unsafe {
fn is_set_low(&self) -> Result<bool, Self::Error> {
Ok(unsafe {
let p = &(*pac::GPIO::ptr()).data_output as *const _ as *const _;
u32_bit_is_clear(p, GPIO::INDEX as usize)
})
@@ -223,8 +223,8 @@ impl<GPIO: GpioIndex, PIN> StatefulOutputPin for Gpio<GPIO, PIN, Output> {
impl<GPIO: GpioIndex, PIN> ToggleableOutputPin for Gpio<GPIO, PIN, Output> {
type Error = core::convert::Infallible;

fn try_toggle(&mut self) -> Result<(), Self::Error> {
unsafe {
fn toggle(&mut self) -> Result<(), Self::Error> {
unsafe {
let p = &(*pac::GPIO::ptr()).data_output as *const _ as *mut _;
u32_toggle_bit(p, GPIO::INDEX as usize);
}
Loading