MicroPython on the P2

Overview

P2 Native MicroPython (“P2NMP”) implements many elements of Python 3 language, yet fits neatly into the memory of the Propeller 2 (P2). P2NMP includes 96~128kB of heap space, enough for programs with several thousand lines of code.

MicroPython was created by Damien George and extended through numerous community contributions. It is used in educational, hobby, commercial, industrial and medical device applications. Team-Oz compiled P2 Native MicroPython using the p2gcc tool, and have added extensions such as BASIC Stamp 2 (BS2) commands and flash storage utilities.

To use P2NMP on a P2, you’ll first load the P2NMP interpreter. Then, you can use the Mu Alpha editor on a Windows/Mac/Linux PC. Or, if you prefer a self-hosted MicroPython (using a serial terminal, and/or monitor and keyboard), instructions are on the TeamOz GitHub here

Preparing your P2 Board for Native MicroPython

This will install MicroPython into the flash memory of your Propeller 2 Evaluation Board or P2 Edge, overwriting any existing contents. Restarting MicroPython is a regular event when writing scripts, and using flash memory provides a quick boot time. 

  • Propeller 2 Evaluation Board: put the flash switch in the ON position, and any SD card is removed.
  • P2 Edge with P2 Edge Module Breadboard: configure the 4-position DIP switch as ON, ON, OFF, OFF as shown in the picture.   
  • Connect a USB cable between the PC and the P2 board. 

Loading P2MP Into Flash Using Propeller Tool (Windows)

  • Install the Propeller tool 2.3.0.0.
  • Download and extract p2mp113.zip from the TeamOz GitHub.
  • Open step1.spin2 and press F10 to load the P2NMP extensions to the P2’s RAM. LED on P56 will flash upon success.
  • Open step2.spin2 and press F11 to load the P2NMP binary into the P2’s flash memory.

Loading P2MP Into Flash Using FlexProp GUI (Windows/Mac/Linux)

  • Follow instructions for installing FlexPropGui, here
  • Download p2mp113.zip and expand its contents into the programming directory
  • From Port menu, select 115200 baud
  • Open Step1.spin2, and press control-R to run the program 
  • After 5~10 seconds you should see LED 56 blinking. Close the terminal window. 
  • Open Step2.spin2, and press control-E to store P2NMP in flash
  • Acknowledge flash switch position 
  • After 5~10 seconds you should see the MicroPython REPL appear in the terminal window.  
  • Close the terminal window so that Mu can access the COM port

Mu Alpha 1.1.0 Alpha 2 Installation (Windows/Mac/Linux)

Mu is a friendly application that allows you to easily edit and run MicroPython code.  It runs on Windows/Mac/Linux operating systems.  Note that the Alpha version is required.  

  • Download the Alpha version of Mu, 1.1.0.Alpha.2 (or later) from inside the purple box at https://codewith.mu/en/download   
  • Install Mu following the Mu instructions 
  • Run Mu (can take many seconds to start)

Examples

Blink the LED Using the REPL
  • Click the REPL button 
  • The following startup text will appear in the bottom (REPL) window

Note that the leds are active-low. p.off() turns *On* the Led. 

You can use up/down arrow keys to scroll through and repeat previous commands

Blink the LED Using Python Script
  • Type the following same code into top coding window;

import machine p=machine.Pin(56) p.on()
  • With the REPL window already open, Press run.
  • Change the code to turn the LED off.

The REPL (lower half) window needs to be already open in order to run.  Clicking Run will open the REPL window if it isn’t already.

A “Hello World” Example – REPL

  • Click the REPL button and move the mouse cursor down into the REPL window (bottom half) and click once so that it is active
  • Type

print ("Hello World!!")

A “Hello World” Example – Python Script

  • Keep the REPL open and click on the coding window (top half)
  • Type the follwing code into top coding window

print ("Hello World!!")

Press Run!

The REPL (lower half) window needs to be already open in order to run.  Clicking Run will open the REPL window if it isn’t already.  In effect you may need to click Run twice.

Advanced Topics and Backtick Commands

Team-Oz have extended the P2NMP experience with some useful extensions. Note the backtick is the character that shared keyboard with ~ symbol, normally on the top left of most keyboards.  It is not the single quote near the Enter key.

Advanced: Saving Your Program to the Flash

  • Add this line to the very first line in Mu. Note the backtick shares key with tilde (~), upper left on most keyboards

`save myprog1

  • Run the code

This saves the text after the first line, to the file myprog1 in the upper area of P2 flash memory.

  • To check your file has been saved, type:

`files

You should see myprog1 listed amongst the filenames.

Advanced: Making a Program Start Automatically

  • Type the following into Mu and click run

`save boot
myprog1

This inserts the filename you want to load at boot time (myprog1) into a file named boot, that ozFFS executes at startup

Advanced: Stopping at Any Time

If you get into endless loops at any time, press Escape four times.  MicroPython will reboot, without executing the boot program.  This is similar to breaking into autoexec.bat on old DOS PCs.

Advanced: Stopping Program from Executing at Boot

  • Type the following into Mu REPL

`delete boot

Note that myprog1 is still stored in flash, if you need it in future.

Further Information
  • MicroPython Documentation at https://docs.micropython.org/en/latest/
  • P2 Native MicroPython on github at https://github.com/team-oz
  • Full list of pin constants.  P2NMP uses the same list of pin constants defined in Spin2 v34Z documentation
  • Using Basic Stamp 2 Functions – import combo.py
  • OzFFS and the flash storage utilities – ozFFS quick reference (REPL or VGA/KB)
  • Built in flash storage utilities (VGA/KB).  Attach keyboard and press F12 to access
  • Built in ozFFS editor.  Access using F12 then select a file
  • Calling other code using the .CPU method
  • Compiling your own variant of MicroPython – config.h, Rogers Notes

Acknowledgements 

Thanks to Chip, Ken, Jeff, Eric R Smith, GarryJ, Dave Hein, Rosco-PC, Damien George, Matt T

New Parallax Website!

We have a brand new website to bring you all the latest and greatest Parallax has to offer. Not to worry, our old site is still available and can be accessed at https://www1.parallax.com!