You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/tutorials/using_apis/events_tutorial.md
+7-7Lines changed: 7 additions & 7 deletions
Original file line number
Diff line number
Diff line change
@@ -34,7 +34,7 @@ Note that though this document assumes the presence of a single event loop in th
34
34
35
35
Once you start the event loop, it can post events. Let's consider an example of a program that attaches two interrupt handlers for an InterruptIn object, using the InterruptIn `rise` and `fall` functions. The `rise` handler will run in interrupt context, and the `fall` handler will run in user context (more specifically, in the context of the event loop's thread). The full code for the example can be found below:
The above code executes two handler functions (`rise_handler` and `fall_handler`) in two different contexts:
40
40
@@ -44,14 +44,14 @@ The above code executes two handler functions (`rise_handler` and `fall_handler`
44
44
This is the output of the above program on an FRDM-K64F board. We reset the board and pressed the SW2 button twice:
45
45
46
46
```
47
-
Starting in context 0x20002c50
48
-
fall_handler in context 0x20002c90
49
-
rise_handler in context 0x0
50
-
fall_handler in context 0x20002c90
51
-
rise_handler in context 0x0
47
+
Starting in context 20001fe0
48
+
fall_handler in context 20000b1c
49
+
rise_handler in context 00000000
50
+
fall_handler in context 20000b1c
51
+
rise_handler in context 00000000
52
52
```
53
53
54
-
The program starts in the context of the thread that runs the `main` function (`0x29992c5`). When the user presses SW2, `fall_handler` is automatically queued in the event queue, and it runs later in the context of thread `t` (`0x20002c90`). When the user releases the button, `rise_handler` is executed immediately, and it displays `0x0`, indicating that the code ran in interrupt context.
54
+
The program starts in the context of the thread that runs the `main` function (`20001fe0`). When the user presses SW2, `fall_handler` is automatically queued in the event queue, and it runs later in the context of thread `t` (`20000b1c`). When the user releases the button, `rise_handler` is executed immediately, and it displays `00000000`, indicating that the code ran in interrupt context.
55
55
56
56
The code for `rise_handler` is problematic because it calls `printf` in interrupt context, which is a potentially unsafe operation. Fortunately, this is exactly the kind of problem that event queues can solve. We can make the code safe by running `rise_handler` in user context (like we already do with `fall_handler`) by replacing this line:
0 commit comments