Created
March 25, 2015 21:44
-
-
Save dpgeorge/bf477eb883b6d189eae9 to your computer and use it in GitHub Desktop.
Low power test script for pyboard
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# cycle through standby, stop, wfi, run modes | |
# uses either rtc wakeup or switch extint | |
# put pyb.usb_mode(None) in boot.py to save more power | |
# can also select lower CPU freq using, eg, pyb.freq(64000000) | |
import pyb, stm | |
from pyb import Pin | |
def low_power_test(): | |
rtc = pyb.RTC() | |
led1 = pyb.LED(1) | |
led2 = pyb.LED(2) | |
led3 = pyb.LED(3) | |
led4 = pyb.LED(4) | |
if hasattr(Pin.board, 'X1'): | |
# pyboard | |
pins = [ | |
(Pin.board.X1, Pin.PULL_UP), | |
(Pin.board.X2, Pin.PULL_UP), | |
(Pin.board.X3, Pin.PULL_UP), | |
(Pin.board.X4, Pin.PULL_UP), | |
(Pin.board.X5, Pin.PULL_UP), | |
(Pin.board.X6, Pin.PULL_UP), | |
(Pin.board.X7, Pin.PULL_UP), | |
(Pin.board.X8, Pin.PULL_UP), | |
(Pin.board.X9, Pin.PULL_NONE), # I2C SCL | |
(Pin.board.X10, Pin.PULL_NONE), # I2C SDA | |
(Pin.board.X11, Pin.PULL_UP), | |
(Pin.board.X12, Pin.PULL_UP), | |
(Pin.board.Y1, Pin.PULL_UP), | |
(Pin.board.Y2, Pin.PULL_UP), | |
(Pin.board.Y3, Pin.PULL_UP), | |
(Pin.board.Y4, Pin.PULL_UP), | |
(Pin.board.Y5, Pin.PULL_UP), | |
(Pin.board.Y6, Pin.PULL_UP), | |
(Pin.board.Y7, Pin.PULL_UP), | |
(Pin.board.Y8, Pin.PULL_UP), | |
(Pin.board.Y9, Pin.PULL_NONE), # I2C SCL | |
(Pin.board.Y10, Pin.PULL_NONE), # I2C SDA | |
(Pin.board.Y11, Pin.PULL_UP), | |
(Pin.board.Y12, Pin.PULL_UP), | |
#(Pin.board.X17, Pin.PULL_NONE), # USR SW already configured | |
(Pin.board.X18, Pin.PULL_UP), | |
(Pin.board.X19, Pin.PULL_UP), | |
(Pin.board.X20, Pin.PULL_UP), | |
(Pin.board.X21, Pin.PULL_UP), | |
(Pin.board.X22, Pin.PULL_UP), | |
] | |
if hasattr(Pin.board, 'MMA_AVDD'): | |
pins += [ | |
(Pin.board.MMA_AVDD, Pin.PULL_DOWN), | |
(Pin.board.MMA_INT, Pin.PULL_NONE), # BOOT1 pulled low by 100k | |
] | |
if hasattr(Pin.board, 'SD_D0'): | |
pins += [ | |
(Pin.board.SD_D0, Pin.PULL_UP), | |
(Pin.board.SD_D1, Pin.PULL_UP), | |
(Pin.board.SD_D2, Pin.PULL_UP), | |
(Pin.board.SD_D3, Pin.PULL_UP), | |
(Pin.board.SD_CMD, Pin.PULL_UP), | |
(Pin.board.SD_CK, Pin.PULL_UP), | |
#(Pin.board.SD_SW, Pin.PULL_NONE), # already configured | |
] | |
if hasattr(Pin.board, 'USB_VBUS'): | |
pins += [ | |
(Pin.board.USB_VBUS, Pin.PULL_UP), | |
(Pin.board.USB_DM, Pin.PULL_UP), | |
(Pin.board.USB_DP, Pin.PULL_UP), | |
] | |
if hasattr(Pin.board, 'USB_ID'): | |
pins += [(Pin.board.USB_ID, Pin.PULL_UP)] | |
# configure pins as input to reduce current leakage | |
for pin, pull in pins: | |
pin.init(Pin.IN, pull) | |
# function to turn LED pins on/off | |
led_pins = [Pin(p) for p in ("LED_RED", "LED_GREEN", "LED_YELLOW", "LED_BLUE") | |
if hasattr(Pin.board, p)] | |
def led_off(): | |
for pin in led_pins: | |
pin.init(Pin.IN, Pin.PULL_DOWN) | |
def led_on(): | |
for pin in led_pins: | |
pin.init(Pin.OUT_PP, Pin.PULL_NONE) | |
if hasattr(Pin.board, "LED_BLUE"): | |
Pin.board.LED_BLUE.init(Pin.AF_PP, Pin.PULL_NONE, af=Pin.AF2_TIM3) | |
# wakeup callback | |
wakeup = False | |
def cb(exti): | |
nonlocal wakeup | |
wakeup = True | |
# configure switch to generate interrupt on press | |
sw = pyb.Switch() | |
sw.callback(lambda:cb(0)) | |
# function to flash an LED | |
def flash(led): | |
led.on() | |
pyb.delay(100) | |
led.off() | |
while True: | |
# standby (need to exit by pressing RST, or wait 15s) | |
if stm.mem32[stm.RTC + stm.RTC_BKP1R] == 0: | |
flash(led1) | |
stm.mem32[stm.RTC + stm.RTC_BKP1R] = 1 | |
rtc.wakeup(15000, cb) | |
pyb.standby() | |
else: | |
stm.mem32[stm.RTC + stm.RTC_BKP1R] = 0 | |
# stop | |
flash(led2) | |
led_off() | |
pyb.stop() | |
led_on() | |
# idle | |
flash(led3) | |
wakeup = False | |
while not wakeup: | |
pyb.wfi() | |
# run | |
flash(led4) | |
wakeup = False | |
while not wakeup: | |
pass | |
low_power_test() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment