Skip to content
This repository was archived by the owner on Dec 24, 2024. It is now read-only.

Commit eb7b9a9

Browse files
committed
eda: introduce Cgo-based EDA driver
1 parent 731d87a commit eb7b9a9

File tree

12 files changed

+3682
-0
lines changed

12 files changed

+3682
-0
lines changed

eda/alt_types.h

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#ifndef __ALT_TYPES_H__
2+
#define __ALT_TYPES_H__
3+
4+
/******************************************************************************
5+
* *
6+
* License Agreement *
7+
* *
8+
* Copyright (c) 2009 Altera Corporation, San Jose, California, USA. *
9+
* All rights reserved. *
10+
* *
11+
* Permission is hereby granted, free of charge, to any person obtaining a *
12+
* copy of this software and associated documentation files (the "Software"), *
13+
* to deal in the Software without restriction, including without limitation *
14+
* the rights to use, copy, modify, merge, publish, distribute, sublicense, *
15+
* and/or sell copies of the Software, and to permit persons to whom the *
16+
* Software is furnished to do so, subject to the following conditions: *
17+
* *
18+
* The above copyright notice and this permission notice shall be included in *
19+
* all copies or substantial portions of the Software. *
20+
* *
21+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
22+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
23+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
24+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
25+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
26+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
27+
* DEALINGS IN THE SOFTWARE. *
28+
* *
29+
* This agreement shall be governed in all respects by the laws of the State *
30+
* of California and by the laws of the United States of America. *
31+
* *
32+
* Altera does not recommend, suggest or require that this reference design *
33+
* file be used in conjunction or combination with any other product. *
34+
******************************************************************************/
35+
36+
/*
37+
* Don't declare these typedefs if this file is included by assembly source.
38+
*/
39+
#ifndef ALT_ASM_SRC
40+
typedef signed char alt_8;
41+
typedef unsigned char alt_u8;
42+
typedef signed short alt_16;
43+
typedef unsigned short alt_u16;
44+
typedef signed long alt_32;
45+
typedef unsigned long alt_u32;
46+
typedef long long alt_64;
47+
typedef unsigned long long alt_u64;
48+
#endif
49+
50+
#define ALT_INLINE __inline__
51+
#define ALT_ALWAYS_INLINE __attribute__((always_inline))
52+
#define ALT_WEAK __attribute__((weak))
53+
54+
#endif /* __ALT_TYPES_H__ */

