Problems with machine.disable_irq()

RP2040 based microcontroller boards running MicroPython.
Target audience: MicroPython users with an RP2040 boards.
This does not include conventional Linux-based Raspberry Pi boards.
Post Reply
srogers
Posts: 11
Joined: Sun Feb 06, 2022 12:25 am

Problems with machine.disable_irq()

Post by srogers » Sun Feb 06, 2022 5:34 am

I have a problem with disabling irq on my Pico Pi with the latest version of Micropython 1.18

When I tried to use machine.disable_irq() in some code I was developing, it stopped before loading and said this:

AttributeError: 'module' object has no attribute 'disable_irq'

I then tried the code in REPL, which worked. By that, I mean that it did not give an error. It did hang the machine, so I took it as the code was executed, but wasn't something you should try in REPL.

So I made the smallest program to test it as follows:

import machine

foo = machine.disable_irq()
machine.enable_irq(foo)

This very small program worked, at least it returned to Thonny without any messages in the REPL window.
So I am trying to figure out why sometimes, it seems, the program has lost track of whether it has the
disable_irq() method. By the way, when the code failed to be accepted, I was calling disable_irq() from
a foreground program in an effort to keep an interrupt process from touching the common data what I was
wanting to make a copy of.

Any ideas?

Here is proof that 1.18 thinks it has disable_irq():

MicroPython v1.18 on 2022-01-17; Raspberry Pi Pico with RP2040

Type "help()" for more information.

import machine
help(machine)
object <module 'umachine'> is of type module
...
__name__ -- umachine
unique_id -- <function>
soft_reset -- <function>
reset -- <function>
reset_cause -- <function>
bootloader -- <function>
freq -- <function>
idle -- <function>
lightsleep -- <function>
deepsleep -- <function>
disable_irq -- <function>
enable_irq -- <function>
...

fivdi
Posts: 16
Joined: Thu Feb 03, 2022 10:28 pm
Contact:

Re: Problems with machine.disable_irq()

Post by fivdi » Sun Feb 06, 2022 10:19 am

srogers wrote:
Sun Feb 06, 2022 5:34 am
When I tried to use machine.disable_irq() in some code I was developing, it stopped before loading and said this:

AttributeError: 'module' object has no attribute 'disable_irq'
It is difficult to say why this error is occurring without seeing sample code that can be used to reproduce the error. Here are some examples of how it could have been produced:

Code: Select all

>>> import micropython
>>> foo = micropython.disable_irq()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'disable_irq'

Code: Select all

>>> import machine
>>> machine.disable_irq = 1
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'disable_irq'
If you can provide a short sample program that can be used to reproduce the error someone may be able to see what the problem is.
srogers wrote:
Sun Feb 06, 2022 5:34 am
I then tried the code in REPL, which worked. By that, I mean that it did not give an error. It did hang the machine, so I took it as the code was executed, but wasn't something you should try in REPL.
Calling machine.disable_irq() from the REPL is tricky because if interrupts are disabled for too long terrible things are likely to happen. For example, I would expect TinyUSB, which is used by the REPL, to stop functioning when interrupts are disabled.

This should work from the REPL as interrupts are only disabled for a short period of time:

Code: Select all

>>> import machine
>>> j = 0
>>> foo = machine.disable_irq(); j += 1; machine.enable_irq(foo)
>>> print(j)
1
>>> 
But this will cause the REPL to hang as interrupts are not being enabled quickly enough to allow the REPL to proceed with it's work:

Code: Select all

>>> import machine
>>> j = 0
>>> foo = machine.disable_irq()
>>>
srogers wrote:
Sun Feb 06, 2022 5:34 am
Any ideas?
I would imagine that there is a programming error somewhere in your code. As mentioned above, if you can provide a short sample program that can be used to reproduce the error someone may spot what the problem is.

Post Reply