User avatar
joan
Posts: 15623
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Pico as a computer peripheral

Sun Apr 11, 2021 10:37 pm

I have written a Python module which allows control of the Pico via the microUSB link (typically /dev/ttyACM0 on Linux). This is a pre-release.

  • reading and writing GPIO singly and in groups
  • PWM pulses
  • Servo pulses
  • Read PWM frequency, dutycycle, and high edges
  • callbacks on GPIO level change
  • I2C wrapper
  • SPI wrapper
  • serial link wrapper
The Python module is called picod and does the standard GPIO type things (PIO is currently unsupported). picod will run on any Python box.

A command interpreter has to be flashed to the Pico. The interpreter is written in C and is called picod.c.

http://abyz.me.uk/picod/index.html

https://github.com/joan2937/picod

Download

The picod archive may be downloaded with the following commands.

Code: Select all

wget http://abyz.me.uk/picod/picod.zip

unzip picod.zip
Build the daemon

Enter the picod/DAEMON directory.

The next step can be ignored if you do not want to build from source. There is a pre-built picod.uf2 in the directory.

Code: Select all

cmake .

make
Copy the daemon to the Pico

Push and hold the BOOTSEL button on the Pico, then connect to your computer using a micro USB cable. Release BOOTSEL once the drive RPI-RP2 appears on your computer.

Drag and drop the picod.uf2 file to the RPI-RP2 drive. The Raspberry Pi Pico will reboot and will now run the daemon.

Build Python modules

Enter the picod/PYTHON directory then build for the versions of Python you plan to use.

Code: Select all

python2 -m build

sudo python2 setup.py install

python3 -m build

sudo python3 setup.py install
To check the installation.

Enter the picod/EXAMPLES/py_picod directory then

Code: Select all

python flash.py # flash the Pico LED

python callback.py # test GPIO callbacks

python adc.py # test ADC reads

epoch1970
Posts: 6519
Joined: Thu May 05, 2016 9:33 am
Location: Paris, France

Re: Pico as a computer peripheral

Mon Apr 12, 2021 11:13 am

Yay!
"S'il n'y a pas de solution, c'est qu'il n'y a pas de problème." Les Shadoks, J. Rouxel

gordon77
Posts: 5686
Joined: Sun Aug 05, 2012 3:12 pm

Re: Pico as a computer peripheral

Mon Apr 12, 2021 4:30 pm

Sounds interesting.

If I want a pwm output on the pico do l need to run a python script on the pi to start it , or alter it?

Can the pico then run standalone and start or alter the pwm output, eg using main.py at boot and a micropython script ?

User avatar
joan
Posts: 15623
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Pico as a computer peripheral

Mon Apr 12, 2021 5:15 pm

gordon77 wrote:
Mon Apr 12, 2021 4:30 pm
Sounds interesting.

If I want a pwm output on the pico do l need to run a python script on the pi to start it , or alter it?

Can the pico then run standalone and start or alter the pwm output, eg using main.py at boot and a micropython script ?
This is a standalone system. The Pico runs a fixed dedicated command interpreter. The Python script talks to the interpreter via /dev/ttyACM0 to tell the Pico what to do.

It has no connection with micropython and can not co-exist.

As far as I can tell the C SDK and the Python (micropython) SDK are all about developing standalone applications to be flashed to the Pico.

Think of what I have done as more suited to port expander type applications. You have a laptop, you have a Pico, you have some hardware. You want to use your laptop to talk to that hardware and are using the Pico as the middleman.

gordon77
Posts: 5686
Joined: Sun Aug 05, 2012 3:12 pm

Re: Pico as a computer peripheral

Mon Apr 12, 2021 5:22 pm

Thanks for the explanation.

User avatar
scruss
Posts: 4101
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON
Contact: Website

Re: Pico as a computer peripheral

Mon Apr 12, 2021 7:17 pm

