Skip to content

Commit 284aaa9

Browse files
SpinFastDirbaio
andcommitted
Multiple generators in the tree
A (memory) mappable generator which assumes the memory is device accessible and the register block can simply be pointed at using a machine pointer. A (memory) unmappable generator which assumes the memory representing the register block must be read/written to using an interface of some sort instead of being available using a machine pointer. Most SoC peripherals would be considered mappable and was the existing generator, so the render function is aliased to the mappable module. The unmappable generator is based on the i2c work previously and should be considered experimental, but the idea is that an interface is needed to read/write a given register at a given address. The registers themselves may be of any bit sizing but placed in a container type (u8, u16, etc). Co-authored-by: Dario Nieuwenhuis <[email protected]>
1 parent 67c6adc commit 284aaa9

File tree

14 files changed

+775
-89
lines changed

14 files changed

+775
-89
lines changed

src/generate/iface/block.rs

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
use anyhow::Result;
2+
use proc_macro2::TokenStream;
3+
use proc_macro2::{Ident, Span};
4+
use quote::quote;
5+
6+
use crate::generate::{process_array, sorted, split_path, Options};
7+
use crate::ir::*;
8+
use crate::util;
9+
10+
pub fn render(opts: &Options, ir: &IR, b: &Block, path: &str) -> Result<TokenStream> {
11+
let common_path = opts.common_path();
12+
13+
let span = Span::call_site();
14+
let mut items = TokenStream::new();
15+
16+
let addr_ty = if let Some(addr_size) = &b.address_size {
17+
match addr_size {
18+
1..=8 => quote!(u8),
19+
9..=16 => quote!(u16),
20+
17..=32 => quote!(u32),
21+
33..=64 => quote!(u64),
22+
_ => panic!("Invalid addr_size {}", addr_size),
23+
}
24+
} else {
25+
quote!(u32)
26+
};
27+
28+
for i in sorted(&b.items, |i| (i.byte_offset, i.name.clone())) {
29+
let name = Ident::new(&i.name, span);
30+
let offset = i.byte_offset as usize;
31+
32+
let doc = util::doc(&i.description);
33+
34+
match &i.inner {
35+
BlockItemInner::Register(r) => {
36+
let reg_ty = if let Some(fieldset_path) = &r.fieldset {
37+
let _f = ir.fieldsets.get(fieldset_path).unwrap();
38+
util::relative_path(fieldset_path, path)
39+
} else {
40+
match r.bit_size {
41+
1..=8 => quote!(u8),
42+
9..=16 => quote!(u16),
43+
17..=32 => quote!(u32),
44+
33..=64 => quote!(u64),
45+
_ => panic!("Invalid bit_size {}", r.bit_size),
46+
}
47+
};
48+
49+
let access = match r.access {
50+
Access::Read => quote!(#common_path::R),
51+
Access::Write => quote!(#common_path::W),
52+
Access::ReadWrite => quote!(#common_path::RW),
53+
};
54+
55+
let ty = quote!(#common_path::Reg<'_, I, #addr_ty, #reg_ty, #access>);
56+
if let Some(array) = &i.array {
57+
let (len, offs_expr) = process_array(array);
58+
items.extend(quote!(
59+
#doc
60+
#[inline(always)]
61+
pub fn #name(&mut self, n: usize) -> #ty {
62+
assert!(n < #len);
63+
#common_path::Reg::new(self.iface, (self.addr + #offset + #offs_expr) as #addr_ty)
64+
}
65+
));
66+
} else {
67+
items.extend(quote!(
68+
#doc
69+
#[inline(always)]
70+
pub fn #name(&mut self) -> #ty {
71+
#common_path::Reg::new(self.iface, (self.addr + #offset) as #addr_ty)
72+
}
73+
));
74+
}
75+
}
76+
BlockItemInner::Block(b) => {
77+
let block_path = &b.block;
78+
let _b2 = ir.blocks.get(block_path).unwrap();
79+
let ty = util::relative_path(block_path, path);
80+
if let Some(array) = &i.array {
81+
let (len, offs_expr) = process_array(array);
82+
83+
items.extend(quote!(
84+
#doc
85+
#[inline(always)]
86+
pub const fn #name(self, n: usize) -> #ty {
87+
assert!(n < #len);
88+
unsafe { #ty::from_ptr(self.ptr.add(#offset + #offs_expr) as _) }
89+
}
90+
));
91+
} else {
92+
items.extend(quote!(
93+
#doc
94+
#[inline(always)]
95+
pub const fn #name(self) -> #ty {
96+
unsafe { #ty::from_ptr(self.ptr.add(#offset) as _) }
97+
}
98+
));
99+
}
100+
}
101+
}
102+
}
103+
104+
let (_, name) = split_path(path);
105+
let name = Ident::new(name, span);
106+
let doc = util::doc(&b.description);
107+
let out = quote! {
108+
#doc
109+
pub struct #name<'a, I> {
110+
pub iface: &'a mut I,
111+
pub addr: usize,
112+
}
113+
114+
impl<'a, I> #name<'a, I> {
115+
#items
116+
}
117+
};
118+
119+
Ok(out)
120+
}

src/generate/iface/common.rs

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
use core::marker::PhantomData;
2+
3+
#[derive(Copy, Clone, PartialEq, Eq)]
4+
pub struct RW;
5+
#[derive(Copy, Clone, PartialEq, Eq)]
6+
pub struct R;
7+
#[derive(Copy, Clone, PartialEq, Eq)]
8+
pub struct W;
9+
10+
/// Interface to read and write register values
11+
pub trait Interface<K, V> {
12+
type Error: core::fmt::Debug;
13+
fn read(&mut self, addr: K) -> Result<V, Self::Error>;
14+
fn write(&mut self, addr: K, value: V) -> Result<(), Self::Error>;
15+
}
16+
17+
mod sealed {
18+
use super::*;
19+
pub trait Access {}
20+
impl Access for R {}
21+
impl Access for W {}
22+
impl Access for RW {}
23+
}
24+
25+
pub trait Access: sealed::Access + Copy {}
26+
impl Access for R {}
27+
impl Access for W {}
28+
impl Access for RW {}
29+
30+
pub trait Read: Access {}
31+
impl Read for RW {}
32+
impl Read for R {}
33+
34+
pub trait Write: Access {}
35+
impl Write for RW {}
36+
impl Write for W {}
37+
38+
#[derive(PartialEq, Eq)]
39+
pub struct Reg<'a, I, K, V, A> {
40+
iface: &'a mut I,
41+
addr: K,
42+
phantom: PhantomData<(V, A)>,
43+
}
44+
45+
impl<'a, I, K: Copy, V, A: Access> Reg<'a, I, K, V, A> {
46+
#[inline(always)]
47+
pub fn new(iface: &'a mut I, addr: K) -> Self {
48+
Self {
49+
iface,
50+
addr,
51+
phantom: PhantomData,
52+
}
53+
}
54+
}
55+
56+
impl<'a, I: Interface<K, V>, K: Copy, V: Copy, A: Read> Reg<'a, I, K, V, A> {
57+
#[inline(always)]
58+
pub fn read(&mut self) -> V {
59+
self.iface.read(self.addr).unwrap()
60+
}
61+
}
62+
63+
impl<'a, I: Interface<K, V>, K: Copy, V: Copy, A: Write> Reg<'a, I, K, V, A> {
64+
#[inline(always)]
65+
pub fn write_value(&mut self, val: V) {
66+
self.iface.write(self.addr, val).unwrap()
67+
}
68+
}
69+
70+
impl<'a, I: Interface<K, V>, K: Copy, V: Default + Copy, A: Write> Reg<'a, I, K, V, A> {
71+
#[inline(always)]
72+
pub fn write<R>(&mut self, f: impl FnOnce(&mut V) -> R) -> R {
73+
let mut val = Default::default();
74+
let res = f(&mut val);
75+
self.write_value(val);
76+
res
77+
}
78+
}
79+
80+
impl<'a, I: Interface<K, V>, K: Copy, V: Copy, A: Read + Write> Reg<'a, I, K, V, A> {
81+
#[inline(always)]
82+
pub fn modify<R>(&mut self, f: impl FnOnce(&mut V) -> R) -> R {
83+
let mut val = self.read();
84+
let res = f(&mut val);
85+
self.write_value(val);
86+
res
87+
}
88+
}

src/generate/device.rs renamed to src/generate/iface/device.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ use quote::quote;
44

55
use crate::ir::*;
66
use crate::util::{self, ToSanitizedUpperCase};
7-
8-
use super::sorted;
7+
use crate::generate::sorted;
98

109
pub fn render(_opts: &super::Options, ir: &IR, d: &Device, path: &str) -> Result<TokenStream> {
1110
let mut out = TokenStream::new();

src/generate/enumm.rs renamed to src/generate/iface/enumm.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ use quote::quote;
77

88
use crate::ir::*;
99
use crate::util;
10-
11-
use super::sorted;
10+
use crate::generate::sorted;
1211

1312
pub fn render(_opts: &super::Options, _ir: &IR, e: &Enum, path: &str) -> Result<TokenStream> {
1413
let span = Span::call_site();
@@ -123,5 +122,6 @@ pub fn render(_opts: &super::Options, _ir: &IR, e: &Enum, path: &str) -> Result<
123122
}
124123
});
125124

125+
126126
Ok(out)
127127
}

