-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCY22150_freq_explorer.cpp
211 lines (195 loc) · 4.57 KB
/
CY22150_freq_explorer.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
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
// compile using: g++ CY22150_freq_explorer.cpp CY22150_lib.c -I./ -o CY22150_freq_explorer
// compile using: g++ -D_WIN32 CY22150_freq_explorer.cpp CY22150_lib.c -I./ -o CY22150_freq_explorer.exe
#include "CY22150_lib.h"
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <ctype.h>
#include <vector>
using namespace std;
#define PRINT 0
struct CONFIG_CY22150 {
int CLKOE_CLK6;
int CLKOE_CLK5;
int CLKOE_LCLK4;
int CLKOE_LCLK3;
int CLKOE_LCLK2;
int CLKOE_LCLK1;
int DIV1SRC;
int DIV1N;
int PB;
int PO;
int Q;
int CLKSRC_LCLK1;
int CLKSRC_LCLK2;
int CLKSRC_LCLK3;
int CLKSRC_LCLK4;
int CLKSRC_CLK5;
int CLKSRC_CLK6;
int DIV2SRC;
int DIV2N;
float fFreq;
};
vector<CONFIG_CY22150> configs;
int calculate_and_output() {
if(!chk_pll(PRINT)) return 0;
if(!chk_div(PRINT)) return 0;
CAPLOAD = CAPLOAD_det(IS_EXTERNAL, (float)CapLoad(XCL, 2, 6), PRINT);
XDRV = XDRV_det(IS_EXTERNAL, REF, CAPLOAD, IS_ESR_30, PRINT);
PUMP = PUMP_det(PRINT);
float fFreq = det_out_freq(1, PRINT);
configs.push_back(
(CONFIG_CY22150){
CLKOE_CLK6,
CLKOE_CLK5,
CLKOE_LCLK4,
CLKOE_LCLK3,
CLKOE_LCLK2,
CLKOE_LCLK1,
DIV1SRC,
DIV1N,
PB,
PO,
Q,
CLKSRC_LCLK1,
CLKSRC_LCLK2,
CLKSRC_LCLK3,
CLKSRC_LCLK4,
CLKSRC_CLK5,
CLKSRC_CLK6,
DIV2SRC,
DIV2N,
fFreq});
return 1;
}
int iterate_internal() {
// Div in 2
DIV1N = 4;
CLKSRC_LCLK1 = 2;
calculate_and_output();
// DIV in 3
DIV1N = 6;
CLKSRC_LCLK1 = 3;
calculate_and_output();
// Div in 4 ~ 127
CLKSRC_LCLK1 = 1;
for(DIV1N = 4; DIV1N < 128; DIV1N++) {
calculate_and_output();
}
}
int main(int argc, char** argv) {
printf("Calculating frequencies...\n");
// With DIV1SRC in VCO
DIV1SRC = 0;
for(Q = 0; Q < (1 << 7); Q++) {
for(PB = 4; PB < (1 << 10); PB++) {
for(PO = 0; PO < 2; PO++) {
iterate_internal();
}
}
}
// With DIV1SRC in REF
PB = 4;
PO = 0;
Q = 1;
DIV1SRC = 1;
iterate_internal();
for(;;)
{
float fFreq = 0.0;
printf("Frequency? [in KHz]: ");
while(scanf("%g", &fFreq) != 1) {
printf("Not valid.\n");
char c = '0';
do {
c = getchar();
}
while (!isdigit(c));
ungetc(c, stdin);
}
float fdif = fabs(configs.begin()->fFreq - fFreq);
vector<CONFIG_CY22150>::iterator best = configs.begin();
for(vector<CONFIG_CY22150>::iterator it = configs.begin(); it != configs.end(); it++) {
float dif = fabs(it->fFreq - fFreq);
if(dif < fdif) {
fdif = dif;
best = it;
}
}
PB = best->PB;
PO = best->PO;
Q = best->Q;
DIV1SRC = best->DIV1SRC;
DIV1N = best->DIV1N;
DIV2SRC = best->DIV2SRC;
DIV2N = best->DIV2N;
CLKOE_CLK6 = best->CLKOE_CLK6;
CLKOE_CLK5 = best->CLKOE_CLK5;
CLKOE_LCLK4 = best->CLKOE_LCLK4;
CLKOE_LCLK3 = best->CLKOE_LCLK3;
CLKOE_LCLK2 = best->CLKOE_LCLK2;
CLKOE_LCLK1 = best->CLKOE_LCLK1;
CLKSRC_LCLK1 = best->CLKSRC_LCLK1;
CLKSRC_LCLK2 = best->CLKSRC_LCLK2;
CLKSRC_LCLK3 = best->CLKSRC_LCLK3;
CLKSRC_LCLK4 = best->CLKSRC_LCLK4;
CLKSRC_CLK5 = best->CLKSRC_CLK5;
CLKSRC_CLK6 = best->CLKSRC_CLK6;
printf("Best match: \n");
printf("%d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %g\n",
PB,
PO,
Q,
DIV1SRC,
DIV1N,
DIV2SRC,
DIV2N,
CLKOE_CLK6,
CLKOE_CLK5,
CLKOE_LCLK4,
CLKOE_LCLK3,
CLKOE_LCLK2,
CLKOE_LCLK1,
CLKSRC_LCLK1,
CLKSRC_LCLK2,
CLKSRC_LCLK3,
CLKSRC_LCLK4,
CLKSRC_CLK5,
CLKSRC_CLK6,
best->fFreq);
printf("Checkings:\n");
if(chk_pll(1)) if(chk_div(1)) {
CAPLOAD = CAPLOAD_det(IS_EXTERNAL, (float)CapLoad(XCL, 2, 6), 1);
XDRV = XDRV_det(IS_EXTERNAL, REF, CAPLOAD, IS_ESR_30, 1);
PUMP = PUMP_det(1);
}
printf("Invoking...\n");
char cmd[256];
#ifdef _WIN32
sprintf(cmd, "./CY22150_prog.exe %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %g",
#else
sprintf(cmd, "./CY22150_prog %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %g",
#endif
PB,
PO,
Q,
DIV1SRC,
DIV1N,
DIV2SRC,
DIV2N,
CLKOE_CLK6,
CLKOE_CLK5,
CLKOE_LCLK4,
CLKOE_LCLK3,
CLKOE_LCLK2,
CLKOE_LCLK1,
CLKSRC_LCLK1,
CLKSRC_LCLK2,
CLKSRC_LCLK3,
CLKSRC_LCLK4,
CLKSRC_CLK5,
CLKSRC_CLK6);
system(cmd);
}
return 0;
}