Skip to content
Merged
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
1 change: 1 addition & 0 deletions artinchip-hal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ embedded-hal = "1.0.0"
embedded-time = "0.12.1"
embedded-io = "0.7.1"
riscv = "0.15.0"
xuantie-riscv = {git = "https://github.com/rustsbi/xuantie.git", branch = "main"}

[features]
d12x = []
Expand Down
2 changes: 2 additions & 0 deletions artinchip-hal/src/axi_cfg.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! AXI Configuration (AXICFG).

mod instance;
mod register;

pub use instance::AxiCfg;
pub use register::*;
25 changes: 25 additions & 0 deletions artinchip-hal/src/axi_cfg/instance.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//! AXI CFG instance.

use super::register::RegisterBlock;
use core::marker::PhantomData;

/// AXI CFG instance.
pub struct AxiCfg {
reg: *const RegisterBlock,
_private: PhantomData<()>,
}

impl AxiCfg {
/// Create a new AXI CFG instance.
pub const fn __new(reg: *const RegisterBlock) -> Self {
Self {
reg,
_private: PhantomData,
}
}

/// Get a reference to the register block.
pub const fn register_block(&self) -> &'static RegisterBlock {
unsafe { &*self.reg }
}
}
2 changes: 2 additions & 0 deletions artinchip-hal/src/ce.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! Crypto Engine (CE).

mod instance;
mod register;

pub use instance::Ce;
pub use register::*;
25 changes: 25 additions & 0 deletions artinchip-hal/src/ce/instance.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//! CE instance.

use super::register::RegisterBlock;
use core::marker::PhantomData;

/// CE instance.
pub struct Ce {
reg: *const RegisterBlock,
_private: PhantomData<()>,
}

impl Ce {
/// Create a new CE instance.
pub const fn __new(reg: *const RegisterBlock) -> Self {
Self {
reg,
_private: PhantomData,
}
}

/// Get a reference to the register block.
pub const fn register_block(&self) -> &'static RegisterBlock {
unsafe { &*self.reg }
}
}
7 changes: 7 additions & 0 deletions artinchip-hal/src/clic.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//! Xuantie CLIC (Core Local Interrupt Controller).

mod instance;
mod register;

pub use instance::Clic;
pub use register::*;
25 changes: 25 additions & 0 deletions artinchip-hal/src/clic/instance.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//! CLIC instance.

use super::register::RegisterBlock;
use core::marker::PhantomData;

/// CLIC instance.
pub struct Clic {
reg: *const RegisterBlock,
_private: PhantomData<()>,
}

impl Clic {
/// Create a new CLIC instance.
pub const fn __new(reg: *const RegisterBlock) -> Self {
Self {
reg,
_private: PhantomData,
}
}

/// Get a reference to the register block.
pub const fn register_block(&self) -> &'static RegisterBlock {
unsafe { &*self.reg }
}
}
3 changes: 3 additions & 0 deletions artinchip-hal/src/clic/register.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
//! CLIC register blocks and registers.

pub use xuantie_riscv::peripheral::clic::Clic as RegisterBlock;
7 changes: 7 additions & 0 deletions artinchip-hal/src/clint.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//! Xuantie CLINT (Core Local Interrupt).

mod instance;
mod register;

pub use instance::Clint;
pub use register::*;
25 changes: 25 additions & 0 deletions artinchip-hal/src/clint/instance.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//! CLINT instance.

use super::register::RegisterBlock;
use core::marker::PhantomData;

/// CLINT instance.
pub struct Clint {
reg: *const RegisterBlock,
_private: PhantomData<()>,
}

impl Clint {
/// Create a new CLINT instance.
pub const fn __new(reg: *const RegisterBlock) -> Self {
Self {
reg,
_private: PhantomData,
}
}

/// Get a reference to the register block.
pub const fn register_block(&self) -> &'static RegisterBlock {
unsafe { &*self.reg }
}
}
3 changes: 3 additions & 0 deletions artinchip-hal/src/clint/register.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
//! CLINT register blocks and registers.

pub use xuantie_riscv::peripheral::clint::THeadClint as RegisterBlock;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch.

