Micropython for P2
ersmith
Posts: 6,068
Note: micropython is now included as part of FlexGUI, so the easiest way to run it is start flexgui.exe and select "Special > Run uPython on P2". But micropython does not depend in any way on FlexGUI, so you can also run it with PNut or any other download program, following the instructions below. If you want to run a newer version of micropython with an older FlexGUI, just unzip the upython.zip file somewhere and then copy all of the files into flexgui\samples\upython.
Also note that the current version can run on both revisions of the P2-ES boards and should fully support USB and VGA on both boards. Other boards (such as the P2D2) are untested, and may not work.
This is a micropython binary that can run on the P2.
Basic instructions: unzip and run upython.binary on the P2-ES board. It will communicate with the PC at 230400 baud on the standard serial pins. It will also try to talk to a VGA board on pin group 48 and USB serial host on pin group 16; the serial and USB/VGA communication are merged together, so input may come from either source and output goes to both.
If an SD card is present when upython.binary starts up, it will attempt to mount it and run "main.py" from the root directory.
See README.txt for more details.
Edit: 2020-01-08: v22, reads frequency settings from $14-$18 so loadp2 -PATCH works
Edit: 2020-01-02: v21, with input() function and some rudimentary help text
Edit: 2019-12-11: v20, now with pyb.Cpu() to allow running compiled PASM on other COGs
Edit: 2019-11-05: v19, new USB and VGA drivers which support both original and new P2-ES boards
Edit: 2019-09-16: v18, some minor performance tweaks and a fix to make the code compatible with new silicon
Edit: 2019-07-19: v17, basically the same micropython with tidied up internal code, a bit more user space, and a performance fix
Edit: 2019-07-17: v16b, same source code as v16 but compiled with a newer riscvp2 compiler for a performance boost
Edit: 2019-07-16: v16, with improved sdcard detection
Edit: 2019-07-14: v15, fixes ANSI escape sequences in the built-in VGA terminal
Edit: 2019-07-12: v14: mostly a clean up, with a few more built in modules (enough to support the pye.py editor). I've mentioned it elsewhere, but just in case: source code is now available at https://github.com/totalspectrum/micropython, using the compiler framework from https://github.com/totalspectrum/riscvp2.
Edit: 2019-06-23: v13: built with a new JIT compiler that supports compressed instructions, so it's faster and has more room for user code (we're back up to 200K for a while it was down to 144K due to all the additional built in modules). There are some debugging hooks in the emulator for I/O and other operations, which should make debugging easier and may allow for interesting hacks like @ozpropdev 's editor to be plugged in; see the README.txt for details, and/or ask here.
The SD card support is enhanced, and now micropython automatically runs "main.py" if there's an SD card plugged in when it starts up. The VGA text code is also enhanced, and allows for underline, blinking, and strikethrough as well as 256 foreground and background colors. With a VGA hooked up on pins 48 and a USB module on pins 16 the micropython interpreter can work in stand-alone mode.
Edit: 2019-06-13: v12, now with SD card FAT file system support
Edit: 2019-05-23: v11, fixes a typo in the buffered serial code that could cause characters to get stuck repeating
Edit: 2019-05-22: updated to v10, with buffered serial using another COG
Edit: 2019-05-21: updated to v9, with VGA on pins 48-55 and USB keyboard on pins 16-24 (via garryj's single COG USB keyboard/mouse driver).
Edit: 2019-05-16: updated to v8, with 192K RAM available now and some minor REPL improvements
Edit: 2019-04-04: updated to v7 with more user RAM available (128K now, was 32K before)
Edit: 2019-03-24: updated to v6, with long integer support (so all 32 bits of smartpin registers are accessible), and support for floating point and math functions
Edit: 2019-03-19: updated to v5, with improved counters and some performance tuning. There are two versions of the interpreter, upython.binary and upython_emu.binary. Both run at 160 MHz and communicate at 230_400 baud. Internally they have very different implementations; upython.binary runs on a JIT compiled RISCV emulator which should generally be much faster, but there may be some circumstances where the cache it uses starts to thrash, and then the plain emulator upython_emu.binary might be faster. Also bugs in one hopefully won't appear in the other, but you never know .
Edit: 2019-03-18: updated to v4, fixed pin reading an access to Pin(0)
Edit: 2019-03-15: updated to v3, now has a few smart pin functions implemented; docs still in README.txt
Here's a very simple MicroPython interpreter running on the P2 Eval board. Directions are in README.txt. Only one set of P2 functions are provided (in the pyb module) to turn pins on and off.
Also note that the current version can run on both revisions of the P2-ES boards and should fully support USB and VGA on both boards. Other boards (such as the P2D2) are untested, and may not work.
This is a micropython binary that can run on the P2.
Basic instructions: unzip and run upython.binary on the P2-ES board. It will communicate with the PC at 230400 baud on the standard serial pins. It will also try to talk to a VGA board on pin group 48 and USB serial host on pin group 16; the serial and USB/VGA communication are merged together, so input may come from either source and output goes to both.
If an SD card is present when upython.binary starts up, it will attempt to mount it and run "main.py" from the root directory.
See README.txt for more details.
Edit: 2020-01-08: v22, reads frequency settings from $14-$18 so loadp2 -PATCH works
Edit: 2020-01-02: v21, with input() function and some rudimentary help text
Edit: 2019-12-11: v20, now with pyb.Cpu() to allow running compiled PASM on other COGs
Edit: 2019-11-05: v19, new USB and VGA drivers which support both original and new P2-ES boards
Edit: 2019-09-16: v18, some minor performance tweaks and a fix to make the code compatible with new silicon
Edit: 2019-07-19: v17, basically the same micropython with tidied up internal code, a bit more user space, and a performance fix
Edit: 2019-07-17: v16b, same source code as v16 but compiled with a newer riscvp2 compiler for a performance boost
Edit: 2019-07-16: v16, with improved sdcard detection
Edit: 2019-07-14: v15, fixes ANSI escape sequences in the built-in VGA terminal
Edit: 2019-07-12: v14: mostly a clean up, with a few more built in modules (enough to support the pye.py editor). I've mentioned it elsewhere, but just in case: source code is now available at https://github.com/totalspectrum/micropython, using the compiler framework from https://github.com/totalspectrum/riscvp2.
Edit: 2019-06-23: v13: built with a new JIT compiler that supports compressed instructions, so it's faster and has more room for user code (we're back up to 200K for a while it was down to 144K due to all the additional built in modules). There are some debugging hooks in the emulator for I/O and other operations, which should make debugging easier and may allow for interesting hacks like @ozpropdev 's editor to be plugged in; see the README.txt for details, and/or ask here.
The SD card support is enhanced, and now micropython automatically runs "main.py" if there's an SD card plugged in when it starts up. The VGA text code is also enhanced, and allows for underline, blinking, and strikethrough as well as 256 foreground and background colors. With a VGA hooked up on pins 48 and a USB module on pins 16 the micropython interpreter can work in stand-alone mode.
Edit: 2019-06-13: v12, now with SD card FAT file system support
Edit: 2019-05-23: v11, fixes a typo in the buffered serial code that could cause characters to get stuck repeating
Edit: 2019-05-22: updated to v10, with buffered serial using another COG
Edit: 2019-05-21: updated to v9, with VGA on pins 48-55 and USB keyboard on pins 16-24 (via garryj's single COG USB keyboard/mouse driver).
Edit: 2019-05-16: updated to v8, with 192K RAM available now and some minor REPL improvements
Edit: 2019-04-04: updated to v7 with more user RAM available (128K now, was 32K before)
Edit: 2019-03-24: updated to v6, with long integer support (so all 32 bits of smartpin registers are accessible), and support for floating point and math functions
Edit: 2019-03-19: updated to v5, with improved counters and some performance tuning. There are two versions of the interpreter, upython.binary and upython_emu.binary. Both run at 160 MHz and communicate at 230_400 baud. Internally they have very different implementations; upython.binary runs on a JIT compiled RISCV emulator which should generally be much faster, but there may be some circumstances where the cache it uses starts to thrash, and then the plain emulator upython_emu.binary might be faster. Also bugs in one hopefully won't appear in the other, but you never know .
Edit: 2019-03-18: updated to v4, fixed pin reading an access to Pin(0)
Edit: 2019-03-15: updated to v3, now has a few smart pin functions implemented; docs still in README.txt
Here's a very simple MicroPython interpreter running on the P2 Eval board. Directions are in README.txt. Only one set of P2 functions are provided (in the pyb module) to turn pins on and off.
zip
156K
Comments
Details por favor? Is this actually MicroPython, or did you write your own Python interpretter? Where's your source? How did you compile it?
I'm just curious...
Only few kilobytes of RAM available to the engine (<64 KB RAM)
Constrained ROM space for the code of the engine (<200 KB ROM)
The engine supports on-device compilation, execution and provides access to peripherals from JavaScript.
Cool. How much code space does it take up in this minimal form?
@FredBlais : What, Python isn't good enough for you? Seriously though my plate is pretty full right now and I already spend more time than I should on my P2 hobby, so I'm not ready to tackle porting yet another language to the P2.
@Mark_T : It's not fully stripped down, so I don't know how low it could go, but the binary size of the current version is readily found from the .binary in the zip file I posted:
It's at 160 MHz.
I should also point out that it's not really optimized at all yet, so I'm not sure how useful timing comparisons will be at this point.
I suppose that people that want to code in Python on a mcu are more interested with the ease of use and rapid prototyping than speed. There is a lot of opportunity with the smartpins, could you add in the pyb module support for WRPIN WXPIN WYPIN and RDPIN on a selected IO?
Any cog does not know, or care what the other code in other cogs is, or how it was compiled.
Some housekeeping stuff to allow shared mapping of data areas, and merge of code for download ? ( Debug could get more interesting... )
Really nice improvement there Eric, that seems about 4x faster on primitive pin toggling
Like Fred said, I doubt speed matters a whole lot at this stage. Spin2 is really going to fly (will be very hard to 'catch), and many of the micropython folk are likely to be interested in what the smartpins can do (since they can choose other micropython platforms for speed, but not the same pin flexibility).
So i agree that opening up the smartpins would be high priority to showcase P2
I think it would be really cool if we put together a list (it will probably be a BIG list) of as many different things that the smartpins can do and came up with a common, language-agnostic API that various HALs could implement. We use some simple psuedo-code (spin syntax ought to work well for this purpose) to define this API and then translating that psuedo code to a specific language would take minimal effort:
and that can be translated into C and Python as:
I bring this up here and now because I'd love to see this happen (help make it happen) rather than everyone start implementing their own unique HALs around the smart pins in the various languages that are quickly developing for the P2.
I think doing pseudo code in spin syntax makes a lot of sense, too.
But I agree with your overall sentiment, that interactive languages like python are probably better suited for development and quick testing of hardware features, so we should provide building blocks that people can use.
Python is quickly becoming the top programming language for education. I'm really not sure about the industry uses either, but educational sales are the primary source of revenue for Parallax. For the Propeller 2, we are currently considering Spin/PASM as the primary language for commercial products and Python for education.
I'm watching this discussion closely, along with others.
Ken Gracey
Luckily, we already have some C solutions coming together.
Basic is also not going away, and looks to already be running on P2, so that's another pathway to attract new users from other hardware.
Users (and their managers) will cool off very quickly, if you say they need to learn both a new language and a new MCU architecture.
Agreed. What we can actually support with examples and documentation is a serious consideration for Parallax. Spin/ASM are certainly going to be supported, along with a language for education - most likely microPython. To get microPython we will need C, so there will be a C compiler. We wouldn't be able to provide the depth of support for C like we would for Spin/ASM and microPython. We have only 30 staff, limited resources, and our educational customers keep this business productive - and have enabled all the P2 R&D.
We're watching the forum discussion and researching options as well with the intent of getting behind some efforts soon.
Ken Gracey
I highly recommend getting behind ersmith's fastspin. It's kind of everything in one for both P1 and P2.
What happened to Blockly? Doesn't that use (or compile to) C?
Tom