/****************************************************************************** * 向下位机烧写固件 ******************************************************************************/ private async Task UploadFile(string portName, uint baudRate, byte[] bin, uint address, uint jumpAddress) { /* 获得页的大小 */ uint psize = uint.Parse(cbPSize.SelectedItem as string); /* 初始化ST boot对象 */ using (var uc = new STBoot()) { /* 打开设备 */ uc.Open(portName, baudRate); /* 通信初始化 */ await uc.Initialize(); /* 更新状态栏信息 */ UpdateStatus(false, string.Format("Connected: Ver: {0}, PID: 0x{1:X4}", uc.Version, uc.ProductID)); /* 延时一会儿,使状态的信息可以被看见 */ await Task.Delay(500); /* 擦除信息 */ UpdateStatus(false, "Erasing..."); /* 全局擦除是否被勾选 */ if (cbxErase.Checked) { await uc.GlobalErase(); } else { /* 擦除操作 */ for (uint i = 0; i < bin.Length; i += psize) { /* 擦除页 */ await uc.ErasePage((i + address - 0x08000000) / psize); /* 更新精度条 */ UpdateProgress((int)i * 100 / bin.Length); } } /* 状态栏显示 编程 提示信息 */ UpdateStatus(false, "Programming..."); /* 创建进度报告对象 */ var p = new Progress <STBootProgress>(UpdateProgress); /* 写Flash */ await uc.WriteMemory(address, bin, 0, bin.Length, p, CancellationToken.None); /* 烧写成功 */ UpdateStatus(false, string.Format("Success: {0} bytes written", bin.Length)); /* 跳转到用户程序地址! */ await uc.Jump(jumpAddress); /* 结束通信 */ uc.Close(); } }
/* upload a binary image to uC */ private async Task UploadFile(string portName, uint baudRate, byte[] bin, uint address, uint jumpAddress) { /* get page size */ uint psize = uint.Parse(cbPSize.SelectedItem as string); /* create new programming interface object */ using (var uc = new STBoot()) { /* open device */ uc.Open(portName, baudRate); /* initialize communication */ await uc.Initialize(); /* update the status */ UpdateStatus(false, string.Format("Connected: Ver: {0}, PID: 0x{1:X4}", uc.Version, uc.ProductID)); /* give some chance see the message */ await Task.Delay(500); /* apply new message */ UpdateStatus(false, "Erasing..."); /* checked? */ if (cbxErase.Checked) { await uc.GlobalErase(); } else { /* erase operation */ for (uint i = 0; i < bin.Length; i += psize) { /* erase page */ await uc.ErasePage((i + address - 0x08000000) / psize); /* update progress bar */ UpdateProgress((int)i * 100 / bin.Length); } } /* apply new message */ UpdateStatus(false, "Programming..."); /* progress reporter */ var p = new Progress <STBootProgress>(UpdateProgress); /* write memory */ await uc.WriteMemory(address, bin, 0, bin.Length, p, CancellationToken.None); /* update the status */ UpdateStatus(false, string.Format("Success: {0} bytes written", bin.Length)); /* go! */ await uc.Jump(jumpAddress); /* end communication */ uc.Close(); } }