Reader small image

You're reading from  MicroPython Projects

Product typeBook
Published inApr 2020
Reading LevelExpert
PublisherPackt
ISBN-139781789958034
Edition1st Edition
Languages
Right arrow
Author (1)
Jacob Beningo
Jacob Beningo
author image
Jacob Beningo

Jacob Beningo is an independent consultant who specializes in microcontroller-based embedded systems. He has advised, coached, and developed systems across multiple industries, including the automotive, defense, industrial, medical, and space sectors. Jacob enjoys working with companies to help them develop and improve their processes and skill sets. He publishes a monthly newsletter, Embedded Bytes, and blogs for publications about embedded system design techniques and challenges. Jacob holds bachelor's degrees in electrical engineering, physics, and mathematics from Central Michigan University and a master's degree in space systems engineering from the University of Michigan.
Read more about Jacob Beningo

Right arrow

Customizing the MicroPython Kernel Start Up Code

Developing embedded software using MicroPython is relatively straightforward but there may come a time when there is a need to build a custom-printed circuit board, adjust the default pin settings in the kernel, handle failure modes, or simply build a software library in the MicroPython kernel. In order to do this, a developer will need to be familiar with the MicroPython kernel by examining it and the steps necessary to customize it, which we will do in this chapter.

The following topics will be covered in this chapter:

  • An overview of the MicroPython kernel
  • Navigating the startup code
  • Modifying the default GPIO initialization
  • Adding MicroPython modules to the kernel
  • Testing the results

Technical requirements

The example code used in this chapter can be found at https://github.com/PacktPublishing/MicroPython-Projects/tree/master/Chapter05.

In order to run the examples and build your own custom MicroPython kernel, you will need the following hardware and software:

  • A Linux machine or virtual machine
  • An STM32L4 IoT Discovery node
  • A RobotDyn I2C 8-bit PCA8574 I/O expander module, or equivalent
  • An Adafruit RGB pushbutton PN: 3423, or equivalent
  • A breadboard
  • 6" jumpers
  • A generic two-position switch
  • A 30-gauge wire-wrapping wire
  • A Terminal application (PuTTY, RealTerm, Terminal, or one of many others)
  • A text editor, such as Sublime Text

An overview of the MicroPython kernel

The MicroPython kernel is a collection of software libraries, code, and a Python interpreter that comes pre-built on a MicroPython board, such as the pyboard. Someone who is new to MicroPython may not even realize that their Python interpreter is made up of C modules. These modules are compiled and then programmed onto their board, which then reveals the filesystem and REPL that we have become so familiar with. In this chapter, we are going to look more closely at the kernel and explore how we can make our own modifications that will enhance our applications.

Downloading the MicroPython kernel

Before you can get a feel for what it contains, you need to download the kernel so that you can...

Adding MicroPython modules to the kernel

MicroPython has a feature that allows a developer to compile their own libraries and then include them within the MicroPython kernel. These modules are often called frozen modules because they are compiled into bytecode. There are several advantages to compiling a module into a frozen module, including the following:

  • The Python module cannot be modified without flashing the kernel.
  • The module is compiled into bytecode, which keeps the source code away from prying eyes.
  • Updating the application scripts is faster because there are fewer modules to update.
  • If something goes wrong with the filesystem and it gets set back to default, the compiled modules will still be present and can be called as part of the default script to get the system back into a safe state.
  • You can put the compiled module into zero wait RAM if it has speed-critical functionality...

Deploying the custom kernel to a board

There are two steps that we need to follow in order to deploy our custom kernel to our development board. First, we need to compile our new kernel. Second, we need to take the output files and program them into the flash memory on our development board. Let's start by looking at how we can compile our kernel.

The compiled output files

Compiling the kernel requires us to execute just a couple of commands that will run the make file on our MicroPython port. Before attempting to invoke the make file, let's first return to the ports/stm32/ folder in the Terminal. I recommend that you clean any previously compiled versions of the kernel by executing the following command:

make clean...

Summary

As we saw in this chapter, developers are able to go into the MicroPython kernel and customize it for their applications. These customizations can be as simple as adjusting the names of the pins on the development board or adjusting the startup states of the GPIO pins, or as complex as communicating with an external device. We also saw that we can save space and increase the execution efficiency of our applications by converting modules into frozen modules, which are then built into the MicroPython kernel. If we need to update those modules, we can also cross-compile them using mpy-cross and deploy a precompiled bytecode version of the module onto our filesystem.

Now that we have a solid foundation on how to develop MicroPython applications and how we can customize the kernel, in the next chapter, we will examine how we can create our own custom debugging tools, which...

Questions

  1. In which folder in the kernel can you find all the MicroPython supported architectures?
  2. Which microcontroller architecture has the most supported development boards?
  3. Which three types of files can be found in a development board folder?
  4. What are a few features that make STM32L475E_IOT01A interesting for MicroPython?
  5. Which board kernel file can be modified to change the pin designation that is used to control a pin in a MicroPython script?
  1. What function must be defined in order to customize the startup code initialization?
  2. What steps should be followed to customize the startup code?
  3. Which compiler tool is used to generate .mpy files and convert Python scripts to frozen modules?
  4. What are the advantages of using a frozen module?
  5. What command is used to compile the kernel with frozen modules?

Further reading

lock icon
The rest of the chapter is locked
You have been reading a chapter from
MicroPython Projects
Published in: Apr 2020Publisher: PacktISBN-13: 9781789958034
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Jacob Beningo

Jacob Beningo is an independent consultant who specializes in microcontroller-based embedded systems. He has advised, coached, and developed systems across multiple industries, including the automotive, defense, industrial, medical, and space sectors. Jacob enjoys working with companies to help them develop and improve their processes and skill sets. He publishes a monthly newsletter, Embedded Bytes, and blogs for publications about embedded system design techniques and challenges. Jacob holds bachelor's degrees in electrical engineering, physics, and mathematics from Central Michigan University and a master's degree in space systems engineering from the University of Michigan.
Read more about Jacob Beningo