Skip to content

Commit 1f68195

Browse files
author
Bas Visscher
committed
Fixes #34, Flash read tool works
1 parent 4356c36 commit 1f68195

File tree

5 files changed

+72
-256
lines changed

5 files changed

+72
-256
lines changed

ESPTool/Communication/Communicator.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,5 +154,10 @@ public void Dispose()
154154
}
155155
_serialPort.Dispose();
156156
}
157+
158+
internal async Task FlushAsync(CancellationToken token)
159+
{
160+
await _serialPort.BaseStream.FlushAsync(token);
161+
}
157162
}
158163
}

ESPTool/ESPToolbox.cs

Lines changed: 1 addition & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -93,60 +93,7 @@ public async Task<byte[]> ReadEfuseAsync(ILoader loader, ChipTypes chipType, EFl
9393
public ReadFlashTool CreateReadFlashTool(Communicator communicator, SoftLoader softLoader, ChipTypes chipType)
9494
{
9595
var deviceConfig = GetDeviceConfig(chipType);
96-
return new ReadFlashTool(softLoader, communicator)
97-
{
98-
SectorSize = (uint)deviceConfig.FlashBlockSize,
99-
BlockSize = 64 // Default read block size
100-
};
101-
}
102-
103-
public async Task ReadFlashAsync(
104-
ReadFlashTool readTool,
105-
uint address,
106-
uint size,
107-
Stream outputStream,
108-
CancellationToken token = default,
109-
IProgress<float>? progress = null)
110-
{
111-
readTool.Progress = progress ?? new Progress<float>();
112-
await readTool.ReadFlashAsync(address, size, outputStream, token);
113-
}
114-
115-
public async Task ReadFlashAsync(
116-
ReadFlashTool readTool,
117-
uint address,
118-
uint size,
119-
Stream outputStream,
120-
bool verifyMd5,
121-
CancellationToken token = default,
122-
IProgress<float>? progress = null)
123-
{
124-
readTool.Progress = progress ?? new Progress<float>();
125-
await readTool.ReadFlashAsync(address, size, outputStream, verifyMd5, token);
126-
}
127-
128-
public async Task<byte[]> ReadFlashAsync(
129-
ReadFlashTool readTool,
130-
uint address,
131-
uint size,
132-
CancellationToken token = default,
133-
IProgress<float>? progress = null)
134-
{
135-
using var memoryStream = new MemoryStream();
136-
await ReadFlashAsync(readTool, address, size, memoryStream, token, progress);
137-
return memoryStream.ToArray();
138-
}
139-
140-
public async Task ReadFlashToFileAsync(
141-
ReadFlashTool readTool,
142-
uint address,
143-
uint size,
144-
string outputPath,
145-
CancellationToken token = default,
146-
IProgress<float>? progress = null)
147-
{
148-
using var fileStream = new FileStream(outputPath, FileMode.Create, FileAccess.Write);
149-
await ReadFlashAsync(readTool, address, size, fileStream, token, progress);
96+
return new ReadFlashTool(softLoader, communicator);
15097
}
15198

15299
public IUploadTool CreateUploadRamTool(ILoader loader, ChipTypes chipType)

ESPTool/Example.cs

Lines changed: 0 additions & 83 deletions
This file was deleted.

ESPTool/Loaders/SoftLoader/SoftLoader.cs

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
using EspDotNet.Commands;
22
using EspDotNet.Communication;
3-
using EspDotNet.Loaders;
4-
using System;
5-
using System.Threading;
6-
using System.Threading.Tasks;
3+
using System.Diagnostics;
74

85
namespace EspDotNet.Loaders.SoftLoader
96
{
@@ -60,40 +57,27 @@ public async Task<byte[]> SPI_FLASH_MD5(uint address, uint size, CancellationTok
6057
return response.Payload.Take(16).ToArray(); // Return the MD5 checksum (first 16 bytes)
6158
}
6259

63-
/// <summary>
64-
/// Begins reading from flash memory.
65-
/// </summary>
66-
/// <param name="address">The flash address to start reading from.</param>
67-
/// <param name="size">The number of bytes to read.</param>
68-
/// <param name="sectorSize">The flash sector size (typically 4096 bytes).</param>
69-
/// <param name="blockSize">The read block size (typically 64 bytes).</param>
70-
/// <param name="token">Cancellation token for the operation.</param>
71-
/// <exception cref="OperationCanceledException">Thrown if the operation is canceled via the token.</exception>
72-
public async Task FlashReadBeginAsync(uint address, uint size, uint sectorSize, uint blockSize, CancellationToken token)
60+
public async Task FlashReadBeginAsync(uint address, uint size, uint blockSize, uint inflight, CancellationToken token)
7361
{
7462
var request = new RequestCommandBuilder()
7563
.WithCommand(0xD2)
7664
.AppendPayload(BitConverter.GetBytes(address))
7765
.AppendPayload(BitConverter.GetBytes(size))
78-
.AppendPayload(BitConverter.GetBytes(sectorSize))
7966
.AppendPayload(BitConverter.GetBytes(blockSize))
67+
.AppendPayload(BitConverter.GetBytes(inflight))
8068
.Build();
8169

70+
8271
var response = await _commandExecutor.ExecuteCommandAsync(request, token);
8372
if (!response.Success)
8473
throw new InvalidOperationException("Failed to begin flash read.");
8574
}
8675

87-
/// <summary>
88-
/// Sends an acknowledgment for flash read data transfer.
89-
/// </summary>
90-
/// <param name="bytesReceived">The number of bytes received so far.</param>
91-
/// <param name="token">Cancellation token for the operation.</param>
92-
/// <exception cref="OperationCanceledException">Thrown if the operation is canceled via the token.</exception>
93-
public async Task FlashReadAckAsync(uint bytesReceived, CancellationToken token)
76+
public async Task FlashReadAckAsync(uint totalBytesReceived, CancellationToken token)
9477
{
95-
var ackData = BitConverter.GetBytes(bytesReceived);
96-
await _communicator.WriteAsync(ackData, token);
78+
var ackData = BitConverter.GetBytes(totalBytesReceived);
79+
await _communicator.WriteFrameAsync(new Frame(ackData), token);
80+
await _communicator.FlushAsync(token);
9781
}
9882

9983
/// <summary>

0 commit comments

Comments
 (0)