|
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; |
5 | 3 | using System.Diagnostics; |
| 4 | +using System.Threading; |
| 5 | +using System; |
6 | 6 |
|
7 | | -namespace EspDotNet |
| 7 | +public class Example |
8 | 8 | { |
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 | | - ); |
62 | 9 |
|
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); |
65 | 15 |
|
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); |
68 | 18 |
|
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); |
72 | 21 |
|
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}")); |
75 | 25 |
|
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 | + } |
99 | 29 |
|
100 | | - public async Task<byte[]> GetMacAddress(CancellationToken token = default) |
101 | | - { |
102 | | - _ = softloader ?? throw new Exception("Initialize first"); |
103 | 30 |
|
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 | + ); |
107 | 43 | } |
| 44 | + |
108 | 45 | } |
0 commit comments