Tiny Core Linux

Tiny Core Base => Raspberry Pi => Topic started by: gavinmc42 on August 31, 2015, 12:40:33 AM

Title: Micropython + GPIO
Post by: gavinmc42 on August 31, 2015, 12:40:33 AM
Been testing the new 7 releases which have Micropython built in.

Was trying to turn GPIO leds on/off, found one solution that works.
Thought someone might be interested.
You also need to make the GPIO class values outputs and writable.
I do this with a separate shell script on startup.

With the ESA taking an interest in Micropython and a Astro RPi + hat going into space maybe they will get the i2c/SPI/GPIO in Micropython working for the Raspberry Pi.


import _os as os
import sys

# red 24, green 22, blue 23

os.system("date")

def led22on():
    os.system("echo 1 > /sys/class/gpio/gpio22/value")
   
def led22off():
    os.system("echo 0 > /sys/class/gpio/gpio22/value")
   
def led23on():
    os.system("echo 1 > /sys/class/gpio/gpio23/value")
   
def led23off():
    os.system("echo 0 > /sys/class/gpio/gpio23/value")
   
def led24on():
    os.system("echo 1 > /sys/class/gpio/gpio24/value")
   
def led24off():
    os.system("echo 0 > /sys/class/gpio/gpio24/value")

led22off()
led23off()
led24off()

led24on()





Title: Re: Micropython + GPIO
Post by: gavinmc42 on August 31, 2015, 06:14:01 PM
Scripting i2c etc.

Been looking at other scripting languages, Lua and Micropython to access GPIO, i2c
i2c requires i2c-tools.tcz installed.
GPIO needs /sys/class/gpio/gpioxx/value to be writable.

Lua
dump = os.execute("sudo i2cdump -y 1 0x08")
print(dump)

Python
dump = os.system("sudo i2cdump -y 1 0x08")
print(dump)

i2cget, i2cset work fine to read/write i2c.

It will be interesting to compare Busybox Shell, Lua, Micropython and Luajit to see how fast the GPIO pins can toggle.



Title: Re: Micropython + GPIO
Post by: Greg Erskine on September 02, 2015, 12:15:22 AM
hi gavinmc42,

I am following this thread so keep posting information.

http://codeandlife.com/2012/07/03/benchmarking-raspberry-pi-gpio-speed/

regards
Greg
Title: Re: Micropython + GPIO
Post by: gavinmc42 on September 03, 2015, 09:11:26 PM
OK Shell is faster than Micropython or Lua calling system commands

Shell script

while true
do
    echo 1 > /sys/class/gpio/gpio22/value
    echo 0 > /sys/class/gpio/gpio22/value
done

Micropython

import _os as os

while True:
    os.system("echo 1 > /sys/class/gpio/gpio22/value")
    os.system("echo 0 > /sys/class/gpio/gpio22/value")

Lua

while true
do
    os.execute("echo 1 > /sys/class/gpio/gpio22/value")
    os.execute("echo 0 > /sys/class/gpio/gpio22/value")
end

Shell 2KHz, Python 42Hz, Lua 42Hz - 50 times slower.

Hey but at least it worked:)
If you only want to read and log a temperature every 5 minutes this is no issue.
Native hardware access to GPIO, I2C, SPI in Micropython, Lua will be better?

Shell is kind of hard to read when you get to a few hundred lines.
Calling Sed and Awk could be slow:)

Hmm call the shell scripts from Lua?
os.execute("./flashled.sh")   no surprise gives 2KHz
Title: Re: Micropython + GPIO
Post by: bmarkus on September 03, 2015, 10:18:33 PM
In the shell version you are using file operations only, while in Python and Lua calling another program with shell to do the same. Equivalent solution in Python:


Code: [Select]
while True:
    fp = open('/sys/class/gpio/gpio22/value', 'w')
    fp.write('1\n')
    fp.close()

    fp = open('/sys/class/gpio/gpio22/value', 'w')
    fp.write('0\n')
    fp.close()


   
Title: Re: Micropython + GPIO
Post by: gavinmc42 on September 04, 2015, 01:30:04 AM
Thanks Bela,

That gives 2.8KHz, a bit faster than Shell.
Lua is 1.3KHz

Might try more file I/O coding.
So much still to learn.
Title: Re: Micropython + GPIO
Post by: bmarkus on September 04, 2015, 01:55:44 AM
Can you try the Python code with the normal 2.7 version instead of Micropython?
Title: Re: Micropython + GPIO
Post by: gavinmc42 on September 04, 2015, 02:14:25 AM
Will have to install the real python 1st.
The point was to try to do everything with the standard SSH piCore to keep the OS size down.
Lua is not to bad at 128KB

