Skip to content

Commit 7d859a8

Browse files
committed
Initial commit
0 parents  commit 7d859a8

10 files changed

+899
-0
lines changed

LICENSE

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2017 Devan Lai
4+
5+
Portions of the libstlink code are ported from the pystlink project:
6+
Copyright (c) 2015 Pavel Revak <[email protected]>
7+
8+
Permission is hereby granted, free of charge, to any person obtaining a copy
9+
of this software and associated documentation files (the "Software"), to deal
10+
in the Software without restriction, including without limitation the rights
11+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
copies of the Software, and to permit persons to whom the Software is
13+
furnished to do so, subject to the following conditions:
14+
15+
The above copyright notice and this permission notice shall be included in all
16+
copies or substantial portions of the Software.
17+
18+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24+
SOFTWARE.

README.md

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
webstlink
2+
---------
3+
webstlink is a port of [pystlink](https://github.com/pavelrevak/pystlink), using the [WebUSB](https://wicg.github.io/webusb/) API as a backend to control ST-Link/V2 and ST-Link/V2-1 debug probes from the browser.
4+
5+
Dependencies
6+
------------
7+
webstlink depends on [WebUSB](https://caniuse.com/#feat=webusb) and many ES6 features.
8+
For best results, test with Chrome 61 or newer.
9+
10+
Local testing
11+
-------------
12+
You can test locally with any webserver. For a one-liner, run:
13+
14+
python -m SimpleHTTPServer
15+
16+
and navigate to http://localhost:8000/demo/
17+
18+
Licensing
19+
---------
20+
webstlink is available under the terms of the MIT license, the same as the pystlink project.

demo/index.html

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<link rel="stylesheet" type="text/css" href="sakura.css" />
5+
<title>WebUSB STLink Test</title>
6+
<script src="test.js" type="module"></script>
7+
<style>
8+
div.info {
9+
white-space: pre;
10+
}
11+
div.log {
12+
min-height: 3em;
13+
max-height: 100em;
14+
overflow: scroll;
15+
font-family: monospace;
16+
}
17+
</style>
18+
</head>
19+
<body>
20+
<p>
21+
<button id="connect">Connect</button>
22+
<button id="flash">Flash</button>
23+
</p>
24+
<div id="log" class="log"></div>
25+
</body>
26+
</html>

demo/sakura.css

+160
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
/* Sakura.css v1.0.0
2+
* ================
3+
* Minimal css theme.
4+
* Project: https://github.com/oxalorg/sakura
5+
*/
6+
/* Body */
7+
html {
8+
font-size: 62.5%;
9+
font-family: serif; }
10+
11+
body {
12+
font-size: 1.8rem;
13+
line-height: 1.618;
14+
max-width: 38em;
15+
margin: auto;
16+
color: #4a4a4a;
17+
background-color: #f9f9f9;
18+
padding: 13px; }
19+
20+
@media (max-width: 684px) {
21+
body {
22+
font-size: 1.53rem; } }
23+
@media (max-width: 382px) {
24+
body {
25+
font-size: 1.35rem; } }
26+
h1, h2, h3, h4, h5, h6 {
27+
line-height: 1.1;
28+
font-family: Verdana, Geneva, sans-serif;
29+
font-weight: 700;
30+
overflow-wrap: break-word;
31+
word-wrap: break-word;
32+
-ms-word-break: break-all;
33+
word-break: break-word;
34+
-ms-hyphens: auto;
35+
-moz-hyphens: auto;
36+
-webkit-hyphens: auto;
37+
hyphens: auto; }
38+
39+
h1 {
40+
font-size: 2.35em; }
41+
42+
h2 {
43+
font-size: 2em; }
44+
45+
h3 {
46+
font-size: 1.75em; }
47+
48+
h4 {
49+
font-size: 1.5em; }
50+
51+
h5 {
52+
font-size: 1.25em; }
53+
54+
h6 {
55+
font-size: 1em; }
56+
57+
small, sub, sup {
58+
font-size: 75%; }
59+
60+
hr {
61+
border-color: #2c8898; }
62+
63+
a {
64+
text-decoration: none;
65+
color: #2c8898; }
66+
a:hover {
67+
color: #982c61;
68+
border-bottom: 2px solid #4a4a4a; }
69+
70+
ul {
71+
padding-left: 1.4em; }
72+
73+
li {
74+
margin-bottom: 0.4em; }
75+
76+
blockquote {
77+
font-style: italic;
78+
margin-left: 1.5em;
79+
padding-left: 1em;
80+
border-left: 3px solid #2c8898; }
81+
82+
img {
83+
max-width: 100%; }
84+
85+
/* Pre and Code */
86+
pre {
87+
background-color: #f1f1f1;
88+
display: block;
89+
padding: 1em;
90+
overflow-x: auto; }
91+
92+
code {
93+
font-size: 0.9em;
94+
padding: 0 0.5em;
95+
background-color: #f1f1f1;
96+
white-space: pre-wrap; }
97+
98+
pre > code {
99+
padding: 0;
100+
background-color: transparent;
101+
white-space: pre; }
102+
103+
/* Tables */
104+
table {
105+
text-align: justify;
106+
width: 100%;
107+
border-collapse: collapse; }
108+
109+
td, th {
110+
padding: 0.5em;
111+
border-bottom: 1px solid #f1f1f1; }
112+
113+
/* Buttons, forms and input */
114+
input, textarea {
115+
border: 1px solid #4a4a4a; }
116+
input:focus, textarea:focus {
117+
border: 1px solid #2c8898; }
118+
119+
textarea {
120+
width: 100%; }
121+
122+
.button, button, input[type="submit"], input[type="reset"], input[type="button"] {
123+
display: inline-block;
124+
padding: 5px 10px;
125+
text-align: center;
126+
text-decoration: none;
127+
white-space: nowrap;
128+
background-color: #2c8898;
129+
color: #f9f9f9;
130+
border-radius: 1px;
131+
border: 1px solid #2c8898;
132+
cursor: pointer;
133+
box-sizing: border-box; }
134+
.button[disabled], button[disabled], input[type="submit"][disabled], input[type="reset"][disabled], input[type="button"][disabled] {
135+
cursor: default;
136+
opacity: .5; }
137+
.button:focus, .button:hover, button:focus, button:hover, input[type="submit"]:focus, input[type="submit"]:hover, input[type="reset"]:focus, input[type="reset"]:hover, input[type="button"]:focus, input[type="button"]:hover {
138+
background-color: #982c61;
139+
border-color: #982c61;
140+
color: #f9f9f9;
141+
outline: 0; }
142+
143+
textarea, select, input[type] {
144+
color: #4a4a4a;
145+
padding: 6px 10px;
146+
/* The 6px vertically centers text on FF, ignored by Webkit */
147+
margin-bottom: 10px;
148+
background-color: #f1f1f1;
149+
border: 1px solid #f1f1f1;
150+
border-radius: 4px;
151+
box-shadow: none;
152+
box-sizing: border-box; }
153+
textarea:focus, select:focus, input[type]:focus {
154+
border: 1px solid #2c8898;
155+
outline: 0; }
156+
157+
label, legend, fieldset {
158+
display: block;
159+
margin-bottom: .5rem;
160+
font-weight: 600; }

demo/test.js

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import * as libstlink from '../src/lib/package.js';
2+
var curr_device;
3+
var probe;
4+
5+
function fetchResource(url) {
6+
return new Promise(function(resolve, reject) {
7+
let xhr = new XMLHttpRequest();
8+
xhr.responseType = "arraybuffer";
9+
xhr.addEventListener("load", function() {
10+
if (this.status != 200) {
11+
reject(this.status);
12+
} else {
13+
resolve(this.response);
14+
}
15+
});
16+
xhr.addEventListener("error", function() {
17+
reject(this.status);
18+
});
19+
xhr.open("GET", url);
20+
xhr.send();
21+
});
22+
}
23+
24+
class Debugger {
25+
constructor(container) {
26+
this.container = container;
27+
}
28+
29+
debug(msg) {
30+
console.log(msg);
31+
let info = document.createElement("div");
32+
info.className = "info";
33+
info.textContent = msg;
34+
this.container.appendChild(info);
35+
}
36+
}
37+
38+
document.addEventListener('DOMContentLoaded', event => {
39+
let connectButton = document.querySelector("#connect");
40+
let log = document.querySelector("#log");
41+
connectButton.addEventListener('click', function() {
42+
navigator.usb.requestDevice({ filters: libstlink.usb.filters }).then(
43+
async device => {
44+
curr_device = device;
45+
let debuggr = new Debugger(log);
46+
let connector = new libstlink.usb.Connector(device, debuggr);
47+
probe = new libstlink.Stlinkv2(connector, debuggr);
48+
await device.open();
49+
await device.selectConfiguration(1);
50+
await device.claimInterface(0);
51+
await device.selectAlternateInterface(0, 0);
52+
try {
53+
await probe.init();
54+
} catch (e) {
55+
console.log(e);
56+
}
57+
}
58+
);
59+
});
60+
});

src/lib/package.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/* package.js
2+
* Module namespace for ST-Link library code
3+
*
4+
* Copyright Devan Lai 2017
5+
*
6+
*/
7+
8+
import * as usb from './stlinkusb.js';
9+
import * as exceptions from './stlinkex.js';
10+
import Stlinkv2 from './stlinkv2.js';
11+
12+
export { usb, exceptions, Stlinkv2 };

src/lib/stlinkex.js

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/* stlinkex.js
2+
* ST-Link exception classes
3+
*
4+
* Copyright Devan Lai 2017
5+
*
6+
* Ported from lib/stlinkex.py in the pystlink project,
7+
* Copyright Pavel Revak 2015
8+
*
9+
*/
10+
11+
import { hex_octet } from './util.js';
12+
13+
const Exception = class StlinkException extends Error {};
14+
const Warning = class StlinkWarning extends Error {};
15+
16+
const UsbError = class StlinkUsbError extends Error {
17+
constructor(message, address) {
18+
super(message);
19+
this.address = address;
20+
}
21+
22+
toString() {
23+
if (this.address) {
24+
const addr_string = "0x" + hex_octet(this.address);
25+
return addr_string + ": " + this.message;
26+
} else {
27+
return this.message;
28+
}
29+
}
30+
};
31+
32+
export { Exception, Warning, UsbError };

0 commit comments

Comments
 (0)