-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
152 lines (129 loc) · 4.29 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
( function(){
const jlinkexe = require('./lib/jlinkexe');
const utils = require('./lib/utils');
const isInstalled = require('./lib/is_installed');
let debug = false;
exports.jlinkexe = jlinkexe;
exports.setDebug = function( flag ){
debug = flag;
jlinkexe.setDebug = flag;
};
exports.setJlinkEXECommand = function( command ){
jlinkexe.JLinkExe = command;
};
exports.setJLinkEXEOptions = function( optionsString ){
jlinkexe.JLinkExeOptions = optionsString;
};
exports.isJLinkEXEInstalled = function(){
return isInstalled( jlinkexe.JLinkExe );
};
function _perform( commands ){
return jlinkexe.executeJlinkCommands( commands, { debug: debug } )
.then( function( result ){
return ( result.code === 0 && !result.error );
});
}
const _commands = {
reset: ["w4 40000544 1", "si 0","tck0", "t0", "t1", "sleep 10" ],
pinReset: ["w4 40000544 1", "r" ],
eraseAll: ["h", "w4 4001e504 2", "w4 4001e50c 1", "sleep 100", "r"]
};
/**
* @return Promise, resolve( true ) or reject( Error )
*/
exports.reset = function(){
return _perform( _commands.reset );
};
/**
* @return Promise, resolve( true ) or reject( Error )
*/
exports.pinReset = function(){
return _perform( _commands.pinReset );
};
/**
* @return Promise, resolve( true ) or reject( Error )
*/
exports.eraseAll = function(){
return _perform( _commands.eraseAll );
};
/**
* @return Promise, resolve( true ) or reject( Error )
*/
exports.program = function( firmwareFilePath ){
if( !firmwareFilePath ) throw new Error("node-jlink: flash() requires firmwareFilePath");
return jlinkexe.executeJlinkCommands( ["r", "h", "loadfile " + firmwareFilePath, "r", "g" ], { debug: debug })
.then( function( result ){
return ( result.code === 0 && !result.error );
});
};
/**
*
* @param address: must be word aligned (default 0)
* @param numBytes: must be word aligned (default 4)
* @return Promise, resolve( Buffer ) or reject( Error )
*/
exports.readmem = function( address, numBytes ){
if( address === void 0 ) address = 0x00000000;
if( numBytes === void 0 ) numBytes = 4;
if( address % 4 !== 0 ) throw new Error("node-jlink: readmMem() address must be 32 bit aligned");
if( numBytes % 4 !== 0 ) throw new Error("node-jlink: readmMem() numBytes must be 32 bit aligned");
const memCommand = `mem 0x${utils.numberToHexString( address, 8)}, 0x${utils.numberToHexString( numBytes, 8 )}`;
return jlinkexe.executeJlinkCommands( ["h", memCommand ], { debug: debug } )
.then( function( result ){
return jlinkexe.parseMemoryStringToBuffer( result.stdout, address, numBytes );
})
.catch( function(error){
console.error( `Failed to read ${numBytes} bytes from 0x${utils.numberToHexString(address, 8)}`);
console.error( `Command was: "${memCommand}"`);
throw error;
});
};
exports.readWordLE = function( address ){
return exports.readmem( address )
.then( function( buffer ){
return buffer.readUInt32LE(0);
});
};
exports.readWordBE = function( address ){
return exports.readmem( address )
.then( function( buffer ){
return buffer.readUInt32BE(0);
});
};
exports.identify = function(){
return jlinkexe.executeJlinkCommands()
.then( function( result ){
return jlinkexe.parseJLinkVersionInfo( result.stdout );
})
// .catch( function( error ){
// if( error && error.message && error.message.indexOf("Can not connect") !== -1 ){
// return Promise.reject( new Error( "node-jlink: Could not connect to JLink"));
// }
// return Promise.reject( )
// });
};
exports.resume = function(){
return jlinkexe.executeJlinkCommands( ["g"], { debug: debug })
.then( function( result ){
return ( result.code === 0 && !result.error );
});
};
/**
*
* @param arrayOfCommands
* @param options
* @return Promise, resolve( Result ) or reject( Error )
*
* Result Format:
* {
* stdout: String, captured from jlinkexe stdout stream, or empty string
* stderr: String, captured from jlinkexe stdout stream, or empty string
* code: Integer, captured from jlinkexe exit code, or 0
* error: Error, from jlinkexe or internal error, or null
* }
*/
exports.executeCommands = function( arrayOfCommands, options ){
if( !options ) options = { debug: debug };
return jlinkexe.executeJlinkCommands( arrayOfCommands, options );
}
})();