Skip to content

Commit

Permalink
Make interface between DFU and control request flash consistent
Browse files Browse the repository at this point in the history
  • Loading branch information
monkbroc committed Aug 21, 2023
1 parent 4e112a2 commit e5c3c50
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 15 deletions.
3 changes: 2 additions & 1 deletion src/device.js
Original file line number Diff line number Diff line change
Expand Up @@ -364,9 +364,10 @@ class Device extends DeviceBase {
* @param {Buffer} data Firmware data.
* @param {Object} [options] Options.
* @param {Number} [options.timeout] Timeout (milliseconds).
* @param {Function} [options.progress] User's callback function to log progress of the flashing process.
* @return {Promise}
*/
async updateFirmware(data, { timeout = DEFAULT_FIRMWARE_UPDATE_TIMEOUT } = {}, progress) {
async updateFirmware(data, { timeout = DEFAULT_FIRMWARE_UPDATE_TIMEOUT, progress } = {}) {
if (!data.length) {
throw new RangeError('Invalid firmware size');
}
Expand Down
16 changes: 9 additions & 7 deletions src/dfu-device.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@ const DfuDevice = (base) => class extends base {
/**
* Flashes the firmware over DFU interface.
*
* @param {Number} altSetting The interface alternate setting.
* @param {Buffer} buffer The binary firmware data to be flashed.
* @param {Number} addr The starting address where the firmware will be written.
* @param {Function} progress User's callback function to log progress of the flashing process.
* @param {Object} options Optional options for the flashing process (noErase, leave).
* @param {Buffer} data The binary firmware data to be flashed.
* @param {Object} options Options.
* @param {Number} options.altSetting The interface alternate setting.
* @param {Number} options.startAddr The starting address where the firmware will be written.
* @param {boolean} [options.noErase] - Skip erasing the device memory.
* @param {boolean} [options.leave] - Leave DFU mode after download.
* @param {Function} [options.progress] User's callback function to log progress of the flashing process.
* @returns {Promise<void>} A Promise that resolves when the firmware is successfully flashed.
*/
async writeOverDfu({ altSetting, buffer, addr, options, progress }) {
async writeOverDfu(data, { altSetting, startAddr, noErase, leave, progress }) {
await this._dfu.setAltSetting(altSetting);
await this._dfu.doDownload(addr, buffer, options, progress);
await this._dfu.doDownload({ startAddr, data, noErase, leave, progress });
}
};

Expand Down
20 changes: 13 additions & 7 deletions src/dfu.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,13 +247,15 @@ class Dfu {
/**
* Perform DFU download of binary data to the device.
*
* @param {number} startAddr - The starting address to write the data.
* @param {Buffer} data - The binary data to write.
* @param {object} options - Options for the download process. (noErase, leave)
* @param {function} progress - Callback function used to log progress.
* @param {Object} options Options.
* @param {number} options.startAddr - The starting address to write the data.
* @param {Buffer} options.data - The binary data to write.
* @param {boolean} [options.noErase] - Skip erasing the device memory.
* @param {boolean} [options.leave] - Leave DFU mode after download.
* @param {function} [options.progress] - Callback function used to log progress.
* @return {Promise}
*/
async doDownload(startAddr, data, options = { noErase: false, leave: false }, progress) {
async doDownload({ startAddr, data, noErase, leave, progress }) {
if (!this._memoryInfo || !this._memoryInfo.segments) {
throw new Error('No memory map available');
}
Expand All @@ -264,7 +266,7 @@ class Dfu {
}
const expectedSize = data.byteLength;

if (!options.noErase) {
if (!noErase) {
this._log.info('Erasing DFU device memory');
await this._erase(startAddress, expectedSize, progress);
}
Expand Down Expand Up @@ -310,7 +312,7 @@ class Dfu {
progress({ event: 'complete-download', bytes: bytesSent });
}

if (options.leave) {
if (leave) {
this._log.info('Manifesting new firmware');
try {
await this.leave();
Expand Down Expand Up @@ -612,6 +614,10 @@ class Dfu {
if (!segment.erasable) {
// Skip over the non-erasable section
bytesErased = Math.min(bytesErased + segment.end - addr, bytesToErase);
if (progress) {
// include a progress event for the skipped section to ensure total matches
progress({ event: 'erased', bytes: segment.end - addr });
}
addr = segment.end;
continue;
}
Expand Down

0 comments on commit e5c3c50

Please sign in to comment.