src/generate/iface/fieldset.rs

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
use anyhow::Result;
2+
use proc_macro2::TokenStream;
3+
use proc_macro2::{Ident, Span};
4+
use quote::quote;
5+
6+
use crate::ir::*;
7+
use crate::util;
8+
use crate::generate::{Options, sorted, split_path, process_array};
9+
10+
pub fn render(_opts: &Options, ir: &IR, fs: &FieldSet, path: &str) -> Result<TokenStream> {
11+
let span = Span::call_site();
12+
let mut items = TokenStream::new();
13+
14+
let ty = match fs.bit_size {
15+
1..=8 => quote!(u8),
16+
9..=16 => quote!(u16),
17+
17..=32 => quote!(u32),
18+
33..=64 => quote!(u64),
19+
_ => panic!("Invalid bit_size {}", fs.bit_size),
20+
};
21+
22+
for f in sorted(&fs.fields, |f| (f.bit_offset, f.name.clone())) {
23+
let name = Ident::new(&f.name, span);
24+
let name_set = Ident::new(&format!("set_{}", f.name), span);
25+
let bit_offset = f.bit_offset as usize;
26+
let _bit_size = f.bit_size as usize;
27+
let mask = util::hex(1u64.wrapping_shl(f.bit_size).wrapping_sub(1));
28+
let doc = util::doc(&f.description);
29+
let field_ty: TokenStream;
30+
let to_bits: TokenStream;
31+
let from_bits: TokenStream;
32+
33+
if let Some(e_path) = &f.enumm {
34+
let Some(e) = ir.enums.get(e_path) else {
35+
panic!("missing enum {}", e_path);
36+
};
37+
38+
let enum_ty = match e.bit_size {
39+
1..=8 => quote!(u8),
40+
9..=16 => quote!(u16),
41+
17..=32 => quote!(u32),
42+
33..=64 => quote!(u64),
43+
_ => panic!("Invalid bit_size {}", e.bit_size),
44+
};
45+
46+
field_ty = util::relative_path(e_path, path);
47+
to_bits = quote!(val.to_bits() as #ty);
48+
from_bits = quote!(#field_ty::from_bits(val as #enum_ty));
49+
} else {
50+
field_ty = match f.bit_size {
51+
1 => quote!(bool),
52+
2..=8 => quote!(u8),
53+
9..=16 => quote!(u16),
54+
17..=32 => quote!(u32),
55+
33..=64 => quote!(u64),
56+
_ => panic!("Invalid bit_size {}", f.bit_size),
57+
};
58+
to_bits = quote!(val as #ty);
59+
from_bits = if f.bit_size == 1 {
60+
quote!(val != 0)
61+
} else {
62+
quote!(val as #field_ty)
63+
}
64+
}
65+
66+
if let Some(array) = &f.array {
67+
let (len, offs_expr) = process_array(array);
68+
items.extend(quote!(
69+
#doc
70+
#[inline(always)]
71+
pub const fn #name(&self, n: usize) -> #field_ty{
72+
assert!(n < #len);
73+
let offs = #bit_offset + #offs_expr;
74+
let val = (self.0 >> offs) & #mask;
75+
#from_bits
76+
}
77+
#doc
78+
#[inline(always)]
79+
pub fn #name_set(&mut self, n: usize, val: #field_ty) {
80+
assert!(n < #len);
81+
let offs = #bit_offset + #offs_expr;
82+
self.0 = (self.0 & !(#mask << offs)) | (((#to_bits) & #mask) << offs);
83+
}
84+
));
85+
} else {
86+
items.extend(quote!(
87+
#doc
88+
#[inline(always)]
89+
pub const fn #name(&self) -> #field_ty{
90+
let val = (self.0 >> #bit_offset) & #mask;
91+
#from_bits
92+
}
93+
#doc
94+
#[inline(always)]
95+
pub fn #name_set(&mut self, val: #field_ty) {
96+
self.0 = (self.0 & !(#mask << #bit_offset)) | (((#to_bits) & #mask) << #bit_offset);
97+
}
98+
));
99+
}
100+
}
101+
102+
let (_, name) = split_path(path);
103+
let name = Ident::new(name, span);
104+
let doc = util::doc(&fs.description);
105+
106+
let out = quote! {
107+
#doc
108+
#[repr(transparent)]
109+
#[derive(Copy, Clone, Eq, PartialEq)]
110+
pub struct #name (pub #ty);
111+
112+
impl #name {
113+
#items
114+
}
115+
116+
impl Default for #name {
117+
#[inline(always)]
118+
fn default() -> #name {
119+
#name(0)
120+
}
121+
}
122+
123+
impl From<#ty> for #name {
124+
fn from(val: #ty) -> #name {
125+
#name(val)
126+
}
127+
}
128+
129+
impl From<#name> for #ty {
130+
fn from(val: #name) -> #ty {
131+
val.0
132+
}
133+
}
134+
};
135+
136+
Ok(out)
137+
}

0 commit comments

Comments
 (0)