libpynq (release 5EWC0-2023 version 0.2.0 of 2023-08-28 20:33)
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 gpio_initialized = false;
40
42 if (gpio_initialized == false) {
43 pynq_error("Interrupts have not been initialized. Call gpio_interupt_init() first.\n");
44 }
45}
46
48 int fd = open("/dev/uio1", O_RDWR, O_CLOEXEC);
49 if (fd < 0) {
50 pynq_error("failed to open interrupts\n");
51 }
52 int32_t m = 1;
53 write(fd, &m, 4);
54 gpio_initialized = true;
55 return fd;
56}
57
60 int pin_bank = pin % 32;
61 int bank = pin < 32 ? 0 : 1;
62 if (bank == 0) {
63 printf("interrupt set 0: %08X %08X\r\n", pin, pin_bank);
64 intc0[0] |= (1 << pin_bank);
65 } else {
66 printf("interrupt set 1: %08X %08X\r\n", pin, pin_bank);
67 intc0[1] |= (1 << (pin_bank));
68 }
69}
70
73 intc0[0] &= ~(1 << pin);
74}
75
78 intc0[0] = 0;
79 intc0[1] = 0;
80}
81
82uint64_t gpio_get_interrupt(void) {
84 uint64_t retv = intc0[3];
85 retv <<= 32;
86 retv |= intc0[2];
87 return retv;
88}
89
92 intc0[2] = 1;
93}
94
96 // TODO check if interrupts are initialized when using other interrupt
97 // functions
98 uint64_t retv = intc0[1];
99 retv <<= 32;
100 retv |= intc0[0];
101 if (pin < 64) {
102 uint64_t bitMask = 1ULL << pin;
103 if (!(bitMask & retv)) {
104 pynq_error("Pin %d is not enabled. Enable by using "
105 "gpio_enable_interrupt(pin). \n",
106 pin);
107 }
108 } else {
109 if (retv == 0) {
110 pynq_error("No interrupts enabled. Enable by using "
111 "gpio_enable_interrupt(pin). \n");
112 }
113 }
114}
115
118 // printf("11c: %08X\r\n", gpio[0x11c / 4]);
119 // printf("128: %08X\r\n", gpio[0x128 / 4]);
120 // printf("120: %08X\r\n", gpio[0x120 / 4]);
121 printf("interrupt 0: %08X %08X\r\n", intc0[0], intc0[2]);
122 printf("interrupt 1: %08X %08X\r\n", intc0[1], intc0[3]);
123}
124
125void findSetBitPositions(uint64_t word, uint8_t *positions) {
126 int index = 0;
127 int count = 0;
128 while (word) {
129 if (word & 1) {
130 positions[count++] = index;
131 }
132 word >>= 1;
133 index++;
134 }
135}
136
140 if (pin > 63) {
141 while (1) {
142 uint64_t interrupt = gpio_get_interrupt();
143 if (interrupt != 0) {
144 break;
145 }
146 }
147 } else {
148 while (1) {
149 uint64_t interrupt = gpio_get_interrupt();
150 uint64_t bitMask = 1ULL << pin;
151 if (bitMask & interrupt) {
152 break;
153 }
154 sleep_msec(100);
155 }
156 }
157}
158
159uint8_t *gpio_get_interrupt_pins(uint8_t *positions) {
161 verify_interrupt_request(64); // check if any interupt pin is enabled
162 // uint8_t *positions = (uint8_t *)malloc(64 * sizeof(uint8_t));
163 uint64_t pin = (uint64_t)((uint64_t)(intc0[3]) << 32 | intc0[2]);
164 findSetBitPositions(pin, positions);
165 // printf("Interrupted pin(s): ");
166 bool empty = true;
167 for (int i = 0; i < 64; i++) {
168 if (positions[i] != 0) {
169 empty = false;
170 // printf("%d ", positions[i]);
171 break;
172 }
173 }
174 if (empty) {
175 printf("WARNING: gpio_get_interrupt_pins: No pins interrupted. ");
176 }
177 printf("\n");
178 return (positions);
179}
uint8_t gpio_t
Definition gpio.h:99
void gpio_disable_all_interrupts(void)
Disables all interrupts from being raised.
Definition interrupt.c:8
void gpio_enable_interrupt(const gpio_t pin)
enables a specific pin to raise interrupts.
Definition interrupt.c:6
uint8_t * gpio_get_interrupt_pins(uint8_t *positions)
Gets all pins on which an interrupt occurred.
Definition interrupt.c:10
int gpio_interrupt_init(void)
Enables interrupts to be set and read.
Definition interrupt.c:2
uint64_t gpio_get_interrupt(void)
Definition interrupt.c:9
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:7
void gpio_print_interrupt(void)
prints the current interrupt word
Definition interrupt.c:5
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:11
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:4
void gpio_ack_interrupt(void)
acknowledges the raised interrupts and resets the interrupt word. Allows new interrupts to occur on t...
Definition interrupt.c:3
#define pynq_error(...)
Definition log.h:118
void sleep_msec(int msec)
Wait for msec milliseconds.
Definition util.c:2
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:125