2 changes: 2 additions & 0 deletions artinchip-hal/src/cmu.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! Clock Manage Unit (CMU).

mod instance;
mod register;

pub use instance::Cmu;
pub use register::*;
25 changes: 25 additions & 0 deletions artinchip-hal/src/cmu/instance.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//! CMU instance.

use super::register::RegisterBlock;
use core::marker::PhantomData;

/// CMU instance.
pub struct Cmu {
reg: *const RegisterBlock,
_private: PhantomData<()>,
}

impl Cmu {
/// Create a new CMU instance.
pub const fn __new(reg: *const RegisterBlock) -> Self {
Self {
reg,
_private: PhantomData,
}
}

/// Get a reference to the register block.
pub const fn register_block(&self) -> &'static RegisterBlock {
unsafe { &*self.reg }
}
}
2 changes: 2 additions & 0 deletions artinchip-hal/src/dma.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! Direct Memory Access (DMA).

mod instance;
mod register;

pub use instance::Dma;
pub use register::*;
25 changes: 25 additions & 0 deletions artinchip-hal/src/dma/instance.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//! DMA instance.

use super::register::RegisterBlock;
use core::marker::PhantomData;

/// DMA instance.
pub struct Dma {
reg: *const RegisterBlock,
_private: PhantomData<()>,
}

impl Dma {
/// Create a new DMA instance.
pub const fn __new(reg: *const RegisterBlock) -> Self {
Self {
reg,
_private: PhantomData,
}
}

/// Get a reference to the register block.
pub const fn register_block(&self) -> &'static RegisterBlock {
unsafe { &*self.reg }
}
}
16 changes: 13 additions & 3 deletions artinchip-hal/src/gtc/delay.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
//! Timer delay source.

use super::instance::Gtc;
use super::register::{CntFreq, RegisterBlock};
use crate::cmu;
use crate::cmu::Cmu;

/// Timer delay source.
pub struct TimerDelay<'a> {
Expand All @@ -10,8 +11,8 @@ pub struct TimerDelay<'a> {

