SD Cards, SPIFFS, File commands, EEPROM, and I O channels. - slviajero/tinybasic Wiki

Mac, Linux, Windows, and MSDOS

On a Mac, Linux, Windows and MSDOS the commands LOAD and SAVE work on the filesystem. They store a file and load it. All files are always stored as ASCII files. Loading is done by calling the storeline() command used for adding new lines. It is essentially like typing in lines by hand. If you have an existing program in memory the loaded one is merged line by line. You need to clear the memory using "NEW" if a new program is to be loaded. If LOAD is called in a program, it clears the old program, loads the new one and then continues execution at the first line of the new program.

EEPROM file storage

If you compile with ARDUINOEEPROM the EEPROM can be used as a file storage. SAVE "!" deposits your program in the EEPROM if there is enough space. LOAD "!" reloads it. With the command SET 1,1 you can activate EEPROM autorun. A program stored in the EEPROM is started automatically after starting the Arduino. SET 1,0 switches off autorun. SET 1, 255 removes the program flag. The EEPROM cannot be loaded any more. The information needed for this logic is stored in the header bytes of the EEPROM. Currently the header size is 3. Don't change these bytes with PEEK and POKE unless you understand what you are doing.

Programs stored in EEPROM can be autorun. After reset the Arduino starts the program without showing a command prompt. Autorun of an EEPROM program is done directly from the EEPROM. There is no LOAD to memory needed. An EEPROM autorun program has the entire RAM for variables. An arduino in EEPROM autorun mode still listens to the Serial port. A running program can be interrupted by typing in the break character # as first character after connecting the Arduino to the computer.

On Arduinos without mass storage, load and save can be used without a parameter. LOAD and SAVE are always available as commands even is the interpreter is compiled with a minimal configuration,

SD cards

If you compile with ARDUINOSD the SD library is loaded. This costs 12 kB of PROGMEM. LOAD and SAVE work like on Mac and Linux. In addition to this basic Apple DOS style commands CATALOG and DELETE is available as file command. CATALOG lists the files on disk. It stops and waits for a key to be pressed if used with an LCD display. DELETE erases a file. No safety net here. The file is gone for good after DELETE.


On ESP SPIFFS is supported for the internal filesystem. The microcontroller has to be prepared and preformated. AZ Delivery has written a nice blog post on this (AZ-Delivery, SPIFFS). All filesystem commands work on the internal flash after compiling the interpreter with the flags #define ESPSPIFFS. I will migrate to LittleFs in the future.

File I/O

Basic file I/O is implemented now. OPEN "filename", 0 opens a file for reading while OPEN "filename", 1 opens for writing. CLOSE 0 and CLOSE 1 closes the respective file. To write to a file the modifier &16 has to be set in a print command. PRINT &16, "hello world" writes to a file. INPUT &16, A$ reads from a file.

File I/O status can be checked by reading the special variable @S. If @S is 0 the operation was successful, any other value indicates an exception. @S has to be reset by the program. It stays different from 0 after an exception until @S=0 is set. Typical conditions are EOF which leads to @S=1.

I/O channels

The modifier &16 in the PRINT and INPUT command addresses the output channels.

BASIC has output stream numbers to steer the input and output of characters. Currently implemented channels are

&1: primary serial line

&2: keyboard and display

&4: secondary serial line (for printers)

&7: I2C wire communication

&8: RF2401 radio communication

&16: File I/O

The I/O channels are the BASIC version of the stream class. For all channels the function AVAIL() in BASIC corresponds to the available() function in C++.