Skip to content

Commit b7754a1

Browse files
2 parents 62787c4 + b1d8046 commit b7754a1

16 files changed

+1178
-724
lines changed

BK7231Flasher/BK7231Flasher.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -978,6 +978,9 @@ bool doEraseInternal(int startSector, int sectors)
978978
int deviceMID;
979979
BKFlash flashInfo;
980980

981+
public BK7231Flasher(CancellationToken ct) : base(ct)
982+
{
983+
}
981984

982985
bool doUnprotect()
983986
{

BK7231Flasher/BK7231Flasher.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
<Reference Include="System.Configuration" />
5757
</ItemGroup>
5858
<ItemGroup>
59+
<Compile Include="RDAFlasher.cs" />
5960
<Compile Include="Xmodem.cs" />
6061
<Compile Include="WMFlasher.cs" />
6162
<Compile Include="ECR6600Flasher.cs" />

BK7231Flasher/BL602Flasher.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,11 @@ public bool doReadInternal(int addr = 0, int amount = 0x200000) {
457457
return false;
458458
}
459459
MemoryStream ms;
460+
461+
public BL602Flasher(CancellationToken ct) : base(ct)
462+
{
463+
}
464+
460465
public override byte[] getReadResult()
461466
{
462467
return ms?.ToArray();

BK7231Flasher/BaseFlasher.cs

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using System;
22
using System.Drawing;
33
using System.IO.Ports;
4+
using System.Threading;
5+
using System.Threading.Tasks;
46

57
namespace BK7231Flasher
68
{
@@ -18,11 +20,14 @@ public enum BKType
1820
RTL8710B,
1921
RTL87X0C,
2022
RTL8720D,
23+
RTL8721DA,
24+
RTL8720E,
2125
LN882H,
2226
BL602,
2327
ECR6600,
2428
W600,
2529
W800,
30+
RDA5981,
2631

2732
BekenSPI,
2833
GenericSPI,
@@ -56,7 +61,7 @@ public override string ToString()
5661
}
5762
}
5863

59-
public class BaseFlasher
64+
public class BaseFlasher : IDisposable
6065
{
6166
protected ILogListener logger;
6267
protected string backupName;
@@ -70,6 +75,24 @@ public class BaseFlasher
7075
protected string serialName;
7176
protected BKType chipType = BKType.BK7231N;
7277
protected int baudrate = 921600;
78+
protected CancellationToken cancellationToken;
79+
protected XMODEM xm;
80+
protected bool isCancelled = false;
81+
82+
public BaseFlasher(CancellationToken ct)
83+
{
84+
cancellationToken = ct;
85+
ct.Register(() =>
86+
{
87+
isCancelled = true;
88+
if(xm != null)
89+
{
90+
xm?.CancelFileTransfer();
91+
xm.InProgress.Wait(500);
92+
}
93+
closePort();
94+
});
95+
}
7396

7497
public void setBasic(ILogListener logger, string serialName, BKType bkType, int baudrate = 921600)
7598
{
@@ -181,7 +204,11 @@ public virtual bool doErase(int startSector = 0x000, int sectors = 10, bool bAll
181204
}
182205
public virtual void closePort()
183206
{
184-
207+
if (serial != null)
208+
{
209+
serial.Close();
210+
serial.Dispose();
211+
}
185212
}
186213
public virtual void doTestReadWrite(int startSector = 0x000, int sectors = 10)
187214
{
@@ -194,6 +221,18 @@ public virtual bool saveReadResult(int startOffset)
194221
{
195222
return false;
196223
}
224+
225+
public virtual void Xm_PacketSent(int sentBytes, int total, int sequence, uint offset)
226+
{
227+
if((sequence % 4) == 1)
228+
{
229+
addLog($"Writing at 0x{offset:X}... ");
230+
}
231+
232+
logger.setProgress(sentBytes, total);
233+
}
234+
235+
public virtual void Dispose() { }
197236
}
198237
}
199238

BK7231Flasher/ECR6600Flasher.cs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,13 @@ namespace BK7231Flasher
1212
public class ECR6600Flasher : BaseFlasher
1313
{
1414
MemoryStream ms;
15-
int flashSizeMB = 2;
15+
16+
public ECR6600Flasher(CancellationToken ct) : base(ct)
17+
{
18+
}
19+
1620
//byte[] flashID;
21+
//int flashSizeMB = 2;
1722

1823
bool doGenericSetup()
1924
{
@@ -210,10 +215,6 @@ byte[] ExecuteCommand(int type, byte[] parms = null,
210215
return null;
211216
}
212217
byte crcret = StubCRC8(bytes, bytes.Length - 1);
213-
//for(int k = 0; k < bytes.Length - 1; k++)
214-
//{
215-
// crcret += bytes[k];
216-
//}
217218
if(crcret != bytes[bytes.Length - 1])
218219
{
219220
addErrorLine("Command CRC is incorrect!");
@@ -323,6 +324,7 @@ private byte[] InternalRead(int addr, int sectors)
323324
int retries = 5;
324325
while(retries-- > 0)
325326
{
327+
if(isCancelled) return null;
326328
addLog($"0x{addr:X}... ");
327329
res = ExecuteCommand(0x03, msg, 2, length);
328330
if(res != null)
@@ -459,7 +461,7 @@ public override bool doErase(int startSector = 0x000, int sectors = 10, bool bAl
459461
msg[5] = (byte)((length >> 8) & 0xFF);
460462
msg[6] = (byte)((length >> 16) & 0xFF);
461463
msg[7] = (byte)((length >> 24) & 0xFF);
462-
var t = ExecuteCommand(0x04, msg, 10);
464+
return ExecuteCommand(0x04, msg, 10) != null;
463465
}
464466
return false;
465467
}
@@ -552,7 +554,7 @@ public override void doReadAndWrite(int startSector, int sectors, string sourceF
552554
InternalWrite(startSector, data);
553555
}
554556
}
555-
if((rwMode == WriteMode.OnlyWrite || rwMode == WriteMode.ReadAndWrite || rwMode == WriteMode.OnlyOBKConfig) && cfg != null)
557+
if((rwMode == WriteMode.OnlyWrite || rwMode == WriteMode.ReadAndWrite || rwMode == WriteMode.OnlyOBKConfig) && cfg != null && !isCancelled)
556558
{
557559
if(cfg != null)
558560
{
@@ -579,6 +581,11 @@ public override void doReadAndWrite(int startSector, int sectors, string sourceF
579581
{
580582
efdata = EasyFlash.SaveValueToNewEasyFlash("ObkCfg", cfgData, areaSize, chipType);
581583
}
584+
if(efdata == null)
585+
{
586+
addLog("Something went wrong with EasyFlash" + Environment.NewLine);
587+
return;
588+
}
582589
addLog("Now will also write OBK config..." + Environment.NewLine);
583590
addLog("Long name from CFG: " + cfg.longDeviceName + Environment.NewLine);
584591
addLog("Short name from CFG: " + cfg.shortDeviceName + Environment.NewLine);

BK7231Flasher/FormMain.cs

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using System.Net.Security;
1111
using System.Security.Cryptography.X509Certificates;
1212
using System.Threading;
13+
using System.Threading.Tasks;
1314
using System.Windows.Forms;
1415

1516
namespace BK7231Flasher
@@ -21,6 +22,7 @@ public partial class FormMain : Form, ILogListener
2122
int chosenBaudRate;
2223
string chosenSourceFile;
2324
FormCustom formCustom;
25+
CancellationTokenSource cts;
2426

2527
public FormMain()
2628
{
@@ -198,11 +200,14 @@ private void Form1_Load(object sender, EventArgs e)
198200
comboBoxChipType.Items.Add(new ChipType(BKType.RTL8710B, "RTL8710B"));
199201
comboBoxChipType.Items.Add(new ChipType(BKType.RTL87X0C, "RTL87X0C"));
200202
comboBoxChipType.Items.Add(new ChipType(BKType.RTL8720D, "RTL8720DN"));
203+
//comboBoxChipType.Items.Add(new ChipType(BKType.RTL8721DA, "RTL8721DA"));
204+
//comboBoxChipType.Items.Add(new ChipType(BKType.RTL8720E, "RTL8720E"));
201205
comboBoxChipType.Items.Add(new ChipType(BKType.LN882H, "LN882H"));
202206
comboBoxChipType.Items.Add(new ChipType(BKType.BL602, "BL602"));
203207
comboBoxChipType.Items.Add(new ChipType(BKType.ECR6600, "ECR6600"));
204208
comboBoxChipType.Items.Add(new ChipType(BKType.W800, "W800"));
205209
comboBoxChipType.Items.Add(new ChipType(BKType.W600, "W600 (write)"));
210+
comboBoxChipType.Items.Add(new ChipType(BKType.RDA5981, "RDA5981"));
206211
comboBoxChipType.Items.Add(new ChipType(BKType.BekenSPI, "Beken SPI CH341"));
207212
comboBoxChipType.Items.Add(new ChipType(BKType.GenericSPI, "Generic SPI CH341"));
208213

@@ -381,7 +386,7 @@ public void addLog(string s, Color col)
381386
// Singleton.buttonRead.Text = s;
382387
// });
383388
//}
384-
Thread worker;
389+
Task worker;
385390
string getSelectedSerialName()
386391
{
387392
if (comboBoxUART.SelectedIndex != -1)
@@ -416,7 +421,8 @@ bool interruptIfRequired()
416421
{
417422
if(worker != null)
418423
{
419-
worker.Abort();
424+
cts?.Cancel();
425+
//worker.Abort();
420426
}
421427
worker = null;
422428
//setButtonReadLabel(label_startRead);
@@ -473,7 +479,9 @@ void clearUp()
473479
{
474480
if (flasher != null)
475481
{
476-
flasher.closePort();
482+
cts.Cancel();
483+
flasher.Dispose();
484+
//flasher.closePort();
477485
flasher = null;
478486
}
479487
}
@@ -484,32 +492,37 @@ void createFlasher()
484492
{
485493
case BKType.RTL8710B:
486494
case BKType.RTL8720D:
487-
flasher = new RTLFlasher();
495+
case BKType.RTL8721DA:
496+
case BKType.RTL8720E:
497+
flasher = new RTLFlasher(cts.Token);
488498
break;
489499
case BKType.RTL87X0C:
490-
flasher = new RTLZ2Flasher();
500+
flasher = new RTLZ2Flasher(cts.Token);
491501
break;
492502
case BKType.LN882H:
493-
flasher = new LN882HFlasher();
503+
flasher = new LN882HFlasher(cts.Token);
494504
break;
495505
case BKType.BL602:
496-
flasher = new BL602Flasher();
506+
flasher = new BL602Flasher(cts.Token);
497507
break;
498508
case BKType.BekenSPI:
499-
flasher = new SPIFlasher_Beken();
509+
flasher = new SPIFlasher_Beken(cts.Token);
500510
break;
501511
case BKType.GenericSPI:
502-
flasher = new SPIFlasher();
512+
flasher = new SPIFlasher(cts.Token);
503513
break;
504514
case BKType.ECR6600:
505-
flasher = new ECR6600Flasher();
515+
flasher = new ECR6600Flasher(cts.Token);
506516
break;
507517
case BKType.W600:
508518
case BKType.W800:
509-
flasher = new WMFlasher();
519+
flasher = new WMFlasher(cts.Token);
520+
break;
521+
case BKType.RDA5981:
522+
flasher = new RDAFlasher(cts.Token);
510523
break;
511524
default:
512-
flasher = new BK7231Flasher();
525+
flasher = new BK7231Flasher(cts.Token);
513526
break;
514527
}
515528
flasher.setBasic(this, serialName, curType, chosenBaudRate);
@@ -894,7 +907,7 @@ public bool checkFirmwareForCurType(string s)
894907
break;
895908
case BKType.W600:
896909
case BKType.W800:
897-
if(s.StartsWith($"Open{curType}_") && s.EndsWith(".fls"))
910+
if(s.StartsWith($"Open{curType}_") && (s.EndsWith(".fls") || s.EndsWith(".bin")))
898911
{
899912
return true;
900913
}
@@ -1390,16 +1403,20 @@ public void doCustomRead(CustomParms customRead)
13901403
startWorkerThread(readThread, customRead);
13911404
}
13921405

1393-
void startWorkerThread(ParameterizedThreadStart ts, object customArg)
1406+
void startWorkerThread(Action<object> ts, object customArg)
13941407
{
1408+
cts?.Dispose();
1409+
cts = new CancellationTokenSource();
13951410
setButtonStates(false);
1396-
worker = new Thread(ts);
1397-
worker.Start(customArg);
1411+
worker = new Task(ts, customArg, cts.Token);
1412+
worker.Start();
13981413
}
1399-
void startWorkerThread(ThreadStart ts)
1414+
void startWorkerThread(Action ts)
14001415
{
1416+
cts?.Dispose();
1417+
cts = new CancellationTokenSource();
14011418
setButtonStates(false);
1402-
worker = new Thread(ts);
1419+
worker = new Task(ts, cts.Token);
14031420
worker.Start();
14041421
}
14051422
private void buttonStartScan_Click(object sender, EventArgs e)

BK7231Flasher/LN882HFlasher.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ namespace BK7231Flasher
1010
{
1111
public class LN882HFlasher : BaseFlasher
1212
{
13-
private SerialPort serial;
1413
int timeoutMs = 10000;
1514
int flashSizeMB = 2;
1615
byte[] flashID;
@@ -391,6 +390,11 @@ public bool doReadInternal(int startSector = 0x000, int size = 0x1000, bool full
391390
return result;
392391
}
393392
MemoryStream ms;
393+
394+
public LN882HFlasher(CancellationToken ct) : base(ct)
395+
{
396+
}
397+
394398
public override byte[] getReadResult()
395399
{
396400
return ms?.GetBuffer();

BK7231Flasher/OBKConfig.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ internal bool loadFrom(byte [] dat, BKType type, bool bApplyOffset)
110110
case BKType.RTL8720D:
111111
case BKType.BL602:
112112
case BKType.ECR6600:
113+
case BKType.RDA5981:
113114
_ = OBKFlashLayout.getConfigLocation(type, out var sectors);
114115
var sname = type == BKType.BL602 ? "mY0bcFg" : "ObkCfg";
115116
dat = EasyFlash.LoadValueFromData(subArray, sname, sectors * BK7231Flasher.SECTOR_SIZE, type, out efdata);
@@ -577,8 +578,10 @@ public static bool isValid(byte [] raw, int extraOfs = 0, BKType type = BKType.I
577578
case BKType.RTL8720D:
578579
case BKType.LN882H:
579580
case BKType.BL602:
581+
case BKType.RDA5981:
580582
crc = CRC.Tiny_CRC8_unsigned(raw, extraOfs + 4, useLen - 4);
581583
break;
584+
case BKType.W600:
582585
case BKType.W800:
583586
useLen = getLenForVersion(3);
584587
crc = CRC.Tiny_CRC8_unsigned(raw, extraOfs + 4, useLen - 4);
@@ -615,8 +618,10 @@ public void saveConfig(BKType type = BKType.Invalid)
615618
case BKType.RTL8720D:
616619
case BKType.LN882H:
617620
case BKType.BL602:
621+
case BKType.RDA5981:
618622
crc = CRC.Tiny_CRC8_unsigned(raw, 4, realLen - 4);
619623
break;
624+
case BKType.W600:
620625
case BKType.W800:
621626
realLen = getLenForVersion(3);
622627
crc = CRC.Tiny_CRC8_unsigned(raw, 4, realLen - 4);

BK7231Flasher/OBKFlashLayout.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ public static int getConfigLocation(BKType type, out int sectors)
3737
case BKType.ECR6600:
3838
sectors = 0x1C000 / BK7231Flasher.SECTOR_SIZE;
3939
return 0x1B9000;
40+
case BKType.RDA5981:
41+
sectors = 0xA000 / BK7231Flasher.SECTOR_SIZE;
42+
return 0xF4000;
4043
//case BKType.XR809:
4144
// sectors = 0x10000 / BK7231Flasher.SECTOR_SIZE;
4245
// return 0x1E0000;

0 commit comments

Comments
 (0)