Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LEDs: Expose presence #45

Merged
merged 2 commits into from
Aug 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 53 additions & 24 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,11 +263,11 @@ fn main() {
// These constant initializers are unusable without knowledge of which type they're for; adding
// the information here to build explicit consts
let macro_functions = [
("SOCK_IPV4_EP_ANY", "sock_udp_ep_t", None, true),
("SOCK_IPV6_EP_ANY", "sock_udp_ep_t", None, true),
("MUTEX_INIT", "mutex_t", None, true),
("SOCK_IPV4_EP_ANY", "sock_udp_ep_t", None, true, None),
("SOCK_IPV6_EP_ANY", "sock_udp_ep_t", None, true, None),
("MUTEX_INIT", "mutex_t", None, true, None),
// neither C2Rust nor bindgen understand the cast without help
("STATUS_NOT_FOUND", "thread_status_t", None, true),
("STATUS_NOT_FOUND", "thread_status_t", None, true, None),
// If any board is ever added that works completely differently, this'll have to go behind
// a feature-gate
(
Expand All @@ -277,29 +277,45 @@ fn main() {
// would be nice to have them const, but on boards like samd21-xpro that'd require
// several nightly features (const_ptr_offset, const_mut_refs).
false,
None,
),
// These are bound to the signature already in periph_init.
("I2C_DEV", "i2c_t", Some("unsigned num"), false),
("SPI_DEV", "spi_t", Some("unsigned num"), false),
("I2C_DEV", "i2c_t", Some("unsigned num"), false, None),
("SPI_DEV", "spi_t", Some("unsigned num"), false, None),
// No good source on why this sould have a fixed signature, but at this point it's a
// pattern.
("UART_DEV", "uart_t", Some("unsigned num"), false),
("PWM_DEV", "pwm_t", Some("unsigned num"), false),
("ADC_LINE", "adc_t", Some("unsigned num"), false),
("TIMER_DEV", "timer_t", Some("unsigned num"), false),
("QDEC_DEV", "qdec_t", Some("unsigned num"), false),
("DAC_LINE", "dac_t", Some("unsigned num"), false),
("UART_DEV", "uart_t", Some("unsigned num"), false, None),
("PWM_DEV", "pwm_t", Some("unsigned num"), false, None),
("ADC_LINE", "adc_t", Some("unsigned num"), false, None),
("TIMER_DEV", "timer_t", Some("unsigned num"), false, None),
("QDEC_DEV", "qdec_t", Some("unsigned num"), false, None),
("DAC_LINE", "dac_t", Some("unsigned num"), false, None),
];
let mut macro_functions: Vec<_> = macro_functions
.iter()
.map(|(macro_name, return_type, args, is_const)| {
(macro_name.to_string(), *return_type, *args, *is_const)
})
.map(
|(macro_name, return_type, args, is_const, fallback_value)| {
(
macro_name.to_string(),
*return_type,
*args,
*is_const,
*fallback_value,
)
},
)
.collect();
for i in 0..8 {
macro_functions.push((format!("LED{}_ON", i), "void", None, false));
macro_functions.push((format!("LED{}_OFF", i), "void", None, false));
macro_functions.push((format!("LED{}_TOGGLE", i), "void", None, false));
macro_functions.push((format!("LED{}_ON", i), "void", None, false, None));
macro_functions.push((format!("LED{}_OFF", i), "void", None, false, None));
macro_functions.push((format!("LED{}_TOGGLE", i), "void", None, false, None));
macro_functions.push((
format!("LED{}_IS_PRESENT", i),
"int",
None,
true,
Some("-1"),
));
}

let mut c_code = String::new();
Expand All @@ -308,7 +324,7 @@ fn main() {
.read_to_string(&mut c_code)
.expect("Failed to read riot-c2rust.h");

for (macro_name, return_type, args, _is_const) in macro_functions.iter() {
for (macro_name, return_type, args, _is_const, fallback_value) in macro_functions.iter() {
let argnames = match args {
None => "".to_string(),
Some("void") => "()".to_string(),
Expand Down Expand Up @@ -340,7 +356,6 @@ fn main() {
{return_type} macro_{macro_name}({args}) {{
{macro_name}{argnames};
}}
#endif
",
return_type = return_type,
macro_name = macro_name,
Expand All @@ -357,7 +372,6 @@ fn main() {
{return_type} result = {macro_name}{argnames};
return result;
}}
#endif
",
return_type = return_type,
macro_name = macro_name,
Expand All @@ -366,6 +380,21 @@ fn main() {
)
}
.unwrap();

if let Some(fallback_value) = fallback_value {
writeln!(
c_code,
r"
#else
{return_type} macro_{macro_name}({args}) {{
return {fallback_value};
}}
",
args = args.unwrap_or("void"),
)
.unwrap();
}
writeln!(c_code, r" #endif").unwrap();
}

let mut outfile =
Expand Down Expand Up @@ -509,7 +538,7 @@ fn main() {
let macro_details = if funcname.len() > 5 && &funcname[..6] == "macro_" {
macro_functions
.iter()
.filter(|(macro_name, _, _, _)| &funcname[6..] == *macro_name)
.filter(|(macro_name, _, _, _, _)| &funcname[6..] == *macro_name)
.next()
} else {
None
Expand All @@ -530,7 +559,7 @@ fn main() {

// As below (no need for extern), and they are const as declared ni the macro_functions
// list.
(_, Some((_, _, _, is_const))) => {
(_, Some((_, _, _, is_const, _))) => {
// No "pub" because that's already a "pub" in front of it, they were never static
match is_const {
// FIXME: These should be unsafe -- just because most of them are const doesn't
Expand Down Expand Up @@ -611,7 +640,7 @@ fn main() {
.iter()
.map(|name| name.to_string())
.collect();
for (macro_name, _, _, _) in macro_functions.iter() {
for (macro_name, _, _, _, _) in macro_functions.iter() {
toplevel_from_inline.push(format!("macro_{}", macro_name));
}
let toplevel_from_inline: Vec<String> = toplevel_from_inline
Expand Down
Loading