Skip to content

Commit de25c4c

Browse files
bors[bot]n8tlarsenAfoHT
authored
Merge #670
670: Add documentation for different Cortex-M architectures r=AfoHT a=n8tlarsen Most of the RTIC documentation focuses on ARMv7-M architectures. Here's some initial thoughts on useful information I would have liked to know before starting with RTIC on ARMv6-M. Co-authored-by: Nathan <[email protected]> Co-authored-by: n8tlarsen <[email protected]> Co-authored-by: Henrik Tjäder <[email protected]>
2 parents 1d4ddb3 + 9c68b87 commit de25c4c

File tree

3 files changed

+76
-1
lines changed

3 files changed

+76
-1
lines changed

book/en/src/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
- [v0.4.x to v0.5.x](./migration/migration_v4.md)
3030
- [RTFM to RTIC](./migration/migration_rtic.md)
3131
- [Under the hood](./internals.md)
32+
- [Cortex-M architectures](./internals/targets.md)
3233
<!--- [Interrupt configuration](./internals/interrupt-configuration.md)-->
3334
<!--- [Non-reentrancy](./internals/non-reentrancy.md)-->
3435
<!--- [Access control](./internals/access.md)-->

book/en/src/by-example/starting_a_project.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# Starting a new project
22

3-
A recommendation when starting a RTIC project from scratch is to follow RTIC's [`defmt-app-template`].
3+
A recommendation when starting a RTIC project from scratch is to
4+
follow RTIC's [`defmt-app-template`].
5+
6+
If you are targeting ARMv6-M or ARMv8-M-base architecture, check out the section [Target Architecture](../internals/targets.md) for more information on hardware limitations to be aware of.
47

58
[`defmt-app-template`]: https://github.com/rtic-rs/defmt-app-template
69

book/en/src/internals/targets.md

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# Target Architecture
2+
3+
While RTIC can currently target all Cortex-m devices there are some key architecure differences that
4+
users should be aware of. Namely the absence of Base Priority Mask Register (`BASEPRI`) which lends
5+
itself exceptionally well to the hardware priority ceiling support used in RTIC, in the ARMv6-M and
6+
ARMv8-M-base architectures, which forces RTIC to use source masking instead. For each implementation
7+
of lock and a detailed commentary of pros and cons, see the implementation of
8+
[lock in src/export.rs][src_export].
9+
10+
[src_export]: https://github.com/rtic-rs/cortex-m-rtic/blob/master/src/export.rs
11+
12+
These differences influence how critical sections are realized, but functionality should be the same
13+
except that ARMv6-M/ARMv8-M-base cannot have tasks with shared resources bound to exception
14+
handlers, as these cannot be masked in hardware.
15+
16+
Table 1 below shows a list of Cortex-m processors and which type of critical section they employ.
17+
18+
#### *Table 1: Critical Section Implementation by Processor Architecture*
19+
20+
| Processor | Architecture | Priority Ceiling | Source Masking |
21+
| :--------- | :----------: | :--------------: | :------------: |
22+
| Cortex-M0 | ARMv6-M | | &#2713 |
23+
| Cortex-M0+ | ARMv6-M | | &#2713 |
24+
| Cortex-M3 | ARMv7-M | &#2713 | |
25+
| Cortex-M4 | ARMv7-M | &#2713 | |
26+
| Cortex-M7 | ARMv7-M | &#2713 | |
27+
| Cortex-M23 | ARMv8-M-base | | &#2713 |
28+
| Cortex-M33 | ARMv8-M-main | &#2713 | |
29+
30+
## Priority Ceiling
31+
32+
This implementation is covered in depth by the [Critical Sections][critical_sections] page of this book.
33+
34+
## Source Masking
35+
36+
Without a `BASEPRI` register which allows for directly setting a priority ceiling in the Nested
37+
Vectored Interrupt Controller (NVIC), RTIC must instead rely on disabling (masking) interrupts.
38+
Consider Figure 1 below, showing two tasks A and B where A has higher priority but shares a resource
39+
with B.
40+
41+
#### *Figure 1: Shared Resources and Source Masking*
42+
43+
```text
44+
┌────────────────────────────────────────────────────────────────┐
45+
│ │
46+
│ │
47+
3 │ Pending Preempts │
48+
2 │ ↑- - -A- - - - -↓A─────────► │
49+
1 │ B───────────────────► - - - - B────────► │
50+
0 │Idle┌─────► Resumes ┌────────► │
51+
├────┴────────────────────────────────────────────┴──────────────┤
52+
│ │
53+
└────────────────────────────────────────────────────────────────┴──► Time
54+
t1 t2 t3 t4
55+
```
56+
57+
At time *t1*, task B locks the shared resource by selectively disabling (using the NVIC) all other
58+
tasks which have a priority equal to or less than any task which shares resouces with B. In effect
59+
this creates a virtual priority ceiling, miroring the `BASEPRI` approach described in the
60+
[Critical Sections][critical_Sections] page. Task A is one such task that shares resources with
61+
task B. At time *t2*, task A is either spawned by task B or becomes pending through an interrupt
62+
condition, but does not yet preempt task B even though its priority is greater. This is because the
63+
NVIC is preventing it from starting due to task A being being disabled. At time *t3*, task B
64+
releases the lock by re-enabling the tasks in the NVIC. Because task A was pending and has a higher
65+
priority than task B, it immediately preempts task B and is free to use the shared resource without
66+
risk of data race conditions. At time *t4*, task A completes and returns the execution context to B.
67+
68+
Since source masking relies on use of the NVIC, core exception sources such as HardFault, SVCall,
69+
PendSV, and SysTick cannot share data with other tasks.
70+
71+
[critical_sections]: https://github.com/rtic-rs/cortex-m-rtic/blob/master/book/en/src/internals/critical-sections.md

0 commit comments

Comments
 (0)