From 286568a5a77cda297704642ce7c82bda4de610a2 Mon Sep 17 00:00:00 2001 From: James Munns Date: Thu, 7 Mar 2019 01:04:02 +0100 Subject: [PATCH 1/4] Add Sharing Data with Interrupts --- README.md | 43 ++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 40 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index b4f79f5..19db291 100644 --- a/README.md +++ b/README.md @@ -26,13 +26,50 @@ And don't forget to check our [Awesome Embedded Rust][aer] list! The thing you a * Useful Links * [Contributing Guide] - * [Item Template](https://github.com/rust-embedded/not-yet-awesome-embedded-rust#not-yet-awesome-item-template) + * [Item Template](#not-yet-awesome-item-template) * Not Yet Awesome List - * [nothing yet...](#) + * [nothing yet...](#sharing-data-with-interrupts) # The List -Nothing here yet... +## Sharing Data With Interrupts + +### Background + +Currently, it is not convenient to share data with an interrupt using safe Rust. Because interrupt handlers are functions that take no arguments, all data consumed by interrupts must either be local `static` variables, or global `static` variables. + +Global variables are not great in Rust, because: + +* All mutable access must be `unsafe` +* Not all data can be initialized in a `const` context, so it's often necessary to use an `Option` +* Global variables aren't typically idiomatic Rust. + +Tools like [cortex-m-rtfm] achieve this in a zero cost fashion by using a Domain Specific Language to automatically provide safe access to shared resources between tasks and interrupts, however these tools can not be used by applications not using RTFM, or by libraries such as HAL or BSP crates. + +**Useful Links** + +* [wg#294] - An Embedded-WG issue discussing this topic +* [bare-metal#15] - One proposed solution hiding the un-idiomatic syntax + +[wg#294]: https://github.com/rust-embedded/wg/issues/294 +[bare-metal#15]: https://github.com/japaric/bare-metal/pull/15 +[cortex-m-rtfm]: https://github.com/japaric/cortex-m-rtfm + +### Success Criteria + +Ideally, we would be able to support all of the following use cases: + +1. Sharing a variable between the main thread and only one interrupt handler +2. Sharing a variable between the main thread and one or more interrupt handlers +3. Moving a variable from the main thread to one interrupt handler + +We should be able to serve the three use cases listed above, while: + +* Using only safe Rust (at least as a user of the library) +* Add only minimal overhead, if not "zero cost" + + + # Not Yet Awesome Item Template From 9fd0073149500ae6eb4c2e13990272bafbfa5aeb Mon Sep 17 00:00:00 2001 From: James Munns Date: Thu, 7 Mar 2019 01:06:37 +0100 Subject: [PATCH 2/4] Fix ToC, HTML comments --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 19db291..ae60c51 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,7 @@ And don't forget to check our [Awesome Embedded Rust][aer] list! The thing you a * [Contributing Guide] * [Item Template](#not-yet-awesome-item-template) * Not Yet Awesome List - * [nothing yet...](#sharing-data-with-interrupts) + * [Sharing Data With Interrupts](#sharing-data-with-interrupts) # The List @@ -68,8 +68,8 @@ We should be able to serve the three use cases listed above, while: * Using only safe Rust (at least as a user of the library) * Add only minimal overhead, if not "zero cost" - - + + # Not Yet Awesome Item Template From 3941a77cfc96e29e3b861953d4bd0c6c679eafde Mon Sep 17 00:00:00 2001 From: James Munns Date: Thu, 7 Mar 2019 12:19:48 +0100 Subject: [PATCH 3/4] Address review comments from @japaric --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index ae60c51..1221a52 100644 --- a/README.md +++ b/README.md @@ -36,15 +36,15 @@ And don't forget to check our [Awesome Embedded Rust][aer] list! The thing you a ### Background -Currently, it is not convenient to share data with an interrupt using safe Rust. Because interrupt handlers are functions that take no arguments, all data consumed by interrupts must either be local `static` variables, or global `static` variables. +Currently, it is not convenient to share non-atomic data with an interrupt using safe Rust. Because interrupt handlers are functions that take no arguments, all data consumed by interrupts must either be Atomic (e.g. `AtomicBool`), local `static` variables, or global or module scoped `static` variables. Global variables are not great in Rust, because: * All mutable access must be `unsafe` -* Not all data can be initialized in a `const` context, so it's often necessary to use an `Option` +* Not all data can be initialized in a `const` context, so it's often necessary to use an `Option` to delay the initialization at runtime * Global variables aren't typically idiomatic Rust. -Tools like [cortex-m-rtfm] achieve this in a zero cost fashion by using a Domain Specific Language to automatically provide safe access to shared resources between tasks and interrupts, however these tools can not be used by applications not using RTFM, or by libraries such as HAL or BSP crates. +Frameworks like [cortex-m-rtfm] achieve this in a zero cost fashion by using a Domain Specific Language to automatically provide safe access to shared resources between tasks and interrupts, however these tools can not be used by applications not using RTFM, or by libraries such as HAL or BSP crates. **Useful Links** From dac52ceb6131df9a5d7e7f669481f02e94ae8962 Mon Sep 17 00:00:00 2001 From: James Munns Date: Thu, 7 Mar 2019 12:22:21 +0100 Subject: [PATCH 4/4] A couple more clarifications --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 1221a52..b129f45 100644 --- a/README.md +++ b/README.md @@ -36,12 +36,12 @@ And don't forget to check our [Awesome Embedded Rust][aer] list! The thing you a ### Background -Currently, it is not convenient to share non-atomic data with an interrupt using safe Rust. Because interrupt handlers are functions that take no arguments, all data consumed by interrupts must either be Atomic (e.g. `AtomicBool`), local `static` variables, or global or module scoped `static` variables. +Currently, it is not convenient to share non-atomic or non-`Sync` data with an interrupt using safe Rust. Because interrupt handlers are functions that take no arguments, all data consumed by interrupts must either be global or module scoped Atomics (e.g. `AtomicBool`), local `static` variables, or global or module scoped `static` variables. Global variables are not great in Rust, because: -* All mutable access must be `unsafe` -* Not all data can be initialized in a `const` context, so it's often necessary to use an `Option` to delay the initialization at runtime +* All mutable access (of non-`Sync`/`Atomic` data) must be `unsafe` +* Not all data can be initialized in a `const` context, so it's often necessary to use an `Option` to delay the initialization to runtime * Global variables aren't typically idiomatic Rust. Frameworks like [cortex-m-rtfm] achieve this in a zero cost fashion by using a Domain Specific Language to automatically provide safe access to shared resources between tasks and interrupts, however these tools can not be used by applications not using RTFM, or by libraries such as HAL or BSP crates.