Skip to content

Commit 133aa67

Browse files
committed
Reworked the tool to be stateless
1 parent 8d3a8e7 commit 133aa67

File tree

3 files changed

+181
-202
lines changed

3 files changed

+181
-202
lines changed

ESPTool/ESPTool.cs

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

ESPTool/ESPToolbox.cs

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
using EspDotNet.Communication;
2+
using EspDotNet.Config;
3+
using EspDotNet.Loaders.SoftLoader;
4+
using EspDotNet.Loaders;
5+
using EspDotNet.Tools.Firmware;
6+
using EspDotNet.Tools;
7+
using Microsoft.Extensions.Logging;
8+
9+
namespace EspDotNet
10+
{
11+
public class ESPToolbox
12+
{
13+
private readonly ESPToolConfig _config;
14+
15+
public ESPToolbox()
16+
{
17+
_config = ConfigProvider.LoadDefaultConfig();
18+
}
19+
20+
public ESPToolbox(ESPToolConfig config)
21+
{
22+
_config = config;
23+
}
24+
25+
public Communicator CreateCommunicator()
26+
{
27+
return new Communicator();
28+
}
29+
30+
public void OpenSerial(Communicator communicator, string portName, int baudRate)
31+
{
32+
communicator.OpenSerial(portName, baudRate);
33+
}
34+
35+
public void CloseSerial(Communicator communicator)
36+
{
37+
communicator.CloseSerial();
38+
}
39+
40+
public async Task<ILoader> StartBootloaderAsync(Communicator communicator, CancellationToken token = default)
41+
{
42+
var bootloader = new BootloaderTool(communicator, _config.BootloaderSequence);
43+
return await bootloader.StartAsync(token);
44+
}
45+
46+
public async Task<ChipTypes> DetectChipTypeAsync(ILoader loader, CancellationToken token = default)
47+
{
48+
var detectTool = new ChipTypeDetectTool(loader, _config);
49+
return await detectTool.DetectChipTypeAsync(token);
50+
}
51+
52+
public async Task<SoftLoader> StartSoftloaderAsync(
53+
Communicator communicator,
54+
ILoader bootloader,
55+
ChipTypes chipType,
56+
CancellationToken token = default)
57+
{
58+
var deviceConfig = GetDeviceConfig(chipType);
59+
var firmwareProvider = DefaultFirmwareProviders.GetSoftloaderForDevice(chipType);
60+
var uploadTool = new UploadRamTool(bootloader)
61+
{
62+
BlockSize = (uint)deviceConfig.FlashBlockSize
63+
};
64+
var softloaderTool = new SoftloaderTool(communicator, uploadTool, firmwareProvider);
65+
return await softloaderTool.StartAsync(token);
66+
}
67+
68+
public async Task ChangeBaudAsync(Communicator communicator, ILoader loader, int baudRate, CancellationToken token = default)
69+
{
70+
var baudTool = new ChangeBaudrateTool(communicator, loader);
71+
await baudTool.ChangeBaudAsync(baudRate, token);
72+
}
73+
74+
public async Task EraseFlashAsync(SoftLoader loader, CancellationToken token = default)
75+
{
76+
var eraseTool = new FlashEraseTool(loader);
77+
await eraseTool.EraseFlashAsync(token);
78+
}
79+
80+
public async Task ResetDeviceAsync(Communicator communicator, CancellationToken token = default)
81+
{
82+
var sequence = _config.ResetSequence ?? throw new InvalidOperationException("No reset sequence in config");
83+
var resetTool = new ResetDeviceTool(communicator, sequence);
84+
await resetTool.ResetAsync(token);
85+
}
86+
87+
public async Task<byte[]> ReadEfuseAsync(ILoader loader, ChipTypes chipType, EFlagKey key, CancellationToken token = default)
88+
{
89+
var tool = new EFuseTool(loader, GetDeviceConfig(chipType));
90+
return await tool.ReadAsync(key, token);
91+
}
92+
93+
public IUploadTool CreateUploadRamTool(ILoader loader, ChipTypes chipType)
94+
{
95+
return new UploadRamTool(loader)
96+
{
97+
BlockSize = (uint)GetDeviceConfig(chipType).FlashBlockSize
98+
};
99+
}
100+
101+
public IUploadTool CreateUploadFlashTool(ILoader loader, ChipTypes chipType)
102+
{
103+
return new UploadFlashTool(loader)
104+
{
105+
BlockSize = (uint)GetDeviceConfig(chipType).FlashBlockSize
106+
};
107+
}
108+
109+
public IUploadTool CreateUploadFlashDeflatedTool(SoftLoader loader, ChipTypes chipType)
110+
{
111+
return new UploadFlashDeflatedTool(loader)
112+
{
113+
BlockSize = (uint)GetDeviceConfig(chipType).FlashBlockSize
114+
};
115+
}
116+
117+
public async Task UploadFirmwareAsync(
118+
IUploadTool uploadTool,
119+
IFirmwareProvider firmware,
120+
CancellationToken token = default,
121+
IProgress<float>? progress = null)
122+
{
123+
var firmwareTool = new FirmwareUploadTool(uploadTool)
124+
{
125+
Progress = progress ?? new Progress<float>()
126+
};
127+
128+
await firmwareTool.UploadFirmwareAsync(firmware, token);
129+
}
130+
131+
public async Task UploadFirmwareAndExecuteAsync(
132+
IUploadTool uploadTool,
133+
IFirmwareProvider firmware,
134+
Communicator communicator,
135+
CancellationToken token = default,
136+
IProgress<float>? progress = null)
137+
{
138+
await UploadFirmwareAsync(uploadTool, firmware, token, progress);
139+
await ResetDeviceAsync(communicator, token);
140+
}
141+
142+
private DeviceConfig GetDeviceConfig(ChipTypes chipType)
143+
{
144+
return _config.Devices.FirstOrDefault(d => d.ChipType == chipType)
145+
?? throw new InvalidOperationException($"No device config found for {chipType}");
146+
}
147+
}
148+
}

