|
3 | 3 | [](https://circleci.com/gh/ARMmbed/dapjs/) |
4 | 4 | [](https://spdx.org/licenses/MIT.html) |
5 | 5 |
|
6 | | -DAP.js is a JavaScript interface to CMSIS-DAP, aiming to implement a subset of |
7 | | -the functionality provided by [pyOCD](https://github.com/mbedmicro/pyOCD), enabling |
8 | | -debugging of Arm Cortex-M devices in Node.js and in the browser using [WebUSB](https://developers.google.com/web/updates/2016/03/access-usb-devices-on-the-web). |
9 | | - |
10 | | -## Features |
11 | | - |
12 | | -- General |
13 | | - - Read core registers |
14 | | - - Run arbitrary assembled Arm machine code |
15 | | -- Debugging |
16 | | - - Set hardware and software breakpoints |
17 | | - - Step (instruction level) |
18 | | -- Memory |
19 | | - - Read blocks from memory: |
20 | | - - 16-bit reads/writes |
21 | | - - 32-bit reads/writes |
22 | | - - Word-aligned mass reads and writes |
23 | | -- Flashing |
24 | | - - Full-chip erase |
25 | | - - Flash binary files and intel hex files |
26 | | - - Support for the NRF51 (including the micro:bit) and NXP's FRDM-K64F. |
27 | | -- Performance |
28 | | - - Support for batched commands to improve HID report utilisation |
29 | | - - Flashing at ~10-20 kB/s, comparable with PyOCD and OpenOCD |
| 6 | +DAP.js is a JavaScript interface to [CMSIS-DAP](https://www.keil.com/pack/doc/CMSIS/DAP/html/index.html), enabling access to Arm Microcontrollers using [Node.js](https://nodejs.org/) or in the browser using [WebUSB](https://wicg.github.io/webusb/). |
30 | 7 |
|
31 | 8 | ## Prerequisites |
32 | 9 |
|
33 | | -[Node.js > v8.9.0](https://nodejs.org), which includes `npm 5`. |
| 10 | +[Node.js > v6.10.0](https://nodejs.org), which includes `npm`. |
34 | 11 |
|
35 | 12 | ## Installation |
36 | 13 |
|
37 | | -The SDK is distributed using npm. To install the package in your project: |
| 14 | +The package is distributed using npm. To install the package in your project: |
38 | 15 |
|
39 | | - $ npm install dapjs |
| 16 | +```bash |
| 17 | +$ npm install dapjs |
| 18 | +``` |
| 19 | + |
| 20 | +## Getting Started |
| 21 | + |
| 22 | +Decide on a transport layer to use (see below) and refer to the [examples folder](https://github.com/ARMmbed/dapjs/tree/master/examples/) to get started. |
| 23 | + |
| 24 | +The web example can be seen running at: |
| 25 | + |
| 26 | +https://armmbed.github.io/dapjs/examples/daplink-flash/web.html |
| 27 | + |
| 28 | +Refer to the [DAPjs API Documentation](https://armmbed.github.io/dapjs/) for more information. |
| 29 | + |
| 30 | +## Choosing a Transport |
| 31 | + |
| 32 | +In order to use DAPjs, you need to install support for one of the transports. Use the following information to help you choose which to use: |
| 33 | + |
| 34 | +### WebUSB |
40 | 35 |
|
41 | | -## Development setup |
| 36 | +If you wish to use DAPjs in a browser environment, you must use WebUSB. Please refer to the [implementation status](https://github.com/WICG/webusb#implementation-status) of WebUSB to understand browser support for this technology. |
42 | 37 |
|
43 | | -After cloning this repository: |
| 38 | +__Note:__ WebUSB in the browser doesn't require any further libraries to be installed. |
44 | 39 |
|
45 | | - $ npm install |
| 40 | +If you also want your program to work in a Node.js environment a [WebUSB library](https://github.com/thegecko/webusb) exists to allow your program to be ported to Node.js. |
46 | 41 |
|
47 | | -Then run one of the gulp commands: |
| 42 | +To install the library for Node.js, use: |
48 | 43 |
|
49 | | - $ gulp |
50 | | - $ gulp watch |
51 | | - $ npm run gulp |
| 44 | +```bash |
| 45 | +$ npm install webusb |
| 46 | +``` |
| 47 | + |
| 48 | +#### Example |
52 | 49 |
|
53 | | -## Examples |
| 50 | +In the browser, require the library: |
54 | 51 |
|
55 | | -For more full-featured examples, please refer to the [examples](https://github.com/ARMmbed/dapjs/tree/master/examples) folder and see the web example running at: |
| 52 | +```html |
| 53 | +<script type="text/javascript" src="bundles/dap.bundle.js"></script> |
| 54 | +``` |
56 | 55 |
|
57 | | -https://armmbed.github.io/dapjs/ |
| 56 | +In Node.js Require the libraries: |
58 | 57 |
|
59 | 58 | ```javascript |
60 | | -device = await navigator.usb.requestDevice({ |
61 | | - filters: [{vendorId: 0x0d28}] |
| 59 | +const usb = require("webusb").usb; |
| 60 | +const DAPjs = require("dapjs"); |
| 61 | +``` |
| 62 | + |
| 63 | +Then in either environment: |
| 64 | + |
| 65 | +```javascript |
| 66 | +<navigator>.usb.requestDevice({ |
| 67 | + filters: [{vendorId: 0xD28}] |
| 68 | +}) |
| 69 | +.then(device => { |
| 70 | + const transport = new DAPjs.WebUSB(device); |
| 71 | + const daplink = new DAPjs.DAPLink(transport); |
| 72 | + |
| 73 | + return daplink.connect() |
| 74 | + .then(() => daplink.disconnect()) |
| 75 | + .then(() => process.exit()); |
| 76 | +}) |
| 77 | +.catch(error => { |
| 78 | + console.error(error.message || error); |
| 79 | + process.exit(); |
62 | 80 | }); |
| 81 | +``` |
| 82 | + |
| 83 | +#### Pros |
| 84 | +- Works in the browser |
| 85 | +- Programs are portable to Node.js environments |
63 | 86 |
|
64 | | -this.deviceCode = device.serialNumber.slice(0, 4); |
65 | | -selector = new DAPjs.PlatformSelector(); |
66 | | -const info = await selector.lookupDevice(this.deviceCode); |
67 | | -this.hid = new DAPjs.HID(device); |
68 | | - |
69 | | -// open hid device |
70 | | -await this.hid.open(); |
71 | | -dapDevice = new DAPjs.DAP(this.hid); |
72 | | -// contains flash algorithms data and memory map |
73 | | -let flashAlgorithmsData = {}; |
74 | | -let flashAlgorithm = new DAPjs.FlashAlgorithm(flashAlgorithmsData, this.deviceCode); |
75 | | -this.target = new DAPjs.FlashTarget(dapDevice, flashAlgorithm); |
76 | | - |
77 | | -// init and halt target |
78 | | -await this.target.init(); |
79 | | -await this.target.halt(); |
80 | | - |
81 | | -// program_data contains binary data |
82 | | -program_data = DAPjs.FlashProgram.fromBinary(0, program_data); |
83 | | -await this.target.program(program_data, (progress) => { |
84 | | - console.log(progress); |
| 87 | +#### Cons |
| 88 | +- Requires a recent version of [DAPLink](https://armmbed.github.io/DAPLink/) to be installed on your target device. |
| 89 | + |
| 90 | +### HID |
| 91 | + |
| 92 | +For the highest level of firmware compatibility in a Node.js environment, the HID transport is recommended. This utilises the `node-hid` library and is installed as follows: |
| 93 | + |
| 94 | +```bash |
| 95 | +$ npm install node-hid |
| 96 | +``` |
| 97 | + |
| 98 | +#### Example |
| 99 | + |
| 100 | +```javascript |
| 101 | +const hid = require("node-hid"); |
| 102 | +const DAPjs = require("dapjs"); |
| 103 | + |
| 104 | +let devices = hid.devices(); |
| 105 | +devices = devices.filter(device => device.vendorId === 0xD28); |
| 106 | + |
| 107 | +const transport = new DAPjs.HID(devices[0]); |
| 108 | +const daplink = new DAPjs.DAPLink(transport); |
| 109 | + |
| 110 | +daplink.connect() |
| 111 | +.then(() => daplink.disconnect()) |
| 112 | +.then(() => process.exit()) |
| 113 | +.catch(error => { |
| 114 | + console.error(error.message || error); |
| 115 | + process.exit(); |
85 | 116 | }); |
| 117 | +``` |
| 118 | + |
| 119 | +#### Pros |
| 120 | +- Compatible with older CMSIS-DAP firmware. |
| 121 | + |
| 122 | +#### Cons |
| 123 | +- Requires HID access to JavaScript in your OS. |
| 124 | + |
| 125 | +### USB |
86 | 126 |
|
87 | | -await this.target.reset(); |
| 127 | +A "pure" USB transport exists which bypasses requiring `WebUSB` and `HID`. |
| 128 | +This utilises the `usb` library and is installed as follows: |
| 129 | + |
| 130 | +```bash |
| 131 | +$ npm install usb |
88 | 132 | ``` |
89 | 133 |
|
90 | | -## Documentation |
| 134 | +#### Example |
| 135 | + |
| 136 | +```javascript |
| 137 | +const usb = require("usb"); |
| 138 | +const DAPjs = require("dapjs"); |
| 139 | + |
| 140 | +let devices = usb.getDeviceList(); |
| 141 | +devices = devices.filter(device => device.deviceDescriptor.idVendor === 0xD28); |
91 | 142 |
|
92 | | -API documentation can be viewed at: |
| 143 | +const transport = new DAPjs.USB(devices[0]); |
| 144 | +const daplink = new DAPjs.DAPLink(transport); |
93 | 145 |
|
94 | | -https://armmbed.github.io/dapjs/docs/ |
| 146 | +daplink.connect() |
| 147 | +.then(() => daplink.disconnect()) |
| 148 | +.then(() => process.exit()) |
| 149 | +.catch(error => { |
| 150 | + console.error(error.message || error); |
| 151 | + process.exit(); |
| 152 | +}); |
| 153 | +``` |
| 154 | + |
| 155 | +#### Pros |
| 156 | +- Doesn't require HID access to JavaScript in your OS. |
| 157 | + |
| 158 | +#### Cons |
| 159 | +- Requires a recent version of [DAPLink](https://armmbed.github.io/DAPLink/) to be installed on your target device. |
| 160 | + |
| 161 | +## Architecture |
| 162 | + |
| 163 | +The architecture of this project is built up in layers as follows: |
| 164 | + |
| 165 | +### Transport |
| 166 | + |
| 167 | +The `Transport` layer offers access to the USB device plugged into the host. Different transports are available based on user needs (see above). |
| 168 | + |
| 169 | +### Proxy |
| 170 | + |
| 171 | +The `Proxy` layer uses the transport layer to expose low-level `CMSIS-DAP` commands to the next layer. A common use for the proxy is as a debug chip attached to the main processor accessed over USB. |
| 172 | + |
| 173 | +A CMSIS-DAP implementation is included, however a network proxy or similar could be introduced at this layer in order to remote commands. |
| 174 | + |
| 175 | +### DAPLink |
| 176 | + |
| 177 | +The `DAPLink` layer is a special derived implementation of the `CMSIS-DAP` proxy implementation. It adds DAPLink vendor specific functionality such as Mass Storage Device `firmware flashing` and `serial control`. |
| 178 | + |
| 179 | +### DAP |
| 180 | + |
| 181 | +The `DAP` (Debug Access Port) layer exposes low-level access to ports, registers and memory. An implementation exists for `ADI` (Arm Debug Interface). |
| 182 | + |
| 183 | +### Processor |
| 184 | + |
| 185 | +The `Processor` layer exposes access to the core processor registers. |
| 186 | + |
| 187 | +## Development |
| 188 | + |
| 189 | +After cloning this repository, install the development dependencies: |
| 190 | + |
| 191 | +```bash |
| 192 | +$ npm install |
| 193 | +``` |
| 194 | + |
| 195 | +### Building |
| 196 | + |
| 197 | +[Gulp](https://gulpjs.com/) is used as a task runner to build the project. |
| 198 | +To build the project, simply run `gulp` or to continually build as source changes, run `gulp watch`: |
| 199 | + |
| 200 | +```bash |
| 201 | +$ gulp |
| 202 | +$ gulp watch |
| 203 | +``` |
| 204 | + |
| 205 | +A `package.json script` exists to run gulp if you don't have it installed globally: |
| 206 | + |
| 207 | +```bash |
| 208 | +$ npm run gulp |
| 209 | +$ npm run gulp watch |
| 210 | +``` |
| 211 | + |
| 212 | +### Running |
| 213 | + |
| 214 | +A local [express](https://expressjs.com/) server is included to run the web example locally: |
| 215 | + |
| 216 | +```bash |
| 217 | +$ node server.js |
| 218 | +``` |
| 219 | + |
| 220 | +The latest build of master is always available to be installed from the `gh-pages` branch: |
| 221 | + |
| 222 | +```bash |
| 223 | +$ npm install ARMmbed/dapjs#gh-pages |
| 224 | +``` |
0 commit comments