1
0
Fork 0

boards: pinetime: Improve safe mode implementation

This commit is contained in:
Daniel Thompson 2020-06-10 08:52:46 +01:00
parent 50f30616c6
commit 12e883e68b

View file

@ -59,41 +59,64 @@ display = ST7789_SPI(240, 240, spi,
res=Pin("DISP_RST", Pin.OUT)) res=Pin("DISP_RST", Pin.OUT))
drawable = draw565.Draw565(display) drawable = draw565.Draw565(display)
# Setup the last few bits and pieces def boot_msg(s):
battery = Battery( drawable.string(s, 0, 108, width=240)
Pin('BATTERY', Pin.IN), if safe_mode:
Signal(Pin('CHARGING', Pin.IN), invert=True), time.sleep_ms(500)
Signal(Pin('USB_PWR', Pin.IN), invert=True))
safe_mode = False
boot_msg("Init button")
button = Pin('BUTTON', Pin.IN) button = Pin('BUTTON', Pin.IN)
i2c = I2C(1, scl='I2C_SCL', sda='I2C_SDA') safe_mode = button.value()
accel = BMA421(i2c) if safe_mode:
touch = CST816S(i2c) backlight.set(1)
vibrator = Vibrator(Pin('MOTOR', Pin.OUT, value=0), active_low=True) time.sleep(1)
# Release flash from deep power-down
nor_cs = Pin('NOR_CS', Pin.OUT, value=1)
nor_cs(0)
spi.write('\xAB')
nor_cs(1)
# Mount the filesystem
flash = FLASH(spi, (Pin('NOR_CS', Pin.OUT, value=1),))
try: try:
os.mount(flash, '/flash') # Setup the last few bits and pieces
except AttributeError: boot_msg("Init hardware")
# Format the filesystem (and provide a default version of main.py) battery = Battery(
os.VfsLfs2.mkfs(flash) Pin('BATTERY', Pin.IN),
os.mount(flash,'/flash') Signal(Pin('CHARGING', Pin.IN), invert=True),
with open('/flash/main.py', 'w') as f: Signal(Pin('USB_PWR', Pin.IN), invert=True))
f.write('''\ i2c = I2C(1, scl='I2C_SCL', sda='I2C_SDA')
accel = BMA421(i2c)
touch = CST816S(i2c)
vibrator = Vibrator(Pin('MOTOR', Pin.OUT, value=0), active_low=True)
# Release flash from deep power-down
boot_msg("Wake SPINOR")
nor_cs = Pin('NOR_CS', Pin.OUT, value=1)
nor_cs(0)
spi.write('\xAB')
nor_cs(1)
# Mount the filesystem
boot_msg("Init SPINOR")
flash = FLASH(spi, (Pin('NOR_CS', Pin.OUT, value=1),))
try:
boot_msg("Mount FS")
os.mount(flash, '/flash')
except AttributeError:
# Format the filesystem (and provide a default version of main.py)
boot_msg("Format FS")
os.VfsLfs2.mkfs(flash)
boot_msg("Retry mount FS")
os.mount(flash,'/flash')
boot_msg("Write main.py")
with open('/flash/main.py', 'w') as f:
f.write('''\
#include('main.py') #include('main.py')
''') ''')
# Only change directory if the button is not pressed (this will # Only change directory if the button is not pressed (this will
# allow us access to fix any problems with main.py)! # allow us access to fix any problems with main.py)!
if not button.value(): if not safe_mode:
os.chdir('/flash') boot_msg("Enter /flash")
os.chdir('/flash')
boot_msg("Run main.py")
else:
boot_msg("Safe mode")
except:
drawable.string("FAILED", 0, 136, width=240)
backlight.set(1) backlight.set(1)
else:
display.poweroff()