Skip to content

Commit 40f5ecd

Browse files
committed
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.
1 parent 2f59352 commit 40f5ecd

3 files changed

Lines changed: 72 additions & 0 deletions

File tree

esp8266/scripts/_boot.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import uos
2+
import builtins
3+
from flashbdev import bdev
4+
5+
try:
6+
vfs = uos.VfsFat(bdev, "/")
7+
except OSError:
8+
import inisetup
9+
inisetup.check_bootsec()
10+
uos.VfsFat.mkfs(bdev)
11+
vfs = uos.VfsFat(bdev, "/")
12+
inisetup.wifi()

esp8266/scripts/flashbdev.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import esp
2+
3+
class FlashBdev:
4+
5+
SEC_SIZE = 4096
6+
START_SEC = 0xa0000 // SEC_SIZE
7+
NUM_BLK = 64
8+
9+
def __init__(self, blocks=NUM_BLK):
10+
self.blocks = blocks
11+
12+
def readblocks(self, n, buf):
13+
print("readblocks(%s, %x(%d))" % (n, id(buf), len(buf)))
14+
esp.flash_read((n + self.START_SEC) * self.SEC_SIZE, buf)
15+
16+
def writeblocks(self, n, buf):
17+
print("writeblocks(%s, %x(%d))" % (n, id(buf), len(buf)))
18+
assert len(buf) <= self.SEC_SIZE, len(buf)
19+
esp.flash_erase(n + self.START_SEC)
20+
esp.flash_write((n + self.START_SEC) * self.SEC_SIZE, buf)
21+
22+
def ioctl(self, op, arg):
23+
print("ioctl(%d, %r)" % (op, arg))
24+
if op == 4: # BP_IOCTL_SEC_COUNT
25+
return self.blocks
26+
if op == 5: # BP_IOCTL_SEC_SIZE
27+
return self.SEC_SIZE
28+
29+
bdev = FlashBdev()

esp8266/scripts/inisetup.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import uos
2+
import network
3+
from flashbdev import bdev
4+
5+
def wifi():
6+
import ubinascii
7+
ap_if = network.WLAN(network.AP_IF)
8+
ap_if.config(essid=b"MicroPython %s" % ubinascii.hexlify(ap_if.mac()[-3:]))
9+
10+
def check_bootsec():
11+
buf = bytearray(bdev.SEC_SIZE)
12+
bdev.readblocks(0, buf)
13+
empty = True
14+
for b in buf:
15+
if b != 0xff:
16+
empty = False
17+
break
18+
if empty:
19+
return True
20+
fs_corrupted()
21+
22+
def fs_corrupted():
23+
import time
24+
while 1:
25+
print("""\
26+
FAT filesystem appears to be corrupted. If you had important data there, you
27+
may want to make a flash snapshot to try to recover it. Otherwise, perform
28+
factory reprogramming of MicroPython firmware (completely erase flash, followed
29+
by firmware programming).
30+
""")
31+
time.sleep(3)

0 commit comments

Comments
 (0)