-
Notifications
You must be signed in to change notification settings - Fork 11
/
si5351.c
857 lines (744 loc) · 18.5 KB
/
si5351.c
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
/*
* si5351.c - Si5351 library for avr-gcc
*
* Copyright (C) 2014 Jason Milldrum <[email protected]>
*
* Some tuning algorithms derived from clk-si5351.c in the Linux kernel.
* Sebastian Hesselbarth <[email protected]>
* Rabeeh Khoury <[email protected]>
*
* rational_best_approximation() derived from lib/rational.c in
* the Linux kernel.
* Copyright (C) 2009 emlix GmbH, Oskar Schirmer <[email protected]>
*
* 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 <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <util/twi.h>
#include <avr/eeprom.h>
#include "si5351.h"
#include "i2c.h"
uint32_t EEMEM ee_ref_correction = 0;
int32_t ref_correction = 0;
uint32_t plla_freq = 0;
uint32_t pllb_freq = 0;
struct Si5351Status dev_status;
struct Si5351IntStatus dev_int_status;
/******************************/
/* Suggested public functions */
/******************************/
/*
* si5351_init(void)
*
* Call this to initialize I2C communications and get the
* Si5351 ready for use.
*/
void si5351_init(void)
{
i2c_init();
/* Set crystal load capacitance */
si5351_write(SI5351_CRYSTAL_LOAD, SI5351_CRYSTAL_LOAD_8PF);
/* Get the correction factor from EEPROM */
ref_correction = eeprom_read_dword(&ee_ref_correction);
}
/*
* si5351_set_freq(uint32_t freq, uint32_t pll_freq, enum si5351_clock output)
*
* Sets the clock frequency of the specified CLK output
*
* freq - Output frequency in Hz
* pll_freq - Frequency of the PLL driving the Multisynth
* Use a 0 to have the function choose a PLL frequency
* clk - Clock output
* (use the si5351_clock enum)
*/
void si5351_set_freq(uint32_t freq, uint32_t pll_freq, enum si5351_clock clk)
{
struct Si5351RegSet ms_reg, pll_reg;
enum si5351_pll target_pll;
/* Calculate the synth parameters */
/* If pll_freq is 0, let the algorithm pick a PLL frequency */
if(pll_freq == 0)
{
uint32_t pll_freq = multisynth_calc(freq, &ms_reg);
}
/* TODO: bounds checking */
else
{
multisynth_recalc(freq, pll_freq, &ms_reg);
}
/* Determine which PLL to use */
/* CLK0 gets PLLA, CLK1 gets PLLB */
/* CLK2 gets PLLB if necessary */
/* Only good for Si5351A3 variant at the moment */
if(clk == SI5351_CLK0)
{
target_pll = SI5351_PLLA;
plla_freq = pll_freq;
}
else if(clk == SI5351_CLK1)
{
target_pll = SI5351_PLLB;
pllb_freq = pll_freq;
}
else
{
/* need to account for CLK2 set before CLK1 */
if(pllb_freq == 0)
{
target_pll = SI5351_PLLB;
pllb_freq = pll_freq;
}
else
{
target_pll = SI5351_PLLB;
pll_freq = pllb_freq;
multisynth_recalc(freq, pll_freq, &ms_reg);
}
}
pll_calc(pll_freq, &pll_reg, ref_correction);
/* Derive the register values to write */
/* Prepare an array for parameters to be written to */
uint8_t *params = malloc(sizeof(uint8_t) * 30);
uint8_t i = 0;
uint8_t temp;
/* PLL parameters first */
if(pll_calc == 0)
{
/* 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 + 1, params);
}
else if(target_pll == SI5351_PLLB)
{
si5351_write_bulk(SI5351_PLLB_PARAMETERS, i + 1, params);
}
}
free(params);
/* Now the multisynth parameters */
params = malloc(sizeof(char) * 30);
i = 0;
/* Registers 42-43 */
temp = (uint8_t)((ms_reg.p3 >> 8) & 0xFF);
params[i++] = temp;
temp = (uint8_t)(ms_reg.p3 & 0xFF);
params[i++] = temp;
/* Register 44 */
/* TODO: add code for output divider */
temp = (uint8_t)((ms_reg.p1 >> 16) & 0x03);
params[i++] = temp;
/* Registers 45-46 */
temp = (uint8_t)((ms_reg.p1 >> 8) & 0xFF);
params[i++] = temp;
temp = (uint8_t)(ms_reg.p1 & 0xFF);
params[i++] = temp;
/* Register 47 */
temp = (uint8_t)((ms_reg.p3 >> 12) & 0xF0);
temp += (uint8_t)((ms_reg.p2 >> 16) & 0x0F);
params[i++] = temp;
/* Registers 48-49 */
temp = (uint8_t)((ms_reg.p2 >> 8) & 0xFF);
params[i++] = temp;
temp = (uint8_t)(ms_reg.p2 & 0xFF);
params[i++] = temp;
/* Write the parameters */
switch(clk)
{
case SI5351_CLK0:
si5351_write_bulk(SI5351_CLK0_PARAMETERS, i + 1, params);
si5351_set_ms_source(clk, target_pll);
break;
case SI5351_CLK1:
si5351_write_bulk(SI5351_CLK1_PARAMETERS, i + 1, params);
si5351_set_ms_source(clk, target_pll);
break;
case SI5351_CLK2:
si5351_write_bulk(SI5351_CLK2_PARAMETERS, i + 1, params);
si5351_set_ms_source(clk, target_pll);
break;
case SI5351_CLK3:
si5351_write_bulk(SI5351_CLK3_PARAMETERS, i + 1, params);
si5351_set_ms_source(clk, target_pll);
break;
case SI5351_CLK4:
si5351_write_bulk(SI5351_CLK4_PARAMETERS, i + 1, params);
si5351_set_ms_source(clk, target_pll);
break;
case SI5351_CLK5:
si5351_write_bulk(SI5351_CLK5_PARAMETERS, i + 1, params);
si5351_set_ms_source(clk, target_pll);
break;
case SI5351_CLK6:
si5351_write_bulk(SI5351_CLK6_PARAMETERS, i + 1, params);
si5351_set_ms_source(clk, target_pll);
break;
case SI5351_CLK7:
si5351_write_bulk(SI5351_CLK7_PARAMETERS, i + 1, params);
si5351_set_ms_source(clk, target_pll);
break;
}
free(params);
}
/*
* si5351_set_pll(uint32_t pll_freq, enum si5351_pll target_pll)
*
* Set the specified PLL to a specific oscillation frequency
*
* pll_freq - Desired PLL frequency
* target_pll - Which PLL to set
* (use the si5351_pll enum)
*/
void si5351_set_pll(uint32_t pll_freq, enum si5351_pll target_pll)
{
struct Si5351RegSet pll_reg;
pll_calc(pll_freq, &pll_reg, ref_correction);
/* Derive the register values to write */
/* Prepare an array for parameters to be written to */
uint8_t *params = malloc(sizeof(uint8_t) * 30);
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 + 1, params);
}
else if(target_pll == SI5351_PLLB)
{
si5351_write_bulk(SI5351_PLLB_PARAMETERS, i + 1, params);
}
}
/*
* si5351_clock_enable(enum si5351_clock clk, uint8_t enable)
*
* Enable or disable a chosen clock
* clk - Clock output
* (use the si5351_clock enum)
* enable - Set to 1 to enable, 0 to disable
*/
void si5351_clock_enable(enum si5351_clock clk, uint8_t enable)
{
uint8_t reg_val;
if(si5351_read(SI5351_OUTPUT_ENABLE_CTRL, ®_val) != 0)
{
return;
}
if(enable == 1)
{
reg_val &= ~(1<<(uint8_t)clk);
}
else
{
reg_val |= (1<<(uint8_t)clk);
}
si5351_write(SI5351_OUTPUT_ENABLE_CTRL, reg_val);
}
/*
* si5351_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;
if(si5351_read(SI5351_CLK0_CTRL + (uint8_t)clk, ®_val) != 0)
{
return;
}
switch(drive)
{
case SI5351_DRIVE_2MA:
reg_val &= ~(mask);
reg_val |= 0x00;
break;
case SI5351_DRIVE_4MA:
reg_val &= ~(mask);
reg_val |= 0x01;
break;
case SI5351_DRIVE_6MA:
reg_val &= ~(mask);
reg_val |= 0x02;
break;
case SI5351_DRIVE_8MA:
reg_val &= ~(mask);
reg_val |= 0x03;
break;
default:
break;
}
si5351_write(SI5351_CLK0_CTRL + (uint8_t)clk, reg_val);
}
/*
* si5351_update_status(void)
*
* Call this to update the status structs, then access them
* via the dev_status and dev_int_status global variables.
*
* 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)
{
si5351_update_sys_status(&dev_status);
si5351_update_int_status(&dev_int_status);
}
/*
* si5351_set_correction(int32_t corr)
*
* Use this to set the oscillator correction factor to
* EEPROM. This value is a signed 32-bit integer of the
* parts-per-10 million 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-10 million 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. The library will read the correction factor from
* EEPROM during initialization for use by the tuning
* algorithms.
*/
void si5351_set_correction(int32_t corr)
{
eeprom_write_dword(&ee_ref_correction, corr);
ref_correction = corr;
}
/*
* si5351_get_correction(void)
*
* Returns the oscillator correction factor stored
* in EEPROM.
*/
int32_t si5351_get_correction(void)
{
return eeprom_read_dword(&ee_ref_correction);
}
/*******************************/
/* Suggested private functions */
/*******************************/
/*
* Calculate best rational approximation for a given fraction
* taking into account restricted register size, e.g. to find
* appropriate values for a pll with 5 bit denominator and
* 8 bit numerator register fields, trying to set up with a
* frequency ratio of 3.1415, one would say:
*
* rational_best_approximation(31415, 10000,
* (1 << 8) - 1, (1 << 5) - 1, &n, &d);
*
* you may look at given_numerator as a fixed point number,
* with the fractional part size described in given_denominator.
*
* for theoretical background, see:
* http://en.wikipedia.org/wiki/Continued_fraction
*/
void rational_best_approximation(
unsigned long given_numerator, unsigned long given_denominator,
unsigned long max_numerator, unsigned long max_denominator,
unsigned long *best_numerator, unsigned long *best_denominator)
{
unsigned long n, d, n0, d0, n1, d1;
n = given_numerator;
d = given_denominator;
n0 = d1 = 0;
n1 = d0 = 1;
for (;;) {
unsigned long t, a;
if ((n1 > max_numerator) || (d1 > max_denominator)) {
n1 = n0;
d1 = d0;
break;
}
if (d == 0)
break;
t = d;
a = n / d;
d = n % d;
n = t;
t = n0 + a * n1;
n0 = n1;
n1 = t;
t = d0 + a * d1;
d0 = d1;
d1 = t;
}
*best_numerator = n1;
*best_denominator = d1;
}
uint32_t pll_calc(uint32_t freq, struct Si5351RegSet *reg, int32_t correction)
{
uint32_t ref_freq = SI5351_XTAL_FREQ;
uint32_t rfrac, denom, a, b, c, p1, p2, p3;
uint64_t lltmp;
/* Factor calibration value into nominal crystal frequency */
/* Measured in parts-per-ten million */
ref_freq += (uint32_t)((double)(correction / 10000000.0) * (double)ref_freq);
/* PLL bounds checking */
if (freq < SI5351_PLL_VCO_MIN)
freq = SI5351_PLL_VCO_MIN;
if (freq > SI5351_PLL_VCO_MAX)
freq = SI5351_PLL_VCO_MAX;
/* Determine integer part of feedback equation */
a = freq / ref_freq;
if (a < SI5351_PLL_A_MIN)
freq = ref_freq * SI5351_PLL_A_MIN;
if (a > SI5351_PLL_A_MAX)
freq = ref_freq * SI5351_PLL_A_MAX;
/* find best approximation for b/c = fVCO mod fIN */
denom = 1000L * 1000L;
lltmp = freq % ref_freq;
lltmp *= denom;
do_div(lltmp, ref_freq);
rfrac = (uint32_t)lltmp;
b = 0;
c = 1;
if (rfrac)
rational_best_approximation(rfrac, denom,
SI5351_PLL_B_MAX, SI5351_PLL_C_MAX, &b, &c);
/* calculate parameters */
p3 = c;
p2 = (128 * b) % c;
p1 = 128 * a;
p1 += (128 * b / c);
p1 -= 512;
/* recalculate rate by fIN * (a + b/c) */
lltmp = ref_freq;
lltmp *= b;
do_div(lltmp, c);
freq = (uint32_t)lltmp;
freq += ref_freq * a;
reg->p1 = p1;
reg->p2 = p2;
reg->p3 = p3;
return freq;
}
uint32_t multisynth_calc(uint32_t freq, struct Si5351RegSet *reg)
{
uint32_t pll_freq;
uint64_t lltmp;
uint32_t a, b, c, p1, p2, p3;
uint8_t divby4;
/* Multisynth bounds checking */
if (freq > SI5351_MULTISYNTH_MAX_FREQ)
freq = SI5351_MULTISYNTH_MAX_FREQ;
if (freq < SI5351_MULTISYNTH_MIN_FREQ)
freq = SI5351_MULTISYNTH_MIN_FREQ;
divby4 = 0;
if (freq > SI5351_MULTISYNTH_DIVBY4_FREQ)
divby4 = 1;
/* Find largest integer divider for max */
/* VCO frequency and given target frequency */
if (divby4 == 0)
{
lltmp = SI5351_PLL_VCO_MAX;
do_div(lltmp, freq);
a = (uint32_t)lltmp;
}
else
a = 4;
b = 0;
c = 1;
pll_freq = a * freq;
/* Recalculate output frequency by fOUT = fIN / (a + b/c) */
lltmp = pll_freq;
lltmp *= c;
do_div(lltmp, a * c + b);
freq = (unsigned long)lltmp;
/* Calculate parameters */
if (divby4)
{
p3 = 1;
p2 = 0;
p1 = 0;
}
else
{
p3 = c;
p2 = (128 * b) % c;
p1 = 128 * a;
p1 += (128 * b / c);
p1 -= 512;
}
reg->p1 = p1;
reg->p2 = p2;
reg->p3 = p3;
return pll_freq;
}
uint32_t multisynth_recalc(uint32_t freq, uint32_t pll_freq, struct Si5351RegSet *reg)
{
uint64_t lltmp;
uint32_t rfrac, denom, a, b, c, p1, p2, p3;
uint8_t divby4;
/* Multisynth bounds checking */
if (freq > SI5351_MULTISYNTH_MAX_FREQ)
freq = SI5351_MULTISYNTH_MAX_FREQ;
if (freq < SI5351_MULTISYNTH_MIN_FREQ)
freq = SI5351_MULTISYNTH_MIN_FREQ;
divby4 = 0;
if (freq > SI5351_MULTISYNTH_DIVBY4_FREQ)
divby4 = 1;
/* Determine integer part of feedback equation */
a = pll_freq / freq;
/* TODO: not sure this is correct */
if (a < SI5351_MULTISYNTH_A_MIN)
freq = pll_freq / SI5351_MULTISYNTH_A_MIN;
if (a > SI5351_MULTISYNTH_A_MAX)
freq = pll_freq / SI5351_MULTISYNTH_A_MAX;
/* find best approximation for b/c */
denom = 1000L * 1000L;
lltmp = pll_freq % freq;
lltmp *= denom;
do_div(lltmp, freq);
rfrac = (uint32_t)lltmp;
b = 0;
c = 1;
if (rfrac)
rational_best_approximation(rfrac, denom,
SI5351_MULTISYNTH_B_MAX, SI5351_MULTISYNTH_C_MAX, &b, &c);
/* Recalculate output frequency by fOUT = fIN / (a + b/c) */
lltmp = pll_freq;
lltmp *= c;
do_div(lltmp, a * c + b);
freq = (unsigned long)lltmp;
/* Calculate parameters */
if (divby4)
{
p3 = 1;
p2 = 0;
p1 = 0;
}
else
{
p3 = c;
p2 = (128 * b) % c;
p1 = 128 * a;
p1 += (128 * b / c);
p1 -= 512;
}
reg->p1 = p1;
reg->p2 = p2;
reg->p3 = p3;
return freq;
}
uint8_t si5351_write_bulk(uint8_t addr, uint8_t bytes, uint8_t *data)
{
int i;
i2c_start();
if(i2c_status() != TW_START)
{
i2c_stop();
return 1;
}
i2c_write(SI5351_BUS_BASE_ADDR);
if(i2c_status() != TW_MT_SLA_ACK)
{
i2c_stop();
return 1;
}
i2c_write(addr);
if(i2c_status() != TW_MT_DATA_ACK)
{
i2c_stop();
return 1;
}
for(i = 0; i < bytes; i++)
{
i2c_write(data[i]);
if(i2c_status() != TW_MT_DATA_ACK)
{
i2c_stop();
return 1;
}
}
i2c_stop();
return 0;
}
uint8_t si5351_write(uint8_t addr, uint8_t data)
{
i2c_start();
if(i2c_status() != TW_START)
{
i2c_stop();
return 1;
}
i2c_write(SI5351_BUS_BASE_ADDR);
if(i2c_status() != TW_MT_SLA_ACK)
{
i2c_stop();
return 1;
}
i2c_write(addr);
if(i2c_status() != TW_MT_DATA_ACK)
{
i2c_stop();
return 1;
}
i2c_write(data);
if(i2c_status() != TW_MT_DATA_ACK)
{
i2c_stop();
return 1;
}
i2c_stop();
return 0;
}
uint8_t si5351_read(uint8_t addr, uint8_t *data)
{
i2c_start();
if(i2c_status() != TW_START)
{
i2c_stop();
return 1;
}
i2c_write(SI5351_BUS_BASE_ADDR);
if(i2c_status() != TW_MT_SLA_ACK)
{
i2c_stop();
return 1;
}
i2c_write(addr);
if(i2c_status() != TW_MT_DATA_ACK)
{
i2c_stop();
return 1;
}
i2c_start();
if(i2c_status() != TW_REP_START)
{
i2c_stop();
return 1;
}
i2c_write(SI5351_BUS_BASE_ADDR | TW_READ);
if(i2c_status() != TW_MR_SLA_ACK)
{
i2c_stop();
return 1;
}
*data = i2c_read_nack();
if(i2c_status() != TW_MR_DATA_NACK)
{
i2c_stop();
return 1;
}
i2c_stop();
return 0;
}
void si5351_update_sys_status(struct Si5351Status *status)
{
uint8_t reg_val = 0;
if(si5351_read(SI5351_DEVICE_STATUS, ®_val) != 0)
{
return;
}
/* Parse the register */
status->SYS_INIT = (reg_val >> 7) & 0x01;
status->LOL_B = (reg_val >> 6) & 0x01;
status->LOL_A = (reg_val >> 5) & 0x01;
status->LOS = (reg_val >> 4) & 0x01;
status->REVID = reg_val & 0x03;
}
void si5351_update_int_status(struct Si5351IntStatus *int_status)
{
uint8_t reg_val = 0;
if(si5351_read(SI5351_DEVICE_STATUS, ®_val) != 0)
{
return;
}
/* Parse the register */
int_status->SYS_INIT_STKY = (reg_val >> 7) & 0x01;
int_status->LOL_B_STKY = (reg_val >> 6) & 0x01;
int_status->LOL_A_STKY = (reg_val >> 5) & 0x01;
int_status->LOS_STKY = (reg_val >> 4) & 0x01;
}
void si5351_set_ms_source(enum si5351_clock clk, enum si5351_pll pll)
{
uint8_t reg_val = 0x0c;
uint8_t reg_val2;
if(si5351_read(SI5351_CLK0_CTRL + (uint8_t)clk, ®_val2) != 0)
{
return;
}
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);
}