public WriteMemory ( uint address, byte buf, int offset, int size, IProgress |
||
address | uint | |
buf | byte | |
offset | int | |
size | int | |
p | IProgress |
|
ct | ||
Résultat | Task |
public async Task WriteMemory(uint address, byte[] buf, int offset,
int size, IProgress<STBootProgress> p, CancellationToken ct)
{
/* number of bytes written */
int bwritten = 0, btotal = size;
/* no support for read? */
if (!Commands.Contains(STCmds.WRITE))
throw new STBootException("Command not supported");
/* data is read in chunks */
while (size > 0 && !ct.IsCancellationRequested) {
/* chunk size */
int csize = Math.Min(size, 256);
/* read a single chunk */
await Write(address, buf, offset, csize);
/* update iterators */
size -= csize; offset += csize; address += (uint)csize;
/* update number of bytes read */
bwritten += csize;
/* report progress */
if (p != null)
p.Report(new STBootProgress(bwritten, btotal));
}
/* throw exception if operation was cancelled */
if (ct.IsCancellationRequested)
throw new OperationCanceledException("Write cancelled");
}
/* 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(); } }