Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Esp support #8

Merged
merged 6 commits into from
Oct 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,21 @@
"license": "UNLICENSED",
"devDependencies": {
"@types/chai": "^4.3.1",
"@types/crypto-js": "^4.1.1",
"@types/mocha": "^9.1.1",
"@types/node": "^17.0.33",
"@types/node": "^18.0.6",
"@types/pako": "^2.0.0",
"@typescript-eslint/eslint-plugin": "^5.23.0",
"@typescript-eslint/parser": "^5.23.0",
"axios": "^0.27.2",
"chai": "^4.3.6",
"crypto-js": "^4.1.1",
"eslint": "^8.15.0",
"eslint-config-airbnb-base": "^15.0.0",
"eslint-config-airbnb-typescript": "^17.0.0",
"eslint-plugin-import": "^2.26.0",
"mocha": "^10.0.0",
"pako": "^2.0.4",
"serialport": "^10.4.0",
"ts-node": "^10.8.0",
"typescript": "^4.6.4",
Expand Down
2 changes: 1 addition & 1 deletion src/avr/stk500-v1/stk500-v1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

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

interface STK500v1Options {
quiet?: boolean;
Expand Down
2 changes: 1 addition & 1 deletion src/avr/stk500-v2/stk500-v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { SerialPort } from 'serialport/dist/index.d';

import statics from './constants';
import { setDTRRTS } from '../../util/serial-helpers';
import asyncTimeout from '../../util/asyncTimeout';
import asyncTimeout from '../../util/async-timeout';

interface STK500v2Options {
quiet?: boolean;
Expand Down
67 changes: 67 additions & 0 deletions src/esp/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { SerialPort } from 'serialport/dist/index.d';
import { ProgramConfig } from '../index.d';
import ESPLoader, { ESPOptions, UploadFileDef } from './loader';
import asyncTimeout from '../util/async-timeout';

const isSupported = (cpu: string) => ['esp8266', 'esp32'].includes(cpu);

export const upload = async (serial: SerialPort, config: ProgramConfig) => {
if (!config.files?.length) throw new Error('No files to upload');
// const log = (...args) => config.debug(`${args.join(' ')}\r\n`);
const log = (...args: any[]) => console.log(...args);
// const term = { log, debug: log, write: config.debug };

// serial.on('data', (data: Buffer) => {
// console.log('read (utf8)', data.toString('utf-8'));
// console.log('read (hex)', data.toString('hex'));
// });

let espLoader;
try {
espLoader = new ESPLoader(serial, {
quiet: !config.verbose,
} as ESPOptions);
await espLoader.mainFn();
// await espLoader.flash_id();
log('> Connected');

if (config.uploadSpeed) {
await espLoader.changeBaudrate(config.uploadSpeed);
}
} catch (err) {
// eslint-disable-next-line no-console
console.error(err);
try {
await serial.close();
} catch (err2) {
// eslint-disable-next-line no-console
console.error(err2);
}
return;
}

try {
log('> Writing main data partition, this may take a while...');
await espLoader.writeFlash({
fileArray: config.files.map((file) => ({ ...file, data: Buffer.from(file.data, 'base64') })),
flashSize: '4MB',
flashFreq: config.flashFreq || 'keep',
flashMode: config.flashMode || 'keep',
// compress: board.props?.build?.mcu !== 'esp8266',
});
await espLoader.reboot();
await asyncTimeout(100);
if (config.uploadSpeed) {
await serial.update({ baudRate: config.speed || 115200 });
}
log('> Successfully written data partition');
log('> Flashing succeeded! Have a nice day! :)');
} catch (err) {
// eslint-disable-next-line no-console
console.error(err);
log('Failed to upload:', err instanceof Error ? err.message : err);
}

};

export default { upload, isSupported };
Loading