@@ -25,20 +25,32 @@ def status(msg):
2525
2626def critical (msg ):
2727 """Print critical message to stderr"""
28- sys .stderr .write ("factory.py: " )
29- sys .stderr .write (msg )
30- sys .stderr .write ("\n " )
31-
28+ sys .stdout .write ("factory.py: " )
29+ sys .stdout .write (msg )
30+ sys .stdout .write ("\n " )
31+
32+
33+ def esptool_call (cmd ):
34+ try :
35+ esptool .main (cmd )
36+ except SystemExit as e :
37+ # Fetch sys.exit() without leaving the script
38+ if e .code == 0 :
39+ return True
40+ else :
41+ print (f"❌ esptool failed with exit code: { e .code } " )
42+ return False
3243
33- def generateFactooryImage (source , target , env ):
44+ def generateFactoryImage (source , target , env ):
3445 status ("Generating factory image for serial flashing" )
3546
3647 app_offset = 0x10000
3748 app_image = env .subst ("$BUILD_DIR/${PROGNAME}.bin" )
3849
3950 # Set fs_offset = 0 to disable LittleFS image generation
4051 # Set fs_offset to the correct offset from the partition to generate a LittleFS image
41- fs_offset = 0
52+ fs_offset = 0x0
53+ # fs_offset = 0x3E0000
4254 fs_image = env .subst ("$BUILD_DIR/littlefs.bin" )
4355
4456 safeboot_offset = 0x10000
@@ -47,15 +59,10 @@ def generateFactooryImage(source, target, env):
4759 if safeboot_image == "" :
4860 safeboot_project = env .GetProjectOption ("custom_safeboot_dir" , "" )
4961 if safeboot_project != "" :
50- status (
51- "Building SafeBoot image for board %s from %s"
52- % (env .get ("BOARD" ), safeboot_project )
53- )
62+ status ("Building SafeBoot image for board %s from %s" % (env .get ("BOARD" ), safeboot_project ))
5463 if not os .path .isdir (safeboot_project ):
5564 raise Exception ("SafeBoot project not found: %s" % safeboot_project )
56- env .Execute (
57- "SAFEBOOT_BOARD=%s pio run -e safeboot -d %s" % (env .get ("BOARD" ), safeboot_project )
58- )
65+ env .Execute ("SAFEBOOT_BOARD=%s pio run -e safeboot -d %s" % (env .get ("BOARD" ), safeboot_project ))
5966 safeboot_image = join (safeboot_project , ".pio/build/safeboot/firmware.bin" )
6067 if not os .path .isfile (safeboot_image ):
6168 raise Exception ("SafeBoot image not found: %s" % safeboot_image )
@@ -65,10 +72,7 @@ def generateFactooryImage(source, target, env):
6572 if safeboot_url != "" :
6673 safeboot_image = env .subst ("$BUILD_DIR/safeboot.bin" )
6774 if not os .path .isfile (safeboot_image ):
68- status (
69- "Downloading SafeBoot image from %s to %s"
70- % (safeboot_url , safeboot_image )
71- )
75+ status ("Downloading SafeBoot image from %s to %s" % (safeboot_url , safeboot_image ))
7276 response = requests .get (safeboot_url )
7377 if response .status_code != 200 :
7478 raise Exception ("Download error: %d" % response .status_code )
@@ -98,14 +102,14 @@ def generateFactooryImage(source, target, env):
98102 cmd = [
99103 "--chip" ,
100104 chip ,
101- "merge_bin " ,
105+ "merge-bin " ,
102106 "-o" ,
103107 factory_image ,
104- "--flash_mode " ,
108+ "--flash-mode " ,
105109 flash_mode ,
106- "--flash_freq " ,
110+ "--flash-freq " ,
107111 flash_freq ,
108- "--flash_size " ,
112+ "--flash-size " ,
109113 flash_size ,
110114 ]
111115
@@ -117,6 +121,9 @@ def generateFactooryImage(source, target, env):
117121 if fw_size > max_size :
118122 raise Exception ("Firmware binary too large: %d > %d" % (fw_size , max_size ))
119123
124+ status ("Generating factory image at: %s" % factory_image )
125+ status ("You can flash it with:\n > esptool.py write_flash 0x0 %s" % factory_image )
126+
120127 status (" Offset | File" )
121128 for section in sections :
122129 sect_adr , sect_file = section .split (" " , 1 )
@@ -138,11 +145,10 @@ def generateFactooryImage(source, target, env):
138145 status (f" - { hex (fs_offset )} | { fs_image } " )
139146 cmd += [hex (fs_offset ), fs_image ]
140147
141- status ("Using esptool.py arguments: %s" % " " .join (cmd ))
142-
143- esptool .main (cmd )
148+ # status("Using esptool.py arguments: %s" % " ".join(cmd))
144149
145- status ("Factory image generated! You can flash it with:\n > esptool.py write_flash 0x0 %s" % factory_image )
150+ esptool_call (["version" ])
151+ esptool_call (cmd )
146152
147153
148- env .AddPostAction ("$BUILD_DIR/${PROGNAME}.bin" , generateFactooryImage )
154+ env .AddPostAction ("$BUILD_DIR/${PROGNAME}.bin" , generateFactoryImage )
0 commit comments