UART IRQ/Callbacks and Class Example

The official pyboard running MicroPython.
This is the reference design and main target board for MicroPython.
You can buy one at the store.
Target audience: Users with a pyboard.
Post Reply
donikuy
Posts: 14
Joined: Fri Feb 09, 2018 10:43 am

UART IRQ/Callbacks and Class Example

Post by donikuy » Mon Feb 19, 2018 1:58 pm

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.

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

Re: UART IRQ/Callbacks and Class Example

Post by pythoncoder » 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.
Peter Hinch

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

Re: UART IRQ/Callbacks and Class Example

Post by donikuy » Tue Feb 20, 2018 4:05 pm

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?

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

Re: UART IRQ/Callbacks and Class Example

Post by pythoncoder » Tue Feb 20, 2018 4:47 pm

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.
Peter Hinch

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

Re: UART IRQ/Callbacks and Class Example

Post by donikuy » Wed Feb 21, 2018 1:13 pm

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?

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

Re: UART IRQ/Callbacks and Class Example

Post by pythoncoder » Thu Feb 22, 2018 8:51 am

I think you have an out of date firmware version. Please upgrade.
Peter Hinch

Post Reply