Skip to content

Commit 9d8296a

Browse files
author
Bas Visscher
committed
Fixes #28, verify bootloader message
1 parent ea7cddd commit 9d8296a

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

ESPTool/Communication/Communicator.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
using EspDotNet.Config;
22
using Microsoft.Extensions.Logging;
33
using System;
4+
using System.Diagnostics;
45
using System.IO.Ports;
6+
using System.Text.RegularExpressions;
7+
using System.Text;
58
using System.Threading;
69
using System.Threading.Tasks;
710

@@ -122,6 +125,12 @@ public async Task ExecutePinSequence(List<PinSequenceStep> sequence, Cancellatio
122125
}
123126
}
124127

128+
public async Task<int> ReadRawAsync(byte[] buffer, CancellationToken token)
129+
{
130+
return await _serialPort.BaseStream.ReadAsync(buffer, 0, buffer.Length, token);
131+
}
132+
133+
125134
/// <summary>
126135
/// Disposes the serial port and associated resources.
127136
/// </summary>

ESPTool/Tools/BootloaderTool.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using EspDotNet.Communication;
22
using EspDotNet.Config;
33
using EspDotNet.Loaders.ESP32BootLoader;
4+
using System.Text.RegularExpressions;
5+
using System.Text;
46

57
namespace EspDotNet.Tools
68
{
@@ -20,6 +22,10 @@ public async Task<ESP32BootLoader> StartAsync(CancellationToken token = default)
2022
// Start bootloader
2123
await _communicator.ExecutePinSequence(_bootloaderSequence, token);
2224

25+
// Check bootloader message
26+
if (!await TryReadBootStartup(token))
27+
throw new Exception("Booloader message not verified");
28+
2329
// Instantiate loader and synchronize
2430
var bootloader = new ESP32BootLoader(_communicator);
2531

@@ -29,6 +35,21 @@ public async Task<ESP32BootLoader> StartAsync(CancellationToken token = default)
2935
return bootloader;
3036
}
3137

38+
private async Task<bool> TryReadBootStartup(CancellationToken token)
39+
{
40+
var buffer = new byte[4096];
41+
var read = await _communicator.ReadRawAsync(buffer, token);
42+
if (read > 0)
43+
{
44+
var data = new byte[read];
45+
Array.Copy(buffer, data, read);
46+
Regex regex = new Regex("boot:(0x[0-9a-fA-F]+)(.*waiting for download)?");
47+
var result = regex.Match(Encoding.ASCII.GetString(data));
48+
return result.Success;
49+
}
50+
return false;
51+
}
52+
3253
private async Task<bool> Synchronize(ESP32BootLoader loader, CancellationToken token)
3354
{
3455
for (int tryNo = 0; tryNo < 100; tryNo++)

0 commit comments

Comments
 (0)