Skip to content

Commit 68bde07

Browse files
authored
Merge pull request #8 from duinoapp/esp-support
Esp support
2 parents a612196 + 5def8a4 commit 68bde07

24 files changed

+2113
-33
lines changed

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,21 @@
1111
"license": "UNLICENSED",
1212
"devDependencies": {
1313
"@types/chai": "^4.3.1",
14+
"@types/crypto-js": "^4.1.1",
1415
"@types/mocha": "^9.1.1",
15-
"@types/node": "^17.0.33",
16+
"@types/node": "^18.0.6",
17+
"@types/pako": "^2.0.0",
1618
"@typescript-eslint/eslint-plugin": "^5.23.0",
1719
"@typescript-eslint/parser": "^5.23.0",
1820
"axios": "^0.27.2",
1921
"chai": "^4.3.6",
22+
"crypto-js": "^4.1.1",
2023
"eslint": "^8.15.0",
2124
"eslint-config-airbnb-base": "^15.0.0",
2225
"eslint-config-airbnb-typescript": "^17.0.0",
2326
"eslint-plugin-import": "^2.26.0",
2427
"mocha": "^10.0.0",
28+
"pako": "^2.0.4",
2529
"serialport": "^10.4.0",
2630
"ts-node": "^10.8.0",
2731
"typescript": "^4.6.4",

src/avr/stk500-v1/stk500-v1.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import { SerialPort } from 'serialport/dist/index.d';
55
import { setDTRRTS } from '../../util/serial-helpers';
6-
import asyncTimeout from '../../util/asyncTimeout';
6+
import asyncTimeout from '../../util/async-timeout';
77

88
interface STK500v1Options {
99
quiet?: boolean;

src/avr/stk500-v2/stk500-v2.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { SerialPort } from 'serialport/dist/index.d';
22

33
import statics from './constants';
44
import { setDTRRTS } from '../../util/serial-helpers';
5-
import asyncTimeout from '../../util/asyncTimeout';
5+
import asyncTimeout from '../../util/async-timeout';
66

77
interface STK500v2Options {
88
quiet?: boolean;

src/esp/index.ts

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import { SerialPort } from 'serialport/dist/index.d';
2+
import { ProgramConfig } from '../index.d';
3+
import ESPLoader, { ESPOptions, UploadFileDef } from './loader';
4+
import asyncTimeout from '../util/async-timeout';
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+
// serial.on('data', (data: Buffer) => {
15+
// console.log('read (utf8)', data.toString('utf-8'));
16+
// console.log('read (hex)', data.toString('hex'));
17+
// });
18+
19+
let espLoader;
20+
try {
21+
espLoader = new ESPLoader(serial, {
22+
quiet: !config.verbose,
23+
} as ESPOptions);
24+
await espLoader.mainFn();
25+
// await espLoader.flash_id();
26+
log('> Connected');
27+
28+
if (config.uploadSpeed) {
29+
await espLoader.changeBaudrate(config.uploadSpeed);
30+
}
31+
} catch (err) {
32+
// eslint-disable-next-line no-console
33+
console.error(err);
34+
try {
35+
await serial.close();
36+
} catch (err2) {
37+
// eslint-disable-next-line no-console
38+
console.error(err2);
39+
}
40+
return;
41+
}
42+
43+
try {
44+
log('> Writing main data partition, this may take a while...');
45+
await espLoader.writeFlash({
46+
fileArray: config.files.map((file) => ({ ...file, data: Buffer.from(file.data, 'base64') })),
47+
flashSize: '4MB',
48+
flashFreq: config.flashFreq || 'keep',
49+
flashMode: config.flashMode || 'keep',
50+
// compress: board.props?.build?.mcu !== 'esp8266',
51+
});
52+
await espLoader.reboot();
53+
await asyncTimeout(100);
54+
if (config.uploadSpeed) {
55+
await serial.update({ baudRate: config.speed || 115200 });
56+
}
57+
log('> Successfully written data partition');
58+
log('> Flashing succeeded! Have a nice day! :)');
59+
} catch (err) {
60+
// eslint-disable-next-line no-console
61+
console.error(err);
62+
log('Failed to upload:', err instanceof Error ? err.message : err);
63+
}
64+
65+
};
66+
67+
export default { upload, isSupported };

0 commit comments

Comments
 (0)