ESPTool/Example.cs

Lines changed: 33 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -1,108 +1,45 @@
1-
using EspDotNet.Config;
2-
using EspDotNet.Loaders.SoftLoader;
3-
using EspDotNet.Tools;
4-
using EspDotNet.Tools.Firmware;
1+
using EspDotNet.Tools.Firmware;
2+
using EspDotNet;
53
using System.Diagnostics;
4+
using System.Threading;
5+
using System;
66

7-
namespace EspDotNet
7+
public class Example
88
{
9-
public class Example
10-
{
11-
ChipTypes chipType;
12-
SoftLoader? softloader;
13-
ESPTool espTool;
14-
int baudRate = 921600;
15-
16-
public Example()
17-
{
18-
espTool = new ESPTool();
19-
chipType = ChipTypes.Unknown;
20-
}
21-
22-
public async Task Init(CancellationToken token = default)
23-
{
24-
// Create the tool and open the port
25-
// The device starts with 115200 baud.
26-
var espTool = new ESPTool();
27-
espTool.OpenSerial("COM30", 115200);
28-
29-
// Starting the bootloader will return a bootloader object.
30-
// This bootloader is used by the tools to execute commands.
31-
var bootLoader = await espTool.GetBootloaderTool().StartAsync(token);
32-
33-
// For example, detect the connected chip type.
34-
var chipType = await espTool.GetChipDetectTool(bootLoader).DetectChipTypeAsync(token);
35-
36-
// Since the bootloader doenst support all commands, we need to start the softloader.
37-
// Similar as before, this returns a softloader object.
38-
// The tool will automatically load the right softloader depending on the chiptype.
39-
var softLoader = await espTool.GetSoftloaderTool(bootLoader, chipType).StartAsync(token);
40-
41-
// Optionally, you can increase the baudrate for faster uploading.
42-
// The comport will also automatically change to the new baudrate.
43-
await espTool.GetChangeBaudrateTool(softLoader).ChangeBaudAsync(baudRate, token);
44-
}
45-
46-
public async Task UploadFirmware(CancellationToken token = default)
47-
{
48-
_ = softloader ?? throw new Exception("Initialize first");
49-
50-
// First, load the firmware to memory.
51-
// Its also possible to create a custom IFirmwareProvider implementation.
52-
var myFirmware = new FirmwareProvider(
53-
entryPoint: 0x00000000,
54-
segments:
55-
[
56-
new FirmwareSegmentProvider(0x00001000, File.ReadAllBytes("bootloader.bin")),
57-
new FirmwareSegmentProvider(0x00008000, File.ReadAllBytes("partition-table.bin")),
58-
new FirmwareSegmentProvider(0x0000F000, File.ReadAllBytes("ota_data_initial.bin")),
59-
new FirmwareSegmentProvider(0x00800000, File.ReadAllBytes("application.bin")),
60-
]
61-
);
629

63-
// Instantiate a tool to upload to RAM
64-
IUploadTool uploadToolRam = espTool.GetUploadRamTool(softloader, chipType);
10+
public async Task UploadFirmwareAsync(CancellationToken token = default)
11+
{
12+
var toolbox = new ESPToolbox();
13+
var communicator = toolbox.CreateCommunicator();
14+
toolbox.OpenSerial(communicator, "COM30", 115200);
6515

66-
// Instantiate a tool to upload to FLASH
67-
IUploadTool uploadToolFlash = espTool.GetUploadFlashTool(softloader, chipType);
16+
var loader = await toolbox.StartBootloaderAsync(communicator);
17+
var chipType = await toolbox.DetectChipTypeAsync(loader);
6818

69-
// Instantiate a tool to upload to FLASH, with a gzip deflated for faster uploading.
70-
// The tool handles the compression, so pass the firmware uncompressed in the next step.
71-
IUploadTool uploadToolFlashDeflated = espTool.GetUploadFlashDeflatedTool(softloader, chipType);
19+
var softloader = await toolbox.StartSoftloaderAsync(communicator, loader, chipType);
20+
await toolbox.ChangeBaudAsync(communicator, softloader, 921600);
7221

73-
// Create the FirmwareUploadTool, use one of the upload tools from before
74-
var firmwareTool = espTool.GetFirmwareUploadTool(uploadToolFlashDeflated);
22+
var uploadTool = toolbox.CreateUploadFlashDeflatedTool(softloader, chipType);
23+
var myFirmware = GetFirmware();
24+
var progress = new Progress<float>(p => Debug.WriteLine($"Upload progress: {p:P0}"));
7525

76-
// Some tools can report progress.
77-
firmwareTool.Progress = new Progress<float>(p =>
78-
{
79-
Debug.WriteLine($"Progress: {p * 100}%");
80-
});
81-
82-
// Upload the firmware
83-
await firmwareTool.UploadFirmwareAsync(myFirmware, token);
84-
85-
// Reset the device
86-
await espTool.GetResetDeviceTool().ResetAsync(token);
87-
}
88-
89-
public async Task EraseFlash(CancellationToken token = default)
90-
{
91-
_ = softloader ?? throw new Exception("Initialize first");
92-
93-
// Erase the flash
94-
await espTool.GetFlashEraseTool(softloader).EraseFlashAsync(token);
95-
96-
// Reset the device
97-
await espTool.GetResetDeviceTool().ResetAsync(token);
98-
}
26+
await toolbox.UploadFirmwareAsync(uploadTool, myFirmware, token, progress);
27+
await toolbox.ResetDeviceAsync(communicator);
28+
}
9929

100-
public async Task<byte[]> GetMacAddress(CancellationToken token = default)
101-
{
102-
_ = softloader ?? throw new Exception("Initialize first");
10330

104-
EFlagKey eFlagToRead = EFlagKey.BaseMacAddress;
105-
return await espTool.GetEFuseTool(softloader, chipType).ReadAsync(eFlagToRead, token);
106-
}
31+
private IFirmwareProvider GetFirmware()
32+
{
33+
return new FirmwareProvider(
34+
entryPoint: 0x00000000,
35+
segments:
36+
[
37+
new FirmwareSegmentProvider(0x00001000, File.ReadAllBytes("bootloader.bin")),
38+
new FirmwareSegmentProvider(0x00008000, File.ReadAllBytes("partition-table.bin")),
39+
new FirmwareSegmentProvider(0x0000F000, File.ReadAllBytes("ota_data_initial.bin")),
40+
new FirmwareSegmentProvider(0x00800000, File.ReadAllBytes("application.bin")),
41+
]
42+
);
10743
}
44+
10845
}

0 commit comments

Comments
 (0)