Lisp for microcontrollers

Lisp for Arduino, Adafruit M0/M4, Micro Bit, STM32, ESP8266/32, and MSP430 boards.

News!

uLisp now supports the RISC-V Sipeed Maixduino board

A new version of uLisp for the RISC-V processor runs on the Sipeed Maixduino, an Arduino form-factor board with a powerful Kendryte K210 RISC-V Dual Core 64 bit 400 MHz processor:

Maixduino.jpg

The RISC-V version of uLisp incorporates a RISC-V assembler which allows you to generate machine-code functions, integrated with Lisp, written in RISC-V mnemonics. In addition, it includes graphics extensions to support the colour TFT display available for the Sipeed Maixduino. For more information see Sipeed MAiX RISC-V boards.

uLisp® is a version of the Lisp programming language specifically designed to run on microcontrollers with a limited amount of RAM, from the Arduino Uno based on the ATmega328 up to the Sipeed Maixduino based on the RISC-V K210. You can use exactly the same uLisp program, irrespective of the platform.

Because uLisp is an interpreter you can type commands in, and see the effect immediately, without having to compile and upload your program. This makes it an ideal environment for learning to program, or for setting up simple electronic devices.

Because Lisp is interactive it is an ideal language for learning about fundamental programming concepts. It incorporates string handling, list processing, and garbage collection, and so is also an ideal language for expressing complex ideas, such as teaching a robot to solve mazes or finding the shortest route on a map. As well as supporting a core set of Lisp functions uLisp includes Arduino extensions, making it ideal as a control language for the Arduino.

You can download the current version of uLisp free from the Download uLisp page.

uLisp projects

ClueRayTrace2.jpgRay tracing with uLisp

GraphicsDisplay.jpgGraphics display interface

LispBadgeHand2.jpgLisp Badge

I2CClock2.jpgI2C clock

GPSPlotter2.jpgGPS mapping application

MAX6675b.jpgThermocouple interface

Requirements

RAM: At least 2 Kbytes.

Program memory: At least 32 Kbytes.

EEPROM, flash, or FRAM memory: If available, used for saving and loading the uLisp workspace.

The versions of uLisp for 8-bit and 16-bit platforms support integers between -32768 and 32767.

AVR version

The AVR version of uLisp supports the following boards:

Arduino Uno or other ATmega328-based cards. These will give you enough memory for a simple uLisp application using short symbol names. All the Simple examples will run on the Arduino Uno.

Arduino Mega 2560 or other ATmega2560-based boards. These will give you enough memory for a fairly complex application; for examples see AnimalsTweetmazeRoute finder, and Infinite precision arithmetic.

ATmega1284. Although there isn't an official Arduino board based on it, the ATmega1284 is easy to wire up on a prototyping board, and provides a generous 16 Kbytes RAM.

ATmega4809 Curiosity Nano. Microchip's ATmega4809 Curiosity Nano evaluation board makes a very low-cost platform for running uLisp using short symbol names.

MSP430 version

The MSP430 version of uLisp supports the following boards:

MSP430 F5529 LaunchPad. This uses the flash memory for saving images, and provides enough memory for a fairly complex application.

MSP430 FR5969 LaunchPad. This version uses the FRAM for the workspace, and for saving images, giving a generous amount of memory.

MSP430 FR5994 LaunchPad. This version uses the FRAM for the workspace, and for saving images, giving a generous amount of memory.

MSP430 FR6989 LaunchPad. This version uses the FRAM for the workspace, and for saving images, and supports writing text to the on-board LCD display.

32/64-bit platforms

The versions of uLisp for 32-bit platforms support integers between 2147483647 and -2147483648, and 32-bit floating-point numbers.

ARM version

The ARM version of uLisp supports the following boards:

Arduino Due. This board is based on the AT91SAM3X8E ARM Cortex-M3 core and provides 512 Kbytes of flash, 96 Kbytes of RAM, and an 84 MHz clock.

Arduino Zero. This board is based on the SAMD21 ARM Cortex-M0+ core and provides 256 Kbytes of flash and 32 Kbytes of RAM.

Arduino MKRZero. This is similar to the Arduino Zero, based on the SAMD21 ARM Cortex-M0+ core and with 256 Kbytes of flash and 32 Kbytes of RAM. It incorporates an SD-card socket, allowing you to use an SD card for saving and loading uLisp images.

Adafruit M0 boards. The Adafruit Gemma M0, Adafruit ItsyBitsy M0, and Adafruit Feather M0 are each based on the ATSAMD21 48 MHz ARM Cortex M0+ microcontroller. They have similar features and performance; the main difference is the form-factor of each board.

