Skip to content

Commit 690338b

Browse files
committed
hal: change to fixed base address for all modules
It simplifies types of structures. OS kernel developers may provide their own ownership model and use sophgo-hal by providing an AsRef implemenetation Signed-off-by: Zhouqi Jiang <luojia@hust.edu.cn>
1 parent ab13fde commit 690338b

File tree

12 files changed

+287
-178
lines changed

12 files changed

+287
-178
lines changed

Cargo.lock

Lines changed: 16 additions & 24 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/blinky/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Blinky example
2+
3+
## Build
4+
5+
```shell
6+
rustup target install riscv64imac-unknown-none-elf
7+
cargo build -p blinky --target riscv64imac-unknown-none-elf --release
8+
```

examples/blinky/src/main.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
// rustup target install riscv64imac-unknown-none-elf
2-
// cargo build -p blinky --target riscv64imac-unknown-none-elf --release
3-
41
#![no_std]
52
#![no_main]
63

examples/hello-world/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Hello World example
2+
3+
## Build
4+
5+
```shell
6+
rustup target install riscv64imac-unknown-none-elf
7+
cargo build -p hello-world --target riscv64imac-unknown-none-elf --release
8+
```

examples/hello-world/src/main.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
// rustup target install riscv64imac-unknown-none-elf
2-
// cargo build -p hello-world --target riscv64imac-unknown-none-elf --release
3-
41
#![no_std]
52
#![no_main]
63

74
use embedded_io::Write;
85
use panic_halt as _;
9-
use sophgo_rom_rt::{entry, Peripherals};
6+
use sophgo_rom_rt::prelude::*;
107

