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

embedded-hal v1.0.0-alpha.8 support #45

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
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 .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ jobs:
continue-on-error: ${{ matrix.experimental || false }}
strategy:
matrix:
# All generated code should be running on stable now, MRSV is 1.42.0
rust: [nightly, stable, 1.42.0]
# All generated code should be running on stable now, MRSV is 1.59.0
rust: [nightly, stable, 1.59.0]

include:
# Nightly is only for reference and allowed to fail
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

- Updated to support `embedded-hal` version `1.0.0-alpha.7`

- Refactored `e310x-hal::spi` module, splitting the abstraction into `SpiBus` and `SpiExclusiveDevice/SpiSharedDevice` to allow multiple devices on a single SPI bus to co-exist

## [v0.9.3] - 2021-08-15
Expand Down
8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "e310x-hal"
version = "0.9.3"
version = "0.10.0-alpha.1"
authors = ["David Craven <[email protected]>"]
repository = "https://github.com/riscv-rust/e310x-hal"
categories = ["embedded", "hardware-support", "no-std"]
Expand All @@ -10,10 +10,10 @@ license = "ISC"
edition = "2018"

[dependencies]
embedded-hal = { version = "0.2.6", features = ["unproven"] }
embedded-hal = "=1.0.0-alpha.8"
nb = "1.0.0"
riscv = "0.7.0"
e310x = { version = "0.9.0", features = ["rt"] }
riscv = "0.9.0-alpha.1"
e310x = { version = "0.9.1", features = ["rt"] }

[features]
g002 = ["e310x/g002"]
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ This project is developed and maintained by the [RISC-V team][team].

## Minimum Supported Rust Version (MSRV)

This crate is guaranteed to compile on stable Rust 1.42.0 and up. It *might*
This crate is guaranteed to compile on stable Rust 1.59.0 and up. It *might*
compile with older versions but that may change in any new patch release.

## License
Expand Down
93 changes: 13 additions & 80 deletions src/delay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

use crate::clock::Clocks;
use crate::core::clint::{MTIME, MTIMECMP};
use embedded_hal::blocking::delay::{DelayMs, DelayUs};
use core::convert::Infallible;
use embedded_hal::delay::blocking::DelayUs;
use riscv::register::{mie, mip};

/// Machine timer (mtime) as a busyloop delay provider
Expand All @@ -17,65 +18,16 @@ impl Delay {
}
}

impl DelayUs<u32> for Delay {
fn delay_us(&mut self, us: u32) {
impl DelayUs for Delay {
type Error = Infallible;

fn delay_us(&mut self, us: u32) -> Result<(), Infallible> {
let ticks = (us as u64) * TICKS_PER_SECOND / 1_000_000;

let mtime = MTIME;
let t = mtime.mtime() + ticks;
while mtime.mtime() < t {}
}
}

// This is a workaround to allow `delay_us(42)` construction without specifying a type.
impl DelayUs<i32> for Delay {
#[inline(always)]
fn delay_us(&mut self, us: i32) {
assert!(us >= 0);
self.delay_us(us as u32);
}
}

impl DelayUs<u16> for Delay {
#[inline(always)]
fn delay_us(&mut self, us: u16) {
self.delay_us(u32::from(us));
}
}

impl DelayUs<u8> for Delay {
#[inline(always)]
fn delay_us(&mut self, us: u8) {
self.delay_us(u32::from(us));
}
}

impl DelayMs<u32> for Delay {
fn delay_ms(&mut self, ms: u32) {
self.delay_us(ms * 1000);
}
}

// This is a workaround to allow `delay_ms(42)` construction without specifying a type.
impl DelayMs<i32> for Delay {
#[inline(always)]
fn delay_ms(&mut self, ms: i32) {
assert!(ms >= 0);
self.delay_ms(ms as u32);
}
}

impl DelayMs<u16> for Delay {
#[inline(always)]
fn delay_ms(&mut self, ms: u16) {
self.delay_ms(u32::from(ms));
}
}

impl DelayMs<u8> for Delay {
#[inline(always)]
fn delay_ms(&mut self, ms: u8) {
self.delay_ms(u32::from(ms));
Ok(())
}
}

Expand All @@ -95,9 +47,11 @@ impl Sleep {
}
}

impl DelayMs<u32> for Sleep {
fn delay_ms(&mut self, ms: u32) {
let ticks = (ms as u64) * (self.clock_freq as u64) / 1000;
impl DelayUs for Sleep {
type Error = Infallible;

fn delay_us(&mut self, us: u32) -> Result<(), Infallible> {
let ticks = (us as u64) * (self.clock_freq as u64) / 1_000_000;
let t = MTIME.mtime() + ticks;

self.mtimecmp.set_mtimecmp(t);
Expand Down Expand Up @@ -126,28 +80,7 @@ impl DelayMs<u32> for Sleep {
unsafe {
mie::clear_mtimer();
}
}
}

// This is a workaround to allow `delay_ms(42)` construction without specifying a type.
impl DelayMs<i32> for Sleep {
#[inline(always)]
fn delay_ms(&mut self, ms: i32) {
assert!(ms >= 0);
self.delay_ms(ms as u32);
}
}

impl DelayMs<u16> for Sleep {
#[inline(always)]
fn delay_ms(&mut self, ms: u16) {
self.delay_ms(u32::from(ms));
}
}

impl DelayMs<u8> for Sleep {
#[inline(always)]
fn delay_ms(&mut self, ms: u8) {
self.delay_ms(u32::from(ms));
Ok(())
}
}
13 changes: 8 additions & 5 deletions src/gpio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,8 @@ macro_rules! gpio {
use core::marker::PhantomData;
use core::convert::Infallible;

use embedded_hal::digital::v2::{InputPin, OutputPin, StatefulOutputPin,
use embedded_hal::digital::ErrorType;
use embedded_hal::digital::blocking::{InputPin, OutputPin, StatefulOutputPin,
ToggleableOutputPin};
use e310x::$GPIOX;
use super::{Unknown, IOF0, IOF1, Drive, Floating, GpioExt, Input, Invert,
Expand Down Expand Up @@ -275,9 +276,11 @@ macro_rules! gpio {
}
}

impl<MODE> InputPin for $PXi<Input<MODE>> {
impl<MODE> ErrorType for $PXi<Input<MODE>> {
type Error = Infallible;
}

impl<MODE> InputPin for $PXi<Input<MODE>> {
fn is_high(&self) -> Result<bool, Infallible> {
Ok($GPIOX::input_value(Self::INDEX))

Expand All @@ -298,9 +301,11 @@ macro_rules! gpio {
}
}

impl<MODE> OutputPin for $PXi<Output<MODE>> {
impl<MODE> ErrorType for $PXi<Output<MODE>> {
type Error = Infallible;
}

impl<MODE> OutputPin for $PXi<Output<MODE>> {
fn set_high(&mut self) -> Result<(), Infallible> {
$GPIOX::set_output_value(Self::INDEX, true);
Ok(())
Expand All @@ -313,8 +318,6 @@ macro_rules! gpio {
}

impl<MODE> ToggleableOutputPin for $PXi<Output<MODE>> {
type Error = Infallible;

/// Toggles the pin state.
fn toggle(&mut self) -> Result<(), Infallible> {
$GPIOX::toggle_pin(Self::INDEX);
Expand Down
Loading