1use proc_macro::TokenStream;
5use proc_macro2::TokenStream as TokenStream2;
6use quote::quote;
7use syn::{
8 parse_macro_input, AttributeArgs, ItemFn, Meta, NestedMeta, ReturnType, Signature, Type,
9};
10
11#[proc_macro_attribute]
42pub fn main(_attr: TokenStream, item: TokenStream) -> TokenStream {
43 let main_func = parse_macro_input!(item as ItemFn);
45
46 check_main_function_signature(&main_func.sig);
47
48 let func_name = main_func.sig.ident.to_string();
50
51 let trampoline = format!(
53 "\
54 #[no_mangle]\n\
55 extern \"Rust\" fn __main_trampoline(arg: core::sync::atomic::AtomicPtr<u8>) {{\n\
56 let arg = arg.load(core::sync::atomic::Ordering::SeqCst) as *mut cortex_m::Peripherals;\n\
57 let arg = unsafe {{ alloc::boxed::Box::from_raw(arg) }};\n\
58 {}(*arg)\n\
59 }}",
60 func_name
61 );
62
63 let trampoline = syn::parse_str::<TokenStream2>(trampoline.as_str()).unwrap();
65
66 quote! {
68 #trampoline
69 #main_func
70 }
71 .into()
72}
73
74#[cfg(feature = "armv6m")]
133#[proc_macro_attribute]
134pub fn handler(attr: TokenStream, item: TokenStream) -> TokenStream {
135 let handler_func = parse_macro_input!(item as ItemFn);
137
138 let attr_args = parse_macro_input!(attr as AttributeArgs);
140
141 check_handler_function_signature(&handler_func.sig);
142
143 let irq = parse_attribute_arg_to_irq(&attr_args);
144 let lower_caes_irq = irq.to_lowercase();
145
146 let func_name = handler_func.sig.ident.to_string();
148
149 let entry_asm = format!(
150 "\
151 #[naked]\n\
152 #[export_name = \"{}\"]\n\
153 unsafe extern \"C\" fn __hopter_{}_entry() {{\n\
154 core::arch::asm!(\n\
155 \"ldr r0, ={{tls_mem_addr}}\",\n\
156 \"ldmia r0!, {{{{r1-r3}}}}\",\n\
157 \"push {{{{r1-r3, lr}}}}\",\n\
158 \"ldr r0, ={{tls_mem_addr}}\",\n\
159 \"ldr r1, ={{cont_stk_boundary}}\",\n\
160 \"movs r2, #0\",\n\
161 \"str r1, [r0]\",\n\
162 \"str r2, [r0, #4]\",\n\
163 \"str r2, [r0, #8]\",\n\
164 // Run the IRQ handler.\n\
165 \"bl {{handler_trampoline}}\",\n\
166 \"pop {{{{r1-r3}}}}\",\n\
167 \"ldr r0, ={{tls_mem_addr}}\",\n\
168 \"stmia r0!, {{{{r1-r3}}}}\",\n\
169 // Exception return.\n\
170 \"pop {{{{pc}}}}\",\n\
171 tls_mem_addr = const hopter::config::__TLS_MEM_ADDR,\n\
172 cont_stk_boundary = const hopter::config::__CONTIGUOUS_STACK_BOUNDARY,\n\
173 handler_trampoline = sym __hopter_{}_trampoline,\n\
174 options(noreturn)\n\
175 )\n\
176 }}\n\
177 ",
178 irq, lower_caes_irq, lower_caes_irq,
179 );
180
181 let entry_asm = syn::parse_str::<TokenStream2>(entry_asm.as_str()).unwrap();
182
183 let trampoline = format!(
184 "\
185 unsafe extern \"C\" fn __hopter_{}_trampoline() {{\n\
186 let prev_is_handler_unwinding = hopter::unwind::unwind::save_and_clear_isr_unwinding();\n\
187 let _ = hopter::unwind::unw_catch::catch_unwind({});\n\
188 hopter::unwind::unwind::set_isr_unwinding(prev_is_handler_unwinding);\n\
189 }}\n\
190 ",
191 lower_caes_irq, func_name,
192 );
193
194 let trampoline = syn::parse_str::<TokenStream2>(trampoline.as_str()).unwrap();
196
197 quote! {
199 #entry_asm
200 #trampoline
201 #handler_func
202 }
203 .into()
204}
205
206#[cfg(not(feature = "armv6m"))]
263#[proc_macro_attribute]
264pub fn handler(attr: TokenStream, item: TokenStream) -> TokenStream {
265 let handler_func = parse_macro_input!(item as ItemFn);
267
268 let attr_args = parse_macro_input!(attr as AttributeArgs);
270
271 check_handler_function_signature(&handler_func.sig);
272
273 let irq = parse_attribute_arg_to_irq(&attr_args);
274 let lower_caes_irq = irq.to_lowercase();
275
276 let func_name = handler_func.sig.ident.to_string();
278
279 let entry_asm = format!(
280 "\
281 #[naked]\n\
282 #[export_name = \"{}\"]\n\
283 unsafe extern \"C\" fn __hopter_{}_entry() {{\n\
284 core::arch::asm!(\n\
285 \"ldr r0, ={{tls_mem_addr}}\",\n\
286 \"ldmia r0, {{{{r1-r3}}}}\",\n\
287 \"push {{{{r1-r3, lr}}}}\",\n\
288 \"ldr r1, ={{cont_stk_boundary}}\",\n\
289 \"mov r2, #0\",\n\
290 \"strd r1, r2, [r0]\",\n\
291 \"str r2, [r0, #8]\",\n\
292 // Run the IRQ handler.\n\
293 \"bl {{handler_trampoline}}\",\n\
294 \"pop {{{{r1-r3}}}}\",\n\
295 \"ldr r0, ={{tls_mem_addr}}\",\n\
296 \"stmia r0, {{{{r1-r3}}}}\",\n\
297 // Exception return.\n\
298 \"pop {{{{pc}}}}\",\n\
299 tls_mem_addr = const hopter::config::__TLS_MEM_ADDR,\n\
300 cont_stk_boundary = const hopter::config::__CONTIGUOUS_STACK_BOUNDARY,\n\
301 handler_trampoline = sym __hopter_{}_trampoline,\n\
302 options(noreturn)\n\
303 )\n\
304 }}\n\
305 ",
306 irq, lower_caes_irq, lower_caes_irq,
307 );
308
309 let entry_asm = syn::parse_str::<TokenStream2>(entry_asm.as_str()).unwrap();
310
311 let trampoline = format!(
312 "\
313 unsafe extern \"C\" fn __hopter_{}_trampoline() {{\n\
314 let prev_is_handler_unwinding = hopter::unwind::unwind::save_and_clear_isr_unwinding();\n\
315 let _ = hopter::unwind::unw_catch::catch_unwind({});\n\
316 hopter::unwind::unwind::set_isr_unwinding(prev_is_handler_unwinding);\n\
317 }}\n\
318 ",
319 lower_caes_irq, func_name,
320 );
321
322 let trampoline = syn::parse_str::<TokenStream2>(trampoline.as_str()).unwrap();
324
325 quote! {
327 #entry_asm
328 #trampoline
329 #handler_func
330 }
331 .into()
332}
333
334macro_rules! hander_macro_arg_error {
335 () => {
336 "Handler's argument must be one of the supported IRQs. Forgot to set the MCU model feature?"
337 };
338}
339
340macro_rules! hander_macro_retval_error {
341 () => {
342 "Handler's return type must be ()."
343 };
344}
345
346fn check_main_function_signature(sig: &Signature) {
353 if sig.inputs.iter().count() != 1 {
354 panic!("Main function must receive one argument of type `cortex_m::Peripherals`.");
355 }
356
357 match &sig.output {
358 ReturnType::Default => {}
360 ReturnType::Type(_, b) => match &**b {
362 Type::Tuple(t) => {
363 if t.elems.len() != 0 {
364 panic!(hander_macro_retval_error!());
365 }
366 }
367 Type::Never(_) => {}
368 _ => panic!(hander_macro_retval_error!()),
369 },
370 }
371
372 if sig.asyncness.is_some() {
373 panic!("Main function cannot be `async`.");
374 }
375
376 if sig.unsafety.is_some() {
377 panic!("Main function must be safe.");
378 }
379
380 if sig.variadic.is_some() {
381 panic!("Handler function cannot be variadic.");
382 }
383}
384
385fn check_handler_function_signature(sig: &Signature) {
391 if sig.inputs.iter().count() != 0 {
392 panic!("Handler function should not have any parameter.");
393 }
394
395 match &sig.output {
396 ReturnType::Default => {}
398 ReturnType::Type(_, b) => match &**b {
400 Type::Tuple(t) => {
401 if t.elems.len() != 0 {
402 panic!(hander_macro_retval_error!());
403 }
404 }
405 _ => panic!(hander_macro_retval_error!()),
406 },
407 }
408
409 if sig.abi.is_some() {
410 panic!("Handler function must have Rust ABI.");
411 }
412
413 if sig.asyncness.is_some() {
414 panic!("Handler function cannot be `async`.");
415 }
416
417 if sig.variadic.is_some() {
418 panic!("Handler function cannot be variadic.");
419 }
420}
421
422fn parse_attribute_arg_to_irq(attr_args: &[NestedMeta]) -> String {
425 if attr_args.len() != 1 {
427 panic!(hander_macro_arg_error!());
428 }
429
430 let arg = match attr_args.first().unwrap() {
432 NestedMeta::Meta(Meta::Path(ss)) => quote! { #ss }.to_string(),
433 _ => panic!(hander_macro_arg_error!()),
434 };
435
436 if !SUPPORTED_IRQS.iter().any(|irq| irq == &arg) {
438 panic!(hander_macro_arg_error!());
439 }
440
441 arg
442}
443
444#[cfg(any(
445 feature = "stm32f401",
446 feature = "stm32f405",
447 feature = "stm32f407",
448 feature = "stm32f410",
449 feature = "stm32f411",
450 feature = "stm32f412",
451 feature = "stm32f413",
452 feature = "stm32f427",
453 feature = "stm32f429",
454 feature = "stm32f446",
455 feature = "stm32f469",
456))]
457mod stm32f4;
458
459#[cfg(any(
460 feature = "stm32f401",
461 feature = "stm32f405",
462 feature = "stm32f407",
463 feature = "stm32f410",
464 feature = "stm32f411",
465 feature = "stm32f412",
466 feature = "stm32f413",
467 feature = "stm32f427",
468 feature = "stm32f429",
469 feature = "stm32f446",
470 feature = "stm32f469",
471))]
472use stm32f4::SUPPORTED_IRQS;
473
474#[cfg(any(
475 feature = "stm32f030",
476 feature = "stm32f030x4",
477 feature = "stm32f030x6",
478 feature = "stm32f030x8",
479 feature = "stm32f030xc",
480 feature = "stm32f031",
481 feature = "stm32f038",
482 feature = "stm32f042",
483 feature = "stm32f048",
484 feature = "stm32f051",
485 feature = "stm32f058",
486 feature = "stm32f070",
487 feature = "stm32f070x6",
488 feature = "stm32f070xb",
489 feature = "stm32f071",
490 feature = "stm32f072",
491 feature = "stm32f078",
492 feature = "stm32f091",
493 feature = "stm32f098",
494))]
495mod stm32f0;
496
497#[cfg(any(
498 feature = "stm32f030",
499 feature = "stm32f030x4",
500 feature = "stm32f030x6",
501 feature = "stm32f030x8",
502 feature = "stm32f030xc",
503 feature = "stm32f031",
504 feature = "stm32f038",
505 feature = "stm32f042",
506 feature = "stm32f048",
507 feature = "stm32f051",
508 feature = "stm32f058",
509 feature = "stm32f070",
510 feature = "stm32f070x6",
511 feature = "stm32f070xb",
512 feature = "stm32f071",
513 feature = "stm32f072",
514 feature = "stm32f078",
515 feature = "stm32f091",
516 feature = "stm32f098",
517))]
518use stm32f0::SUPPORTED_IRQS;
519
520#[cfg(not(any(
521 feature = "stm32f401",
522 feature = "stm32f405",
523 feature = "stm32f407",
524 feature = "stm32f410",
525 feature = "stm32f411",
526 feature = "stm32f412",
527 feature = "stm32f413",
528 feature = "stm32f427",
529 feature = "stm32f429",
530 feature = "stm32f446",
531 feature = "stm32f469",
532 feature = "stm32f030",
533 feature = "stm32f030x4",
534 feature = "stm32f030x6",
535 feature = "stm32f030x8",
536 feature = "stm32f030xc",
537 feature = "stm32f031",
538 feature = "stm32f038",
539 feature = "stm32f042",
540 feature = "stm32f048",
541 feature = "stm32f051",
542 feature = "stm32f058",
543 feature = "stm32f070",
544 feature = "stm32f070x6",
545 feature = "stm32f070xb",
546 feature = "stm32f071",
547 feature = "stm32f072",
548 feature = "stm32f078",
549 feature = "stm32f091",
550 feature = "stm32f098",
551)))]
552const SUPPORTED_IRQS: [&str; 0] = [];