コード例 #1
0
ファイル: T8Can.cs プロジェクト: mattiasclaesson/T8CANLib
        /// <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;

            SendKeepAlive();
            _securityLevel = AccessLevel.AccessLevel01;
            CastInfoEvent("Erasing flash", ActivityType.StartErasingFlash);
            if (SendrequestDownload(true))
            {
                CastInfoEvent("Programming flash", ActivityType.UploadingFlash);
                for (int blockNumber = 0; blockNumber <= 0xF50; blockNumber++)
                {
                    float percentage = ((float)blockNumber * 100) / 3920F;
                    CastProgressWriteEvent(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))
                            {
                                AddToCanTrace("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(1000);
                        ulong data = response.getData();
                        if (getCanData(data, 0) != 0x01 || getCanData(data, 1) != 0x76)
                        {
                            return false;
                        }
                        SendKeepAlive();
                    }
                }
                sw.Stop();
                CastInfoEvent("Flash upload completed", ActivityType.FinishedFlashing);
                // what else to do?
                Send0120();
                CastInfoEvent("Session ended", ActivityType.FinishedFlashing);
            }
            else
            {
                sw.Stop();
                CastInfoEvent("Failed to erase flash", ActivityType.FinishedFlashing);
                Send0120();
                CastInfoEvent("Session ended", ActivityType.FinishedFlashing);
                return false;

            }
            _stallKeepAlive = false;
            return true;
        }
コード例 #2
0
ファイル: T8Can.cs プロジェクト: mattiasclaesson/T8CANLib
        public bool UpdateFlash(string filename)
        {
            if (!canUsbDevice.isOpen()) return false;
            _needRecovery = false;
            BlockManager bm = new BlockManager();
            bm.SetFilename(filename);

            _stallKeepAlive = true;
            int startAddress = 0x020000;
            SendKeepAlive();
            sw.Reset();
            sw.Start();
            CastInfoEvent("Starting session", ActivityType.UploadingBootloader);
            StartSession10();
            CastInfoEvent("Requesting mandatory data", ActivityType.UploadingBootloader);
            RequestECUInfo(0x90);
            RequestECUInfo(0x97);
            RequestECUInfo(0x92);
            RequestECUInfo(0xB4);
            RequestECUInfo(0xC1);
            RequestECUInfo(0xC2);
            RequestECUInfo(0xC3);
            RequestECUInfo(0xC4);
            RequestECUInfo(0xC5);
            RequestECUInfo(0xC6);
            Send0120();
            Thread.Sleep(1000);
            StartSession1081();
            StartSession10();
            CastInfoEvent("Telling ECU to clear CANbus", ActivityType.UploadingBootloader);
            SendShutup();
            SendA2();
            SendA5();
            SendA503();
            Thread.Sleep(500);
            SendKeepAlive();

            // verified upto here

            _securityLevel = AccessLevel.AccessLevel01;
            CastInfoEvent("Requesting security access", ActivityType.UploadingBootloader);
            if (!RequestSecurityAccess(2000))
            {
                CastInfoEvent("Failed to get security access", ActivityType.UploadingFlash);
                _stallKeepAlive = false;
                return false;
            }
            Thread.Sleep(500);
            CastInfoEvent("Uploading bootloader", ActivityType.UploadingBootloader);
            if(!UploadBootloaderProg())
            {
                CastInfoEvent("Failed to upload bootloader", ActivityType.UploadingFlash);
                _stallKeepAlive = false;
                return false;
            }
            CastInfoEvent("Starting bootloader", ActivityType.UploadingBootloader);
            // start bootloader in ECU
            //SendKeepAlive();
            Thread.Sleep(500);
            if(!StartBootloader())
            {
                CastInfoEvent("Failed to start bootloader", ActivityType.UploadingFlash);
                _stallKeepAlive = false;
                return false;
            }
            Thread.Sleep(500);
            SendKeepAlive();
            Thread.Sleep(200);

            CastInfoEvent("Erasing flash", ActivityType.StartErasingFlash);
            if (SendrequestDownload(false))
            {
                _needRecovery = true;
                CastInfoEvent("Programming flash", ActivityType.UploadingFlash);
                for (int blockNumber = 0; blockNumber <= 0xF50; blockNumber++)
                {
                    float percentage = ((float)blockNumber * 100) / 3920F;
                    CastProgressWriteEvent(percentage);
                    byte[] data2Send = bm.GetNextBlock();
                    int length = 0xF0;
                    if (blockNumber == 0xF50) length = 0xE6;
                    if (SendTransferData(length, startAddress + (blockNumber * 0xEA), 0x7E8))
                    {
                        // 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))
                            {
                                AddToCanTrace("Couldn't send message");
                            }
                            Thread.Sleep(2);
                        }

                        // 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(1000);
                        ulong data = response.getData();
                        if (getCanData(data, 0) != 0x01 || getCanData(data, 1) != 0x76)
                        {
                            _stallKeepAlive = false;
                            return false;
                        }
                        SendKeepAlive();
                    }
                }
                sw.Stop();
                _needRecovery = false;
                CastInfoEvent("Flash upload completed", ActivityType.FinishedFlashing);
                // what else to do?
                Send0120();
                CastInfoEvent("Session ended", ActivityType.FinishedFlashing);
            }
            else
            {
                sw.Stop();
                _needRecovery = false;
                _stallKeepAlive = false;
                CastInfoEvent("Failed to erase flash", ActivityType.FinishedFlashing);
                Send0120();
                CastInfoEvent("Session ended", ActivityType.FinishedFlashing);
                return false;

            }
            _stallKeepAlive = false;
            return true;
        }
コード例 #3
0
ファイル: T8Can.cs プロジェクト: mattiasclaesson/T8CANLib
        public bool RecoverECU(string filename)
        {
            string diagDataID = GetDiagnosticDataIdentifier0101();
            Console.WriteLine("DataID: " + diagDataID);
            if (diagDataID == string.Empty)
            {
                BlockManager bm = new BlockManager();
                bm.SetFilename(filename);
                int startAddress = 0x020000;
                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);
                            UploadBootloaderProg011();
                            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);
                                for (int blockNumber = 0; blockNumber <= 0xF50; blockNumber++)
                                {
                                    float percentage = ((float)blockNumber * 100) / 3920F;
                                    CastProgressWriteEvent(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))
                                            {
                                                AddToCanTrace("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(1000);
                                        ulong data = response.getData();
                                        if (getCanData(data, 0) != 0x01 || getCanData(data, 1) != 0x76)
                                        {
                                            _stallKeepAlive = false;
                                            return false;
                                        }
                                        BroadcastKeepAlive();
                                    }
                                }
                                sw.Stop();
                                _needRecovery = false;
                                CastInfoEvent("Recovery completed", ActivityType.FinishedFlashing);
                                // what else to do?
                                Send0120();
                                CastInfoEvent("Session ended", ActivityType.FinishedFlashing);
                                return true;
                            }
                            else
                            {
                                sw.Stop();
                                _needRecovery = false;
                                _stallKeepAlive = false;
                                CastInfoEvent("Failed to erase flash", ActivityType.FinishedFlashing);
                                Send0120();
                                CastInfoEvent("Session ended", ActivityType.FinishedFlashing);
                                return false;

                            }
                        }
                    }
                    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);
            }
            return false;
        }