Index: scripts/update_package_script/update_package.py =================================================================== diff -u -r52796f2db103cc8f253d00b7334b4f79c2d2cfd2 -re96bee4b319519f249302d308534a50a4902c4ce --- scripts/update_package_script/update_package.py (.../update_package.py) (revision 52796f2db103cc8f253d00b7334b4f79c2d2cfd2) +++ scripts/update_package_script/update_package.py (.../update_package.py) (revision e96bee4b319519f249302d308534a50a4902c4ce) @@ -12,15 +12,18 @@ The class runs the firmware and FPGA updates """ - _SW_UPDATE_PAYLOAD_BYTES = 512 #256 + _TD_SW_UPDATE_PAYLOAD_BYTES = 256 + _DD_SW_UPDATE_PAYLOAD_BYTES = 512 _BROADCAST_MESSAGE_INTERVAL_S = 0.1 + _WAIT_FOR_BOOTLOADER_AFTER_RESET_S = 1.5 _SW_UPDATE_STACK = 'stack_name' _SW_UPDATE_DEST = 'stack_target' _BINARY_FILE_SIZE = 'file_size' _WRITTEN_BYTE_COUNT = 'byte_count' _WRITE_COUNTER = 'write_counter' _CUR_UPDATE_ELAPSED_TIME_S = 'elapsed_time_s' _BINARY_LEN_BYTES = 'binary_len_bytes' + _STACK_BYTE_SIZE_TO_READ = 'stack_bytes_to_read' _MCS_DATA_LINE_LEN = 43 _MCS_START_CODE_DEC = ':' @@ -61,6 +64,7 @@ self._sw_update_status[self._WRITE_COUNTER] = 0 self._sw_update_status[self._CUR_UPDATE_ELAPSED_TIME_S] = 0 self._sw_update_status[self._BINARY_LEN_BYTES] = 0 + self._sw_update_status[self._STACK_BYTE_SIZE_TO_READ] = self._TD_SW_UPDATE_PAYLOAD_BYTES # Default to TD self._utilities.clear_msg_ack_nack_status(self._utilities.SEND_MSG_ACK_STATUS_KEY_NAME) self._utilities.clear_msg_ack_nack_status(self._utilities.UPDATE_MSG_ACK_STATUS_KEY_NAME) @@ -94,11 +98,14 @@ # TODo manifest #self._sw_update_status[self._SW_UPDATE_STACK] = stack_to_update #self._sw_update_status[self._SW_UPDATE_TARGET] = stack_target + if self._sw_update_status[self._SW_UPDATE_STACK] == UpdateStacks.STACK_DD.value: \ + self._sw_update_status[self._STACK_BYTE_SIZE_TO_READ] = self._DD_SW_UPDATE_PAYLOAD_BYTES + # If the file is a .bin the entire file has to be written, if it is an .mcs then we should look into only the data portion self._sw_update_status[self._BINARY_FILE_SIZE] = os.path.getsize(file_path) if file_path.endswith(".mcs"): num_of_lines_with_data = 0 # Command: grep -c ":10" Leahi_103488_ctlbrd_hyd_L018_update.mcs - # Look for the lines with :10 at the beginning of them + # Look for the lines with :10 at the beginning of them and count the number of lines. cmd = ['grep', '-c', '^' + self._MCS_START_CODE_DEC + self._MCS_NUM_OF_BYTES_DEC, file_path] process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout, stderr = process.communicate() @@ -129,7 +136,7 @@ @return none """ - if clear_print: return # If clear print, return + if clear_print: return # If clear no print, return progress_bar_scale = 10 binary_len = self._sw_update_status[self._BINARY_LEN_BYTES] write_counter = self._sw_update_status[self._WRITE_COUNTER] @@ -139,18 +146,19 @@ destination = self._sw_update_status[self._SW_UPDATE_DEST] destination = UpdateStacksDestinations(destination).name bytes_written = self._sw_update_status[self._WRITTEN_BYTE_COUNT] + stack_target_bytes_2_read = self._sw_update_status[self._STACK_BYTE_SIZE_TO_READ] # If the file size is greater than 1 MB, then set the scale to bigger than default so the progress fits in the screen if binary_file_size_bytes >= 1000000: progress_bar_scale = 50 elapsed_time = time.time() - self._sw_update_status[self._CUR_UPDATE_ELAPSED_TIME_S] # Get number of payloads needed to update the binary but since this is for display only, it is scaled down - num_of_payloads_needed = math.ceil(binary_file_size_bytes / (self._SW_UPDATE_PAYLOAD_BYTES * progress_bar_scale)) + num_of_payloads_needed = math.ceil(binary_file_size_bytes / (stack_target_bytes_2_read * progress_bar_scale)) # Update the latest number of bytes that were successfully sent to the bootloader bytes_written += binary_len # If the accumulation of the bytes written is a division of the scale (for display) then update the progress bar - if bytes_written % (self._SW_UPDATE_PAYLOAD_BYTES * progress_bar_scale) == 0: write_counter += 1 + if bytes_written % (stack_target_bytes_2_read * progress_bar_scale) == 0: write_counter += 1 # If the length of the bytes written is less than the default bytes to extract and write, it means we are the end of the # update of the binary file, then update the progress bar - if binary_len < self._SW_UPDATE_PAYLOAD_BYTES: write_counter += 1 + if binary_len < stack_target_bytes_2_read: write_counter += 1 # Calculate the percentage of the progress percent = (bytes_written / self._sw_update_status[self._BINARY_FILE_SIZE]) * 100 # Add more # and subtract from the empty spaces @@ -178,7 +186,7 @@ # Read through the file, once ack status says the CAN bus is ready read another batch of bytes if update_ack_status == CanCommStatus.CAN_COMM_READY.value: # Read the next batch of bytes in the provided number of bytes - line = f.read(self._SW_UPDATE_PAYLOAD_BYTES) + line = f.read(self._sw_update_status[self._STACK_BYTE_SIZE_TO_READ]) target = self._sw_update_status[self._SW_UPDATE_STACK] self._sw_update_status[self._BINARY_LEN_BYTES] = len(line) if line != b'': @@ -201,7 +209,7 @@ @return none """ - f = open(file_path, 'r') + f = open(file_path, 'r') # Read the .mcs file but not as binary since the there are more values than the bytes to be read (e.g. length, CRC, address) payload_bytes = bytes() for line_number, line in enumerate(f, start=1): summation = 0 @@ -235,7 +243,8 @@ summation += int_value # Get the 8-bit modulo. The CRC is the 2's complement remainder = summation % self._MCS_SUM_MODULO_256 - crc = self._MCS_SUM_MODULO_256 - remainder + # Calculate the 2's complement and then & with 0xFF to truncate it to a 1-byte value. (e.g. 256 & 0xFF = 0) + crc = ((remainder ^ 0xFF) + 1) & 0xFF if crc != line_crc: self._utilities.file_handle.write("Line Num: {}, Line: {}, Line CRC: {}, Calc CRC: {}\r".format(line_number, line, line_crc, crc)) # TODO if the CRC failed, we should fail the update update_data = line[self._MCS_UPDATE_DATA_START_INDEX:len(line) - self._MCS_LINE_CRC_LEN] @@ -249,7 +258,7 @@ conc_int = int(conc_bytes, 16) payload_bytes += self._utilities.convert_data_to_bytes('