Skip to content

Commit

Permalink
feat: added support for mega boards
Browse files Browse the repository at this point in the history
  • Loading branch information
xiduzo committed Aug 23, 2024
1 parent 5a90e43 commit ed4d630
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 30 deletions.
37 changes: 19 additions & 18 deletions packages/flasher/src/constants.ts
Original file line number Diff line number Diff line change
@@ -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 = [
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion packages/flasher/src/protocols/Avr109.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion packages/flasher/src/protocols/Stk500v1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export class Stk500v1 extends Protocol implements Flasher {

private bootload(hex: Buffer) {
return new Promise<void>((resolve, reject) => {
new STK500({ debug: true }).bootload(
new STK500({ debug: false }).bootload(
this.connection.serialPort,
hex,
this.board,
Expand Down
53 changes: 44 additions & 9 deletions packages/flasher/src/protocols/Stk500v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -65,4 +61,43 @@ export class Stk500v2 extends Protocol implements Flasher {
});
});
}

private async enterProgrammingMode(stk500v2: any, board: Board) {
return new Promise<void>((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<void>((resolve, reject) => {
stk500v2.upload(hex, pageSize, (error: unknown) => {
if (error) {
reject(error);
return;
}

resolve();
});
});
}

private async exitProgrammingMode(stk500v2: any) {
return new Promise<void>((resolve, reject) => {
stk500v2.exitProgrammingMode((error: unknown) => {
if (error) {
reject(error);
return;
}

resolve();
});
});
}
}
5 changes: 4 additions & 1 deletion packages/flasher/test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
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(''));
const filePath = path.resolve(
__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);
Expand Down
32 changes: 32 additions & 0 deletions patches/stk500-v2+1.0.4.patch
Original file line number Diff line number Diff line change
@@ -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.

0 comments on commit ed4d630

Please sign in to comment.