impl<'a> TimerDelay<'a> {
/// Create a new TimerDelay instance.
pub fn new(reg: &'a RegisterBlock, freq: CntFreq, cmu: &cmu::RegisterBlock) -> Self {
let clk = &cmu.clock_gtc;
pub fn new(reg: &'a RegisterBlock, freq: CntFreq, cmu: &Cmu) -> Self {
let clk = &cmu.register_block().clock_gtc;
unsafe {
// Initialize module clock.
// Reference: https://aicdoc.artinchip.com/topics/ic/cmu/cmu-function2-d13x.html#topic_yvp_f24_4bc__table_qb3_bn5_ydc
Expand Down Expand Up @@ -58,4 +59,13 @@ impl<'a> TimerDelay<'a> {
pub fn delay_ms(&self, ms: u64) {
self.delay_us(ms * 1_000);
}

/// Free the TimerDelay and return GTC instance.
pub fn free(self, cmu: &Cmu) -> Gtc {
unsafe {
let clk = &cmu.register_block().clock_gtc;
clk.modify(|v| v.disable_bus_clk().enable_module_reset());
}
Gtc::__new(self.reg)
}
}
4 changes: 2 additions & 2 deletions artinchip-hal/src/gtc/gtc_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

use super::delay::TimerDelay;
use super::register::CntFreq;
use crate::cmu;
use crate::cmu::Cmu;

pub trait GtcExt<'a> {
/// Creates a new TimerDelay instance.
fn new_timer_delay(self, freq: CntFreq, cmu: &cmu::RegisterBlock) -> TimerDelay<'a>;
fn new_timer_delay(self, freq: CntFreq, cmu: &Cmu) -> TimerDelay<'a>;
}
6 changes: 3 additions & 3 deletions artinchip-hal/src/gtc/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
use super::delay::TimerDelay;
use super::gtc_ext::GtcExt;
use super::register::{CntFreq, RegisterBlock};
use crate::cmu;
use crate::cmu::Cmu;
use core::marker::PhantomData;

/// Generic Timer Counter.
/// GTC instance.
pub struct Gtc {
reg: *const RegisterBlock,
_private: PhantomData<()>,
Expand All @@ -28,7 +28,7 @@ impl Gtc {
}

impl GtcExt<'static> for Gtc {
fn new_timer_delay(self, freq: CntFreq, cmu: &cmu::RegisterBlock) -> TimerDelay<'static> {
fn new_timer_delay(self, freq: CntFreq, cmu: &Cmu) -> TimerDelay<'static> {
TimerDelay::new(self.register_block(), freq, cmu)
}
}
14 changes: 5 additions & 9 deletions artinchip-hal/src/i2c/blocking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use super::config::{I2cConfig, Role};
use super::instance::I2c;
use super::pad::{I2cPad, SerialClock, SerialData};
use super::register::{AddressMode, RegisterBlock, SpeedMode, TransferMode};
use crate::cmu;
use crate::cmu::Cmu;

/// Blocking I2C interface.
pub struct BlockingI2c<'a, const I: u8, SCL, SDA>
Expand All @@ -28,14 +28,9 @@ where
const I2C_DEFAULT_CLOCK: u32 = 24_000_000;

/// Create a new blocking serial.
pub fn new(
reg: &'a RegisterBlock,
scl: SCL,
sda: SDA,
config: I2cConfig,
clk: &cmu::RegisterBlock,
) -> Self {
pub fn new(reg: &'a RegisterBlock, scl: SCL, sda: SDA, config: I2cConfig, cmu: &Cmu) -> Self {
// Reference: https://aicdoc.artinchip.com/topics/ic/i2c/i2c-programming-guide-d13x.html
let clk = cmu.register_block();
let i2c_clk = match I {
0 => &clk.clock_i2c0,
1 => &clk.clock_i2c1,
Expand Down Expand Up @@ -157,8 +152,9 @@ where
}

/// Free the blocking I2C and return I2C instance, SCL and SDA pads.
pub fn free(self, clk: &cmu::RegisterBlock) -> (I2c<I>, SCL, SDA) {
pub fn free(self, cmu: &Cmu) -> (I2c<I>, SCL, SDA) {
unsafe {
let clk = cmu.register_block();
let i2c_clk = match I {
0 => &clk.clock_i2c0,
1 => &clk.clock_i2c1,
Expand Down
4 changes: 2 additions & 2 deletions artinchip-hal/src/i2c/i2c_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use super::blocking::BlockingI2c;
use super::config::I2cConfig;
use super::pad::{I2cPad, SerialClock, SerialData};
use crate::cmu;
use crate::cmu::Cmu;

pub trait I2cExt<'a, const I: u8> {
/// Creates a blocking I2C interface with the specified pads.
Expand All @@ -12,7 +12,7 @@ pub trait I2cExt<'a, const I: u8> {
scl: SCL,
sda: SDA,
config: I2cConfig,
cmu: &cmu::RegisterBlock,
cmu: &Cmu,
) -> BlockingI2c<'a, I, SCL, SDA>
where
SCL: I2cPad<I> + SerialClock<I>,
Expand Down
6 changes: 3 additions & 3 deletions artinchip-hal/src/i2c/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use super::config::I2cConfig;
use super::i2c_ext::I2cExt;
use super::pad::{I2cPad, SerialClock, SerialData};
use super::register::RegisterBlock;
use crate::cmu;
use crate::cmu::Cmu;
use core::marker::PhantomData;

/// I2C with statically known instance number.
Expand Down Expand Up @@ -35,12 +35,12 @@ impl<const I: u8> I2cExt<'static, I> for I2c<I> {
scl: SCL,
sda: SDA,
config: I2cConfig,
clk: &cmu::RegisterBlock,
cmu: &Cmu,
) -> BlockingI2c<'static, I, SCL, SDA>
where
SCL: I2cPad<I> + SerialClock<I>,
SDA: I2cPad<I> + SerialData<I>,
{
BlockingI2c::new(self.register_block(), scl, sda, config, clk)
BlockingI2c::new(self.register_block(), scl, sda, config, cmu)
}
}
Loading