Skip to content

Commit a78a40a

Browse files
committed
Extracted lpc pt code to src/hal/lpc17xx once again.
1 parent e56972b commit a78a40a

File tree

5 files changed

+237
-205
lines changed

5 files changed

+237
-205
lines changed

platformtree/builder.rs

Lines changed: 1 addition & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use syntax::ast;
1818
use syntax::codemap::{ExpnInfo, NameAndSpan, MacroBang};
1919
use syntax::ext::base::ExtCtxt;
2020

21+
use lpc17xx_pt;
2122
use node;
2223

2324
pub struct Builder {
@@ -71,126 +72,3 @@ fn build_mcu(builder: &mut Builder, cx: &mut ExtCtxt, node: &Gc<node::Node>) {
7172

7273
cx.bt_pop();
7374
}
74-
75-
mod lpc17xx_pt {
76-
use std::gc::Gc;
77-
use syntax::ext::base::ExtCtxt;
78-
use syntax::ext::build::AstBuilder;
79-
use syntax::ext::quote::rt::{ToTokens, ExtParseUtils};
80-
use syntax::ast::TokenTree;
81-
82-
use super::Builder;
83-
use node;
84-
85-
86-
pub fn build_mcu(builder: &mut Builder, cx: &mut ExtCtxt,
87-
node: &Gc<node::Node>) {
88-
if !node.expect_no_attributes(cx) {
89-
return;
90-
}
91-
// init stack
92-
builder.add_main_statement(cx.stmt_expr(quote_expr!(&*cx,
93-
{
94-
use zinc::hal::stack;
95-
extern { static _eglobals: u32; }
96-
stack::set_stack_limit((&_eglobals as *u32) as u32);
97-
}
98-
)));
99-
100-
// init data
101-
builder.add_main_statement(cx.stmt_expr(quote_expr!(&*cx,
102-
zinc::hal::mem_init::init_data();
103-
)));
104-
105-
node.get_by_path("clock").and_then(|sub| -> Option<bool> {
106-
build_clock(builder, cx, sub);
107-
None
108-
});
109-
}
110-
111-
pub fn build_clock(builder: &mut Builder, cx: &mut ExtCtxt,
112-
node: &Gc<node::Node>) {
113-
if !node.expect_attributes(cx, vec!(("source", node::StringAttribute))) {
114-
return;
115-
}
116-
117-
let source = node.get_string_attr("source").unwrap();
118-
let clock_source = ClockSource::new(match source.as_slice() {
119-
"internal-oscillator" => "init::Internal".to_str(),
120-
"rtc-oscillator" => "init::RTC".to_str(),
121-
"main-oscillator" => {
122-
let some_source_frequency =
123-
node.get_required_int_attr(cx, "source_frequency");
124-
if some_source_frequency == None {
125-
"BAD".to_str()
126-
} else {
127-
format!("init::Main({})", some_source_frequency.unwrap())
128-
}
129-
},
130-
other => {
131-
cx.span_err(
132-
node.get_attr("source").value_span,
133-
format!("unknown oscillator value `{}`", other).as_slice());
134-
"BAD".to_str()
135-
},
136-
});
137-
138-
let some_pll_conf = node.get_by_path("pll").and_then(|sub|
139-
-> Option<(uint, uint, uint)> {
140-
if !sub.expect_no_subnodes(cx) || !sub.expect_attributes(cx, vec!(
141-
("m", node::IntAttribute),
142-
("n", node::IntAttribute),
143-
("divisor", node::IntAttribute))) {
144-
None
145-
} else {
146-
let m = sub.get_int_attr("m").unwrap();
147-
let n = sub.get_int_attr("n").unwrap();
148-
let divisor = sub.get_int_attr("divisor").unwrap();
149-
Some((m, n, divisor))
150-
}
151-
});
152-
if some_pll_conf.is_none() {
153-
cx.parse_sess().span_diagnostic.span_err(node.name_span,
154-
"required subnode `pll` is missing");
155-
return;
156-
}
157-
158-
let (m, n, divisor) = some_pll_conf.unwrap();
159-
160-
let ex = quote_expr!(&*cx,
161-
{
162-
use zinc::hal::lpc17xx::init;
163-
init::init_clock(
164-
init::Clock {
165-
source: $clock_source,
166-
pll: init::PLL0 {
167-
enabled: true,
168-
m: $m,
169-
n: $n,
170-
divisor: $divisor,
171-
}
172-
}
173-
);
174-
}
175-
);
176-
builder.add_main_statement(cx.stmt_expr(ex));
177-
}
178-
179-
struct ClockSource {
180-
pub s: String,
181-
}
182-
183-
impl ClockSource {
184-
pub fn new(s: String) -> ClockSource {
185-
ClockSource {
186-
s: s,
187-
}
188-
}
189-
}
190-
191-
impl ToTokens for ClockSource {
192-
fn to_tokens(&self, cx: &ExtCtxt) -> Vec<TokenTree> {
193-
(cx as &ExtParseUtils).parse_tts(self.s.clone())
194-
}
195-
}
196-
}

platformtree/builder_test.rs

Lines changed: 1 addition & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -13,88 +13,7 @@
1313
// See the License for the specific language governing permissions and
1414
// limitations under the License.
1515

16-
use builder::build_platformtree;
17-
use test_helpers::{assert_equal_source, with_parsed, fails_to_build};
18-
19-
#[test]
20-
fn parses_lpc17xx() {
21-
with_parsed("mcu@lpc17xx;", |cx, failed, pt| {
22-
let builder = build_platformtree(cx, pt);
23-
assert!(unsafe{*failed} == false);
24-
assert!(builder.main_stmts.len() == 2);
25-
26-
assert_equal_source(builder.main_stmts.get(0),
27-
"{
28-
use zinc::hal::stack;
29-
extern \"C\" {
30-
static _eglobals: u32;
31-
}
32-
stack::set_stack_limit((&_eglobals as *u32) as u32);
33-
}");
34-
assert_equal_source(builder.main_stmts.get(1),
35-
"zinc::hal::mem_init::init_data()");
36-
});
37-
}
38-
39-
#[test]
40-
fn fails_to_parse_lpc17xx_with_garbage_attrs() {
41-
fails_to_build("mcu@lpc17xx { key = 1; }");
42-
}
43-
44-
#[test]
45-
fn parses_lpc17xx_clock() {
46-
with_parsed("mcu@lpc17xx {
47-
clock {
48-
source = \"main-oscillator\";
49-
source_frequency = 12_000_000;
50-
pll {
51-
m = 50;
52-
n = 3;
53-
divisor = 4;
54-
}
55-
}
56-
}", |cx, failed, pt| {
57-
let builder = build_platformtree(cx, pt);
58-
assert!(unsafe{*failed} == false);
59-
assert!(builder.main_stmts.len() == 3);
60-
61-
assert_equal_source(builder.main_stmts.get(2),
62-
"{
63-
use zinc::hal::lpc17xx::init;
64-
init::init_clock(
65-
init::Clock {
66-
source: init::Main(12000000),
67-
pll: init::PLL0 {
68-
enabled: true,
69-
m: 50u,
70-
n: 3u,
71-
divisor: 4u,
72-
},
73-
}
74-
);
75-
}");
76-
});
77-
}
78-
79-
#[test]
80-
fn fails_to_parse_lpc17xx_with_bad_clock_conf() {
81-
fails_to_build("mcu@lpc17xx { clock {
82-
no_source = 1;
83-
source_frequency = 12_000_000;
84-
}}");
85-
fails_to_build("mcu@lpc17xx { clock {
86-
source = \"missing\";
87-
source_frequency = 12_000_000;
88-
}}");
89-
}
90-
91-
#[test]
92-
fn fails_to_parse_lpc17xx_with_no_pll() {
93-
fails_to_build("mcu@lpc17xx { clock {
94-
source = \"main-oscillator\";
95-
source_frequency = 12_000_000;
96-
}}");
97-
}
16+
use test_helpers::fails_to_build;
9817

