# Minimal simple Interrupt handling with UIO device in Pynq

**URL:** <https://discuss.pynq.io/t/minimal-simple-interrupt-handling-with-uio-device-in-pynq/848>\
**Category:** Support\
**Created:** [January 13, 2020, 5:14pm UTC](https://discuss.pynq.io/t/minimal-simple-interrupt-handling-with-uio-device-in-pynq/848 "2020-01-13T17:14:11Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![embeder](https://avatars.discourse-cdn.com/v4/letter/e/f04885/32.png) [@embeder](https://discuss.pynq.io/u/embeder)\
**Post date:** [January 13, 2020, 5:14pm UTC](https://discuss.pynq.io/t/minimal-simple-interrupt-handling-with-uio-device-in-pynq/848/1 "2020-01-13T17:14:11Z")

</div>

Hi,  
I want to test and build a simple Interrupt example for a custom board, connecting from an external signal using only UIO framework.

1. I have connected the external pin with IRQ\_F2P[0:0] interrupt line on Zynq IP

2. I have added device tree fragment in system\_user.dtsi

**&amba\_pl {**

```
#address-cells = <1>;
#size-cells = <1>;
compatible = "simple-bus";
ranges ;

uio_0: uio_int0@0{
            compatible="generic-uio";
            status="okay";
            interrupt-controller;
            interrupt-parent=<&intc>;
            interrupts=<0x0 0x1d 0x4>;
};

```

1. once the system boots up the uio device is in place as  
**/dev/uio0**  
and also in cat /proc/interrupt  
**49: 1 0 GIC-0 63 Level uio0**

Previously i have used and serviced the interrupt using c applications.  
Now in my Board Pynq framework is working and tested.  
What will be the minimal python code to test the interrupt using the only uio.py?

---

<div class="post-metadata">

**Author:** ![PeterOgden](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.pynq.io/peterogden/32/55_2.png) [@PeterOgden](https://discuss.pynq.io/u/PeterOgden)\
**Post date:** [January 16, 2020, 11:58am UTC](https://discuss.pynq.io/t/minimal-simple-interrupt-handling-with-uio-device-in-pynq/848/2 "2020-01-16T11:58:20Z")

</div>

The `UioController` class bridges the asyncio and UIO worlds and works by waiting on the UIO file and setting asyncio events when an interrupt is received.

```python
controller = UioController("/dev/uio0")
event = asyncio.Event()
async def interrupt_test():
   while True:
       event.clear()
       controller.add_event(event, 0) # Number is a dummy variable
       await event
       print("Interrupt Triggered")

loop = asyncio.get_event_loop()
loop.run_until_complete(interrupt_test())

```

This should print the message each time that the interrupt is triggered. You can create other asyncio coroutines to generate the interrupts if you want to test everything in process. If you’re using a GPIO controller to driver the interrupt line then something along the lines of:

```auto
async def drive_interrupts():
    while True:
         gpio.write(0, 0x1)
         gpio.write(0, 0x0)
         await asyncio.sleep(1)

driver_task = asyncio.ensure_future(drive_interrupts())

```

In a full program, you should ensure that the interrupt line is cleared before calling `add_event` otherwise you might end up with interrupts being duplicated.

Hope this makes some kind of sense - the underlying asyncio code can be fairly messy which is why we tried to abstract as much as possible of this away with the Interrupt class. That assumes a tree structure of AXI interrupts controllers connected to interrupt line 0 and a UIO device with the name “fabric”.

Peter

---

<div class="post-metadata">

**Author:** ![embeder](https://avatars.discourse-cdn.com/v4/letter/e/f04885/32.png) [@embeder](https://discuss.pynq.io/u/embeder)\
**Post date:** [January 20, 2020, 12:30pm UTC](https://discuss.pynq.io/t/minimal-simple-interrupt-handling-with-uio-device-in-pynq/848/3 "2020-01-20T12:30:54Z")

</div>

Hi,  
Thanks for the detailed solution it worked nicely, for multiple interrupts, I followed external axi-int-controller in pl, just a side question may be its more related to python.  
In pynq interrupt service routine is using asyncio but i dont now how to run it in the background? Is there any standard way? Do you have any reference implementation for monitoring interrupt in Pynq Framework in backgroun?

---

<div class="post-metadata">

**Author:** ![PeterOgden](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.pynq.io/peterogden/32/55_2.png) [@PeterOgden](https://discuss.pynq.io/u/PeterOgden)\
**Post date:** [January 20, 2020, 12:43pm UTC](https://discuss.pynq.io/t/minimal-simple-interrupt-handling-with-uio-device-in-pynq/848/4 "2020-01-20T12:43:54Z")

</div>

As long as you are careful with the multi-threading aspects you can start a new thread to run the asyncio loop - cell 2 in this [this notebook](https://github.com/PeterOgden/ZCU104_VideoDemo/blob/master/notebooks/Mixer.ipynb) shows one way to go about doing this. You can then add tasks to run in the background to the loop using `asyncio.run_coroutine_threadsafe` as shown in the following cell. The `threadsafe` is important to avoid corrupting the event loop when adding new tasks.

Peter

---

<div class="post-metadata">

**Author:** ![atopet](https://avatars.discourse-cdn.com/v4/letter/a/85f322/32.png) [@atopet](https://discuss.pynq.io/u/atopet)\
**Post date:** [January 26, 2022, 6:18pm UTC](https://discuss.pynq.io/t/minimal-simple-interrupt-handling-with-uio-device-in-pynq/848/6 "2022-01-26T18:18:09Z")

</div>

Hi,

I want to read my BRAM on Linux of my PYNQ board, can you give any guide on this, will uio serve this purpose? if yes, can you give any reference material or tutorial for using UIO

---

<div class="post-metadata">

**Author:** ![Mikael](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.pynq.io/mikael/32/2654_2.png) [@Mikael](https://discuss.pynq.io/u/Mikael)\
**Post date:** [January 27, 2022, 9:04am UTC](https://discuss.pynq.io/t/minimal-simple-interrupt-handling-with-uio-device-in-pynq/848/7 "2022-01-27T09:04:44Z")

</div>

Hello,  
It is best to use mmio for this. It allows you to read and write registers on memory-mapped buses such as AXI4.

With Pynq, you can do this by loading your bitstream, then find the attribute corresponding to your IP using. The `read` and `write` methods of your IP objects are shortcuts for the mmio api: `overlay.axi_xxxx.read` and `overlay.axi_xxxx.write`.

With any language, you can the mmio interface using mmap on /dev/mem and setting the propers offsets corresponding to your IP’s address on the memory bus.

---

<div class="post-metadata">

**Author:** ![atopet](https://avatars.discourse-cdn.com/v4/letter/a/85f322/32.png) [@atopet](https://discuss.pynq.io/u/atopet)\
**Post date:** [January 27, 2022, 9:11am UTC](https://discuss.pynq.io/t/minimal-simple-interrupt-handling-with-uio-device-in-pynq/848/8 "2022-01-27T09:11:12Z")

</div>

Hello,

Thank you for your response, However, I think loading my bitstream, then using overlay.axi\_xxxx.mmio.read to read will reset the values on my BRAM and give me a default value on the BRAM.

I aim to read the data stored on my BRAM immediately after an inference, I noticed after the inference, the server time out and I have to reload the bitstream before I can read using the overlay.axi\_xxxx.mmio.read

Thank you for your assistance

---

<div class="post-metadata">

**Author:** ![Mikael](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.pynq.io/mikael/32/2654_2.png) [@Mikael](https://discuss.pynq.io/u/Mikael)\
**Post date:** [January 27, 2022, 9:12am UTC](https://discuss.pynq.io/t/minimal-simple-interrupt-handling-with-uio-device-in-pynq/848/9 "2022-01-27T09:12:11Z")

</div>

Then set the option download=False when you load the bitstream in the Pynq API. It will keep your FPGA untouched.

---

<div class="post-metadata">

**Author:** ![Mikael](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.pynq.io/mikael/32/2654_2.png) [@Mikael](https://discuss.pynq.io/u/Mikael)\
**Post date:** [January 27, 2022, 9:13am UTC](https://discuss.pynq.io/t/minimal-simple-interrupt-handling-with-uio-device-in-pynq/848/10 "2022-01-27T09:13:38Z")

</div>

Here is the relevant [documentation page](https://pynq.readthedocs.io/en/latest/pynq_libraries/overlay.html?highlight=overlay).

---

<div class="post-metadata">

**Author:** ![atopet](https://avatars.discourse-cdn.com/v4/letter/a/85f322/32.png) [@atopet](https://discuss.pynq.io/u/atopet)\
**Post date:** [January 27, 2022, 9:17am UTC](https://discuss.pynq.io/t/minimal-simple-interrupt-handling-with-uio-device-in-pynq/848/11 "2022-01-27T09:17:54Z")

</div>

Thank you, will try and give a feedback.

One more question, my 32b BRAM has a depth of 512, can I read the various data written on each array row?

---

<div class="post-metadata">

**Author:** ![Mikael](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.pynq.io/mikael/32/2654_2.png) [@Mikael](https://discuss.pynq.io/u/Mikael)\
**Post date:** [January 27, 2022, 12:33pm UTC](https://discuss.pynq.io/t/minimal-simple-interrupt-handling-with-uio-device-in-pynq/848/12 "2022-01-27T12:33:20Z")

</div>

Yes, just read at the corresponding address.

---

<div class="post-metadata">

**Author:** ![marioruiz](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.pynq.io/marioruiz/32/2177_2.png) [@marioruiz](https://discuss.pynq.io/u/marioruiz)\
**Post date:** [January 27, 2022, 6:49pm UTC](https://discuss.pynq.io/t/minimal-simple-interrupt-handling-with-uio-device-in-pynq/848/14 "2022-01-27T18:49:39Z")

</div>

You need to reset the parser after the Overlay object is created as shown in here

> [@Accessing an overlay from multiple python interpreters](https://discuss.pynq.io/t/accessing-an-overlay-from-multiple-python-interpreters/3438/2):
>
> Hi, What pynq version are you using? In 2.7 I was able to do this using this piece of code import pynq ol = pynq.Overlay(\<your\_overlay\>.bit, download=False) pynq.Device.active\_device.reset(ol.parser, ol.timestamp, ol.bitfile\_name) Mario

Mario

---

<div class="post-metadata">

**Author:** ![atopet](https://avatars.discourse-cdn.com/v4/letter/a/85f322/32.png) [@atopet](https://discuss.pynq.io/u/atopet)\
**Post date:** [February 6, 2022, 10:35am UTC](https://discuss.pynq.io/t/minimal-simple-interrupt-handling-with-uio-device-in-pynq/848/16 "2022-02-06T10:35:49Z")

</div>

Thank you Mario for taking this back to the original thread
