Skip to content

Commit eafc976

Browse files
committed
lowpower: improve USB detection and README.md
1 parent 42c18c9 commit eafc976

File tree

2 files changed

+22
-10
lines changed

2 files changed

+22
-10
lines changed

lowpower/README.md

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,14 @@ before yielding with a zero delay. The duration of the `stop` condition
139139
full speed. The `yield` allows each pending task to run once before the
140140
scheduler is again paused (if `latency` > 0).
141141

142+
The line
143+
```python
144+
rtc_time_cfg.enabled = True
145+
```
146+
must be issued before importing `uasyncio` and before importing any modules
147+
which use it, otherwise low-power mode will not be engaged. It is wise to do
148+
this at the start of application code.
149+
142150
###### [Contents](./README.md#a-low-power-usayncio-adaptation)
143151

144152
### 3.2.1 Consequences of stop mode
@@ -147,15 +155,15 @@ scheduler is again paused (if `latency` > 0).
147155

148156
A minor limitation is that the Pyboard 1.x RTC cannot resolve times of less
149157
than 4ms so there is a theoretical reduction in the accuracy of delays. This
150-
does not apply to the Pyboard D. In practice this is somewhat academic. As
151-
explained in the [tutorial](../TUTORIAL.md), issuing
158+
does not apply to the Pyboard D. This is somewhat academic. As explained in the
159+
[tutorial](../TUTORIAL.md), issuing
152160

153161
```python
154162
await asyncio.sleep_ms(t)
155163
```
156164

157165
specifies a minimum delay: the maximum may be substantially higher depending on
158-
the behaviour of other tasks.
166+
the behaviour of other tasks. Also the `latency` value will be added to `t`.
159167

160168
RTC time rollover is at 7 days. The maximum supported `asyncio.sleep()` value
161169
is 302399999 seconds (3.5 days - 1s).
@@ -166,7 +174,14 @@ Programs using `pyb.stop` disable the USB connection to the PC. This is
166174
inconvenient for debugging so `rtc_time.py` detects an active USB connection
167175
and disables power saving. This enables an application to be developed normally
168176
via a USB connected PC. The board can then be disconnected from the PC and run
169-
from a separate power source for power measurements.
177+
from a separate power source for power measurements, the application being
178+
started by `main.py`.
179+
180+
An active USB connection is one where a PC application is accessing the port:
181+
an unused port can power the Pyboard and the library will assume low-power
182+
mode. If the Pyboard is booted in safe mode to bypass `main.py` and the
183+
application is started at the REPL, USB detection will disable low power mode
184+
to keep the connection open.
170185

171186
Applications can detect which timebase is in use by issuing:
172187

lowpower/rtc_time.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# rtc_time.py Pyboard-only RTC based timing for low power uasyncio
22
# Author: Peter Hinch
3-
# Copyright Peter Hinch 2018 Released under the MIT license
3+
# Copyright Peter Hinch 2018-2019 Released under the MIT license
44

55
# Code based on extmod/utime_mphal.c
66
# millisecs roll over on 7 days rather than 12.42757 days
@@ -29,11 +29,8 @@
2929
if mode is None: # USB is disabled
3030
use_utime = False # use RTC timebase
3131
elif 'VCP' in mode: # User has enabled VCP in boot.py
32-
if d_series: # Detect an active connection to the PC
33-
usb_conn = pyb.USB_VCP().isconnected()
34-
else:
35-
usb_conn = pyb.Pin.board.USB_VBUS.value() # USB physically connected to pyb V1.x
36-
if usb_conn:
32+
# Detect an active connection (not just a power source)
33+
if pyb.USB_VCP().isconnected(): # USB will work normally
3734
print('USB connection: rtc_time disabled.')
3835
else:
3936
pyb.usb_mode(None) # Save power

0 commit comments

Comments
 (0)