//--------------------------------------------------------------------- // WriteFlash33_1_0_0() //--------------------------------------------------------------------- private static bool WriteFlash33_1_0_0(clsSerialPort pobjPort, clsDevice pobjDevice, clsHex33FJ pobjHex, bool pbWriteProgram, bool pbWriteConfigs, int iTabLevel) { //-------------------------------------------------------------------------- // Variables //-------------------------------------------------------------------------- int iChecksum; int iSum; int iAddrP; int iRetries; byte[] bBuffer = new byte[256]; int iBufferIndx; byte bAddrU, bAddrH, bAddrL, bSize, bProg; bool bRetry = false; int iByteToWrite = pobjHex.BytesToWrite( pobjDevice, pbWriteProgram, false, pbWriteConfigs ); int iBytesWritten = 0; bool bProcessWriteResponseResult = false; //-------------------------------------------------------------------------- // Write flash //-------------------------------------------------------------------------- int iRow; if ( pbWriteProgram == true ) { OnDownloading( new clsDownloadingEventArgs(clsDownloadingEventArgs.EventType.info, "Writing flash...", iTabLevel) ); //------------------------------------------------------------- // Iterate pages //------------------------------------------------------------- for ( int iPage = 0; iPage < clsHex33FJ.iProgPageUsedBufferSize; iPage++ ) { if ( pobjHex.bProgPageUsed[iPage] == true ) { iRetries = 0; do { if ( clsds30Loader.debugMode ) { OnDownloading( new clsDownloadingEventArgs(clsDownloadingEventArgs.EventType.info, "Erasing page " + iPage.ToString(), iTabLevel) ); } //--------------------------------------------------------- // Erase page //--------------------------------------------------------- iSum = 0; iBufferIndx = 0; // Calculate iAddrP = iPage * pobjDevice.pageSizeR * pobjDevice.rowsizeW * 2; //24-bit address in memory to write to bAddrU = Convert.ToByte((iAddrP & 0xff0000) >> 16); //address upper byte bAddrH = Convert.ToByte((iAddrP & 0x00ff00) >> 8); //address high byte bAddrL = Convert.ToByte((iAddrP & 0x0000ff) >> 0); //address low byte bSize = Convert.ToByte(1/*checksum*/ ); // bBuffer[iBufferIndx++] = bAddrU; iSum += bAddrU; bBuffer[iBufferIndx++] = bAddrH; iSum += bAddrH; bBuffer[iBufferIndx++] = bAddrL; iSum += bAddrL; bBuffer[iBufferIndx++] = cCmdErasePage; iSum += cCmdErasePage; bBuffer[iBufferIndx++] = bSize; iSum += bSize; // Calculate checksum and add to buffer iSum %= 256; iChecksum = Convert.ToInt16((256 - iSum) % 256); bBuffer[iBufferIndx++] = Convert.ToByte(iChecksum); // Send address, command, packetsize & checksum pobjPort.SendBytes(ref bBuffer, iBufferIndx); // Get response, checksum ok ProcessWriteResponse( pobjPort, ref iRetries, iTabLevel, "flash", ref bRetry, ref bProcessWriteResponseResult ); if ( bProcessWriteResponseResult == false ) { return false; } // Application.DoEvents(); } while (bRetry == true && bAbort == false); //--------------------------------------------------------- // Iterate rows, write all rows in page even if not in hex-file //--------------------------------------------------------- for ( iRow = iPage * pobjDevice.pageSizeR; iRow < iPage * pobjDevice.pageSizeR + pobjDevice.pageSizeR; iRow++ ) { iRetries = 0; do { if ( clsds30Loader.debugMode ) { OnDownloading( new clsDownloadingEventArgs(clsDownloadingEventArgs.EventType.info, "Writing row " + iRow.ToString(), iTabLevel) ); } iBufferIndx = 0; iSum = 0; // Calculate iAddrP = iRow * (pobjDevice.rowsizeW * 2); //24-bit address in memory to write to bAddrU = Convert.ToByte((iAddrP & 0xff0000) >> 16); //address upper byte bAddrH = Convert.ToByte((iAddrP & 0x00ff00) >> 8); //address high byte bAddrL = Convert.ToByte((iAddrP & 0x0000ff)); //address low byte bSize = Convert.ToByte(pobjDevice.rowsizeW * 3 + 1/*checksum*/ ); // bBuffer[iBufferIndx++] = bAddrU; iSum += bAddrU; bBuffer[iBufferIndx++] = bAddrH; iSum += bAddrH; bBuffer[iBufferIndx++] = bAddrL; iSum += bAddrL; bBuffer[iBufferIndx++] = cCmdWriteRow; iSum += cCmdWriteRow; bBuffer[iBufferIndx++] = bSize; iSum += bSize; // Calculate startindex in hex-buffer iAddrP = iRow * pobjDevice.rowsizeW * 3; // Buffer row, upper-low-high, ordered in ParseHex() for ( int iByte = 0; iByte < pobjDevice.rowsizeW * 3; iByte++ ) { bProg = Convert.ToByte( pobjHex.iProgMem[iAddrP + iByte] ); bBuffer[ iBufferIndx++ ] = bProg; iSum = (iSum + bProg) % 256; } // Calculate checksum and add to buffer iSum %= 256; iChecksum = Convert.ToInt16((256 - iSum) % 256); bBuffer[iBufferIndx++] = Convert.ToByte(iChecksum); // Send address, command, packetsize, row & checksum pobjPort.SendBytes(ref bBuffer, iBufferIndx); // Get response, checksum ok ProcessWriteResponse( pobjPort, ref iRetries, iTabLevel, "flash", ref bRetry, ref bProcessWriteResponseResult ); if ( bProcessWriteResponseResult == false ) { return false; } // Application.DoEvents(); } while ( bRetry == true && bAbort == false ); // if (bAbort == true) { OnDownloading( new clsDownloadingEventArgs(clsDownloadingEventArgs.EventType.error, "aborted by user", -1) ); return false; } // iBytesWritten += pobjDevice.rowsizeW * 3; OnDownloading( new clsDownloadingEventArgs(clsDownloadingEventArgs.EventType.progress, "", (100 * iBytesWritten) / iByteToWrite ) ); }//for ( iRow = 0; iRow < iRowUsedBufferSize; iRow++ ) { } //if ( bPageUsed[iIter] == true ) { }//for ( iPage = 0; iPage < iPageUsedBufferSize; iPage++ ) { OnDownloading( new clsDownloadingEventArgs(clsDownloadingEventArgs.EventType.success, "ok", -1) ); }//if ( pbWriteProgram == true ) { //-------------------------------------------------------------------------- // Write Config Bits //-------------------------------------------------------------------------- if ( pbWriteConfigs == true ) { int iConfig; int iByte; OnDownloading( new clsDownloadingEventArgs(clsDownloadingEventArgs.EventType.info, "Writing configs...", iTabLevel) ); iRetries = 0; for ( iConfig = 0; iConfig < clsHex33FJ.iConfigWordsUsedBufferSize; iConfig++ ) { if ( pobjHex.bConfigWordUsed[iConfig] == true ) { iRetries = 0; do { for ( iByte = 0; iByte < 2; iByte++ ) { if ( clsds30Loader.debugMode ) { OnDownloading( new clsDownloadingEventArgs(clsDownloadingEventArgs.EventType.info, "Writing config byte " + (iConfig*2 + iByte).ToString(), iTabLevel) ); } iBufferIndx = 0; iSum = 0; // Calculate iAddrP = 0xF80000 + iConfig * 2 + iByte; //24-bit address in memory to write to bAddrU = Convert.ToByte( (iAddrP & 0xff0000) >> 16 ); //address upper byte bAddrH = Convert.ToByte( (iAddrP & 0x00ff00) >> 8 ); //address high byte bAddrL = Convert.ToByte( (iAddrP & 0x0000ff) ); //address low byte bSize = Convert.ToByte( 1 + 1/*checksum*/ ); // bBuffer[ iBufferIndx++ ] = bAddrU; iSum += bAddrU; bBuffer[ iBufferIndx++ ] = bAddrH; iSum += bAddrH; bBuffer[ iBufferIndx++ ] = bAddrL; iSum += bAddrL; bBuffer[ iBufferIndx++ ] = cCmdWriteConfig;iSum += cCmdWriteConfig; bBuffer[ iBufferIndx++ ] = bSize; iSum += bSize; // Buffer config word bProg = Convert.ToByte( pobjHex.iConfigMem[ iConfig * 2 + iByte ] ); bBuffer[ iBufferIndx++ ] = bProg; iSum = (iSum + bProg) % 256; // Calculate checksum and add to buffer iSum %= 256; iChecksum = Convert.ToInt16( (256 - iSum) % 256 ); bBuffer[ iBufferIndx++ ] = Convert.ToByte(iChecksum); // Send row+checksum pobjPort.SendBytes( ref bBuffer, iBufferIndx ); // Get response, checksum ok ProcessWriteResponse( pobjPort, ref iRetries, iTabLevel, "flash", ref bRetry, ref bProcessWriteResponseResult ); if ( bProcessWriteResponseResult == false ) { return false; } } Application.DoEvents(); } while ( bRetry == true && bAbort == false ); // if ( bAbort == true ) { OnDownloading( new clsDownloadingEventArgs(clsDownloadingEventArgs.EventType.error, "aborted by user", -1) ); return false; } // iBytesWritten += 2; OnDownloading( new clsDownloadingEventArgs(clsDownloadingEventArgs.EventType.progress, "", (100 * iBytesWritten) / iByteToWrite ) ); }//if ( bConfigUsed[iIter] == true ) { }//for ( iIter = 0; iIter < iConfigUsedBufferSize; iIter++ ) { OnDownloading( new clsDownloadingEventArgs(clsDownloadingEventArgs.EventType.success, "ok", -1) ); }//if ( bWriteConfigs == true ) //-------------------------------------------------------------------------- // Write completed //-------------------------------------------------------------------------- return true; }
//--------------------------------------------------------------------- // ResetDevice() //--------------------------------------------------------------------- private static void ResetDevice( clsSerialPort pobjPort, clsDownloadSettings pobjSettings, int iTabLevel, ref bool pbResult ) { pbResult = false; //-------------------------------------------------------------------------- // Reset - command //-------------------------------------------------------------------------- if ( pobjSettings.resetCommand == true ) { // Inform user OnDownloading( new clsDownloadingEventArgs(clsDownloadingEventArgs.EventType.info, "Sending reset command", iTabLevel) ); // Parse resetcommand bool bSplitResult = false; byte[] bBytes = pobjPort.SplitHexStringToBytes( pobjSettings.resetCommandSequence, ref bSplitResult ); if ( bSplitResult == false ) { OnDownloading( new clsDownloadingEventArgs(clsDownloadingEventArgs.EventType.error, "Couldn't parse reset command sequence", iTabLevel) ); return; } /*string[] strBytes = pobjSettings.resetCommandSequence.Split( ';' ); byte[] bBytes = new byte[ strBytes.Length ]; int iIndex = 0; foreach ( string strByte in strBytes ) { bBytes[iIndex] = byte.Parse( strByte, System.Globalization.NumberStyles.HexNumber ); ++iIndex; }*/ // Open port int iOldBaudrate = pobjPort.baudrate; pobjPort.baudrate = pobjSettings.resetBaudrate; bool bOpenPortResult = false; PortOpen( pobjPort, pobjSettings, ref bOpenPortResult ); // Send reset command and close port pobjPort.SendBytes( ref bBytes, bBytes.Length ); while ( pobjPort.outBufferCount > 0 ); pobjPort.Close(); // Restore baudrate pobjPort.baudrate = iOldBaudrate; SleepResponsive( pobjSettings.resetTime ); //-------------------------------------------------------------------------- // Reset - dtr //-------------------------------------------------------------------------- } else if ( pobjSettings.resetDtr == true ) { OnDownloading( new clsDownloadingEventArgs(clsDownloadingEventArgs.EventType.info, "Resetting by dtr", iTabLevel) ); pobjPort.dtrEnable = true; SleepResponsive( pobjSettings.resetTime ); pobjPort.dtrEnable = false; //-------------------------------------------------------------------------- // Reset - rts //-------------------------------------------------------------------------- } else if ( pobjSettings.resetRts == true ) { OnDownloading( new clsDownloadingEventArgs(clsDownloadingEventArgs.EventType.info, "Resetting by rts", iTabLevel) ); pobjPort.rtsEnable = true; SleepResponsive( pobjSettings.resetTime ); pobjPort.rtsEnable = false; } pbResult = true; }
//--------------------------------------------------------------------- // WriteFlash30_1_0_1() //--------------------------------------------------------------------- private static bool WriteFlash30_1_0_1( clsSerialPort pobjPort, clsDevice pobjDevice, clsHex30F pobjHex, bool pbWriteProgram, bool pbWriteEEPROM, bool pbWriteConfigs, int iTabLevel ) { //-------------------------------------------------------------------------- // Variables //-------------------------------------------------------------------------- int iIter2; int iChecksum; int iSum; int iAddrP; int iRetries; byte [] bBuffer = new byte [256]; int iBufferIndx; byte bAddrU, bAddrH, bAddrL, bSize, bProg; bool bRetry = false; int iByteToWrite = pobjHex.BytesToWrite( pobjDevice, pbWriteProgram, pbWriteEEPROM, pbWriteConfigs ); int iBytesWritten = 0; bool bProcessWriteResponseResult = false; //-------------------------------------------------------------------------- // Write flash //-------------------------------------------------------------------------- if ( pbWriteProgram == true ) { OnDownloading( new clsDownloadingEventArgs(clsDownloadingEventArgs.EventType.info, "Writing flash...", iTabLevel) ); //------------------------------------------------------------- // Iterate rows //------------------------------------------------------------- for ( int iRow = 0; iRow < clsHex30F.iProgRowsUsedBufferSize; iRow++ ) { if ( pobjHex.bProgRowUsed[iRow] == true ) { iRetries = 0; do { if ( clsds30Loader.debugMode ) { OnDownloading( new clsDownloadingEventArgs(clsDownloadingEventArgs.EventType.info, "Writing row " + iRow.ToString(), iTabLevel) ); } iBufferIndx = 0; iSum = 0; // Calculate iAddrP = iRow * (pobjDevice.rowsizeW * 2); //24-bit address in memory to write to bAddrU = Convert.ToByte( (iAddrP & 0xff0000) >> 16 ); //address upper byte bAddrH = Convert.ToByte( (iAddrP & 0x00ff00) >> 8 ); //address high byte bAddrL = Convert.ToByte( (iAddrP & 0x0000ff) ); //address low byte bSize = Convert.ToByte( pobjDevice.rowsizeW * 3 + 1/*checksum*/ ); // bBuffer[ iBufferIndx++ ] = bAddrU; iSum += bAddrU; bBuffer[ iBufferIndx++ ] = bAddrH; iSum += bAddrH; bBuffer[ iBufferIndx++ ] = bAddrL; iSum += bAddrL; bBuffer[ iBufferIndx++ ] = bSize; iSum += bSize; // Calculate startindex in hex-buffer iAddrP = iRow * pobjDevice.rowsizeW * 3; // Buffer row, upper-low-high, ordered in ParseHex() for ( int iByte = 0; iByte < pobjDevice.rowsizeW*3; iByte++ ) { bProg = Convert.ToByte( pobjHex.iProgMem[iAddrP + iByte] ); bBuffer[ iBufferIndx++ ] = bProg; iSum = (iSum + bProg) % 256; } // Calculate checksum and add to buffer iSum %= 256; iChecksum = Convert.ToInt16( (256 - iSum) % 256 ); bBuffer[ iBufferIndx++ ] = Convert.ToByte(iChecksum); // Send address, command, packetsize, row & checksum pobjPort.SendBytes( ref bBuffer, iBufferIndx ); // Get response, checksum ok ProcessWriteResponse( pobjPort, ref iRetries, iTabLevel, "flash", ref bRetry, ref bProcessWriteResponseResult ); if ( bProcessWriteResponseResult == false ) { return false; } // Application.DoEvents(); } while ( bRetry == true && bAbort == false ); // if ( bAbort == true ) { OnDownloading( new clsDownloadingEventArgs(clsDownloadingEventArgs.EventType.error, "aborted by user", -1) ); return false; } // iBytesWritten += pobjDevice.rowsizeW * 3; OnDownloading( new clsDownloadingEventArgs(clsDownloadingEventArgs.EventType.progress, "", (100 * iBytesWritten) / iByteToWrite ) ); } //if ( bProgRowUsed[iIter] == true ) { }//for ( iRow = 0; iRow < iProgRowUsedBufferSize; iRow++ ) { OnDownloading( new clsDownloadingEventArgs(clsDownloadingEventArgs.EventType.success, "ok", -1) ); }//if ( pbWriteProgram == true ) { //-------------------------------------------------------------------------- // Write EEPROM //-------------------------------------------------------------------------- if ( pbWriteEEPROM == true ) { OnDownloading( new clsDownloadingEventArgs(clsDownloadingEventArgs.EventType.info, "Writing eeprom...", iTabLevel) ); for ( int iEEPROMWord = 0; iEEPROMWord < pobjDevice.eepromSizeB/2; iEEPROMWord++ ) { if ( pobjHex.bEEWordUsed[iEEPROMWord] == true ) { iRetries = 0; do { if ( clsds30Loader.debugMode ) { OnDownloading( new clsDownloadingEventArgs(clsDownloadingEventArgs.EventType.info, "Writing eeprom word " + iEEPROMWord.ToString(), iTabLevel) ); } iBufferIndx = 0; iSum = 0; // Calculate iAddrP = pobjDevice.eepromStartAddress + iEEPROMWord * 2; //24-bit address in memory to write to bAddrU = Convert.ToByte( (iAddrP & 0xff0000) >> 16 ); //address upper byte bAddrH = Convert.ToByte( (iAddrP & 0x00ff00) >> 8 ); //address high byte bAddrL = Convert.ToByte( (iAddrP & 0x0000ff) ); //address low byte bSize = Convert.ToByte( 2 + 1/*checksum*/ ); // bBuffer[ iBufferIndx++ ] = bAddrU; iSum += bAddrU; bBuffer[ iBufferIndx++ ] = bAddrH; iSum += bAddrH; bBuffer[ iBufferIndx++ ] = bAddrL; iSum += bAddrL; bBuffer[ iBufferIndx++ ] = bSize; iSum += bSize; // Low byte bProg = Convert.ToByte( pobjHex.iEEMem[iEEPROMWord*2 + 0] ); bBuffer[ iBufferIndx++ ] = bProg; iSum = (iSum + bProg) % 256; // High byte bProg = Convert.ToByte( pobjHex.iEEMem[iEEPROMWord*2 + 1] ); bBuffer[ iBufferIndx++ ] = bProg; iSum = (iSum + bProg) % 256; // Calculate checksum and add to buffer iSum %= 256; iChecksum = Convert.ToInt16( (256 - iSum) % 256 ); bBuffer[ iBufferIndx++ ] = Convert.ToByte(iChecksum); // Send row+checksum pobjPort.SendBytes( ref bBuffer, iBufferIndx ); // Get response, checksum ok ProcessWriteResponse( pobjPort, ref iRetries, iTabLevel, "flash", ref bRetry, ref bProcessWriteResponseResult ); if ( bProcessWriteResponseResult == false ) { return false; } // Application.DoEvents(); } while ( bRetry == true && bAbort == false ); // if ( bAbort == true ) { OnDownloading( new clsDownloadingEventArgs(clsDownloadingEventArgs.EventType.error, "aborted by user", -1) ); return false; } // iBytesWritten += 2; OnDownloading( new clsDownloadingEventArgs(clsDownloadingEventArgs.EventType.progress, "", (100 * iBytesWritten) / iByteToWrite ) ); }//if ( bEEWordUsed[iEEPROMWord] == true ) { }//for ( iIter = 0; iIter < pobjDevice.eepromSizeB/2; iIter++ ) { OnDownloading( new clsDownloadingEventArgs(clsDownloadingEventArgs.EventType.success, "ok", -1) ); }//if ( pbWriteEEPROM == true ) { //-------------------------------------------------------------------------- // Write Config Bits //-------------------------------------------------------------------------- if ( pbWriteConfigs == true ) { OnDownloading( new clsDownloadingEventArgs(clsDownloadingEventArgs.EventType.info, "Writing configs...", iTabLevel) ); for ( int iConfig = 0; iConfig < clsHex30F.iConfigWordsUsedBufferSize; iConfig++ ) { if ( pobjHex.bConfigWordUsed[iConfig] == true ) { iRetries = 0; do { if ( clsds30Loader.debugMode ) { OnDownloading( new clsDownloadingEventArgs(clsDownloadingEventArgs.EventType.info, "Writing config word " + iConfig.ToString(), iTabLevel) ); } iBufferIndx = 0; iSum = 0; // Calculate iAddrP = 0xF80000 + iConfig * 2; //24-bit address in memory to write to bAddrU = Convert.ToByte( (iAddrP & 0xff0000) >> 16 ); //address upper byte bAddrH = Convert.ToByte( (iAddrP & 0x00ff00) >> 8 ); //address high byte bAddrL = Convert.ToByte( (iAddrP & 0x0000ff) ); //address low byte bSize = Convert.ToByte( 2 + 1/*checksum*/ ); // bBuffer[ iBufferIndx++ ] = bAddrU; iSum += bAddrU; bBuffer[ iBufferIndx++ ] = bAddrH; iSum += bAddrH; bBuffer[ iBufferIndx++ ] = bAddrL; iSum += bAddrL; bBuffer[ iBufferIndx++ ] = bSize; iSum += bSize; // Buffer config word for ( iIter2 = 0; iIter2 < 2; iIter2++ ) { bProg = Convert.ToByte( pobjHex.iConfigMem[ iConfig * 2 + iIter2 ] ); bBuffer[ iBufferIndx++ ] = bProg; iSum = (iSum + bProg) % 256; } // Calculate checksum and add to buffer iSum %= 256; iChecksum = Convert.ToInt16( (256 - iSum) % 256 ); bBuffer[ iBufferIndx++ ] = Convert.ToByte(iChecksum); // Send row+checksum pobjPort.SendBytes( ref bBuffer, iBufferIndx ); // Get response, checksum ok ProcessWriteResponse( pobjPort, ref iRetries, iTabLevel, "flash", ref bRetry, ref bProcessWriteResponseResult ); if ( bProcessWriteResponseResult == false ) { return false; } // Application.DoEvents(); } while ( bRetry == true && bAbort == false ); // if ( bAbort == true ) { OnDownloading( new clsDownloadingEventArgs(clsDownloadingEventArgs.EventType.error, "aborted by user", -1) ); return false; } // iBytesWritten += 2; OnDownloading( new clsDownloadingEventArgs(clsDownloadingEventArgs.EventType.progress, "", (100 * iBytesWritten) / iByteToWrite ) ); }//if ( bConfigWordUsed[iIter] == true ) { }//for ( iIter = 0; iIter < iConfigUsedBufferSize; iIter++ ) { OnDownloading( new clsDownloadingEventArgs(clsDownloadingEventArgs.EventType.success, "ok", -1) ); }//if ( bWriteConfigs == true ) //-------------------------------------------------------------------------- // Write completed //-------------------------------------------------------------------------- return true; }