private bool WriteFlashRecover(BlockManager bm) { int startAddress = 0x020000; int saved_progress = 0; for (int blockNumber = 0; blockNumber <= 0xF50; blockNumber++) { int percentage = (int)(((float)blockNumber * 100) / 3920F); if (percentage > saved_progress) { CastProgressWriteEvent(percentage); saved_progress = percentage; } byte[] data2Send = bm.GetNextBlock(); int length = 0xF0; if (blockNumber == 0xF50) length = 0xE6; if (SendTransferData(length, startAddress + (blockNumber * 0xEA), 0x311)) { //canUsbDevice.RequestDeviceReady(); // send the data from the block // calculate number of frames int numberOfFrames = (int)data2Send.Length / 7; // remnants? if (((int)data2Send.Length % 7) > 0) numberOfFrames++; byte iFrameNumber = 0x21; int txpnt = 0; CANMessage msg = new CANMessage(0x7E0, 0, 8); for (int frame = 0; frame < numberOfFrames; frame++) { var cmd = BitTools.GetFrameBytes(iFrameNumber, data2Send, txpnt); msg.setData(cmd); txpnt += 7; iFrameNumber++; if (iFrameNumber > 0x2F) iFrameNumber = 0x20; msg.elmExpectedResponses = frame == numberOfFrames - 1 ? 1 : 0; if (frame == numberOfFrames - 1) m_canListener.ClearQueue(); if (!canUsbDevice.sendMessage(msg)) { logger.Debug("Couldn't send message"); } if (m_sleepTime > 0) Thread.Sleep(m_sleepTime); } // now wait for 01 76 00 00 00 00 00 00 ulong data = m_canListener.waitMessage(timeoutP2ct, 0x7E8).getData(); if (getCanData(data, 0) != 0x01 || getCanData(data, 1) != 0x76) { _stallKeepAlive = false; return false; } //canUsbDevice.RequestDeviceReady(); BroadcastKeepAlive(); } } return true; }
private bool ProgramFlash(BlockManager bm) { const int startAddress = 0x020000; int lastBlockNumber = bm.GetLastBlockNumber(); int saved_progress = 0; for (int blockNumber = 0; blockNumber <= lastBlockNumber; blockNumber++) // All blocks == 0xF50 { int percentage = (int)(((float)blockNumber * 100) / (float)lastBlockNumber); if (percentage > saved_progress) { CastProgressWriteEvent(percentage); saved_progress = percentage; } byte[] data2Send = bm.GetNextBlock(); int length = 0xF0; if (blockNumber == 0xF50) length = 0xE6; int currentAddress = startAddress + (blockNumber * 0xEA); sw.Reset(); sw.Start(); if (SendTransferData(length, currentAddress, 0x7E8)) { canUsbDevice.RequestDeviceReady(); // calculate number of frames int numberOfFrames = (int)data2Send.Length / 7; // remnants? if (((int)data2Send.Length % 7) > 0) numberOfFrames++; byte iFrameNumber = 0x21; int txpnt = 0; CANMessage msg = new CANMessage(0x7E0, 0, 8); for (int frame = 0; frame < numberOfFrames; frame++) { var cmd = BitTools.GetFrameBytes(iFrameNumber, data2Send, txpnt); msg.setData(cmd); txpnt += 7; iFrameNumber++; if (iFrameNumber > 0x2F) iFrameNumber = 0x20; msg.elmExpectedResponses = (frame == numberOfFrames - 1) ? 1 : 0; if (frame == numberOfFrames - 1) m_canListener.ClearQueue(); if (!canUsbDevice.sendMessage(msg)) { logger.Debug("Couldn't send message"); } if (m_sleepTime > 0) Thread.Sleep(m_sleepTime); } Application.DoEvents(); // now wait for 01 76 00 00 00 00 00 00 ulong data = m_canListener.waitMessage(timeoutP2ct, 0x7E8).getData(); if (getCanData(data, 0) != 0x01 || getCanData(data, 1) != 0x76) { CastInfoEvent("Got incorrect response " + data.ToString("X16"), ActivityType.UploadingFlash); _stallKeepAlive = false; return false; } canUsbDevice.RequestDeviceReady(); SendKeepAlive(); } sw.Stop(); } return true; }
public void WriteFlash(object sender, DoWorkEventArgs workEvent) { BackgroundWorker bw = sender as BackgroundWorker; string filename = (string)workEvent.Argument; if (!canUsbDevice.isOpen()) return; _needRecovery = false; BlockManager bm = new BlockManager(); bm.SetFilename(filename); _stallKeepAlive = true; SendKeepAlive(); sw.Reset(); sw.Start(); CastInfoEvent("Starting session", ActivityType.UploadingBootloader); StartSession10(); CastInfoEvent("Telling ECU to clear CANbus", ActivityType.UploadingBootloader); SendShutup(); SendA2(); SendA5(); SendA503(); Thread.Sleep(50); SendKeepAlive(); // verified upto here _securityLevel = AccessLevel.AccessLevel01; //CastInfoEvent("Requesting security access", ActivityType.UploadingBootloader); if (!RequestSecurityAccess(0)) // was 2000 milli-seconds { CastInfoEvent("Failed to get security access", ActivityType.UploadingFlash); _stallKeepAlive = false; workEvent.Result = false; return; } Thread.Sleep(50); CastInfoEvent("Uploading bootloader", ActivityType.UploadingBootloader); if (!UploadBootloaderWrite()) { CastInfoEvent("Failed to upload bootloader", ActivityType.UploadingFlash); _stallKeepAlive = false; workEvent.Result = false; return; } CastInfoEvent("Starting bootloader", ActivityType.UploadingBootloader); // start bootloader in ECU //SendKeepAlive(); Thread.Sleep(50); if (!StartBootloader()) { CastInfoEvent("Failed to start bootloader", ActivityType.UploadingFlash); _stallKeepAlive = false; workEvent.Result = false; return; } Thread.Sleep(100); SendKeepAlive(); Thread.Sleep(50); CastInfoEvent("Erasing FLASH", ActivityType.StartErasingFlash); if (SendrequestDownload(false)) { _needRecovery = true; SendShutup(); CastInfoEvent("Programming FLASH", ActivityType.UploadingFlash); bool success = ProgramFlash(bm); if (success) CastInfoEvent("FLASH upload completed", ActivityType.ConvertingFile); else CastInfoEvent("FLASH upload failed", ActivityType.ConvertingFile); sw.Stop(); _needRecovery = false; // what else to do? Send0120(); CastInfoEvent("Session ended", ActivityType.FinishedFlashing); } else { sw.Stop(); _needRecovery = false; _stallKeepAlive = false; CastInfoEvent("Failed to erase FLASH", ActivityType.ConvertingFile); Send0120(); CastInfoEvent("Session ended", ActivityType.FinishedFlashing); workEvent.Result = false; return; } _stallKeepAlive = false; workEvent.Result = true; }
/// <summary> /// Send ONLY the erase and write commands, ECU is already in running bootloader /// </summary> /// <param name="filename"></param> /// <returns></returns> public bool RecoverECUOLD(string filename) { if (!canUsbDevice.isOpen()) return false; _stallKeepAlive = true; BlockManager bm = new BlockManager(); bm.SetFilename(filename); int startAddress = 0x020000; int saved_progress = 0; SendKeepAlive(); _securityLevel = AccessLevel.AccessLevel01; CastInfoEvent("Erasing FLASH", ActivityType.StartErasingFlash); if (SendrequestDownload(true)) { CastInfoEvent("Programming FLASH", ActivityType.UploadingFlash); for (int blockNumber = 0; blockNumber <= 0xF50; blockNumber++) { int percentage = (int)(((float)blockNumber * 100) / 3920F); if (percentage > saved_progress) { CastProgressWriteEvent(percentage); saved_progress = percentage; } byte[] data2Send = bm.GetNextBlock(); int length = 0xF0; if (blockNumber == 0xF50) length = 0xE6; if (SendTransferData(length, startAddress + (blockNumber * 0xEA), 0x311)) { // send the data from the block // calculate number of frames int numberOfFrames = (int)data2Send.Length / 7; // remnants? if (((int)data2Send.Length % 7) > 0) numberOfFrames++; byte iFrameNumber = 0x21; int txpnt = 0; CANMessage msg = new CANMessage(0x7E0, 0, 8); for (int frame = 0; frame < numberOfFrames; frame++) { ulong cmd = 0x0000000000000000; // 0x34 = upload data to ECU msg.setData(cmd); msg.setCanData(iFrameNumber, 0); msg.setCanData(data2Send[txpnt++], 1); msg.setCanData(data2Send[txpnt++], 2); msg.setCanData(data2Send[txpnt++], 3); msg.setCanData(data2Send[txpnt++], 4); msg.setCanData(data2Send[txpnt++], 5); msg.setCanData(data2Send[txpnt++], 6); msg.setCanData(data2Send[txpnt++], 7); iFrameNumber++; if (iFrameNumber > 0x2F) iFrameNumber = 0x20; if (!canUsbDevice.sendMessage(msg)) { logger.Debug("Couldn't send message"); } Thread.Sleep(1); } // send the remaining data m_canListener.setupWaitMessage(0x7E8); // now wait for 01 76 00 00 00 00 00 00 CANMessage response = new CANMessage(); response = new CANMessage(); response = m_canListener.waitMessage(timeoutP2ct); ulong data = response.getData(); if (getCanData(data, 0) != 0x01 || getCanData(data, 1) != 0x76) { return false; } SendKeepAlive(); } } sw.Stop(); CastInfoEvent("FLASH upload completed", ActivityType.ConvertingFile); // what else to do? Send0120(); CastInfoEvent("Session ended", ActivityType.FinishedFlashing); } else { sw.Stop(); CastInfoEvent("Failed to erase FLASH", ActivityType.ConvertingFile); Send0120(); CastInfoEvent("Session ended", ActivityType.FinishedFlashing); return false; } _stallKeepAlive = false; return true; }
public void RecoverECU(object sender, DoWorkEventArgs workEvent) { BackgroundWorker bw = sender as BackgroundWorker; string filename = (string)workEvent.Argument; string diagDataID = GetDiagnosticDataIdentifier0101(); Console.WriteLine("DataID: " + diagDataID); if (diagDataID == string.Empty) { canUsbDevice.SetupCANFilter("7E8", "000"); //canUsbDevice.SetAutomaticFlowControl(false); BlockManager bm = new BlockManager(); bm.SetFilename(filename); sw.Reset(); sw.Start(); _stallKeepAlive = true; CastInfoEvent("Recovery needed...", ActivityType.UploadingBootloader); BroadcastKeepAlive(); Thread.Sleep(200); // was 1 BroadcastKeepAlive(); Thread.Sleep(500); CastInfoEvent("Starting session", ActivityType.UploadingBootloader); BroadcastSession10(); Thread.Sleep(200); // was 1 CastInfoEvent("Telling ECU to clear CANbus", ActivityType.UploadingBootloader); BroadcastShutup(); Thread.Sleep(200); // was 1 int progState = GetProgrammingState(0x311); if (progState == 0x01) { CastInfoEvent("Recovery needed phase 1", ActivityType.UploadingBootloader); BroadcastShutup011(); if (GetProgrammingState011() == 0x01) { CastInfoEvent("Recovery needed phase 2", ActivityType.UploadingBootloader); SendA5011(); Thread.Sleep(100); SendA503011(); Thread.Sleep(100); BroadcastKeepAlive(); Thread.Sleep(100); CastInfoEvent("Requesting security access...", ActivityType.UploadingBootloader); if (RequestSecurityAccess011(0)) { CastInfoEvent("Security access granted, uploading bootloader", ActivityType.UploadingBootloader); UploadBootloaderRecover(); CastInfoEvent("Starting bootloader", ActivityType.UploadingBootloader); Thread.Sleep(500); StartBootloader011(); Thread.Sleep(500); CastInfoEvent("Erasing FLASH", ActivityType.StartErasingFlash); if (SendrequestDownload(true)) { _needRecovery = true; CastInfoEvent("Programming FLASH", ActivityType.UploadingFlash); bool success = WriteFlashRecover(bm); sw.Stop(); _needRecovery = false; // what else to do? Send0120(); if (success) { CastInfoEvent("Recovery completed", ActivityType.ConvertingFile); CastInfoEvent("Session ended", ActivityType.FinishedFlashing); workEvent.Result = true; return; } else { CastInfoEvent("Recovery failed", ActivityType.ConvertingFile); workEvent.Result = false; return; } } else { sw.Stop(); _needRecovery = false; _stallKeepAlive = false; CastInfoEvent("Failed to erase FLASH", ActivityType.ConvertingFile); Send0120(); CastInfoEvent("Session ended", ActivityType.FinishedFlashing); workEvent.Result = false; return; } } } else { CastInfoEvent("Recovery not needed...", ActivityType.UploadingBootloader); } } else if (progState == 0x00) { CastInfoEvent("Recovery not needed...", ActivityType.UploadingBootloader); } else if (progState == -1) { CastInfoEvent("Unable to communicate with the ECU...", ActivityType.UploadingBootloader); } sw.Stop(); } else { CastInfoEvent("Recovery not needed...", ActivityType.UploadingBootloader); } workEvent.Result = false; return; }
private bool ProgramFlashT8(BlockManager bm) { int startAddress = 0x020000; for (int blockNumber = 0; blockNumber <= 0xF50; blockNumber++) { float percentage = ((float)blockNumber * 100) / 3920F; CastProgressWriteEvent(percentage); bool canSkip = bm.CanSkipCurrentBlock(); byte[] data2Send = bm.GetNextBlock(); int length = 0xF0; if (blockNumber == 0xF50) length = 0xE6; Stopwatch blockSw = new Stopwatch(); int currentAddress = startAddress + (blockNumber * 0xEA); if (!canSkip) { sw.Reset(); sw.Start(); if (SendTransferData(length, currentAddress, 0x7E8)) { canUsbDevice.RequestDeviceReady(); // calculate number of frames int numberOfFrames = (int)data2Send.Length / 7; // remnants? if (((int)data2Send.Length % 7) > 0) numberOfFrames++; byte iFrameNumber = 0x21; int txpnt = 0; CANMessage msg = new CANMessage(0x7E0, 0, 8); for (int frame = 0; frame < numberOfFrames; frame++) { var cmd = BitTools.GetFrameBytes(iFrameNumber, data2Send, txpnt); msg.setData(cmd); txpnt += 7; iFrameNumber++; if (iFrameNumber > 0x2F) iFrameNumber = 0x20; msg.elmExpectedResponses = (frame == numberOfFrames - 1) ? 1 : 0; if (frame == numberOfFrames - 1) m_canListener.ClearQueue(); if (!canUsbDevice.sendMessage(msg)) { AddToCanTrace("Couldn't send message"); } if (m_sleepTime > 0) Thread.Sleep(m_sleepTime); } Application.DoEvents(); // now wait for 01 76 00 00 00 00 00 00 ulong data = m_canListener.waitMessage(1000, 0x7E8).getData(); if (getCanData(data, 0) != 0x01 || getCanData(data, 1) != 0x76) { CastInfoEvent("Got incorrect response " + data.ToString("X16"), ActivityType.UploadingFlash); _stallKeepAlive = false; return false; } canUsbDevice.RequestDeviceReady(); SendKeepAlive(); } sw.Stop(); //Console.WriteLine(string.Format("Programming block {0} took {1}", blockNumber, sw.Elapsed),ActivityType.UploadingFlash); } else { Console.WriteLine("Skipping block at " + currentAddress); } } return true; }