forked from sanni/cartreader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
clkcal.ino
221 lines (198 loc) · 5.43 KB
/
clkcal.ino
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
//******************************************
// Clock Calibration Module
//******************************************
#ifdef clockgen_calibration
#include <FreqCount.h>
#include "snes_clk.h"
#include "SdFat.h"
/******************************************
Variables
*****************************************/
int32_t cal_factor = 0;
int32_t old_cal = 0;
int32_t cal_offset = 100;
/******************************************
Clock Calibration
*****************************************/
void clkcal() {
// Adafruit Clock Generator
// last number is the clock correction factor which is custom for each clock generator
cal_factor = readClockOffset();
display.clearDisplay();
display.setCursor(0, 0);
display.print("Read correction: ");
display.println(cal_factor);
display.display();
delay(500);
if (cal_factor > INT32_MIN) {
i2c_found = clockgen.init(SI5351_CRYSTAL_LOAD_8PF, 0, cal_factor);
} else {
i2c_found = clockgen.init(SI5351_CRYSTAL_LOAD_8PF, 0, 0);
cal_factor = 0;
}
if (!i2c_found) {
display_Clear();
print_Error(F("Clock Generator not found"), true);
}
//clockgen.set_correction(cal_factor, SI5351_PLL_INPUT_XO);
clockgen.set_pll(SI5351_PLL_FIXED, SI5351_PLLA);
clockgen.set_pll(SI5351_PLL_FIXED, SI5351_PLLB);
//clockgen.pll_reset(SI5351_PLLA);
//clockgen.pll_reset(SI5351_PLLB);
clockgen.set_freq(400000000ULL, SI5351_CLK0);
clockgen.set_freq(100000000ULL, SI5351_CLK1);
clockgen.set_freq(307200000ULL, SI5351_CLK2);
clockgen.output_enable(SI5351_CLK1, 1);
clockgen.output_enable(SI5351_CLK2, 1);
clockgen.output_enable(SI5351_CLK0, 1);
// Frequency Counter
delay(500);
FreqCount.begin(1000);
while (1)
{
if (old_cal != cal_factor) {
display_Clear();
println_Msg(F(""));
println_Msg(F(""));
println_Msg(F(""));
println_Msg(F(""));
println_Msg(F(" Adjusting"));
display_Update();
clockgen.set_correction(cal_factor, SI5351_PLL_INPUT_XO);
clockgen.set_pll(SI5351_PLL_FIXED, SI5351_PLLA);
clockgen.set_pll(SI5351_PLL_FIXED, SI5351_PLLB);
clockgen.pll_reset(SI5351_PLLA);
clockgen.pll_reset(SI5351_PLLB);
clockgen.set_freq(400000000ULL, SI5351_CLK0);
clockgen.set_freq(100000000ULL, SI5351_CLK1);
clockgen.set_freq(307200000ULL, SI5351_CLK2);
old_cal = cal_factor;
delay(500);
}
else {
clockgen.update_status();
while (clockgen.dev_status.SYS_INIT == 1) {
}
if (FreqCount.available()) {
float count = FreqCount.read();
display_Clear();
println_Msg(F("Clock Calibration"));
println_Msg(F(""));
print_Msg(F("Freq: "));
print_Msg(count);
println_Msg(F("Hz"));
print_Msg(F("Correction:"));
print_right(cal_factor);
print_Msg(F("Adjustment:"));
print_right(cal_offset);
#ifdef enable_Button2
println_Msg(F("(Hold button to save)"));
println_Msg(F(""));
println_Msg(F("Decrease Increase"));
#else
#ifdef enable_rotary
println_Msg(F("Rotate to adjust"));
#else
println_Msg(F("Click/dbl to adjust"));
#endif
#endif
display_Update();
}
#ifdef enable_Button2
// get input button
int a = checkButton1();
int b = checkButton2();
// if the cart readers input button is pressed shortly
if (a == 1) {
old_cal = cal_factor;
cal_factor -= cal_offset;
}
if (b == 1) {
old_cal = cal_factor;
cal_factor += cal_offset;
}
// if the cart readers input buttons is double clicked
if (a == 2) {
cal_offset /= 10ULL;
if (cal_offset < 1)
{
cal_offset = 100000000ULL;
}
}
if (b == 2) {
cal_offset *= 10ULL;
if (cal_offset > 100000000ULL)
{
cal_offset = 1;
}
}
// if the cart readers input button is pressed long
if (a == 3) {
savetofile();
}
if (b == 3) {
savetofile();
}
#else
//Handle inputs for either rotary encoder or single button interface.
int a = checkButton();
if (a == 1) { //clockwise rotation or single click
old_cal = cal_factor;
cal_factor += cal_offset;
}
if (a == 2) { //counterclockwise rotation or double click
old_cal = cal_factor;
cal_factor -= cal_offset;
}
if (a == 3) { //button short hold
cal_offset *= 10ULL;
if (cal_offset > 100000000ULL)
{
cal_offset = 1;
}
}
if (a == 4) { //button long hold
savetofile();
}
#endif
}
}
}
void print_right(int32_t number)
{
int32_t abs_number = number;
if (abs_number < 0)
abs_number *= -1;
else
print_Msg(F(" "));
if (abs_number == 0)
abs_number = 1;
while (abs_number < 100000000ULL)
{
print_Msg(F(" "));
abs_number *= 10ULL;
}
println_Msg(number);
}
void savetofile() {
display_Clear();
println_Msg(F("Saving..."));
println_Msg(cal_factor);
display_Update();
delay(2000);
if (!myFile.open("/snes_clk.txt", O_WRITE | O_CREAT | O_TRUNC)) {
print_Error(F("SD Error"), true);
}
// Write calibration factor to file
myFile.print(cal_factor);
// Close the file:
myFile.close();
println_Msg(F("Done"));
display_Update();
delay(1000);
resetArduino();
}
#endif
//******************************************
// End of File
//******************************************