Design and Implementation of an Embedded Python Run-Time System

Design and Implementation of an Embedded Python Run-Time System

Design and Implementation of an Embedded Python Run-Time System Thomas W. Barr Rebecca Smith Scott Rixner Rice University ftwb, rjs, [email protected] Abstract As the capabilities of embedded systems increase, this situation is becoming untenable. Programming must be This paper presents the design and implementation of simplified to meet the demand for increasingly complex a complete embedded Python run-time system for the microcontroller applications. ARM Cortex-M3 microcontroller. The Owl embedded This paper presents one mechanism for doing so: Python run-time system introduces several key innova- an efficient embedded Python run-time system named tions, including a toolchain that is capable of producing Owl. The Owl system is a complete Python development relocatable memory images that can be utilized directly toolchain and run-time system for microcontrollers that by the run-time system and a novel foreign function in- do not have enough resources to run a real operating sys- terface that enables the efficient integration of native C tem, but are still capable of running sophisticated soft- code with Python. ware systems. These microcontrollers typically operate The Owl system demonstrates that it is practical to run at 50–100 MHz, have 64–128 KB of SRAM, and have high-level languages on embedded microcontrollers. In- up to 512 KB of on-chip flash. One example of such strumentation within the system has led to an overall sys- a microcontroller is the ARM Cortex-M3. ARM pre- tem design that enables Python code to be executed with dicts that in 2015, the market for these Cortex-M class low memory and speed overheads. Furthermore, this pa- microcontrollers will be around 18 billion units [7]. In per presents an evaluation of an autonomous RC car that contrast, Gartner, Inc. predicts that 404 million x86 pro- uses a controller written entirely in Python. This demon- cessors will ship in 2012 [9]. strates the ease with which complex embedded software systems can be built using the Owl infrastructure. Owl is a complete system designed for ARM Cortex- M microcontrollers that includes an interactive develop- ment environment, a set of profilers, and an interpreter. It 1 Introduction is derived from portions of several open-source projects, including CPython and Baobab. Most notably, the core For every microprocessor in a traditional computer sys- run-time system for Owl is a modified version of Dean tem, there are dozens of microcontrollers in cars, appli- Hall’s Python-on-a-Chip (p14p).1 ances, and consumer electronics. These systems have We have developed a comprehensive set of profilers significant software requirements, as users demand elab- and analysis tools for the Owl system. Using the data orate user interfaces, networking capabilities, and re- from these static and dynamic profiling tools, as well sponsive controls. However, the programming environ- as experience from having a large user base at Rice, ments and run-time systems for microcontrollers are ex- we significantly expanded, re-architected, and improved tremely primitive compared to conventional computer the robustness of the original p14p system. The Owl systems. toolchain we developed includes simple tools to program Modern microcontrollers are almost always pro- the microcontroller on Windows, OS X, and Linux. We grammed in C, either by hand or generated automatically have also added two native function interfaces, stack pro- from models. This code, which runs at a very low level tection, autoboxing, a code cache, and many other im- with no reliance on operating systems, is extremely dif- provements to the run-time system. Furthermore, the ficult to debug, analyze, and maintain. At best, a sim- toolchain and run-time system have been re-architected ple real-time operating system (RTOS) is used to facili- to eliminate the need for dynamic loading. tate thread scheduling, synchronization, and communica- The Owl system demonstrates that it is possible to de- tion [1, 3, 8, 11]. Typically, such RTOS’s provide primi- velop complex embedded systems software using a high- tive, low-level mechanisms that require significant exper- tise to use and do very little to simplify programming. 1http://code.google.com/p/python-on-a-chip/ level programming language. Many software applica- While Android uses an interpreter, Dalvik, that runs on tions have been developed within the Owl system, in- embedded systems, it has very different design goals than cluding a GPS tracker, a web server, a read/write FAT32 these projects [5]. Dalvik relies on the underlying Linux file system, and an artificial horizon display. Further- kernel to provide I/O, memory allocation, process isola- more, Owl is capable of running a soft real-time system, tion and a file system. It is designed for systems with at an autonomous RC car. These applications were writ- least 64 MB of memory, three orders of magnitude more ten entirely in Python by programmers with no prior em- than is available on ARM Cortex-M microcontrollers. bedded systems experience, showing that programming Arduino2 is a simple microcontroller platform that has microcontrollers with a managed run-time system is not been widely adopted by hobbyists and universities. It has only possible but extremely productive. Additionally, shown that there is great interest in making programming Owl is used as the software platform for Rice Univer- microcontrollers easier. However, Arduino focuses on sity’s r-one educational robot [15]. A class of twenty- raising the abstraction level of I/O by providing high- five first-semester students programmed their robots in level libraries, while Owl raises the abstraction level of Python without the interpreter ever crashing. computation with a managed run-time system. The cornerstone of this productivity is the interactive Recently, two open-source run-time systems for high- development process. A user can connect to the micro- level languages on microcontrollers have been devel- controller and type statements to be executed immedi- oped: python-on-a-chip (p14p) and eLua. p14p is a ately. This allows easy experimentation with peripherals Python run-time system that has been ported to several and other functionality, making incremental program de- microcontrollers, including AVR, PIC and ARM. The velopment for microcontrollers almost trivial. In a tra- p14p system is a portable Python VM that can run with ditional development environment, the programmer has very few resources and supports a significant fraction of to go through a tedious compile/link/flash/run cycle re- the Python language. Similarly, eLua is a Lua run-time peatedly as code is written and debugged. Alternatively, system that runs on ARM microcontrollers.3 The over- in the Owl system a user can try one thing at the inter- all objectives and method of operation of eLua are very active prompt and then immediately try something else similar to p14p, so they will not be repeated here. after simply hitting “return”. The cost of experimenta- The fundamental innovation of p14p is the read-eval- tion is almost nothing. print-loop that utilizes the host Python compiler to trans- Microcontrollers are incredibly hard to program. They late source code into Python bytecodes at run-time. A have massive peripheral sets that are typically exposed p14p memory image is built from the compiled code ob- directly to the programmer. As embedded systems con- ject and then sent to the microcontroller. On the micro- tinue to proliferate and become more complex, better controller, an image loader reads the image, creates the programming environments are needed. The Owl sys- necessary Python objects, and then executes the byte- tem demonstrates that a managed run-time system for a codes. In this manner, an interactive Python prompt op- high-level language is not only practical to implement for erating on the host computer can be used to interact with modern microcontrollers, but also makes programming the embedded run-time system over USB or other serial complex embedded applications dramatically easier. connection. This leads to an extremely powerful sys- tem in which microcontrollers can be programmed in- 2 Related Work and Background teractively without the typical compile/link/flash/run cy- cle. This process has been re-architected and improved Early commercial attempts to build embedded run-time in Owl, as described in Section 3.2. systems, such as the BASIC Stamp [12], required mul- The interactive Python prompt also gives unprece- tiple chips and have not been used much beyond educa- dented visibility into what is happening on the embed- tional applications. Academic projects have largely fo- ded system. Typically, a user is presented with a prim- cused on extremely small 8-bit devices [10, 13]. These itive command system that only enables limited interac- systems are built to run programs that are only dozens of tion and debugging on the microcontroller. Debuggers, lines long and are simply not designed for more modern such as gdb, are needed for additional capability. In con- and capable 32-bit microcontrollers. trast, an interactive prompt allows users to run arbitrary The Java Card system ran an embedded JVM sub- code, print out arbitrary objects, and very easily under- set to allow smartcards to perform some limited com- stand the state of the system. This leads to much more putation [6]. While there were some small proof-of- rapid software development and debugging. concept applications developed for it such as a basic In p14p, native C functions can be wrapped in a web-server [16], the limited computational and I/O ca- Python function. This allows arbitrary C functions to be pabilities of smartcards rendered building large applica- 2http://arduino.cc/ tions in Java Card impractical [17]. 3http://www.eluaproject.net/ compiled on the host, stored in SRAM on the micro- Source built-in controller, and is then executed immediately.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    12 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us