Skip to content
This repository has been archived by the owner on Oct 7, 2024. It is now read-only.
/ webdfu Public archive

Driver for working with DFU in a browser over WebUSB

License

Notifications You must be signed in to change notification settings

genoswitch/webdfu

Repository files navigation

WebDFU

NPM package CI in main branch

WebDFU — driver for working with DFU and DfuseDriver in a browser over Web USB or Web Bluetooth.

  • Reading and writing the current device firmware by DFU 1.1
  • ST DfuSe download and upload firmware To be re-implemented.
  • Switching from the runtime configuration to the DFU bootloader (DFU detach)

Install

npm i dfu-next

Usage

Full example in: webdfu/demo

Basic example:

import { DeviceBootstrapper } from "dfu";


const connect = async () => {
    const device = await navigator.usb.requestDevice({ filters: [] });

    const bootstrapper = new DeviceBootstrapper(device);

    const dfuDevice = await bootstrapper.init();

    //  Read firmware from devie
    const readEvents = dfuDevice.beginRead();
    readEvents.on("read/finish", (bytesRead, blocks, data) => {
        // save the data (blob)
        console.log("Successfully read firmware from device")
    })

    // Write firmware to device
    const firmwareFile = new ArrayBuffer(1024);
    const writeEvents = dfuDevice.beginWrite(firmwareFile);

    writeEvents.on("write/finish", (bytesSent) => {
        console.log("Sucessfully written new firmware to device.")
    })
}

/*
  The browser's security policy requires that WebUSB be accessed only by an explicit user action.
  Add the button in your html and run the WebDFu after click in button.
  In HTML: <button id="connect-button">Connect</button>
*/
document.getElementById("connect-button").addEventListener("click", connect);```