With Shell, Micropython and Lua I have three scripting languages I can use without bloating the OS.
Python is 9MB + libs, may as well install Go 29MB and learn another language:)
Most of my stuff is sensor logging with web serving the data via busybox-httpd.
RPi is over kill so I have spare cpu %.

Maintaining OS's is a pain, backing up Raspbian or Ubuntu Mate is 8GB at a time.
I try to fit everything on my smallest SD cards, 128MB, quick, easy to backup.
That's why I love piCore.
Title: Re: Micropython + GPIO
Post by: bmarkus on September 04, 2015, 02:30:46 AM
If you get some time, please try Python just to see the performance.

GO creates static executables so expect larger applications, which are running nearly as fast as native C code.

You can try LUAJIT too, which significantly speeds up execution of LUA scripts. Just for testing.


Title: Re: Micropython + GPIO
Post by: curaga on September 04, 2015, 02:31:28 AM
The latest Go released just recently finally supports shared executables. Also gccgo supported them for a while.
Title: Re: Micropython + GPIO
Post by: bmarkus on September 04, 2015, 06:54:44 AM
gavinmc42:

Are you testing on RPi or RPi2?
Title: Re: Micropython + GPIO
Post by: bmarkus on September 04, 2015, 06:56:38 AM
The latest Go released just recently finally supports shared executables. Also gccgo supported them for a while.

Thanks, I haven't recognized the new 1.5 which is a major new release with interesting changes. However at first and fast reading of  change list have seen nothing about shared libs.
Title: Re: Micropython + GPIO
Post by: bmarkus on September 04, 2015, 09:02:40 AM
Go 1.5 build fails on Raspberry Pi and x86. Maybe better to wait for 1.5.2 or so...
Title: Re: Micropython + GPIO
Post by: gavinmc42 on September 06, 2015, 06:35:41 PM
Hi Bela,

Using up stock of my old Model B's.
Latest Luajit and Go not working any more, think they may already be compiled for ARM7, Pi2.
Get an ARM6 expecting ARM7 warning.
Come to think of it maybe I only tested Go on the Pi 2.
Only reason for learning Go is the multicore capability, no need on old Pi's.

Will try the real python.tcz, stay tuned.

Title: Re: Micropython + GPIO
Post by: gavinmc42 on September 06, 2015, 07:10:38 PM
Bela,

Using your file method, python 2.7 flashes the led at 980Hz, Micropython does 2.8KHz, three times faster.
Maybe because of all the bloat stuff the normal python loads.

Just remembered GoBot, runs GO so it must work on old models.
http://gobot.io/
Uses version 1.4?
Made one attempt to install it and failed, will wait it someone smarter or with more time figures it out.
Wanted to make some bots for the kid, teach him Go at the same time.

Title: Re: Micropython + GPIO
Post by: bmarkus on September 06, 2015, 10:34:37 PM
Each line is 2 bytes long. If you are not closing the file output will be buffered and written at once causing unwanted wave form and timing on the GPIO pin.

Note: did not test with scope or analyzer, so it is just a theory :)
Title: Re: Micropython + GPIO
Post by: bmarkus on September 06, 2015, 10:38:32 PM
Hi Bela,

Using up stock of my old Model B's.
Latest Luajit and Go not working any more, think they may already be compiled for ARM7, Pi2.


