1
0
Fork 0

boards: pinetime: Improve safe mode implementation

pull/38/head
Daniel Thompson 2020-06-10 08:52:46 +01:00
parent 50f30616c6
commit 12e883e68b
1 changed files with 54 additions and 31 deletions

View File

@ -59,41 +59,64 @@ display = ST7789_SPI(240, 240, spi,
res=Pin("DISP_RST", Pin.OUT))
drawable = draw565.Draw565(display)
# Setup the last few bits and pieces
battery = Battery(
Pin('BATTERY', Pin.IN),
Signal(Pin('CHARGING', Pin.IN), invert=True),
Signal(Pin('USB_PWR', Pin.IN), invert=True))
def boot_msg(s):
drawable.string(s, 0, 108, width=240)
if safe_mode:
time.sleep_ms(500)
safe_mode = False
boot_msg("Init button")
button = Pin('BUTTON', Pin.IN)
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)
safe_mode = button.value()
if safe_mode:
backlight.set(1)
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:
os.mount(flash, '/flash')
except AttributeError:
# Format the filesystem (and provide a default version of main.py)
os.VfsLfs2.mkfs(flash)
os.mount(flash,'/flash')
with open('/flash/main.py', 'w') as f:
f.write('''\
# Setup the last few bits and pieces
boot_msg("Init hardware")
battery = Battery(
Pin('BATTERY', Pin.IN),
Signal(Pin('CHARGING', Pin.IN), invert=True),
Signal(Pin('USB_PWR', Pin.IN), invert=True))
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')
''')
# Only change directory if the button is not pressed (this will
# allow us access to fix any problems with main.py)!
if not button.value():
os.chdir('/flash')
# Only change directory if the button is not pressed (this will
# allow us access to fix any problems with main.py)!
if not safe_mode:
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)
else:
display.poweroff()