Skip to content

Commit 8812447

Browse files
committed
Prior to Damiens low power fix for D series
1 parent eafc976 commit 8812447

File tree

2 files changed

+44
-32
lines changed

2 files changed

+44
-32
lines changed

lowpower/README.md

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
# A low power usayncio adaptation
22

3-
Release 0.11 9th April 2019
3+
Release 0.11 11th April 2019
44

5-
API change: low power applications must now import `rtc_time_cfg` and set its
5+
API changes: low power applications must now import `rtc_time_cfg` and set its
66
`enabled` flag.
7-
This is specific to Pyboards including the D series.
7+
`Latency` class: Constructor requires event loop arg.
8+
9+
This module is specific to Pyboards including the D series.
810

911
1. [Introduction](./README.md#1-introduction)
1012
2. [Installation](./README.md#2-installation)
@@ -252,10 +254,10 @@ Horizontal 500μs/div
252254

253255
### 3.2.4 Pyboard D measurements
254256

255-
As of this release the two demo applications consume around 3.3mA. This is high
256-
because the unused pins are floating. When I discover which pins can be set to
257-
input with pullups as per the Pyboard 1.x implementation I hope to see figures
258-
comparable to Pyboard 1.x.
257+
As of this release the `lpdemo.py` script consumes around 1.1mA. I believe this
258+
can be reduced because some unused pins are floating. When I discover which
259+
pins can be set to input with pullups as per the Pyboard 1.x implementation I
260+
hope to see figures comparable to Pyboard 1.x.
259261

260262
###### [Contents](./README.md#a-low-power-usayncio-adaptation)
261263

@@ -283,12 +285,12 @@ reason is explained in the code comments. It is recommended to use the `utime`
283285
method explicitly if needed.
284286

285287
Latency Class:
286-
* Constructor: Positional arg `t_ms=100`. Period for which the scheduler
287-
enters `stop` i.e. initial latency period.
288-
* Method: `value` Arg `val=None`. Controls period for which scheduler stops.
289-
It returns the period in ms. If the default `None` is passed the value is
290-
unchanged. If 0 is passed the scheduler runs at full speed. A value > 0 sets
291-
the stop period in ms.
288+
* Constructor: Positional args `loop` - the event loop, `t_ms=100` - period
289+
for which the scheduler enters `stop` i.e. initial latency period.
290+
* Method: `value` Arg `val=None`. Controls period for which scheduler
291+
stops. It returns the period in ms prior to any change in value. If the
292+
default `None` is passed the value is unchanged. If 0 is passed the scheduler
293+
runs at full speed. A value > 0 sets the stop period in ms.
292294

293295
The higher the value, the greater the latency experienced by other tasks and
294296
by I/O. Smaller values will result in higher power consumption with other tasks
@@ -298,7 +300,7 @@ The class is a singleton consequently there is no need to pass an instance
298300
around or to make it global. Once instantiated, latency may be changed by
299301

300302
```python
301-
rtc_time.Latency().value(t)
303+
rtc_time.Latency(t)
302304
```
303305

304306
###### [Contents](./README.md#a-low-power-usayncio-adaptation)

lowpower/rtc_time.py

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@
1414
from os import uname
1515
from rtc_time_cfg import enabled
1616
if not enabled:
17-
print('rtc_time module has not been enabled.')
18-
sys.exit(0)
17+
raise ImportError('rtc_time is not enabled.')
18+
19+
# sleep_ms is defined to stop things breaking if someone imports uasyncio.core
20+
# Power won't be saved if this is done.
21+
sleep_ms = utime.sleep_ms
1922

20-
_PERIOD = const(604800000) # ms in 7 days
21-
_PERIOD_2 = const(302400000) # half period
22-
_SS_TO_MS = 1000/256 # Subsecs to ms
2323
d_series = uname().machine[:5] == 'PYBD_'
2424
use_utime = True # Assume the normal utime timebase
2525

@@ -39,25 +39,37 @@
3939
raise OSError('rtc_time.py is Pyboard-specific.')
4040

4141
# For lowest power consumption set unused pins as inputs with pullups.
42-
# Note the 4K7 I2C pullups on X9 X10 Y9 Y10.
42+
# Note the 4K7 I2C pullups on X9 X10 Y9 Y10 (Pyboard 1.x).
4343
if d_series:
4444
print('Running on Pyboard D') # Investigate which pins we can do this to TODO
45-
#for pin in [p for p in dir(pyb.Pin.board) if p[0] in 'XYW']:
46-
#pin_x = pyb.Pin(pin, pyb.Pin.IN, pyb.Pin.PULL_UP)
45+
# pinlist = [p for p in dir(pyb.Pin.board) if p.startswith('W') and p[1].isdigit() and p[-1].isdigit()]
46+
# sorted(pinlist, key=lambda s: int(s[1:]))
47+
#pinlist = ['W3', 'W5', 'W6', 'W7', 'W8', 'W9', 'W10', 'W11', 'W12', 'W14', 'W15',
48+
#'W16', 'W17', 'W18', 'W19', 'W20', 'W22', 'W23', 'W24', 'W25',
49+
#'W26', 'W27', 'W28', 'W29', 'W30', 'W32', 'W33', 'W34', 'W43', 'W45',
50+
#'W46', 'W47', 'W49', 'W50', 'W51', 'W52', 'W53', 'W54', 'W55', 'W56',
51+
#'W57', 'W58', 'W59', 'W60', 'W61', 'W62', 'W63', 'W64', 'W65', 'W66',
52+
#'W67', 'W68', 'W70', 'W71', 'W72', 'W73', 'W74']
53+
# sorted([p for p in dir(pyb.Pin.board) if p[0] in 'XY' and p[-1].isdigit()], key=lambda x: int(x[1:]) if x[0]=='X' else int(x[1:])+100)
54+
pinlist = ['X1', 'X2', 'X3', 'X4', 'X5', 'X6', 'X7', 'X8', 'X9', 'X10', 'X11', 'X12',
55+
'Y3', 'Y4', 'Y5', 'Y6', 'Y7', 'Y8', 'Y9', 'Y10', 'Y11', 'Y12']
56+
for pin in pinlist:
57+
pin_x = pyb.Pin(pin, pyb.Pin.IN, pyb.Pin.PULL_UP)
58+
pyb.Pin('EN_3V3').off()
4759
else:
4860
print('Running on Pyboard 1.x')
4961
for pin in [p for p in dir(pyb.Pin.board) if p[0] in 'XY']:
5062
pin_x = pyb.Pin(pin, pyb.Pin.IN, pyb.Pin.PULL_UP)
5163
# User code redefines any pins in use
5264

53-
# sleep_ms is defined to stop things breaking if someone imports uasyncio.core
54-
# Power won't be saved if this is done.
55-
sleep_ms = utime.sleep_ms
56-
if use_utime: # Run utime: Pyboard connected to PC via USB or alien platform
65+
if use_utime:
5766
ticks_ms = utime.ticks_ms
5867
ticks_add = utime.ticks_add
5968
ticks_diff = utime.ticks_diff
60-
else:
69+
else: # All conditions met for low power operation
70+
_PERIOD = const(604800000) # ms in 7 days
71+
_PERIOD_2 = const(302400000) # half period
72+
_SS_TO_MS = 1000/256 # Subsecs to ms
6173
rtc = pyb.RTC()
6274
# dt: (year, month, day, weekday, hours, minutes, seconds, subseconds)
6375
# weekday is 1-7 for Monday through Sunday.
@@ -82,9 +94,6 @@ def ticks_diff(end, start):
8294

8395
import uasyncio as asyncio
8496

85-
# Common version has a needless dict: https://www.python.org/dev/peps/pep-0318/#examples
86-
# https://stackoverflow.com/questions/6760685/creating-a-singleton-in-python
87-
# Resolved: https://forum.micropython.org/viewtopic.php?f=2&t=5033&p=28824#p28824
8897
def singleton(cls):
8998
instance = None
9099
def getinstance(*args, **kwargs):
@@ -124,6 +133,7 @@ def _run(self):
124133
rtc.wakeup(None)
125134

126135
def value(self, val=None):
127-
if val is not None and not use_utime:
136+
v = self._t_ms
137+
if val is not None:
128138
self._t_ms = max(val, 0)
129-
return self._t_ms
139+
return v

0 commit comments

Comments
 (0)