Skip to content

Commit

Permalink
Merge pull request #8 from duinoapp/esp-support
Browse files Browse the repository at this point in the history
Esp support
  • Loading branch information
mrfrase3 authored Oct 29, 2022
2 parents a612196 + 5def8a4 commit 68bde07
Show file tree
Hide file tree
Showing 24 changed files with 2,113 additions and 33 deletions.
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

0 comments on commit 68bde07

Please sign in to comment.