@@ -89,7 +89,7 @@ def config(self, value: int):
8989 if value >= 0 and value <= 0xFF :
9090 self ._write (_EXPANDER_REG_CONFIG , struct .pack ("B" , value ))
9191
92- def install_firmware (self , filename : str , progress_cb = None ):
92+ def install_firmware (self , filename : str , progress_cb ):
9393 print ("Installing latest CH32 firmware" )
9494 time .sleep_ms (10 ) # make sure writes are spaced out to workaround Fri3dCamp/badge_2026_fw/issues/2
9595 self .config = 0x0B # trigger SWD enable
@@ -115,8 +115,7 @@ def install_firmware(self, filename: str, progress_cb=None):
115115 else :
116116 prog .x03x_program (fw , None ) # throws exception if it fails
117117 for pct in range (100 - progress_margin_end , 101 ):
118- if progress_cb :
119- progress_cb ("waiting for CH32 boot" , pct )
118+ progress_cb ("waiting for CH32 boot" , pct )
120119 time .sleep (4 / progress_margin_end ) # wait 4 seconds total
121120 print ("Latest CH32 firmware installed." )
122121
@@ -125,38 +124,32 @@ def wait_for_normal_mode(self, min_uptime_ms: int = 1000, poll_ms: int = 10):
125124 while time .ticks_diff (time .ticks_ms (), start ) < min_uptime_ms :
126125 time .sleep_ms (poll_ms )
127126
127+ # Returns True if expander_i2c needs to be re-initialized
128128 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- warning_cb = None ,
135- failure_cb = None
136- ) -> bool : # Returns True if expander_i2c needs to be re-initialized
129+ self , filename : str , latest_version : tuple [int , int , int ],
130+ progress_cb , success_cb , warning_cb , failure_cb ) -> bool :
137131 # Check expander firmware version and if none or too low: install latest
138132 try :
139- time .sleep_ms (10 ) # desparate attempt to try avoid sporadic version misreads like (0, 255, 15) maybe Fri3dCamp/badge_2026_fw/issues/2
140133 current_version = self .version
141134 print (f"Current_version of CH32 firmware: { current_version } " )
142135 except Exception as e :
143136 print ("Could not check CH32 firmware version, assuming 0.0.0" )
144137 current_version = (0 , 0 , 0 )
145- if current_version == (0 , 255 , 15 ): # basically 00FFF0
146- if warning_cb :
147- warning_cb ("CH32 firmware version (0,255,15) bug - assuming latest version!" )
148- return False
138+ if current_version == (0 , 255 , 15 ):
139+ warning_cb (msg = "CH32 firmware version (0,255,15) bug, trying re-read..." )
140+ current_version = self .version # re-read seems to work (often?)
141+ if current_version == (0 , 255 , 15 ):
142+ warning_cb (msg = "CH32 firmware version (0,255,15) bug after re-read, skipping firmware install/update" , sleep_ms = 4000 , r = 96 , g = 21 , b = 21 )
143+ return False
149144 if latest_version <= current_version :
150145 print (f"CH32 firmware { latest_version } <= { current_version } so not updating it" )
151146 return False
152147 print (f"CH32 firmware { latest_version } > { current_version } so updating it" )
153148 try :
154149 self .install_firmware (filename , progress_cb )
155- if success_cb :
156- success_cb ()
150+ success_cb ()
157151 except Exception as e :
158- if failure_cb :
159- failure_cb (e )
152+ failure_cb (e )
160153 return True # re-initialize expander_i2c, even if the install failed, just in case
161154 print ("Firmware installed, checking new version" )
162155 try :
0 commit comments