Skip to content

Commit ed195ef

Browse files
fri3d_2026: cleanup board initialization
1 parent 298b29f commit ed195ef

2 files changed

Lines changed: 51 additions & 34 deletions

File tree

internal_filesystem/lib/drivers/fri3d/expander.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,3 +118,42 @@ def install_firmware(self, filename: str, progress_cb=None):
118118
progress_cb("waiting for CH32 boot", pct)
119119
time.sleep(4 / progress_margin_end) # wait 4 seconds total
120120
print("Latest CH32 firmware installed.")
121+
122+
def wait_for_normal_mode(self, min_uptime_ms: int = 1000, poll_ms: int = 10):
123+
import time
124+
start = time.ticks_ms()
125+
while time.ticks_diff(time.ticks_ms(), start) < min_uptime_ms:
126+
time.sleep_ms(poll_ms)
127+
128+
def install_firmware_if_needed(
129+
self,
130+
filename: str,
131+
latest_version: tuple[int, int, int],
132+
progress_cb=None,
133+
success_cb=None,
134+
) -> bool:
135+
# Check expander firmware version and if none or too low: install latest
136+
try:
137+
current_version = self.version
138+
print(f"Current_version of CH32 firmware: {current_version}")
139+
except Exception as e:
140+
print("Could not check CH32 firmware version, assuming 0.0.0")
141+
current_version = (0, 0, 0)
142+
if latest_version > current_version:
143+
print(f"CH32 firmware is lower than latest {latest_version} so updating...")
144+
try:
145+
self.install_firmware(filename, progress_cb)
146+
if success_cb:
147+
success_cb()
148+
except Exception as e:
149+
print(f"CH32 firmware install got exception: {e}")
150+
import sys
151+
sys.print_exception(e)
152+
return False
153+
try:
154+
current_version = self.version
155+
print(f"After install, current_version of CH32 firmware: {current_version}")
156+
except Exception as e:
157+
print("Could not check CH32 firmware version after install, many things, including LCD RESET, won't work!")
158+
return True
159+
return False

internal_filesystem/lib/mpos/board/fri3d_2026.py

Lines changed: 12 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -71,51 +71,29 @@
7171
LightsManager.set_led(4, 255, 0, 0)
7272
LightsManager.write()
7373

74-
# CH32 coprocessor / IO expander
75-
from machine import I2C, Pin
76-
expander_i2c = I2C(1, sda=Pin(39), scl=Pin(42), freq=400000)
77-
from drivers.fri3d.expander import Expander
78-
expander = Expander(i2c_bus=expander_i2c)
79-
from mpos import AppearanceManager
80-
8174
# Avoid excessive prints here because it slows down if the serial connects during printing?!
8275
def progress(msg, pct):
8376
#print(f"{msg}: {pct}%")
8477
twentieth = int(pct / 20)
8578
lednr = max(0,4 - twentieth)
8679
#color = (int(pct*2.5), int(255-pct*2.5), abs(128-int(pct*2.5)))
80+
from mpos import AppearanceManager
8781
color = AppearanceManager.percent_to_rainbow_color(pct)
8882
#print(f"setting LED {lednr} color {color}")
8983
LightsManager.set_led(lednr, *color)
9084
LightsManager.write()
9185

92-
# Check expander firmware version and if none or too low: install latest
93-
try:
94-
current_version = expander.version
95-
print(f"Current_version of CH32 firmware: {current_version}")
96-
except Exception as e:
97-
print("Could not check CH32 firmware version, assuming 0.0.0")
98-
current_version = (0, 0, 0)
99-
latest_version = (1, 2, 1)
100-
if latest_version > current_version:
101-
print(f"CH32 firmware is lower than latest {latest_version} so updating...")
102-
try:
103-
expander.install_firmware("/builtin/firmware/fri3d_2026/coprocessor_1.2.1.fw", progress)
104-
LightsManager.set_all(0,255,0)
105-
LightsManager.write()
106-
# Need to reinitialize:
107-
expander_i2c = I2C(sda=Pin(39), scl=Pin(42), freq=400000)
108-
expander = Expander(i2c_bus=expander_i2c)
109-
except Exception as e:
110-
print(f"CH32 firmware install got exception: {e}")
111-
import sys
112-
sys.print_exception(e)
113-
try:
114-
current_version = expander.version
115-
print(f"After install, current_version of CH32 firmware: {current_version}")
116-
except Exception as e:
117-
print("Could not check CH32 firmware version after install, many things, including LCD RESET, won't work!")
118-
86+
# CH32 coprocessor / IO expander
87+
expander_i2c = I2C(1, sda=Pin(39), scl=Pin(42), freq=400000)
88+
from drivers.fri3d.expander import Expander
89+
expander = Expander(i2c_bus=expander_i2c)
90+
expander.wait_for_normal_mode()
91+
if expander.install_firmware_if_needed(
92+
"/builtin/firmware/fri3d_2026/coprocessor_1.2.1.fw", (1, 2, 1), progress_cb=progress,
93+
success_cb=lambda: (LightsManager.set_all(0, 255, 0), LightsManager.write())):
94+
# Reinitialize after firmware install:
95+
expander_i2c = I2C(sda=Pin(39), scl=Pin(42), freq=400000)
96+
expander = Expander(i2c_bus=expander_i2c)
11997

12098
# Quick and dirty patch of BatteryManager to use the CH32 battery level:
12199
def get_voltage(force_refresh=False, raw_adc_value=None):

0 commit comments

Comments
 (0)