Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit e9356c0

Browse files
committedSep 3, 2016
Adds chip select delay option
1 parent 31c441d commit e9356c0

File tree

2 files changed

+83
-14
lines changed

2 files changed

+83
-14
lines changed
 

‎node/tessel-export.js

+28-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/10 || 0;
921+
920922
if (this.chipSelectActive) {
921923
// active high, pull low for now
922924
this.chipSelect.low();
@@ -969,11 +971,16 @@ 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+
this._port.cork();
979+
this._port._tx(data, callback);
980+
this.chipSelect.high();
981+
this._port.uncork();
982+
}, this.chipSelectDelay);
983+
977984
};
978985

979986
Tessel.SPI.prototype.disable = function() {
@@ -984,19 +991,27 @@ Tessel.SPI.prototype.disable = function() {
984991
};
985992

986993
Tessel.SPI.prototype.receive = function(data_len, callback) {
987-
this._port.cork();
994+
988995
this.chipSelect.low();
989-
this._port._rx(data_len, callback);
990-
this.chipSelect.high();
991-
this._port.uncork();
996+
997+
setTimeout(() => {
998+
this._port.cork();
999+
this._port._rx(data_len, callback);
1000+
this.chipSelect.high();
1001+
this._port.uncork();
1002+
}, this.chipSelectDelay);
9921003
};
9931004

9941005
Tessel.SPI.prototype.transfer = function(data, callback) {
995-
this._port.cork();
1006+
9961007
this.chipSelect.low();
997-
this._port._txrx(data, callback);
998-
this.chipSelect.high();
999-
this._port.uncork();
1008+
1009+
setTimeout(() => {
1010+
this._port.cork();
1011+
this._port._txrx(data, callback);
1012+
this.chipSelect.high();
1013+
this._port.uncork();
1014+
}, this.chipSelectDelay);
10001015
};
10011016

10021017
Tessel.UART = function(port, options) {
@@ -1509,7 +1524,7 @@ function getWifiInfo() {
15091524
if (bcastMatches === null) {
15101525
recursiveWifi(network);
15111526
} else {
1512-
// Successful matches will have a result that looks like:
1527+
// Successful matches will have a result that looks like:
15131528
// ["Bcast:0.0.0.0", "Bcast", "0.0.0.0"]
15141529
if (bcastMatches.length === 3) {
15151530
network.ip = bcastMatches[2];

‎node/test/unit/tessel.js

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

0 commit comments

Comments
 (0)
Please sign in to comment.