-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathQN8066.cpp
2133 lines (1918 loc) · 61.5 KB
/
QN8066.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
/**
* @mainpage QN8066 Arduino Library implementation
*
* @details This is an Arduino library for the QN8066 FM RX/TX device (Digital FM Transceiver for Portable Devices).
* @details The communication used by this library is I2C.
* @details You can see a complete documentation on <https://github.com/pu2clr/QN8066> or <https://pu2clr.github.io/QN8066/>
* @details
* @details This library can be freely distributed. This means that you can copy, modify, merge, and redistribute the code for free under the terms of the [MIT License](https://github.com/pu2clr/QN8066/blob/main/LICENSE).
* @details There are examples that can help you in your project on <https://github.com/pu2clr/QN8066/tree/master/examples>
* @see [General Documentation](https://pu2clr.github.io/QN8066/)
* @author PU2CLR - Ricardo Lima Caratti - pu2clr@gmail.com
* @date 2024-06
* @copyright MIT Free Software model. See [Copyright (c) 2024 Ricardo Lima Caratti](https://github.com/pu2clr/QN8066/blob/main/LICENSE).
*/
#include <QN8066.h>
/** @defgroup group01 Device Checking*/
/**
* @ingroup group01 Detect Device
* @brief Checks communication with QN8066 via I2C
* @details Checks if the QN8066 is available on the I2C bus. Remember that the QN8066 responds to the address 0x21
* @return true or false
*/
bool QN8066::detectDevice() {
Wire.begin();
// check 0x21 I2C address
Wire.beginTransmission(QN8066_I2C_ADDRESS);
return !Wire.endTransmission();
}
/**
* @ingroup group01 Scan I2C Devices
* @brief Scans the I2C bus and returns the addresses of the devices found.
* @details Searches for devices connected to the I2C bus. The addresses of the devices found are stored in the "device" array.
* @param device array of device addresses found.
* @return uint8_t number of devices found or 0 if no device found or error.
*/
uint8_t QN8066::scanI2CBus(uint8_t *device) {
uint8_t error, address;
uint8_t idxDevice = 0;
Wire.begin();
for (address = 1; address < 127; address++) {
Wire.beginTransmission(address);
error = Wire.endTransmission();
if (error == 0) {
device[idxDevice] = address;
idxDevice++;
} else if (error == 4) {
return 0;
}
}
return idxDevice;
}
/** @defgroup group02 Basic Functions*/
/**
* @ingroup group02 I2C
* @brief Gets register information
* @details 8066 commands. It provides a means to run commands that are not currently supported by the standard API.
* @param registerNumber
* @return uint8_t Value of the register
*/
uint8_t QN8066::getRegister(uint8_t registerNumber) {
Wire.beginTransmission(QN8066_I2C_ADDRESS);
Wire.write(registerNumber);
Wire.endTransmission();
delayMicroseconds(QN8066_DELAY_COMMAND);
Wire.requestFrom(QN8066_I2C_ADDRESS, 1);
return Wire.read();
}
/**
* @ingroup group02 I2C
* @brief Stores a velue to a given register
* @details 8066 commands. It provides a means to run commands that are not currently supported by the standard API.
* @param registerNumber
* @param value
*/
void QN8066::setRegister(uint8_t registerNumber, uint8_t value) {
Wire.beginTransmission(QN8066_I2C_ADDRESS);
Wire.write(registerNumber);
Wire.write(value);
Wire.endTransmission();
delayMicroseconds(QN8066_DELAY_COMMAND);
}
/**
* @ingroup group02 Device Status
* @brief Gets the current device Status stored in STATUS1 register
* @return qn8066_status1
* @see QUINTIC Datasheet - Rev 0.2, Page 24.
* @details Example
* @code
* #include <QN8066.h>
* QN8066 tx;
* void setup() {
* Serial.begin(9600);
* tx.setup();
* Serial.print("\nStarting the system...");
* tx.setTX(1069); // Sets the transmitter to 106.9 MHz
* tx.setTxStereo(true);
* qn8066_status1 s1 = tx.getStatus1();
* Serial.println(s1.arg.FSM); // Shows FSM state code - QUINTIC Datasheet - Rev 0.2, Page 24.
* }
* void loop() {
* delay(5);
*}
* @endcode
*
*/
qn8066_status1 QN8066::getStatus1() {
qn8066_status1 value;
value.raw = this->getRegister(QN_STATUS1);
return value;
}
/**
* @ingroup group02 Device Status
* @brief Gets the current device Status stored in STATUS2 register
* @return qn8066_status2
* @see QUINTIC Datasheet - Rev 0.2, Page 28.
* @details Example
* @code
* #include <QN8066.h>
* QN8066 tx;
* void setup() {
* Serial.begin(9600);
* tx.setup();
* Serial.print("\nStarting the system...");
* tx.setTX(1069); // Sets the transmitter to 106.9 MHz
* tx.setTxStereo(true);
* qn8066_status2 s2 = tx.getStatus2();
* Serial.println(s2.arg.RDSSYNC); // Shows RDS block synchronous indicator.
* }
* void loop() {
* delay(5);
*}
* @endcode
*/
qn8066_status2 QN8066::getStatus2() {
qn8066_status2 value;
value.raw = this->getRegister(QN_STATUS2);
return value;
}
/**
* @ingroup group02 Device Status
* @brief Gets the current device Status stored in STATUS3 register
* @return qn8066_status3
* @see QUINTIC Datasheet - Rev 0.2, Page 30.
* @details Example
* @code
* #include <QN8066.h>
* QN8066 tx;
* void setup() {
* Serial.begin(9600);
* tx.setup();
* Serial.print("\nStarting the system...");
* tx.setTX(1069); // Sets the transmitter to 106.9 MHz
* tx.setTxStereo(true);
* ...
* qn8066_status3 s3 = tx.getStatus2();
* Serial.println(s3.arg.RDS_TXUPD); // Checks RDS TX update
* }
* void loop() {
* delay(5);
*}
* @endcode
*/
qn8066_status3 QN8066::getStatus3() {
qn8066_status3 value;
value.raw = this->getRegister(QN_STATUS3);
return value;
}
/**
* @ingroup group02 Init Device
* @brief Device initial configuration
* @details The functions described in this group are used to configure the QN8066.
*/
/**
* @ingroup group02 Init Device
* @brief Starts the QN8066 instance.
* @details It is a minimalist code to initiate the QN8066 device. Use the function setup if you want more option during the start process.
* @details Example
* @code
* #include <QN8066.h>
* QN8066 tx;
* void setup() {
* // Call setup setting Divider = 1000, Stereo, RDS on and PreEmphasis (tc) 75us
* tx.begin();
* tx.setTX(1069); // Set the transmitter to 106.9 MHz
* }
*
* void loop() {
* }
* @endcode
*/
void QN8066::begin() {
delay(600); // Chip power-up time
this->system1.raw = 0B11100011;
this->system2.raw = 0;
this->cca.raw = this->getRegister(QN_CCA);
this->cca.arg.xtal_inj = 1;
this->gplt.raw = this->getRegister(QN_GPLT);
this->fdev.raw = this->getRegister(QN_FDEV);
this->rds.raw = this->getRegister(QN_RDS);
this->reg_vga.raw = this->getRegister(QN_REG_VGA);
this->int_ctrl.raw = this->getRegister(QN_INT_CTRL);
this->pac.raw = this->getRegister(QN_PAC);
this->vol_ctl.raw = this->getRegister(QN_VOL_CTL);
Wire.begin();
}
/**
* @ingroup group02 Init Device
* @brief Set transmission request
* @details This function enable or disable transmission
* @param value - 0 = disable; 1 = enable
*/
inline void QN8066::setTxMode(uint8_t value) {
this->system1.arg.txreq = value;
this->setRegister(QN_SYSTEM1, this->system1.raw);
}
/**
* @ingroup group02 Init Device
* @brief Stops transmitting
*/
void QN8066::stopTransmitting() {
this->setTxMode(0);
}
/**
* @ingroup group02 Init Device
* @brief Starts transmitting
*/
void QN8066::startTransmitting() {
this->setTxMode(1);
}
/**
* @ingroup group02 Init Device
* @brief QN8066 initial configuration
* @details This function can be called without arguments (parameters). In this case, the default value will be assumed. See the following list of parameters.
* @param xtalDiv - Divider based on frequency select of reference clock source. Default 1000 (see Datasheet pag. 18 and 23).
* @param mono - If false, the TX will start stereo mode. Default false.
* @param rds - if true, TX will start with RDS on. Default false.
* @param PreEmphasis - Valid values: 0 or 1 (0=50us; 1=75us). Default 0.
* @param xtalInj - type of the reference clock source (0 = Inject sine-wave clock; 1 = Inject digital clock).
* @param imageRejection - Image Rejection (0=LO<RF, image is in lower side; 1=LO>RF, image is in upper side).
* @param txSoftClipThreshold - TX soft clip threshold. Default 0. See Datasheet page 34.
* @param oneMinutOff - Selection of 1 minute time for PA off when no audio (3 = Infinity (never); 2=59s; 1=58s; 0=57s).
* @param gainTxPLT - Gain of TX pilot to adjust pilot frequency deviation. See Datasheet page 34.
* @param txFreqDev - Specify total TX frequency deviation. TX frequency deviation = 0.69KHz*TX_FEDV. Default 125.
* @param rdsLineIn - Audio Line-in enable control. Default 0
* @param rdsFreqDev - RDS frequency deviation. RDS frequency deviation = 0.35KHz*RDSFDEV in normal mode. Default 60
* @param inImpedance - TX mode input impedance for both L/R channels (Default 1 => 20K )
* @param txAgcDig - TX digital gain (Default 0)
* @param txAgcBuffer - TX input buffer gain (Default 1)
* @param txSoftClip - TX soft clipping enable (Default 0 - Disable)
* @details Example
* @code
* #include <QN8066.h>
* QN8066 tx;
* void setup() {
* // Call setup setting Divider = 1000, Stereo, RDS on and PreEmphasis (tc) 75us
* tx.setup(1000, false, true, 1);
* tx.setTX(1069); // Set the transmitter to 106.9 MHz
* }
*
* void loop() {
* }
* @endcode
*/
void QN8066::setup(uint16_t xtalDiv,
bool mono, bool rds,
uint8_t PreEmphasis, uint8_t xtalInj, uint8_t imageRejection,
uint8_t txSoftClipThreshold, uint8_t oneMinutOff, uint8_t gainTxPLT,
uint8_t txFreqDev, uint8_t rdsLineIn, uint8_t rdsFreqDev,
uint8_t inImpedance, uint8_t txAgcDig, uint8_t txAgcBuffer, uint8_t txSoftClip ) {
Wire.begin();
delay(200); // Chip power-up time
this->xtal_div = xtalDiv;
this->system1.raw = 0B11100011;
this->system2.raw = 0;
this->system2.arg.tx_mono = mono; // Default stereo
this->system2.arg.tx_rdsen = rds; // RDS ON
this->system2.arg.tc = PreEmphasis; // Default 50
this->cca.raw = this->getRegister(QN_CCA);
this->cca.arg.xtal_inj = xtalInj;
this->cca.arg.imr = imageRejection;
this->cca.arg.SNR_CCA_TH = 0B010000;
this->gplt.raw = this->getRegister(QN_GPLT);
this->gplt.arg.GAIN_TXPLT = gainTxPLT;
this->gplt.arg.t1m_sel = oneMinutOff;
this->gplt.arg.tx_sftclpth = txSoftClipThreshold;
// FDEV register
this->fdev.raw = txFreqDev;
// RDS register
this->rds.arg.line_in_en = rdsLineIn;
this->rds.arg.RDSFDEV = rdsFreqDev;
// this->int_ctrl.raw = this->getRegister(QN_INT_CTRL);
// REG_VGA
// this->reg_vga.raw = this->getRegister(QN_REG_VGA);
this->reg_vga.arg.RIN = inImpedance;
this->reg_vga.arg.TXAGC_GDB = txAgcDig;
this->reg_vga.arg.TXAGC_GVGA = txAgcBuffer;
this->reg_vga.arg.tx_sftclpen = txSoftClip;
}
/**
* @defgroup group03 RX Functions
* @brief QN8066 Receiver funtions
* @details The functions defined in this group are responsible for controlling the reception of the QN8066.
*/
/**
* @ingroup group03 RX
* @brief sets the devive to RX
* @param frequency - frequency that the receiver will start
* @todo Need to be optimized to improve space size
*/
void QN8066::setRX(uint16_t frequency) {
this->system1.raw = 0B11100011;
this->setRegister(QN_SYSTEM1, this->system1.raw); // SYSTEM1 => 11100011 => swrst = 1; recal = 1; stnby = 1; ccs_ch_dis = 1; cca_ch_dis = 1
this->setRegister(QN_SYSTEM2, this->system2.raw);
this->system2.arg.rdsrdy = !(this->system2.arg.rdsrdy); // Toggle
this->setRegister(QN_SYSTEM2, this->system2.raw);
this->setRegister(QN_CCA, this->cca.raw); // CCA => 01010000 => xtal_inj = 0; imr = 1; SNR_CCA_TH = 010000
// Sets the crystal oscillator divider
this->setRegister(QN_XTAL_DIV0, this->xtal_div & 0xFF); // Lower 8 bits of xtal_div[10:0].
this->setRegister(QN_XTAL_DIV1, (this->xtal_div >> 8) | 0B0001000); // Higher 3 bits of xtal_div[10:0].
this->setRegister(QN_XTAL_DIV2, 0B01011100); // XTAL_DIV2 = > 01011100 (It is the default value)
// this->setRegister(QN_SYSTEM1, this->system1.raw); // SYSTEM1 => 00001011 => Set TX On
this->setRegister(QN_FDEV, this->fdev.raw); // FDEV => 01111101 => 125 (Decimal)
this->setRegister(QN_RDS, this->rds.raw); // RDS => 00111100 => Line_in_en = 0; RDSFDEV = 60 (Decimal)
this->setRegister(QN_GPLT, this->gplt.raw); // GPLT => 00111001 => Tx_sftclpth = 00 (12’d2051 - 3db back off from 0.5v); t1m_sel = 11 (Infinity); GAIN_TXPLT = 1001 (9% 75 kHz)
this->rxCurrentFrequency = frequency;
uint16_t channel = (frequency - 600) * 2;
qn8066_rx_ch rxch;
qn8066_ch_step ch_step;
ch_step.raw = this->getRegister(QN_CH_STEP);
ch_step.arg.RXCH = 0B0000000000000011 & (channel >> 8);
rxch.RXCH = 0B0000000011111111 & channel;
this->setRegister(QN_CH_STEP, ch_step.raw );
this->setRegister(QN_RX_CH,rxch.RXCH);
// Checking unkown registers
// this->setRegister(0x49, 0B11101000);
this->setRegister(0x49, 0B11011111);
this->setRegister(0x6E, 0B11111111);
this->system1.raw = 0B00010011; // Receiver request
this->setRegister(QN_SYSTEM1, this->system1.raw); // SYSTEM1 => 00001011 => txreq = 1; ccs_ch_dis = 1; cca_ch_dis = 1
// this->setRegister(QN_REG_VGA, 0B01011011); // REG_VGA => 01011011 => Tx_sftclpen = 0; TXAGC_GVGA = 101; TXAGC_GDB = 10; RIN = 11 (80K)
this->setRegister(QN_REG_VGA, this->reg_vga.raw); // REG_VGA => 01011011 => Tx_sftclpen = 0; TXAGC_GVGA = 101; TXAGC_GDB = 10; RIN = 11 (80K)
delay(100);
}
/**
* @ingroup group03 RX
* @brief sets the receiver frequency
*/
void QN8066::setRxFrequency(uint16_t frequency) {
this->rxCurrentFrequency = frequency;
uint16_t channel = (frequency - 600) * 2;
qn8066_rx_ch rxch;
qn8066_ch_step ch_step;
ch_step.raw = this->getRegister(QN_CH_STEP);
ch_step.arg.RXCH = 0B0000000000000011 & (channel >> 8);
rxch.RXCH = 0B0000000011111111 & channel;
this->setRegister(QN_CH_STEP, ch_step.raw );
this->setRegister(QN_RX_CH,rxch.RXCH);
}
/**
* @ingroup group03 RX
* @brief Sets the frequency range of the receiver
* @details default values is 64.0 Mhz (640) to 108.0 MHz (1080)
* @param min
* @param max
*/
void QN8066::setRxFrequencyRange(uint16_t min, uint16_t max ) {
this->minimalFrequency = min;
this->maximalFrequency = max;
}
/**
* @ingroup group03 RX
* @brief Increments the current receiver frequency
* @todo under construction...
*/
void QN8066::setRxFrequencyUp() {
this->rxCurrentFrequency += this->rxCurrentStep;
if ( this->rxCurrentFrequency > this->maximalFrequency )
this->rxCurrentFrequency = this->minimalFrequency;
this->setRxFrequency(this->rxCurrentFrequency);
}
/**
* @ingroup group03 RX
* @brief Decrements the current receiver frequency
* @todo under construction...
*/
void QN8066::setRxFrequencyDown() {
this->rxCurrentFrequency -= this->rxCurrentStep;
if ( this->rxCurrentFrequency < this->minimalFrequency )
this->rxCurrentFrequency = this->maximalFrequency;
this->setRxFrequency(this->rxCurrentFrequency);
}
/**
* @ingroup group03 RX
* @brief Sets the current Step frequency
* @details 0 = 50KHz; 1 = 100KHz; 2 = 200KHz; 3 = Reserved
* @param value
*/
void QN8066::setRxFrequencyStep(uint8_t value) {
qn8066_ch_step step;
step.raw = this->getRegister(QN_CH_STEP);
step.arg.CH_FSTEP = value;
this->rxCurrentStep = value;
this->setRegister(QN_CH_STEP, step.raw);
}
/**
* @ingroup group03 RX
* @brief Enables RDS for RX
*/
void QN8066::rdsEnableRX(bool value) {
this->system2.arg.rx_rdsen = value;
this->setRegister(QN_SYSTEM2, this->system2.raw);
}
/**
* @ingroup group03 RX
* @brief Mute or unmute the audio in receiver mode
* @param value - if true mutes the audio; if false, mute disabled
*/
void QN8066::setAudioMuteRX(bool value) {
this->system2.arg.rx_mute = value;
this->setRegister(QN_SYSTEM2, this->system2.raw);
}
/**
* @ingroup group03 RX
* @brief Gets the current SNR (Estimated RF input)
* @return current SNR in dB
*/
uint8_t QN8066::getRxSNR() {
return this->getRegister(QN_SNR);
}
/**
* @ingroup group03 RX
* @brief Gets the current RSSI
* @details In-band signal RSSI (Received signal strength indicator) dBuV value. dBuV=RSSI-49
* @return current RSSI in dB
*/
uint8_t QN8066::getRxRSSI() {
return this->getRegister(QN_RSSISIG);
}
/**
* @ingroup group03 RX
* @brief Gets a valid or no valid rx channel
* @details RXCCA status flag. To indicate whether a valid channel is found during RX CCA.
* @details If a valid channel is found, channel index will stay there, and RXCCA_FAIL=0;
* @details otherwise, it will stay at the end of scan range and RXCCA_FAIL=1.
* @return true valid channel
* @return false not found a valid channel
*/
bool QN8066::isValidRxChannel() {
qn8066_status1 s1;
s1.raw = this->getRegister(QN_STATUS1);
return !(s1.arg.rxcca_fail);
}
/**
* @ingroup group03 RX
* @brief Receiver status
* @return true - Is receiving
*/
bool QN8066::isRxReceiving() {
qn8066_status1 s1;
s1.raw = this->getRegister(QN_STATUS1);
return s1.arg.RXSTATUS;
}
/**
* @ingroup group03 RX
* @brief RX AGC Status
* @details RX AGC Settling status
* @return true - Settled / Stabled
*/
bool QN8066::isRxAgcStable() {
qn8066_status1 s1;
s1.raw = this->getRegister(QN_STATUS1);
return s1.arg.RXAGCSET;
}
/**
* @ingroup group03 RX
* @brief Stereo receiving status
* @return true - Stereo
*/
bool QN8066::isRxStereo() {
qn8066_status1 s1;
s1.raw = this->getRegister(QN_STATUS1);
return !(s1.arg.ST_MO_RX);
}
/**
* @ingroup group03 RX
* @brief Scans a station
* @details Searches for a station within a specified frequency range.
* @param startFrequency - initial frequency to start the search.
* @param stopFrequyency - final frequency to stop the search.
* @param frequencyStep - 0 = 50KHz; 1 = 100KHz; 2 = 200KHz
*/
void QN8066::scanRxStation(uint16_t startFrequency, uint16_t stopFrequyency, uint8_t frequencyStep ) {
qn8066_ch_start start;
qn8066_ch_stop stop;
qn8066_ch_step step;
step.raw = this->getRegister(QN_CH_STEP);
int16_t auxFreq = (startFrequency - 600) * 2;
start.CH_START = 0B0000000011111111 & auxFreq;
step.arg.CH_STA = auxFreq >> 8;
auxFreq = (stopFrequyency - 600) * 2;
stop.CH_STOP = 0B0000000011111111 & auxFreq;
step.arg.CH_STP = auxFreq >> 8;
step.arg.CH_FSTEP = frequencyStep;
this->setRegister(QN_CH_START,start.raw);
this->setRegister(QN_CH_STOP, stop.raw);
this->setRegister(QN_CH_STEP, step.raw);
}
/**
* @defgroup group031 RX RDS Functions
* @brief QN8066 RDS Receiver
* @details Functions to decode RDS service
*/
/**
* @defgroup group031 RX RDS Functions
* @brief Enables RX RDS
* @todo under construction...
*/
void QN8066::rdsRxEnable(bool value) {
this->system2.arg.rx_rdsen = value;
this->setRegister(QN_SYSTEM2, this->system2.raw);
}
/**
* @defgroup group031 RX RDS Functions
* @brief Gets the current Program Station
* @todo under construction...
*/
char* QN8066::rdsRxGetPS(char *ps) {
return ps;
}
/**
* @defgroup group031 RX RDS Functions
* @brief Gets the current Radio Text
* @todo under construction...
*/
char* QN8066::rdsRxGetRT(char *rt) {
return rt;
}
/**
* @defgroup group031 RX RDS Functions
* @brief Gets the current Date and time
* @todo under construction...
*/
char* QN8066::rdsRxGetTime(char *time) {
return time;
}
/**
* @defgroup group04 TX Functions
* @details The functions defined in this group are responsible for controlling the transmitting of the QN8066.
*/
/**
* @ingroup group04 Start TX
* @brief Sets the TX mode
* @details To avoid working with the float data type, the frequency parameter must be the desired frequency multiplied by 10.
* @details For example, if the user wants to tune to 106.9 MHz, the parameter to be sent is 1067.
* @details This approach reduces the size of the final code (binary) as well as avoids the inaccuracies of floating-point mathematical operations.
* @param frequency - Frequency to be set
* @details Example
* @code
* #include <QN8066.h>
* QN8066 tx;
* void setup() {
* tx.setup();
* tx.setTX(1069); // Set the transmitter to 106.9 MHz
* }
*
* void loop() {
* }
* @endcode
* @todo Under improvements - Need to be optimized to improve space size
*/
void QN8066::setTX(uint16_t frequency) {
this->system1.raw = 0B11100011;
this->setRegister(QN_SYSTEM1, this->system1.raw); // SYSTEM1 => 11100011 => swrst = 1; recal = 1; stnby = 1; ccs_ch_dis = 1; cca_ch_dis = 1
this->setRegister(QN_SYSTEM2, this->system2.raw);
this->system2.arg.rdsrdy = !(this->system2.arg.rdsrdy); // Toggle
this->setRegister(QN_SYSTEM2, this->system2.raw);
this->setRegister(QN_CCA, this->cca.raw); // CCA => 01010000 => xtal_inj = 0; imr = 1; SNR_CCA_TH = 010000
// Sets the crystal oscillator divider
this->setRegister(QN_XTAL_DIV0, this->xtal_div & 0xFF); // Lower 8 bits of xtal_div[10:0].
this->setRegister(QN_XTAL_DIV1, (this->xtal_div >> 8) | 0B0001000); // Higher 3 bits of xtal_div[10:0].
this->setRegister(QN_XTAL_DIV2, 0B01011100); // XTAL_DIV2 = > 01011100 (It is the default value)
// this->setRegister(QN_SYSTEM1, this->system1.raw); // SYSTEM1 => 00001011 => Set TX On
this->setRegister(QN_FDEV, this->fdev.raw); // FDEV => 01111101 => 125 (Decimal)
this->setRegister(QN_RDS, this->rds.raw); // RDS => 00111100 => Line_in_en = 0; RDSFDEV = 60 (Decimal)
this->setRegister(QN_GPLT, this->gplt.raw); // GPLT => 00111001 => Tx_sftclpth = 00 (12’d2051 - 3db back off from 0.5v); t1m_sel = 11 (Infinity); GAIN_TXPLT = 1001 (9% 75 kHz)
int16_t auxFreq = (frequency - 600) * 2;
this->int_ctrl.raw = 0B00100000 | auxFreq >> 8;
this->setRegister(QN_INT_CTRL,this->int_ctrl.raw );
this->setRegister(QN_TXCH, 0B11111111 & auxFreq);
// Checking unkown registers
// this->setRegister(0x49, 0B11101000);
this->setRegister(0x49, 0B11011111);
this->setRegister(0x6E, 0B11111111);
this->system1.raw = 0B00001011;
this->setRegister(QN_SYSTEM1, this->system1.raw); // SYSTEM1 => 00001011 => txreq = 1; ccs_ch_dis = 1; cca_ch_dis = 1
// this->setRegister(QN_REG_VGA, 0B01011011); // REG_VGA => 01011011 => Tx_sftclpen = 0; TXAGC_GVGA = 101; TXAGC_GDB = 10; RIN = 11 (80K)
this->setRegister(QN_REG_VGA, this->reg_vga.raw); // REG_VGA => 01011011 => Tx_sftclpen = 0; TXAGC_GVGA = 101; TXAGC_GDB = 10; RIN = 11 (80K)
delay(100);
}
/**
* @ingroup group02 Init Device
* @brief QN8066 initial configuration of the of reference clock source
* @details Reference clock source based on the active crystal or signal generator you are using.
* @details The table below shows some tested sources (active crystal or signal generator) and divider values
* | Source (kHz) | Divider |
* | -------------- | ------------- |
* | 32.768 | 1 |
* | < 1,310.720 | did not work |
* | 1,310.720 | 40 |
* | 1,638.400 | 50 |
* | 3,276.800 | 100 |
* | 13,107.200 | 400 |
* | 16,384.000 | 500 |
* | 32,768,000 | 1000 |
* @param xtalDiv - Divider based on frequency select of reference clock source. Default 1000 (see Datasheet pag. 18 and 23).
* @param xtalInj - type of the reference clock source (0 = Inject sine-wave clock; 1 = Inject digital clock).
* @param imageRejection - Image Rejection (0=LO<RF, image is in lower side; 1=LO>RF, image is in upper side).
* @details Example
* @code
* #include <QN8066.h>
* QN8066 tx;
* void setup() {
* tx.begin();
* tx.setXtal(1,1,1);
* tx.setTX(1069); // Set the transmitter to 106.9 MHz
* }
*
* void loop() {
* }
* @endcode
*/
void QN8066::setXtal(uint16_t divider, uint8_t xtalInj, uint8_t imageRejection) {
this->xtal_div = divider;
this->cca.arg.xtal_inj = xtalInj;
this->cca.arg.imr = imageRejection;
}
/**
* @ingroup group04 TX Setup
* @brief Set TX Stereo or Mono
* @details
* @param value (true = stereo; false = mono)
* @details Example
* @code
* #include <QN8066.h>
* QN8066 tx;
* void setup() {
* tx.setup();
* tx.setTX(1069); // Set the transmitter to 106.9 MHz
* tx.setTxStereo(true);
* }
*
* void loop() {
* }
* @endcode
*/
void QN8066::setTxStereo( bool value ) {
qn8066_system2 system2;
system2.raw = this->getRegister(QN_SYSTEM2);
system2.arg.tx_mono = !value;
this->setRegister(QN_SYSTEM2, system2.raw);
this->system2 = system2;
}
/**
* @ingroup group04 TX Setup
* @brief Set TX Stereo or Mono (Same setTxStereo )
* @details
* @param value (1 = Mono, 0 = Stereo )
* @details Example
* @code
* #include <QN8066.h>
* QN8066 tx;
* void setup() {
* tx.setup();
* tx.setTX(1069); // Set the transmitter to 106.9 MHz
* tx.setTxMono(0); // Sets Stereo
* }
*
* void loop() {
* }
* @endcode
*/
void QN8066::setTxMono(uint8_t value) {
qn8066_system2 system2;
system2.raw = this->getRegister(QN_SYSTEM2);
system2.arg.tx_mono = value;
this->setRegister(QN_SYSTEM2, system2.raw);
this->system2 = system2;
}
/**
* @ingroup group04 TX Setup
* @brief Gets TX Stereo or Mono setup
* @return uint8_t - 1 = Mono; 0 = Stereo
* @details Example
* @code
* #include <QN8066.h>
* QN8066 tx;
* void setup() {
* tx.setup();
* tx.setTX(1069); // Set the transmitter to 106.9 MHz
* if ( tx.getTxMono() ) {
* // It is in mono mode
* }
* }
*
* void loop() {
* }
* @endcode
*/
uint8_t QN8066::getTxMono() {
return this->system2.arg.tx_mono;
}
/**
* @ingroup group04 TX Setup
* @brief Pre-emphasis and de-emphasis time constant
* @details The valid values are 50 and 75. Any value not equal to 75 sets the Pre-emphasis to 50.
* @param value (valids values: 50 or 75);
* @details Example
* @code
* #include <QN8066.h>
* QN8066 tx;
* void setup() {
* tx.setup();
* tx.setTX(1069); // Set the transmitter to 106.9 MHz
* tx.setTxPreEmphasis(75);
* }
*
* void loop() {
* }
* @endcode
*/
void QN8066::setTxPreEmphasis( uint8_t value ) {
qn8066_system2 system2;
system2.raw = this->getRegister(QN_SYSTEM2);
system2.arg.tc = (value == 75);
this->setRegister(QN_SYSTEM2, system2.raw);
this->system2 = system2;
}
/**
* @ingroup group04 TX Setup
* @brief Pre-emphasis and de-emphasis time constant - Same setTxPreEmphasis.
* @details The valid values are 0 and 1.
* @param value (valids values: 0 = 50us or 1 = 75us );
* @details Example
* @code
* #include <QN8066.h>
* QN8066 tx;
* void setup() {
* tx.setup();
* tx.setTX(1069); // Set the transmitter to 106.9 MHz
* tx.setPreEmphasis(1); // 75 us
* }
*
* void loop() {
* }
* @endcode
*/
void QN8066::setPreEmphasis( uint8_t value ) {
qn8066_system2 system2;
system2.raw = this->getRegister(QN_SYSTEM2);
system2.arg.tc = value;
this->setRegister(QN_SYSTEM2, system2.raw);
this->system2 = system2;
}
/**
* @ingroup group04 TX Setup
* @brief Gain of TX pilot to adjust pilot frequency deviation.
* @details Refers to peak frequency deviation of MPX signal when audio input is full scale. Valid values: between 7 and 10.
* @details the frequency deviation is value (%) * 75 kHz.
* @param value
* @details Example
* @code
* #include <QN8066.h>
* QN8066 tx;
* void setup() {
* tx.setup();
* tx.setTX(1069); // Set the transmitter to 106.9 MHz
* tx.setTxPilotGain(7); // 7 * 75 kHz
* }
*
* void loop() {
* }
* @endcode
*/
void QN8066::setTxPilotGain(uint8_t value) {
qn8066_gplt gptl;
if (value > 6 && value < 11) {
gptl.raw = this->getRegister(QN_GPLT);
gptl.arg.GAIN_TXPLT = value;
this->setRegister(QN_GPLT, gptl.raw);
this->gplt = gptl;
}
}
/**
* @ingroup group04 TX Setup
* @brief TX soft clip threshold
* @details See table below.
* | tx_sftclpth | value |
* | ---------- | ----- |
* | 0 - 00 | 12’d2051 (3db back off from 0.5v) |
* | 1 - 01 | 12’d1725 (4.5db back off from 0.5v) |
* | 2 - 10 | 12’d1452 (6db back off from 0.5v) |
* | 3 - 11 | 12’d1028 (9db back off from 0.5v) |
* @param value
* @details Example
* @code
* #include <QN8066.h>
* QN8066 tx;
* void setup() {
* tx.setup();
* tx.setTX(1069); // Set the transmitter to 106.9 MHz
* tx.setTxSoftClipThreshold(2); // 2 = 12’d1452 (6db back off from 0.5v)
* }
*
* void loop() {
* }
* @endcode
*/
void QN8066::setTxSoftClipThreshold(uint8_t value) {
qn8066_gplt gptl;
gptl.raw = this->getRegister(QN_GPLT);
gptl.arg.tx_sftclpth = value;
this->setRegister(QN_GPLT, gptl.raw);
this->gplt = gptl;
}
/**
* @ingroup group04 TX Setup
* @brief Set of 1 minute time for PA off when no audio.
* @param value (true = Infinity (never turn it off); false = turn it ofafter about 1 minue )
* @details Example
* @code
* #include <QN8066.h>
* QN8066 tx;
* void setup() {
* tx.setup();
* tx.setTX(1069); // Set the transmitter to 106.9 MHz
* tx.setTxOffAfterOneMinuteNoAudio(false); // Do not sleep after 1 minute with no audio
* }
*
* void loop() {
* }
* @endcode
*/
void QN8066::setTxOffAfterOneMinuteNoAudio(bool value) {
qn8066_gplt gptl;
gptl.raw = this->getRegister(QN_GPLT);
gptl.arg.t1m_sel = (value)? 2:3;
this->setRegister(QN_GPLT, gptl.raw);
this->gplt = gptl;
}
/**
* @ingroup group04 TX Setup
* @brief Sets PA Off after 1 minute time when no audio.
* @details do almost the same setTxOffAfterOneMinuteNoAudio
* @param value (3 = Infinity (never turn it off); 2 = off after 59s; 1 = off after 58s; 0 = off after 57s )
* @details Example
* @code
* #include <QN8066.h>
* QN8066 tx;
* void setup() {
* tx.setup();
* tx.setTX(1069); // Set the transmitter to 106.9 MHz
* tx.setTxOffAfterOneMinute(3); // Never sleep
* }
*
* void loop() {
* }
* @endcode
*/
void QN8066::setTxOffAfterOneMinute(uint8_t value) {
qn8066_gplt gptl;
gptl.raw = this->getRegister(QN_GPLT);
gptl.arg.t1m_sel = value;
this->setRegister(QN_GPLT, gptl.raw);
this->gplt = gptl;
}
/**
* @ingroup group04 TX Setup
* @brief Sets volume control gain of analog portion
* @details Valid values are 0 to 7.
* @param value
* @details Example
* @code
* #include <QN8066.h>
* QN8066 tx;
* void setup() {
* tx.setup();
* tx.setTX(1069); // Set the transmitter to 106.9 MHz
* tx.setAudioAnalogGain(5); // -24 dB
* }