upload.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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 = upload_delete_old_bins and not marlin_long_filename_host_support
  171. try:
  172. # Start upload job
  173. print(f"Uploading firmware '{os.path.basename(upload_firmware_target_name)}' to '{marlin_motherboard}' via '{upload_port}'")
  174. # Dump some debug info
  175. if Debug:
  176. print('Upload using:')
  177. print('---- Marlin -----------------------------------')
  178. print(f' PIOENV : {marlin_pioenv}')
  179. print(f' SHORT_BUILD_VERSION : {marlin_short_build_version}')
  180. print(f' STRING_CONFIG_H_AUTHOR : {marlin_string_config_h_author}')
  181. print(f' MOTHERBOARD : {marlin_motherboard}')
  182. print(f' BOARD_INFO_NAME : {marlin_board_info_name}')
  183. print(f' CUSTOM_BUILD_FLAGS : {marlin_board_custom_build_flags}')
  184. print(f' FIRMWARE_BIN : {marlin_firmware_bin}')
  185. print(f' LONG_FILENAME_HOST_SUPPORT : {marlin_long_filename_host_support}')
  186. print(f' LONG_FILENAME_WRITE_SUPPORT : {marlin_longname_write}')
  187. print(f' CUSTOM_FIRMWARE_UPLOAD : {marlin_custom_firmware_upload}')
  188. print('---- Upload parameters ------------------------')
  189. print(f' Source : {upload_firmware_source_name}')
  190. print(f' Target : {upload_firmware_target_name}')
  191. print(f' Port : {upload_port} @ {upload_speed} baudrate')
  192. print(f' Timeout : {upload_timeout}')
  193. print(f' Block size : {upload_blocksize}')
  194. print(f' Compression : {upload_compression}')
  195. print(f' Error ratio : {upload_error_ratio}')
  196. print(f' Test : {upload_test}')
  197. print(f' Reset : {upload_reset}')
  198. print('-----------------------------------------------')
  199. # Custom implementations based on board parameters
  200. # Generate a new 8.3 random filename
  201. if upload_random_filename:
  202. upload_firmware_target_name = f"fw-{''.join(random.choices('ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', k=5))}.BIN"
  203. print(f"Board {marlin_motherboard}: Overriding firmware filename to '{upload_firmware_target_name}'")
  204. # Delete all *.bin files on the root of SD Card (if flagged)
  205. if upload_delete_old_bins:
  206. # CUSTOM_FIRMWARE_UPLOAD is needed for this feature
  207. if not marlin_custom_firmware_upload:
  208. raise Exception(f"CUSTOM_FIRMWARE_UPLOAD must be enabled in 'Configuration_adv.h' for '{marlin_motherboard}'")
  209. # Init & Open serial port
  210. port = serial.Serial(upload_port, baudrate = upload_speed, write_timeout = 0, timeout = 0.1)
  211. _OpenPort()
  212. # Check SD card status
  213. _CheckSDCard()
  214. # Get firmware files
  215. FirmwareFiles = _GetFirmwareFiles(marlin_long_filename_host_support)
  216. if Debug:
  217. for FirmwareFile in FirmwareFiles:
  218. print(f'Found: {FirmwareFile}')
  219. # Get all 1st level firmware files (to remove)
  220. OldFirmwareFiles = _FilterFirmwareFiles(FirmwareFiles[1:len(FirmwareFiles)-2], marlin_long_filename_host_support) # Skip header and footers of list
  221. if len(OldFirmwareFiles) == 0:
  222. print('No old firmware files to delete')
  223. else:
  224. print(f"Remove {len(OldFirmwareFiles)} old firmware file{'s' if len(OldFirmwareFiles) != 1 else ''}:")
  225. for OldFirmwareFile in OldFirmwareFiles:
  226. print(f" -Removing- '{OldFirmwareFile}'...")
  227. print(' OK' if _RemoveFirmwareFile(OldFirmwareFile) else ' Error!')
  228. # Close serial
  229. _ClosePort()
  230. # Cleanup completed
  231. debugPrint('Cleanup completed')
  232. # WARNING! The serial port must be closed here because the serial transfer that follow needs it!
  233. # Upload firmware file
  234. debugPrint(f"Copy '{upload_firmware_source_name}' --> '{upload_firmware_target_name}'")
  235. protocol = MarlinBinaryProtocol.Protocol(upload_port, upload_speed, upload_blocksize, float(upload_error_ratio), int(upload_timeout))
  236. #echologger = MarlinBinaryProtocol.EchoProtocol(protocol)
  237. protocol.connect()
  238. # Mark the rollback (delete broken transfer) from this point on
  239. rollback = True
  240. filetransfer = MarlinBinaryProtocol.FileTransferProtocol(protocol)
  241. transferOK = filetransfer.copy(upload_firmware_source_name, upload_firmware_target_name, upload_compression, upload_test)
  242. protocol.disconnect()
  243. # Notify upload completed
  244. protocol.send_ascii('M117 Firmware uploaded' if transferOK else 'M117 Firmware upload failed')
  245. # Remount SD card
  246. print('Wait for SD card release...')
  247. time.sleep(1)
  248. print('Remount SD card')
  249. protocol.send_ascii('M21')
  250. # Transfer failed?
  251. if not transferOK:
  252. protocol.shutdown()
  253. _RollbackUpload(upload_firmware_target_name)
  254. else:
  255. # Trigger firmware update
  256. if upload_reset:
  257. print('Trigger firmware update...')
  258. protocol.send_ascii('M997', True)
  259. protocol.shutdown()
  260. print('Firmware update completed' if transferOK else 'Firmware update failed')
  261. return 0 if transferOK else -1
  262. except KeyboardInterrupt:
  263. print('Aborted by user')
  264. if filetransfer: filetransfer.abort()
  265. if protocol:
  266. protocol.disconnect()
  267. protocol.shutdown()
  268. _RollbackUpload(upload_firmware_target_name)
  269. _ClosePort()
  270. raise
  271. except serial.SerialException as se:
  272. # This exception is raised only for send_ascii data (not for binary transfer)
  273. print(f'Serial excepion: {se}, transfer aborted')
  274. if protocol:
  275. protocol.disconnect()
  276. protocol.shutdown()
  277. _RollbackUpload(upload_firmware_target_name)
  278. _ClosePort()
  279. raise Exception(se)
  280. except MarlinBinaryProtocol.FatalError:
  281. print('Too many retries, transfer aborted')
  282. if protocol:
  283. protocol.disconnect()
  284. protocol.shutdown()
  285. _RollbackUpload(upload_firmware_target_name)
  286. _ClosePort()
  287. raise
  288. except Exception as ex:
  289. print(f"\nException: {ex}, transfer aborted")
  290. if protocol:
  291. protocol.disconnect()
  292. protocol.shutdown()
  293. _RollbackUpload(upload_firmware_target_name)
  294. _ClosePort()
  295. print('Firmware not updated')
  296. raise
  297. # Attach custom upload callback
  298. env.Replace(UPLOADCMD=Upload)