eda/altera_avalon_fifo_regs.h

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/******************************************************************************
2+
* *
3+
* License Agreement *
4+
* *
5+
* Copyright (c) 2006 Altera Corporation, San Jose, California, USA. *
6+
* All rights reserved. *
7+
* *
8+
* Permission is hereby granted, free of charge, to any person obtaining a *
9+
* copy of this software and associated documentation files (the "Software"), *
10+
* to deal in the Software without restriction, including without limitation *
11+
* the rights to use, copy, modify, merge, publish, distribute, sublicense, *
12+
* and/or sell copies of the Software, and to permit persons to whom the *
13+
* Software is furnished to do so, subject to the following conditions: *
14+
* *
15+
* The above copyright notice and this permission notice shall be included in *
16+
* all copies or substantial portions of the Software. *
17+
* *
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
23+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
24+
* DEALINGS IN THE SOFTWARE. *
25+
* *
26+
* This agreement shall be governed in all respects by the laws of the State *
27+
* of California and by the laws of the United States of America. *
28+
* *
29+
******************************************************************************/
30+
31+
#ifndef __ALTERA_AVALON_FIFO_REGS_H__
32+
#define __ALTERA_AVALON_FIFO_REGS_H__
33+
34+
#include <io.h>
35+
36+
#define ALTERA_AVALON_FIFO_OTHER_INFO_REG 1
37+
#define ALTERA_AVALON_FIFO_DATA_REG 0
38+
39+
#define ALTERA_AVALON_FIFO_LEVEL_REG 0
40+
#define ALTERA_AVALON_FIFO_STATUS_REG 1
41+
#define ALTERA_AVALON_FIFO_EVENT_REG 2
42+
#define ALTERA_AVALON_FIFO_IENABLE_REG 3
43+
#define ALTERA_AVALON_FIFO_ALMOSTFULL_REG 4
44+
#define ALTERA_AVALON_FIFO_ALMOSTEMPTY_REG 5
45+
46+
// Read slave
47+
#define IORD_ALTERA_AVALON_FIFO_DATA(base) \
48+
IORD(base, ALTERA_AVALON_FIFO_DATA_REG)
49+
50+
#define IORD_ALTERA_AVALON_FIFO_OTHER_INFO(base) \
51+
IORD(base, ALTERA_AVALON_FIFO_OTHER_INFO_REG)
52+
53+
// Write slave
54+
#define IOWR_ALTERA_AVALON_FIFO_DATA(base, data) \
55+
IOWR(base, ALTERA_AVALON_FIFO_DATA_REG, data)
56+
57+
#define IOWR_ALTERA_AVALON_FIFO_OTHER_INFO(base, data) \
58+
IOWR(base, ALTERA_AVALON_FIFO_OTHER_INFO_REG, data)
59+
60+
// Control slave
61+
#define IORD_ALTERA_AVALON_FIFO_LEVEL(base) \
62+
IORD(base, ALTERA_AVALON_FIFO_LEVEL_REG)
63+
64+
#define IORD_ALTERA_AVALON_FIFO_STATUS(base) \
65+
IORD(base, ALTERA_AVALON_FIFO_STATUS_REG)
66+
67+
#define IORD_ALTERA_AVALON_FIFO_EVENT(base) \
68+
IORD(base, ALTERA_AVALON_FIFO_EVENT_REG)
69+
70+
#define IORD_ALTERA_AVALON_FIFO_IENABLE(base) \
71+
IORD(base, ALTERA_AVALON_FIFO_IENABLE_REG)
72+
73+
#define IORD_ALTERA_AVALON_FIFO_ALMOSTFULL(base) \
74+
IORD(base, ALTERA_AVALON_FIFO_ALMOSTFULL_REG)
75+
76+
#define IORD_ALTERA_AVALON_FIFO_ALMOSTEMPTY(base) \
77+
IORD(base, ALTERA_AVALON_FIFO_ALMOSTEMPTY_REG)
78+
79+
#define IOWR_ALTERA_AVALON_FIFO_EVENT(base, data) \
80+
IOWR(base, ALTERA_AVALON_FIFO_EVENT_REG, data)
81+
82+
#define IOWR_ALTERA_AVALON_FIFO_IENABLE(base, data) \
83+
IOWR(base, ALTERA_AVALON_FIFO_IENABLE_REG, data)
84+
85+
#define IOWR_ALTERA_AVALON_FIFO_ALMOSTFULL(base, data) \
86+
IOWR(base, ALTERA_AVALON_FIFO_ALMOSTFULL_REG, data)
87+
88+
#define IOWR_ALTERA_AVALON_FIFO_ALMOSTEMPTY(base, data) \
89+
IOWR(base, ALTERA_AVALON_FIFO_ALMOSTEMPTY_REG, data)
90+
91+
#define ALTERA_AVALON_FIFO_EVENT_F_MSK (0x01)
92+
#define ALTERA_AVALON_FIFO_EVENT_E_MSK (0x02)
93+
#define ALTERA_AVALON_FIFO_EVENT_AF_MSK (0x04)
94+
#define ALTERA_AVALON_FIFO_EVENT_AE_MSK (0x08)
95+
#define ALTERA_AVALON_FIFO_EVENT_OVF_MSK (0x10)
96+
#define ALTERA_AVALON_FIFO_EVENT_UDF_MSK (0x20)
97+
#define ALTERA_AVALON_FIFO_EVENT_ALL (0x3F)
98+
99+
#define ALTERA_AVALON_FIFO_STATUS_F_MSK (0x01)
100+
#define ALTERA_AVALON_FIFO_STATUS_E_MSK (0x02)
101+
#define ALTERA_AVALON_FIFO_STATUS_AF_MSK (0x04)
102+
#define ALTERA_AVALON_FIFO_STATUS_AE_MSK (0x08)
103+
#define ALTERA_AVALON_FIFO_STATUS_OVF_MSK (0x10)
104+
#define ALTERA_AVALON_FIFO_STATUS_UDF_MSK (0x20)
105+
#define ALTERA_AVALON_FIFO_STATUS_ALL (0x3F)
106+
107+
#define ALTERA_AVALON_FIFO_IENABLE_F_MSK (0x01)
108+
#define ALTERA_AVALON_FIFO_IENABLE_E_MSK (0x02)
109+
#define ALTERA_AVALON_FIFO_IENABLE_AF_MSK (0x04)
110+
#define ALTERA_AVALON_FIFO_IENABLE_AE_MSK (0x08)
111+
#define ALTERA_AVALON_FIFO_IENABLE_OVF_MSK (0x10)
112+
#define ALTERA_AVALON_FIFO_IENABLE_UDF_MSK (0x20)
113+
#define ALTERA_AVALON_FIFO_IENABLE_ALL (0x3F)
114+
115+
#endif /* __ALTERA_AVALON_FIFO_REGS_H__ */

