-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtestpru1.c
501 lines (437 loc) · 10.7 KB
/
testpru1.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
/*
* testpru
*
*/
#define PRU1
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <stdio.h>
#include <strings.h>
#include "debug.h"
#include "pru_defs.h"
#include "prucomm.h"
#include "syscall.h"
extern void delay_cycles(u32 delay);
extern void delay_cycles_accurate(u32 delay);
extern void delay_cycles_accurate2(u32 delay);
extern u32 read_other_r30(void);
extern void update_gpo(u32 clrmsk, u32 setmsk);
void pwm_loop(u32 hi, u32 lo)
{
while (!pru_signal()) {
__R30 |= (1 << 13);
delay_cycles(hi);
__R30 &= ~(1 << 13);
delay_cycles(lo);
}
}
void pwm_loop2(u32 hi, u32 lo)
{
while (!pru_signal()) {
__R30 |= (1 << 13);
delay_cycles_accurate2(hi);
__R30 &= ~(1 << 13);
delay_cycles_accurate2(lo);
}
}
#define T1 asm (" .global T1\nT1:");
#define T2 asm (" .global T2\nT2:");
struct pwm_multi_config cfg;
static void pwm_setup(void)
{
u8 i;
cfg.enmask = 0;
for (i = 0; i < MAX_PWMS; i++)
cfg.hilo[i][0] = cfg.hilo[i][1] = PRU_us(200);
#if 0
cfg.enmask = BIT(13) | BIT(12);
cfg.hilo[12][0] = PRU_us(333);
cfg.hilo[12][1] = PRU_us(333);
cfg.hilo[13][0] = PRU_us(100);
cfg.hilo[13][1] = PRU_us(100);
#endif
}
#undef USE_PWM_LOOP
#define USE_PWM_MACRO
struct cxt {
u32 cnt;
u32 next;
u32 enmask;
u32 stmask;
u32 setmsk;
u32 clrmsk;
u32 deltamin;
u32 *next_hi_lo;
};
static inline u32 read_PIEP_COUNT(void)
{
return PIEP_COUNT;
}
static void handle_pwm_cmd(struct cxt *cxt)
{
u8 i;
u32 msk, setmsk, clrmsk;
u32 enmask, stmask, cnt, deltamin, next;
struct pwm_cmd *pwmc;
u32 *nextp;
u32 *next_hi_lop;
const u32 *hilop;
cnt = cxt->cnt;
next = cxt->next;
enmask = cxt->enmask;
stmask = cxt->stmask;
setmsk = cxt->setmsk;
clrmsk = cxt->clrmsk;
deltamin = cxt->deltamin;
next_hi_lop = cxt->next_hi_lo;
// sc_printf("cnt=%x next=%x deltamin=%x", cnt, next, deltamin);
pwmc = PWM_CMD;
if (pwmc->cmd == PWM_CMD_CONFIG) {
enmask = pwmc->u.cfg.enmask;
stmask = 0; /* starting all low */
clrmsk = 0;
for (i = 0, msk = 1, nextp = next_hi_lop, hilop = &pwmc->u.cfg.hilo[0][0];
i < MAX_PWMS;
i++, msk <<= 1, nextp += 3, hilop += 2) {
if ((enmask & msk) == 0) {
nextp[1] = PRU_us(100); /* default */
nextp[2] = PRU_us(100);
continue;
}
nextp[0] = cnt; /* next */
nextp[1] = hilop[0]; /* hi */
nextp[2] = hilop[1]; /* lo */
}
clrmsk = enmask;
setmsk = 0;
/* guaranteed to be immediate */
deltamin = 0;
} else if (pwmc->cmd == PWM_CMD_ENABLE) {
msk = BIT(pwmc->pwm_nr);
if (pwmc->pwm_nr < MAX_PWMS && (PWM_EN_MASK & msk) && (enmask & msk) == 0) {
enmask |= msk;
if (pwmc->pwm_nr < 16)
__R30 |= msk;
nextp = &next_hi_lop[pwmc->pwm_nr * 3];
nextp[0] = cnt; /* since we start high, wait this amount */
/* first enable */
if (enmask == msk)
cnt = read_PIEP_COUNT();
deltamin = 0;
next = cnt;
// sc_printf("e %d %d %d - next=%x cnt=%x", pwmc->pwm_nr, nextp[1], nextp[2], next, cnt);
}
} else if (pwmc->cmd == PWM_CMD_DISABLE) {
msk = BIT(pwmc->pwm_nr);
if (pwmc->pwm_nr < MAX_PWMS && (PWM_EN_MASK & msk) && (enmask & msk) != 0) {
enmask &= ~msk;
/* clear bit */
if (pwmc->pwm_nr < 16)
__R30 &= ~msk;
// sc_printf("d %d", pwmc->pwm_nr);
}
} else if (pwmc->cmd == PWM_CMD_MODIFY) {
msk = BIT(pwmc->pwm_nr);
if (pwmc->pwm_nr < MAX_PWMS && (PWM_EN_MASK & msk)) {
nextp = &next_hi_lop[pwmc->pwm_nr * 3];
/* only allow sane values */
if (pwmc->u.hilo[0] >= MIN_PWM_PULSE &&
pwmc->u.hilo[1] >= MIN_PWM_PULSE) {
nextp[1] = pwmc->u.hilo[0];
nextp[2] = pwmc->u.hilo[1];
}
// sc_printf("m %d %d %d", pwmc->pwm_nr, nextp[1], nextp[2]);
}
} else if (pwmc->cmd == PWM_CMD_SET || pwmc->cmd == PWM_CMD_CLR) {
msk = BIT(pwmc->pwm_nr);
if (pwmc->pwm_nr < MAX_PWMS) {
/* set bit (if the pwm is running it will be temporary) */
if (pwmc->pwm_nr < 16) {
if (pwmc->cmd == PWM_CMD_SET)
__R30 |= msk;
else
__R30 &= ~msk;
}
}
} else if (pwmc->cmd == PWM_CMD_TEST) {
/* nothing */
}
pwmc->magic = PWM_REPLY_MAGIC;
SIGNAL_EVENT(SYSEV_THIS_PRU_TO_OTHER_PRU);
// sc_printf("cnt=%x next=%x deltamin=%x", cnt, next, deltamin);
cxt->cnt = cnt;
cxt->next = next;
cxt->enmask = enmask;
cxt->stmask = stmask;
cxt->setmsk = setmsk;
cxt->clrmsk = clrmsk;
cxt->deltamin = deltamin;
}
static void handle_pru_signal(struct cxt *cxt)
{
if (PINTC_SRSR0 & (1 << SYSEV_OTHER_PRU_TO_THIS_PRU)) {
PINTC_SICR = SYSEV_OTHER_PRU_TO_THIS_PRU;
// sc_printf("PRU signal");
if (PWM_CMD->magic == PWM_CMD_MAGIC)
handle_pwm_cmd(cxt);
}
if (PINTC_SRSR0 & (1 << SYSEV_ARM_TO_THIS_PRU)) {
PINTC_SICR = SYSEV_ARM_TO_THIS_PRU;
// sc_puts("ARM signal");
}
}
int main(int argc, char *argv[])
{
u8 i;
u32 cnt, next;
u32 msk, setmsk, clrmsk;
u32 delta, deltamin, tnext, hi, lo;
u32 *nextp;
const u32 *hilop;
u32 enmask; /* enable mask */
u32 stmask; /* state mask */
static u32 next_hi_lo[MAX_PWMS][3];
static struct cxt cxt;
/* enable OCP master port */
PRUCFG_SYSCFG &= ~SYSCFG_STANDBY_INIT;
PRUCFG_SYSCFG = (PRUCFG_SYSCFG &
~(SYSCFG_IDLE_MODE_M | SYSCFG_STANDBY_MODE_M)) |
SYSCFG_IDLE_MODE_NO | SYSCFG_STANDBY_MODE_NO;
/* our PRU wins arbitration */
#if defined(PRU0)
PRUCFG_SPP &= ~SPP_PRU1_PAD_HP_EN;
#elif defined(PRU1)
PRUCFG_SPP |= SPP_PRU1_PAD_HP_EN;
#endif
pwm_setup();
sc_puts("PRU1; go");
/* configure timer */
PIEP_GLOBAL_CFG = GLOBAL_CFG_CNT_ENABLE |
GLOBAL_CFG_DEFAULT_INC(1) |
GLOBAL_CFG_CMP_INC(1);
PIEP_CMP_STATUS = CMD_STATUS_CMP_HIT(1); /* clear the interrupt */
PIEP_CMP_CFG |= CMP_CFG_CMP_EN(1);
/* copy from cfg to cxt */
/* initialize */
cnt = read_PIEP_COUNT();
enmask = cfg.enmask;
stmask = 0; /* starting all low */
clrmsk = 0;
for (i = 0, msk = 1, nextp = &next_hi_lo[0][0], hilop = &cfg.hilo[0][0];
i < MAX_PWMS;
i++, msk <<= 1, nextp += 3, hilop += 2) {
if ((enmask & msk) == 0) {
nextp[1] = PRU_us(100); /* default */
nextp[2] = PRU_us(100);
continue;
}
nextp[0] = cnt; /* next */
nextp[1] = hilop[0]; /* hi */
nextp[2] = hilop[1]; /* lo */
}
clrmsk = enmask;
setmsk = 0;
/* guaranteed to be immediate */
deltamin = 0;
next = cnt + deltamin;
for (;;) {
/* signalled interrupt from either PRU0 or host */
if (pru_signal()) {
cxt.cnt = cnt;
cxt.next = next;
cxt.enmask = enmask;
cxt.stmask = stmask;
cxt.setmsk = setmsk;
cxt.clrmsk = clrmsk;
cxt.deltamin = deltamin;
cxt.next_hi_lo = &next_hi_lo[0][0];
handle_pru_signal(&cxt);
cnt = cxt.cnt;
next = cxt.next;
enmask = cxt.enmask;
stmask = cxt.stmask;
setmsk = cxt.setmsk;
clrmsk = cxt.clrmsk;
deltamin = cxt.deltamin;
// sc_printf("next=%x", next);
}
/* if nothing is enabled just skip it all */
if (enmask == 0)
continue;
setmsk = 0;
clrmsk = (u32)-1;
deltamin = PRU_ms(100); /* (1U << 31) - 1; */
next = cnt + deltamin;
#define SINGLE_PWM(_i) \
do { \
if (enmask & (1U << (_i))) { \
nextp = &next_hi_lo[(_i)][0]; \
tnext = nextp[0]; \
hi = nextp[1]; \
lo = nextp[2]; \
/* avoid signed arithmetic */ \
while (((delta = (tnext - cnt)) & (1U << 31)) != 0) { \
/* toggle the state */ \
if (stmask & (1U << (_i))) { \
stmask &= ~(1U << (_i)); \
clrmsk &= ~(1U << (_i)); \
tnext += lo; \
} else { \
stmask |= (1U << (_i)); \
setmsk |= (1U << (_i)); \
tnext += hi; \
} \
} \
if (delta <= deltamin) { \
deltamin = delta; \
next = tnext; \
} \
nextp[0] = tnext; \
} \
} while (0)
#ifdef USE_PWM_LOOP
for (i = 0, msk = 1, nextp = &next_hi_lo[0][0]; i < MAX_PWMS; i++, msk <<= 1, nextp += 3) {
if ((enmask & msk) == 0)
continue;
tnext = nextp[0];
hi = nextp[1];
lo = nextp[2];
/* avoid signed arithmetic */
while (((delta = (tnext - cnt)) & (1U << 31)) != 0) {
/* toggle the state */
if (stmask & msk) {
stmask &= ~msk;
clrmsk &= ~msk;
tnext += lo;
} else {
stmask |= msk;
setmsk |= msk;
tnext += hi;
}
}
if (delta <= deltamin) {
deltamin = delta;
next = tnext;
}
nextp[0] = tnext;
}
#endif
#ifdef USE_PWM_MACRO
#if MAX_PWMS > 0 && (PWM_EN_MASK & BIT(0))
SINGLE_PWM(0);
#endif
#if MAX_PWMS > 1 && (PWM_EN_MASK & BIT(1))
SINGLE_PWM(1);
#endif
#if MAX_PWMS > 2 && (PWM_EN_MASK & BIT(2))
SINGLE_PWM(2);
#endif
#if MAX_PWMS > 3 && (PWM_EN_MASK & BIT(3))
SINGLE_PWM(3);
#endif
#if MAX_PWMS > 4 && (PWM_EN_MASK & BIT(4))
SINGLE_PWM(4);
#endif
#if MAX_PWMS > 5 && (PWM_EN_MASK & BIT(5))
SINGLE_PWM(5);
#endif
#if MAX_PWMS > 6 && (PWM_EN_MASK & BIT(6))
SINGLE_PWM(6);
#endif
#if MAX_PWMS > 7 && (PWM_EN_MASK & BIT(7))
SINGLE_PWM(7);
#endif
#if MAX_PWMS > 8 && (PWM_EN_MASK & BIT(8))
SINGLE_PWM(8);
#endif
#if MAX_PWMS > 9 && (PWM_EN_MASK & BIT(9))
SINGLE_PWM(9);
#endif
#if MAX_PWMS > 10 && (PWM_EN_MASK & BIT(10))
SINGLE_PWM(10);
#endif
#if MAX_PWMS > 11 && (PWM_EN_MASK & BIT(11))
SINGLE_PWM(11);
#endif
#if MAX_PWMS > 12 && (PWM_EN_MASK & BIT(12))
SINGLE_PWM(12);
#endif
#if MAX_PWMS > 13 && (PWM_EN_MASK & BIT(13))
SINGLE_PWM(13);
#endif
#if MAX_PWMS > 14 && (PWM_EN_MASK & BIT(14))
SINGLE_PWM(14);
#endif
#if MAX_PWMS > 15 && (PWM_EN_MASK & BIT(15))
SINGLE_PWM(15);
#endif
#if MAX_PWMS > 16 && (PWM_EN_MASK & BIT(16))
SINGLE_PWM(16);
#endif
#if MAX_PWMS > 17 && (PWM_EN_MASK & BIT(17))
SINGLE_PWM(17);
#endif
#if MAX_PWMS > 18 && (PWM_EN_MASK & BIT(18))
SINGLE_PWM(18);
#endif
#if MAX_PWMS > 19 && (PWM_EN_MASK & BIT(19))
SINGLE_PWM(19);
#endif
#if MAX_PWMS > 20 && (PWM_EN_MASK & BIT(20))
SINGLE_PWM(20);
#endif
#if MAX_PWMS > 21 && (PWM_EN_MASK & BIT(21))
SINGLE_PWM(21);
#endif
#if MAX_PWMS > 22 && (PWM_EN_MASK & BIT(22))
SINGLE_PWM(22);
#endif
#if MAX_PWMS > 23 && (PWM_EN_MASK & BIT(23))
SINGLE_PWM(23);
#endif
#if MAX_PWMS > 24 && (PWM_EN_MASK & BIT(24))
SINGLE_PWM(24);
#endif
#if MAX_PWMS > 25 && (PWM_EN_MASK & BIT(25))
SINGLE_PWM(25);
#endif
#if MAX_PWMS > 26 && (PWM_EN_MASK & BIT(26))
SINGLE_PWM(26);
#endif
#if MAX_PWMS > 27 && (PWM_EN_MASK & BIT(27))
SINGLE_PWM(27);
#endif
#if MAX_PWMS > 28 && (PWM_EN_MASK & BIT(28))
SINGLE_PWM(28);
#endif
#if MAX_PWMS > 29 && (PWM_EN_MASK & BIT(29))
SINGLE_PWM(29);
#endif
#if MAX_PWMS > 30 && (PWM_EN_MASK & BIT(30))
SINGLE_PWM(30);
#endif
#if MAX_PWMS > 31 && (PWM_EN_MASK & BIT(31))
SINGLE_PWM(30);
#endif
#endif
/* results in set bits where there are changes */
delta = ~clrmsk | setmsk;
if ((delta & 0xffff) != 0)
__R30 = (__R30 & (clrmsk & 0xffff)) | (setmsk & 0xffff);
if ((delta >> 16) != 0)
pru_other_and_or_reg(30, (clrmsk >> 16) | 0xffff0000, setmsk >> 16);
#if 0
cnt = read_PIEP_COUNT();
if (((next - cnt) & (1U << 31)) == 0 && delta > PRU_ms(1)) {
sc_printf("bad next=%x cnt=%x", next, cnt);
}
#endif
/* loop while nothing changes */
do {
cnt = read_PIEP_COUNT();
if (pru_signal())
break;
} while (((next - cnt) & (1U << 31)) == 0);
}
}