Skip to content

Commit ba8621b

Browse files
committed
Adds chip select delay option
1 parent 31c441d commit ba8621b

File tree

2 files changed

+99
-14
lines changed

2 files changed

+99
-14
lines changed

node/tessel-export.js

+42-13
Original file line numberDiff line numberDiff line change
@@ -917,6 +917,8 @@ Tessel.SPI = function(params, port) {
917917

918918
this.chipSelectActive = params.chipSelectActive === 'high' || params.chipSelectActive === 1 ? 1 : 0;
919919

920+
this.chipSelectDelay = (params.chipSelectDelayUs/1e3) || 0;
921+
920922
if (this.chipSelectActive) {
921923
// active high, pull low for now
922924
this.chipSelect.low();
@@ -969,11 +971,21 @@ Tessel.SPI = function(params, port) {
969971
};
970972

971973
Tessel.SPI.prototype.send = function(data, callback) {
972-
this._port.cork();
974+
973975
this.chipSelect.low();
974-
this._port._tx(data, callback);
975-
this.chipSelect.high();
976-
this._port.uncork();
976+
977+
setTimeout(() => {
978+
// Only cork if we have specified a target delay, otherwise delay increases
979+
if (this.chipSelectDelay > 0) {
980+
this._port.cork();
981+
}
982+
this._port._tx(data, callback);
983+
this.chipSelect.high();
984+
if (this.chipSelectDelay > 0) {
985+
this._port.uncork();
986+
}
987+
}, this.chipSelectDelay);
988+
977989
};
978990

979991
Tessel.SPI.prototype.disable = function() {
@@ -984,19 +996,36 @@ Tessel.SPI.prototype.disable = function() {
984996
};
985997

986998
Tessel.SPI.prototype.receive = function(data_len, callback) {
987-
this._port.cork();
999+
9881000
this.chipSelect.low();
989-
this._port._rx(data_len, callback);
990-
this.chipSelect.high();
991-
this._port.uncork();
1001+
1002+
setTimeout(() => {
1003+
// Only cork if we have specified a target delay, otherwise delay increases
1004+
if (this.chipSelectDelay > 0) {
1005+
this._port.cork();
1006+
}
1007+
this._port._rx(data_len, callback);
1008+
if (this.chipSelectDelay > 0) {
1009+
this._port.uncork();
1010+
}
1011+
}, this.chipSelectDelay);
9921012
};
9931013

9941014
Tessel.SPI.prototype.transfer = function(data, callback) {
995-
this._port.cork();
1015+
9961016
this.chipSelect.low();
997-
this._port._txrx(data, callback);
998-
this.chipSelect.high();
999-
this._port.uncork();
1017+
1018+
setTimeout(() => {
1019+
// Only cork if we have specified a target delay, otherwise delay increases
1020+
if (this.chipSelectDelay > 0) {
1021+
this._port.cork();
1022+
}
1023+
this._port._txrx(data, callback);
1024+
this.chipSelect.high();
1025+
if (this.chipSelectDelay > 0) {
1026+
this._port.uncork();
1027+
}
1028+
}, this.chipSelectDelay);
10001029
};
10011030

10021031
Tessel.UART = function(port, options) {
@@ -1509,7 +1538,7 @@ function getWifiInfo() {
15091538
if (bcastMatches === null) {
15101539
recursiveWifi(network);
15111540
} else {
1512-
// Successful matches will have a result that looks like:
1541+
// Successful matches will have a result that looks like:
15131542
// ["Bcast:0.0.0.0", "Bcast", "0.0.0.0"]
15141543
if (bcastMatches.length === 3) {
15151544
network.ip = bcastMatches[2];

node/test/unit/tessel.js

+57-1
Original file line numberDiff line numberDiff line change
@@ -2354,7 +2354,10 @@ exports['Tessel.SPI'] = {
23542354
return this.socket;
23552355
}.bind(this));
23562356

2357-
this.tessel = new Tessel();
2357+
// We want to disable other ports so that we can pass around mock data.
2358+
// Otherwise, port A and B get 'readable' events without having any
2359+
// queued callbacks (because they are queued on the port we create below).
2360+
this.tessel = new Tessel({ ports: {A: false, B: false} });
23582361

23592362
this.cork = sandbox.stub(Tessel.Port.prototype, 'cork');
23602363
this.uncork = sandbox.stub(Tessel.Port.prototype, 'uncork');
@@ -2432,6 +2435,59 @@ exports['Tessel.SPI'] = {
24322435
test.ok(this.spiDisable.calledOnce, true);
24332436

24342437
test.done();
2438+
},
2439+
chipSelectDelayOptions: function(test) {
2440+
test.expect(2);
2441+
2442+
var delayUs = 10000;
2443+
var usToMs = 1000;
2444+
2445+
var s1 = new this.port.SPI();
2446+
2447+
var s2 = new this.port.SPI({chipSelectDelayUs: delayUs});
2448+
2449+
test.equal(s1.chipSelectDelay, 0);
2450+
2451+
test.equal(s2.chipSelectDelay, delayUs/usToMs);
2452+
2453+
test.done();
2454+
},
2455+
chipSelectDelayFunctionality: function(test) {
2456+
test.expect(2);
2457+
2458+
var delayUs = 10000;
2459+
var usToMs = 1000;
2460+
2461+
var s1 = new this.port.SPI({chipSelectDelayUs: delayUs});
2462+
2463+
var timeoutSpy = sandbox.spy(global, 'setTimeout');
2464+
2465+
s1.transfer(new Buffer(1), () => {
2466+
// One timeout called within transfer and one in the test below
2467+
test.equal(timeoutSpy.callCount, 2);
2468+
// The first call (chip select delay) waited an appropriate amount of time)
2469+
test.equal(timeoutSpy.getCall(0).args[1], delayUs/usToMs);
2470+
// Restore setTimeout
2471+
timeoutSpy.restore();
2472+
test.done();
2473+
});
2474+
2475+
// Wait until after the chip select delay has passed
2476+
// and the callback was enqueued. Only return data on the first request
2477+
setTimeout(() => {
2478+
var called = false;
2479+
this.socket.read = () => {
2480+
if (called) {
2481+
return new Buffer([]);
2482+
}
2483+
called = true;
2484+
2485+
return new Buffer([0x84, 0x1]);
2486+
};
2487+
2488+
// Prod the socket to read our buffer
2489+
s1._port.sock.emit('readable');
2490+
}, (delayUs / 10) * 2 );
24352491
}
24362492
};
24372493

0 commit comments

Comments
 (0)