forked from mikelikespie/gopru
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prussdrv.go
699 lines (561 loc) · 15.1 KB
/
prussdrv.go
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
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
package pruss
import (
"fmt"
"io"
"io/ioutil"
"log"
"os"
"reflect"
"strconv"
"strings"
"syscall"
"unsafe"
)
type PruRamId int
type EvtOut int
type PruNum int
const (
NUM_PRU_HOSTIRQS = 8
NUM_PRU_HOSTS = 10
NUM_PRU_CHANNELS = 10
NUM_PRU_SYS_EVTS = 64
PRUSS0_PRU0_DATARAM PruRamId = 0
PRUSS0_PRU1_DATARAM PruRamId = 1
PRUSS0_PRU0_IRAM PruRamId = 2
PRUSS0_PRU1_IRAM PruRamId = 3
PRU_EVTOUT_0 EvtOut = 0
PRU_EVTOUT_1 EvtOut = 1
PRU_EVTOUT_2 EvtOut = 2
PRU_EVTOUT_3 EvtOut = 3
PRU_EVTOUT_4 EvtOut = 4
PRU_EVTOUT_5 EvtOut = 5
PRU_EVTOUT_6 EvtOut = 6
PRU_EVTOUT_7 EvtOut = 7
Pru0 PruNum = 0
Pru1 PruNum = 1
PruCount PruNum = 2
)
type PruRamType int
const (
DATARAM PruRamType = 0
IRAM PruRamType = iota
// This is shared between both PRUs
SHARED PruRamType = iota
EXTERNAL PruRamType = iota
)
type Channel uint16
type Host uint16
type EventNum uint16
type SysevtToChannelMap map[EventNum]Channel
type ChannelToHostMap map[Channel]Host
const enableL3ram = false
var PruEvts = []EvtOut{
PRU_EVTOUT_0,
PRU_EVTOUT_1,
}
type Pru interface {
Reset() error
Enable() error
Disable() error
WriteMemory(ramType PruRamType, writeOffset uint, sourceData []byte) error
ExecProgram(filename string) error
ExecImage(image []byte) error
// The default evt out to listen on
DefaultEvtOut() EvtOut
DataramMem() []byte
// Maps a pointer to the ram type
// it will panic if things go wrong
// Usage:
// var iptr *int32
// pru.MapPointer(EXTERNAL, 0, &iptr)
// *iptr = 3
// The "map" operation itself isn't incredibly efficient, but the pointer will be directly mapped after this
MapPointer(ramType PruRamType, offset int, ptrPtr interface{})
}
type PrussDrv interface {
OpenInterrupt(evtOut EvtOut) (l EventListener, err error)
Pru(pruNum PruNum) (pru Pru)
InitInterrupts() (err error)
Close() (err error)
// Returns a reference to the DDR memory
ExtRamMem() []byte
ExtRamPhys() uintptr
SharedRamMem() []byte
SendEvent(eventNum EventNum) error
GetPhysicalAddress(ptr unsafe.Pointer) uintptr
}
func InitDrv() (drv PrussDrv, err error) {
d := &prussDrv{
initData: DefaultInitData,
prus: []prussPru{
prussPru{pruNum: Pru0},
prussPru{pruNum: Pru1},
},
eventListeners: make(map[EvtOut]*eventListener),
}
for i, _ := range d.prus {
d.prus[i].drv = d
}
drv = d
return
}
type prussPru struct {
drv *prussDrv
pruNum PruNum
dataramMem []byte
controlMem []byte
// Remapped offset 0 of controlMem
controlMemPtr *uint32
debugMem []byte
iramMem []byte
}
// Outer is for requests, inner is chan for responses
type EventListener interface {
// Returns a channel that will yield true when we receive an event
Wait() uint32
// Blocks on enquing a wait task, but will return a channel that will receive an element after the wait is finished
WaitC() <-chan uint32
ClearEvent(eventNum EventNum) error
}
type eventListener struct {
f *os.File
drv *prussDrv
// Incoming wait requests
in chan bool
// Outgoing wait requests
out chan uint32
// Event out number
evt EvtOut
}
type prussDrv struct {
initData PruIntcInitData
prus []prussPru
eventListeners map[EvtOut]*eventListener
mmapFdFile *os.File
physMem uintptr
l3RamPhysBase uintptr
extRamPhysBase uintptr
// mmapped files
intcMem []byte
l3RamMem []byte
extRamMem []byte
sharedramMem []byte
}
func readIntFromFile(fileName string) (num uintptr, err error) {
c, err := ioutil.ReadFile(fileName)
if err != nil {
return 0, err
}
i, err := strconv.ParseUint(strings.TrimSpace(string(c[2:])), 16, 32)
if err == nil {
num = uintptr(i)
}
return
}
func (d *prussDrv) rambaseOffset(physMem int) []byte {
offset := physMem - DATARAM0_PHYS_BASE
ramMem := d.prus[0].dataramMem
return ramMem[offset:]
}
func (d *prussDrv) memmapInit() (err error) {
// Get the first one
for _, l := range d.eventListeners {
d.mmapFdFile = l.f
break
}
if d.mmapFdFile == nil {
panic("We should have an fd by now")
}
d.physMem, err = readIntFromFile(PRUSS_UIO_DRV_PRUSS_BASE)
if err != nil {
return err
}
prussMapSize, err := readIntFromFile(PRUSS_UIO_DRV_PRUSS_SIZE)
if err != nil {
return err
}
d.prus[0].dataramMem, err = syscall.Mmap(
int(d.mmapFdFile.Fd()),
int64(PRUSS_UIO_MAP_OFFSET_PRUSS),
int(prussMapSize),
syscall.PROT_READ|syscall.PROT_WRITE,
syscall.MAP_SHARED,
)
if err != nil {
return err
}
d.prus[1].dataramMem = d.rambaseOffset(DATARAM1_PHYS_BASE)
d.intcMem = d.rambaseOffset(INTC_PHYS_BASE)
if len(d.intcMem) < PRU_INTC_HIER_REG {
return fmt.Errorf(
"Expected d.intcMem to be at least %d big, but it is only of length %d",
PRU_INTC_HIER_REG,
len(d.intcMem),
)
}
d.sharedramMem = d.rambaseOffset(PRUSS_SHAREDRAM_BASE)
d.prus[0].controlMem = d.rambaseOffset(PRU0CONTROL_PHYS_BASE)
d.prus[0].controlMemPtr = (*uint32)(unsafe.Pointer(&d.prus[0].controlMem[0]))
d.prus[1].controlMem = d.rambaseOffset(PRU1CONTROL_PHYS_BASE)
d.prus[1].controlMemPtr = (*uint32)(unsafe.Pointer(&d.prus[1].controlMem[0]))
d.prus[0].debugMem = d.rambaseOffset(PRU0DEBUG_PHYS_BASE)
d.prus[1].debugMem = d.rambaseOffset(PRU1DEBUG_PHYS_BASE)
d.prus[0].iramMem = d.rambaseOffset(PRU0IRAM_PHYS_BASE)
d.prus[1].iramMem = d.rambaseOffset(PRU1IRAM_PHYS_BASE)
if enableL3ram {
d.l3RamPhysBase, err = readIntFromFile(PRUSS_UIO_DRV_L3RAM_BASE)
if err != nil {
return err
}
l3RamMapSize, err := readIntFromFile(PRUSS_UIO_DRV_L3RAM_SIZE)
if err != nil {
return err
}
d.l3RamMem, err = syscall.Mmap(
int(d.mmapFdFile.Fd()),
int64(PRUSS_UIO_MAP_OFFSET_L3RAM),
int(l3RamMapSize),
syscall.PROT_READ|syscall.PROT_WRITE,
syscall.MAP_SHARED,
)
}
d.extRamPhysBase, err = readIntFromFile(PRUSS_UIO_DRV_EXTRAM_BASE)
if err != nil {
return err
}
extRamMapSize, err := readIntFromFile(PRUSS_UIO_DRV_EXTRAM_SIZE)
if err != nil {
return err
}
d.extRamMem, err = syscall.Mmap(
int(d.mmapFdFile.Fd()),
int64(PRUSS_UIO_MAP_OFFSET_EXTRAM),
int(extRamMapSize),
syscall.PROT_READ|syscall.PROT_WRITE,
syscall.MAP_SHARED,
)
return
}
func (d *prussDrv) OpenInterrupt(evtOut EvtOut) (l EventListener, err error) {
if l, found := d.eventListeners[evtOut]; found {
return l, nil
}
f, err := os.OpenFile(
"/dev/uio"+strconv.Itoa(int(evtOut)),
os.O_SYNC|os.O_RDWR,
0600,
)
if err != nil {
return
}
ret := &eventListener{
f: f,
drv: d,
in: make(chan bool),
out: make(chan uint32),
evt: evtOut,
}
d.eventListeners[evtOut] = ret
go ret.consumeEvents()
// We initialize memmap stuff here since it uses it
if d.mmapFdFile == nil {
if err = d.memmapInit(); err != nil {
return nil, err
}
}
return ret, nil
}
func (d *prussDrv) Pru(pruNum PruNum) (pru Pru) {
return &d.prus[pruNum]
}
// Returns an int pointer into the interrupt register offset by bytes
func (d *prussDrv) intcOffsetInt(offsetBytes uint32, offsetInt int) *uint32 {
offset := int(offsetBytes) + offsetInt*int(unsafe.Sizeof(uint32(0)))
if offset+int(unsafe.Sizeof(uint32(0)))-1 > len(d.intcMem) {
log.Panicf("Invalid offset %d. Max size of intc is %d", offset, len(d.intcMem))
}
sPtr := &d.intcMem[offset]
return (*uint32)(unsafe.Pointer(sPtr))
}
func (d *prussDrv) intcSetCmr(sysevt EventNum, channel Channel) {
offset := PRU_INTC_CMR1_REG + uint32(uint16(sysevt) & ^(uint16(0x3)))
*d.intcOffsetInt(offset, 0) |= ((uint32(channel) & 0xF) << ((uint32(sysevt) & 0x3) << 3))
}
func (d *prussDrv) intcSetHmr(channel Channel, host Host) {
offset := PRU_INTC_HMR1_REG + uint32(uint16(channel) & ^(uint16(0x3)))
*d.intcOffsetInt(offset, 0) |= ((uint32(host) & 0xF) << ((uint32(channel) & 0x3) << 3))
}
func (d *prussDrv) getHardwareVersion() (v uint32) {
return *d.intcOffsetInt(0, 0)
}
// This is a transliteration of pruss's prussdrv_pruintc_init.
func (d *prussDrv) InitInterrupts() (err error) {
*d.intcOffsetInt(PRU_INTC_SIPR1_REG, 0) = 0xFFFFFFFF
*d.intcOffsetInt(PRU_INTC_SIPR2_REG, 0) = 0xFFFFFFFF
for i := 0; i < NUM_PRU_SYS_EVTS; i++ {
*d.intcOffsetInt(PRU_INTC_CMR1_REG, i) = 0x0
}
for evt, channel := range d.initData.SysevtToChannelMap {
d.intcSetCmr(evt, channel)
}
for i := 0; i < NUM_PRU_HOSTS; i++ {
*d.intcOffsetInt(PRU_INTC_HMR1_REG, i) = 0x0
}
for channel, host := range d.initData.ChannelToHostMap {
d.intcSetHmr(channel, host)
}
*d.intcOffsetInt(PRU_INTC_SITR1_REG, 0) = 0x0
*d.intcOffsetInt(PRU_INTC_SITR2_REG, 0) = 0x0
var mask1, mask2 uint32
for _, sysevt := range d.initData.SysevtsEnabled {
switch {
case sysevt < 32:
mask1 |= 1 << sysevt
case sysevt < 64:
mask2 |= 1 << (sysevt - 32)
default:
return fmt.Errorf("SYS_EVT%d out of range", sysevt)
}
}
*d.intcOffsetInt(PRU_INTC_ESR1_REG, 0) = mask1
*d.intcOffsetInt(PRU_INTC_SECR1_REG, 0) = mask1
*d.intcOffsetInt(PRU_INTC_ESR2_REG, 0) = mask2
*d.intcOffsetInt(PRU_INTC_SECR2_REG, 0) = mask2
for _, h := range d.initData.HostEnabled {
// We set this one at a time to enable the interrupts
// see http://elinux.org/PRUSSv2_Interrupt_Controller#Enabling_the_Interrupt_Controller
if h < MAX_HOSTS_SUPPORTED {
*d.intcOffsetInt(PRU_INTC_HIEISR_REG, 0) = uint32(h)
}
}
*d.intcOffsetInt(PRU_INTC_GER_REG, 0) = 0x1
if d.getHardwareVersion() != 0x4E82A900 {
return fmt.Errorf("Unexpected hardware version")
}
return
}
func clearAndMunmap(v *[]byte) {
if *v != nil {
syscall.Munmap(*v)
*v = nil
}
}
func (d *prussDrv) Close() (err error) {
for _, l := range d.eventListeners {
l.Close()
}
clearAndMunmap(&d.prus[0].dataramMem)
d.intcMem = nil
d.sharedramMem = nil
if enableL3ram {
clearAndMunmap(&d.l3RamMem)
}
clearAndMunmap(&d.extRamMem)
for _, pru := range d.prus {
pru.controlMem = nil
pru.controlMemPtr = nil
pru.dataramMem = nil
pru.debugMem = nil
pru.iramMem = nil
}
d.mmapFdFile = nil
return
}
func (p *prussPru) Enable() (err error) {
*p.controlMemPtr = 2
return
}
func (p *prussPru) Reset() (err error) {
*p.controlMemPtr = 0
return
}
func (p *prussPru) Disable() (err error) {
*p.controlMemPtr = 1
return
}
func (p *prussPru) WriteMemory(ramType PruRamType, writeOffset uint, sourceData []byte) (err error) {
var memory []byte
switch ramType {
case IRAM:
memory = p.iramMem
case DATARAM:
memory = p.dataramMem
case SHARED:
memory = p.drv.sharedramMem
case EXTERNAL:
memory = p.drv.extRamMem
}
copy(memory[writeOffset:], sourceData)
return
}
func (p *prussPru) ExecImage(image []byte) (err error) {
if err = p.Disable(); err != nil {
return
}
if err = p.WriteMemory(IRAM, 0, image); err != nil {
return
}
if err = p.Enable(); err != nil {
return
}
return
}
func (p *prussPru) ExecProgram(filename string) (err error) {
b, err := ioutil.ReadFile(filename)
if err != nil {
return
}
if len(b) >= PRUSS_IRAM_SIZE {
err = fmt.Errorf(
"Image size (%d) from (%s) exceeds max iram size (%d)",
len(b),
filename,
PRUSS_IRAM_SIZE,
)
return
}
return p.ExecImage(b)
}
func (p *prussPru) DefaultEvtOut() EvtOut {
return PruEvts[p.pruNum]
}
func (p *prussPru) DataramMem() []byte {
return p.dataramMem
}
func (p *prussPru) MapPointer(ramType PruRamType, offset int, ptr interface{}) {
ptrPtrVal := reflect.ValueOf(ptr)
if ptrPtrVal.Kind() != reflect.Ptr {
log.Panicln("Can only call MapPointer with a pointer to a pointer")
}
ptrVal := ptrPtrVal.Elem()
if ptrVal.Kind() != reflect.Ptr {
log.Panicln("Can only call MapPointer with a pointer to a pointer")
}
elemType := ptrVal.Type().Elem()
switch elemType.Kind() {
case reflect.Slice, reflect.Map, reflect.Interface, reflect.Chan:
log.Panicln("MapPointer does not support kind %+v", ptrPtrVal.Type())
default:
}
var memory []byte
switch ramType {
case IRAM:
memory = p.iramMem
case DATARAM:
memory = p.dataramMem
case SHARED:
memory = p.drv.sharedramMem
case EXTERNAL:
memory = p.drv.extRamMem
}
// Making a slice so it bounds checks
physPtr := uintptr(unsafe.Pointer(&memory[offset:elemType.Size()][0]))
*(*uintptr)(unsafe.Pointer(ptrPtrVal.Pointer())) = physPtr
}
func (d *prussDrv) ExtRamMem() []byte {
return d.extRamMem
}
func (d *prussDrv) ExtRamPhys() uintptr {
return uintptr(d.extRamPhysBase)
}
func (d *prussDrv) SharedRamMem() []byte {
return d.sharedramMem
}
func (d *prussDrv) SendEvent(eventNum EventNum) (err error) {
switch {
case eventNum < 32:
*d.intcOffsetInt(PRU_INTC_SRSR1_REG, 0) = uint32(eventNum)
case eventNum < 64:
*d.intcOffsetInt(PRU_INTC_SRSR2_REG, 0) = uint32(eventNum) - 32
default:
return fmt.Errorf("Invalid EventNum: %d", eventNum)
}
return
}
func (d *prussDrv) GetPhysicalAddress(address unsafe.Pointer) (physAddr uintptr) {
addr := uintptr(address)
dataRamBase := uintptr(unsafe.Pointer(&d.prus[0].dataramMem[0]))
var l3base, l3end uintptr
if enableL3ram {
l3base = uintptr(unsafe.Pointer(&d.l3RamMem[0]))
l3end = l3base + uintptr(len(d.l3RamMem))
}
extBase := uintptr(unsafe.Pointer(&d.extRamMem[0]))
extEnd := extBase + uintptr(len(d.extRamMem))
switch {
case addr >= dataRamBase && addr < dataRamBase+uintptr(len(d.prus[0].dataramMem)):
physAddr = addr - dataRamBase + d.extRamPhysBase
case enableL3ram && addr >= l3base && addr < l3end:
physAddr = addr - l3base + d.l3RamPhysBase
case addr >= extBase && addr < extEnd:
physAddr = addr - extBase + d.extRamPhysBase
}
return
}
/*
unsigned int retaddr = 0;
if ((address >= prussdrv.pru0_dataram_base)
&& (address <
prussdrv.pru0_dataram_base + prussdrv.pruss_map_size)) {
retaddr =
((unsigned int) (address - prussdrv.pru0_dataram_base) +
prussdrv.pru0_dataram_phy_base);
} else if ((address >= prussdrv.l3ram_base)
&& (address <
prussdrv.l3ram_base + prussdrv.l3ram_map_size)) {
retaddr =
((unsigned int) (address - prussdrv.l3ram_base) +
prussdrv.l3ram_phys_base);
} else if ((address >= prussdrv.extram_base)
&& (address <
prussdrv.extram_base + prussdrv.extram_map_size)) {
retaddr =
((unsigned int) (address - prussdrv.extram_base) +
prussdrv.extram_phys_base);
}
return retaddr;
*/
func (el *eventListener) ClearEvent(eventNum EventNum) (err error) {
switch {
case eventNum < 32:
*el.drv.intcOffsetInt(PRU_INTC_SECR1_REG, 0) = uint32(eventNum)
case eventNum < 64:
*el.drv.intcOffsetInt(PRU_INTC_SECR2_REG, 0) = uint32(eventNum) - 32
default:
return fmt.Errorf("Invalid event num: %d", eventNum)
}
*el.drv.intcOffsetInt(PRU_INTC_HIEISR_REG, 0) = uint32(el.evt) + 2
return
}
func (el *eventListener) consumeEvents() {
defer close(el.out)
buf := make([]byte, 4)
numEventsBuff := (*uint32)(unsafe.Pointer(&buf[0]))
for _ = range el.in {
n, err := el.f.Read(buf)
switch {
case err == nil && n == len(buf):
el.out <- *numEventsBuff
*el.drv.intcOffsetInt(PRU_INTC_HIEISR_REG, 0) = uint32(el.evt) + 2
case err == io.EOF:
return
case err == nil && n != len(buf):
log.Panicln("read bytes not as long as buff. don't know what to do :(")
default:
log.Panicln("error reading stream. error: ", err)
}
}
}
func (el *eventListener) Wait() uint32 {
return <-el.WaitC()
}
func (el *eventListener) WaitC() <-chan uint32 {
el.in <- true
return el.out
}
func (el *eventListener) Close() {
close(el.in)
for _ = range el.out {
}
el.f.Close()
}