private void FlashReadButton_Click(object sender, RoutedEventArgs e) { List <ParameterGrid> pgList = new List <ParameterGrid>(); pgList.Add(new ParameterGrid( "Address", "读取基址", @"^[0-9A-Fa-f]*$", 8, "输入读取Flash基址", FlashReadAddress.ToString("X08"), null ) ); pgList.Add(new ParameterGrid( "Length", "读取长度(KB)", @"^[0-9]*$", 0, "输入读取Flash长度", FlashFileLength.ToString(), null ) ); ParameterWindow pw = new ParameterWindow(this, "读取Flash配置", pgList); if (pw.ShowDialog() != true) { Log.warn("取消读取!"); return; } FlashReadAddress = uint.Parse(pw.ParaValueResult["Address"], NumberStyles.HexNumber); int ReadLength = int.Parse(pw.ParaValueResult["Length"]); ReadLength = (ReadLength / 4 + (ReadLength % 4 == 0 ? 0 : 1)) * 4; FlashFileLength = ReadLength; FlashFileList = new List <byte>(); MessageBox.Show(this, "读取文件从 0x" + FlashReadAddress.ToString("X08") + "到 0x" + (FlashReadAddress + ReadLength * 1024).ToString("X08")); for (uint offset = 0; offset < (FlashFileLength * 1024); offset += 4096) { var cmd = new FlashCmd("Read", FlashReadAddress + offset, null); Serialcon.Cmd.Add(cmd); } Serialcon.Cmd.Start(); }
private void FlashEraseButton_Click(object sender, RoutedEventArgs e) { List <ParameterGrid> pgList = new List <ParameterGrid>(); pgList.Add(new ParameterGrid( "Address", "擦除基址", @"^[0-9A-Fa-f]*$", 8, "输入擦除Flash基址", FlashEraseAddress.ToString("X08"), null ) ); pgList.Add(new ParameterGrid( "Length", "擦除长度(KB)", @"^[0-9]*$", 0, "输入擦除Flash长度", FlashEraseLength.ToString(), null ) ); ParameterWindow pw = new ParameterWindow(this, "擦除Flash配置", pgList); if (pw.ShowDialog() != true) { Log.warn("取消擦除!"); return; } FlashEraseAddress = uint.Parse(pw.ParaValueResult["Address"], NumberStyles.HexNumber); FlashEraseLength = int.Parse(pw.ParaValueResult["Length"]); FlashEraseLength = (FlashEraseLength / 4 + (FlashEraseLength % 4 == 0 ? 0 : 1)) * 4; FlashFileList = new List <byte>(); MessageBox.Show(this, "擦除从 0x" + FlashEraseAddress.ToString("X08") + "到 0x" + (FlashEraseAddress + FlashEraseLength * 1024).ToString("X08")); byte[] EraseLength = BitConverter.GetBytes(FlashEraseLength / 4); Array.Reverse(EraseLength); var cmd = new FlashCmd("Erase", FlashEraseAddress, EraseLength); Serialcon.Cmd.Add(cmd); Serialcon.Cmd.Start(); }
private void DownloadButton_Click(object sender, RoutedEventArgs e) { fw.IsCheckSection = false; if (!fw.FirmwareReady) { Log.error("还未配置可下载固件,下载中止!"); return; } if (MessageBox.Show(this, "将下载0x" + fw.BinFileByte.Length.ToString("X08") + "数据到" + FlashWriteAddress.ToString("X08"), "确认", MessageBoxButton.YesNo) != MessageBoxResult.Yes) { Log.warn("取消下载!"); return; } for (uint offset = 0; offset < fw.BinFileByte.Length; offset += 4096) { byte[] Write4Kbyte = new byte[4096]; Array.Copy(fw.BinFileByte, offset, Write4Kbyte, 0, 4096); bool IsOnlyClear = true; for (uint i = 0; i < 4096; i++) { if (Write4Kbyte[i] != 0xFF) { IsOnlyClear = false; break; } } FlashCmd cmd; if (IsOnlyClear) { byte[] EraseLength = BitConverter.GetBytes(4 / 4); Array.Reverse(EraseLength); cmd = new FlashCmd("Erase", (offset + FlashWriteAddress), EraseLength); } else { cmd = new FlashCmd("Write", (offset + FlashWriteAddress), Write4Kbyte); } Serialcon.Cmd.Add(cmd); } Serialcon.Cmd.Start(); }
private void InitializeFlashCmd() { FlashCmd.RegisterCMD(FlashReadCmd); FlashCmd.RegisterCMD(FlashWriteCmd); FlashCmd.RegisterCMD(FlashEraseCmd); }