joan wrote:
Mon Apr 12, 2021 5:15 pm
Think of what I have done as more suited to port expander type applications. You have a laptop, you have a Pico, you have some hardware. You want to use your laptop to talk to that hardware and are using the Pico as the middleman.
Very nice. A bit like a more flexible version of the old Firmata protocol for Arduinos.
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.
Pronouns: he/him

User avatar
nick.mccloud
Posts: 1228
Joined: Sat Feb 04, 2012 4:18 pm

Re: Pico as a computer peripheral

Tue Apr 13, 2021 9:02 am

Wow, just wow. A fantastic utility.

The first work of genius is putting the version number in the top level of the repro.

But the code, it's got so much structure, so many comments, many explanations, diagrams etc. Brilliant. Absolutely amazing brilliant.

Now I HAVE to find a project to use this on ASAP so I can bathe in the warm glow of it's awesomeness.


Only issue - the use of BSD brace style ;)
Pico/RP2040 ≠ Arduino
Pico = hot rod kit car, Arduino = hot rod kit car wrapped in cotton wool with buoyancy aids & parachute

User avatar
joan
Posts: 15623
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Pico as a computer peripheral

Tue Apr 13, 2021 11:16 am

There are a number of issues I am working on.

  • GPIO callback timestamps. USB traffic screws up the recording of GPIO level changes. I though this could be cured by putting the recording on the other core. That thought was wrong. Perhaps I have screwed up the buffering.
  • when receiving as a SPI slave not all data is returned by a spi_pop(). Some data is not moved from the hardware FIFO until the FIFO receives more data.
  • event and reply callbacks need examples.
  • hardware UART 0 doesn't work properly. Presumably still problems between stdio going to USB and the UART.

Mallon
Posts: 10
Joined: Wed Apr 28, 2021 8:52 am

Re: Pico as a computer peripheral

Wed Apr 28, 2021 8:59 am

This is amazing! Been playing around with it and it's a dream so far.

Quick query, how do I make sense of the GPIO_read return? How would I tell which level is for which pin?

User avatar
Gavinmc42
Posts: 5694
Joined: Wed Aug 28, 2013 3:31 am

Re: Pico as a computer peripheral

Wed Apr 28, 2021 9:47 am

Going to need even more Picos.
DPI/SPI LCD interface?
I'm dancing on Rainbows.
Raspberries are not Apples or Oranges

User avatar
B.Goode
Posts: 12089
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Pico as a computer peripheral

Wed Apr 28, 2021 9:55 am

Mallon wrote:
Wed Apr 28, 2021 8:59 am
This is amazing! Been playing around with it and it's a dream so far.

Quick query, how do I make sense of the GPIO_read return? How would I tell which level is for which pin?


Ref: https://github.com/joan2937/picod/blob/ ... N/picod.py

