|
20 | 20 | * SOFTWARE. |
21 | 21 | */ |
22 | 22 |
|
23 | | -const EventEmitter = require("events"); |
24 | | -const DAPjs = require("../../"); |
| 23 | +const DAPjs = require('../../'); |
25 | 24 |
|
26 | | -// Emit keyboard input |
27 | | -const inputEmitter = new EventEmitter(); |
28 | | -process.stdin.setRawMode(true); |
29 | | -process.stdin.setEncoding("utf8"); |
30 | | -process.stdin.on("readable", () => { |
31 | | - let input; |
32 | | - while (input = process.stdin.read()) { |
33 | | - if (input === "\u0003") { |
34 | | - process.exit(); |
35 | | - } else if (input !== null) { |
36 | | - let index = parseInt(input); |
37 | | - inputEmitter.emit("input", index); |
38 | | - } |
| 25 | +// Select a device from the list |
| 26 | +const selectDevice = async (devices) => { |
| 27 | + if (devices.length === 0) { |
| 28 | + throw new Error('No devices found'); |
39 | 29 | } |
40 | | -}); |
| 30 | + |
| 31 | + console.log('Select a device to read registers:'); |
| 32 | + devices.forEach((device, index) => { |
| 33 | + console.log(`${index + 1}: ${device.name}`); |
| 34 | + }); |
| 35 | + |
| 36 | + const device = await new Promise(resolve => { |
| 37 | + process.stdin.setRawMode(true); |
| 38 | + process.stdin.setEncoding('utf8'); |
| 39 | + process.stdin.on('readable', () => { |
| 40 | + let input; |
| 41 | + while (input = process.stdin.read()) { |
| 42 | + if (input === '\u0003') { |
| 43 | + process.exit(); |
| 44 | + } else if (input !== null) { |
| 45 | + let index = parseInt(input); |
| 46 | + if (index <= devices.length) { |
| 47 | + resolve(devices[index - 1]); |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + }); |
| 52 | + }); |
| 53 | + |
| 54 | + return device; |
| 55 | +} |
41 | 56 |
|
42 | 57 | // Read device registers |
43 | | -function readRegisters(transport) { |
| 58 | +const readRegisters = async transport => { |
44 | 59 | const processor = new DAPjs.CortexM(transport); |
45 | 60 |
|
46 | | - return processor.connect() |
47 | | - .then(() => { |
48 | | - return processor.halt(); |
49 | | - }) |
50 | | - .then(() => { |
51 | | - const registers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]; |
52 | | - return processor.readCoreRegisters(registers); |
53 | | - }) |
54 | | - .then(registers => { |
55 | | - registers.forEach((register, index) => { |
56 | | - console.log(`R${index}: ${("00000000" + register.toString(16)).slice(-8)}`); |
57 | | - }); |
58 | | - return processor.resume(); |
59 | | - }) |
60 | | - .then(() => { |
61 | | - return processor.disconnect(); |
| 61 | + await processor.connect(); |
| 62 | + await processor.halt(); |
| 63 | + const registers = await processor.readCoreRegisters([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]); |
| 64 | + |
| 65 | + registers.forEach((register, index) => { |
| 66 | + console.log(`R${index}: ${('00000000' + register.toString(16)).slice(-8)}`); |
62 | 67 | }); |
| 68 | + |
| 69 | + await processor.resume(); |
| 70 | + await processor.disconnect(); |
63 | 71 | } |
64 | 72 |
|
65 | 73 | module.exports = { |
66 | | - inputEmitter: inputEmitter, |
67 | | - readRegisters: readRegisters |
| 74 | + DAPLINK_VENDOR: 0xD28, |
| 75 | + selectDevice, |
| 76 | + readRegisters |
68 | 77 | }; |
0 commit comments