From 12e883e68b1445cc60c61535333d460cb551ac9a Mon Sep 17 00:00:00 2001 From: Daniel Thompson Date: Wed, 10 Jun 2020 08:52:46 +0100 Subject: [PATCH] boards: pinetime: Improve safe mode implementation --- wasp/boards/pinetime/watch.py.in | 85 ++++++++++++++++++++------------ 1 file changed, 54 insertions(+), 31 deletions(-) diff --git a/wasp/boards/pinetime/watch.py.in b/wasp/boards/pinetime/watch.py.in index d064d8f..ef7d0f4 100644 --- a/wasp/boards/pinetime/watch.py.in +++ b/wasp/boards/pinetime/watch.py.in @@ -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() -