
commit
97c25a1631
5 changed files with 131 additions and 0 deletions
@ -0,0 +1,15 @@
|
||||
[package] |
||||
name = "k210-hal" |
||||
version = "0.1.0" |
||||
authors = ["The RISC-V Team <risc-v@teams.rust-embedded.org>"] |
||||
categories = ["embedded", "hardware-support", "no-std"] |
||||
description = "HAL for K210 SoC" |
||||
keywords = ["riscv", "k210", "hal"] |
||||
edition = "2018" |
||||
|
||||
[dependencies] |
||||
embedded-hal = { version = "0.2.1", features = ["unproven"] } |
||||
nb = "0.1.1" |
||||
riscv-rt = "0.5.0" |
||||
k210-pac = { version = "0.1.0", features = ["rt"] } |
||||
void = { version = "1.0.2", default-features = false } |
@ -0,0 +1,12 @@
|
||||
//! HAL for the K210 SoC
|
||||
//!
|
||||
//! This is an implementation of the [`embedded-hal`] traits for the K210 SoC
|
||||
|
||||
#![deny(missing_docs)] |
||||
#![deny(warnings)] |
||||
#![no_std] |
||||
|
||||
pub use k210_pac as pac; |
||||
|
||||
pub mod stdout; |
||||
pub mod time; |
@ -0,0 +1,33 @@
|
||||
//! Stdout
|
||||
pub use core::fmt::Write; |
||||
use nb::block; |
||||
|
||||
/// Stdout implements the core::fmt::Write trait for hal::serial::Write
|
||||
/// implementations.
|
||||
pub struct Stdout<'p, T>(pub &'p mut T) |
||||
where |
||||
T: 'p; |
||||
|
||||
impl<'p, T> Write for Stdout<'p, T> |
||||
where |
||||
T: embedded_hal::serial::Write<u8>, |
||||
{ |
||||
fn write_str(&mut self, s: &str) -> ::core::fmt::Result { |
||||
for byte in s.as_bytes() { |
||||
let res = block!(self.0.write(*byte)); |
||||
|
||||
if res.is_err() { |
||||
return Err(::core::fmt::Error); |
||||
} |
||||
|
||||
if *byte == '\n' as u8 { |
||||
let res = block!(self.0.write('\r' as u8)); |
||||
|
||||
if res.is_err() { |
||||
return Err(::core::fmt::Error); |
||||
} |
||||
} |
||||
} |
||||
Ok(()) |
||||
} |
||||
} |
@ -0,0 +1,68 @@
|
||||
//! Time units
|
||||
|
||||
/// Bits per second
|
||||
#[derive(Clone, Copy)] |
||||
pub struct Bps(pub u32); |
||||
|
||||
/// Hertz
|
||||
#[derive(Clone, Copy)] |
||||
pub struct Hertz(pub u32); |
||||
|
||||
/// KiloHertz
|
||||
#[derive(Clone, Copy)] |
||||
pub struct KiloHertz(pub u32); |
||||
|
||||
/// MegaHertz
|
||||
#[derive(Clone, Copy)] |
||||
pub struct MegaHertz(pub u32); |
||||
|
||||
/// Extension trait that adds convenience methods to the `u32` type
|
||||
pub trait U32Ext { |
||||
/// Wrap in `Bps`
|
||||
fn bps(self) -> Bps; |
||||
|
||||
/// Wrap in `Hertz`
|
||||
fn hz(self) -> Hertz; |
||||
|
||||
/// Wrap in `KiloHertz`
|
||||
fn khz(self) -> KiloHertz; |
||||
|
||||
/// Wrap in `MegaHertz`
|
||||
fn mhz(self) -> MegaHertz; |
||||
} |
||||
|
||||
impl U32Ext for u32 { |
||||
fn bps(self) -> Bps { |
||||
Bps(self) |
||||
} |
||||
|
||||
fn hz(self) -> Hertz { |
||||
Hertz(self) |
||||
} |
||||
|
||||
fn khz(self) -> KiloHertz { |
||||
KiloHertz(self) |
||||
} |
||||
|
||||
fn mhz(self) -> MegaHertz { |
||||
MegaHertz(self) |
||||
} |
||||
} |
||||
|
||||
impl Into<Hertz> for KiloHertz { |
||||
fn into(self) -> Hertz { |
||||
Hertz(self.0 * 1_000) |
||||
} |
||||
} |
||||
|
||||
impl Into<Hertz> for MegaHertz { |
||||
fn into(self) -> Hertz { |
||||
Hertz(self.0 * 1_000_000) |
||||
} |
||||
} |
||||
|
||||
impl Into<KiloHertz> for MegaHertz { |
||||
fn into(self) -> KiloHertz { |
||||
KiloHertz(self.0 * 1_000) |
||||
} |
||||
} |
Loading…
Reference in new issue