From 989d08407525e40a8b643bb18182c05133e8d421 Mon Sep 17 00:00:00 2001 From: "Peter D. Gray" Date: Mon, 19 Feb 2024 09:43:42 -0500 Subject: [PATCH] support slot B in wipe_microsd_card --- shared/files.py | 65 ++++++++++++++++++++++++++++++--------------- unix/variant/os.py | 6 +++++ unix/variant/pyb.py | 8 ++++++ 3 files changed, 57 insertions(+), 22 deletions(-) diff --git a/shared/files.py b/shared/files.py index e3e5de00..f412ecab 100644 --- a/shared/files.py +++ b/shared/files.py @@ -89,43 +89,64 @@ def wipe_microsd_card(): # Erase and re-format SD card. Not secure erase, because that is too slow. import ckcc, pyb from glob import dis + from version import num_sd_slots + + if not CardSlot.is_inserted(): + return try: + # just in case os.umount('/sd') except: pass - sd = pyb.SDCard() - assert sd + if num_sd_slots == 2: + # pick slot with a card or default A if both installed + slot_b = (CardSlot.sd_detect2() == 0) + if CardSlot.sd_detect() == 0: + slot_b = False + CardSlot.mux(1 if slot_b else 0) # top slot = A + active_led = CardSlot.active_led2 if slot_b else CardSlot.active_led1 + else: + active_led = CardSlot.active_led + slot_b = False - if not sd.present(): + try: + active_led.on() - return + sd = pyb.SDCard() + assert sd - # power cycle so card details (like size) are re-read from current card - sd.power(0) - sd.power(1) + # power cycle so card details (like size) are re-read from current card + sd.power(0) + sd.power(1) - dis.fullscreen('Part Erase...') - cutoff = 1024 # arbitrary - blk = bytearray(512) - - for bnum in range(cutoff): + dis.fullscreen('Part Erase...') + cutoff = 1024 # arbitrary + blk = bytearray(512) ckcc.rng_bytes(blk) - sd.writeblocks(bnum, blk) - dis.progress_bar_show(bnum/cutoff) - dis.fullscreen('Formatting...') + for bnum in range(cutoff): + sd.writeblocks(bnum, blk) + dis.progress_bar_show(bnum/cutoff) - # remount, with newfs option - os.mount(sd, '/sd', readonly=0, mkfs=1) + dis.fullscreen('Formatting...') - # done, cleanup - os.umount('/sd') + # remount, with newfs option -- this does the formating (very quick) + os.mount(sd, '/sd', readonly=0, mkfs=1) - # important: turn off power - sd = pyb.SDCard() - sd.power(0) + # done, cleanup + os.umount('/sd') + finally: + active_led.off() + + # important: turn off power + sd = pyb.SDCard() + sd.power(0) + + if slot_b: + # optional? + CardSlot.mux(0) # top slot = A def dfu_parse(fd): # do just a little parsing of DFU headers, to find start/length of main binary diff --git a/unix/variant/os.py b/unix/variant/os.py index afa3e626..c8481fe5 100644 --- a/unix/variant/os.py +++ b/unix/variant/os.py @@ -26,4 +26,10 @@ def listdir(path="."): return res +# so wipe_microsd_card() can pretend to work +def mount(*a, **kws): + return +def umount(*a): + return + # EOF diff --git a/unix/variant/pyb.py b/unix/variant/pyb.py index 54f616c6..4c06ad67 100644 --- a/unix/variant/pyb.py +++ b/unix/variant/pyb.py @@ -113,6 +113,8 @@ class SDCard: @classmethod def present(cls): + # after Q, this should not really be called anymore + raise RuntimeError("avoid sd.present") if cls.ejected: return False SDCard.power(1) @@ -120,6 +122,7 @@ class SDCard: @classmethod def power(cls, st=0): + # on real hardware, this resets the sdcard h/w module, which is important if st: time.sleep(0.100) # drama return False @@ -131,6 +134,11 @@ class SDCard: b'2\x00^\x00\xd6\x81Y[\x8f\xff\xb7\xed\x94\x00@\x16', b'APA\tDU F\xd9\x92\x11\x10\x9a\x1a\x01\xdf') + # so wipe_microsd_card() can pretend to work + @classmethod + def writeblocks(*a): + pass + class ExtInt: def __init__(self, *a, **kw):