Adafruit M4 boards. The Adafruit Metro M4 Grand Central, Adafruit Metro M4, Adafruit ItsyBitsy M4, and Adafruit Feather M4 are each based on the ATSAMD51 120 MHz ARM Cortex M4 microcontroller.

Adafruit nRF52840 boards. The Adafruit CLUE and Adafruit ItsyBitsy nRF52840 are each based on the Nordic Semiconductor nRF52840 64 MHz ARM Cortex-M4 microcontroller, with 1 Mbyte of flash program memory and 256 Kbytes of RAM.

BBC Micro BitThis is based on a Nordic Semiconductor nRF51822 ARM Cortex-M0 microcontroller. It runs at 16 MHz and provides 256 Kbytes of flash program memory and 16 Kbytes of RAM.

Maxim MAX32620FTHRThis is based on a Maxim MAX32620 ARM Cortex-M4F microcontroller running at 96 MHz, with 2048 Kbytes of flash memory and 256 Kbytes of RAM.

STM32 version

STM32 boards. The STM32 Maple Mini and Blue Pill boards are based on the STM32F103 ARM Cortex-M3 processor running at 72 MHz, with 128 Kbytes of flash and 20 Kbytes of RAM.

ESP8266/ESP32 version

The ESP8266/ESP32 version of uLisp supports the following boards:

ESP8266 boards. These boards are based on the 32-bit Tensilica Xtensa L106 microprocessor running at 80 MHz, with 4 Mbytes of flash and 80 Kbytes of RAM. They include integrated Wi-Fi.

ESP32 boards. These boards are based on the 32-bit Tensilica Xtensa LX6 microprocessor running at 160 or 240 MHz, with 4 Mbytes of flash and 520 Kbytes of RAM. They include integrated Wi-Fi and dual-mode Bluetooth.

RISC-V version

The RISC-V version of uLisp supports the following board:

Sipeed MAiX RISC-V boards. These boards are based on the Kendryte K210 RISC-V Dual Core 64 bit 400 MHz processor and provide 8 Mbytes RAM and 16 Mbytes flash. They are similar in performance.

Performance

The following table gives a summary of the performance of the different versions:

8/16-bit platforms

Platform Processor Clock Objects Image GC time Tak Q2
Arduino Uno, Arduino Nano ATmega328 16 MHz 315 256 0.6 ms 62 s  
Arduino Mega 2560 ATmega2560 16 MHz 1214 1024 2.1 ms 51 s 119 s
Tiny Lisp Computer, Lisp Badge ATmega1284 16 MHz 2816 1024 4.8 ms 55 s  
Curiosity Nano ATmega4809 20 MHz 1068 64 1.3 ms 39 s  
ATtiny Lisp ATtiny3216 20 MHz 306 64 0.3 ms 47 s  
MSP430 F5529 LaunchPad MSP430F5529 25 MHz 1280 1280 1.3 ms 20 s  
MSP430 FR5969 LaunchPad MSP430FR5969  16 MHz 3072 1536 10.2 ms 60 s  
MSP430 FR5994 LaunchPad MSP430FR5994 16 MHz 3072 1536 6.7 ms 45 s  
MSP430 FR6989 LaunchPad MSP430FR6989 16 MHz 3072 1536 9.2 ms 62 s  

32-bit platforms

Platform Processor Clock Objects Image Code GC time Tak Q2 FFT
Arduino Due ATSAM3X8E 84 MHz 10240 * * 3.5 ms 8.0 s   213 ms
Arduino Zero ATSAMD21 48 MHz 3072 * 128 2.6 ms 12 s   348 ms
Arduino MKRZero ATSAMD21 48 MHz 3072 * 128 2.6 ms 18 s   446 ms
Adafruit Gemma M0 ATSAMD21 48 MHz 3072 * 128 1.2 ms 14 s   445 ms
Adafruit ItsyBitsy M0 ATSAMD21 48 MHz 3072 All 128 2.9 ms 15 s 40 s 445 ms
Adafruit Feather M0 Adalogger ATSAMD21 48 MHz 3072 All 128 3.4 ms 14 s    
Adafruit Metro M4 Grand Central ATSAMD51 120 MHz 30720 All 256 4.9 ms 4.2 s 11 s 122 ms
Adafruit Metro M4 ATSAMD51 120 MHz 20480 All 256 3.3 ms 4.2 s   122 ms
Adafruit ItsyBitsy M4 ATSAMD51 120 MHz 20480 All 256 3.4 ms 4.3 s    
Adafruit Feather M4 ATSAMD51 120 MHz 20480 All 256 3.4 ms 4.3 s    
Adafruit CLUE nRF52840 64 MHz 20480 All 256 4.9 ms 6.8 s    
Adafruit ItsyBitsy nRF52840 nRF52840 64 MHz 20992 All 256 5.9 ms 6.8 s 17 s  
BBC Micro Bit nRF51822 16 MHz 1024 * 64 1.7 ms 34 s    
Maxim MAX32620FTHR MAX32620 96 MHz 24576 * 256 5.4 ms 6.4 s 16 s 179 ms
Maple Mini STM32F103 72 MHz 1130 1280 * 0.7 ms 9 s   366 ms
Blue Pill STM32F103 72 MHz 1472 1280 * 0.7 ms 9 s    
ESP8266 boards ESP8266 80 MHz 3072 512 * 0.6 ms 12 s    
ESP32 boards ESP32 160 MHz 8000 512 * 0.3 ms 7.5 s 23 s 242 ms
Sipeed MAiX RISC-V boards K210 400 MHz 80000 * 512 6.4 ms 1.0 s 2.5 s 28 ms

Objects gives the number of Lisp objects of storage available, each equivalent to 4 bytes on the 8/16-bit platforms and 8 bytes on the 32-bit platforms.

Image gives the number of objects that can be saved to non-volatile storage using save-image.
* These platforms don't provide non-volatile storage for saving an image, but you can save images to an SD card with a suitable interface.

Code gives the number of bytes of machine code that can be stored by the ARM assembler, on ARM or RISC-V platforms.
* means not available.

GC time gives the time taken for a garbage collection.

Tak gives the time taken to calculate (tak 18 12 6); see Benchmarks.

Q2 gives the time taken to calculate (q2 7 8); see Benchmarks.

FFT (32-bit platforms only) gives the time taken to run the floating-point 32-point fft benchmark; see Fast Fourier Transform

Specification

The language is generally a subset of Common Lisp, and uLisp programs should also run under Common Lisp.

Types supported: list, symbol, integer, character, string, and stream.

An integer is a sequence of digits, optionally prefixed with "+" or "-". Integers can be between -32768 and 32767 (or between 2147483647 to -2147483648 on the 32-bit platforms). You can enter integers in hexadecimal, octal, or binary with the notations #x2A, #o52, or #b101010, all of which represent 42.

The 32-bit platforms also support 32-bit floating-point numbers, and provide a full set of floating-point functions.

On platforms with more than 2 Kbytes of RAM arbitrary user-defined symbol names are supported. Any sequence that isn't an integer can be used as a symbol; so, for example, 12a is a valid symbol. On platforms with only 2 Kbytes symbol names can have up to three characters consisting of a-z and 0-9.

There is one namespace for functions and variables; in other words, you cannot use the same name for a function and a variable. uLisp provides tail-call optimization, so applications written using recursive functions can be as efficient as using iteration.

Strings can consist of an arbitrary sequence of ASCII characters. Strings can be unlimited length, and are automatically garbage-collected.

uLisp includes a mark and sweep garbage collector. Garbage collection takes under 1 msec on an Arduino Uno or under 3 msec on an Arduino Mega 2560 (see above table).

uLisp also includes a simple program editor (see Using the program editor), a trace facility, and a pretty printer (see Debugging in uLisp).

Example

The following example illustrates how you might use uLisp.

After uploading uLisp to your microcontroller board you can communicate it via by typing or pasting commands into the Serial Monitor. For more information see Using uLisp.

Suppose you have a red LED connected to the analogue output pin 9 on an Arduino Uno. Then you can type in the Lisp command:

(analogwrite 9 128)

to set the LED to 128, which corresponds to half brightness.

To save having to write this command every time you want to set the red LED you can define a function called red:

(defun red (x) (analogwrite 9 x))

Now you can achieve the same effect simply by writing:

(red 128)

In each case the LED changes immediately, as soon as you type in the command.

Suppose you've got a potentiometer connected to vary the voltage on the analogue input A0. You could define a function dim to make the potentiometer adjust the brightness of the LED with:

(defun dim () (loop (red (/ (analogread 0) 4)))

and run it by typing:

(dim)

Finally, you could save the uLisp image to EEPROM, and specify that dim should be run on load, by entering:

(save-image 'dim)

When you reset the Arduino dim will now load and run automatically.

This is a simple example showing how uLisp allows you to build up complex programs from simpler components, testing each of the components as you go along.


Next: Using uLisp