diff --git a/packages/flasher/src/constants.ts b/packages/flasher/src/constants.ts index e12a3c0..2b2dd3d 100644 --- a/packages/flasher/src/constants.ts +++ b/packages/flasher/src/constants.ts @@ -1,5 +1,6 @@ import { Avr109 } from './protocols/Avr109'; import { Stk500v1 } from './protocols/Stk500v1'; +import { Stk500v2 } from './protocols/Stk500v2'; // import { Stk500v2 } from './protocols/Stk500v2'; export const BOARDS = [ @@ -26,24 +27,24 @@ export const BOARDS = [ 'https://web.archive.org/web/20150813095112/https://www.arduino.cc/en/Main/ArduinoBoardNano', protocol: Stk500v1, }, - // { - // name: 'mega', - // baudRate: 115200, - // signature: Buffer.from([0x1e, 0x98, 0x01]), // ATmega2560 - // pageSize: 256, - // delay1: 10, - // delay2: 1, - // timeout: 0xc8, - // stabDelay: 0x64, - // cmdexeDelay: 0x19, - // synchLoops: 0x20, - // byteDelay: 0x00, - // pollValue: 0x53, - // pollIndex: 0x03, - // productIds: ['0042', '6001', '0010', '7523'], - // productPage: 'https://store.arduino.cc/mega-2560-r3', - // protocol: Stk500v2, - // }, + { + name: 'mega', + baudRate: 115200, + signature: Buffer.from([0x1e, 0x98, 0x01]), // ATmega2560 + pageSize: 256, + delay1: 10, + delay2: 1, + timeout: 0xc8, + stabDelay: 0x64, + cmdexeDelay: 0x19, + synchLoops: 0x20, + byteDelay: 0x00, + pollValue: 0x53, + pollIndex: 0x03, + productIds: ['0042', '6001', '0010', '7523'], + productPage: 'https://store.arduino.cc/mega-2560-r3', + protocol: Stk500v2, + }, { name: 'nano (new bootloader)', baudRate: 115200, diff --git a/packages/flasher/src/protocols/Avr109.ts b/packages/flasher/src/protocols/Avr109.ts index d6b58ca..a28e763 100644 --- a/packages/flasher/src/protocols/Avr109.ts +++ b/packages/flasher/src/protocols/Avr109.ts @@ -55,7 +55,7 @@ export class Avr109 extends Protocol implements Flasher { AVR109.init( this.connection.serialPort, - { signature, debug: true }, + { signature, debug: false }, async (error: unknown, flasher: any) => { if (error) { reject(error); diff --git a/packages/flasher/src/protocols/Stk500v1.ts b/packages/flasher/src/protocols/Stk500v1.ts index dc2faf1..c0d0205 100644 --- a/packages/flasher/src/protocols/Stk500v1.ts +++ b/packages/flasher/src/protocols/Stk500v1.ts @@ -24,7 +24,7 @@ export class Stk500v1 extends Protocol implements Flasher { private bootload(hex: Buffer) { return new Promise((resolve, reject) => { - new STK500({ debug: true }).bootload( + new STK500({ debug: false }).bootload( this.connection.serialPort, hex, this.board, diff --git a/packages/flasher/src/protocols/Stk500v2.ts b/packages/flasher/src/protocols/Stk500v2.ts index 4139f5c..7169ab9 100644 --- a/packages/flasher/src/protocols/Stk500v2.ts +++ b/packages/flasher/src/protocols/Stk500v2.ts @@ -21,19 +21,15 @@ export class Stk500v2 extends Protocol implements Flasher { const hex = this.parseHex(file); await this.connection.open(); - await this.reset(); const stk500v2 = new STK500v2(this.connection.serialPort); - console.log('syncing'); + await this.reset(); + await this.sync(stk500v2, 5); - console.log('synced'); await this.verifySignature(stk500v2, this.board.signature); - console.log(stk500v2); - // await STK500v2.sync(5); - // await STK500v2.verifySignature(this.board.signature); - // await STK500v2.enterProgrammingMode(this.board); - // await STK500v2.upload(hex, this.board.pageSize); - // await STK500v2.exitProgrammingMode(); + await this.enterProgrammingMode(stk500v2, this.board); + await this.upload(stk500v2, hex, this.board.pageSize); + await this.exitProgrammingMode(stk500v2); } private async reset() { @@ -65,4 +61,43 @@ export class Stk500v2 extends Protocol implements Flasher { }); }); } + + private async enterProgrammingMode(stk500v2: any, board: Board) { + return new Promise((resolve, reject) => { + stk500v2.enterProgrammingMode(board, (error: unknown) => { + if (error) { + reject(error); + return; + } + + resolve(); + }); + }); + } + + private async upload(stk500v2: any, hex: any, pageSize: number) { + return new Promise((resolve, reject) => { + stk500v2.upload(hex, pageSize, (error: unknown) => { + if (error) { + reject(error); + return; + } + + resolve(); + }); + }); + } + + private async exitProgrammingMode(stk500v2: any) { + return new Promise((resolve, reject) => { + stk500v2.exitProgrammingMode((error: unknown) => { + if (error) { + reject(error); + return; + } + + resolve(); + }); + }); + } } diff --git a/packages/flasher/test.ts b/packages/flasher/test.ts index a80b364..10ed456 100644 --- a/packages/flasher/test.ts +++ b/packages/flasher/test.ts @@ -1,7 +1,10 @@ import path from 'path'; import { Flasher } from './src/Flasher'; +import { getConnectedPorts } from './src/serialport'; async function flash() { + const ports = await getConnectedPorts(); + console.log(ports); const board = 'mega'; try { const __dirname = path.resolve(path.dirname('')); @@ -9,7 +12,7 @@ async function flash() { __dirname, `../../apps/electron-app/hex/${board}/StandardFirmata.cpp.hex`, ); - await new Flasher(board, '/dev/tty.usbmodem1101').flash(filePath); + await new Flasher(board, '/dev/tty.usbmodem1401').flash(filePath); console.log('done'); } catch (error) { console.log(error); diff --git a/patches/stk500-v2+1.0.4.patch b/patches/stk500-v2+1.0.4.patch new file mode 100644 index 0000000..ab6a54b --- /dev/null +++ b/patches/stk500-v2+1.0.4.patch @@ -0,0 +1,32 @@ +diff --git a/node_modules/stk500-v2/index.js b/node_modules/stk500-v2/index.js +index b379200..faf5156 100644 +--- a/node_modules/stk500-v2/index.js ++++ b/node_modules/stk500-v2/index.js +@@ -265,10 +265,10 @@ stk500.prototype.loadAddress = function(useaddr, done) { + //console.log("load address"); + var self = this; + +- msb = (useaddr >> 24) & 0xff | 0x80; +- xsb = (useaddr >> 16) & 0xff; +- ysb = (useaddr >> 8) & 0xff; +- lsb = useaddr & 0xff; ++ var msb = (useaddr >> 24) & 0xff | 0x80; ++ var xsb = (useaddr >> 16) & 0xff; ++ var ysb = (useaddr >> 8) & 0xff; ++ var lsb = useaddr & 0xff; + + var cmdBuf = Buffer.from([CMD_LOAD_ADDRESS, msb, xsb, ysb, lsb]); + +diff --git a/node_modules/stk500-v2/lib/parser-v2.js b/node_modules/stk500-v2/lib/parser-v2.js +index 339f8fc..11184bf 100644 +--- a/node_modules/stk500-v2/lib/parser-v2.js ++++ b/node_modules/stk500-v2/lib/parser-v2.js +@@ -64,7 +64,7 @@ module.exports = function(serialPort){ + //received. The total timeout period is 200 ms for the CMD_SIGN_ON command, 5 + //seconds for the CMD_READ/PROGRAM_FLASH/EEPROM commands, and 1 + //second for all other commands. +- timeout = 1000; ++ var timeout = 1000; + if(typeByte === c.CMD_SIGN_ON) timeout = 200; + else { + // grab the constant names.