-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathrtdm_pruss_irq-test.c
310 lines (277 loc) · 7.46 KB
/
rtdm_pruss_irq-test.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <sys/mman.h>
#include <error.h>
#include <string.h>
#include <rtdm/rtdm.h>
#include <time.h>
#include <inttypes.h>
#include "rtdm_pruss_irq-test_bin.h"
#define USE_PRUSSDRV
//#define USE_MEMMAP_PRU
#ifdef USE_PRUSSDRV
#include <prussdrv.h>
#endif
#ifdef XENOMAI_SKIN_posix
#include <pthread.h>
pthread_t demo_task;
#endif
#ifdef XENOMAI_SKIN_native
#include <rtdk.h>
#include <native/task.h>
RT_TASK demo_task;
#endif
char demo_task_name[] = "real-time-task";
#define VERBOSE
int fd;
int pin = 69;
int timingpin = 0;
int tpretcode;
int gShouldStop = 0;
char *rtdm_driver = "/dev/rtdm/rtdm_pruss_irq_0";
#define EVERY 1000
#if 0
static inline int toggle_timing_pin(void)
{
if (!timingpin)
return 0;
#ifdef VERBOSE
printf("requesting GPIO_IRQ_PIN_TOGGLE %d on the timing pin %d\n", GPIO_IRQ_PIN_TOGGLE, timingpin);
#endif
tpretcode = ioctl(fd, GPIO_IRQ_PIN_TOGGLE, &timingpin);
if(tpretcode < 0)
{
printf("ioctl returned %d\n", tpretcode);
}
return tpretcode;
}
#endif
static const uint32_t GPIO_SIZE = 0x198;
static const uint32_t GPIO_DATAIN = (0x138 / 4);
static const uint32_t GPIO_CLEARDATAOUT = (0x190 / 4);
static const uint32_t GPIO_SETDATAOUT = (0x194 / 4);
static const uint32_t GPIO_ADDRESSES[4] = {
0x44E07000,
0x4804C000,
0x481AC000,
0x481AE000,
};
unsigned int pinMask = 1 << 18;
unsigned int* gpio;
int init_timing_gpio()
{
fd = open("/dev/mem", O_RDWR);
int bank = 0;
pin = 22;
pinMask = 1 << pin;
uint32_t gpioBase = GPIO_ADDRESSES[bank];
gpio = (uint32_t *)mmap(0, GPIO_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, gpioBase);
if(gpio == MAP_FAILED){
fprintf(stderr, "Unable to map GPIO pin %u\n", pin);
return -2;
}
return 0;
}
void* demo(void *arg) {
struct timespec req;
req.tv_sec = 0;
req.tv_nsec = 100000000;
while(!gShouldStop)
{
int dest;
rt_printf("Reading from userspace\n");
gpio[GPIO_CLEARDATAOUT] = pinMask;
read(fd, &dest, sizeof(dest));
gpio[GPIO_SETDATAOUT] = pinMask;
rt_printf("Successfully read from userspace\n");
#ifdef XENOMAI_SKIN_native
//rt_task_sleep(req.tv_nsec);
#endif
#ifdef XENOMAI_SKIN_posix
nanosleep(&req, NULL);
#endif
}
return NULL;
}
#if 0
{
int irqs = EVERY;
struct gpio_irq_data rd = {
.pin = pin,
.falling = 0
};
int rc;
pthread_setname_np(pthread_self(), "trivial");
struct sched_param param = { .sched_priority = 99 };
pthread_setschedparam(pthread_self(), SCHED_FIFO, ¶m);
#ifdef VERBOSE
printf("requesting GPIO_IRQ_BIND %d on the pin %d\n", GPIO_IRQ_BIND, pin);
#endif
if ((rc = ioctl(fd, GPIO_IRQ_BIND, &rd)) < 0) {
perror("ioctl GPIO_IRQ_BIND");
return (void*)-2;
}
while (!gShouldStop) {
#ifdef VERBOSE
printf("waiting %d for pin %d\n", GPIO_IRQ_PIN_WAIT, pin);
#endif
if ((rc = ioctl (fd, GPIO_IRQ_PIN_WAIT, 0)) < 0) {
printf("ioctl error! rc=%d %s\n", rc, strerror(-rc));
break;
}
if(toggle_timing_pin() < 0)
return (void*)-1;
if(toggle_timing_pin() < 0)
return (void*)-1;
#ifdef VERBOSE
printf("resuming\n");
irqs--;
if (!irqs) {
irqs = EVERY;
printf("%d IRQs, tpretcode=%d\n",EVERY, tpretcode);
}
#endif
}
return (void*)0;
}
#endif
void catch_signal(int sig)
{
fprintf (stderr, "catch_signal sig=%d\n", sig);
signal(SIGTERM, SIG_DFL);
signal(SIGINT, SIG_DFL);
gShouldStop = 1;
}
static int devMemWrite(off_t target, uint32_t* value)
{
const unsigned long MAP_SIZE = 4096UL;
const unsigned long MAP_MASK = (MAP_SIZE - 1);
int fd;
void *map_base, *virt_addr;
uint32_t writeval = *value;
if((fd = open("/dev/mem", O_RDWR | O_SYNC)) == -1)
{
fprintf(stderr, "Unable to open /dev/mem: %d %s\n", fd, strerror(-fd));
return -1;
}
map_base = mmap(0, MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, target & ~MAP_MASK);
if(map_base == (void *) -1)
{
fprintf(stderr, "Unable to mmap %ld\n", target);
return -1;
}
virt_addr = ((char*)map_base) + (target & MAP_MASK);
// writes 4-byte word (unsigned long)
*((unsigned long *) virt_addr) = writeval;
uint32_t read_result = *((unsigned long *) virt_addr);
*value = read_result;
if(munmap(map_base, MAP_SIZE) == -1)
{
fprintf(stderr, "Error while unmapping memory\n");
}
close(fd);
return 0;
}
int main(int argc, char* argv[])
{
signal(SIGTERM, catch_signal);
signal(SIGINT, catch_signal);
#ifdef USE_PRUSSDRV
printf("Init'ing prussdrv and starting PRU\n");
prussdrv_init();
if(prussdrv_open(PRU_EVTOUT_0)) {
fprintf(stderr, "Failed to open PRU driver\n");
return 1;
}
// if(prussdrv_exec_program(1, "pru_irq_test.bin"))
if(prussdrv_exec_code(1, PRUcode, sizeof(PRUcode)))
{
fprintf(stderr, "Error while loading PRU program\n");
return -1;
}
#endif
#ifdef USE_MEMMAP_PRU
printf("Init'ing PRU via memmap\n");
//memmap the PRU memory
if((fd = open("/dev/mem", O_RDWR | O_SYNC)) == -1)
{
fprintf(stderr, "Unable to open /dev/mem: %d %s\n", fd, strerror(-fd));
return -1;
}
int size = 0x7ffff;
int target = 0x4a300000;
const unsigned long MAP_SIZE = 4096UL;
const unsigned long MAP_MASK = (MAP_SIZE - 1);
void* map_base = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, target & ~MAP_MASK);
void* virt_addr = ((char*)map_base) + (target & MAP_MASK);
uint32_t value;
uint32_t ctrl_register_offset = 0x24000;
uint32_t instruction_memory_offset = 0x38000;
memcpy(virt_addr, &value, sizeof(value));
printf("Read: %#x\n", value);
// disable PRU (will reset the INTC registers, so run before open()'ing the driver )
value = 1;
memcpy(virt_addr + ctrl_register_offset, &value, sizeof(value));
// load code to PRU instruction RAM
memcpy(virt_addr + instruction_memory_offset, PRUcode, sizeof(PRUcode));
// enable PRU
value = 2;
memcpy(virt_addr + ctrl_register_offset, &value, sizeof(value));
#endif
if(init_timing_gpio())
exit(1);
printf("Pointer: %#p\n", gpio);
if (argc > 1)
pin = atoi(argv[1]);
if (argc > 2)
timingpin = atoi(argv[2]);
/* Avoids memory swapping for this program */
mlockall(MCL_CURRENT|MCL_FUTURE);
// Open RTDM driver
if ((fd = open(rtdm_driver, O_RDWR)) < 0) {
perror("rt_open");
exit(-1);
}
printf("Returned fd: %d\n", fd);
#ifdef XENOMAI_SKIN_posix
pthread_create(&demo_task, NULL, demo, NULL);
pthread_setname_np(demo_task, demo_task_name);
void* taskReturn;
pthread_join(demo_task, &taskReturn);
int iret = (int)taskReturn;
printf("Returned: %d\n", iret);
#endif
#ifdef XENOMAI_SKIN_native
int ret;
if(ret = rt_task_create(&demo_task, demo_task_name, 16384 * 8, 90, T_JOINABLE | T_FPU))
{
fprintf(stderr, "Impossible to create task: %d %s\n", -ret, strerror(-ret));
}
if(ret = rt_task_start(&demo_task, &demo, 0))
{
fprintf(stderr, "Impossible to start task: %d %s\n", -ret, strerror(-ret));
}
while(!gShouldStop)
usleep(1000000);
rt_task_delete(&demo_task);
rt_task_join(&demo_task);
#endif
#ifdef USE_PRUSSDRV
prussdrv_exit();
#endif
#ifdef USE_MEMMAP_PRU
#define PRU_INTC_SECR1_REG 0x280
#define PRU_INTC_SECR2_REG 0x284
value = 0xFFFFFFFF;
memcpy(virt_addr + PRU_INTC_SECR1_REG, &value, sizeof(value));
memcpy(virt_addr + PRU_INTC_SECR2_REG, &value, sizeof(value));
// disable PRU
value = 1;
memcpy(virt_addr + ctrl_register_offset, &value, sizeof(value));
#endif
close(fd);
printf("Closed\n");
return 0;
}