Code: Select all

 def GPIO_read(self, reply=REPLY_NOW, flush=True):
      """
      Returns the current levels of all GPIO.
      If GPIO x is high bit x of the returned levels will be set.
      If GPIO x is low bit x of the returned levels will be clear.

To translate from GPIO numbers to physical pins on the 2x20-way header see something like https://pinout.xyz/ or use pinout at a shell prompt on the RasPiOS system.

User avatar
joan
Posts: 15623
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Pico as a computer peripheral

Wed Apr 28, 2021 11:03 am

I did update the example for that yesterday.

Code: Select all

status, levels = pico.GPIO_read()

if status == picod.STATUS_OKAY:
   print("GPIO levels are 0x{:x}".format(levels))
>>> GPIO levels are 0x100800e

   if levels & (1<<6): # check gpio 6
      print("gpio 6 is high")
   else:
      print("gpio 6 is low")
>>> gpio 6 is low

User avatar
joan
Posts: 15623
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Pico as a computer peripheral

Wed Apr 28, 2021 11:41 am

Update.

I have put GPIO callbacks on the other core and they are now reliable. I did 75855 DHT22 reads at one every two seconds before I got bored. Every one was error free.

I am disappointed with the throughput of GPIO events, it seems to handle 800 per second sustained. I expected a higher figure. Up to 10 thousand events can be buffered at the Pico.

I have corrected an error which stopped it working on Windows/Macs.

Mallon
Posts: 10
Joined: Wed Apr 28, 2021 8:52 am

Re: Pico as a computer peripheral

Wed Apr 28, 2021 11:46 am

Lovely, I understand now. Thanks for the quick response!

User avatar
Gavinmc42
Posts: 5694
Joined: Wed Aug 28, 2013 3:31 am

Re: Pico as a computer peripheral

Thu Apr 29, 2021 8:17 am

Example uses ILI9341 driver, 2.8" LCD?
Small slow vids?
Or a Linux diagnostics display?
I am sure people can think of things to do with it.

A Zero version can do Youtubes.

DisplayLink is propriety, this is open
I'm dancing on Rainbows.
Raspberries are not Apples or Oranges

Mallon
Posts: 10
Joined: Wed Apr 28, 2021 8:52 am

Re: Pico as a computer peripheral

Sat May 01, 2021 3:26 pm

Have you ever tried using this on two Picos at once?

Its working across the two pico's well for me however the /dev/tty* id for the devices changes all the time which makes it difficult to maintain consistency.

I did go down the line of trying to give each pico a persistent device name via udev rules on my host pi, however the pico 's both have the same idVendor, idProduct and Serial values. I wonder is there any way to solve this using the picod library, its been a real nuisance.

User avatar
joan
Posts: 15623
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Pico as a computer peripheral

Sat May 01, 2021 4:02 pm

All I did was confirm that you could open two Picos within one script.

It would be outside of the scope of the module to make sense of the dynamic naming of ports. I doubt any Python modules do that.

I will amend the module so that it can return the Pico's unique Id.


4.2.13.3.1. pico_get_unique_board_id

void pico_get_unique_board_id (pico_unique_board_id_t *id_out)

Get unique ID.

Get the unique 64-bit device identifier which was retrieved from the external NOR flash device at boot.

On PICO_NO_FLASH builds the unique identifier is set to all 0xEE.

Parameters
• id_out a pointer to a pico_unique_board_id_t struct, to which the identifier will be written

Mallon
Posts: 10
Joined: Wed Apr 28, 2021 8:52 am

Re: Pico as a computer peripheral

Sat May 01, 2021 4:23 pm

joan wrote:
Sat May 01, 2021 4:02 pm
All I did was confirm that you could open two Picos within one script.

It would be outside of the scope of the module to make sense of the dynamic naming of ports. I doubt any Python modules do that.

I will amend the module so that it can return the Pico's unique Id.


4.2.13.3.1. pico_get_unique_board_id

void pico_get_unique_board_id (pico_unique_board_id_t *id_out)

Get unique ID.

Get the unique 64-bit device identifier which was retrieved from the external NOR flash device at boot.

On PICO_NO_FLASH builds the unique identifier is set to all 0xEE.

Parameters
• id_out a pointer to a pico_unique_board_id_t struct, to which the identifier will be written
Great job again, will make life a lot simpler.

User avatar
joan
Posts: 15623
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Pico as a computer peripheral

Sat May 01, 2021 7:57 pm

I have added a uid function in v0.0.6.0.

http://abyz.me.uk/picod/py_picod.html#uid

Mallon
Posts: 10
Joined: Wed Apr 28, 2021 8:52 am

Re: Pico as a computer peripheral

Tue May 04, 2021 7:56 pm

I've had a play around with the UID function to find the uid of all attached picos and the port it is connected to. Then I can attach it to the same static pico name variable each time.

Code: Select all

def get_ID_64Bit(rng):
    ID_64Bit = []
    ports = []
    i = 0

    try: 
        while i < rng:
            status, uid = picod.pico(device='/dev/pico'+ str(i)).uid()
            ID_64Bit.append(uid)
            ports.append(i)
            i += 1
    except (FileNotFoundError, IOError) as e:
            i += 1
    
    finally:
        print("64Bit Unique ID of each Pico device collected")

    return ID_64Bit, ports
    
    
That will get the uid and associated /dev/pico* port for you.
UID feature is great.

I have been gettin this error every once in a while. I cant consistenly replicate it, but am I timing out the seral connection?
xception in thread Thread-3:
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/serial/serialposix.py", line 501, in read
'device reports readiness to read but returned no data '
serial.serialutil.SerialException: device reports readiness to read but returned no data (device disconnected or multiple access on port?)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/usr/lib/python3.7/threading.py", line 917, in _bootstrap_inner
self.run()
File "/usr/local/lib/python3.7/dist-packages/picod-0.0.6.0-py3.7.egg/picod.py", line 515, in run
d = self._pico_serial_read(5000)
File "/usr/local/lib/python3.7/dist-packages/picod-0.0.6.0-py3.7.egg/picod.py", line 2744, in _serial_read
return bytearray(_pico_serial.read(count))
File "/usr/lib/python3/dist-packages/serial/serialposix.py", line 509, in read
raise SerialException('read failed: {}'.format(e))
serial.serialutil.SerialException: read failed: device reports readiness to read but returned no data (device disconnected or multiple access on port?)
Any idea how to prevent this? I do notice the GPIO_read() function is taking varying times to complete.
Last edited by Mallon on Thu May 06, 2021 6:23 pm, edited 1 time in total.

User avatar
joan
Posts: 15623
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Pico as a computer peripheral

Tue May 04, 2021 8:05 pm

I assumed the uid was constant. I thought is was the serial number of the flash chip.

I associate the error you are seeing with having another process trying to read the same /dev/ttyACM device.

Mallon
Posts: 10
Joined: Wed Apr 28, 2021 8:52 am

Re: Pico as a computer peripheral

Thu May 06, 2021 6:23 pm

I apologise, on review, the uid is permanent. I removed it from the original reply to avoid confusion for any future users.

Still trying to locate the source of the device readiness problem. I'm not calling any ports at same time, I will have to delve deeper!

User avatar
joan
Posts: 15623
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Pico as a computer peripheral

Thu May 06, 2021 7:32 pm

Mallon wrote:
Thu May 06, 2021 6:23 pm
I apologise, on review, the uid is permanent. I removed it from the original reply to avoid confusion for any future users.

Still trying to locate the source of the device readiness problem. I'm not calling any ports at same time, I will have to delve deeper!
Thanks. If you find you can easily reproduce the device readiness problem that would be a great help. Hopefully it will be fixable.

Mallon
Posts: 10
Joined: Wed Apr 28, 2021 8:52 am

Re: Pico as a computer peripheral

Fri May 07, 2021 4:44 pm

joan wrote:
Thu May 06, 2021 7:32 pm
Mallon wrote:
Thu May 06, 2021 6:23 pm
I apologise, on review, the uid is permanent. I removed it from the original reply to avoid confusion for any future users.

Still trying to locate the source of the device readiness problem. I'm not calling any ports at same time, I will have to delve deeper!
Thanks. If you find you can easily reproduce the device readiness problem that would be a great help. Hopefully it will be fixable.

I can reproduce the problem, but it could just be my fault. I have been running a loop which does a GPIO_read() and stores it into a list, every 5 seconds. So potentially its looping too fast and causing it to try check the GPIOs whilst they are still being accessed by the previous loop.

User avatar
joan
Posts: 15623
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Pico as a computer peripheral

Fri May 07, 2021 4:47 pm

Mallon wrote:
Fri May 07, 2021 4:44 pm
...
I can reproduce the problem, but it could just be my fault. I have been running a loop which does a GPIO_read() and stores it into a list, every 5 seconds. So potentially its looping too fast and causing it to try check the GPIOs whilst they are still being accessed by the previous loop.
That doesn't sound like it should cause an error.

Could you post a script for me to run and test.

Return to “General”