-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvna_ad9951.cpp
155 lines (128 loc) · 2.79 KB
/
vna_ad9951.cpp
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
#include "vna_ad9951.h"
#include <iostream>
#include <sys/io.h>
#include <cerrno>
using namespace std;
#define SETBIT(x, b) ((x) |= (b))
#define TESTBIT(x, b) ((x) & (b))
#define CFR1_SIZE 32
#define CFR2_SIZE 24
#define ASF_SIZE 16
#define ARR_SIZE 8
#define FTW0_SIZE 32
#define POW0_SIZE 16
#define IBYTE_SIZE 8
#define CFR1_ADDR 0
#define CFR2_ADDR 1
#define ASF_ADDR 2
#define ARR_ADDR 3
#define FTW0_ADDR 4
#define POW0_ADDR 5
int port9;
VnaAD9951::VnaAD9951()
{
}
VnaAD9951::~VnaAD9951()
{
}
int VnaAD9951::initVna(int port_init, QString device_init)
{
int result;
port9 = port_init;
// cout << port9 << "VNA init" <<endl;
result = ioperm(0x80,3,1);
if (result == 0)
{
result = ioperm(port9,3,1);
if (result == 0)
initDDS();
}
return (result);
}
void VnaAD9951::initMeasure(unsigned int start, unsigned int step, int points, int mode)
{
setToTransmittance(mode == 1);
}
void VnaAD9951::setFrequency(unsigned int n)
{
cout << port9 << "VNA set to:" << n << endl;
sendCommand(FTW0_ADDR, FTW0_SIZE, n);
}
void VnaAD9951::getData(int n, int *return_loss, int *angle, int *gain1, int *gain2, int *gain3)
{
char byte;
int bit;
*angle = 0;
*return_loss = 0;
outb(56, port9); // 111000
for (bit=512; bit>0; bit >>=1) {
outb_p(32, port9); // 00000 -- 32 = 100000
byte = inb(port9+1);
// cout << "byte:" << byte << endl;
if (TESTBIT(byte,16))
SETBIT(*angle,bit);
if (TESTBIT(byte,32))
SETBIT(*return_loss,bit);
outb_p(40, port9); // 01000 clock AD -- 40 = 101000
outb(32, port9); // 00000 -- 32 = 100000
}
outb(56, port9); // 111000
}
void VnaAD9951::closePort()
{
}
int VnaAD9951::getError()
{
return (errno);
}
void VnaAD9951::setToTransmittance(bool transm)
{
if (transm)
outb(0x00,port9+2);
else
outb(0x01,port9+2);
}
void VnaAD9951::initDDS()
{
sendCommand(CFR2_ADDR, CFR2_SIZE, 132);
sendCommand(POW0_ADDR, POW0_SIZE, 0);
}
void VnaAD9951::sendCommand(int command, int size, unsigned int data)
{
int i;
unsigned int k;
i = IBYTE_SIZE;
k = 1;
k <<= (IBYTE_SIZE - 1);
do {
(command & k) ? send1() : send0();
command <<= 1;
} while (--i);
i = size;
k = 1;
k <<= (size -1);
do {
(data & k) ? send1() : send0();
data >>= 1;
} while (--i);
send1();
sendLE();
}
void VnaAD9951::send0()
{
outb(0x18, port9);
outb(0x1A, port9);
outb(0x18, port9);
}
void VnaAD9951::send1()
{
outb(0x19, port9);
outb(0x1B, port9);
outb(0x19, port9);
}
void VnaAD9951::sendLE()
{
outb(0x18, port9);
outb(0x1C, port9);
//outb(0x18, port9);
}