Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upHow to integrate interrupts into embedded-hal #57
Comments
hannobraun
added
the
discussion
label
Mar 7, 2018
This comment has been minimized.
This comment has been minimized.
|
Good stuff you're adding a new ticket for this very important topic. There's from my side at least three important aspects to cover:
All of this has to be done manually right now but at least for the setup of the interrupt handlers there's already some support (for Cortex-M MCUs) in the
Ideally there would be a possibility for an embedded-hal implementation to insert a static interrupt handler definition at compile time from a macro call inside the program. Only at this level the compiler can ensure that the required resources are still available and the HAL implementation that the interrupt generation setup and the interrupt handler itself match up and are will be operational. One alternative might be to always use a veneer for all interrupt handlers and allow the HAL to assign the real interrupt handler at runtime. Of course higher level Cortex-Ms and other architectures may allow to change the interrupt handlers on the fly instead of being fixed addresses in flash. |
This comment has been minimized.
This comment has been minimized.
nordmoen
commented
Mar 8, 2018
|
Just want to chime in and say that this is sorely needed. I'm developing a small crate for Without much experience in embedded work, the workflow I would imagine is something along the line of users passing a type implementing |
This comment has been minimized.
This comment has been minimized.
|
@nordmoen Unfortunately it doesn't quite work like this. First of all each platform has a completely different way of setting up the interrupts and different capabilities when it comes to them (e.g. some platforms have a number of interrupts and you're completely free to map them to certain "events" happing in the system, while others have very specific interrupts mapped to exact peripherals). Then there's a lot of options depending on user choice, e.g. if you want to have an interrupt when a button is pressed you'll need to tell the system whether it is pulled low or high and also whether you'd like it edge-triggered or level-triggered; some also allow debouncing, etc. Then there's different ways how the interrupts work in the system, e.g. Cortex-M0 have a fixed interrupt/exception handler table at the beginning of the flash, whereas Cortex-M3 or higher have the same but can can change the address of the interrupt handler table and then there're other chips allowing to completely dynamically remap interrupts. And of course, once you successfully arrived in the interrupt handler, you still need to know how to deal with the interrupt, i.e. figure out what exactly the cause of the interrupt was and also clear it, which again, is completely MCU dependent... |
This comment has been minimized.
This comment has been minimized.
puzrin
commented
Mar 16, 2018
|
IMHO at HAL level of abstraction interrupts are not self-sufficient. Every interrupt is a part of hardware block (timer, dma, spi, i2c, ...) with specific details.
|
This comment has been minimized.
This comment has been minimized.
jcsoo
commented
Mar 21, 2018
|
To me, That doesn't mean that these traits are optimal for every purpose. If they aren't, one can use a more suitable driver-specific API or write a custom driver that does exactly what is needed. I do think there will ultimately be a handful of traits for each hardware protocol - we already have blocking and non-blocking variants, and I can also see zero-copy ownership-passing interfaces as well as command queues and scatter-gather for some types of hardware. However, even those more complex traits must abstract away the details of how drivers are implemented and instead focus on the protocol that the client and driver agree to. |
This comment has been minimized.
This comment has been minimized.
And by "driver" you mean? At the moment they're they are the responsibility of the application. And while I do agree with everything you've said I still think we should at least have the capability of simplifying the use of interrupts with embedded-hal implementations. |
This comment has been minimized.
This comment has been minimized.
jcsoo
commented
Mar 21, 2018
By "driver" I mean whatever concrete type is implementing the trait, and "client" covers the code that is calling the methods of the trait. "Application" covers a lot of territory and could include code on both sides of that boundary. As far as interrupts go, I see two separate but related concerns. First is the issue of hardware interrupts and the various methods of configuring them, especially how one dispatches to handlers at either build time or run time. That requires using a combination of vendor-specific code to configure the interrupts (maybe "Low-Level HAL" is a good term for this), perhaps a cross-platform build-time or link-time tool (RTFM and cortex-m-rt do some of this) to manage static dispatching, or alternatively, architecture-specific tools for managing vector relocation (as on Cortex-M) and/or run-time interrupt dispatch mechanisms. Second is the question of how to structure an application as a whole to work well in an asynchronous environment. Embedded-HAL doesn't really deal with that at all - it simply supports a blocking or non-blocking model and pushes policy to either the driver or the code calling the interface. For some applications a busy loop or call to WFI is fine, but for others we start wanting some parts of an RTOS. Both of those areas haven't had as much attention as Embedded-HAL itself because they involve much more policy and complexity than Embedded-HAL. I'd be very interesting in seeing specific examples of applications that run into these issues. I've done some experimentation with both dynamic handler assignment using relocated vector tables and dispatchers, each of which has strengths and weaknesses, and I've also been working on coming up with a core set of primitives on which to base a simple RTOS. |
This comment has been minimized.
This comment has been minimized.
I respectfully disagree. The application (or more commonly called "firmware" in embedded systems) is the binary running on the system which sets up the hardware and drivers and binds all bits and pieces together to achieve whatever it is supposed to do in the end. While it could theoretically provide an HAL implementation consumed by the drivers it uses, this is not really the intended use case.
I agree with the two points but you're missing one important detail from my POV: the traits in embedded-hal serve as a common interface for HAL implementations, drivers and applications. And as such I would also expect a common interface to figure out which interrupts can be used by a peripheral, activate it and of course take ownership. embedded-hal seems like the natural place for such an interface. |
This comment has been minimized.
This comment has been minimized.
jcsoo
commented
Mar 21, 2018
My understanding is that embedded-hal is specifically about common ways of using drivers and explicitly excludes initializing and configuring them - see the documentation:
Initialization and configuration (of which interrupt management is one part) is hugely important, but it's also incredibly broad with many, many possible approaches. Because of that I think it's better handled outside of this crate, which should keep its focus on defining a minimal set of interfaces for interoperability. |
This comment has been minimized.
This comment has been minimized.
|
Yes, the embedded-hal traits define the interface, they do not actually do anything (well, not a lot) by themselves. |
This comment has been minimized.
This comment has been minimized.
puzrin
commented
Mar 21, 2018
Do you mean that interrupts can be relocated in MCU on hardware level? That looks reasonable point to reflect support in independent interface. |
This comment has been minimized.
This comment has been minimized.
|
@puzrin Some (most?) MCU have mappable interrupts, e.g. STM32 with the EXTI but that's only a part of my point. When you're using interrupts there's a common set of operations you'll typically need to do:
By having a generic trait for interrupts in embedded-hal, the HAL implementation can provide specific implementations for the trait for the available interrupts and model them similarly to other peripherals thus simplifying the handling. |
This comment has been minimized.
This comment has been minimized.
puzrin
commented
Mar 22, 2018
|
For exti it could be reasonable to have exti hal, not abstract interrupts call. I can't understand what can be common with dma or timer there. |
This comment has been minimized.
This comment has been minimized.
|
EXTI is STM32 specific and thus not a good choice for a trait. The abstraction is not about the interrupt handler calls but the interrupt setup and handling; it would be great if the interrupt handlers could be abstracted, too, but that would require some serious magic which is beyond my skills. I have an idea. Let me see if I can find some time later today to test it and come up with a proposal. |
This comment has been minimized.
This comment has been minimized.
puzrin
commented
Mar 22, 2018
Ah, seems i got your point. It it's something like pin definitions, that may be useful to configure autogenerated mcu hal-s. |
This comment has been minimized.
This comment has been minimized.
|
I've added a rough cut of my idea here: https://github.com/therealprof/embedded-hal/tree/features/interrupts This is what an exemplary implementation for the
To use it one would instantiate the component:
and in the interrupt handler one would clear it:
|
This comment has been minimized.
This comment has been minimized.
|
This is an interesting idea, but I have a couple of questions.
|
This comment has been minimized.
This comment has been minimized.
I'd compose it into the HAL implementation if necessary.
Using the usual |
hannobraun commentedMar 7, 2018
•
edited
Interrupts are an important topic in microcontroller programming, but so far there's no support for them in embedded-hal. I'd like to start a discussion about whether such support should be added, and what it could look like.
I think right now we need two kinds of contributions to this discussion:
There's been some previous discussion in #37.
Edit (2018-03-08): Feedback from driver authors is also needed. Added it to the list.