Electronicdesign 8259 Fig1 1
Electronicdesign 8259 Fig1 1
Electronicdesign 8259 Fig1 1
Electronicdesign 8259 Fig1 1
Electronicdesign 8259 Fig1 1

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. 

Sponsored Recommendations

Highly Integrated 20A Digital Power Module for High Current Applications

March 20, 2024
Renesas latest power module delivers the highest efficiency (up to 94% peak) and fast time-to-market solution in an extremely small footprint. The RRM12120 is ideal for space...

Empowering Innovation: Your Power Partner for Tomorrow's Challenges

March 20, 2024
Discover how innovation, quality, and reliability are embedded into every aspect of Renesas' power products.

Article: Meeting the challenges of power conversion in e-bikes

March 18, 2024
Managing electrical noise in a compact and lightweight vehicle is a perpetual obstacle

Power modules provide high-efficiency conversion between 400V and 800V systems for electric vehicles

March 18, 2024
Porsche, Hyundai and GMC all are converting 400 – 800V today in very different ways. Learn more about how power modules stack up to these discrete designs.

Comments

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