9918
#[test]
10019
fn fails_to_parse_pt_with_unknown_root_node() {

platformtree/platformtree.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ pub mod node;
2525
pub mod parser;
2626
pub mod builder;
2727

28+
#[path="../src/hal/lpc17xx/platformtree.rs"] mod lpc17xx_pt;
29+
#[cfg(test)]
30+
#[path="../src/hal/lpc17xx/platformtree_test.rs"] mod lpc17xx_pt_test;
31+
2832
#[cfg(test)] mod test_helpers;
2933
#[cfg(test)] mod parser_test;
3034
#[cfg(test)] mod builder_test;

src/hal/lpc17xx/platformtree.rs

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
// Zinc, the bare metal stack for rust.
2+
// Copyright 2014 Vladimir "farcaller" Pouzanov <[email protected]>
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
use std::gc::Gc;
17+
use syntax::ext::base::ExtCtxt;
18+
use syntax::ext::build::AstBuilder;
19+
use syntax::ext::quote::rt::{ToTokens, ExtParseUtils};
20+
use syntax::ast::TokenTree;
21+
22+
use builder::Builder;
23+
use node;
24+
25+
pub fn build_mcu(builder: &mut Builder, cx: &mut ExtCtxt,
26+
node: &Gc<node::Node>) {
27+
if !node.expect_no_attributes(cx) {
28+
return;
29+
}
30+
// init stack
31+
builder.add_main_statement(cx.stmt_expr(quote_expr!(&*cx,
32+
{
33+
use zinc::hal::stack;
34+
extern { static _eglobals: u32; }
35+
stack::set_stack_limit((&_eglobals as *u32) as u32);
36+
}
37+
)));
38+
39+
// init data
40+
builder.add_main_statement(cx.stmt_expr(quote_expr!(&*cx,
41+
zinc::hal::mem_init::init_data();
42+
)));
43+
44+
node.get_by_path("clock").and_then(|sub| -> Option<bool> {
45+
build_clock(builder, cx, sub);
46+
None
47+
});
48+
}
49+
50+
pub fn build_clock(builder: &mut Builder, cx: &mut ExtCtxt,
51+
node: &Gc<node::Node>) {
52+
if !node.expect_attributes(cx, vec!(("source", node::StringAttribute))) {
53+
return;
54+
}
55+
56+
let source = node.get_string_attr("source").unwrap();
57+
let clock_source = ClockSource::new(match source.as_slice() {
58+
"internal-oscillator" => "init::Internal".to_str(),
59+
"rtc-oscillator" => "init::RTC".to_str(),
60+
"main-oscillator" => {
61+
let some_source_frequency =
62+
node.get_required_int_attr(cx, "source_frequency");
63+
if some_source_frequency == None {
64+
"BAD".to_str()
65+
} else {
66+
format!("init::Main({})", some_source_frequency.unwrap())
67+
}
68+
},
69+
other => {
70+
cx.span_err(
71+
node.get_attr("source").value_span,
72+
format!("unknown oscillator value `{}`", other).as_slice());
73+
"BAD".to_str()
74+
},
75+
});
76+
77+
let some_pll_conf = node.get_by_path("pll").and_then(|sub|
78+
-> Option<(uint, uint, uint)> {
79+
if !sub.expect_no_subnodes(cx) || !sub.expect_attributes(cx, vec!(
80+
("m", node::IntAttribute),
81+
("n", node::IntAttribute),
82+
("divisor", node::IntAttribute))) {
83+
None
84+
} else {
85+
let m = sub.get_int_attr("m").unwrap();
86+
let n = sub.get_int_attr("n").unwrap();
87+
let divisor = sub.get_int_attr("divisor").unwrap();
88+
Some((m, n, divisor))
89+
}
90+
});
91+
if some_pll_conf.is_none() {
92+
cx.parse_sess().span_diagnostic.span_err(node.name_span,
93+
"required subnode `pll` is missing");
94+
return;
95+
}
96+
97+
let (m, n, divisor) = some_pll_conf.unwrap();
98+
99+
let ex = quote_expr!(&*cx,
100+
{
101+
use zinc::hal::lpc17xx::init;
102+
init::init_clock(
103+
init::Clock {
104+
source: $clock_source,
105+
pll: init::PLL0 {
106+
enabled: true,
107+
m: $m,
108+
n: $n,
109+
divisor: $divisor,
110+
}
111+
}
112+
);
113+
}
114+
);
115+
builder.add_main_statement(cx.stmt_expr(ex));
116+
}
117+
118+
struct ClockSource {
119+
pub s: String,
120+
}
121+
122+
impl ClockSource {
123+
pub fn new(s: String) -> ClockSource {
124+
ClockSource {
125+
s: s,
126+
}
127+
}
128+
}
129+
130+
impl ToTokens for ClockSource {
131+
fn to_tokens(&self, cx: &ExtCtxt) -> Vec<TokenTree> {
132+
(cx as &ExtParseUtils).parse_tts(self.s.clone())
133+
}
134+
}

0 commit comments

Comments
 (0)