-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsi5351.cpp
1830 lines (1595 loc) · 41.7 KB
/
si5351.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* si5351.cpp - Si5351 library for Arduino
*
* Copyright (C) 2015 - 2019 Jason Milldrum <milldrum@gmail.com>
* Dana H. Myers <k6jq@comcast.net>
*
* Some tuning algorithms derived from clk-si5351.c in the Linux kernel.
* Sebastian Hesselbarth <sebastian.hesselbarth@gmail.com>
* Rabeeh Khoury <rabeeh@solid-run.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <linux/i2c-dev.h>
#include "si5351.h"
int selectDevice(int fd, int addr, char *name)
{
int s;
char str[128];
s = ioctl(fd, I2C_SLAVE, addr);
if (s == -1)
{
sprintf(str, "selectDevice for %s", name);
perror(str);
}
return s;
}
/********************/
/* Public functions */
/********************/
Si5351::Si5351(uint8_t i2c_addr):
i2c_bus_addr(i2c_addr)
{
xtal_freq[0] = SI5351_XTAL_FREQ;
// Start by using XO ref osc as default for each PLL
plla_ref_osc = SI5351_PLL_INPUT_XO;
pllb_ref_osc = SI5351_PLL_INPUT_XO;
clkin_div = SI5351_CLKIN_DIV_1;
}
/*
* init(uint8_t xtal_load_c, uint32_t ref_osc_freq, int32_t corr)
*
* Setup communications to the Si5351 and set the crystal
* load capacitance.
*
* xtal_load_c - Crystal load capacitance. Use the SI5351_CRYSTAL_LOAD_*PF
* defines in the header file
* xo_freq - Crystal/reference oscillator frequency in 1 Hz increments.
* Defaults to 25000000 if a 0 is used here.
* corr - Frequency correction constant in parts-per-billion
*
* Returns a boolean that indicates whether a device was found on the desired
* I2C address.
*
*/
bool Si5351::init(uint8_t xtal_load_c, uint32_t xo_freq, int32_t corr)
{
uint8_t reg_val = 0; // assume I2C hardware is already connected and initialized
unsigned char buf[256];
sprintf(buf, "/dev/i2c-1"); // fix I2C bus to 1 (can be changed later)
if ((fd = open(buf, O_RDWR)) < 0)
{
// Open port for reading and writing
fprintf(stderr, "Failed to open i2c bus /dev/i2c-1\n");
exit(1);
}
/* initialise i2c bus */
selectDevice(fd, SI5351_BUS_BASE_ADDR, "Si5351"); // use default I2C address defined in .h file
if(reg_val == 0)
{
// Wait for SYS_INIT flag to be clear, indicating that device is ready
uint8_t status_reg = 0;
do
{
status_reg = si5351_read(SI5351_DEVICE_STATUS);
} while (status_reg >> 7 == 1);
// Set crystal load capacitance
si5351_write(SI5351_CRYSTAL_LOAD, (xtal_load_c & SI5351_CRYSTAL_LOAD_MASK) | 0b00010010);
// Set up the XO reference frequency
if (xo_freq != 0)
{
set_ref_freq(xo_freq, SI5351_PLL_INPUT_XO);
}
else
{
set_ref_freq(SI5351_XTAL_FREQ, SI5351_PLL_INPUT_XO);
}
// Set the frequency calibration for the XO
set_correction(corr, SI5351_PLL_INPUT_XO);
reset();
return true;
}
else
{
return false;
}
}
/*
* reset(void)
*
* Call to reset the Si5351 to the state initialized by the library.
*
*/
void Si5351::reset(void)
{
// Initialize the CLK outputs according to flowchart in datasheet
// First, turn them off
si5351_write(16, 0x80);
si5351_write(17, 0x80);
si5351_write(18, 0x80);
si5351_write(19, 0x80);
si5351_write(20, 0x80);
si5351_write(21, 0x80);
si5351_write(22, 0x80);
si5351_write(23, 0x80);
// Turn the clocks back on...
si5351_write(16, 0x0c);
si5351_write(17, 0x0c);
si5351_write(18, 0x0c);
si5351_write(19, 0x0c);
si5351_write(20, 0x0c);
si5351_write(21, 0x0c);
si5351_write(22, 0x0c);
si5351_write(23, 0x0c);
// Set PLLA and PLLB to 800 MHz for automatic tuning
set_pll(SI5351_PLL_FIXED, SI5351_PLLA);
set_pll(SI5351_PLL_FIXED, SI5351_PLLB);
// Make PLL to CLK assignments for automatic tuning
pll_assignment[0] = SI5351_PLLA;
pll_assignment[1] = SI5351_PLLA;
pll_assignment[2] = SI5351_PLLA;
pll_assignment[3] = SI5351_PLLA;
pll_assignment[4] = SI5351_PLLA;
pll_assignment[5] = SI5351_PLLA;
pll_assignment[6] = SI5351_PLLB;
pll_assignment[7] = SI5351_PLLB;
set_ms_source(SI5351_CLK0, SI5351_PLLA);
set_ms_source(SI5351_CLK1, SI5351_PLLA);
set_ms_source(SI5351_CLK2, SI5351_PLLA);
set_ms_source(SI5351_CLK3, SI5351_PLLA);
set_ms_source(SI5351_CLK4, SI5351_PLLA);
set_ms_source(SI5351_CLK5, SI5351_PLLA);
set_ms_source(SI5351_CLK6, SI5351_PLLB);
set_ms_source(SI5351_CLK7, SI5351_PLLB);
// Reset the VCXO param
si5351_write(SI5351_VXCO_PARAMETERS_LOW, 0);
si5351_write(SI5351_VXCO_PARAMETERS_MID, 0);
si5351_write(SI5351_VXCO_PARAMETERS_HIGH, 0);
// Then reset the PLLs
pll_reset(SI5351_PLLA);
pll_reset(SI5351_PLLB);
// Set initial frequencies
uint8_t i;
for(i = 0; i < 8; i++)
{
clk_freq[i] = 0;
output_enable((enum si5351_clock)i, 0);
clk_first_set[i] = false;
}
}
/*
* set_freq(uint64_t freq, enum si5351_clock clk)
*
* Sets the clock frequency of the specified CLK output.
* Frequency range of 8 kHz to 150 MHz
*
* freq - Output frequency in Hz
* clk - Clock output
* (use the si5351_clock enum)
*/
uint8_t Si5351::set_freq(uint64_t freq, enum si5351_clock clk)
{
struct Si5351RegSet ms_reg;
uint64_t pll_freq;
uint8_t int_mode = 0;
uint8_t div_by_4 = 0;
uint8_t r_div = 0;
// Check which Multisynth is being set
if((uint8_t)clk <= (uint8_t)SI5351_CLK5)
{
// MS0 through MS5 logic
// ---------------------
// Lower bounds check
if(freq > 0 && freq < SI5351_CLKOUT_MIN_FREQ * SI5351_FREQ_MULT)
{
freq = SI5351_CLKOUT_MIN_FREQ * SI5351_FREQ_MULT;
}
// Upper bounds check
if(freq > SI5351_MULTISYNTH_MAX_FREQ * SI5351_FREQ_MULT)
{
freq = SI5351_MULTISYNTH_MAX_FREQ * SI5351_FREQ_MULT;
}
// If requested freq >100 MHz and no other outputs are already >100 MHz,
// we need to recalculate PLLA and then recalculate all other CLK outputs
// on same PLL
if(freq > (SI5351_MULTISYNTH_SHARE_MAX * SI5351_FREQ_MULT))
{
// Check other clocks on same PLL
uint8_t i;
for(i = 0; i < 6; i++)
{
if(clk_freq[i] > (SI5351_MULTISYNTH_SHARE_MAX * SI5351_FREQ_MULT))
{
if(i != (uint8_t)clk && pll_assignment[i] == pll_assignment[clk])
{
return 1; // won't set if any other clks already >100 MHz
}
}
}
// Enable the output on first set_freq only
if(clk_first_set[(uint8_t)clk] == false)
{
output_enable(clk, 1);
clk_first_set[(uint8_t)clk] = true;
}
// Set the freq in memory
clk_freq[(uint8_t)clk] = freq;
// Calculate the proper PLL frequency
pll_freq = multisynth_calc(freq, 0, &ms_reg);
// Set PLL
set_pll(pll_freq, pll_assignment[clk]);
// Recalculate params for other synths on same PLL
for(i = 0; i < 6; i++)
{
if(clk_freq[i] != 0)
{
if(pll_assignment[i] == pll_assignment[clk])
{
struct Si5351RegSet temp_reg;
uint64_t temp_freq;
// Select the proper R div value
temp_freq = clk_freq[i];
r_div = select_r_div(&temp_freq);
multisynth_calc(temp_freq, pll_freq, &temp_reg);
// If freq > 150 MHz, we need to use DIVBY4 and integer mode
if(temp_freq >= SI5351_MULTISYNTH_DIVBY4_FREQ * SI5351_FREQ_MULT)
{
div_by_4 = 1;
int_mode = 1;
}
else
{
div_by_4 = 0;
int_mode = 0;
}
// Set multisynth registers
set_ms((enum si5351_clock)i, temp_reg, int_mode, r_div, div_by_4);
}
}
}
// Reset the PLL
pll_reset(pll_assignment[clk]);
}
else
{
clk_freq[(uint8_t)clk] = freq;
// Enable the output on first set_freq only
if(clk_first_set[(uint8_t)clk] == false)
{
output_enable(clk, 1);
clk_first_set[(uint8_t)clk] = true;
}
// Select the proper R div value
r_div = select_r_div(&freq);
// Calculate the synth parameters
if(pll_assignment[clk] == SI5351_PLLA)
{
multisynth_calc(freq, plla_freq, &ms_reg);
}
else
{
multisynth_calc(freq, pllb_freq, &ms_reg);
}
// Set multisynth registers
set_ms(clk, ms_reg, int_mode, r_div, div_by_4);
// Reset the PLL
//pll_reset(pll_assignment[clk]);
}
return 0;
}
else
{
// MS6 and MS7 logic
// -----------------
// Lower bounds check
if(freq > 0 && freq < SI5351_CLKOUT67_MIN_FREQ * SI5351_FREQ_MULT)
{
freq = SI5351_CLKOUT_MIN_FREQ * SI5351_FREQ_MULT;
}
// Upper bounds check
if(freq >= SI5351_MULTISYNTH_DIVBY4_FREQ * SI5351_FREQ_MULT)
{
freq = SI5351_MULTISYNTH_DIVBY4_FREQ * SI5351_FREQ_MULT - 1;
}
// If one of CLK6 or CLK7 is already set when trying to set the other,
// we have to ensure that it will also have an integer division ratio
// with the same PLL, otherwise do not set it.
if(clk == SI5351_CLK6)
{
if(clk_freq[7] != 0)
{
if(pllb_freq % freq == 0)
{
if((pllb_freq / freq) % 2 != 0)
{
// Not an even divide ratio, no bueno
return 1;
}
else
{
// Set the freq in memory
clk_freq[(uint8_t)clk] = freq;
// Select the proper R div value
r_div = select_r_div_ms67(&freq);
multisynth67_calc(freq, pllb_freq, &ms_reg);
}
}
else
{
// Not an integer divide ratio, no good
return 1;
}
}
else
{
// No previous assignment, so set PLLB based on CLK6
// Set the freq in memory
clk_freq[(uint8_t)clk] = freq;
// Select the proper R div value
r_div = select_r_div_ms67(&freq);
pll_freq = multisynth67_calc(freq, 0, &ms_reg);
//pllb_freq = pll_freq;
set_pll(pll_freq, SI5351_PLLB);
}
}
else
{
if(clk_freq[6] != 0)
{
if(pllb_freq % freq == 0)
{
if((pllb_freq / freq) % 2 != 0)
{
// Not an even divide ratio, no bueno
return 1;
}
else
{
// Set the freq in memory
clk_freq[(uint8_t)clk] = freq;
// Select the proper R div value
r_div = select_r_div_ms67(&freq);
multisynth67_calc(freq, pllb_freq, &ms_reg);
}
}
else
{
// Not an integer divide ratio, no good
return 1;
}
}
else
{
// No previous assignment, so set PLLB based on CLK7
// Set the freq in memory
clk_freq[(uint8_t)clk] = freq;
// Select the proper R div value
r_div = select_r_div_ms67(&freq);
pll_freq = multisynth67_calc(freq, 0, &ms_reg);
//pllb_freq = pll_freq;
set_pll(pll_freq, pll_assignment[clk]);
}
}
div_by_4 = 0;
int_mode = 0;
// Set multisynth registers (MS must be set before PLL)
set_ms(clk, ms_reg, int_mode, r_div, div_by_4);
return 0;
}
}
/*
* set_freq_manual(uint64_t freq, uint64_t pll_freq, enum si5351_clock clk)
*
* Sets the clock frequency of the specified CLK output using the given PLL
* frequency. You must ensure that the MS is assigned to the correct PLL and
* that the PLL is set to the correct frequency before using this method.
*
* It is important to note that if you use this method, you will have to
* track that all settings are sane yourself.
*
* freq - Output frequency in Hz
* pll_freq - Frequency of the PLL driving the Multisynth in Hz * 100
* clk - Clock output
* (use the si5351_clock enum)
*/
uint8_t Si5351::set_freq_manual(uint64_t freq, uint64_t pll_freq, enum si5351_clock clk)
{
struct Si5351RegSet ms_reg;
uint8_t int_mode = 0;
uint8_t div_by_4 = 0;
// Lower bounds check
if(freq > 0 && freq < SI5351_CLKOUT_MIN_FREQ * SI5351_FREQ_MULT)
{
freq = SI5351_CLKOUT_MIN_FREQ * SI5351_FREQ_MULT;
}
// Upper bounds check
if(freq > SI5351_CLKOUT_MAX_FREQ * SI5351_FREQ_MULT)
{
freq = SI5351_CLKOUT_MAX_FREQ * SI5351_FREQ_MULT;
}
uint8_t r_div;
clk_freq[(uint8_t)clk] = freq;
set_pll(pll_freq, pll_assignment[clk]);
// Enable the output
output_enable(clk, 1);
// Select the proper R div value
r_div = select_r_div(&freq);
// Calculate the synth parameters
multisynth_calc(freq, pll_freq, &ms_reg);
// If freq > 150 MHz, we need to use DIVBY4 and integer mode
if(freq >= SI5351_MULTISYNTH_DIVBY4_FREQ * SI5351_FREQ_MULT)
{
div_by_4 = 1;
int_mode = 1;
}
// Set multisynth registers (MS must be set before PLL)
set_ms(clk, ms_reg, int_mode, r_div, div_by_4);
return 0;
}
/*
* set_pll(uint64_t pll_freq, enum si5351_pll target_pll)
*
* Set the specified PLL to a specific oscillation frequency
*
* pll_freq - Desired PLL frequency in Hz * 100
* target_pll - Which PLL to set
* (use the si5351_pll enum)
*/
void Si5351::set_pll(uint64_t pll_freq, enum si5351_pll target_pll)
{
struct Si5351RegSet pll_reg;
if(target_pll == SI5351_PLLA)
{
pll_calc(SI5351_PLLA, pll_freq, &pll_reg, ref_correction[plla_ref_osc], 0);
}
else
{
pll_calc(SI5351_PLLB, pll_freq, &pll_reg, ref_correction[pllb_ref_osc], 0);
}
// Derive the register values to write
// Prepare an array for parameters to be written to
uint8_t *params = new uint8_t[20];
uint8_t i = 0;
uint8_t temp;
// Registers 26-27
temp = ((pll_reg.p3 >> 8) & 0xFF);
params[i++] = temp;
temp = (uint8_t)(pll_reg.p3 & 0xFF);
params[i++] = temp;
// Register 28
temp = (uint8_t)((pll_reg.p1 >> 16) & 0x03);
params[i++] = temp;
// Registers 29-30
temp = (uint8_t)((pll_reg.p1 >> 8) & 0xFF);
params[i++] = temp;
temp = (uint8_t)(pll_reg.p1 & 0xFF);
params[i++] = temp;
// Register 31
temp = (uint8_t)((pll_reg.p3 >> 12) & 0xF0);
temp += (uint8_t)((pll_reg.p2 >> 16) & 0x0F);
params[i++] = temp;
// Registers 32-33
temp = (uint8_t)((pll_reg.p2 >> 8) & 0xFF);
params[i++] = temp;
temp = (uint8_t)(pll_reg.p2 & 0xFF);
params[i++] = temp;
// Write the parameters
if(target_pll == SI5351_PLLA)
{
si5351_write_bulk(SI5351_PLLA_PARAMETERS, i, params);
plla_freq = pll_freq;
}
else if(target_pll == SI5351_PLLB)
{
si5351_write_bulk(SI5351_PLLB_PARAMETERS, i, params);
pllb_freq = pll_freq;
}
delete params;
}
/*
* set_ms(enum si5351_clock clk, struct Si5351RegSet ms_reg, uint8_t int_mode, uint8_t r_div, uint8_t div_by_4)
*
* Set the specified multisynth parameters. Not normally needed, but public for advanced users.
*
* clk - Clock output
* (use the si5351_clock enum)
* int_mode - Set integer mode
* Set to 1 to enable, 0 to disable
* r_div - Desired r_div ratio
* div_by_4 - Set Divide By 4 mode
* Set to 1 to enable, 0 to disable
*/
void Si5351::set_ms(enum si5351_clock clk, struct Si5351RegSet ms_reg, uint8_t int_mode, uint8_t r_div, uint8_t div_by_4)
{
uint8_t *params = new uint8_t[20];
uint8_t i = 0;
uint8_t temp;
uint8_t reg_val;
if((uint8_t)clk <= (uint8_t)SI5351_CLK5)
{
// Registers 42-43 for CLK0
temp = (uint8_t)((ms_reg.p3 >> 8) & 0xFF);
params[i++] = temp;
temp = (uint8_t)(ms_reg.p3 & 0xFF);
params[i++] = temp;
// Register 44 for CLK0
reg_val = si5351_read((SI5351_CLK0_PARAMETERS + 2) + (clk * 8));
reg_val &= ~(0x03);
temp = reg_val | ((uint8_t)((ms_reg.p1 >> 16) & 0x03));
params[i++] = temp;
// Registers 45-46 for CLK0
temp = (uint8_t)((ms_reg.p1 >> 8) & 0xFF);
params[i++] = temp;
temp = (uint8_t)(ms_reg.p1 & 0xFF);
params[i++] = temp;
// Register 47 for CLK0
temp = (uint8_t)((ms_reg.p3 >> 12) & 0xF0);
temp += (uint8_t)((ms_reg.p2 >> 16) & 0x0F);
params[i++] = temp;
// Registers 48-49 for CLK0
temp = (uint8_t)((ms_reg.p2 >> 8) & 0xFF);
params[i++] = temp;
temp = (uint8_t)(ms_reg.p2 & 0xFF);
params[i++] = temp;
}
else
{
// MS6 and MS7 only use one register
temp = ms_reg.p1;
}
// Write the parameters
switch(clk)
{
case SI5351_CLK0:
si5351_write_bulk(SI5351_CLK0_PARAMETERS, i, params);
set_int(clk, int_mode);
ms_div(clk, r_div, div_by_4);
break;
case SI5351_CLK1:
si5351_write_bulk(SI5351_CLK1_PARAMETERS, i, params);
set_int(clk, int_mode);
ms_div(clk, r_div, div_by_4);
break;
case SI5351_CLK2:
si5351_write_bulk(SI5351_CLK2_PARAMETERS, i, params);
set_int(clk, int_mode);
ms_div(clk, r_div, div_by_4);
break;
case SI5351_CLK3:
si5351_write_bulk(SI5351_CLK3_PARAMETERS, i, params);
set_int(clk, int_mode);
ms_div(clk, r_div, div_by_4);
break;
case SI5351_CLK4:
si5351_write_bulk(SI5351_CLK4_PARAMETERS, i, params);
set_int(clk, int_mode);
ms_div(clk, r_div, div_by_4);
break;
case SI5351_CLK5:
si5351_write_bulk(SI5351_CLK5_PARAMETERS, i, params);
set_int(clk, int_mode);
ms_div(clk, r_div, div_by_4);
break;
case SI5351_CLK6:
si5351_write(SI5351_CLK6_PARAMETERS, temp);
ms_div(clk, r_div, div_by_4);
break;
case SI5351_CLK7:
si5351_write(SI5351_CLK7_PARAMETERS, temp);
ms_div(clk, r_div, div_by_4);
break;
}
delete params;
}
/*
* output_enable(enum si5351_clock clk, uint8_t enable)
*
* Enable or disable a chosen output
* clk - Clock output
* (use the si5351_clock enum)
* enable - Set to 1 to enable, 0 to disable
*/
void Si5351::output_enable(enum si5351_clock clk, uint8_t enable)
{
uint8_t reg_val;
reg_val = si5351_read(SI5351_OUTPUT_ENABLE_CTRL);
if(enable == 1)
{
reg_val &= ~(1<<(uint8_t)clk);
}
else
{
reg_val |= (1<<(uint8_t)clk);
}
si5351_write(SI5351_OUTPUT_ENABLE_CTRL, reg_val);
}
/*
* drive_strength(enum si5351_clock clk, enum si5351_drive drive)
*
* Sets the drive strength of the specified clock output
*
* clk - Clock output
* (use the si5351_clock enum)
* drive - Desired drive level
* (use the si5351_drive enum)
*/
void Si5351::drive_strength(enum si5351_clock clk, enum si5351_drive drive)
{
uint8_t reg_val;
const uint8_t mask = 0x03;
reg_val = si5351_read(SI5351_CLK0_CTRL + (uint8_t)clk);
reg_val &= ~(mask);
switch(drive)
{
case SI5351_DRIVE_2MA:
reg_val |= 0x00;
break;
case SI5351_DRIVE_4MA:
reg_val |= 0x01;
break;
case SI5351_DRIVE_6MA:
reg_val |= 0x02;
break;
case SI5351_DRIVE_8MA:
reg_val |= 0x03;
break;
default:
break;
}
si5351_write(SI5351_CLK0_CTRL + (uint8_t)clk, reg_val);
}
/*
* update_status(void)
*
* Call this to update the status structs, then access them
* via the dev_status and dev_int_status global members.
*
* See the header file for the struct definitions. These
* correspond to the flag names for registers 0 and 1 in
* the Si5351 datasheet.
*/
void Si5351::update_status(void)
{
update_sys_status(&dev_status);
update_int_status(&dev_int_status);
}
/*
* set_correction(int32_t corr, enum si5351_pll_input ref_osc)
*
* corr - Correction factor in ppb
* ref_osc - Desired reference oscillator
* (use the si5351_pll_input enum)
*
* Use this to set the oscillator correction factor.
* This value is a signed 32-bit integer of the
* parts-per-billion value that the actual oscillation
* frequency deviates from the specified frequency.
*
* The frequency calibration is done as a one-time procedure.
* Any desired test frequency within the normal range of the
* Si5351 should be set, then the actual output frequency
* should be measured as accurately as possible. The
* difference between the measured and specified frequencies
* should be calculated in Hertz, then multiplied by 10 in
* order to get the parts-per-billion value.
*
* Since the Si5351 itself has an intrinsic 0 PPM error, this
* correction factor is good across the entire tuning range of
* the Si5351. Once this calibration is done accurately, it
* should not have to be done again for the same Si5351 and
* crystal.
*/
void Si5351::set_correction(int32_t corr, enum si5351_pll_input ref_osc)
{
ref_correction[(uint8_t)ref_osc] = corr;
// Recalculate and set PLL freqs based on correction value
set_pll(plla_freq, SI5351_PLLA);
set_pll(pllb_freq, SI5351_PLLB);
}
/*
* set_phase(enum si5351_clock clk, uint8_t phase)
*
* clk - Clock output
* (use the si5351_clock enum)
* phase - 7-bit phase word
* (in units of VCO/4 period)
*
* Write the 7-bit phase register. This must be used
* with a user-set PLL frequency so that the user can
* calculate the proper tuning word based on the PLL period.
*/
void Si5351::set_phase(enum si5351_clock clk, uint8_t phase)
{
// Mask off the upper bit since it is reserved
phase = phase & 0b01111111;
si5351_write(SI5351_CLK0_PHASE_OFFSET + (uint8_t)clk, phase);
}
/*
* get_correction(enum si5351_pll_input ref_osc)
*
* ref_osc - Desired reference oscillator
* 0: crystal oscillator (XO)
* 1: external clock input (CLKIN)
*
* Returns the oscillator correction factor stored
* in RAM.
*/
int32_t Si5351::get_correction(enum si5351_pll_input ref_osc)
{
return ref_correction[(uint8_t)ref_osc];
}
/*
* pll_reset(enum si5351_pll target_pll)
*
* target_pll - Which PLL to reset
* (use the si5351_pll enum)
*
* Apply a reset to the indicated PLL.
*/
void Si5351::pll_reset(enum si5351_pll target_pll)
{
if(target_pll == SI5351_PLLA)
{
si5351_write(SI5351_PLL_RESET, SI5351_PLL_RESET_A);
}
else if(target_pll == SI5351_PLLB)
{
si5351_write(SI5351_PLL_RESET, SI5351_PLL_RESET_B);
}
}
/*
* set_ms_source(enum si5351_clock clk, enum si5351_pll pll)
*
* clk - Clock output
* (use the si5351_clock enum)
* pll - Which PLL to use as the source
* (use the si5351_pll enum)
*
* Set the desired PLL source for a multisynth.
*/
void Si5351::set_ms_source(enum si5351_clock clk, enum si5351_pll pll)
{
uint8_t reg_val;
reg_val = si5351_read(SI5351_CLK0_CTRL + (uint8_t)clk);
if(pll == SI5351_PLLA)
{
reg_val &= ~(SI5351_CLK_PLL_SELECT);
}
else if(pll == SI5351_PLLB)
{
reg_val |= SI5351_CLK_PLL_SELECT;
}
si5351_write(SI5351_CLK0_CTRL + (uint8_t)clk, reg_val);
pll_assignment[(uint8_t)clk] = pll;
}
/*
* set_int(enum si5351_clock clk, uint8_t int_mode)
*
* clk - Clock output
* (use the si5351_clock enum)
* enable - Set to 1 to enable, 0 to disable
*
* Set the indicated multisynth into integer mode.
*/
void Si5351::set_int(enum si5351_clock clk, uint8_t enable)
{
uint8_t reg_val;
reg_val = si5351_read(SI5351_CLK0_CTRL + (uint8_t)clk);
if(enable == 1)
{
reg_val |= (SI5351_CLK_INTEGER_MODE);
}
else
{
reg_val &= ~(SI5351_CLK_INTEGER_MODE);
}
si5351_write(SI5351_CLK0_CTRL + (uint8_t)clk, reg_val);
// Integer mode indication
/*
switch(clk)
{
case SI5351_CLK0:
clk0_int_mode = enable;
break;
case SI5351_CLK1:
clk1_int_mode = enable;
break;
case SI5351_CLK2:
clk2_int_mode = enable;
break;
default:
break;
}
*/
}
/*
* set_clock_pwr(enum si5351_clock clk, uint8_t pwr)
*
* clk - Clock output
* (use the si5351_clock enum)
* pwr - Set to 1 to enable, 0 to disable
*
* Enable or disable power to a clock output (a power
* saving feature).
*/
void Si5351::set_clock_pwr(enum si5351_clock clk, uint8_t pwr)
{
uint8_t reg_val; //, reg;
reg_val = si5351_read(SI5351_CLK0_CTRL + (uint8_t)clk);
if(pwr == 1)
{
reg_val &= 0b01111111;
}
else
{
reg_val |= 0b10000000;
}
si5351_write(SI5351_CLK0_CTRL + (uint8_t)clk, reg_val);
}
/*
* set_clock_invert(enum si5351_clock clk, uint8_t inv)
*
* clk - Clock output
* (use the si5351_clock enum)
* inv - Set to 1 to enable, 0 to disable
*
* Enable to invert the clock output waveform.
*/
void Si5351::set_clock_invert(enum si5351_clock clk, uint8_t inv)
{
uint8_t reg_val;
reg_val = si5351_read(SI5351_CLK0_CTRL + (uint8_t)clk);
if(inv == 1)
{
reg_val |= (SI5351_CLK_INVERT);
}
else