Skip to content

Commit f9e4f8d

Browse files
authored
Merge pull request #58 from PropGit/Fix_CrOS_DL_Failures
Fix Chrome OS Download Failures
2 parents 229edcd + 1b4b1df commit f9e4f8d

File tree

3 files changed

+42
-28
lines changed

3 files changed

+42
-28
lines changed

loader.js

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
// Programming metrics
1919
let postResetDelay = null; //Delay after reset and before serial stream; Post-Reset Delay is set by loadPropeller()
20-
let autoAdjust = 20; //Amount to adjust postResetDelay upon each failure
20+
let autoAdjust = 30; //Amount to adjust postResetDelay upon each failure
2121
let txData; //Data to transmit to the Propeller (size/contents created later)
2222

2323
const defaultClockSpeed = 80000000;
@@ -140,17 +140,21 @@ function loadPropeller(sock, portPath, action, payload, debug) {
140140
//Set postResetDelay based on platform; ideal Post-Reset Delay = 100 ms; adjust downward according to typically-busy operating systems
141141
postResetDelay = (platform === pfWin) ? 60 : 100;
142142
if (port.connId) {
143-
// Connection exists, prep to reuse it
144-
originalBaudrate = port.baud;
145-
updatePort(port, {mode: "programming", bSocket: sock});
146-
connect = function() {return changeBaudrate(port, initialBaudrate)}
143+
// Connection exists, prep to close it first (to reset it), then open it (fresh)
144+
originalBaudrate = initialBaudrate;
145+
connect = function() {return closePort(port).then(function() {return openPort(sock, portPath, initialBaudrate, "programming")}).catch(function(e) {return Promise.reject(e)})}
146+
// The following temporarily removed (and replaced above) to intentionally close and reopen port in hopes it eliminates the CrOS v67+ failed download problem
147+
// // Connection exists, prep to reuse it
148+
// originalBaudrate = port.baud;
149+
// updatePort(port, {mode: "programming", bSocket: sock});
150+
// connect = function() {return changeBaudrate(port, initialBaudrate)}
147151
} else {
148152
// No connection yet, prep to create one
149153
originalBaudrate = initialBaudrate;
150154
connect = function() {return openPort(sock, portPath, initialBaudrate, "programming")}
151155
}
152156
} else {
153-
//Virtually-clear postResetDelay (it's controlled by wireless device)
157+
//Nearly-clear the postResetDelay (it's controlled by wireless device)
154158
postResetDelay = 1;
155159
//TODO Retrieve actual current baudrate
156160
originalBaudrate = initialBaudrate;
@@ -180,9 +184,11 @@ function loadPropeller(sock, portPath, action, payload, debug) {
180184
log(e.message, mAll, sock);
181185
log(notice(neDownloadFailed), mAll, sock);
182186
updatePort(port, {mode: "none"});
183-
if ((port.isWired && port.connId) || port.isWireless) {changeBaudrate(port, originalBaudrate)}
184-
if (port.isWireless) {closePort(port, false)}
185-
});
187+
if ((port.isWired && port.connId) || port.isWireless) {return changeBaudrate(port, originalBaudrate)}
188+
})
189+
.catch(function(e) {log(e.message, mAll, sock)})
190+
.then(function() {if (port.isWireless) return closePort(port, false)})
191+
.catch(function(e) {log(e.message, mAll, sock);});
186192
} else {
187193
// Port not found
188194
log(notice(neCanNotFindPort, [portPath]), mAll, sock);
@@ -304,7 +310,9 @@ function talkToProp(sock, port, binImage, toEEPROM) {
304310
.catch(function(e) { //Error!
305311
if (noticeCode(e.message) === nePropellerNotFound && --attempts) { // Retry (if "Propeller not found" and more attempts available)
306312
log("Propeller not found: retrying...", mDeep);
307-
postResetDelay = Math.max(postResetDelay - autoAdjust, 1); // Shorten Post Reset Delay upon every attempt (min 1)
313+
if (platform === pfWin) { // If Windows platform,
314+
postResetDelay = Math.max(postResetDelay - autoAdjust, 1); // Shorten Post Reset Delay upon every attempt (min 1)
315+
}
308316
return sendLoader(); // note: sendLoader does not return execution below (promises continue at next .then/.catch)
309317
}
310318
return reject(e); // Or if other error (or out of retry attempts), reject with message
@@ -429,15 +437,15 @@ function talkToProp(sock, port, binImage, toEEPROM) {
429437
var next;
430438

431439
/* Calculate expected Micro Boot Loader and User Application delivery times
432-
= 300 [>max post-reset-delay] + ((10 [bits per byte] * (data bytes [transmitting] + 20 silence bytes [MBL waiting] + 8 MBL "ready" bytes [MBL responding])) /
433-
initial baud rate) * 1,000 [to scale ms to integer] + 1 [to always round up] + 500 [Rx hardware to OS slack time] */
434-
var mblDeliveryTime = 300+((10*(txData.byteLength+20+8))/initialBaudrate)*1000+1 + (port.isWireless) ? 1500 : 250;
440+
= 210 [max post-reset-delay] + ((10 [bits per byte] * (data bytes [transmitting] + 20 silence bytes [MBL waiting] + 8 MBL "ready" bytes [MBL responding])) /
441+
initial baud rate) * 1,000 [to scale ms to integer] + 1 [to always round up] + 250 or 1500 [Rx hardware or Network to OS slack time] */
442+
var mblDeliveryTime = Math.trunc(210+((10*(txData.byteLength+20+8))/initialBaudrate)*1000+1 + ((port.isWired) ? 250 : 1500));
435443

436444
//=((10 [bits per byte] * [max packet size]) / final baud rate) * 1,000 [to scale ms to integer] + 1 [to always round up] + 1500 or 250 [Network or Rx hardware to OS slack time]
437-
var userDeliveryTime = ((10*maxDataSize)/finalBaudrate)*1000+1 + (port.isWireless) ? 1500 : 250;
445+
var userDeliveryTime = Math.trunc(((10*maxDataSize)/finalBaudrate)*1000+1 + ((port.isWired) ? 250 : 1500));
438446

439-
//Set for limited retry attempts (multiple when wired to try various post-reset timing)
440-
var attempts = (port.isWired) ? Math.trunc(postResetDelay / autoAdjust) + 1 : 1;
447+
//Set for limited retry attempts (multiple when wired; potentially trying various post-reset timing)
448+
var attempts = (port.isWired) ? ((platform === pfWin) ? Math.max(Math.trunc(postResetDelay / autoAdjust), 1) : 1) : 1;
441449

442450
Promise.resolve()
443451
.then(function() {return sendLoader();}) //Get Propeller's attention and send initial application (Micro Boot Loader)

manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "BlocklyProp Launcher",
33
"description": "A Chrome application that connects your Propeller-Powered Hardware to the BlocklyProp website.",
4-
"version": "0.9.2",
4+
"version": "0.9.4",
55
"manifest_version": 2,
66
"minimum_chrome_version": "45",
77

serial.js

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,9 @@ function openPort(sock, portPath, baudrate, connMode) {
4949
function (openInfo) {
5050
if (!chrome.runtime.lastError) {
5151
// No error; update serial port object
52-
updatePort(port, {connId: openInfo.connectionId, bSocket: sock, mode: connMode, baud: baudrate});
53-
log("Port " + portPath + " open with ID " + openInfo.connectionId, mStat);
52+
updatePort(port, {connId: openInfo.connectionId, bSocket: sock, mode: connMode});
53+
port.baud = baudrate; //Update baud; does not use updatePort() to avoid unnecessary port activity
54+
log("Port " + portPath + " open with ID " + openInfo.connectionId + " at " + baudrate + " baud", mDbug);
5455
resolve();
5556
} else {
5657
// Error
@@ -108,11 +109,11 @@ function closePort(port, command) {
108109
function socketClose(socket) {
109110
// Nullify port's HTTP or Telnet socket reference
110111
let sID = port[socket];
111-
updatePort(port, {[socket]: null});
112112
if (sID) {
113-
log("Closing socket", mDbug);
113+
updatePort(port, {[socket]: null});
114114
// Disconnect and/or close socket (if necessary)
115115
chrome.sockets.tcp.getInfo(sID, function(info) {
116+
log("Closed socket " + sID, mDbug);
116117
if (info.connected) {
117118
chrome.sockets.tcp.disconnect(sID, function() {
118119
chrome.sockets.tcp.close(sID, function() {
@@ -126,7 +127,6 @@ function closePort(port, command) {
126127
}
127128
});
128129
} else {
129-
log("Not closing socket", mDbug);
130130
reject(Error(notice(neCanNotClosePort, [port.path])));
131131
}
132132
}
@@ -137,12 +137,12 @@ function closePort(port, command) {
137137
if (port.connId) {
138138
chrome.serial.disconnect(port.connId, function (closeResult) {
139139
if (closeResult) {
140-
log("Closed port " + port.path + " (id " + port.connId + ")", mStat);
140+
log("Closed port " + port.path + " (id " + port.connId + ")", mDbug);
141141
// Clear connection id to indicate port is closed
142142
updatePort(port, {connId: null});
143-
resolve();
143+
setTimeout(resolve, 250); //Delay resolve() to prevent future openPort() calls from arriving too soon to accommodate
144144
} else {
145-
log("Could not close port " + port.path + " (id " + port.connId + ")", mStat);
145+
log("Could not close port " + port.path + " (id " + port.connId + ")", mDbug);
146146
reject(Error(notice(neCanNotClosePort, [port.path])));
147147
}
148148
});
@@ -170,7 +170,7 @@ function changeBaudrate(port, baudrate) {
170170
if (port.isWired) {
171171
chrome.serial.update(port.connId, {'bitrate': baudrate}, function (updateResult) {
172172
if (updateResult) {
173-
port.baud = baudrate; //Update baud; does not use updatePort() because of circular reference //!!!
173+
port.baud = baudrate; //Update baud; does not use updatePort() to avoid circular reference
174174
resolve();
175175
} else {
176176
reject(Error(notice(neCanNotSetBaudrate, [port.path, baudrate])));
@@ -315,9 +315,15 @@ function debugErrorReceiver(info) {
315315
// log("Error: PortID "+info.connectionId+" "+info.error, mDeep);
316316
} else {
317317
switch (info.resultCode) {
318-
case -100: //port closed
318+
case -100: //Port closed
319+
//Find port by Propeller Telnet ID or HTTP ID and clear record
319320
let port = findPort(byPTID, info.socketId);
320-
if (!port) {port = findPort(byPHID, info.socketId)}
321+
if (port) {
322+
updatePort(port, {ptSocket: null});
323+
} else {
324+
port = findPort(byPHID, info.socketId);
325+
if (port) {updatePort(port, {phSocket: null})}
326+
}
321327
if (port) {
322328
log("SocketID "+info.socketId+" connection closed" + ((port) ? " for port " + port.path + "." : "."), mDeep);
323329
}

0 commit comments

Comments
 (0)