Skip to content

Commit a639f09

Browse files
committed
test(port_arm): use ::rza1 to access memory-mapped registers
1 parent 637a3d1 commit a639f09

File tree

3 files changed

+64
-179
lines changed

3 files changed

+64
-179
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/constance_port_arm_test_driver/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ board-realview_pbx_a9 = [
2323
board-rza1 = [
2424
"output-semihosting",
2525
"constance_support_rza1",
26+
"rza1",
2627
]
2728

2829
output-semihosting = [
@@ -38,6 +39,7 @@ constance = { path = "../constance", optional = true }
3839

3940
staticvec = { version = "0.10.2", optional = true, default-features = false }
4041
register = { version = "0.5.1", optional = true }
42+
rza1 = { version = "0.2.0", optional = true, features = ["cpg", "gpio", "scif"] }
4143
log = { version = "0.4.8", optional = true }
4244

4345
[dependencies.constance_test_suite]
Lines changed: 61 additions & 179 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use core::fmt::Write;
2-
use register::FieldValue;
2+
use rza1::scif0 as scif;
33

44
struct Logger;
55

@@ -9,9 +9,11 @@ impl log::Log for Logger {
99
}
1010

1111
fn log(&self, record: &log::Record) {
12+
let peripherals = unsafe { rza1::Peripherals::steal() };
13+
1214
interrupt_free(|| {
1315
writeln!(
14-
SCWriter(rza1::SC2()),
16+
SCWriter(&peripherals.SCIF2),
1517
"[{:5} {}] {}",
1618
record.level(),
1719
record.target(),
@@ -25,9 +27,9 @@ impl log::Log for Logger {
2527
}
2628

2729
#[derive(Clone, Copy)]
28-
struct SCWriter(&'static rza1::SC);
30+
struct SCWriter<'a>(&'a scif::RegisterBlock);
2931

30-
impl Write for SCWriter {
32+
impl Write for SCWriter<'_> {
3133
fn write_str(&mut self, s: &str) -> core::fmt::Result {
3234
for &b in s.as_bytes() {
3335
self.write_u8(b);
@@ -36,50 +38,76 @@ impl Write for SCWriter {
3638
}
3739
}
3840

39-
impl SCWriter {
41+
impl SCWriter<'_> {
4042
fn write_u8(self, x: u8) {
4143
let sc = self.0;
4244
if x == b'\n' {
4345
self.write_u8(b'\r');
4446
}
45-
while sc.FSR.read(rza1::FSR::TDFE) == 0 {}
46-
sc.FTDR.set(x);
47-
sc.FSR
48-
.modify(rza1::FSR::TDFE::CLEAR + rza1::FSR::TEND::CLEAR);
47+
while sc.fsr.read().tdfe().bit_is_clear() {}
48+
sc.ftdr.write(|w| w.d().bits(x));
49+
sc.fsr
50+
.modify(|_, w| w.tdfe().clear_bit().tend().clear_bit());
4951
}
5052
}
5153

5254
pub fn init() {
55+
let peripherals = unsafe { rza1::Peripherals::steal() };
56+
let rza1::Peripherals {
57+
CPG, GPIO, SCIF2, ..
58+
} = peripherals;
59+
5360
// Supply clock to SC2
54-
rza1::STBCR4().set(rza1::STBCR4().get() & !0x20);
61+
CPG.stbcr4.modify(|_, w| w.mstp45().clear_bit());
5562

5663
// On GR-PEACH, the nets `TGT_[TR]XD` are connected to `P6_[23]`. Configure
5764
// `P6_[23]` to use its 7th alternative function - `SC2`.
5865
let (mask, shift) = (0b11, 2);
59-
rza1::PMC(6).modify(FieldValue::<u16, ()>::new(mask, shift, 0b11));
60-
rza1::PFCAE(6).modify(FieldValue::<u16, ()>::new(mask, shift, 0b11));
61-
rza1::PFCE(6).modify(FieldValue::<u16, ()>::new(mask, shift, 0b11));
62-
rza1::PFC(6).modify(FieldValue::<u16, ()>::new(mask, shift, 0b00));
63-
rza1::PM(6).modify(FieldValue::<u16, ()>::new(mask, shift, 0b01));
64-
65-
let sc2 = rza1::SC2();
66-
sc2.SCR.write(
67-
rza1::SCR::TIE::CLEAR
68-
+ rza1::SCR::RIE::CLEAR
69-
+ rza1::SCR::TE::SET
70-
+ rza1::SCR::RE::CLEAR
71-
+ rza1::SCR::REIE::CLEAR
72-
+ rza1::SCR::CKE::AsynchronousInternalNoOutput,
73-
);
74-
sc2.SMR.write(
75-
rza1::SMR::CA::Asynchronous
76-
+ rza1::SMR::CHR::EightBitData
77-
+ rza1::SMR::PE::NoParityBit
78-
+ rza1::SMR::STOP::OneStopBit
79-
+ rza1::SMR::CKS::DivideBy1,
80-
);
66+
67+
GPIO.pmc6
68+
.modify(|_, w| w.pmc62().set_bit().pmc63().set_bit());
69+
GPIO.pfcae6
70+
.modify(|_, w| w.pfcae62().set_bit().pfcae63().set_bit());
71+
GPIO.pfce6
72+
.modify(|_, w| w.pfce62().set_bit().pfce63().set_bit());
73+
GPIO.pfc6
74+
.modify(|_, w| w.pfc62().clear_bit().pfc63().clear_bit());
75+
GPIO.pm6
76+
.modify(|_, w| w.pm62().set_bit().pm63().clear_bit());
77+
78+
SCIF2.scr.write(|w| {
79+
w.tie()
80+
.clear_bit()
81+
.rie()
82+
.clear_bit()
83+
.te()
84+
.set_bit()
85+
.re()
86+
.clear_bit()
87+
.reie()
88+
.clear_bit()
89+
.cke()
90+
.internal_sck_in()
91+
});
92+
SCIF2.smr.write(|w| {
93+
w
94+
// Asynchronous
95+
.ca()
96+
.clear_bit()
97+
// 8-bit data
98+
.chr()
99+
.clear_bit()
100+
// No parity bits
101+
.pe()
102+
.clear_bit()
103+
// One stop bit
104+
.stop()
105+
.clear_bit()
106+
.cks()
107+
.divide_by_1()
108+
});
81109
// 66.666e6/115200/(64*2**(2*0-1))-1 = 17.0843...
82-
sc2.BRR.set(17);
110+
SCIF2.brr.write(|w| w.d().bits(17));
83111

84112
log::set_logger(&Logger).unwrap();
85113
log::set_max_level(log::LevelFilter::Trace);
@@ -101,149 +129,3 @@ fn interrupt_free<T>(x: impl FnOnce() -> T) -> T {
101129

102130
ret
103131
}
104-
105-
#[allow(non_snake_case)]
106-
mod rza1 {
107-
use register::mmio::{ReadOnly, ReadWrite, WriteOnly};
108-
109-
#[inline]
110-
pub fn STBCR4() -> &'static ReadWrite<u8> {
111-
unsafe { &*(0xfcfe0424 as *const _) }
112-
}
113-
114-
/// Port mode register (set = input)
115-
#[inline]
116-
pub fn PM(n: usize) -> &'static ReadWrite<u16> {
117-
assert!(n < 12);
118-
unsafe { &*((0xfcfe3300 + n * 4) as *const _) }
119-
}
120-
121-
/// Port mode control register
122-
#[inline]
123-
pub fn PMC(n: usize) -> &'static ReadWrite<u16> {
124-
assert!(n < 12);
125-
unsafe { &*((0xfcfe3400 + n * 4) as *const _) }
126-
}
127-
128-
/// Port function control register
129-
#[inline]
130-
pub fn PFC(n: usize) -> &'static ReadWrite<u16> {
131-
assert!(n < 12);
132-
unsafe { &*((0xfcfe3500 + n * 4) as *const _) }
133-
}
134-
135-
/// Port function control expansion register
136-
#[inline]
137-
pub fn PFCE(n: usize) -> &'static ReadWrite<u16> {
138-
assert!(n < 12);
139-
unsafe { &*((0xfcfe3600 + n * 4) as *const _) }
140-
}
141-
142-
/// Port function control additional expansion register
143-
#[inline]
144-
pub fn PFCAE(n: usize) -> &'static ReadWrite<u16> {
145-
assert!(n < 12);
146-
unsafe { &*((0xfcfe3a00 + n * 4) as *const _) }
147-
}
148-
149-
/// Serial Communication Interface with FIFO
150-
#[repr(C)]
151-
pub struct SC {
152-
/// Serial mode register
153-
pub SMR: ReadWrite<u16, SMR::Register>,
154-
_r0: u16,
155-
/// Bit rate register
156-
pub BRR: ReadWrite<u8>,
157-
_r1: [u8; 3],
158-
/// Serial control register
159-
pub SCR: ReadWrite<u16, SCR::Register>,
160-
_r2: u16,
161-
/// Transmit FIFO data register
162-
pub FTDR: WriteOnly<u8>,
163-
_r3: [u8; 3],
164-
/// Serial status register
165-
pub FSR: ReadWrite<u16, FSR::Register>,
166-
_r4: u16,
167-
/// Receive FIFO data register
168-
pub FRDR: ReadOnly<u8>,
169-
_r5: [u8; 3],
170-
/// FIFO control register
171-
pub FCR: ReadWrite<u16>,
172-
_r6: u16,
173-
/// FIFO data count set register
174-
pub FDR: ReadOnly<u16>,
175-
_r7: u16,
176-
/// Serial port register
177-
pub SPTR: ReadWrite<u16>,
178-
_r8: u16,
179-
/// Line status register
180-
pub LSR: ReadWrite<u16>,
181-
_r9: u16,
182-
/// Serial extension mode register
183-
pub EMR: ReadWrite<u16>,
184-
_r10: u16,
185-
}
186-
187-
pub fn SC2() -> &'static SC {
188-
unsafe { &*(0xE8008000 as *const SC) }
189-
}
190-
191-
register::register_bitfields! {u16,
192-
pub SMR [
193-
CA OFFSET(7) NUMBITS(1) [
194-
Asynchronous = 0,
195-
ClockSynchronous = 1
196-
],
197-
CHR OFFSET(6) NUMBITS(1) [
198-
EightBitData = 0,
199-
SevenBitData = 1
200-
],
201-
PE OFFSET(5) NUMBITS(1) [
202-
NoParityBit = 0,
203-
HasParityBit = 1
204-
],
205-
OOE OFFSET(4) NUMBITS(1) [
206-
EvenParity = 0,
207-
OddParity = 1
208-
],
209-
STOP OFFSET(3) NUMBITS(1) [
210-
OneStopBit = 0,
211-
TwoStopBits = 1
212-
],
213-
CKS OFFSET(0) NUMBITS(2) [
214-
DivideBy1 = 0b00,
215-
DivideBy4 = 0b01,
216-
DivideBy16 = 0b10,
217-
DivideBy64 = 0b11
218-
]
219-
]
220-
}
221-
222-
register::register_bitfields! {u16,
223-
pub SCR [
224-
/// Transmit Interrupt Enable
225-
TIE OFFSET(7) NUMBITS(1) [],
226-
/// Receive Interrupt Enable
227-
RIE OFFSET(6) NUMBITS(1) [],
228-
/// Transmit Enable
229-
TE OFFSET(5) NUMBITS(1) [],
230-
/// Receive Enable
231-
RE OFFSET(4) NUMBITS(1) [],
232-
/// Receive Error Interrupt Enable
233-
REIE OFFSET(3) NUMBITS(1) [],
234-
/// Clock Enable
235-
CKE OFFSET(0) NUMBITS(2) [
236-
AsynchronousInternalNoOutput = 0b00
237-
]
238-
]
239-
}
240-
241-
register::register_bitfields! {u16,
242-
pub FSR [
243-
/// Transmit FIFO Data Empty
244-
TDFE OFFSET(5) NUMBITS(1) [],
245-
/// Transmit End
246-
TEND OFFSET(6) NUMBITS(1) []
247-
]
248-
}
249-
}

0 commit comments

Comments
 (0)