-
Notifications
You must be signed in to change notification settings - Fork 158
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
[RFC] Change the cortex-m-rt attribute syntax #407
Comments
I haven't thought about all of it yet but a few points:
|
Typo fixed, thanks.
This does make sense when viewing an
Well, it looks like they do share this property, yes. But the old handlers were still using
Yes, this should definitely be implementable as proposed. |
This also has the potential to make interrupt handler registration more robust. Currently it works by reexporting the interrupt enum under the same name as the With this RFC it would use a syntax like (I haven't yet thought too deeply about this, but this seems like a nice improvement if it works out – of course with rust-embedded/cortex-m-rt#250 this would instead apply to some hypothetical porcelain crate) |
Great RFC, it is very well described! However in my opinion we should not be extending the functionality of the I think this is going to be an important discussion to have soon. And I do not think making To conclude, I argue that |
Now that rust-analyzer diagnoses unsafe operations outside of |
Summary
Change the syntax of interrupt and exception handlers provided by
cortex-m-rt
to be more obvious,more internally consistent, and closer to RTFM's:
&mut T
, with the initial value provided via an#[init(<expr>)]
attribute.#[exception(SVCall)]
), instead of using the handler function name as the interrupt/exception to handle.DefaultHandler
, the IRQ number can be requested by attaching#[irqn]
to ani16
function argument. Currently the handler signature is required to take exactly onei16
argument.DefaultHandler
can access the exception frame by attaching#[exception_frame]
to an argument of type&cortex_m_rt::ExceptionFrame
.Motivation
Resource Syntax
The biggest motivation for changing the syntax used by
cortex-m-rt
is to improve the way handler resources work.Currently, handlers defined via
cortex-m-rt
's#[interrupt]
,#[exception]
and#[entry]
macroscan declare resources owned by them by declaring a list of
static mut
items as their firststatements. The procedural macros then transform them to function arguments, and their types are
changed from
T
to&mut T
. This comes with a few drawbacks:static mut
items in the function are just regularstatic mut
s. While this is potentially fixable, it still leavesstatic mut
meaning something very different in handler functions than in any other function.Here is an example that shows the shortcomings of the current syntax:
This RFC proposes a new way of declaring resources below, which should address all of these issues.
Handler Names
Another minor issue with the current implementation is that the name of the handler function determines the exception or interrupt that is handled:
This forces the user to write an unidiomatic function name containing upper-case letters, and which
might not describe the purpose of the handler very clearly.
For example, an MCU might have 3 USARTs with dedicated interrupts, each used to handle a different external peripheral. In such an application it would be clearer to call those handlers
gps_rx
,modem_rx
, anduser_rx
instead ofUSART0
,USART1
andUSART2
.Handler Arguments
Finally, two exception handlers,
DefaultHandler
andHardFault
, are special in that they take anargument of type
i16
and&ExceptionFrame
, respectively. The&ExceptionFrame
in particular isprovided by an assembly shim that cannot be disabled.
This RFC proposes a way to add the
&ExceptionFrame
(actually,&mut ExceptionFrame
) to anyexception handler, and makes it optional for
HardFault
. Thei16
exception number passed toDefaultHandler
is also made optional.How handlers are expanded now
Note how the code for declaring an
SVCall
exception handler is currently transformed by the#[exception]
attribute:This code becomes:
The proposed syntax will embrace this transformation instead of hiding it.
Detailed design
To start off, this is how the above code would be written with the syntax proposed by this RFC:
It expands to the following code:
Notably, the user-defined function stays as-is and is not transformed at all. The type of the
resource doesn't get silently changed, and the name of the function can be freely chosen.
In addition to
#[init]
, resource arguments also accept the following built-in Rust attributes:#[export_name]
,#[link_section]
,#[no_mangle]
,#[used]
. When these are encountered, theywill be applied to the generated
static
item in the trampoline, allowing fine-grained controlover the allocated memory.
Like now, resources declared on the
#[entry]
handler can be given'static
lifetime, while otherhandlers are restricted to elided lifetimes to ensure soundness. This check now has to be done in
the macro implementation by inspecting the parameter type. Named lifetime parameters are not
permitted in the parameter type. So a parameter of type
&'a mut T
would be rejected, while&mut Type<'static>
would be allowed.Exception Frame access
Currently, the
HardFault
handler is required to take an immutable&ExceptionFrame
. The proposed syntax makes that optional and opt-in.To obtain an
&ExceptionFrame
with the new syntax, the#[exception_frame]
attribute has to beplaced on the handler argument.
Another possible improvement to the
&ExceptionFrame
API is described in issue #234. While it isalso a breaking change, it is orthogonal to the syntax changes proposed in this RFC.
A more complicated example
This will get expanded to:
How We Teach This
Most of the API documentation in
cortex-m-rt
will have to be rewritten to reflect these changes.The Book also makes use of
cortex-m-rt
, mostly in the Exceptions and Interrupts chapters.These will likewise be updated to reflect the changes.
The
cortex-m-quickstart
repository will beupdated to the new version of
cortex-m-rt
once this change has been released. Other repositoriesand HALs will follow.
With the proposed changes,
cortex-m-rt
should generally become easier to teach, since it movesthings closer to RTFM and normal Rust.
Drawbacks
This is a breaking change. The proposed syntax is completely incompatible to the current syntax. Any users of
cortex-m-rt
will have to migrate.Alternatives
#[exception(<name>)]
change and continue using the function name to determine the exception).Unresolved questions
As is tradition with syntactic changes, a large amount of bikeshedding is expected.
The syntax of the
#[exception]
attribute could be adjusted to match RTFM's more closely:#[exception(binds = ExceptionName)]
instead of#[exception(ExceptionName)]
. This syntax would also be more extensible: New arguments besidebinds
could be added easily without changing the rest of the syntax.We could completely remove the support for the
irqn
argument forDefaultHandler
. The number of the currently handled IRQ can be fetched at any time by usingSCB::vect_active()
(though this API could use some improvement).The text was updated successfully, but these errors were encountered: