Skip to content

Commit 7b669d7

Browse files
committed
Implemented SPI batched write for slight performance improvement
1 parent fb890be commit 7b669d7

File tree

4 files changed

+36
-17
lines changed

4 files changed

+36
-17
lines changed

include/javaproxy.h

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

19-
// TWI limit is 255, SPI limit is 64
20-
#define ARRAY_SIZE 255
19+
#define ARRAY_SIZE 254
2120

2221
#if defined(ANDROID_MODULE)
2322
#define attachCurrentThread() g_jvm->AttachCurrentThread(&g_env, nullptr)

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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
package ioio.smallbasic;
22

3-
import java.io.IOException;
4-
import java.util.Arrays;
5-
63
import ioio.lib.api.IOIO;
74
import ioio.lib.api.SpiMaster;
85
import ioio.lib.api.exception.ConnectionLostException;
96
import ioio.lib.spi.Log;
107

8+
import java.io.IOException;
9+
1110
public class SpiMasterImpl extends IOTask {
1211
private static final String TAG = "SpiMasterImpl";
12+
private static final int SPI_WRITE_MAX = 63;
13+
private final byte[] BATCH = new byte[SPI_WRITE_MAX];
1314
private final IOLock<SpiMaster> lock = new IOLock<>();
1415
private SpiMaster spiMaster = null;
1516
private int miso;
@@ -56,7 +57,17 @@ public long readWrite(int readLen, final byte[] write, int writeLen) {
5657
public void write(final byte[] write, int writeLen) {
5758
handleError();
5859
lock.invoke((i) -> {
59-
spiMaster.writeRead(write, writeLen, writeLen, null, 0);
60+
if (writeLen < SPI_WRITE_MAX) {
61+
spiMaster.writeRead(write, writeLen, writeLen, null, 0);
62+
} else {
63+
int srcPos = 0;
64+
while (srcPos < writeLen) {
65+
int batchLen = Math.min(writeLen - srcPos, SPI_WRITE_MAX);
66+
System.arraycopy(write, srcPos, BATCH, 0, batchLen);
67+
spiMaster.writeRead(BATCH, batchLen, batchLen, null, 0);
68+
srcPos += SPI_WRITE_MAX;
69+
}
70+
}
6071
});
6172
}
6273

ioio/main.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +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
2830

2931
struct IOTask : JavaProxy {
3032
IOTask() : JavaProxy() {
@@ -46,8 +48,8 @@ struct IOTask : JavaProxy {
4648
int invokeSpiReadWrite(int argc, slib_par_t *arg, var_s *retval) {
4749
int result = 0;
4850
int writeLen = populateByteArray(argc, arg, 1);
49-
if (writeLen > ARRAY_SIZE) {
50-
error(retval, "write array", 1, ARRAY_SIZE);
51+
if (writeLen > SPI_WRITE_MAX) {
52+
error(retval, "write array", 1, SPI_WRITE_MAX);
5153
} else if (_instance != nullptr) {
5254
attachCurrentThread();
5355
jmethodID method = g_env->GetMethodID(_clazz, "readWrite", "(I[BI)J");
@@ -89,8 +91,8 @@ struct IOTask : JavaProxy {
8991
int invokeTwiReadWrite(int argc, slib_par_t *arg, var_s *retval) {
9092
int result = 0;
9193
int writeLen = populateByteArray(argc, arg, 2);
92-
if (writeLen > ARRAY_SIZE) {
93-
error(retval, "write array", 1, ARRAY_SIZE);
94+
if (writeLen > TWI_WRITE_MAX) {
95+
error(retval, "write array", 1, TWI_WRITE_MAX);
9496
} else if (_instance != nullptr) {
9597
attachCurrentThread();
9698
jmethodID method = g_env->GetMethodID(_clazz, "readWrite", "(II[BI)J");
@@ -113,8 +115,8 @@ struct IOTask : JavaProxy {
113115
int invokeTwiWrite(int argc, slib_par_t *arg, var_s *retval) {
114116
int result = 0;
115117
int writeLen = populateByteArray(argc, arg, 1);
116-
if (writeLen > ARRAY_SIZE) {
117-
error(retval, "write array", 1, ARRAY_SIZE);
118+
if (writeLen > TWI_WRITE_MAX) {
119+
error(retval, "write array", 1, TWI_WRITE_MAX);
118120
} else if (_instance != nullptr) {
119121
attachCurrentThread();
120122
jmethodID method = g_env->GetMethodID(_clazz, "write", "(I[BI)V");

ioio/samples/st7735s.bas

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ rem
1616
import ioio
1717

1818
const ST7735_SLPOUT 0x11
19+
const ST7735_DISPOFF 0x28
1920
const ST7735_DISPON 0x29
2021
const ST7735_CASET 0x2A
2122
const ST7735_RASET 0x2B
@@ -82,7 +83,7 @@ sub resetDisplay()
8283
rstOut.write(0)
8384
delay 50
8485
rstOut.write(1)
85-
delay 50
86+
delay 150
8687
end
8788

8889
sub initST7735S()
@@ -133,16 +134,18 @@ end
133134
rem Fill display with a given RGB value
134135
sub fill_LCD(r, g, b)
135136
local i, _data
136-
Set_LCD_for_write_at_X_Y(0, 0)
137+
' higher values didn't improve performance
138+
local bufSize = 40
137139

138-
for i = 1 to 21
140+
for i = 1 to bufSize
139141
_data << b
140142
_data << g
141143
_data << r
142144
next
143145

146+
Set_LCD_for_write_at_X_Y(0, 0)
144147
rsOut.write(1)
145-
for i = 0 to (128 * 128) / 21
148+
for i = 0 to (128 * 128) / bufSize
146149
spi.write(_data)
147150
next i
148151
end
@@ -154,4 +157,8 @@ sub put_Pixel(x, y, r, g, b)
154157
end
155158

156159
initST7735S()
157-
fill_LCD(1, 212, 31)
160+
randomize
161+
t1 = timer
162+
fill_LCD(rnd*255, rnd*255, rnd*255)
163+
print format("Elap: ###", timer-t1)
164+
delay 5000

0 commit comments

Comments
 (0)