PicoCalc Shell (Replaces UF2 Loader's menu)

Here is a command line shell that lets you manipulate the SD Card between runs of programs (uf2’s). It is a drop in replacement for BOOT2350.UF2 for @pelrun 's UF2 Loader, which needs to be installed first.

You need a RP2350-based board to run the shell. BOOT2350.UF2 runs in RAM and there in not enough room in the RP2040’s RAM for it.

There are two flavours of the shell: Unix and DOS. Please choose the flavour you are most comfortable using. There is a user guide for each: Unix User Guide and Dos User Guide.

Both support scripting (shell scripts or batch files). They also support redirection (>, >>, <), pipes (|), globs (*, ?, …), command history, and so forth.

The Unix flavour comes with vi for editing files; DOS comes with EDIT (similar to MS-DOS’s EDIT).

To start a UF2, just type it’s name. For example, to play rogue.uf2, just type: rogue. The path is initialized to /pico2-apps or C:\PICO2-APPS, but this can be set with the PATH variable.

We are still using @pelrun’s boot loader and have only replaced the menu with a shell, so you can return to the shell using UP/F1, to BOOTSEL with DOWN, just as you always have.

UNIX example:

/home>ls
notes/  todo.txt
/home>cat todo.txt
buy batteries
/home>cd /
/>
/>man cp
usage: cp [-r] from... to
Copies files, -r directories

DOS example:

C:\HOME>DIR
 Volume in drive C has no label
 Volume Serial Number is 5C23-0000
 Directory of C:\HOME

NOTES          <DIR>
TODO     TXT                          6
    1 file(s)              6 bytes
                  66,040,832 bytes free
C:\HOME>CD ..
C:\>
C:\>HELP COPY
usage: COPY source [destination]
Copy one or more files

You can also implement a startup script (Unix: /etc/profile; DOS: C:\AUTOEXEC.BAT.

Unix example:

# /etc/profile
phosphor amber
font 5x10
PATH=/pico2-apps:/games
cd /home

DOS example:

@ECHO OFF
PATH C:\PICO2-APPS;C:\GAMES
PROMPT $P$G
PHOSPHOR amber
CD \HOME

Developers

Also, included is a convenience header file that shows how you can let users exit their app and return to the shell or uf2loader’s menu.


8 Likes

Very nice. I particularly like the implementation of PATH so I don’t have to load all of my apps into /pico2-apps that I have to with the old UF2 loader.

1 Like

I’ve opened up a PR on your code to add a new command “select” which displays a menu of programs to run based on a text file with various choices

1 Like

Shell V1.1 Release

shell v1.1 adds the date and time, NTP on the Pico 2 W, and menus. A big thank you to @chrisleif for the idea to use the always-on timer to share date and time with the programs launched by the shell. Also, thank @dzubin for the idea to introduce menus, though the final implementation came out differently.

What’s new

  • Date and time. The clock is kept in the RP2350’s always-on timer, through reboots and program runs, and in a DS3231 on I2C when the PicoCalc has one, so it survives power-off. File dates are stamped on every write.
    • UNIX: date, a POSIX TZ in /etc/profile, and dates in ls -l and from touch.
    • DOS: DATE, TIME, the date formats that COUNTRY sets in CONFIG.SYS, dates in DIR, $D and $T in PROMPT, and COPY and XCOPY keeping the source file’s date.
  • NTP on the Pico 2 W. The W builds add ntp (DOS: NTP [/F]), which runs ntp.uf2 at most once per power-on, using the network named in /ntp/ntp.cfg, to set the clock.
  • Menus. select and break for scripts in the UNIX flavour; CHOICE in the DOS flavour.
  • The clock for programs. include/picocalc_shell.h adds picocalc_shell_time() and picocalc_shell_set_time().
  • info and MEM show the PSRAM. info’s lines fit in 40 columns.
  • The UNIX flavour has a single font, the Master 512’s, in 40 columns.
  • picocalc_shell_exit() refuses to run when UF2 Loader did not start the program.

2 Likes

Thank you, Blair, and for the credit. You took the idea a good deal further than I had: the check record in the POWMAN scratch registers, the local offset from TZ, the DS3231 and NTP. That’s a much sturdier clock than the one I suggested.

I’ll move my programs over to picocalc_shell_time() and retire the settime program in picocalc-extras, since the shell’s date and time (and NTP) do that job properly now.

3 Likes

Shell v1.2 Release

Shell v1.2 adds booting directly to the shell at power-on, provided by a build of UF2 Loader’s first stage, and a few things for scripts and the DOS flavour.

What’s new

  • The shell at power-on (optional). A build of UF2 Loader 2.5’s first stage with a small patch (patches/uf2loader-boot-shell.patch) is included:
    • Power on goes straight to the shell, and holding Up, F1 or F5 starts the last program; Down or F3 still give BOOTSEL;
    • running a program and returning to the shell skips the loader’s half-second wait for a key: back from a program to the shell in about 0.85 s, where the stock loader takes about 1.55 s;
    • a program that restarts itself still comes back to itself; hold Up, F1 or F5 through the restart, or switch off and on, for the shell;
    • with no SD Card, switching on starts the last program.
  • > /dev/null (and >> /dev/null) throws a command’s output away in the UNIX flavour, as > NUL does in the DOS flavour.
  • The clock at power-on. The guides show how to set the clock in /etc/profile or AUTOEXEC.BAT: from the DS3231, from NTP, or from NTP only when the DS3231 has no time.
  • DOS flavour:
    • The switch / is now treated as a delimiter so switches work as they do in DOS: DIR \/W is DIR \ /W.
    • COPY /B NAME+,, updates a file’s time, same as touch in the Unix flavour. COPY accepts /B.

3 Likes

Excited after many months of not touching my PicoCalc, I downloaded the code of project # PicoCalc Shell
With Claude’s help, I then modified the shell code to implement file management via web, drawing inspiration and some code from # PicoMultiLoader: an ESP32-P4-WIFI brain for the PicoCalc, with an SD-card app loader project.
Since I really like being able to use a BTE keyboard (I have 3 from Keyrom), I modified the code, drawing Diamond: a WordStar-style word processor for the PicoCalc (alpha) code, so I could use it in the shell. With a Raspberry Pi Pico 2 W, memory is limited, so I had to make some compromises, but it works, and I’m happy about that.
Thanks @BlairLeduc, @chrisleif, @Luciano_Faria, @pelrun for letting me have fun.
I hope there are no issues with my taking inspiration from, or using part of, your code.”

1 Like

No issues at all, Giovanni: Diamond and picocalc-extras are MIT licensed, so use whatever helps; just keep the licence notice with the code. Glad the keyboards are working in the shell. The Bluetooth code there comes from picocalc-extras, and its latest version, 1.3, has a couple of fixes since Diamond’s copy. Blair suggested today that pairing could live in the shell and programs could use it, so your work is right on topic. I’d be interested to hear what compromises you had to make for memory.

Memory management for Bluetooth in the shell

The shell runs entirely from SRAM: 512 KB (524,288 B) for code and data together. The first count with the Bluetooth stack (BTstack, classic and BLE) and the CYW43 radio firmware came to about 879 KB. A small optimisation could not close that gap. I needed roughly 360 KB less.

The choice. I kept full Bluetooth, BLE included, and took memory out elsewhere, in three places:

Change Saving (approx.)
Radio firmware read from the SD card instead of embedded 232 KB
-Os compilation in the W builds 56 KB
32 KB vi/EDIT buffer in the W builds (was 64 KB) 45 KB

The firmware is the largest piece and is used only once, when the radio starts, so it is best kept on the card. Shrinking vi costs little: a text file over 32 KB is rare on this machine.

Measured result (W development build): 495,424 B used, 28,864 B free. BTstack uses only about 7 KB of heap at run time. The Pico 2 build without Wi-Fi is unchanged (454,416 B).

How it works.

  • Firmware from the SD card. The CYW43 driver expects the firmware as an array in memory. I use a patched copy of cyw43_ll.c. The array becomes a virtual address. Every read goes to a function which reads /bluetooth/cyw43-fw.bin from the card with a 4 KB read-ahead. The file stays open until the chip is up, then is closed.
  • Targeted -Os. The W builds use -Os. The four files that speed depends on, the panel and card drivers (lcd.c, screen.c, sd.c, diskio.c), stay at -O3.
  • Smaller vi buffers. Sets vi/EDIT’s text buffer to 32 KB .
  • The radio starts only when needed. It starts after the shell, and only if /bluetooth/pairing.dat exists (created by your programbtpair). With no pairing the radio does not start and uses no memory. If it fails to start, the shell starts anyway and prints one line: bluetooth: <reason>.
  • Only one thread touches the card. BTstack runs from the asynchronous context. So the pairing file and the card are used only from the main loop, which reads the Bluetooth keys with btkbd_poll().

With the radio on, usbdisk reads about 19% slower (497 vs 612 KB/s). Writing is unchanged. I chose to leave it as it is.

Nice work, Giovanni. Loading the radio firmware from the card is a clever way around the RAM limit. Two things: picocalc-extras 1.3 is the latest Bluetooth code (two BTstack fixes, BLE, and mice); if your copy came from Diamond 0.3 it’s older. And I’m thinking of letting pairing.dat hold a keyboard and a mouse at the same time. Since your shell reads that file too, let’s agree on the new format first, so neither of us breaks the other.

Yes, my code comes from Diamond 0.3. Handling the keyboard and mouse at the same time is a great idea!! I use your btpair) program for pairing, so I’ll go along with your choices. I’ll modify the code so that it conforms to the new file format.

Thanks, Giovanni. One thing first: Diamond 0.3 has picocalc-extras 1.1’s Bluetooth code, which reads the older pairing file (PBK1) and has no BLE. btpair 1.2 and 1.3 write PBK2, so please move your copy to 1.3’s btkbd.c. The keyboard and mouse won’t change the format beyond that: the file stays PBK2, and the mouse gets its own record (tags PTR0 for classic, PTL0 for BLE) next to the keyboard’s (KBD0 and KBL0). Code that doesn’t know the new tags just ignores them, so you can add the mouse whenever it suits you.

1 Like

Blair, a small housekeeping question when you have a moment. The root of my card is getting crowded with settings files (virtualt.cfg, and now calcterm.cfg from a VT100 terminal I’m working on). I see the shell’s world uses a folder per program (/ntp/ntp.cfg, /atom/pico-atom.cfg) and keeps /etc for /etc/profile, and Diamond already does the same with /diamond/diamond.cfg. Is a folder per program the convention you’d like programs to follow, or would you rather see settings under /etc? I’d just like us all doing the same thing. No hurry at all.

So, I had to encourage some developers to put their files into a folder and not treat the entire card as they owned it. So having all a program’s files in one folder, I consider it a small victory.

First we had BOOTSEL, which one would flash the Pico with one firmware and it was a pain. The the boot loaders came which allowed us to switch firmware much easier, but it still was the concept of switching between firmware. With the patched UF2 loader and always booting to the shell changes things again, where the user considers the firmware as a program that they run, exit back to the shell and run another (although it is not a perfect analogy).

Perhaps this is a community effort if we want to have folders like Unix or DOS where we organize our SD cards like we organized our hard drives in the past.

For my various toy projects, I’ve started using folders for their config and save files, so I think that is a great idea.

Release v1.3

This release passes arguments from the command line to the launched UF2, returns you to your directory after a program or restart, and brings each flavour closer to the system it follows.

What’s new

  • Passing arguments. pico-oric -t game.tap (DOS: PICO-ORIC /T GAME.TAP) finds the file from your directory and gives the program its full path.
    • A program has to declare that it takes arguments. Any other program says takes no arguments and isn’t started.
    • Programs need updating to use this. pico-oric will support it from its next release.
    • Program authors can find the details in the README and picocalc_shell.h.
  • Back to your directory. Coming back from a program, a reboot or menu (DOS: EXIT), the shell returns to the directory you left, if it’s still on the card. After poweroff, or the power switch at the prompt, it starts as usual.

UNIX flavour:

  • Error messages: 2>, 2>>, 2>&1 and >&2, plus 2> /dev/null, < /dev/null and cat /dev/null.
  • Scripts: ./backup.sh, a file whose first line starts #!, or a name ending .sh on $PATH. exit [N] ends a script.
  • New commands: find, sort, uniq, tee, less, which, test and [, true, false.
  • Options: rm -f, -rf and -R; cp -R and -f; mv -f.
  • Line editing and directories: Ctrl-W, Ctrl-L, ~, cd -, $PWD and $OLDPWD.
  • Programs: ./chess, a path with no extension, and chess.uf2 found on $PATH.

DOS flavour:

  • COPY devices: CON and NUL, so COPY CON NOTE.TXT writes what you type, COPY NOTE.TXT NUL tests that a file can be read, and COPY NUL NOTE.TXTmakes an empty file.
  • Programs with their files: a program can live in any directory, and can be run from there, by its path, or as NAME.UF2.

Changes that may affect you (DOS flavour)

  • / no longer separates names. It only starts a switch, so CD /HOME now says Invalid switch - /HOME. Use \ in paths, including in PATH and batch files.
  • The shell starts in C:\, not C:\HOME. Put a CD in AUTOEXEC.BAT to start elsewhere.
  • The search order has changed. In each directory, a program now comes before a batch file of the same name. Type NAME.BAT to run the batch file.

4 Likes