Skip to content

Commit bad613f

Browse files
committed
add esp interface and initial testing
1 parent 1d886b8 commit bad613f

File tree

6 files changed

+122
-32
lines changed

6 files changed

+122
-32
lines changed

src/esp/ESPLoader.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import StubLoader from './StubLoader';
55
import roms from './roms';
66
import ROM from './roms/rom';
77

8-
interface ESPOptions {
8+
export interface ESPOptions {
99
quiet?: boolean;
1010
stubUrl?: string;
1111
stdout?: any;

src/esp/index.ts

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import { SerialPort } from 'serialport/dist/index.d';
2+
import { ProgramConfig } from '../index.d';
3+
import ESPLoader, { ESPOptions, UploadFileDef } from './ESPLoader';
4+
import asyncTimeout from '../util/asyncTimeout';
5+
6+
const isSupported = (cpu: string) => ['esp8266', 'esp32'].includes(cpu);
7+
8+
export const upload = async (serial: SerialPort, config: ProgramConfig) => {
9+
if (!config.files?.length) throw new Error('No files to upload');
10+
// const log = (...args) => config.debug(`${args.join(' ')}\r\n`);
11+
const log = (...args: any[]) => console.log(...args);
12+
// const term = { log, debug: log, write: config.debug };
13+
14+
// const { port } = serial;
15+
// const transport = new Transport(port, term);
16+
let espLoader;
17+
18+
try {
19+
log('> Connecting...');
20+
espLoader = new ESPLoader(serial, {
21+
quiet: !config.verbose,
22+
} as ESPOptions);
23+
await espLoader.mainFn();
24+
// await espLoader.flash_id();
25+
log('> Connected');
26+
} catch (err) {
27+
// eslint-disable-next-line no-console
28+
console.error(err);
29+
// log('Failed to connect:', typeof err === 'string' ? err : err.message);
30+
try {
31+
await serial.close();
32+
} catch (err2) {
33+
// eslint-disable-next-line no-console
34+
console.error(err2);
35+
}
36+
return;
37+
}
38+
39+
try {
40+
// if (board.config?.wipe && board.config.wipe !== 'none') {
41+
// log('> Erasing device flash...');
42+
// await espLoader.erase_flash();
43+
// log('> Successfully erased device flash');
44+
// }
45+
log('> Writing main data partition, this may take a while...');
46+
await espLoader.writeFlash({
47+
fileArray: config.files.map((file) => ({ ...file, data: Buffer.from(file.data, 'base64') })),
48+
flashSize: 'keep',
49+
// flash_freq,
50+
// flash_mode,
51+
// compress: board.props?.build?.mcu !== 'esp8266',
52+
});
53+
await espLoader.flashDeflFinish({ reboot: true });
54+
await asyncTimeout(100);
55+
log('> Successfully written data partition');
56+
log('> Flashing succeeded! Have a nice day! :)');
57+
} catch (err) {
58+
// eslint-disable-next-line no-console
59+
console.error(err);
60+
log('Failed to upload:', err instanceof Error ? err.message : err);
61+
}
62+
63+
// try {
64+
// await serial.close();
65+
// } catch (err) {
66+
// // eslint-disable-next-line no-console
67+
// console.error(err);
68+
// }
69+
};
70+
71+
export default { upload, isSupported };

src/index.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { SerialPort } from 'serialport/dist/index.d';
33

44
import { setBaud, waitForOpen } from './util/serial-helpers';
55
import avr from './avr';
6-
// import esp from './esp';
6+
import esp from './esp';
77

88
export const upload = async (serial: SerialPort, config: ProgramConfig) => {
99
if (!config.hex && !config.files?.length) {
@@ -27,10 +27,10 @@ export const upload = async (serial: SerialPort, config: ProgramConfig) => {
2727
case 'avrdude':
2828
await avr.upload(serial, config);
2929
break;
30-
// case 'esptool':
31-
// case 'esptool_py':
32-
// await esp.upload(serial, config);
33-
// break;
30+
case 'esptool':
31+
case 'esptool_py':
32+
await esp.upload(serial, config);
33+
break;
3434
default:
3535
throw new Error(`Tool ${config.tool} not supported`);
3636
}
@@ -46,9 +46,9 @@ export const isSupported = (tool: string, cpu: string) => {
4646
case 'avr':
4747
case 'avrdude':
4848
return avr.isSupported(cpu);
49-
// case 'esptool':
50-
// case 'esptool_py':
51-
// return esp.isSupported(cpu);
49+
case 'esptool':
50+
case 'esptool_py':
51+
return esp.isSupported(cpu);
5252
default:
5353
return false;
5454
}

test/boards.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@ import { SerialPort } from 'serialport';
55
import { upload } from '../src/index';
66
import { waitForData, config, getHex } from './util';
77
import { waitForOpen } from '../src/util/serial-helpers';
8+
import { ProgramFile } from '../src/index.d';
89

910
Object.keys(config.devices).forEach((deviceRef) => {
1011
const device = config.devices[deviceRef];
1112
let key = '';
12-
let hex: Buffer;
13+
let hex: Buffer | undefined;
14+
let files: ProgramFile[] | undefined;
1315
let serial: SerialPort;
1416

1517
describe(`upload to ${device.name}`, function () {
@@ -19,6 +21,7 @@ Object.keys(config.devices).forEach((deviceRef) => {
1921
const res = await getHex(device.code, device.fqbn);
2022
key = res.key;
2123
hex = res.hex;
24+
files = res.files;
2225
console.log('compiled hex');
2326
});
2427

@@ -44,6 +47,7 @@ Object.keys(config.devices).forEach((deviceRef) => {
4447
it(`should upload to ${device.name}`, async () => {
4548
await upload(serial, {
4649
hex,
50+
files,
4751
speed: device.speed,
4852
tool: device.tool,
4953
cpu: device.cpu,

test/test-config.yml

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,38 @@
11
devices:
2-
uno:
3-
name: Arduino Uno
4-
# Find VID and PID in the Arduino IDE > Tools > Get Board Info
2+
# uno:
3+
# name: Arduino Uno
4+
# # Find VID and PID in the Arduino IDE > Tools > Get Board Info
5+
# vendorIds:
6+
# - '2341'
7+
# productIds:
8+
# - '0001'
9+
# code: blink
10+
# fqbn: arduino:avr:uno
11+
# cpu: atmega328p
12+
# tool: avrdude
13+
# speed: 115200
14+
# mega:
15+
# name: Arduino Mega 2560
16+
# vendorIds:
17+
# - '2341'
18+
# productIds:
19+
# - '0042'
20+
# code: blink
21+
# fqbn: arduino:avr:mega
22+
# cpu: atmega2560
23+
# tool: avrdude
24+
# speed: 115200
25+
nodemcuv2:
26+
name: ESP 8266 - Node MCU V2
527
vendorIds:
6-
- '2341'
28+
- '1a86'
729
productIds:
8-
- '0001'
30+
- '7523'
931
code: blink
10-
fqbn: arduino:avr:uno
11-
cpu: atmega328p
12-
tool: avrdude
32+
fqbn: esp8266:esp8266:nodemcuv2
33+
cpu: esp8266
34+
tool: esptool
1335
speed: 115200
14-
mega:
15-
name: Arduino Mega 2560
16-
vendorIds:
17-
- '2341'
18-
productIds:
19-
- '0042'
20-
code: blink
21-
fqbn: arduino:avr:mega
22-
cpu: atmega2560
23-
tool: avrdude
24-
speed: 115200
25-
# verbose: true
36+
37+
verbose: true
2638
compileServer: https://compile.duino.app

test/util.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import YAML from 'yaml';
33
import fs from 'fs';
44
import axios from 'axios';
55
import path from 'path';
6+
import { ProgramFile } from '../src/index.d';
67

78
export const waitForData = (
89
serial: SerialPort,
@@ -33,7 +34,8 @@ export const waitForData = (
3334
export const config = YAML.parse(fs.readFileSync(path.join(__dirname, 'test-config.yml'), 'utf8'));
3435

3536
interface HexResult {
36-
hex: Buffer;
37+
hex?: Buffer;
38+
files?: ProgramFile[];
3739
key: string;
3840
code: string;
3941
}
@@ -51,7 +53,8 @@ export const getHex = async (file: string, fqbn: string): Promise<HexResult> =>
5153
}],
5254
});
5355
return {
54-
hex: Buffer.from(res.data.hex, 'base64'),
56+
hex: res.data.hex ? Buffer.from(res.data.hex, 'base64') : undefined,
57+
files: res.data.files as ProgramFile[],
5558
key,
5659
code,
5760
} as HexResult;

0 commit comments

Comments
 (0)