Skip to content

Commit ab07355

Browse files
committed
TEENSY: Run from SD card
1 parent 6c20b9a commit ab07355

File tree

8 files changed

+109
-135
lines changed

8 files changed

+109
-135
lines changed

src/platform/teensy/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ include_directories(${MODULES}/SPI)
4444
include_directories(${MODULES}/Adafruit_SSD1306)
4545
include_directories(${MODULES}/USBHost_t36)
4646
include_directories(${MODULES}/Wire)
47+
include_directories(${MODULES}/SD/src)
48+
include_directories(${MODULES}/SdFat/src)
4749

4850
# include config.h and common headers
4951
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../..)

src/platform/teensy/libs/CMakeLists.txt

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ set(SPI_DIR ${MODULES_DIR}/SPI)
1414
set(SSD_DIR ${MODULES_DIR}/Adafruit_SSD1306)
1515
set(USB_DIR ${MODULES_DIR}/USBHost_t36)
1616
set(WIRE_DIR ${MODULES_DIR}/Wire)
17+
set(SD_DIR ${MODULES_DIR}/SD/src)
18+
set(SDFAT_DIR ${MODULES_DIR}/SdFat/src)
1719

1820
set(SOURCES
1921
${ADC_DIR}/ADC.cpp
@@ -115,6 +117,51 @@ set(SOURCES
115117
${WIRE_DIR}/Wire.cpp
116118
${WIRE_DIR}/WireIMXRT.cpp
117119
${WIRE_DIR}/WireKinetis.cpp
120+
${SD_DIR}/SD.cpp
121+
${SDFAT_DIR}/SdCard/SdCardInfo.cpp
122+
${SDFAT_DIR}/SdCard/SdioTeensy.cpp
123+
${SDFAT_DIR}/SdCard/SdSpiCard.cpp
124+
${SDFAT_DIR}/SpiDriver/SdSpiChipSelect.cpp
125+
${SDFAT_DIR}/SpiDriver/SdSpiArtemis.cpp
126+
${SDFAT_DIR}/SpiDriver/SdSpiDue.cpp
127+
${SDFAT_DIR}/SpiDriver/SdSpiESP.cpp
128+
${SDFAT_DIR}/SpiDriver/SdSpiParticle.cpp
129+
${SDFAT_DIR}/SpiDriver/SdSpiSTM32.cpp
130+
${SDFAT_DIR}/SpiDriver/SdSpiSTM32Core.cpp
131+
${SDFAT_DIR}/SpiDriver/SdSpiTeensy3.cpp
132+
${SDFAT_DIR}/iostream/istream.cpp
133+
${SDFAT_DIR}/iostream/ostream.cpp
134+
${SDFAT_DIR}/iostream/StdioStream.cpp
135+
${SDFAT_DIR}/iostream/StreamBaseClass.cpp
136+
${SDFAT_DIR}/FsLib/FsFile.cpp
137+
${SDFAT_DIR}/FsLib/FsNew.cpp
138+
${SDFAT_DIR}/FsLib/FsVolume.cpp
139+
${SDFAT_DIR}/FatLib/FatDbg.cpp
140+
${SDFAT_DIR}/FatLib/FatFile.cpp
141+
${SDFAT_DIR}/FatLib/FatFileLFN.cpp
142+
${SDFAT_DIR}/FatLib/FatFilePrint.cpp
143+
${SDFAT_DIR}/FatLib/FatFileSFN.cpp
144+
${SDFAT_DIR}/FatLib/FatFormatter.cpp
145+
${SDFAT_DIR}/FatLib/FatName.cpp
146+
${SDFAT_DIR}/FatLib/FatPartition.cpp
147+
${SDFAT_DIR}/FatLib/FatVolume.cpp
148+
${SDFAT_DIR}/ExFatLib/ExFatDbg.cpp
149+
${SDFAT_DIR}/ExFatLib/ExFatFile.cpp
150+
${SDFAT_DIR}/ExFatLib/ExFatFilePrint.cpp
151+
${SDFAT_DIR}/ExFatLib/ExFatFileWrite.cpp
152+
${SDFAT_DIR}/ExFatLib/ExFatFormatter.cpp
153+
${SDFAT_DIR}/ExFatLib/ExFatName.cpp
154+
${SDFAT_DIR}/ExFatLib/ExFatPartition.cpp
155+
${SDFAT_DIR}/ExFatLib/ExFatVolume.cpp
156+
${SDFAT_DIR}/ExFatLib/upcase.cpp
157+
${SDFAT_DIR}/common/FmtNumber.cpp
158+
${SDFAT_DIR}/common/FsCache.cpp
159+
${SDFAT_DIR}/common/FsDateTime.cpp
160+
${SDFAT_DIR}/common/FsName.cpp
161+
${SDFAT_DIR}/common/FsStructs.cpp
162+
${SDFAT_DIR}/common/FsUtf.cpp
163+
${SDFAT_DIR}/common/PrintBasic.cpp
164+
${SDFAT_DIR}/common/upcase.cpp
118165
)
119166

120167
add_library(libs STATIC ${SOURCES})
@@ -127,4 +174,6 @@ target_include_directories(libs PRIVATE ${MODULES_DIR}/SdFat/src)
127174
target_include_directories(libs PRIVATE ${SPI_DIR})
128175
target_include_directories(libs PRIVATE ${TEENSY_DIR})
129176
target_include_directories(libs PRIVATE ${WIRE_DIR})
177+
target_include_directories(libs PRIVATE ${SD_DIR})
178+
target_include_directories(libs PRIVATE ${SDFAT_DIR})
130179

src/platform/teensy/main.bas

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +0,0 @@
1-
import teensy
2-
import ssd1306 as display
3-
4-
const welcome = "Hello SmallBASIC!!"
5-
display.init()
6-
display.dim(1)
7-
display.print(welcome)
8-
9-
const sz = display.getTextSize(welcome)
10-
display.setCursor(0, sz.height + 2)
11-
display.flush()
12-
13-
const out = teensy.openDigitalOutput(13)
14-
const usb = teensy.openSerial()
15-
16-
sub show_data(byref s)
17-
display.clear()
18-
display.setCursor(0,0)
19-
display.print("Received:")
20-
display.setCursor(0, sz.height + 2)
21-
display.print(s)
22-
display.flush()
23-
end
24-
25-
sub blink
26-
local i
27-
for i = 0 to 5
28-
out.write(value)
29-
value = !value
30-
delay 10 + (i * 10)
31-
next
32-
out.write(0)
33-
end
34-
35-
while 1
36-
if usb.ready() then
37-
s = usb.receive()
38-
if (len(s) > 0) then
39-
show_data(s)
40-
usb.send(sbver)
41-
blink
42-
endif
43-
else
44-
delay 10
45-
endif
46-
wend

src/platform/teensy/setup.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ DIR=`pwd`
77
git clone https://github.com/PaulStoffregen/SPI.git && \
88
git clone https://github.com/PaulStoffregen/Wire.git && \
99
git clone https://github.com/PaulStoffregen/USBHost_t36.git && \
10+
git clone https://github.com/PaulStoffregen/SD.git && \
1011
git clone https://github.com/PaulStoffregen/SdFat.git && \
1112
git clone https://github.com/PaulStoffregen/teensy_loader_cli.git && \
1213
git clone https://github.com/pedvide/ADC && \

src/platform/teensy/src/device.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
//
2525
void serial_init() {
2626
if (CrashReport) {
27-
while(!Serial) {
28-
delay(10);
27+
while (!Serial) {
28+
delay(250);
2929
}
3030
Serial.print(CrashReport);
3131
}
@@ -67,12 +67,12 @@ int dev_events(int wait_flag) {
6767
if (wait_flag) {
6868
delay(10);
6969
}
70-
#if INTERACTIVE
70+
// #if INTERACTIVE
7171
// break when new code available
7272
result = Serial.available() > 0 ? -2 : 0;
73-
#else
74-
result = 0;
75-
#endif
73+
// #else
74+
// result = 0;
75+
//#endif
7676
yield();
7777
return result;
7878
}

src/platform/teensy/src/main.cpp

Lines changed: 50 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,17 @@
88
//
99

1010
#include <Arduino.h>
11+
#include <SPI.h>
12+
#include <SD.h>
13+
#include "common/device.h"
1114
#include "config.h"
1215
#include "common/sbapp.h"
1316
#include "main_bas.h"
1417
#include "module.h"
1518
#include "ui/strlib.h"
1619

1720
#define MAIN_BAS "__main_bas__"
18-
#define SERIAL_BAS "__serial_bas__"
21+
#define SERIAL_SD_BAS "__serial_sd_bas__"
1922

2023
strlib::String buffer;
2124

@@ -57,7 +60,7 @@ char *dev_read(const char *fileName) {
5760
data = (char *)malloc(main_bas_len + 1);
5861
memcpy(data, main_bas, main_bas_len);
5962
data[main_bas_len] = '\0';
60-
} else if (strcmp(fileName, SERIAL_BAS) == 0) {
63+
} else if (strcmp(fileName, SERIAL_SD_BAS) == 0) {
6164
int len = buffer.length();
6265
data = (char *)malloc(len + 1);
6366
memcpy(data, buffer.c_str(), len);
@@ -109,10 +112,10 @@ void serial_read() {
109112
eof = true;
110113
} else {
111114
delay(250);
112-
dev_print("\rInteractive mode - waiting for data...");
113115
}
114116
}
115-
dev_printf("received %d bytes.\r\n", buffer.length());
117+
118+
dev_printf("Received %d bytes.\r\n", buffer.length());
116119
}
117120

118121
void print_source_line(const char *text, int line, bool last) {
@@ -169,8 +172,14 @@ void print_error(char *source) {
169172

170173
void interactive_main() {
171174
while (true) {
175+
while (!Serial) {
176+
delay(250);
177+
}
178+
179+
dev_print("Interactive mode - waiting for data...\r\n");
180+
172181
serial_read();
173-
if (!sbasic_main(SERIAL_BAS)) {
182+
if (!sbasic_main(SERIAL_SD_BAS)) {
174183
print_error((char *)buffer.c_str());
175184
}
176185
}
@@ -179,16 +188,41 @@ void interactive_main() {
179188
extern "C" int main(void) {
180189
setup();
181190

182-
#if INTERACTIVE
183-
interactive_main();
184-
#else
185-
if (!sbasic_main(MAIN_BAS)) {
186-
dev_print("Error: run failed\n");
187-
opt_quiet = 0;
188-
opt_verbose = 1;
189-
sbasic_main(MAIN_BAS);
191+
if (SD.begin(BUILTIN_SDCARD)) {
192+
File sdFile = SD.open("/MAIN.BAS", FILE_READ);
193+
if (sdFile) {
194+
uint32_t fileSize = sdFile.available();
195+
char *bufferSD = new char[fileSize + 1];
196+
197+
sdFile.read(bufferSD, fileSize);
198+
bufferSD[fileSize] = '\0';
199+
buffer.clear();
200+
buffer.append(bufferSD);
201+
202+
delete[] bufferSD;
203+
sdFile.close();
204+
205+
if (!sbasic_main(SERIAL_SD_BAS)) {
206+
while (!Serial) {
207+
delay(250);
208+
}
209+
dev_print("Error executing main.bas from SD card:\n");
210+
print_error((char *)buffer.c_str());
211+
} else {
212+
dev_print("main.bas from SD card ended\n");
213+
}
214+
}
215+
} else if (main_bas_len > 0) {
216+
if (!sbasic_main(MAIN_BAS)) {
217+
while (!Serial) {
218+
delay(250);
219+
}
220+
dev_print("Error executing main.bas from memory:\n");
221+
print_error((char *) main_bas);
222+
} else {
223+
dev_print("main.bas ended");
224+
}
190225
} else {
191-
dev_print("main.bas ended");
226+
interactive_main();
192227
}
193-
#endif
194-
}
228+
}

src/platform/teensy/src/main_bas.h

Lines changed: 1 addition & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,3 @@
11
unsigned char main_bas[] = {
2-
0x69, 0x6d, 0x70, 0x6f, 0x72, 0x74, 0x20, 0x74, 0x65, 0x65, 0x6e, 0x73,
3-
0x79, 0x0a, 0x69, 0x6d, 0x70, 0x6f, 0x72, 0x74, 0x20, 0x73, 0x73, 0x64,
4-
0x31, 0x33, 0x30, 0x36, 0x20, 0x61, 0x73, 0x20, 0x64, 0x69, 0x73, 0x70,
5-
0x6c, 0x61, 0x79, 0x0a, 0x0a, 0x63, 0x6f, 0x6e, 0x73, 0x74, 0x20, 0x77,
6-
0x65, 0x6c, 0x63, 0x6f, 0x6d, 0x65, 0x20, 0x3d, 0x20, 0x22, 0x48, 0x65,
7-
0x6c, 0x6c, 0x6f, 0x20, 0x53, 0x6d, 0x61, 0x6c, 0x6c, 0x42, 0x41, 0x53,
8-
0x49, 0x43, 0x21, 0x21, 0x22, 0x0a, 0x64, 0x69, 0x73, 0x70, 0x6c, 0x61,
9-
0x79, 0x2e, 0x69, 0x6e, 0x69, 0x74, 0x28, 0x29, 0x0a, 0x64, 0x69, 0x73,
10-
0x70, 0x6c, 0x61, 0x79, 0x2e, 0x64, 0x69, 0x6d, 0x28, 0x31, 0x29, 0x0a,
11-
0x64, 0x69, 0x73, 0x70, 0x6c, 0x61, 0x79, 0x2e, 0x70, 0x72, 0x69, 0x6e,
12-
0x74, 0x28, 0x77, 0x65, 0x6c, 0x63, 0x6f, 0x6d, 0x65, 0x29, 0x0a, 0x0a,
13-
0x63, 0x6f, 0x6e, 0x73, 0x74, 0x20, 0x73, 0x7a, 0x20, 0x3d, 0x20, 0x64,
14-
0x69, 0x73, 0x70, 0x6c, 0x61, 0x79, 0x2e, 0x67, 0x65, 0x74, 0x54, 0x65,
15-
0x78, 0x74, 0x53, 0x69, 0x7a, 0x65, 0x28, 0x77, 0x65, 0x6c, 0x63, 0x6f,
16-
0x6d, 0x65, 0x29, 0x0a, 0x64, 0x69, 0x73, 0x70, 0x6c, 0x61, 0x79, 0x2e,
17-
0x73, 0x65, 0x74, 0x43, 0x75, 0x72, 0x73, 0x6f, 0x72, 0x28, 0x30, 0x2c,
18-
0x20, 0x73, 0x7a, 0x2e, 0x68, 0x65, 0x69, 0x67, 0x68, 0x74, 0x20, 0x2b,
19-
0x20, 0x32, 0x29, 0x0a, 0x64, 0x69, 0x73, 0x70, 0x6c, 0x61, 0x79, 0x2e,
20-
0x66, 0x6c, 0x75, 0x73, 0x68, 0x28, 0x29, 0x0a, 0x0a, 0x63, 0x6f, 0x6e,
21-
0x73, 0x74, 0x20, 0x6f, 0x75, 0x74, 0x20, 0x3d, 0x20, 0x74, 0x65, 0x65,
22-
0x6e, 0x73, 0x79, 0x2e, 0x6f, 0x70, 0x65, 0x6e, 0x44, 0x69, 0x67, 0x69,
23-
0x74, 0x61, 0x6c, 0x4f, 0x75, 0x74, 0x70, 0x75, 0x74, 0x28, 0x31, 0x33,
24-
0x29, 0x0a, 0x63, 0x6f, 0x6e, 0x73, 0x74, 0x20, 0x75, 0x73, 0x62, 0x20,
25-
0x3d, 0x20, 0x74, 0x65, 0x65, 0x6e, 0x73, 0x79, 0x2e, 0x6f, 0x70, 0x65,
26-
0x6e, 0x53, 0x65, 0x72, 0x69, 0x61, 0x6c, 0x28, 0x29, 0x0a, 0x0a, 0x73,
27-
0x75, 0x62, 0x20, 0x73, 0x68, 0x6f, 0x77, 0x5f, 0x64, 0x61, 0x74, 0x61,
28-
0x28, 0x62, 0x79, 0x72, 0x65, 0x66, 0x20, 0x73, 0x29, 0x0a, 0x20, 0x20,
29-
0x64, 0x69, 0x73, 0x70, 0x6c, 0x61, 0x79, 0x2e, 0x63, 0x6c, 0x65, 0x61,
30-
0x72, 0x28, 0x29, 0x0a, 0x20, 0x20, 0x64, 0x69, 0x73, 0x70, 0x6c, 0x61,
31-
0x79, 0x2e, 0x73, 0x65, 0x74, 0x43, 0x75, 0x72, 0x73, 0x6f, 0x72, 0x28,
32-
0x30, 0x2c, 0x30, 0x29, 0x0a, 0x20, 0x20, 0x64, 0x69, 0x73, 0x70, 0x6c,
33-
0x61, 0x79, 0x2e, 0x70, 0x72, 0x69, 0x6e, 0x74, 0x28, 0x22, 0x52, 0x65,
34-
0x63, 0x65, 0x69, 0x76, 0x65, 0x64, 0x3a, 0x22, 0x29, 0x0a, 0x20, 0x20,
35-
0x64, 0x69, 0x73, 0x70, 0x6c, 0x61, 0x79, 0x2e, 0x73, 0x65, 0x74, 0x43,
36-
0x75, 0x72, 0x73, 0x6f, 0x72, 0x28, 0x30, 0x2c, 0x20, 0x73, 0x7a, 0x2e,
37-
0x68, 0x65, 0x69, 0x67, 0x68, 0x74, 0x20, 0x2b, 0x20, 0x32, 0x29, 0x0a,
38-
0x20, 0x20, 0x64, 0x69, 0x73, 0x70, 0x6c, 0x61, 0x79, 0x2e, 0x70, 0x72,
39-
0x69, 0x6e, 0x74, 0x28, 0x73, 0x29, 0x0a, 0x20, 0x20, 0x64, 0x69, 0x73,
40-
0x70, 0x6c, 0x61, 0x79, 0x2e, 0x66, 0x6c, 0x75, 0x73, 0x68, 0x28, 0x29,
41-
0x0a, 0x65, 0x6e, 0x64, 0x0a, 0x0a, 0x73, 0x75, 0x62, 0x20, 0x62, 0x6c,
42-
0x69, 0x6e, 0x6b, 0x0a, 0x20, 0x20, 0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x20,
43-
0x69, 0x0a, 0x20, 0x20, 0x66, 0x6f, 0x72, 0x20, 0x69, 0x20, 0x3d, 0x20,
44-
0x30, 0x20, 0x74, 0x6f, 0x20, 0x35, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x6f,
45-
0x75, 0x74, 0x2e, 0x77, 0x72, 0x69, 0x74, 0x65, 0x28, 0x76, 0x61, 0x6c,
46-
0x75, 0x65, 0x29, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x76, 0x61, 0x6c, 0x75,
47-
0x65, 0x20, 0x3d, 0x20, 0x21, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x0a, 0x20,
48-
0x20, 0x20, 0x20, 0x64, 0x65, 0x6c, 0x61, 0x79, 0x20, 0x31, 0x30, 0x20,
49-
0x2b, 0x20, 0x28, 0x69, 0x20, 0x2a, 0x20, 0x31, 0x30, 0x29, 0x0a, 0x20,
50-
0x20, 0x6e, 0x65, 0x78, 0x74, 0x0a, 0x20, 0x20, 0x6f, 0x75, 0x74, 0x2e,
51-
0x77, 0x72, 0x69, 0x74, 0x65, 0x28, 0x30, 0x29, 0x0a, 0x65, 0x6e, 0x64,
52-
0x0a, 0x0a, 0x77, 0x68, 0x69, 0x6c, 0x65, 0x20, 0x31, 0x0a, 0x20, 0x20,
53-
0x69, 0x66, 0x20, 0x75, 0x73, 0x62, 0x2e, 0x72, 0x65, 0x61, 0x64, 0x79,
54-
0x28, 0x29, 0x20, 0x74, 0x68, 0x65, 0x6e, 0x0a, 0x20, 0x20, 0x20, 0x20,
55-
0x73, 0x20, 0x3d, 0x20, 0x75, 0x73, 0x62, 0x2e, 0x72, 0x65, 0x63, 0x65,
56-
0x69, 0x76, 0x65, 0x28, 0x29, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x69, 0x66,
57-
0x20, 0x28, 0x6c, 0x65, 0x6e, 0x28, 0x73, 0x29, 0x20, 0x3e, 0x20, 0x30,
58-
0x29, 0x20, 0x74, 0x68, 0x65, 0x6e, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20,
59-
0x20, 0x73, 0x68, 0x6f, 0x77, 0x5f, 0x64, 0x61, 0x74, 0x61, 0x28, 0x73,
60-
0x29, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x75, 0x73, 0x62, 0x2e,
61-
0x73, 0x65, 0x6e, 0x64, 0x28, 0x73, 0x62, 0x76, 0x65, 0x72, 0x29, 0x0a,
62-
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x62, 0x6c, 0x69, 0x6e, 0x6b, 0x0a,
63-
0x20, 0x20, 0x20, 0x20, 0x65, 0x6e, 0x64, 0x69, 0x66, 0x0a, 0x20, 0x20,
64-
0x65, 0x6c, 0x73, 0x65, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x64, 0x65, 0x6c,
65-
0x61, 0x79, 0x20, 0x31, 0x30, 0x0a, 0x20, 0x20, 0x65, 0x6e, 0x64, 0x69,
66-
0x66, 0x0a, 0x77, 0x65, 0x6e, 0x64, 0x0a
672
};
68-
unsigned int main_bas_len = 775;
3+
unsigned int main_bas_len = 0;

src/platform/teensy/src/teensy.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
//
99

1010
#include <Arduino.h>
11-
1211
#include "config.h"
1312
#include "languages/messages.en.h"
1413
#include "include/var_map.h"

0 commit comments

Comments
 (0)