My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

upload.py 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. import argparse
  2. import sys
  3. import os
  4. import time
  5. import random
  6. import serial
  7. Import("env")
  8. # Needed (only) for compression, but there are problems with pip install heatshrink
  9. #try:
  10. # import heatshrink
  11. #except ImportError:
  12. # # Install heatshrink
  13. # print("Installing 'heatshrink' python module...")
  14. # env.Execute(env.subst("$PYTHONEXE -m pip install heatshrink"))
  15. #
  16. # Not tested: If it's safe to install python libraries in PIO python try:
  17. # env.Execute(env.subst("$PYTHONEXE -m pip install https://github.com/p3p/pyheatshrink/releases/download/0.3.3/pyheatshrink-pip.zip"))
  18. import MarlinBinaryProtocol
  19. #-----------------#
  20. # Upload Callback #
  21. #-----------------#
  22. def Upload(source, target, env):
  23. #-------#
  24. # Debug #
  25. #-------#
  26. Debug = False # Set to True to enable script debug
  27. def debugPrint(data):
  28. if Debug: print(f"[Debug]: {data}")
  29. #------------------#
  30. # Marlin functions #
  31. #------------------#
  32. def _GetMarlinEnv(marlinEnv, feature):
  33. if not marlinEnv: return None
  34. return marlinEnv[feature] if feature in marlinEnv else None
  35. #----------------#
  36. # Port functions #
  37. #----------------#
  38. def _GetUploadPort(env):
  39. debugPrint('Autodetecting upload port...')
  40. env.AutodetectUploadPort(env)
  41. portName = env.subst('$UPLOAD_PORT')
  42. if not portName:
  43. raise Exception('Error detecting the upload port.')
  44. debugPrint('OK')
  45. return portName
  46. #-------------------------#
  47. # Simple serial functions #
  48. #-------------------------#
  49. def _OpenPort():
  50. # Open serial port
  51. if port.is_open: return
  52. debugPrint('Opening upload port...')
  53. port.open()
  54. port.reset_input_buffer()
  55. debugPrint('OK')
  56. def _ClosePort():
  57. # Open serial port
  58. if port is None: return
  59. if not port.is_open: return
  60. debugPrint('Closing upload port...')
  61. port.close()
  62. debugPrint('OK')
  63. def _Send(data):
  64. debugPrint(f'>> {data}')
  65. strdata = bytearray(data, 'utf8') + b'\n'
  66. port.write(strdata)
  67. time.sleep(0.010)
  68. def _Recv():
  69. clean_responses = []
  70. responses = port.readlines()
  71. for Resp in responses:
  72. # Suppress invalid chars (coming from debug info)
  73. try:
  74. clean_response = Resp.decode('utf8').rstrip().lstrip()
  75. clean_responses.append(clean_response)
  76. debugPrint(f'<< {clean_response}')
  77. except:
  78. pass
  79. return clean_responses
  80. #------------------#
  81. # SDCard functions #
  82. #------------------#
  83. def _CheckSDCard():
  84. debugPrint('Checking SD card...')
  85. _Send('M21')
  86. Responses = _Recv()
  87. if len(Responses) < 1 or not any('SD card ok' in r for r in Responses):
  88. raise Exception('Error accessing SD card')
  89. debugPrint('SD Card OK')
  90. return True
  91. #----------------#
  92. # File functions #
  93. #----------------#
  94. def _GetFirmwareFiles(UseLongFilenames):
  95. debugPrint('Get firmware files...')
  96. _Send(f"M20 F{'L' if UseLongFilenames else ''}")
  97. Responses = _Recv()
  98. if len(Responses) < 3 or not any('file list' in r for r in Responses):
  99. raise Exception('Error getting firmware files')
  100. debugPrint('OK')
  101. return Responses
  102. def _FilterFirmwareFiles(FirmwareList, UseLongFilenames):
  103. Firmwares = []
  104. for FWFile in FirmwareList:
  105. # For long filenames take the 3rd column of the firmwares list
  106. if UseLongFilenames:
  107. Space = 0
  108. Space = FWFile.find(' ')
  109. if Space >= 0: Space = FWFile.find(' ', Space + 1)
  110. if Space >= 0: FWFile = FWFile[Space + 1:]
  111. if not '/' in FWFile and '.BIN' in FWFile.upper():
  112. Firmwares.append(FWFile[:FWFile.upper().index('.BIN') + 4])
  113. return Firmwares
  114. def _RemoveFirmwareFile(FirmwareFile):
  115. _Send(f'M30 /{FirmwareFile}')
  116. Responses = _Recv()
  117. Removed = len(Responses) >= 1 and any('File deleted' in r for r in Responses)
  118. if not Removed:
  119. raise Exception(f"Firmware file '{FirmwareFile}' not removed")
  120. return Removed
  121. def _RollbackUpload(FirmwareFile):
  122. if not rollback: return
  123. print(f"Rollback: trying to delete firmware '{FirmwareFile}'...")
  124. _OpenPort()
  125. # Wait for SD card release
  126. time.sleep(1)
  127. # Remount SD card
  128. _CheckSDCard()
  129. print(' OK' if _RemoveFirmwareFile(FirmwareFile) else ' Error!')
  130. _ClosePort()
  131. #---------------------#
  132. # Callback Entrypoint #
  133. #---------------------#
  134. port = None
  135. protocol = None
  136. filetransfer = None
  137. rollback = False
  138. # Get Marlin evironment vars
  139. MarlinEnv = env['MARLIN_FEATURES']
  140. marlin_pioenv = _GetMarlinEnv(MarlinEnv, 'PIOENV')
  141. marlin_motherboard = _GetMarlinEnv(MarlinEnv, 'MOTHERBOARD')
  142. marlin_board_info_name = _GetMarlinEnv(MarlinEnv, 'BOARD_INFO_NAME')
  143. marlin_board_custom_build_flags = _GetMarlinEnv(MarlinEnv, 'BOARD_CUSTOM_BUILD_FLAGS')
  144. marlin_firmware_bin = _GetMarlinEnv(MarlinEnv, 'FIRMWARE_BIN')
  145. marlin_long_filename_host_support = _GetMarlinEnv(MarlinEnv, 'LONG_FILENAME_HOST_SUPPORT') is not None
  146. marlin_longname_write = _GetMarlinEnv(MarlinEnv, 'LONG_FILENAME_WRITE_SUPPORT') is not None
  147. marlin_custom_firmware_upload = _GetMarlinEnv(MarlinEnv, 'CUSTOM_FIRMWARE_UPLOAD') is not None
  148. marlin_short_build_version = _GetMarlinEnv(MarlinEnv, 'SHORT_BUILD_VERSION')
  149. marlin_string_config_h_author = _GetMarlinEnv(MarlinEnv, 'STRING_CONFIG_H_AUTHOR')
  150. # Get firmware upload params
  151. upload_firmware_source_name = str(source[0]) # Source firmware filename
  152. upload_speed = env['UPLOAD_SPEED'] if 'UPLOAD_SPEED' in env else 115200
  153. # baud rate of serial connection
  154. upload_port = _GetUploadPort(env) # Serial port to use
  155. # Set local upload params
  156. upload_firmware_target_name = os.path.basename(upload_firmware_source_name)
  157. # Target firmware filename
  158. upload_timeout = 1000 # Communication timout, lossy/slow connections need higher values
  159. upload_blocksize = 512 # Transfer block size. 512 = Autodetect
  160. upload_compression = True # Enable compression
  161. upload_error_ratio = 0 # Simulated corruption ratio
  162. upload_test = False # Benchmark the serial link without storing the file
  163. upload_reset = True # Trigger a soft reset for firmware update after the upload
  164. # Set local upload params based on board type to change script behavior
  165. # "upload_delete_old_bins": delete all *.bin files in the root of SD Card
  166. upload_delete_old_bins = marlin_motherboard in ['BOARD_CREALITY_V4', 'BOARD_CREALITY_V4210', 'BOARD_CREALITY_V422', 'BOARD_CREALITY_V423',
  167. 'BOARD_CREALITY_V427', 'BOARD_CREALITY_V431', 'BOARD_CREALITY_V452', 'BOARD_CREALITY_V453',
  168. 'BOARD_CREALITY_V24S1']
  169. # "upload_random_name": generate a random 8.3 firmware filename to upload
  170. upload_random_filename = marlin_motherboard in ['BOARD_CREALITY_V4', 'BOARD_CREALITY_V4210', 'BOARD_CREALITY_V422', 'BOARD_CREALITY_V423',
  171. 'BOARD_CREALITY_V427', 'BOARD_CREALITY_V431', 'BOARD_CREALITY_V452', 'BOARD_CREALITY_V453',
  172. 'BOARD_CREALITY_V24S1'] and not marlin_long_filename_host_support
  173. try:
  174. # Start upload job
  175. print(f"Uploading firmware '{os.path.basename(upload_firmware_target_name)}' to '{marlin_motherboard}' via '{upload_port}'")
  176. # Dump some debug info
  177. if Debug:
  178. print('Upload using:')
  179. print('---- Marlin -----------------------------------')
  180. print(f' PIOENV : {marlin_pioenv}')
  181. print(f' SHORT_BUILD_VERSION : {marlin_short_build_version}')
  182. print(f' STRING_CONFIG_H_AUTHOR : {marlin_string_config_h_author}')
  183. print(f' MOTHERBOARD : {marlin_motherboard}')
  184. print(f' BOARD_INFO_NAME : {marlin_board_info_name}')
  185. print(f' CUSTOM_BUILD_FLAGS : {marlin_board_custom_build_flags}')
  186. print(f' FIRMWARE_BIN : {marlin_firmware_bin}')
  187. print(f' LONG_FILENAME_HOST_SUPPORT : {marlin_long_filename_host_support}')
  188. print(f' LONG_FILENAME_WRITE_SUPPORT : {marlin_longname_write}')
  189. print(f' CUSTOM_FIRMWARE_UPLOAD : {marlin_custom_firmware_upload}')
  190. print('---- Upload parameters ------------------------')
  191. print(f' Source : {upload_firmware_source_name}')
  192. print(f' Target : {upload_firmware_target_name}')
  193. print(f' Port : {upload_port} @ {upload_speed} baudrate')
  194. print(f' Timeout : {upload_timeout}')
  195. print(f' Block size : {upload_blocksize}')
  196. print(f' Compression : {upload_compression}')
  197. print(f' Error ratio : {upload_error_ratio}')
  198. print(f' Test : {upload_test}')
  199. print(f' Reset : {upload_reset}')
  200. print('-----------------------------------------------')
  201. # Custom implementations based on board parameters
  202. # Generate a new 8.3 random filename
  203. if upload_random_filename:
  204. upload_firmware_target_name = f"fw-{''.join(random.choices('ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', k=5))}.BIN"
  205. print(f"Board {marlin_motherboard}: Overriding firmware filename to '{upload_firmware_target_name}'")
  206. # Delete all *.bin files on the root of SD Card (if flagged)
  207. if upload_delete_old_bins:
  208. # CUSTOM_FIRMWARE_UPLOAD is needed for this feature
  209. if not marlin_custom_firmware_upload:
  210. raise Exception(f"CUSTOM_FIRMWARE_UPLOAD must be enabled in 'Configuration_adv.h' for '{marlin_motherboard}'")
  211. # Init & Open serial port
  212. port = serial.Serial(upload_port, baudrate = upload_speed, write_timeout = 0, timeout = 0.1)
  213. _OpenPort()
  214. # Check SD card status
  215. _CheckSDCard()
  216. # Get firmware files
  217. FirmwareFiles = _GetFirmwareFiles(marlin_long_filename_host_support)
  218. if Debug:
  219. for FirmwareFile in FirmwareFiles:
  220. print(f'Found: {FirmwareFile}')
  221. # Get all 1st level firmware files (to remove)
  222. OldFirmwareFiles = _FilterFirmwareFiles(FirmwareFiles[1:len(FirmwareFiles)-2], marlin_long_filename_host_support) # Skip header and footers of list
  223. if len(OldFirmwareFiles) == 0:
  224. print('No old firmware files to delete')
  225. else:
  226. print(f"Remove {len(OldFirmwareFiles)} old firmware file{'s' if len(OldFirmwareFiles) != 1 else ''}:")
  227. for OldFirmwareFile in OldFirmwareFiles:
  228. print(f" -Removing- '{OldFirmwareFile}'...")
  229. print(' OK' if _RemoveFirmwareFile(OldFirmwareFile) else ' Error!')
  230. # Close serial
  231. _ClosePort()
  232. # Cleanup completed
  233. debugPrint('Cleanup completed')
  234. # WARNING! The serial port must be closed here because the serial transfer that follow needs it!
  235. # Upload firmware file
  236. debugPrint(f"Copy '{upload_firmware_source_name}' --> '{upload_firmware_target_name}'")
  237. protocol = MarlinBinaryProtocol.Protocol(upload_port, upload_speed, upload_blocksize, float(upload_error_ratio), int(upload_timeout))
  238. #echologger = MarlinBinaryProtocol.EchoProtocol(protocol)
  239. protocol.connect()
  240. # Mark the rollback (delete broken transfer) from this point on
  241. rollback = True
  242. filetransfer = MarlinBinaryProtocol.FileTransferProtocol(protocol)
  243. transferOK = filetransfer.copy(upload_firmware_source_name, upload_firmware_target_name, upload_compression, upload_test)
  244. protocol.disconnect()
  245. # Notify upload completed
  246. protocol.send_ascii('M117 Firmware uploaded' if transferOK else 'M117 Firmware upload failed')
  247. # Remount SD card
  248. print('Wait for SD card release...')
  249. time.sleep(1)
  250. print('Remount SD card')
  251. protocol.send_ascii('M21')
  252. # Transfer failed?
  253. if not transferOK:
  254. protocol.shutdown()
  255. _RollbackUpload(upload_firmware_target_name)
  256. else:
  257. # Trigger firmware update
  258. if upload_reset:
  259. print('Trigger firmware update...')
  260. protocol.send_ascii('M997', True)
  261. protocol.shutdown()
  262. print('Firmware update completed' if transferOK else 'Firmware update failed')
  263. return 0 if transferOK else -1
  264. except KeyboardInterrupt:
  265. print('Aborted by user')
  266. if filetransfer: filetransfer.abort()
  267. if protocol:
  268. protocol.disconnect()
  269. protocol.shutdown()
  270. _RollbackUpload(upload_firmware_target_name)
  271. _ClosePort()
  272. raise
  273. except serial.SerialException as se:
  274. # This exception is raised only for send_ascii data (not for binary transfer)
  275. print(f'Serial excepion: {se}, transfer aborted')
  276. if protocol:
  277. protocol.disconnect()
  278. protocol.shutdown()
  279. _RollbackUpload(upload_firmware_target_name)
  280. _ClosePort()
  281. raise Exception(se)
  282. except MarlinBinaryProtocol.FatalError:
  283. print('Too many retries, transfer aborted')
  284. if protocol:
  285. protocol.disconnect()
  286. protocol.shutdown()
  287. _RollbackUpload(upload_firmware_target_name)
  288. _ClosePort()
  289. raise
  290. except Exception as ex:
  291. print(f"\nException: {ex}, transfer aborted")
  292. if protocol:
  293. protocol.disconnect()
  294. protocol.shutdown()
  295. _RollbackUpload(upload_firmware_target_name)
  296. _ClosePort()
  297. print('Firmware not updated')
  298. raise
  299. # Attach custom upload callback
  300. env.Replace(UPLOADCMD=Upload)