118
#[entry]
129
fn main(p: Peripherals) -> ! {

sophgo-hal/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ repository.workspace = true
99
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
1010

1111
[dependencies]
12-
base-address = "0.0.0"
1312
volatile-register = "0.2.1"
1413
embedded-hal = "1.0.0"
1514
embedded-io = "0.6.1"

sophgo-hal/src/gpio.rs

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
//! General Purpose Input/Output.
22
3-
use crate::{
4-
pad::{GpioFunc, Pad, PullUp},
5-
GPIO,
6-
};
7-
use base_address::BaseAddress;
3+
use crate::pad::{self, GpioFunc, Pad, PullUp};
84
use core::marker::PhantomData;
95
use embedded_hal::digital::{ErrorType, OutputPin};
106
use volatile_register::{RO, RW, WO};
@@ -69,8 +65,8 @@ impl Direction {
6965
}
7066

7167
/// Owned GPIO peripheral signal with mode type state.
72-
pub struct Gpio<A: BaseAddress, const I: u8, M> {
73-
base: GPIO<A>,
68+
pub struct Gpio<T, const I: u8, M> {
69+
inner: T,
7470
_mode: PhantomData<M>,
7571
}
7672

@@ -80,7 +76,7 @@ pub struct Input;
8076
/// Output mode (type state).
8177
pub struct Output;
8278

83-
impl<A: BaseAddress, const I: u8, M> Gpio<A, I, M> {
79+
impl<T: AsRef<RegisterBlock>, const I: u8, M> Gpio<T, I, M> {
8480
/// Configures the GPIO signal as a `GpioPad` operating as a pull up output.
8581
///
8682
/// # Examples
@@ -93,16 +89,16 @@ impl<A: BaseAddress, const I: u8, M> Gpio<A, I, M> {
9389
/// let mut led = p.pwr_gpio.a2.into_pull_up_output(pad_led);
9490
/// ```
9591
#[inline]
96-
pub fn into_pull_up_output<A2: BaseAddress, const N: usize>(
92+
pub fn into_pull_up_output<U, const N: usize>(
9793
self,
98-
pad: Pad<A2, N, GpioFunc<PullUp>>,
99-
) -> GpioPad<Gpio<A, I, Output>, Pad<A2, N, GpioFunc<PullUp>>> {
94+
pad: Pad<U, N, GpioFunc<PullUp>>,
95+
) -> GpioPad<Gpio<T, I, Output>, Pad<U, N, GpioFunc<PullUp>>> {
10096
unsafe {
101-
self.base.direction.modify(|w| w.set_output(I));
97+
self.inner.as_ref().direction.modify(|w| w.set_output(I));
10298
}
10399
GpioPad {
104100
gpio: Gpio {
105-
base: self.base,
101+
inner: self.inner,
106102
_mode: PhantomData,
107103
},
108104
pad,
@@ -116,12 +112,12 @@ pub struct GpioPad<T, U> {
116112
pad: U,
117113
}
118114

119-
impl<A: BaseAddress, A2: BaseAddress, const I: u8, const N: usize, M, T>
120-
GpioPad<Gpio<A, I, M>, Pad<A2, N, GpioFunc<T>>>
115+
impl<T: AsRef<RegisterBlock>, U: AsRef<pad::PadConfigs>, const I: u8, const N: usize, M>
116+
GpioPad<Gpio<T, I, M>, Pad<U, N, GpioFunc<T>>>
121117
{
122118
/// Reconfigures the `GpioPad` to operate as a pull up output.
123119
#[inline]
124-
pub fn into_pull_up_output(self) -> GpioPad<Gpio<A, I, Output>, Pad<A2, N, GpioFunc<PullUp>>> {
120+
pub fn into_pull_up_output(self) -> GpioPad<Gpio<T, I, Output>, Pad<U, N, GpioFunc<PullUp>>> {
125121
let (gpio, pad) = self.into_inner();
126122
gpio.into_pull_up_output(pad.into_gpio_pull_up())
127123
}
@@ -139,19 +135,19 @@ impl<T, U> ErrorType for GpioPad<T, U> {
139135
type Error = core::convert::Infallible;
140136
}
141137

142-
impl<A: BaseAddress, const I: u8, U> OutputPin for GpioPad<Gpio<A, I, Output>, U> {
138+
impl<T: AsRef<RegisterBlock>, const I: u8, U> OutputPin for GpioPad<Gpio<T, I, Output>, U> {
143139
#[inline]
144140
fn set_low(&mut self) -> Result<(), Self::Error> {
145141
unsafe {
146-
self.gpio.base.data.modify(|w| w & !(1 << I));
142+
self.gpio.inner.as_ref().data.modify(|w| w & !(1 << I));
147143
}
148144
Ok(())
149145
}
150146

151147
#[inline]
152148
fn set_high(&mut self) -> Result<(), Self::Error> {
153149
unsafe {
154-
self.gpio.base.data.modify(|w| w | (1 << I));
150+
self.gpio.inner.as_ref().data.modify(|w| w | (1 << I));
155151
}
156152
Ok(())
157153
}

sophgo-hal/src/lib.rs

Lines changed: 2 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -4,55 +4,6 @@ pub mod gpio;
44
pub mod pad;
55
pub mod uart;
66

7-
use core::ops;
8-
9-
use base_address::BaseAddress;
10-
11-
/// Universal Asynchronous Receiver/Transmitter.
12-
pub struct UART<A: BaseAddress, const I: usize> {
13-
base: A,
14-
}
15-
16-
impl<A: BaseAddress, const I: usize> ops::Deref for UART<A, I> {
17-
type Target = uart::RegisterBlock;
18-
19-
#[inline(always)]
20-
fn deref(&self) -> &Self::Target {
21-
unsafe { &*(self.base.ptr() as *const _) }
22-
}
23-
}
24-
25-
/// General Purpose Input/Output.
26-
pub struct GPIO<A: BaseAddress> {
27-
base: A,
28-
}
29-
30-
impl<A: BaseAddress> ops::Deref for GPIO<A> {
31-
type Target = gpio::RegisterBlock;
32-
33-
#[inline(always)]
34-
fn deref(&self) -> &Self::Target {
35-
unsafe { &*(self.base.ptr() as *const _) }
36-
}
37-
}
38-
39-
/// Pad function multiplexer peripheral.
40-
pub struct PINMUX<A: BaseAddress> {
41-
base: A,
42-
}
43-
44-
impl<A: BaseAddress> ops::Deref for PINMUX<A> {
45-
type Target = pad::PinMux;
46-
47-
#[inline(always)]
48-
fn deref(&self) -> &Self::Target {
49-
unsafe { &*(self.base.ptr() as *const _) }
50-
}
51-
}
52-
53-
impl<A: BaseAddress> AsRef<pad::FMux> for PINMUX<A> {
54-
#[inline(always)]
55-
fn as_ref(&self) -> &pad::FMux {
56-
&self.fmux
57-
}
7+
pub mod prelude {
8+
pub use crate::uart::UartExt as __sophgo_hal__uart__UartExt;
589
}

sophgo-hal/src/pad.rs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
//! Silicon pad multiplexer and configurations.
22
3-
use base_address::BaseAddress;
43
use core::marker::PhantomData;
54
use volatile_register::RW;
65

@@ -164,43 +163,42 @@ impl PadConfig {
164163
}
165164

166165
/// Ownership of a pad with function type state.
167-
pub struct Pad<A: BaseAddress, const N: usize, F> {
168-
base: A,
166+
pub struct Pad<T, const N: usize, F> {
167+
inner: T,
169168
_function: PhantomData<F>,
170169
}
171170

172-
impl<A: BaseAddress, const N: usize, F> Pad<A, N, F> {
171+
impl<T: AsRef<PadConfigs>, const N: usize, F> Pad<T, N, F> {
173172
/// Converts the function of this pad.
174173
#[inline]
175-
pub fn into_function<F2: Function>(self, fmux: impl AsRef<FMux>) -> Pad<A, N, F2> {
174+
pub fn into_function<F2: Function>(self, fmux: impl AsRef<FMux>) -> Pad<T, N, F2> {
176175
unsafe { fmux.as_ref().fmux::<N>().write(F2::fmux::<N>()) };
177176
unsafe { self.pad_config().modify(|w| w.set_pull(F2::PULL)) };
178177
Pad {
179-
base: self.base,
178+
inner: self.inner,
180179
_function: PhantomData,
181180
}
182181
}
183182
#[inline]
184183
fn pad_config(&self) -> &RW<PadConfig> {
185184
match N {
186185
// TODO in range of power pads ...
187-
49 => unsafe { &*(self.base.ptr() as *const PwrPadConfigs) }.pad_config::<N>(),
186+
49 => unsafe { &*(self.inner.as_ref() as *const _ as *const PwrPadConfigs) }
187+
.pad_config::<N>(),
188188
// TODO in range of conventional pads ...
189-
18..=19 | 28..=29 => {
190-
unsafe { &*(self.base.ptr() as *const PadConfigs) }.pad_config::<N>()
191-
}
189+
18..=19 | 28..=29 => self.inner.as_ref().pad_config::<N>(),
192190
// .. => { ... }
193191
_ => todo!(),
194192
}
195193
}
196194
}
197195

198-
impl<A: BaseAddress, const N: usize, T> Pad<A, N, GpioFunc<T>> {
196+
impl<T: AsRef<PadConfigs>, const N: usize, F> Pad<T, N, GpioFunc<F>> {
199197
#[inline]
200-
pub(crate) fn into_gpio_pull_up(self) -> Pad<A, N, GpioFunc<PullUp>> {
198+
pub(crate) fn into_gpio_pull_up(self) -> Pad<T, N, GpioFunc<PullUp>> {
201199
unsafe { self.pad_config().modify(|w| w.set_pull(Pull::Up)) };
202200
Pad {
203-
base: self.base,
201+
inner: self.inner,
204202
_function: PhantomData,
205203
}
206204
}

0 commit comments

Comments
 (0)