-
Notifications
You must be signed in to change notification settings - Fork 222
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
How to integrate interrupts into embedded-hal #57
Comments
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. |
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 |
@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... |
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.
|
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. |
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. |
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. |
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. |
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. |
Yes, the embedded-hal traits define the interface, they do not actually do anything (well, not a lot) by themselves. |
Do you mean that interrupts can be relocated in MCU on hardware level? That looks reasonable point to reflect support in independent interface. |
@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. |
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. |
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. |
Ah, seems i got your point. It it's something like pin definitions, that may be useful to configure autogenerated mcu hal-s. |
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 is an interesting idea, but I have a couple of questions.
|
I'd compose it into the HAL implementation if necessary.
Using the usual |
Agree this seems like something that would ideally be provided by embedded-hal. Can we have a list, akin to #37 about requirements? Off the top of my head, we'd need Would they need anything, at the HAL level, other than just the interrupt number? While it would be nice to encapsulate the interrupt vector setup in the HAL, I don't think that's a requirement in order to proceed with a trait or two that describes the basic functions of the interrupt controller. |
I was recently working on an embedded linux program and the concept of an interrupt for |
57: Add github actions CI r=eldruin a=ryankurte Replaces rust-embedded#56, just adding the actions executor without swapping bors or removing travis. Odds are this doesn't work but, seemingly one can't add actions to a repo from a branch so it has to be manually merged anyway and fixed in PRs... @posborne seem (un)reasonable? Co-authored-by: ryan <[email protected]>
This is an old thread, but I implemented this or a similar abstraction for Embassy, but perhaps embedded-hal is a more appropriate place to implement these generalized abstractions? These abstractions are beneficial to my use case, and I can open a PR if there's interest. |
I'm not keen on maintaining compatibility with both embedded-hal 1.0.0 and 0.2.7 in my project, so it might be a good idea to upgrade. Also: - Replaced "esp32-hal" to "esp-hal" to accommodate more ESP series chips, such as ESP32c3 (https://github.com/zephyr-atomi/esp32c3-exp). - However, this raises another issue: is a specific backend, for example "esp32", a good idea? Typically, non-ESP32 users might skip the provided esp32-interrupt and use their own backend (esp32c3 / stm32f1xx / rp2040 / ...). Since "loadcell" is a HAL-level library, it might be better not to lock it to just ESP32. Maybe we can consider removing the interrupt; after all, implementing interrupts seems challenging in embedded-hal: rust-embedded/embedded-hal#57
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.
The text was updated successfully, but these errors were encountered: