Electronic Design
Using Functional Programming on a Microchip PIC

Using Functional Programming on a Microchip PIC

OCaPIC is an embedded OCaml implementation that runs on Microchip PIC microcontrollers.

Functional programming is a programming style that is significantly different than imperative programming languages like C and C++. Functional programs tend to avoid mutable data like variable assignments. Actually, functional programming tends to use a single-assignment approach. A variables value will not change once it is assigned.

Functional programming has been used successfully in a wide range of applications including the financial sector. Haskell, a functional programming language, has been used in embedded applications. Haskell is not the only functional programming language. Microsoft’s F# is another.

OCaml is another popular functional programming language that also supports imperative and object-oriented languages. This allows it to be used by developers more familiar with non-functional programming languages.

OCaPIC is a compact OCaml implementation that runs on an 8-bit PIC18 microcontroller from Microchip. The team that designed the system implemented an OCaml virtual machine in only 4 Kbytes for flash. It also allows applications to operate with the limited RAM available with PIC platforms.

Getting OCaml running on an 8-bit platform is not an easy task. It takes implementation tricks post processing compiled application bytecode to reduce heap usage and do things like eliminating unused closures and reducing indirections. Of course, using OCaml will not magically give the PIC18 more memory, so applications are obviously constrained by the capabilities of the microcontroller.

On the other hand, OCaml and OCaPIC bring a high-level, multi-paradigm programming language to developers. This includes a robust type system with object-oriented support, exception support, and parametric polymorphism. OCaPIC brings OCaml’s functional support such as first-class functions and closures. It even has automatic memory management.

The project ported a good bit of the OCaml runtime and standard library so developers are not starting from scratch.

OCaPIC is not the only way to get OCaml into a low-end, embedded environment. The OCamlcc project translates OCaml bytecode to C that can then be compiled and used on an embedded system. 

Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish