Skip to content

Instantly share code, notes, and snippets.

@IvanSanchez
Created February 13, 2018 14:10
Show Gist options
  • Save IvanSanchez/0ad8008789a263f142b027d2566e18ff to your computer and use it in GitHub Desktop.
Save IvanSanchez/0ad8008789a263f142b027d2566e18ff to your computer and use it in GitHub Desktop.
RTT logging
const jprog = require('pc-nrfjprog-js');
const rtt = jprog.RTT;
const debug = require('debug');
function rttReadLoop(sn, ch, size, log){
rtt.read(ch, size, (err, str)=>{
if (err) {
log(err);
} else if (str) {
log(str);
rttReadLoop(sn, ch, size, log);
} else {
setTimeout(()=>{
rttReadLoop(sn, ch, size, log)
}, 5);
}
});
}
module.exports = new Promise((res, rej)=>{
jprog.getSerialNumbers((err, sns)=>{
if (!sns || !sns.length) {
// return rej('No jlink probes present');
return res();
}
for (const sn of sns) {
jprog.getProbeInfo(sn, (err, probeInfo)=>{
const probeDebug = debug('rtt:' + sn);
if (err) {
return probeDebug(err);
}
probeDebug(probeInfo.firmwareString);
rtt.start(sn, {}, (err, down, up)=>{
probeDebug('Down channels (to probe):', down);
probeDebug('Up channels (from probe):', up);
if (!up || up.length === 0) {
return rej('No down channels');
}
for (upChannel of up) {
const channelDebug = debug('rtt:' + sn + ':up' + upChannel.channelIndex);
rttReadLoop(sn, upChannel.channelIndex, upChannel.size, channelDebug);
// Exit after the first channel of the first probe, because of no
// multithreading in jprog.
return res(rtt);
}
// const channelDebug = debug('rtt:' + sn + ':up0');
// rttReadLoop(sn, 0, 4096, channelDebug);
// No RTT channels on the first probe
return rej();
})
})
}
})
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment