|
1 | | -# boot.py -- run on boot-up |
2 | | -# can run arbitrary Python, but best to keep it minimal |
3 | | -import pyb, os, micropython, time |
4 | | -import sys |
5 | | - |
6 | | -# Clean sys.path from qspi |
7 | | -# Shouldn't happen in production, but just in case. |
8 | | -for p in sys.path: |
9 | | - if "qspi" in sys.path: |
10 | | - sys.path.remove(p) |
11 | | - |
12 | | -# power hold |
13 | | -pwr = pyb.Pin("B15", pyb.Pin.OUT) |
14 | | -pwr.on() |
15 | | - |
16 | | -version = "<version:tag10>0100900099</version:tag10>" |
17 | | - |
18 | | -# get i2c |
19 | | -i2c = pyb.I2C(1) |
20 | | -i2c.init() |
21 | | -# start measurements |
22 | | -if 112 in i2c.scan(): |
23 | | - i2c.mem_write(0b00010000, 112, 0) |
24 | | - |
25 | | -leds = [pyb.LED(i) for i in range(1,5)] |
26 | | -# poweroff on button press |
27 | | -def pwrcb(e): |
28 | | - micropython.schedule(poweroff, 0) |
29 | | - |
30 | | -# callback scheduled from the interrupt |
31 | | -def poweroff(_): |
32 | | - # make sure it disables power no matter what |
33 | | - try: |
34 | | - for led in leds: |
35 | | - led.toggle() |
36 | | - # stop battery manangement |
37 | | - if 112 in i2c.scan(): |
38 | | - i2c.mem_write(0, 112, 0) |
39 | | - # sync filesystem |
40 | | - os.sync() |
41 | | - time.sleep_ms(300) |
42 | | - finally: |
43 | | - # disable power |
44 | | - pwr.off() |
45 | | - time.sleep_ms(300) |
46 | | - # will never reach here |
47 | | - for led in leds: |
48 | | - led.toggle() |
49 | | - |
50 | | -pyb.ExtInt(pyb.Pin('B1'), pyb.ExtInt.IRQ_FALLING, pyb.Pin.PULL_NONE, pwrcb) |
51 | | - |
52 | | -# configure usb from start if you want, |
53 | | -# otherwise will be configured after PIN |
54 | | -# pyb.usb_mode("VCP+MSC") # debug mode with USB and mounted storages from start |
55 | | -# pyb.usb_mode("VCP") # debug mode with USB from start |
56 | | -# disable at start |
57 | | -pyb.usb_mode(None) |
58 | | -os.dupterm(None,0) |
59 | | -os.dupterm(None,1) |
60 | | - |
61 | | -# inject version and i2c to platform module |
| 1 | +# boot.py -- run on boot-up |
| 2 | +# can run arbitrary Python, but best to keep it minimal |
| 3 | +import pyb, os, micropython, time |
| 4 | +import sys |
| 5 | + |
| 6 | +# Clean sys.path from qspi |
| 7 | +# Shouldn't happen in production, but just in case. |
| 8 | +for p in sys.path: |
| 9 | + if "qspi" in sys.path: |
| 10 | + sys.path.remove(p) |
| 11 | + |
| 12 | +# power hold |
| 13 | +pwr = pyb.Pin("B15", pyb.Pin.OUT) |
| 14 | +pwr.on() |
| 15 | + |
| 16 | +version = "<version:tag10>0101000099</version:tag10>" |
| 17 | + |
| 18 | +# get i2c |
| 19 | +i2c = pyb.I2C(1) |
| 20 | +i2c.init() |
| 21 | +# start measurements |
| 22 | +if 112 in i2c.scan(): |
| 23 | + i2c.mem_write(0b00010000, 112, 0) |
| 24 | + |
| 25 | +leds = [pyb.LED(i) for i in range(1,5)] |
| 26 | +# poweroff on button press |
| 27 | +def pwrcb(e): |
| 28 | + micropython.schedule(poweroff, 0) |
| 29 | + |
| 30 | +# callback scheduled from the interrupt |
| 31 | +def poweroff(_): |
| 32 | + # make sure it disables power no matter what |
| 33 | + try: |
| 34 | + for led in leds: |
| 35 | + led.toggle() |
| 36 | + # stop battery manangement |
| 37 | + if 112 in i2c.scan(): |
| 38 | + i2c.mem_write(0, 112, 0) |
| 39 | + # sync filesystem |
| 40 | + os.sync() |
| 41 | + time.sleep_ms(300) |
| 42 | + finally: |
| 43 | + # disable power |
| 44 | + pwr.off() |
| 45 | + time.sleep_ms(300) |
| 46 | + # will never reach here |
| 47 | + for led in leds: |
| 48 | + led.toggle() |
| 49 | + |
| 50 | +pyb.ExtInt(pyb.Pin('B1'), pyb.ExtInt.IRQ_FALLING, pyb.Pin.PULL_NONE, pwrcb) |
| 51 | + |
| 52 | +# configure usb from start if you want, |
| 53 | +# otherwise will be configured after PIN |
| 54 | +# pyb.usb_mode("VCP+MSC") # debug mode with USB and mounted storages from start |
| 55 | +# pyb.usb_mode("VCP") # debug mode with USB from start |
| 56 | +# disable at start |
| 57 | +pyb.usb_mode(None) |
| 58 | +os.dupterm(None,0) |
| 59 | +os.dupterm(None,1) |
| 60 | + |
| 61 | +# inject version and i2c to platform module |
62 | 62 | import platform |
63 | 63 | platform.version = version |
64 | 64 | platform.i2c = i2c |
|
0 commit comments