Image

Using Functional Programming on a Microchip PIC

Sept. 17, 2015
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. 

About the Author

William G. Wong | Senior Content Director - Electronic Design and Microwaves & RF

I am Editor of Electronic Design focusing on embedded, software, and systems. As Senior Content Director, I also manage Microwaves & RF and I work with a great team of editors to provide engineers, programmers, developers and technical managers with interesting and useful articles and videos on a regular basis. Check out our free newsletters to see the latest content.

You can send press releases for new products for possible coverage on the website. I am also interested in receiving contributed articles for publishing on our website. Use our template and send to me along with a signed release form. 

Check out my blog, AltEmbedded on Electronic Design, as well as his latest articles on this site that are listed below. 

You can visit my social media via these links:

I earned a Bachelor of Electrical Engineering at the Georgia Institute of Technology and a Masters in Computer Science from Rutgers University. I still do a bit of programming using everything from C and C++ to Rust and Ada/SPARK. I do a bit of PHP programming for Drupal websites. I have posted a few Drupal modules.  

I still get a hand on software and electronic hardware. Some of this can be found on our Kit Close-Up video series. You can also see me on many of our TechXchange Talk videos. I am interested in a range of projects from robotics to artificial intelligence. 

Sponsored Recommendations

Comments

To join the conversation, and become an exclusive member of Electronic Design, create an account today!