UART IRQ issue

Discussion and questions about boards that can run MicroPython but don't have a dedicated forum.
Target audience: Everyone interested in running MicroPython on other hardware.
Post Reply
arvin
Posts: 1
Joined: Tue May 23, 2017 9:39 am

UART IRQ issue

Post by arvin » Tue May 23, 2017 9:47 am

It noted: AttributeError: 'UART' object has no attribute 'irq' after I typed "u2.irq(trigger = UART.RX_ANY, priority = 1, handler = irq_fun, wake=machine.IDLE)" in REPL。 I guess the current firmware on my board do not provide UART.irq funciton, I need to compile a new firmware, right ?

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

Re: UART IRQ issue

Post by pythoncoder » Wed May 24, 2017 6:38 am

What platform are you using? I'm familiar with the Pyboard and the ESP8266 and I can't say I've come across a UART.irq method in the docs.

There are various ways of achieving asynchronous programming with UARTs. You might like to look at uasyncio, select and poll.
Peter Hinch
Index to my micropython libraries.

User avatar
Dugite
Posts: 21
Joined: Thu Jan 18, 2018 1:29 pm

Re: UART IRQ issue

Post by Dugite » Thu Jan 18, 2018 2:03 pm

I noticed on the following MicroPython help page that it refers to an interrupt for the UART for the Wipy board (UART.irq(trigger, priority=1, handler=None, wake=machine.IDLE)):

http://docs.micropython.org/en/latest/w ... .UART.html

But for the Pyboard there is no such interrupt as shown below:

http://docs.micropython.org/en/latest/p ... .UART.html

I have only recently started using MicroPython (fantastic product by the way) and I would like to know why the Pyboard does not have the same interrupt as the Wipy? I would expect that having a receive interrupt on the UART is an important feature?

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

Re: UART IRQ issue

Post by pythoncoder » Fri Jan 19, 2018 9:58 am

The Pyboard supports the arguably more powerful IORead mechanism. The example below performs concurrent UART send and receive (link Pyboard X1 and X2 if you want to try it).

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()
Peter Hinch
Index to my micropython libraries.

User avatar
Dugite
Posts: 21
Joined: Thu Jan 18, 2018 1:29 pm

Re: UART IRQ issue

Post by Dugite » Sat Jan 20, 2018 2:28 pm

Thank you for the prompt reply.

I have been looking at how to get the 'micropthon-uasyncio' module installed, but this is all new to me, so I will need to read up about it.

Is there any way to have just a UART receive interrupt using the standard modules?

The device is also battery powered, so I need it to be in sleep mode if it is not doing anything. It appears the 'micropthon-uasyncio' module may use sleep, but I have not confirmed this yet.

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

Re: UART IRQ issue

Post by pythoncoder » Tue Jan 23, 2018 10:43 am

Re uasyncio you might like to look at the tutorial in this repo. But uasyncio doesn't use the low power modes.

If you put the Pyboard into "sleep" mode I don't think an incoming character on the UART will wake it. It might be possible to link the rxdata pin to another input pin and have a pin interrupt on the second pin wake the chip on a falling edge. But I'm guessing here - I've not tried such tricks ;)
Peter Hinch
Index to my micropython libraries.

User avatar
Dugite
Posts: 21
Joined: Thu Jan 18, 2018 1:29 pm

Re: UART IRQ issue

Post by Dugite » Wed Mar 28, 2018 1:28 pm

I did get the uasyncio module working, but it is just overkill for what I need (which is just reading UART Rx bytes).

It seems the only option I have now to get UART interrupts happening on the Pyboard is to attach and external interrupt pin to the UART Rx pin, but this will result in far more interrupts than necessary. A shame it has been missed on the Pyboard as it is such a fantastic product.

Post Reply