stk500-esm
Version:
A modern, ESM-compatible, TypeScript implementation of the STK500v1 protocol for programming Arduino boards directly from Node.js or the browser.
119 lines (118 loc) • 4.8 kB
TypeScript
import type { Duplex } from "node:stream";
/**
* Represents the configuration for a specific board.
*/
export interface Board {
/** The name of the board. */
name: string;
/** The baud rate for communication with the board. */
baudRate: number;
/** The expected signature of the board. */
signature: Uint8Array;
/** The page size for programming operations. */
pageSize: number;
/** The timeout duration for operations in milliseconds. */
timeout: number;
/** Whether to use 8-bit addresses for memory operations. */
use8BitAddresses?: boolean;
}
/**
* Options for configuring the STK500 instance.
*/
interface STK500Options {
/** Whether to suppress logging output. */
quiet?: boolean;
}
/**
* A callback function for reporting bootloading progress.
* @param status - The current status of the bootloading process.
* @param percentage - The percentage of the bootloading process that is complete.
* @returns void
*/
type BootloadProgressCallback = (status: string, percentage: number) => void;
declare class STK500 {
private log;
private stream;
private board;
/**
* Creates an instance of the STK500 programmer.
* @param stream - The read/write stream for communication with the device.
* @param board - The board configuration.
* @param opts - Additional options for the STK500 instance.
*/
constructor(stream: Duplex, board: Board, opts?: STK500Options);
/**
* Attempts to synchronize communication with the device.
* @param attempts - The number of synchronization attempts.
* @returns A promise that resolves with the synchronization response data.
* @throws Error if synchronization fails after all attempts.
*/
sync(attempts: number): Promise<Uint8Array>;
/**
* Verifies the device signature.
* @returns A promise that resolves with the verification response data.
* @throws Error if the signature verification fails.
*/
verifySignature(): Promise<Uint8Array>;
/**
* Retrieves the device signature.
* @returns A promise that resolves with the device signature data.
*/
getSignature(): Promise<Uint8Array>;
/**
* Sets device-specific options.
* @param options - An object containing device-specific options.
* @returns A promise that resolves when the options are set.
*/
setOptions(options: Record<string, number>): Promise<void>;
/**
* Enters programming mode on the device.
* @returns A promise that resolves with the response data.
*/
enterProgrammingMode(): Promise<Uint8Array>;
/**
* Loads a memory address for subsequent operations.
* @param useaddr - The address to load.
* @returns A promise that resolves with the response data.
*/
loadAddress(useaddr: number): Promise<Uint8Array>;
/**
* Loads a page of data to be programmed.
* @param writeBytes - The data to be programmed.
* @returns A promise that resolves with the response data.
*/
loadPage(writeBytes: Uint8Array): Promise<Uint8Array>;
/**
* Uploads the provided hex data to the device.
* @param hexData - The hex data to be uploaded, as a string or Uint8Array.
* @param progressCallback - Optional callback to report upload progress.
* @returns A promise that resolves when the upload is complete.
*/
upload(hexData: string | Uint8Array, progressCallback?: (percentage: number) => void): Promise<void>;
/**
* Exits programming mode on the device.
* @returns A promise that resolves with the response data.
*/
exitProgrammingMode(): Promise<Uint8Array>;
/**
* Verifies the uploaded data against the provided hex data.
* @param hexData - The hex data to verify against, as a string or Uint8Array.
* @param progressCallback - Optional callback to report verification progress.
* @returns A promise that resolves when verification is complete.
*/
verify(hexData: string | Uint8Array, progressCallback?: (percentage: number) => void): Promise<void>;
/**
* Verifies a single page of data.
* @param writeBytes - The data to be verified.
* @returns A promise that resolves with the verification response data.
*/
verifyPage(writeBytes: Uint8Array): Promise<Uint8Array>;
/**
* Performs the complete bootloading process for a device.
* @param hexData - The hex data to be uploaded, as a string or Uint8Array.
* @param progressCallback - Optional callback to report progress.
* @returns A promise that resolves when the bootloading process is complete.
*/
bootload(hexData: string | Uint8Array, progressCallback?: BootloadProgressCallback): Promise<void>;
}
export default STK500;