forked from ngdxzy/PYNQ_Boost_Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGTH_Vernier.cpp
More file actions
191 lines (173 loc) · 3.59 KB
/
GTH_Vernier.cpp
File metadata and controls
191 lines (173 loc) · 3.59 KB
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
#include <stdio.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <fcntl.h>
#include <stdint.h>
#include <stdlib.h>
#include <unistd.h>
#include <boost/python.hpp>
using namespace boost::python;
using namespace std;
namespace bp = boost::python;
typedef unsigned char u8;
typedef unsigned short u16;
typedef unsigned int u32;
typedef unsigned long u64;
typedef struct{
volatile u32 data[448];
}Vernier_Data;
typedef struct{
u32 en;
u32 average;
u32 phase_counter;
u8 r_id;
u8 w_id;
u8 reserved0;
u8 reserved1;
u32 w_addr;
u32 GTH_DATA;
u32 reserved3;
u32 reserved4;
u32 data[8];
}Vernier_Ctrl;
char buffer[128];
inline void ttyPrint(char* s)
{
sprintf(buffer,"echo %s > /dev/ttyPS0",s);
system(buffer);
}
class GTH_System {
private:
public:
unsigned int mem_fd;
Vernier_Data* GTH_Data;
Vernier_Ctrl* GTH_Ctrl;
int shift_counter;
GTH_System(u32 ctrl_addr,u32 data_addr)
{
mem_fd = open("/dev/mem", O_RDWR);
GTH_Data = (Vernier_Data*)mmap(0x00,sizeof(Vernier_Data) , PROT_READ | PROT_WRITE, MAP_SHARED, mem_fd, data_addr);
GTH_Ctrl = (Vernier_Ctrl*)mmap(0x00,sizeof(Vernier_Ctrl) , PROT_READ | PROT_WRITE, MAP_SHARED, mem_fd, ctrl_addr);
}
~GTH_System()
{
munmap(GTH_Data,sizeof(Vernier_Data));
munmap(GTH_Ctrl,sizeof(Vernier_Ctrl));
close(mem_fd);
}
u32 read_bare_data(u8 i)
{
if(i < 8)
{
return GTH_Ctrl->data[i];
}
else
{
return -1;
}
}
u32 read_data(u32 i)
{
if(i < 448)
{
return GTH_Data->data[i];
}
else
{
return -1;
}
}
void start_capture(const char* name)
{
FILE* fp;
int j;
fp = fopen(name,"wb");
if(fp == NULL)
return ;
for (j = 0;j < 448;j++)
fprintf(fp,"%d\n",GTH_Data->data[j]);
fflush(fp);
fclose(fp);
return ;
}
void send_out_package()
{
char buffer[128];
int j;
ttyPrint("start:");
for (j = 0;j < 448;j++)
{
sprintf(buffer,"%d",GTH_Data->data[j]);
ttyPrint(buffer);
}
}
void set_gth_data(u32 d)
{
GTH_Ctrl->GTH_DATA = d;
}
u32 get_gth_data()
{
return GTH_Ctrl->GTH_DATA;
}
void enable()
{
GTH_Ctrl->en = 1;
}
void disable()
{
GTH_Ctrl->en = 0;
}
void set_average(u32 average)
{
GTH_Ctrl->average = average;
}
u8 get_wid()
{
return GTH_Ctrl->w_id;
}
u8 get_rid()
{
return GTH_Ctrl->r_id;
}
u32 get_phasecounter()
{
return GTH_Ctrl->phase_counter;
}
void capture_to_numpy(boost::python::object obj)
{
PyObject* pobj = obj.ptr();
Py_buffer pybuf;
if(PyObject_GetBuffer(pobj, &pybuf, PyBUF_SIMPLE)!=-1)
{
void *buf = pybuf.buf;
u32 *p = (u32*)buf;
u32 *q = (u32*)GTH_Data->data;
for (int j = 0;j < 448;j++)
*p++ = *q++;
PyBuffer_Release(&pybuf);
}
}
u32 get_average(u32 average)
{
return GTH_Ctrl->average;
}
};
BOOST_PYTHON_MODULE(GTH_Vernier)
{
class_<GTH_System>("GTH_System", init<u32,u32>())
.def(init<u32,u32>())
.def("start_capture",>H_System::start_capture)
.def("read_data",>H_System::read_data)
.def("read_bare_data",>H_System::read_bare_data)
.def("enable",>H_System::enable)
.def("disable",>H_System::disable)
.def("set_average",>H_System::set_average)
.def("set_gth_data",>H_System::set_gth_data)
.def("get_gth_data",>H_System::get_gth_data)
.def("send_out_package",>H_System::send_out_package)
.def("capture_to_numpy",>H_System::capture_to_numpy)
.def("get_wid",>H_System::get_wid)
.def("get_rid",>H_System::get_rid)
.def_readonly("mem_fd",>H_System::mem_fd)
;
}