Skip to content

Commit a2646bd

Browse files
committed
Minor performance fixes which ultimately didn't help SPI
1 parent 385e8cb commit a2646bd

File tree

7 files changed

+40
-58
lines changed

7 files changed

+40
-58
lines changed

include/javaproxy.h

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ JNIEnv *g_env;
1616
JavaVM *g_jvm;
1717
jobject g_activity;
1818

19-
#define ARRAY_SIZE 254
20-
2119
#if defined(ANDROID_MODULE)
2220
#define attachCurrentThread() g_jvm->AttachCurrentThread(&g_env, nullptr)
2321
#define detachCurrentThread() g_jvm->DetachCurrentThread()
@@ -73,7 +71,8 @@ struct JavaProxy {
7371
JavaProxy():
7472
_clazz(nullptr),
7573
_instance(nullptr),
76-
_array(nullptr) {
74+
_array(nullptr),
75+
_arraySize(0) {
7776
}
7877

7978
virtual ~JavaProxy() {
@@ -286,23 +285,28 @@ struct JavaProxy {
286285
}
287286

288287
// populate the java byte array with the contents of the basic array
289-
int populateByteArray(int argc, slib_par_t *params, int offset) {
288+
int populateByteArray(int argc, slib_par_t *params, int offset, int arraySize) {
290289
int result;
290+
if (_array && _arraySize < arraySize) {
291+
g_env->DeleteLocalRef(_array);
292+
_array = nullptr;
293+
}
291294
if (!_array) {
292-
_array = g_env->NewByteArray(ARRAY_SIZE);
295+
_array = g_env->NewByteArray(arraySize);
296+
_arraySize = arraySize;
293297
}
294298
jbyte *elements = g_env->GetByteArrayElements(_array, nullptr);
295299
if ((argc - offset) == 1 && is_param_array(argc, params, offset)) {
296300
// argument is an array (assume of ints)
297301
var_s *array = params[offset].var_p;
298302
int size = v_asize(array);
299-
for (int i = 0; i < size && i < ARRAY_SIZE; i++) {
303+
for (int i = 0; i < size && i < arraySize; i++) {
300304
var_s *elem = v_elem(array, i);
301305
elements[i] = v_is_type(elem, V_INT) ? elem->v.i : elem->v.n;
302306
}
303307
result = size;
304308
} else {
305-
for (int i = offset, j = 0; i < argc && i < ARRAY_SIZE; i++, j++) {
309+
for (int i = offset, j = 0; i < argc && i < arraySize; i++, j++) {
306310
elements[j] = get_param_int(argc, params, i, 0);
307311
}
308312
result = argc - offset;
@@ -316,6 +320,7 @@ struct JavaProxy {
316320
jclass _clazz;
317321
jobject _instance;
318322
jbyteArray _array;
323+
int _arraySize;
319324
};
320325

321326
#if defined(ANDROID_MODULE)

ioio/ioio/src/main/java/ioio/smallbasic/IOIOImpl.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,6 @@ public void sync() {
6161

6262
public void waitForConnect(int latency) {
6363
IOUtil.setError(null);
64-
if (latency < 0) {
65-
IOUtil.setHardReset(true);
66-
} else {
67-
TimerUtil.setLatency(latency);
68-
}
6964
IOService.getInstance().start();
7065
handleError();
7166
lock.invoke(IOIO::waitForConnect);

ioio/ioio/src/main/java/ioio/smallbasic/IOService.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@ private void registerPin(int pin) throws IOException {
8686

8787
public class IOServiceLooper implements IOIOLooper {
8888
private IOIO ioio;
89-
private long lastAccessMillis;
9089

9190
@Override
9291
public void disconnected() {
@@ -106,7 +105,7 @@ public void incompatible(IOIO ioio) {
106105

107106
@Override
108107
public void loop() throws ConnectionLostException, InterruptedException {
109-
lastAccessMillis = TimerUtil.tick(lastAccessMillis);
108+
Thread.sleep(0, 5);
110109
for (IOTask next: ioTasks) {
111110
try {
112111
next.loop();
@@ -120,7 +119,6 @@ public void loop() throws ConnectionLostException, InterruptedException {
120119
@Override
121120
public void setup(IOIO ioio) {
122121
this.ioio = ioio;
123-
this.lastAccessMillis = System.currentTimeMillis();
124122
for (IOTask next: ioTasks) {
125123
try {
126124
next.setup(ioio);

ioio/ioio/src/main/java/ioio/smallbasic/SpiMasterImpl.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
public class SpiMasterImpl extends IOTask {
1111
private static final String TAG = "SpiMasterImpl";
12-
private static final int SPI_WRITE_MAX = 63;
12+
private static final int SPI_WRITE_MAX = 62;
1313
private final byte[] BATCH = new byte[SPI_WRITE_MAX];
1414
private final IOLock<SpiMaster> lock = new IOLock<>();
1515
private SpiMaster spiMaster = null;
@@ -57,16 +57,16 @@ public long readWrite(int readLen, final byte[] write, int writeLen) {
5757
public void write(final byte[] write, int writeLen) {
5858
handleError();
5959
lock.invoke((i) -> {
60-
if (writeLen < SPI_WRITE_MAX) {
61-
spiMaster.writeRead(write, writeLen, writeLen, null, 0);
62-
} else {
60+
if (writeLen > SPI_WRITE_MAX) {
6361
int srcPos = 0;
6462
while (srcPos < writeLen) {
65-
int batchLen = Math.min(writeLen - srcPos, SPI_WRITE_MAX);
63+
int batchLen = Math.min(writeLen - srcPos, 2);
6664
System.arraycopy(write, srcPos, BATCH, 0, batchLen);
67-
spiMaster.writeRead(BATCH, batchLen, batchLen, null, 0);
68-
srcPos += SPI_WRITE_MAX;
65+
spiMaster.writeReadAsync(0, BATCH, batchLen, batchLen, null, 0);
66+
srcPos += batchLen;
6967
}
68+
} else {
69+
spiMaster.writeRead(write, writeLen, writeLen, null, 0);
7070
}
7171
});
7272
}
@@ -79,7 +79,7 @@ void loop() throws ConnectionLostException, InterruptedException {
7979
@Override
8080
void setup(IOIO ioio) throws ConnectionLostException {
8181
Log.i(TAG, "setup entered: miso:" + miso + " mosi:" + mosi + " clk:" + clk + " cs:" + slaveSelect);
82-
spiMaster = ioio.openSpiMaster(miso, mosi, clk, slaveSelect, SpiMaster.Rate.RATE_1M);
82+
spiMaster = ioio.openSpiMaster(miso, mosi, clk, slaveSelect, SpiMaster.Rate.RATE_4M);
8383
}
8484

8585
private void pinError(String name) {

ioio/ioio/src/main/java/ioio/smallbasic/TimerUtil.java

Lines changed: 0 additions & 21 deletions
This file was deleted.

ioio/ioio/src/main/java/ioio/smallbasic/pc/SerialPortIOIOConnection.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public boolean canClose() {
3333
@Override
3434
synchronized public void disconnect() {
3535
abort = true;
36-
if (serialPort != null) {
36+
if (serialPort != null && inputStream != null) {
3737
try {
3838
inputStream.close();
3939
} catch (IOException e) {

ioio/main.cpp

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525
#define CLASS_SPIMASTER "ioio/smallbasic/SpiMasterImpl"
2626
#define CLASS_IOIO "ioio/smallbasic/IOIOImpl"
2727
#define CLASS_IOTASK_ID 1
28-
#define SPI_WRITE_MAX 63
29-
#define TWI_WRITE_MAX ARRAY_SIZE
28+
#define SPI_WRITE_MAX 62
29+
#define TWI_WRITE_MAX 255
3030

3131
struct IOTask : JavaProxy {
3232
IOTask() : JavaProxy() {
@@ -47,7 +47,7 @@ struct IOTask : JavaProxy {
4747
// int readWrite(bytes, byte[] write) {
4848
int invokeSpiReadWrite(int argc, slib_par_t *arg, var_s *retval) {
4949
int result = 0;
50-
int writeLen = populateByteArray(argc, arg, 1);
50+
int writeLen = populateByteArray(argc, arg, 1, SPI_WRITE_MAX);
5151
if (writeLen > SPI_WRITE_MAX) {
5252
error(retval, "write array", 1, SPI_WRITE_MAX);
5353
} else if (_instance != nullptr) {
@@ -67,12 +67,17 @@ struct IOTask : JavaProxy {
6767
return result;
6868
}
6969

70-
// int write(byte[] write) {
70+
// int write(byte[] write, int length) {
7171
int invokeSpiWrite(int argc, slib_par_t *arg, var_s *retval) {
7272
int result = 0;
73-
int writeLen = populateByteArray(argc, arg, 0);
74-
if (writeLen > ARRAY_SIZE) {
75-
error(retval, "write array", 1, ARRAY_SIZE);
73+
int maxSize = SPI_WRITE_MAX;
74+
if (is_param_array(argc, arg, 0)) {
75+
// allow an entire LCD to be updated within one IOIOLooper.loop() call
76+
maxSize = v_asize(arg[0].var_p);
77+
}
78+
int writeLen = populateByteArray(argc, arg, 0, maxSize);
79+
if (writeLen > maxSize) {
80+
error(retval, "write array", 1, maxSize);
7681
} else if (_instance != nullptr) {
7782
attachCurrentThread();
7883
jmethodID method = g_env->GetMethodID(_clazz, "write", "([BI)V");
@@ -90,7 +95,7 @@ struct IOTask : JavaProxy {
9095
// int readWrite(int address, byte[] write) {
9196
int invokeTwiReadWrite(int argc, slib_par_t *arg, var_s *retval) {
9297
int result = 0;
93-
int writeLen = populateByteArray(argc, arg, 2);
98+
int writeLen = populateByteArray(argc, arg, 2, TWI_WRITE_MAX);
9499
if (writeLen > TWI_WRITE_MAX) {
95100
error(retval, "write array", 1, TWI_WRITE_MAX);
96101
} else if (_instance != nullptr) {
@@ -114,7 +119,7 @@ struct IOTask : JavaProxy {
114119
// int write(int address, byte[] write) {
115120
int invokeTwiWrite(int argc, slib_par_t *arg, var_s *retval) {
116121
int result = 0;
117-
int writeLen = populateByteArray(argc, arg, 1);
122+
int writeLen = populateByteArray(argc, arg, 1, TWI_WRITE_MAX);
118123
if (writeLen > TWI_WRITE_MAX) {
119124
error(retval, "write array", 1, TWI_WRITE_MAX);
120125
} else if (_instance != nullptr) {
@@ -155,7 +160,7 @@ static int cmd_twimaster_readwrite(var_s *self, int argc, slib_par_t *arg, var_s
155160
int result = 0;
156161
auto readBytes = get_param_int(argc, arg, 1, 0);
157162
if (argc < 2) {
158-
error(retval, "TwiMaster.readWrite(address, read-bytes, [data]", 2, ARRAY_SIZE);
163+
error(retval, "TwiMaster.readWrite(address, read-bytes, [data]", 2, TWI_WRITE_MAX);
159164
} else if (readBytes < 1 || readBytes > 8) {
160165
error(retval, "read-bytes value out of range. Expected a number between 1 and 8");
161166
} else {
@@ -170,7 +175,7 @@ static int cmd_twimaster_readwrite(var_s *self, int argc, slib_par_t *arg, var_s
170175
static int cmd_twimaster_write(var_s *self, int argc, slib_par_t *arg, var_s *retval) {
171176
int result = 0;
172177
if (argc < 2) {
173-
error(retval, "TwiMaster.write", 2, ARRAY_SIZE);
178+
error(retval, "TwiMaster.write", 2, TWI_WRITE_MAX);
174179
} else {
175180
int id = get_io_class_id(self, retval);
176181
if (id != -1) {
@@ -184,7 +189,7 @@ static int cmd_spimaster_readwrite(var_s *self, int argc, slib_par_t *arg, var_s
184189
int result = 0;
185190
auto readBytes = get_param_int(argc, arg, 0, 0);
186191
if (argc < 2) {
187-
error(retval, "SpiMaster.readWrite(read-bytes, [data]", 2, ARRAY_SIZE);
192+
error(retval, "SpiMaster.readWrite(read-bytes, [data]", 2, SPI_WRITE_MAX);
188193
} else if (readBytes < 1 || readBytes > 8) {
189194
error(retval, "read-bytes value out of range. Expected a number between 1 and 8");
190195
} else {
@@ -199,7 +204,7 @@ static int cmd_spimaster_readwrite(var_s *self, int argc, slib_par_t *arg, var_s
199204
static int cmd_spimaster_write(var_s *self, int argc, slib_par_t *arg, var_s *retval) {
200205
int result = 0;
201206
if (argc < 1) {
202-
error(retval, "SpiMaster.write", 1, ARRAY_SIZE);
207+
error(retval, "SpiMaster.write", 1, SPI_WRITE_MAX);
203208
} else {
204209
int id = get_io_class_id(self, retval);
205210
if (id != -1) {

0 commit comments

Comments
 (0)