Page 1 of 1

UART IRQ

Posted: Wed Mar 02, 2016 9:06 pm
by danielm
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'

Re: UART IRQ

Posted: Wed Mar 02, 2016 9:33 pm
by danielm
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

Re: UART IRQ

Posted: Thu Mar 03, 2016 6:30 am
by danicampora
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

Re: UART IRQ

Posted: Thu Mar 03, 2016 9:09 am
by danielm
Daniel, thanks, this seems to work ;-)

Re: UART IRQ

Posted: Tue Feb 20, 2018 9:15 am
by donikuy
How can I get this to work on a pyboard? I cannot find any way of using interrrupts on uart.

Re: UART IRQ

Posted: Tue Feb 20, 2018 12:15 pm
by pythoncoder
You can't. See my reply here on ways to access the UARTs.

Re: UART IRQ

Posted: Wed Jan 10, 2024 9:40 pm
by Alexel
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.