summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Sokolovsky <pfalcon@users.sourceforge.net>2016-04-12 00:37:04 +0300
committerPaul Sokolovsky <pfalcon@users.sourceforge.net>2016-04-12 00:37:04 +0300
commit40f5ecd3a82e810203b39131cb61474f101de58e (patch)
tree765543f380ff64472af0fd7262f11c7fca5d112b
parent2f5935269b7c1240bd5a8835b5d53ab0fb3a2058 (diff)
esp8266: Add Python modules for initial configuration.
Main entry point is _boot.py which checks whether FAT FS in flash mountable, and if so, mounts it. Otherwise, it checks if flash is empty, and if so, performs initial module setup: makes FAT FS, configures default AP name, etc. As a last option, if flash is not empty, and could not be mounted, it means filesystem corruption, and warning message with instructions is printed in an infinite loop.
-rw-r--r--esp8266/scripts/_boot.py12
-rw-r--r--esp8266/scripts/flashbdev.py29
-rw-r--r--esp8266/scripts/inisetup.py31
3 files changed, 72 insertions, 0 deletions
diff --git a/esp8266/scripts/_boot.py b/esp8266/scripts/_boot.py
new file mode 100644
index 000000000..104034fb2
--- /dev/null
+++ b/esp8266/scripts/_boot.py
@@ -0,0 +1,12 @@
+import uos
+import builtins
+from flashbdev import bdev
+
+try:
+ vfs = uos.VfsFat(bdev, "/")
+except OSError:
+ import inisetup
+ inisetup.check_bootsec()
+ uos.VfsFat.mkfs(bdev)
+ vfs = uos.VfsFat(bdev, "/")
+ inisetup.wifi()
diff --git a/esp8266/scripts/flashbdev.py b/esp8266/scripts/flashbdev.py
new file mode 100644
index 000000000..1d120b920
--- /dev/null
+++ b/esp8266/scripts/flashbdev.py
@@ -0,0 +1,29 @@
+import esp
+
+class FlashBdev:
+
+ SEC_SIZE = 4096
+ START_SEC = 0xa0000 // SEC_SIZE
+ NUM_BLK = 64
+
+ def __init__(self, blocks=NUM_BLK):
+ self.blocks = blocks
+
+ def readblocks(self, n, buf):
+ print("readblocks(%s, %x(%d))" % (n, id(buf), len(buf)))
+ esp.flash_read((n + self.START_SEC) * self.SEC_SIZE, buf)
+
+ def writeblocks(self, n, buf):
+ print("writeblocks(%s, %x(%d))" % (n, id(buf), len(buf)))
+ assert len(buf) <= self.SEC_SIZE, len(buf)
+ esp.flash_erase(n + self.START_SEC)
+ esp.flash_write((n + self.START_SEC) * self.SEC_SIZE, buf)
+
+ def ioctl(self, op, arg):
+ print("ioctl(%d, %r)" % (op, arg))
+ if op == 4: # BP_IOCTL_SEC_COUNT
+ return self.blocks
+ if op == 5: # BP_IOCTL_SEC_SIZE
+ return self.SEC_SIZE
+
+bdev = FlashBdev()
diff --git a/esp8266/scripts/inisetup.py b/esp8266/scripts/inisetup.py
new file mode 100644
index 000000000..7a5be607e
--- /dev/null
+++ b/esp8266/scripts/inisetup.py
@@ -0,0 +1,31 @@
+import uos
+import network
+from flashbdev import bdev
+
+def wifi():
+ import ubinascii
+ ap_if = network.WLAN(network.AP_IF)
+ ap_if.config(essid=b"MicroPython %s" % ubinascii.hexlify(ap_if.mac()[-3:]))
+
+def check_bootsec():
+ buf = bytearray(bdev.SEC_SIZE)
+ bdev.readblocks(0, buf)
+ empty = True
+ for b in buf:
+ if b != 0xff:
+ empty = False
+ break
+ if empty:
+ return True
+ fs_corrupted()
+
+def fs_corrupted():
+ import time
+ while 1:
+ print("""\
+FAT filesystem appears to be corrupted. If you had important data there, you
+may want to make a flash snapshot to try to recover it. Otherwise, perform
+factory reprogramming of MicroPython firmware (completely erase flash, followed
+by firmware programming).
+""")
+ time.sleep(3)