Interesting, will recheck. Thanks for reporting.
Title: Re: Micropython + GPIO
Post by: Rich on September 06, 2015, 10:45:38 PM
Hi bmarkus
Why did you delete my post?
Title: Re: Micropython + GPIO
Post by: bmarkus on September 06, 2015, 10:46:40 PM
Sorry, it was an accident :(
Title: Re: Micropython + GPIO
Post by: Rich on September 06, 2015, 10:51:05 PM
Hi bmarkus
No problem, I'll recreate it.
Title: Re: Micropython + GPIO
Post by: Rich on September 06, 2015, 11:01:57 PM
Hi gavinmc42
Quote
Using your file method, python 2.7 flashes the led at 980Hz, Micropython does 2.8KHz, three times faster.
Maybe because of all the bloat stuff the normal python loads.
You set up the loop like this:
Code: [Select]
fp = open('/sys/class/gpio/gpio22/value', 'w')
while True:
    fp.write('1\n')
    fp.write('0\n')
Right? If you include the file open and close commands in the loop it will probably slow things down.

In response to bmarkus's comment:
Quote
Each line is 2 bytes long. If you are not closing the file output will be buffered and written at once causing unwanted wave form and timing on the GPIO pin.

Note: did not test with scope or analyzer, so it is just a theory
If that's the case, adding a flush command after each write would be appropriate.
Title: Re: Micropython + GPIO
Post by: bmarkus on September 06, 2015, 11:27:46 PM
If that's the case, adding a flush command after each write would be appropriate.

The MicroPython os module do not have flush() method. You can make os.system('sync') but it is a slow operation. Need testing. Anyhow, MicroPython seems to be a valuable tool.
Title: Re: Micropython + GPIO
Post by: gavinmc42 on September 07, 2015, 12:05:04 AM
Smoking,
Micropython 58KHz, about the same for Lua on my multimeter.
Will have to stick CRO on pin to check jitter.

Sort of makes sense, GPIO pin is not really a file.
Probably will have more to do with locking?

Use script to write 16x2 LCD commands.
Some of my boxes really need an ip address display.
Could be a simple solution.
Write only to 5V LCD, can do that with 6 pins and 3.3v I/O.
Hmm PCB redesign.
Title: Re: Micropython + GPIO
Post by: bmarkus on September 07, 2015, 01:06:59 AM
Hi Bela,

Using up stock of my old Model B's.
Latest Luajit and Go not working any more, think they may already be compiled for ARM7, Pi2.
Get an ARM6 expecting ARM7 warning.
Come to think of it maybe I only tested Go on the Pi 2.
Only reason for learning Go is the multicore capability, no need on old Pi's.

Will try the real python.tcz, stay tuned.

GO/LUAJIT issue confirmed, I will fix them.
Title: Re: Micropython + GPIO
Post by: gavinmc42 on September 07, 2015, 01:49:20 AM
Thanks Bela,

Interested in Lua because of Luajit.
Near C compiled speeds?

Title: Re: Micropython + GPIO
Post by: bmarkus on September 07, 2015, 02:06:00 AM
A year or so ago we made benchmarks. It was focusing on memory operations, allocations and not a full blown test. Winner was pure C. Next, very close GO and BACON BASIC compiler as a supprize. It was followed by LUAJIT a bit slower. Then far behind Ruby, than PERL and cPython. For sure it may very a bit depending on applications, but first three is expected are C/C++, GO and BACON. That time C# (Mono) was not tested.
Title: Re: Micropython + GPIO
Post by: bmarkus on September 07, 2015, 05:07:27 AM
Latest Luajit and Go not working any more, think they may already be compiled for ARM7, Pi2.

Tested luajit on piCore-7.0alpha3 running on old 'Rev B' RPi:

Code: [Select]
# uname -a
Linux box 4.1.6-piCore+ #1 PREEMPT Fri Aug 28 07:13:51 CEST 2015 armv6l GNU/Linux

# luajit -v
LuaJIT 2.0.3 -- Copyright (C) 2005-2014 Mike Pall. http://luajit.org/

# lua fact.lua
Enter a number:
6
720

# luajit fact.lua
Enter a number:
6
720

# lua hello.lua
Hello World

# luajit hello.lua
Hello World

It works fine as expected. Everything is freshly installed from the repo.

Can you show the case when it fails?


 
Title: Re: Micropython + GPIO
Post by: gavinmc42 on September 07, 2015, 07:52:32 PM
I manually installed this version again.
http://distro.ibiblio.org/tinycorelinux/7.x/armv6/tcz/

I might have put the ARM7 version in by mistake.
Luajit is working now, version 2.0.3

file = io.open("/sys/class/gpio/gpio22/value", "w")
while true do
    file:write("1")
    file:flush()
    file:write("0")
    file:flush()
end

Took me a while to figure out that in Lua file write is buffered.
26KHz for Lua, about half speed of micropython because of the two extra file:flush() lines
92KHz same code -> Luajit

Title: Re: Micropython + GPIO
Post by: bmarkus on September 08, 2015, 12:01:06 AM
LuaJIT updated to 2.0.4
Title: Re: Micropython + GPIO
Post by: gavinmc42 on September 08, 2015, 12:40:45 AM
Not yet pushed onto
http://distro.ibiblio.org/tinycorelinux/7.x/armv6/tcz/
?

Just noticed I've got old i2c-tools as well.

Micropython is now fast enough to drive a stepper motor, could not do it fast enough in shell.
Had just started designing a Cypress PSoC pcb to do the driving, controlled via i2c.
Now I can do it all on the Pi:)