eda/cdevice.go

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
// Copyright 2021 The go-lpc Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package eda
6+
7+
/** TO DO LIST
8+
*
9+
* change HR_id numbering (1 to 8 instead of 0 to 7) (+ processing script)
10+
* change trigger_id numbering (start 1) (+ processing script)
11+
*
12+
*/
13+
14+
//#cgo CFLAGS: -g -Wall -std=c99 -D_GNU_SOURCE=1 -I.
15+
//#cgo CFLAGS arm: -I/build/soc_eds/ip/altera/hps/altera_hps/hwlib/include
16+
//#cgo LDFLAGS arm: -static
17+
//
18+
//#include <stdlib.h>
19+
//#include <string.h>
20+
//#include <stdint.h>
21+
//#include "device.h"
22+
import "C"
23+
24+
import (
25+
"fmt"
26+
"net"
27+
"strconv"
28+
"time"
29+
"unsafe"
30+
31+
"github.com/go-lpc/mim/conddb"
32+
)
33+
34+
type cdevice struct {
35+
ctx *C.Device_t
36+
37+
done chan int // signal to stop DAQ
38+
}
39+
40+
var _ device = (*cdevice)(nil)
41+
42+
func newCDevice(devmem, odir, devshm string, opts ...Option) (*cdevice, error) {
43+
ctx := C.new_device()
44+
if ctx == nil {
45+
return nil, fmt.Errorf("ceda: could not create EDA device")
46+
}
47+
48+
dev := &cdevice{
49+
ctx: ctx,
50+
}
51+
52+
return dev, nil
53+
}
54+
55+
func (dev *cdevice) Close() error {
56+
if dev.ctx == nil {
57+
return nil
58+
}
59+
C.device_free(dev.ctx)
60+
dev.ctx = nil
61+
return nil
62+
}
63+
64+
func (dev *cdevice) Boot(cfg []conddb.RFM) error {
65+
for _, rfm := range cfg {
66+
rc := C.device_boot_rfm(
67+
dev.ctx,
68+
C.uint8_t(rfm.ID), C.int(rfm.Slot),
69+
C.uint32_t(rfm.DAQ.RShaper),
70+
C.uint32_t(rfm.DAQ.TriggerMode),
71+
)
72+
if rc != 0 {
73+
return fmt.Errorf(
74+
"ceda: could not boot RFM=%d slot=%d: err=%v",
75+
rfm.ID, rfm.Slot, rc,
76+
)
77+
}
78+
}
79+
return nil
80+
}
81+
82+
func (dev *cdevice) ConfigureDIF(addr string, dif uint8, asics []conddb.ASIC) error {
83+
for i, asic := range asics {
84+
var (
85+
cfg = asic.HRConfig()
86+
ihr = uint32(i)
87+
n = len(cfg)
88+
)
89+
for i, v := range cfg {
90+
dev.hrscSetBit(ihr, uint32(n-1-i), uint32(v))
91+
}
92+
}
93+
94+
host, port_, err := net.SplitHostPort(addr)
95+
if err != nil {
96+
return fmt.Errorf(
97+
"ceda: could not infer rfm-socket host/port from %q: %w",
98+
addr, err,
99+
)
100+
}
101+
102+
c_addr := C.CString(host)
103+
defer C.free(unsafe.Pointer(c_addr))
104+
105+
port, err := strconv.Atoi(port_)
106+
if err != nil {
107+
return fmt.Errorf(
108+
"ceda: could not infer rfm-socket port from %q: %w", addr, err,
109+
)
110+
}
111+
112+
rc := C.device_configure_dif(dev.ctx, C.uint8_t(dif), c_addr, C.int(port))
113+
if rc != 0 {
114+
return fmt.Errorf("ceda: could not configure EDA-DIF=%d: rc=%d", dif, rc)
115+
}
116+
117+
return nil
118+
}
119+
120+
func (dev *cdevice) Initialize() error {
121+
rc := C.device_initialize(dev.ctx)
122+
if rc != 0 {
123+
return fmt.Errorf("ceda: could not initialize EDA device rc=%d", rc)
124+
}
125+
return nil
126+
}
127+
128+
func (dev *cdevice) Start(run uint32) error {
129+
rc := C.device_start(dev.ctx, C.uint32_t(run))
130+
if rc != 0 {
131+
return fmt.Errorf("ceda: could not start EDA device: rc=%d", rc)
132+
}
133+
go dev.loop()
134+
return nil
135+
}
136+
137+
func (dev *cdevice) loop() {
138+
dev.done = make(chan int)
139+
defer close(dev.done)
140+
141+
C.device_loop(dev.ctx)
142+
}
143+
144+
func (dev *cdevice) Stop() error {
145+
const timeout = 10 * time.Second
146+
tck := time.NewTimer(timeout)
147+
defer tck.Stop()
148+
149+
C.device_stop_loop(dev.ctx)
150+
151+
select {
152+
case <-dev.done:
153+
case <-tck.C:
154+
return fmt.Errorf("ceda: could not stop DAQ (timeout=%v)", timeout)
155+
}
156+
157+
rc := C.device_stop(dev.ctx)
158+
if rc != 0 {
159+
return fmt.Errorf("ceda: could not stop EDA device: rc=%d", rc)
160+
}
161+
return nil
162+
}
163+
164+
func (dev *cdevice) hrscSetBit(hr, addr, bit uint32) {
165+
C.HRSC_set_bit(C.alt_u32(hr), C.alt_u32(addr), C.alt_u32(bit))
166+
}

0 commit comments

Comments
 (0)