Release (machine.)Pin resources?

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
Jibun no kage
Posts: 144
Joined: Mon Jul 25, 2022 9:45 pm

Release (machine.)Pin resources?

Post by Jibun no kage » Mon Aug 08, 2022 4:57 pm

I happen to be working with a Pico image. Was looking for a way to release or clean up Pin allocation? For example in Pi OS, GPIO.cleanup(). But for (machine.)Pin, no method?

>>> from machine import Pin
>>> dir(Pin)
['__class__', '__name__', 'value', '__bases__', '__dict__', 'ALT', 'IN', 'IRQ_FALLING', 'IRQ_RISING', 'OPEN_DRAIN', 'OUT', 'PULL_DOWN', 'PULL_UP', 'high', 'init', 'irq', 'low', 'off', 'on', 'toggle']

Was expecting, maybe, 'deinit'?

User avatar
scruss
Posts: 360
Joined: Sat Aug 12, 2017 2:27 pm
Location: Toronto, Canada
Contact:

Re: Release (machine.)Pin resources?

Post by scruss » Mon Aug 08, 2022 5:17 pm

With MicroPython, that's left up to you. Since pins aren't guaranteed to be initialized in a known state on powerup, it might be hard to know what state to leave them in.

Jibun no kage
Posts: 144
Joined: Mon Jul 25, 2022 9:45 pm

Re: Release (machine.)Pin resources?

Post by Jibun no kage » Mon Aug 08, 2022 11:10 pm

Yeah, I guess a reasonable compromise is to always leave a given pin something consistent, maybe Pin.OUT, 0. But I was also thinking of releasing the resources of the actual Pin object, but that should or could be handled via object variable set to None, then call gc.collect(), right?

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

Re: Release (machine.)Pin resources?

Post by jimmo » Thu Aug 11, 2022 1:22 am

On most ports (including rp2) the pins are statically allocated in RAM and do not live in the heap, there's nothing to free. (See the table at the top of rp2/machine_pin.c)

Jibun no kage
Posts: 144
Joined: Mon Jul 25, 2022 9:45 pm

Re: Release (machine.)Pin resources?

Post by Jibun no kage » Thu Aug 11, 2022 1:24 am

Ah, good to know, if a bit of bummer.

Post Reply