Skip to content

Commit 28ecbe0

Browse files
committed
esp32: Add scripts to init and mount filesystem.
1 parent abe39f5 commit 28ecbe0

File tree

4 files changed

+89
-0
lines changed

4 files changed

+89
-0
lines changed

esp32/main.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,13 @@ void mp_task(void *pvParameter) {
6666
mp_obj_list_init(mp_sys_argv, 0);
6767
readline_init0();
6868

69+
// run boot-up scripts
70+
pyexec_frozen_module("_boot.py");
71+
pyexec_file("boot.py");
72+
if (pyexec_mode_kind == PYEXEC_MODE_FRIENDLY_REPL) {
73+
pyexec_file("main.py");
74+
}
75+
6976
for (;;) {
7077
if (pyexec_mode_kind == PYEXEC_MODE_RAW_REPL) {
7178
if (pyexec_raw_repl() != 0) {

esp32/modules/_boot.py

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

esp32/modules/flashbdev.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import esp
2+
3+
class FlashBdev:
4+
5+
SEC_SIZE = 4096
6+
START_SEC = esp.flash_user_start() // SEC_SIZE
7+
8+
def __init__(self, blocks):
9+
self.blocks = blocks
10+
11+
def readblocks(self, n, buf):
12+
#print("readblocks(%s, %x(%d))" % (n, id(buf), len(buf)))
13+
esp.flash_read((n + self.START_SEC) * self.SEC_SIZE, buf)
14+
15+
def writeblocks(self, n, buf):
16+
#print("writeblocks(%s, %x(%d))" % (n, id(buf), len(buf)))
17+
#assert len(buf) <= self.SEC_SIZE, len(buf)
18+
esp.flash_erase(n + self.START_SEC)
19+
esp.flash_write((n + self.START_SEC) * self.SEC_SIZE, buf)
20+
21+
def ioctl(self, op, arg):
22+
#print("ioctl(%d, %r)" % (op, arg))
23+
if op == 4: # BP_IOCTL_SEC_COUNT
24+
return self.blocks
25+
if op == 5: # BP_IOCTL_SEC_SIZE
26+
return self.SEC_SIZE
27+
28+
size = esp.flash_size()
29+
if size < 1024*1024:
30+
# flash too small for a filesystem
31+
bdev = None
32+
else:
33+
# for now we use a fixed size for the filesystem
34+
bdev = FlashBdev(256 * 1024 // FlashBdev.SEC_SIZE)

esp32/modules/inisetup.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import uos
2+
from flashbdev import bdev
3+
4+
def check_bootsec():
5+
buf = bytearray(bdev.SEC_SIZE)
6+
bdev.readblocks(0, buf)
7+
empty = True
8+
for b in buf:
9+
if b != 0xff:
10+
empty = False
11+
break
12+
if empty:
13+
return True
14+
fs_corrupted()
15+
16+
def fs_corrupted():
17+
import time
18+
while 1:
19+
print("""\
20+
FAT filesystem appears to be corrupted. If you had important data there, you
21+
may want to make a flash snapshot to try to recover it. Otherwise, perform
22+
factory reprogramming of MicroPython firmware (completely erase flash, followed
23+
by firmware programming).
24+
""")
25+
time.sleep(3)
26+
27+
def setup():
28+
check_bootsec()
29+
print("Performing initial setup")
30+
uos.VfsFat.mkfs(bdev)
31+
vfs = uos.VfsFat(bdev, "")
32+
with open("/boot.py", "w") as f:
33+
f.write("""\
34+
# This file is executed on every boot (including wake-boot from deepsleep)
35+
""")
36+
return vfs

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy