UART IRQ

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
danielm
Posts: 167
Joined: Mon Oct 05, 2015 12:24 pm

UART IRQ

Post by danielm » Wed Mar 02, 2016 9:06 pm

I am trying to setup second UART with interrupt for Nextion HMI display from REPL. What am I doing wrong?
Platform is WiPy port on custom CC3200 board.

Code: Select all

>>> uart                   # this object is created in boot.py for terminal duplication
UART(0, baudrate=115200, bits=8, parity=None, stop=1)
>>> uart2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name not defined
>>> uart2 = machine.UART(1, baudrate=9600, bits=8, parity=None, stop=1, pins=('GP3', 'GP4'))
>>> uart2
UART(1, baudrate=9600, bits=8, parity=None, stop=1)
>>> def irq_fun():
...     received = uart2.read()
...     print("Received: ",received)
...
...
...
>>> uart2IRQ = uart2.irq(trigger = UART.RX_ANY, priority = 1, handler = irq_fun, wake=machine.IDLE)
>>>
>>> Uncaught exception in callback handler           
TypeError:

>>>
TypeError exception shows after touching the button on the display.

When I try to call irq_fun() directly from REPL it works - these bytes are sent from Nextion HMI display by touch of a button once:

Code: Select all

>>> irq_fun()
Received:  b'e\x00\x05\x01\xff\xff\xff'

danielm
Posts: 167
Joined: Mon Oct 05, 2015 12:24 pm

Re: UART IRQ

Post by danielm » Wed Mar 02, 2016 9:33 pm

It seems that interrupt function is called with UART class parameter, on which I can call function any() ( it does not work with read() ).

Code: Select all

>>> def irq_fun(obj):
...     print("Type: ", type(obj))
...     print(obj.any())
...
...
...
>>> uart2IRQ = uart2.irq(trigger = UART.RX_ANY, priority = 1, handler = irq_fun, wake=machine.IDLE)
>>> Type:  <class 'UART'>
105
Type:  <class 'UART'>
112
Type:  <class 'UART'>
119
Type:  <class 'UART'>
126
Type:  <class 'UART'>
133
Type:  <class 'UART'>
140

User avatar
danicampora
Posts: 342
Joined: Tue Sep 30, 2014 7:20 am
Contact:

Re: UART IRQ

Post by danicampora » Thu Mar 03, 2016 6:30 am

Hello,

Please use the .readinto() method instead, since .read() needs to allocate memory to return the array, and memory allocations are not possible within interrupt context.

Cheers,
Daniel

danielm
Posts: 167
Joined: Mon Oct 05, 2015 12:24 pm

Re: UART IRQ

Post by danielm » Thu Mar 03, 2016 9:09 am

Daniel, thanks, this seems to work ;-)

donikuy
Posts: 14
Joined: Fri Feb 09, 2018 10:43 am

Re: UART IRQ

Post by donikuy » Tue Feb 20, 2018 9:15 am

How can I get this to work on a pyboard? I cannot find any way of using interrrupts on uart.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: UART IRQ

Post by pythoncoder » Tue Feb 20, 2018 12:15 pm

You can't. See my reply here on ways to access the UARTs.
Peter Hinch
Index to my micropython libraries.

Alexel
Posts: 3
Joined: Sun Dec 15, 2019 8:46 pm

Re: UART IRQ

Post by Alexel » Wed Jan 10, 2024 9:40 pm

danielm wrote:
Wed Mar 02, 2016 9:33 pm
It seems that interrupt function is called with UART class parameter, on which I can call function any() ( it does not work with read() ).

Code: Select all

>>> def irq_fun(obj):
...     print("Type: ", type(obj))
...     print(obj.any())
...
...
...
>>> uart2IRQ = uart2.irq(trigger = UART.RX_ANY, priority = 1, handler = irq_fun, wake=machine.IDLE)
>>> Type:  <class 'UART'>
105
Type:  <class 'UART'>
112
Type:  <class 'UART'>
119
Type:  <class 'UART'>
126
Type:  <class 'UART'>
133
Type:  <class 'UART'>
140
i made some weird one that is working. after calling ISR callback set sleep for 5.1ms (depends on your baudrate,, this one is for 9600), then read the rx with uart.any() > 0, uart.read()

this halts the mcu for 5.1ms, but working. im sure that there is better solution.

Post Reply