Skip to content

Commit ffcb16e

Browse files
committed
rt: derive peripheral structs using soc macro
Signed-off-by: Zhouqi Jiang <luojia@hust.edu.cn>
1 parent 690338b commit ffcb16e

File tree

2 files changed

+54
-154
lines changed

2 files changed

+54
-154
lines changed

sophgo-rom-rt/src/lib.rs

Lines changed: 35 additions & 154 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,22 @@
33

44
pub use sophgo_rom_rt_macros::entry;
55

6+
/// Rom runtime prelude.
67
pub mod prelude {
78
pub use crate::{entry, Peripherals};
89
pub use sophgo_hal::prelude::*;
910
}
1011

12+
#[macro_use]
13+
mod macros;
1114
use sophgo_hal::{
1215
gpio::{Gpio, Input},
1316
pad::{FMux, Floating, GpioFunc, Pad, PadConfigs, PinMux, PwrPadConfigs, UartFunc},
1417
};
1518

1619
/// Peripherals available on ROM start.
1720
pub struct Peripherals {
21+
/// Pad function multiplexer peripheral.
1822
pub pinmux: PINMUX,
1923
// TODO pub pads: sophgo_hal::gpio::Pads<Static<xxxx>>,
2024
/// General Purpose Input/Output 0.
@@ -60,21 +64,39 @@ pub struct Peripherals {
6064
// TODO sd1: sophgo_hal::SD<Static<0x04320000>>,
6165
// TODO usb: sophgo_hal::USB<Static<0x04340000>>,
6266
// TODO documents
67+
/// SoC pads.
6368
pub pads: Pads<PINMUX>,
69+
/// Low-power Domain General Purpose Input/Output signal port.
6470
pub pwr_gpio: GpioPort<PWR_GPIO>,
71+
/// Low-power Domain SoC pads.
6572
pub pwr_pads: PwrPads<PWR_PINMUX>,
6673
}
6774

68-
/// Pad function multiplexer peripheral.
69-
pub struct PINMUX {
70-
_private: (),
71-
}
72-
73-
impl AsRef<PinMux> for PINMUX {
74-
#[inline(always)]
75-
fn as_ref(&self) -> &sophgo_hal::pad::PinMux {
76-
unsafe { &*(0x03001000 as *const _) }
77-
}
75+
soc! {
76+
/// Pad function multiplexer peripheral.
77+
pub struct PINMUX => 0x03001000, PinMux;
78+
/// General Purpose Input/Output peripheral 0.
79+
pub struct GPIO0 => 0x03020000, sophgo_hal::gpio::RegisterBlock;
80+
/// General Purpose Input/Output peripheral 1.
81+
pub struct GPIO1 => 0x03021000, sophgo_hal::gpio::RegisterBlock;
82+
/// General Purpose Input/Output peripheral 2.
83+
pub struct GPIO2 => 0x03022000, sophgo_hal::gpio::RegisterBlock;
84+
/// General Purpose Input/Output peripheral 3.
85+
pub struct GPIO3 => 0x03023000, sophgo_hal::gpio::RegisterBlock;
86+
/// Universal Asynchronous Receiver/Transmitter peripheral 0.
87+
pub struct UART0 => 0x04140000, sophgo_hal::uart::RegisterBlock;
88+
/// Universal Asynchronous Receiver/Transmitter peripheral 1.
89+
pub struct UART1 => 0x04150000, sophgo_hal::uart::RegisterBlock;
90+
/// Universal Asynchronous Receiver/Transmitter peripheral 2.
91+
pub struct UART2 => 0x04160000, sophgo_hal::uart::RegisterBlock;
92+
/// Universal Asynchronous Receiver/Transmitter peripheral 3.
93+
pub struct UART3 => 0x04170000, sophgo_hal::uart::RegisterBlock;
94+
/// Universal Asynchronous Receiver/Transmitter peripheral 4.
95+
pub struct UART4 => 0x041C0000, sophgo_hal::uart::RegisterBlock;
96+
/// Low-power Domain General Purpose Input/Output peripheral.
97+
pub struct PWR_GPIO => 0x05021000, sophgo_hal::gpio::RegisterBlock;
98+
/// Low-power Domain pad configuration peripheral.
99+
pub struct PWR_PINMUX => 0x05027000, PadConfigs, PwrPadConfigs;
78100
}
79101

80102
impl AsRef<FMux> for PINMUX {
@@ -91,93 +113,15 @@ impl AsRef<PadConfigs> for PINMUX {
91113
}
92114
}
93115

94-
/// General Purpose Input/Output peripheral 0.
95-
pub struct GPIO0 {
96-
_private: (),
97-
}
98-
99-
impl AsRef<sophgo_hal::gpio::RegisterBlock> for GPIO0 {
100-
#[inline(always)]
101-
fn as_ref(&self) -> &sophgo_hal::gpio::RegisterBlock {
102-
unsafe { &*(0x03020000 as *const _) }
103-
}
104-
}
105-
106-
/// General Purpose Input/Output peripheral 1.
107-
pub struct GPIO1 {
108-
_private: (),
109-
}
110-
111-
impl AsRef<sophgo_hal::gpio::RegisterBlock> for GPIO1 {
112-
#[inline(always)]
113-
fn as_ref(&self) -> &sophgo_hal::gpio::RegisterBlock {
114-
unsafe { &*(0x03021000 as *const _) }
115-
}
116-
}
117-
118-
/// General Purpose Input/Output peripheral 2.
119-
pub struct GPIO2 {
120-
_private: (),
121-
}
122-
123-
impl AsRef<sophgo_hal::gpio::RegisterBlock> for GPIO2 {
124-
#[inline(always)]
125-
fn as_ref(&self) -> &sophgo_hal::gpio::RegisterBlock {
126-
unsafe { &*(0x03022000 as *const _) }
127-
}
128-
}
129-
130-
/// General Purpose Input/Output peripheral 3.
131-
pub struct GPIO3 {
132-
_private: (),
133-
}
134-
135-
impl AsRef<sophgo_hal::gpio::RegisterBlock> for GPIO3 {
136-
#[inline(always)]
137-
fn as_ref(&self) -> &sophgo_hal::gpio::RegisterBlock {
138-
unsafe { &*(0x03023000 as *const _) }
139-
}
140-
}
141-
142-
/// Low-power Domain General Purpose Input/Output peripheral.
143-
#[allow(non_camel_case_types)]
144-
pub struct PWR_GPIO {
145-
_private: (),
146-
}
147-
148-
impl AsRef<sophgo_hal::gpio::RegisterBlock> for PWR_GPIO {
149-
#[inline(always)]
150-
fn as_ref(&self) -> &sophgo_hal::gpio::RegisterBlock {
151-
unsafe { &*(0x05021000 as *const _) }
152-
}
153-
}
154-
116+
/// General Purpose Input/Output signal port.
155117
pub struct GpioPort<T> {
156118
pub a0: Gpio<T, 0, Input>,
157119
pub a1: Gpio<T, 1, Input>,
158120
pub a2: Gpio<T, 2, Input>,
159121
// TODO a3 to a31
160122
}
161123

162-
#[allow(non_camel_case_types)]
163-
pub struct PWR_PINMUX {
164-
_private: (),
165-
}
166-
167-
impl AsRef<PwrPadConfigs> for PWR_PINMUX {
168-
#[inline(always)]
169-
fn as_ref(&self) -> &PwrPadConfigs {
170-
unsafe { &*(0x05027000 as *const _) }
171-
}
172-
}
173-
174-
impl AsRef<PadConfigs> for PWR_PINMUX {
175-
#[inline(always)]
176-
fn as_ref(&self) -> &PadConfigs {
177-
unsafe { &*(0x05027000 as *const _) }
178-
}
179-
}
180-
124+
/// SoC pads.
181125
pub struct Pads<T> {
182126
pub sd0_clk: Pad<T, 6, ()>, // TODO sd0_clk default function
183127
pub uart0_tx: Pad<T, 18, UartFunc<0>>,
@@ -187,80 +131,17 @@ pub struct Pads<T> {
187131
// TODO ...
188132
}
189133

134+
/// Low-power Domain SoC pads.
190135
pub struct PwrPads<T> {
191136
pub gpio1: Pad<T, 48, GpioFunc<Floating>>,
192137
pub gpio2: Pad<T, 49, GpioFunc<Floating>>,
193138
// TODO ...
194139
}
195140

196-
/// Universal Asynchronous Receiver/Transmitter peripheral 0.
197-
pub struct UART0 {
198-
_private: (),
199-
}
200-
201-
impl AsRef<sophgo_hal::uart::RegisterBlock> for UART0 {
202-
#[inline(always)]
203-
fn as_ref(&self) -> &sophgo_hal::uart::RegisterBlock {
204-
unsafe { &*(0x04140000 as *const _) }
205-
}
206-
}
207-
208141
impl sophgo_hal::uart::UartExt<0> for UART0 {}
209-
210-
/// Universal Asynchronous Receiver/Transmitter peripheral 1.
211-
pub struct UART1 {
212-
_private: (),
213-
}
214-
215-
impl AsRef<sophgo_hal::uart::RegisterBlock> for UART1 {
216-
#[inline(always)]
217-
fn as_ref(&self) -> &sophgo_hal::uart::RegisterBlock {
218-
unsafe { &*(0x04150000 as *const _) }
219-
}
220-
}
221-
222142
impl sophgo_hal::uart::UartExt<1> for UART1 {}
223-
224-
/// Universal Asynchronous Receiver/Transmitter peripheral 2.
225-
pub struct UART2 {
226-
_private: (),
227-
}
228-
229-
impl AsRef<sophgo_hal::uart::RegisterBlock> for UART2 {
230-
#[inline(always)]
231-
fn as_ref(&self) -> &sophgo_hal::uart::RegisterBlock {
232-
unsafe { &*(0x04160000 as *const _) }
233-
}
234-
}
235-
236143
impl sophgo_hal::uart::UartExt<2> for UART2 {}
237-
238-
/// Universal Asynchronous Receiver/Transmitter peripheral 3.
239-
pub struct UART3 {
240-
_private: (),
241-
}
242-
243-
impl AsRef<sophgo_hal::uart::RegisterBlock> for UART3 {
244-
#[inline(always)]
245-
fn as_ref(&self) -> &sophgo_hal::uart::RegisterBlock {
246-
unsafe { &*(0x04170000 as *const _) }
247-
}
248-
}
249-
250144
impl sophgo_hal::uart::UartExt<3> for UART3 {}
251-
252-
/// Universal Asynchronous Receiver/Transmitter peripheral 3.
253-
pub struct UART4 {
254-
_private: (),
255-
}
256-
257-
impl AsRef<sophgo_hal::uart::RegisterBlock> for UART4 {
258-
#[inline(always)]
259-
fn as_ref(&self) -> &sophgo_hal::uart::RegisterBlock {
260-
unsafe { &*(0x041C0000 as *const _) }
261-
}
262-
}
263-
264145
impl sophgo_hal::uart::UartExt<4> for UART4 {}
265146

266147
#[cfg(target_arch = "riscv64")]

sophgo-rom-rt/src/macros.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
macro_rules! soc {
2+
($($(#[$doc:meta])* pub struct $Ty: ident => $paddr: expr$(, $AsRefTy: ty)+;)+) => {
3+
$(
4+
$(#[$doc])*
5+
#[allow(non_camel_case_types)]
6+
pub struct $Ty {
7+
_private: (),
8+
}
9+
$(
10+
impl AsRef<$AsRefTy> for $Ty {
11+
#[inline(always)]
12+
fn as_ref(&self) -> &$AsRefTy {
13+
unsafe { &*($paddr as *const _) }
14+
}
15+
}
16+
)+
17+
)+
18+
};
19+
}

0 commit comments

Comments
 (0)