Not sure if I want to do a three axis controller yet.
Title: Re: Micropython + GPIO
Post by: bmarkus on September 08, 2015, 01:18:28 AM
Not yet pushed onto
http://distro.ibiblio.org/tinycorelinux/7.x/armv6/tcz/
?


Main repo is repo.tinycorelinux.net ibiblio is just a mirror synced once a day.
Title: Re: Micropython + GPIO
Post by: gavinmc42 on September 08, 2015, 06:04:54 PM
That mirror is the only one I can download tcz's from in Windows.
Thought it might be my browser but with Chrome and Firefox all I can see is a txt list of tcz's.

I can live with once a day update on http://distro.ibiblio.org/tinycorelinux/7.x/armv6/tcz/ but it is broken at the moment :'(
Title: Re: Micropython + GPIO
Post by: gavinmc42 on September 08, 2015, 07:56:54 PM
Working now, Luajit 2.0.4

Also updated from 4.1.4 to 4.1.6 Alpha3, much faster boot and SSH is also faster.
Filezilla is also faster, used to be a wait before anything downloaded.

piCore just keeps getting better.
Thanks Bela.

Rechecked Go got following message

runtime: this CPU has no VFPv3 floating point hardware, so it cannot run
this GOARM=7 binary. Recompile using GOARM=6.
Title: Re: Micropython + GPIO
Post by: bmarkus on September 09, 2015, 02:10:16 AM

Rechecked Go got following message

runtime: this CPU has no VFPv3 floating point hardware, so it cannot run
this GOARM=7 binary. Recompile using GOARM=6.

Go updated for armv6
Title: Re: Micropython + GPIO
Post by: gavinmc42 on September 09, 2015, 02:10:59 AM
Thanks Bela,

Will try it tomorrow
Title: Re: Micropython + GPIO
Post by: gavinmc42 on September 09, 2015, 06:48:36 PM
Interesting.

Go is working, did a Println("hello world")
Generated a nearly 1.6MB run file.
Wonder what gets compiled into that?
Go compiles quick so maybe use go run xxx.go instead of go build xxx.go.

Might leave C and Go for another day.
Title: Re: Micropython + GPIO
Post by: bmarkus on September 10, 2015, 12:59:53 AM
Interesting.

Go is working, did a Println("hello world")
Generated a nearly 1.6MB run file.
Wonder what gets compiled into that?
Go compiles quick so maybe use go run xxx.go instead of go build xxx.go.

Might leave C and Go for another day.

Go creates static linked executable, it is why size is big. Built executable doesn't need Go environment run.

If you are looking for small size and high performance, use C.
Title: Re: Micropython + GPIO
Post by: curaga on September 10, 2015, 01:57:31 AM
The tiny C compiler (tcc) looks like it has some arm support.
Title: Re: Micropython + GPIO
Post by: gavinmc42 on September 10, 2015, 11:19:29 PM
Just tried compiling Synergy on Raspbian.
So many dependencies and internet required to apt-get install them.

Going to leave c compiling for another day/year and stick with scripting.

TCC would be nice though.
I liked QuickC on Windows.
Current Visualxxx bloatware does not impress me.
Hmm jump in time machine , go back to Turbopascal? Latest version is FPC + Lazarus, 700MB? ok not small then.
While I'm back in the past -Forth?
Yep alive and kicking on the Pi, even bare metal Forth??? 8k install!!!


Title: Re: Micropython + GPIO(where is the prob)
Post by: vinikon on February 21, 2018, 06:23:41 AM
Hi,
I just tried "shell" version to write directly to pins and got this:
"can't create /sys/class/gpio22/value: nonexistent directory"

I also get this " ls /sys/class/gpio22 /sys/class/gpio22: No such file or directory"

I'm using Raspi ver.1 on Linux box 4.9.22-piCore #1 Sat Apr 15 15:30:03 UTC 2017 armv6l GNU/Linux

Please tell me what's wrong with.

Thanks
Title: Re: Micropython + GPIO(where is the prob)
Post by: bmarkus on February 21, 2018, 09:00:21 PM
Hi,
I just tried "shell" version to write directly to pins and got this:
"can't create /sys/class/gpio22/value: nonexistent directory"

I also get this " ls /sys/class/gpio22 /sys/class/gpio22: No such file or directory"

I'm using Raspi ver.1 on Linux box 4.9.22-piCore #1 Sat Apr 15 15:30:03 UTC 2017 armv6l GNU/Linux

Please tell me what's wrong with.

Thanks

If the script you are running is unknown no way to give answer.