Page 1 of 1

UART IRQ/Callbacks and Class Example

Posted: Mon Feb 19, 2018 1:58 pm
by donikuy
Hi,

I have managed to send and receive data via UART on the pyboard, but have been unable to do that using interrupts. Please see code below. Documentation indicates that irq is available:
>>> import pyb
>>> from pyb import UART
>>> uart1 = UART(1,baudrate=9600)
>>> uart1.irq(UART.RX_ANY,priority=1,handler=print('Uart callback!'),wake=machE)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'UART' object has no attribute 'irq'

How can this be fixed? Is there amother way or any example of how I can use irq/callbacks to detect uart incoming messages?
Ideally I would like to create a UART class that performs this functionality.

Thank you.

Re: UART IRQ/Callbacks and Class Example

Posted: Tue Feb 20, 2018 6:07 am
by pythoncoder
donikuy wrote:
Mon Feb 19, 2018 1:58 pm
...Documentation indicates that irq is available...
I don't know where you found that. The official docs have no reference to this as far as I can see.

There are various ways to use the UART. You can poll any() to see if data is available. If you don't mind methods which block until data is received you can use the character or line reading methods. The most elegant way is to use uasyncio: UARTS are stream devices and can be run as asynchronous tasks. The following code fragment performs concurrent I/O on a single UART and can be tested with a loopback between pins X1 and X2:

Code: Select all

import uasyncio as asyncio
from pyb import UART
uart = UART(4, 9600)

async def sender():
    swriter = asyncio.StreamWriter(uart, {})
    while True:
        await swriter.awrite('Hello uart\n')
        await asyncio.sleep(2)

async def receiver():
    sreader = asyncio.StreamReader(uart)
    while True:
        res = await sreader.readline()
        print('Recieved', res)

loop = asyncio.get_event_loop()
loop.create_task(sender())
loop.create_task(receiver())
loop.run_forever()
The point here is that you can have other tasks running concurrently while the receiver task pauses pending input.

Re: UART IRQ/Callbacks and Class Example

Posted: Tue Feb 20, 2018 4:05 pm
by donikuy
Thank you for your help.
I think i will need to add the uasyncio module to my python project and load, build the project using cmake and then load it onto the pybv1.1.

At the moment I am having trouble with my cmake. Would you be able to send me the new dfu file the includes the uasyncio module please?

Re: UART IRQ/Callbacks and Class Example

Posted: Tue Feb 20, 2018 4:47 pm
by pythoncoder
I suggest you look at the tutorial in here which describes the installation of uasyncio. There is no need for a special build - you can simply copy files across.

Re: UART IRQ/Callbacks and Class Example

Posted: Wed Feb 21, 2018 1:13 pm
by donikuy
Thank you. I have copied the files over and i am now trying to run this script:
import uasyncio as asyncio
loop = asyncio.get_event_loop()
async def bar():
count = 0
while True:
count += 1
print(count)
await asyncio.sleep(1) # Pause 1s

loop.create_task(bar()) # Schedule ASAP
loop.run_forever()


I get the below error:
AttributeError: 'utimeq' object has no attribute 'peektime'

Would you be able to tell me what the problem is?

Re: UART IRQ/Callbacks and Class Example

Posted: Thu Feb 22, 2018 8:51 am
by pythoncoder
I think you have an out of date firmware version. Please upgrade.

Re: UART IRQ/Callbacks and Class Example

Posted: Wed Jan 10, 2024 9:36 pm
by Alexel
pythoncoder wrote:
Tue Feb 20, 2018 6:07 am
donikuy wrote:
Mon Feb 19, 2018 1:58 pm
...Documentation indicates that irq is available...
I don't know where you found that. The official docs have no reference to this as far as I can see.

There are various ways to use the UART. You can poll any() to see if data is available. If you don't mind methods which block until data is received you can use the character or line reading methods. The most elegant way is to use uasyncio: UARTS are stream devices and can be run as asynchronous tasks. The following code fragment performs concurrent I/O on a single UART and can be tested with a loopback between pins X1 and X2:

Code: Select all

import uasyncio as asyncio
from pyb import UART
uart = UART(4, 9600)

async def sender():
    swriter = asyncio.StreamWriter(uart, {})
    while True:
        await swriter.awrite('Hello uart\n')
        await asyncio.sleep(2)

async def receiver():
    sreader = asyncio.StreamReader(uart)
    while True:
        res = await sreader.readline()
        print('Recieved', res)

loop = asyncio.get_event_loop()
loop.create_task(sender())
loop.create_task(receiver())
loop.run_forever()
The point here is that you can have other tasks running concurrently while the receiver task pauses pending input.

for some reason this code is not working for me. nothing happens.
esp32 s2, v1.22.1