Some issue of Pyboard GPIO speed

The official pyboard running MicroPython.
This is the reference design and main target board for MicroPython.
You can buy one at the store.
Target audience: Users with a pyboard.
Post Reply
doone
Posts: 3
Joined: Wed Jul 06, 2022 1:53 pm

Some issue of Pyboard GPIO speed

Post by doone » Wed Jul 06, 2022 2:23 pm

Hi, I am a fresh man and I have some troubles of my pyboard project. I need use pyboard V1.1 to control sk6182rgbw led array. Usually we use SPI mosi pin to do this, but this time all mosi pin had been used. So I try to use software SPI to do this, but:
1. I dont know the pyboard GPIO speed? There have no parameters to config this in pyb.Pin
2. Datasheet said that GPIO slew rate can reach Mhz, but I test it just 77.6Khz, and I dont know why.
test code:

Code: Select all

    
    from pyb import Pin,freq
    import stm

    led = Pin('Y9',Pin.OUT_PP)

    while 1:
        led.high()
        led.low()
Looking forward your help , thanks.

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: Some issue of Pyboard GPIO speed

Post by jimmo » Wed Jul 06, 2022 3:31 pm

doone wrote:
Wed Jul 06, 2022 2:23 pm
So I try to use software SPI to do this, but:
Software SPI means something else in MicroPython -- see https://docs.micropython.org/en/latest/ ... ne.SoftSPI (basically the firmware provides a bit-banging implementation of SPI in C for you. it's much faster than doing it from Python).

But yeah, what you're doing is bit-banging from Python, which will be slow.
doone wrote:
Wed Jul 06, 2022 2:23 pm
sk6182rgbw led array
The SK6182 doesn't use SPI, it uses a syncronous serial protocol similar to NeoPixels (WS2812). There's no way this can be done from Python, it's hard enough from C.

Fortunately we provide a driver (machine.bitstream) for this protocol in the Pyboard firmware, and there's a Python library you can use to control it -- https://github.com/micropython/micropyt ... eopixel.py

Note that the timing for SK6812 is different to WS2812, so you'll need to set a custom timing when you create the NeoPixel object. See https://cdn-shop.adafruit.com/product-f ... sheet+.pdf

Code: Select all

import neopixel

pin = machine.Pin('X1')
n = NeoPixel(pin, 100, bpp=4, timing=(300, 900,600,600))

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: Some issue of Pyboard GPIO speed

Post by jimmo » Wed Jul 06, 2022 3:41 pm

Forgot to include a link to the documentation: https://docs.micropython.org/en/latest/ ... pixel.html

doone
Posts: 3
Joined: Wed Jul 06, 2022 1:53 pm

Re: Some issue of Pyboard GPIO speed

Post by doone » Wed Jul 06, 2022 4:13 pm

jimmo wrote:
Wed Jul 06, 2022 3:31 pm
doone wrote:
Wed Jul 06, 2022 2:23 pm
So I try to use software SPI to do this, but:
Software SPI means something else in MicroPython -- see https://docs.micropython.org/en/latest/ ... ne.SoftSPI (basically the firmware provides a bit-banging implementation of SPI in C for you. it's much faster than doing it from Python).

But yeah, what you're doing is bit-banging from Python, which will be slow.
doone wrote:
Wed Jul 06, 2022 2:23 pm
sk6182rgbw led array
The SK6182 doesn't use SPI, it uses a syncronous serial protocol similar to NeoPixels (WS2812). There's no way this can be done from Python, it's hard enough from C.

Fortunately we provide a driver (machine.bitstream) for this protocol in the Pyboard firmware, and there's a Python library you can use to control it -- https://github.com/micropython/micropyt ... eopixel.py

Note that the timing for SK6812 is different to WS2812, so you'll need to set a custom timing when you create the NeoPixel object. See https://cdn-shop.adafruit.com/product-f ... sheet+.pdf

Code: Select all

import neopixel

pin = machine.Pin('X1')
n = NeoPixel(pin, 100, bpp=4, timing=(300, 900,600,600))
Thank you very much, it is perfect to solve my trouble. And I have realized control sk6812 by software SPI. I didn't expect Python to reduce the GPIO frequency so much.

doone
Posts: 3
Joined: Wed Jul 06, 2022 1:53 pm

Re: Some issue of Pyboard GPIO speed

Post by doone » Wed Jul 06, 2022 8:35 pm

jimmo wrote:
Wed Jul 06, 2022 3:31 pm
Fortunately we provide a driver (machine.bitstream) for this protocol in the Pyboard firmware, and there's a Python library you can use to control it -- https://github.com/micropython/micropyt ... eopixel.py
Hello, as your suggestion, I also try to use the neopixel library to control my SK6812, but there a error when use it in pyboard V1.1

Code: Select all

from neopixel import NeoPixel
pin = machine.Pin('Y9')
n = NeoPixel(pin, 5, bpp=4)
n.ORDER = (0,1,2,3)
n.TIMING = (300,900,600,600)
n.__setitem__((0,0,0,0))
n.write()

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "neopixel.py", line 26, in write
AttributeError: 'module' object has no attribute 'pixelbitstream'
I also try to use pyfile from https://github.com/micropython/micropyt ... eopixel.py, but it is also an error:

Code: Select all

from machine import bitstream

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: can't import name bitstream
Is the bitstream function had been integrated in pyboard lib? How to fix this error?

BTW, I have another question which maybe stupid, If I want to see the source code of built-in libraries like neopixel.py, where can I get them? because neopixel.py have an error but I can't find it and fix the error.

Thank you again!

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: Some issue of Pyboard GPIO speed

Post by jimmo » Thu Jul 07, 2022 12:39 am

doone wrote:
Wed Jul 06, 2022 8:35 pm
AttributeError: 'module' object has no attribute 'pixelbitstream'
Where did you get the neopixel.py that is running on your device? As far as I know "pixelbitstream" was never in a released version? You need to use the version I linked to.
doone wrote:
Wed Jul 06, 2022 8:35 pm
Is the bitstream function had been integrated in pyboard lib? How to fix this error?
That looks like the correct version. Perhaps your MicroPython firmware is out of date, this was only added relatively recently? See https://micropython.org/download/pybv11/
doone wrote:
Wed Jul 06, 2022 8:35 pm
BTW, I have another question which maybe stupid, If I want to see the source code of built-in libraries like neopixel.py, where can I get them? because neopixel.py have an error but I can't find it and fix the error.
On the pyboard, neopixel.py isn't built-in, but it uses the built-in "bitstream" function in the machine module (see https://github.com/micropython/micropyt ... itstream.c and https://github.com/micropython/micropyt ... itstream.c). On ports where it is (esp8266, esp32, rp2), it's the file I linked to. (Technically the Python file is "frozen" into the firmware).

For other built-in libraries, they are typically implemented in C and are either in the "py" or "extmod" directory of the main repo. e.g. the "sys" module is here https://github.com/micropython/micropyt ... y/modsys.c and and the json module is here https://github.com/micropython/micropyt ... modujson.c

Post Reply