Skip to main content

Notes on Architecture

Chapter 2 The C SDK and assembly

The Raspberry Pi foundation provides an excellent software development kit, or SDK. The SDK is a library of C functions that facilitate application development on the Pico. In that regard, it is a bit like CircuitPython. The difference is that CircuitPython runs in an interpreter that the Pico itself hosts. Different Pico peripherals, or more advanced code for fancier CircuitPython boards, may require more CircuitPython libraries, and these have to be provided to the Pico board for it to run such code. Any definitions required at runtime have to be available this way.
In contrast, C code is compiled into machine code (ARM assembly) that the Pico can run directly. The machine code is a sequence of instructions encoded as bit patterns. The Pico can run these instructions directly, if the bit patterns are stored in its memory. The C SDK provides a library of functions that can be called from C code, and that are compiled into machine code. The machine code is stored in the Pico’s memory, and the Pico can run it directly. No interpreter or libraries are required at runtime. Thus, software written in C can be more efficient than software written in CircuitPython. This efficiency is realized both in terms of speed and in terms of memory usage.
Moreover, advanced boards often have facilities that cannot be used, or can only partially be used, from CircuitPython. For example, the Pico has a programmable input/output, or PIO, that can be used to implement custom hardware interfaces. The PIO is not fully accessible from CircuitPython. Other typical exceptions include most interrupt facilities, and some features of asynchronous or concurrent programming. These features are available from C.
The machine code is difficult to read directly, because it is encoded in bit patterns. However, there is a human-readable form of this machine code, called assembly language. The assembly is equivalent to the compiled machine code, but it is easier to read and understand. Every CS student should have some understanding of machine code and assembly, because the way it all works has had a profound influence on the design of many popular high-level languages, including Python.
In this chapter, we introduce the C SDK and assembly. We will use the C SDK to write some simple programs, and we will study the form of assembly programming in order to write some very simple assembly code. For the most part, we will make free use of the SDK to simplify our lives. It is challenging for even experience programmers to write assembly code, and we will not attempt to do so in any serious way. You may never write another line of assembly after this, but your appreciation of the work done for you by the authors of compilers, interpreters, and so on, will last a lifetime. Moreover it is often helpful to be able to read assembly code during debugging.
The days when human programmers could write more efficient code than compilers are past, but still within living memory. However, today’s compilers would really be hard to beat. Folks who write assembly today do it for the love of the game. There are even literal games featuring assembly programming, for example Shenzhen IO from Zachtronics (available on Steam, e.g.).