Skip to content

Commit 01820e8

Browse files
committed
monitor: Add trigger function. Improve README.
1 parent 9289744 commit 01820e8

File tree

5 files changed

+32
-8
lines changed

5 files changed

+32
-8
lines changed

v3/as_demos/monitor/README.md

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,21 @@ system state at the time of the transient event may be examined.
2121

2222
The following image shows the `quick_test.py` code being monitored at the point
2323
when a task hogs the CPU. The top line 00 shows the "hog detect" trigger. Line
24-
02 shows the fast running `hog_detect` task which cannot run at the time of the
25-
trigger because another task is hogging the CPU. Lines 01 and 03 show the `foo`
26-
and `bar` tasks.
24+
01 shows the fast running `hog_detect` task which cannot run at the time of the
25+
trigger because another task is hogging the CPU. Lines 02 and 04 show the `foo`
26+
and `bar` tasks. Line 03 shows the `hog` task and line 05 is a trigger issued
27+
by `hog()` when it starts monopolising the CPU. The Pico issues the "hog
28+
detect" trigger 100ms after hogging starts.
2729
![Image](./monitor.jpg)
2830

31+
The following image shows brief (<4ms) hogging while `quick_test.py` ran. The
32+
likely cause is garbage collection on the Pyboard D host.
33+
![Image](./monitor_gc.jpg)
34+
2935
### Status
3036

37+
2nd Oct 2021 Add trigger function.
38+
3139
30th Sep 2021 Pico code has improved hog detection.
3240

3341
27th Sep 2021 SPI support added. The `set_uart` method is replaced by
@@ -145,7 +153,9 @@ timer may be adjusted. Other modes of hog detection are also supported. See
145153
In general there are easier ways to debug synchronous code. However in the
146154
context of a monitored asynchronous application there may be a need to view the
147155
timing of synchronous code. Functions and methods may be monitored either in
148-
the declaration via a decorator or when called via a context manager.
156+
the declaration via a decorator or when called via a context manager. Timing
157+
markers may be inserted in code: a call to `monitor.trigger` will cause a Pico
158+
pin to pulse.
149159

150160
## 2.1 The mon_func decorator
151161

@@ -173,6 +183,12 @@ with mon_call(22):
173183
It is advisable not to use the context manager with a function having the
174184
`mon_func` decorator. The pin and report behaviour is confusing.
175185

186+
## 2.3 The trigger timing marker
187+
188+
A call to `monitor.trigger(n)` may be inserted anywhere in synchronous or
189+
asynchronous code. When this runs, a brief (~80μs) pulse will occur on the Pico
190+
pin with ident `n`.
191+
176192
# 3. Pico Pin mapping
177193

178194
The Pico GPIO numbers used by idents start at 3 and have a gap where the Pico

v3/as_demos/monitor/monitor.jpg

38.9 KB
Loading

v3/as_demos/monitor/monitor.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import uasyncio as asyncio
88
from machine import UART, SPI, Pin
9+
from time import sleep_us
910

1011
_write = lambda _ : print('Must run set_device')
1112
_dummy = lambda : None # If UART do nothing.
@@ -121,3 +122,10 @@ def __enter__(self):
121122
def __exit__(self, type, value, traceback):
122123
_write(self.vend)
123124
return False # Don't silence exceptions
125+
126+
# Cause pico ident n to produce a brief (~80μs) pulse
127+
def trigger(n):
128+
_validate(n)
129+
_write(bytes(chr(0x40 + n), 'utf8'))
130+
sleep_us(20)
131+
_write(bytes(chr(0x60 + n), 'utf8'))

v3/as_demos/monitor/monitor_gc.jpg

72.6 KB
Loading

v3/as_demos/monitor/quick_test.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import uasyncio as asyncio
77
import time
88
from machine import Pin, UART, SPI
9-
from monitor import monitor, monitor_init, hog_detect, set_device
9+
from monitor import monitor, monitor_init, hog_detect, set_device, trigger
1010

1111
# Define interface to use
1212
set_device(UART(2, 1_000_000)) # UART must be 1MHz
@@ -20,9 +20,9 @@ async def foo(t, pin):
2020

2121
@monitor(2)
2222
async def hog():
23-
while True:
24-
await asyncio.sleep(5)
25-
time.sleep_ms(500)
23+
await asyncio.sleep(5)
24+
trigger(4) # Hog start
25+
time.sleep_ms(500)
2626

2727
@monitor(3)
2828
async def bar(t):

0 commit comments

Comments
 (0)