libpynq (release 5EWC0-2023 version 0.1.0 of 2023-08-14 14:01)
Loading...
Searching...
No Matches
interrupt.c
Go to the documentation of this file.
1/*
2Copyright (c) 2023 Eindhoven University of Technology
3
4Permission is hereby granted, free of charge, to any person obtaining a copy
5of this software and associated documentation files (the "Software"), to deal
6in the Software without restriction, including without limitation the rights
7to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8copies of the Software, and to permit persons to whom the Software is
9furnished to do so, subject to the following conditions:
10
11The above copyright notice and this permission notice shall be included in all
12copies or substantial portions of the Software.
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20SOFTWARE.
21*/
23#include <fcntl.h>
24#include <gpio.h>
25#include <log.h>
26#include <platform.h>
27#include <stdbool.h>
28#include <stdint.h>
29#include <stdio.h>
30#include <stdlib.h>
31#include <unistd.h>
32#include <util.h>
33
34#define DOMAIN "Interrupt"
35
36extern uint32_t *gpio;
37extern uint32_t *intc0;
38
39static bool started = false;
40
42 if (started == false) {
43 pynq_error("Interrupts are not initialized. Use gpio_interupt_init() to "
44 "initialize these. \n");
45 }
46}
47
49 int fd = open("/dev/uio1", O_RDWR, O_CLOEXEC);
50 if (fd < 0) {
51 pynq_error("failed to open interrupts\n");
52 }
53 int32_t m = 1;
54 write(fd, &m, 4);
55 started = true;
56 return fd;
57}
58
61 int pin_bank = pin % 32;
62 int bank = pin < 32 ? 0 : 1;
63 if (bank == 0) {
64 printf("interrupt set 0: %08X %08X\r\n", pin, pin_bank);
65 intc0[0] |= (1 << pin_bank);
66 } else {
67 printf("interrupt set 1: %08X %08X\r\n", pin, pin_bank);
68 intc0[1] |= (1 << (pin_bank));
69 }
70}
71
74 intc0[0] &= ~(1 << pin);
75}
76
79 intc0[0] = 0;
80 intc0[1] = 0;
81}
82
83uint64_t gpio_get_interrupt(void) {
85 uint64_t retv = intc0[3];
86 retv <<= 32;
87 retv |= intc0[2];
88 return retv;
89}
90
93 // if (gpio[0x120 / 4] > 0) {
94 // gpio[0x0120 / 4] = 1;
95 // }
96 // intc[0xc / 4] = 1;
97 intc0[2] = 1;
98}
99
101 // TODO check if interrupts are initialized when using other interrupt
102 // functions
103 uint64_t retv = intc0[1];
104 retv <<= 32;
105 retv |= intc0[0];
106 if (pin < 64) {
107 uint64_t bitMask = 1ULL << pin;
108 if (!(bitMask & retv)) {
109 pynq_error("Pin %d is not enabled. Enable by using "
110 "gpio_enable_interrupt(pin). \n",
111 pin);
112 }
113 } else {
114 if (retv == 0) {
115 pynq_error("No interrupts enabled. Enable by using "
116 "gpio_enable_interrupt(pin). \n");
117 }
118 }
119}
120
123 // printf("11c: %08X\r\n", gpio[0x11c / 4]);
124 // printf("128: %08X\r\n", gpio[0x128 / 4]);
125 // printf("120: %08X\r\n", gpio[0x120 / 4]);
126 printf("interrupt 0: %08X %08X\r\n", intc0[0], intc0[2]);
127 printf("interrupt 1: %08X %08X\r\n", intc0[1], intc0[3]);
128}
129
130void findSetBitPositions(uint64_t word, uint8_t *positions) {
131 int index = 0;
132 int count = 0;
133 while (word) {
134 if (word & 1) {
135 positions[count++] = index;
136 }
137 word >>= 1;
138 index++;
139 }
140}
141
145 if (pin > 63) {
146 while (1) {
147 uint64_t interrupt = gpio_get_interrupt();
148 if (interrupt != 0) {
149 break;
150 }
151 }
152 } else {
153 while (1) {
154 uint64_t interrupt = gpio_get_interrupt();
155 uint64_t bitMask = 1ULL << pin;
156 if (bitMask & interrupt) {
157 break;
158 }
159 sleep_msec(100);
160 }
161 }
162}
163
164uint8_t *gpio_get_interrupt_pins(uint8_t *positions) {
166 verify_interrupt_request(64); // check if any interupt pin is enabled
167 // uint8_t *positions = (uint8_t *)malloc(64 * sizeof(uint8_t));
168 uint64_t pin = (uint64_t)((uint64_t)(intc0[3]) << 32 | intc0[2]);
169 findSetBitPositions(pin, positions);
170 // printf("Interrupted pin(s): ");
171 bool empty = true;
172 for (int i = 0; i < 64; i++) {
173 if (positions[i] != 0) {
174 empty = false;
175 // printf("%d ", positions[i]);
176 break;
177 }
178 }
179 if (empty) {
180 printf("WARNING: gpio_get_interrupt_pins: No pins interrupted. ");
181 }
182 printf("\n");
183 return (positions);
184}
void gpio_enable_interrupt(const gpio_t pin)
enables a specific pin to raise interrupts.
Definition interrupt.c:59
uint64_t gpio_get_interrupt(void)
Definition interrupt.c:83
uint8_t * gpio_get_interrupt_pins()
void gpio_disable_interrupt(const gpio_t pin)
Disables interrupts from occuring on the specific pin. Hereafter, the pin will not trigger an interru...
Definition interrupt.c:72
void gpio_print_interrupt(void)
prints the current interrupt word
Definition interrupt.c:121
uint8_t gpio_t
Definition gpio.h:99
void gpio_disable_all_interrupts(void)
Disables all interrupts from being raised.
Definition interrupt.c:77
void gpio_ack_interrupt(void)
acknowledges the raised interrupts and resets the interrupt word. Allows new interrupts to occur on t...
Definition interrupt.c:91
int gpio_interrupt_init(void)
Enables interrupts to be set and read.
Definition interrupt.c:48
void gpio_wait_for_interrupt(const gpio_t pin)
Waits untill an interrupt occurs on the specified pin or if the value of pin is larger than 63,...
Definition interrupt.c:142
void verify_interrupt_request(const gpio_t pin)
Checks for error in enabled pin. Terminates the process if the pin is not enabled.
Definition interrupt.c:100
#define pynq_error(...)
Definition log.h:118
void sleep_msec(int msec)
Wait for msec milliseconds.
Definition util.c:32
uint32_t * gpio
Definition gpio.c:32
void check_initialization(void)
Definition interrupt.c:41
uint32_t * intc0
Definition gpio.c:33
void findSetBitPositions(uint64_t word, uint8_t *positions)
Definition interrupt.c:130