-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathKT0915.cpp
1032 lines (937 loc) · 32.4 KB
/
KT0915.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
/**
* @brief PU2CLR KT0915 Arduino Library
* @details KT0915 Arduino Library implementation. This is an Arduino library for the KT0915, BROADCAST RECEIVER.
* @details It works with I2C protocol and can provide an easier interface to control the KT0915 device.<br>
* @details This library was built based on KT0915 Datasheet from KTMicro (Monolithic Digital FM/MW/SW/LW Receiver Radio-on-a-Chip TM).
* @details This library can be freely distributed using the MIT Free Software model.
* @copyright Copyright (c) 2020 Ricardo Lima Caratti.
* @author Ricardo LIma Caratti (pu2clr@gmail.com)
*/
#include <KT0915.h>
/**
* @defgroup GA03 Basic Methods
* @section GA03 Basic
* @details Low level functions used to operate with the KT09XX registers
*/
/**
* @ingroup GA03
* @brief Set I2C bus address
*
* @param deviceAddress I2C address
*/
void KT0915::setI2CBusAddress(int deviceAddress)
{
this->deviceAddress = deviceAddress;
}
/**
* @ingroup GA03
* @brief Sets the a value to a given KT09XX register
*
* @param reg register number to be written (0x1 ~ 0x3C) - See #define REG_ in KT0915.h
* @param parameter content you want to store
*/
void KT0915::setRegister(int reg, uint16_t parameter)
{
word16_to_bytes param;
param.raw = parameter;
Wire.beginTransmission(this->deviceAddress);
Wire.write(reg);
Wire.write(param.refined.highByte);
Wire.write(param.refined.lowByte);
Wire.endTransmission();
delayMicroseconds(6000);
}
/**
* @ingroup GA03
* @brief Gets a given KT09XX register content
* @details It is a basic function to get a value from a given KT0915 device register
* @param reg register number to be read (0x1 ~ 0x3C) - See #define REG_ in KT0915.h
* @return the register content
*/
uint16_t KT0915::getRegister(int reg)
{
word16_to_bytes result;
Wire.beginTransmission(this->deviceAddress);
Wire.write(reg);
Wire.endTransmission(false);
delayMicroseconds(6000);
Wire.requestFrom(this->deviceAddress, 2);
result.refined.highByte = Wire.read();
result.refined.lowByte = Wire.read();
Wire.endTransmission(true);
delayMicroseconds(6000);
return result.raw;
}
/**
* @ingroup GA03
* @brief Gets the Device Id
* @return uint16_t 16 bits word with the device id number
*/
uint16_t KT0915::getDeviceId()
{
this->deviceId = getRegister(REG_CHIP_ID);
return this->deviceId;
}
/**
* @ingroup GA03
* @brief Gets the Crystal Status information
*
* @return true
* @return false
*/
bool KT0915::isCrystalReady()
{
kt09xx_statusa reg;
reg.raw = getRegister(REG_STATUSA);
return reg.refined.XTAL_OK;
}
/**
* @ingroup GA03
* @brief Sets the Crystal Type
* @details Configures the Crystal or reference clock you are using in your circuit.
* @details For a low frequency crystal oscillator, selects 32.768KHz or 38KHz crystals.
* @details Alternatively, you can use a CMOS level external reference clock may be used by setting
* @details the parameter ref_clock to 1 (REF_CLOCK_ENABLE) and setting the reference clock according to the table below.
* * Crystal type table
* | Dec | binary | Description | defined constant |
* | -- | ------ | ----------- | --------------- |
* | 0 | 0000 | 32.768KHz | OSCILLATOR_32KHZ |
* | 1 | 0001 | 6.5MHz | OSCILLATOR_6_5MHZ |
* | 2 | 0010 | 7.6MHz | OSCILLATOR_7_6MHZ |
* | 3 | 0011 | 12MHz | OSCILLATOR_12MHZ |
* | 4 | 0100 | 13MHz | OSCILLATOR_13MHZ |
* | 5 | 0101 | 15.2MHz | OSCILLATOR_15_2MHZ |
* | 6 | 0110 | 19.2MHz | OSCILLATOR_19_2MHZ |
* | 7 | 0111 | 24MHz | OSCILLATOR_24MHZ |
* | 8 | 1000 | 26MHz | OSCILLATOR_26MHZ |
* | 9 | 1001 | ?? 38KHz ?? | OSCILLATOR_38KHz |
*
* @param crystal Reference Clock Selection. See table above.
* @param ref_clock 0 = Crystal (default); 1 = Reference clock enabled.
*/
void KT0915::setReferenceClockType(uint8_t crystal, uint8_t ref_clock)
{
kt09xx_amsyscfg reg;
reg.raw = getRegister(REG_AMSYSCFG); // Gets the current value of the register
reg.refined.REFCLK = crystal; // Changes just crystal parameter
reg.refined.RCLK_EN = ref_clock; // Reference Clock Enable => Crystal
setRegister(REG_AMSYSCFG, reg.raw); // Strores the new value to the register
this->currentRefClockType = crystal;
this->currentRefClockEnabled = ref_clock;
}
/**
* @ingroup GA03
* @brief Sets the enable pin (9) of the KT0915 high or low
* @details This function can be used to enable (1) and disable (0) the KT0915 device. You have to select a MCU (Arduino) pin for this function.
* @details Also, you can set -1 to used this control via circuit.
*
* @see setup
*
* @param on_off 1 = enable; 0 = disable
*/
void KT0915::enable(uint8_t on_off)
{
if (this->enablePin < 0)
return;
pinMode(this->enablePin, OUTPUT);
digitalWrite(this->enablePin, on_off);
delay(10);
}
/**
* @ingroup GA03
* @todo You need to check mechanical Tune features
* @brief Sets Tune Dial Mode Interface On
* @details This method sets the KT0915 to deal with a mechanical tuning via an external 100K resistor.
* @details KT0915 supports a unique Dial Mode (mechanical tuning wheel with a variable resistor) which is
* @details enabled by GPIO1 to 2 (10). The dial can be a variable resistor with the tap connected to CH (pin 1).
*
* @see KT0915; Monolithic Digital FM/MW/SW/LW Receiver Radio on a Chip(TM); pages 19 and 20.
*
* @param minimu_frequency Start frequency for the user band
* @param maximum_frequency Final frequency for the user band
*/
void KT0915::setTuneDialModeOn(uint32_t minimu_frequency, uint32_t maximum_frequency)
{
kt09xx_amsyscfg reg;
kt09xx_gpiocfg gpio;
kt09xx_userstartch uf;
kt09xx_userguard ug;
kt09xx_userchannum uc;
reg.raw = getRegister(REG_AMSYSCFG); // Gets the current value from the register
reg.refined.USERBAND = this->currentDialMode = DIAL_MODE_ON;
setRegister(REG_AMSYSCFG, reg.raw); // Strores the new value in the register
gpio.raw = getRegister(REG_GPIOCFG); // Gets the current value from the register
gpio.refined.GPIO1 = 2; // Sets Dial Mode interface (pin 1/CH)
setRegister(REG_GPIOCFG, gpio.raw); // Stores the new value in the register
// TODO: Sets the frequency limits for user and
if (this->currentMode == MODE_AM)
{
uf.refined.USER_START_CHAN = minimu_frequency;
uc.refined.USER_CHAN_NUM = (maximum_frequency - minimu_frequency) / this->currentStep;
ug.refined.USER_GUARD = 0x0011;
}
else
{
uf.refined.USER_START_CHAN = minimu_frequency / 50;
uc.refined.USER_CHAN_NUM = ((maximum_frequency - minimu_frequency) / 50) / this->currentStep;
ug.refined.USER_GUARD = 0x001D;
}
setRegister(REG_USERSTARTCH, uf.raw);
setRegister(REG_USERGUARD, ug.raw);
setRegister(REG_USERCHANNUM, uc.raw);
};
/**
* @ingroup GA03
* @brief Turns the Tune Dial Mode interface Off
* @see setTuneDialModeOn, setKeyMode, setKeyControl
* @see KT0915; Monolithic Digital FM/MW/SW/LW Receiver Radio on a Chip(TM); page 20.
*/
void KT0915::setTuneDialModeOff()
{
kt09xx_amsyscfg reg;
kt09xx_gpiocfg gpio;
reg.raw = getRegister(REG_AMSYSCFG); // Gets the current value of the register
reg.refined.USERBAND = this->currentDialMode = DIAL_MODE_OFF;
setRegister(REG_AMSYSCFG, reg.raw); // Strores the new value to the register
gpio.raw = getRegister(REG_GPIOCFG); // Gets the current value of the register
gpio.refined.GPIO1 = 0; // Sets MCU (Arduino) control (High Z)
setRegister(REG_GPIOCFG, gpio.raw); // Stores the new value in the register
}
/**
* @ingroup GA03
* @brief Sets Volume Dial Mode Interface On
* @details This method sets the KT0915 to deal with a mechanical volume control via an external 100K resistor.
* @details KT0915 supports a unique Dial Mode which is enabled by GPIO2 to 2 (10).
* @details The dial can be a variable resistor with the tap connected to VOL (pin 16).
*
* @see KT0915; Monolithic Digital FM/MW/SW/LW Receiver Radio on a Chip(TM); page 20.
*/
void KT0915::setVolumeDialModeOn()
{
kt09xx_gpiocfg gpio;
gpio.raw = getRegister(REG_GPIOCFG); // Gets the current value from the register
gpio.refined.GPIO2 = 2; // Sets Dial Mode interface (pin 16/VOL)
setRegister(REG_GPIOCFG, gpio.raw); // Stores the new value in the register
};
/**
* @ingroup GA03
* @brief Turns the Volume Dial Mode interface Off
* @see setVolumeDialModeOn, setKeyMode, setKeyControl
* @see KT0915; Monolithic Digital FM/MW/SW/LW Receiver Radio on a Chip(TM); page 20.
*/
void KT0915::setVolumeDialModeOff()
{
kt09xx_gpiocfg gpio;
gpio.raw = getRegister(REG_GPIOCFG); // Gets the current value of the register
gpio.refined.GPIO2 = 0; // Sets to MCU (Arduino) control (High Z)
setRegister(REG_GPIOCFG, gpio.raw); // Stores the new value in the register
}
/**
* @ingroup GA03
* @todo Check this function
* @brief Key Mode setup
* @details KT0915 allows user to control the channel and volume by using keys/buttons to send digital control signals to CH and VOL pins. Please refer to Section 4 for a typical application circuit. The key mode is enabled by setting GPIO1<1:0> and GPIO2<1:0> to 01.
* @details Mode A:
* @details If KEY_MODE<1:0> is set to 00, Mode A is selected. In this mode, each time the CHP (CHM) is pressed, the channel frequency increases (decreases) by one step. The step sizes are defined by FMSPACE<1:0> and AMSPACE<1:0>. If the CHP (CHM) key is pressed for and held for a certain time (defined by TIME1<1:0>), the channel frequency will continue to increase (decrease) automatically at a certain pace (as defined by TIME2<2:0>) until the key is released.
* @details Mode B:
* @details If KEY_MODE<1:0> is set to 01, Mode B is selected. In this mode, each time the CHP (CHM) is pressed, the channel increases (decreases) by one step. The step sizes are defined by FMSPACE<1:0> and AMSPACE<1:0>. If the CHP (CHM) key is pressed and held for a specific time (TIME1<1:0>), the channel will continue to increase (decrease) automatically at a certain pace (TIME2<2:0>) even if the key is released. The movement is stopped when the key is pressed again.
* @see KT0915; Monolithic Digital FM/MW/SW/LW Receiver Radio on a Chip(TM); pages 8 and 23.
* @param value 00 = Working mode A; 01 = Working mode B; Others = Reserved
*/
void KT0915::setKeyMode(uint8_t value)
{
kt09xx_amcfg r;
r.raw = getRegister(REG_AMCFG); // Gets the current value of the register
r.refined.KEY_MODE = value; // Set the key mode
setRegister(REG_AMCFG, r.raw); // Stores the new value in the register
}
/**
* @ingroup GA03
* @todo Check this function
* @brief Sets the Audio and Channel Key Control
* @details VOL Pin Mode Selection 00 = High Z; 01 = Key controlled volume increase/decrease; 10 = Dial controlled volume increase/decrease; 11 = Reserved
* @details CH Pin Mode Selection; 00 = High Z; 01 = Key controlled channel increase / decrease; 10 = Dial controlled channel increase / decrease; 11 = Reserved
* @see KT0915; Monolithic Digital FM/MW/SW/LW Receiver Radio on a Chip(TM); page 20.
* @param audioControl VOL Pin Mode Selection; 00 = High Z; 01 = Key controlled volume increase/decrease; 10 = Dial controlled volume increase/decrease; 11 = Reserved
* @param channelControl CH Pin Mode Selection; 00 = High Z; 01 = Key controlled channel increase / decrease; 10 = Dial controlled channel increase / decrease 11 = Reserved
*/
void KT0915::setKeyControl(uint8_t audioControl, uint8_t channelControl)
{
kt09xx_gpiocfg r;
r.refined.RESERVED = 0; // Always 0; I need check it
r.refined.GPIO1 = channelControl; //
r.refined.GPIO2 = audioControl; //
setRegister(REG_GPIOCFG, r.raw); // Stores the new value in the register
}
/**
* @ingroup GA03
* @brief Audio Gain
* @details This function set the audio gain you want to use. See table below.
* | value | Gain Selection |
* | ----- | -------------- |
* | 0 | 3dB |
* | 1 | 6dB |
* | 2 | -3dB |
* | 3 | 0dB |
*
* @param gain See table above
*/
void KT0915::setAudioGain(uint8_t gain)
{
kt09xx_amsyscfg r;
r.raw = getRegister(REG_AMSYSCFG);
r.refined.AU_GAIN = gain;
setRegister(REG_AMSYSCFG, r.raw);
}
/**
* @ingroup GA03
* @brief Sets the audio volume level
* @details This method is used to control the audio volume level. The value 0 mutes the device and 31 sets the device to the maximum volume.
* @param volume between 0 and 31.
*/
void KT0915::setVolume(uint8_t volume)
{
kt09xx_rxcfg rx;
rx.raw = getRegister(REG_RXCFG);
rx.refined.VOLUME = volume;
setRegister(REG_RXCFG, rx.raw);
this->currentVolume = volume;
}
/**
* @ingroup GA03
* @brief Increases the audio volume
*
*/
void KT0915::setVolumeUp()
{
if (this->currentVolume == 31)
return;
setVolume(this->currentVolume + 1);
}
/**
* @ingroup GA03
* @brief Decreases the audio volume
*
*/
void KT0915::setVolumeDown()
{
if (this->currentVolume == 0)
return;
setVolume(this->currentVolume - 1);
}
/**
* @ingroup GA03
* @brief Returns the current audio volume
* @details Returns a value between 0 and 31.
* @return uint8_t Value between 0 and 31.
*/
uint8_t KT0915::getVolume()
{
return this->currentVolume;
}
/**
* @ingroup GA03
* @brief Sets the bass level
* @details Bass Boost Effect Mode Selection
* | Value | Level |
* | ----- | ------- |
* | 0 | Disable |
* | 1 | Low |
* | 2 | Med |
* | 3 | High |
*
* @param on_off see table above
*/
void KT0915::setAudioBass(uint8_t bass)
{
kt09xx_volume v;
v.raw = getRegister(REG_VOLUME);
v.refined.BASS = bass;
setRegister(REG_VOLUME, v.raw); // Stores the new values of the register
}
/**
* @brief Sets the output audio mute
*
* @param mute_on_off 1 = mute; 0 unmute
*/
void KT0915::setAudioMute(uint8_t mute_on_off)
{
kt09xx_volume v;
v.raw = getRegister(REG_VOLUME);
v.refined.DMUTE = !mute_on_off;
setRegister(REG_VOLUME, v.raw); // Stores the new values of the register
}
/**
* @ingroup GA03
* @brief Sets Audio DAC Anti-pop Configuration
*
* @details Bass Boost Effect Mode Selection
* | Value | AC - coupling capacitor |
* | ----- | ------------------------- |
* | 0 | 100uF |
* | 1 | 60uF |
* | 2 | 20uF |
* | 3 | 10uF |
*
* @param on_off see table above
*/
void KT0915::setAudioAntiPop(uint8_t value)
{
kt09xx_volume v;
v.refined.POP = value;
setRegister(REG_VOLUME, v.raw); // Stores the new values of the register
}
/**
* @ingroup GA03
* @brief Sets the Left Channel Inverse Control
* @details If enable, inverts the left channel audio signal
* @details A fully differential audio signal can be got from LOUT an ROUT if the INV_LEFT_AUDIO bit and MONO bit are set to 1.
* @param value ENABLE_ON (1); ENABLE_OFF (0)
*/
void KT0915::setLeftChannelInverseControl(uint8_t enable_disable)
{
kt09xx_amdsp r;
r.raw = getRegister(REG_AMDSP);
r.refined.INV_LEFT_AUDIO = enable_disable;
setRegister(REG_AMDSP,r.raw);
}
/**
* @ingroup GA03
* @brief Receiver startup
* @details You have to use this method to configure the way that the device will work. For example: enable and disable device control; oscillator type and reference clock type (crystal or external)
* @details The tabe below shows the oscillator frequencies supported by the device.
* @details If you omit the crystal type parameter, will be considered 0 (32.768KHz).
* @details For a low frequency crystal oscillator, selects 32.768KHz or 38KHz crystals.
* @details Alternatively, you can use a CMOS level external reference clock may be used by setting
* @details the parameter ref_clock to 1 (REF_CLOCK_ENABLE) and setting the reference clock according to the table below.
* @details The code below shows how to use the setup function.
* @details the enable_pin parameter sets the way you are controlling the KT0915 pin 9.
*
* @code
* #include <KT0915.h>
* #define RESET_PIN 12 // set it to -1 if you want to use the RST pin of your MCU.
* KT0915 radio;
* void setup() {
* // Set the parameter enablePin to -1 if you are controlling the enable status via circuit; Select OSCILLATOR_32KHZ, OSCILLATOR_12MHZ etc
* // radio.setup(RESET_PIN); Instead the line below, if you use this line, the crystal type considered will be 32.768KHz.
* radio.setup(RESET_PIN, OSCILLATOR_12MHZ, REF_CLOCK_DISABLE );
* }
* @endcode
*
* Oscillator frequencies supported
* | Dec | binary | Description | defined constant |
* | -- | ------ | ----------- | --------------- |
* | 0 | 0000 | 32.768KHz | OSCILLATOR_32KHZ |
* | 1 | 0001 | 6.5MHz | OSCILLATOR_6_5MHZ |
* | 2 | 0010 | 7.6MHz | OSCILLATOR_7_6MHZ |
* | 3 | 0011 | 12MHz | OSCILLATOR_12MHZ |
* | 4 | 0100 | 13MHz | OSCILLATOR_13MHZ |
* | 5 | 0101 | 15.2MHz | OSCILLATOR_15_2MHZ |
* | 6 | 0110 | 19.2MHz | OSCILLATOR_19_2MHZ |
* | 7 | 0111 | 24MHz | OSCILLATOR_24MHZ |
* | 8 | 1000 | 26MHz | OSCILLATOR_26MHZ |
* | 9 | 1001 | 38KHz | OSCILLATOR_38KHz |
*
* @see KT0915; Monolithic Digital FM/MW/SW/LW Receiver Radio on a Chip(TM); 3.6 Crystal and reference clock; page 9.
* @see setReferenceClockType
*
* @param enablePin if >= 0, then you control the device enable or disable status. if -1, you are using the circuit to crontole that.
* @param oscillator_type oscillator type. You can use crystal or external clock. See comments and table above.
* @param ref_clock set to 0 if you are using crystal (Reference clock disabled - default); set to 1 if you are using an external reference clock.
*/
void KT0915::setup(int enable_pin, uint8_t oscillator_type, uint8_t ref_clock)
{
this->enablePin = enable_pin;
enable(1);
setReferenceClockType(oscillator_type, ref_clock);
setVolume(this->currentVolume);
}
/**
* @defgroup GA04 Tune Methods
* @section GA04 Tune Methods
* @details Methods to tune and set the receiver mode
*/
/**
* @ingroup GA04
* @brief Sets the receiver Stereo or Mono
* @details Set this parameter to true to force mono or false to stereo
* @param on_off true = mono; fale = stereo
*/
void KT0915::setMono(bool on_off)
{
kt09xx_dspcfga reg;
reg.raw = getRegister(REG_DSPCFGA);
reg.refined.MONO = on_off;
setRegister(REG_DSPCFGA, reg.raw);
}
/**
* @ingroup GA04
* @brief Return true if the stereo indicator is set to 3;
* @return true is stereo
*/
bool KT0915::isFmStereo()
{
kt09xx_statusa r;
r.raw = getRegister(REG_STATUSA);
return (r.refined.ST == 3);
}
/**
* @ingroup GA04
* @brief Gets the current FM RSSI
*
* @return int RSSI value
*/
int KT0915::getFmRssi()
{
kt09xx_statusa r;
r.raw = getRegister(REG_STATUSA);
return (r.refined.FMRSSI * 3);
};
/**
* @ingroup GA04
* @brief Gets the current AM RSSI
*
* @return int RSSI value
*/
int KT0915::getAmRssi()
{
kt09xx_amdstatusa r;
r.raw = getRegister(REG_AMSTATUSA);
return (r.refined.AMRSSI * 3);
};
/**
* @ingroup GA04
* @brief Gets current SNR value
*
* @return int FM SNR value
*/
int KT0915::getFmSnr()
{
kt09xx_statusc r;
r.raw = getRegister(REG_STATUSC);
return (r.refined.FMSNR);
};
/**
* @ingroup GA04
* @brief Sets the De-emphasis Time Constant Selection
* @param value 0 = 75us; 1 = 50us
*/
void KT0915::setDeEmphasis(uint8_t value)
{
kt09xx_dspcfga reg;
reg.raw = getRegister(REG_DSPCFGA);
reg.refined.DE = value;
setRegister(REG_DSPCFGA, reg.raw);
}
/**
* @ingroup GA04
* @brief Sets FM AFC Disable Control
* @details This function inverts the register enable/disable concept. So, here, 1 means enable and 0 disable.
* @param value true = enable AFC; false = disable AFC.
*/
void KT0915::setFmAfc(bool value)
{
kt09xx_locfga r;
r.refined.FMAFCD = !value;
setRegister(REG_LOCFGA, r.raw);
}
/**
* @ingroup GA04
* @todo Check the value of RESERVED1 and audio setup
* @brief Sets AM AFC Disable Control
* @details This function inverts the register enable/disable concept. So, here, 1 means enable and 0 disable.
* @see @see KT0915; Monolithic Digital FM/MW/SW/LW Receiver Radio on a Chip(TM); page 19
* @param value true = enable AFC; false = disable AFC.
*/
void KT0915::setAmAfc(bool value)
{
kt09xx_amsyscfg r;
r.raw = getRegister(REG_AMSYSCFG); // Gets the current value of the register
r.refined.RESERVED1 = 1; // See page 19.
r.refined.AMAFCD = !value;
setRegister(REG_AMSYSCFG, r.raw);
}
/**
* @ingroup GA04
* @brief Sets the AM Space
* @details Selects AM channel space
*
* | value | space |
* | ----- | ----- |
* | 0 | 1KHz |
* | 1 | 9KHz |
* | 2 | 10KHz |
* | 3 | 10KHz |
*
* @param value See table above
*/
void KT0915::setAmSpace(uint8_t value)
{
kt09xx_amcfg r;
r.raw = getRegister(REG_AMCFG);
this->currentAmSpace = r.refined.AMSPACE = value;
setRegister(REG_AMCFG, r.raw);
}
/**
* @ingroup GA04
* @brief Sets the FM Space
* @details FM Channel Spacing; 00 = 200KHz; 01 = 100KHz; 10 = 50KHz
* @details Selects FN channel space
*
* | value | space KHz |
* | ----- | --------- |
* | 0 | 200 |
* | 1 | 100 |
* | 2 | 50 |
* | 3 | ? |
*
* @param value See table above
*/
void KT0915::setFmSpace(uint8_t value)
{
kt09xx_seek r;
r.raw = getRegister(REG_SEEK);
this->currentFmSpace = r.refined.FMSPACE = value;
setRegister(REG_SEEK, r.raw);
}
/**
* @ingroup GA04
* @todo Check the reserved bits [0:2]
* @brief Sets AM Channel Bandwidth Selection
* @details Configures the AM Bandwidth
*
* | value | Bandwidth |
* | ----- | --------- |
* | 0 | 2KHz |
* | 1 | 2KHz |
* | 2 | 4KHz |
* | 3 | 6KHz |
*
* @param value See table above
*/
void KT0915::setAmBandwidth(uint8_t value)
{
kt09xx_amdsp r;
r.raw = getRegister(REG_AMDSP);
r.refined.RESERVED1 = 0b100; // See page 21.
r.refined.AM_BW = value;
setRegister(REG_AMDSP, r.raw);
}
/**
* @ingroup GA04
* @brief Gets current AM Channel Bandwidth Selection
*
* | value | Bandwidth |
* | ----- | --------- |
* | 0 | 2KHz |
* | 1 | 2KHz |
* | 2 | 4KHz |
* | 3 | 6KHz |
*
* @return See table above
*/
uint8_t KT0915::getAmBandwidth()
{
kt09xx_amdsp r;
r.raw = getRegister(REG_AMDSP);
return r.refined.AM_BW;
}
/**
* @ingroup GA04
* @brief Sets the receiver to FM mode
* @details Configures the receiver on FM mode; Also sets the band limits, defaul frequency and step.
*
* @param minimum_frequency minimum frequency for the band
* @param maximum_frequency maximum frequency for the band
* @param default_frequency default freuency
* @param step increment and decrement frequency step
*/
void KT0915::setFM(uint32_t minimum_frequency, uint32_t maximum_frequency, uint32_t default_frequency, uint16_t step)
{
kt09xx_amsyscfg reg;
kt09xx_locfgc fm32;
this->currentStep = step;
this->currentFrequency = default_frequency;
this->minimumFrequency = minimum_frequency;
this->maximumFrequency = maximum_frequency;
this->currentMode = MODE_FM;
reg.raw = getRegister(REG_AMSYSCFG);
reg.refined.AM_FM = MODE_FM;
reg.refined.USERBAND = this->currentDialMode;
reg.refined.REFCLK = this->currentRefClockType;
reg.refined.RCLK_EN = this->currentRefClockEnabled;
setRegister(REG_AMSYSCFG, reg.raw); // Stores the new value in the register
// Select the right FM band (Campus Band or regular band)
fm32.raw = getRegister(REG_LOCFGC);
fm32.refined.CAMPUSBAND_EN = (maximum_frequency <= 64000)? 1:0;
setRegister(REG_LOCFGC, fm32.raw);
if (this->currentDialMode == DIAL_MODE_ON)
setTuneDialModeOn(minimum_frequency, maximum_frequency);
else
setFrequency(default_frequency);
};
/**
* @todo Check RESERVED values and AM audio setup
* @ingroup GA04
* @brief Sets the receiver to AM mode
* @details Configures the receiver on AM mode; Also sets the band limits, defaul frequency and step.
*
* @param minimum_frequency minimum frequency for the band
* @param maximum_frequency maximum frequency for the band
* @param default_frequency default freuency
* @param step increment and decrement frequency step
*/
void KT0915::setAM(uint32_t minimum_frequency, uint32_t maximum_frequency, uint32_t default_frequency, uint16_t step, uint8_t am_space)
{
kt09xx_amsyscfg reg;
this->currentStep = step;
this->currentFrequency = default_frequency;
this->minimumFrequency = minimum_frequency;
this->maximumFrequency = maximum_frequency;
this->currentMode = MODE_AM;
reg.raw = getRegister(REG_AMSYSCFG);
reg.refined.AM_FM = MODE_AM;
// reg.refined.RESERVED1 = 1; // TODO: check it (page 19)
reg.refined.USERBAND = this->currentDialMode;
reg.refined.REFCLK = this->currentRefClockType;
reg.refined.RCLK_EN = this->currentRefClockEnabled;
setRegister(REG_AMSYSCFG, reg.raw); // Stores the new value in the register
setAmSpace(am_space);
if (this->currentDialMode == DIAL_MODE_ON)
setTuneDialModeOn(minimum_frequency, maximum_frequency);
else
setFrequency(default_frequency);
}
/**
* @ingroup GA04
* @brief Sets the current frequency
*
* @param frequency
*/
void KT0915::setFrequency(uint32_t frequency)
{
kt09xx_amchan reg_amchan;
kt09xx_tune reg_tune;
if (this->currentMode == MODE_AM)
{
reg_amchan.refined.AMTUNE = 1; // TODO Check
reg_amchan.refined.AMCHAN = frequency;
setRegister(REG_AMCHAN, reg_amchan.raw);
}
else
{
reg_tune.refined.FMTUNE = 1; // // TODO Check
reg_tune.refined.RESERVED = 0;
reg_tune.refined.FMCHAN = frequency / 50;
setRegister(REG_TUNE, reg_tune.raw);
}
this->currentFrequency = frequency;
delay(30);
}
/**
* @ingroup GA04
* @brief Increments the frequency one step
* @details if the frequency plus the step value is greater than the maximum frequency for the band,
* @details tne current frequency will be set to minimum frequency.
*
* @see setFrequency
*/
void KT0915::frequencyUp()
{
this->currentFrequency += this->currentStep;
if (this->currentFrequency > this->maximumFrequency)
this->currentFrequency = this->minimumFrequency;
setFrequency(this->currentFrequency);
}
/**
* @ingroup GA04
* @brief Decrements the frequency one step
* @details if the frequency minus the step value is less than the minimum frequency for the band,
* @details tne current frequency will be set to minimum frequency.
*
* @see setFrequency
*/
void KT0915::frequencyDown()
{
this->currentFrequency -= this->currentStep;
if (this->currentFrequency < this->minimumFrequency)
this->currentFrequency = this->maximumFrequency;
setFrequency(this->currentFrequency);
};
/**
* @ingroup GA04
* @brief Sets the frequency step
* @details Sets increment and decrement frequency
* @param step Values: 1, 5, 9, 10, 100, 200 in KHz
*/
void KT0915::setStep(uint16_t step)
{
this->currentStep = step;
}
/**
* @ingroup GA04
* @brief Gets the current frequency
* @return frequency in KHz
*/
uint32_t KT0915::getFrequency()
{
return this->currentFrequency;
}
/**
* @ingroup GA04
* @brief Gets the FM Channel Setting
* @details This method returns the current channel value for FM tune.
* @details The channel value multiplied by 50 is the current FM frequency in KHz.
* @return FM Channel number
*/
uint16_t KT0915::getFmCurrentChannel() {
kt09xx_tune r;
r.raw = getRegister(REG_TUNE);
return r.refined.FMCHAN;
};
/**
* @ingroup GA04
* @brief Gets the current AM Channel Setting
* @details This method returns the current channel value for AM tune
* @details Actually this value is the AM current frequency in KHz
* @return AM Channel number (frequency in KHz)
*/
uint16_t KT0915::getAmCurrentChannel() {
kt09xx_amchan r;
r.raw = getRegister(REG_AMCHAN);
return r.refined.AMCHAN;
};
/**
* @todo Not enough information to do this so far.
* @ingroup GA04
* @brief Should Seek a station
* @details However, there no enough information to implement it.
*/
void KT0915::seekStation()
{
// TODO
}
/**
* @defgroup GA05 Softmute xetup
* @section GA05 Softmute methods
*/
/**
* @ingroup GA05
* @brief AM and FM Softmute control
* @details The internal KT0915 device register, FM and AM SMUTE 0 means enable and 1 disable. This function inverts this concept. So 1 means enable and 0 disable.
*
* @param value true = ON (enable); off = OFF disable
*/
void KT0915::setSoftMute(bool value)
{
kt09xx_volume v;
v.raw = getRegister(REG_VOLUME);
v.refined.AMDSMUTE = v.refined.FMDSMUTE = !value;
setRegister(REG_VOLUME, v.raw); // Stores the new values of the register
}
/**
* @ingroup GA05
* @brief Sets Softmute Attenuation
* @details Deal with Device Softmute setup
*
* Attenuation values
*
* | value | description |
* | ----- | ----------- |
* | 0 | Strong |
* | 1 | Strongest |
* | 2 | Weak |
* | 3 | Weakest |
*
* @see KT0915; Monolithic Digital FM/MW/SW/LW Receiver Radio on a Chip(TM); page 22
*
* @param value See the table above
*/
void KT0915::setSoftmuteAttenuation(uint8_t value)
{
kt09xx_softmute r;
r.raw = getRegister(REG_SOFTMUTE); // gets the current stored value
r.refined.SMUTEA = value; // chenges only the parameter Soft Mute Attenuation
setRegister(REG_SOFTMUTE, r.raw); // stores the new values of the register
}
/**
* @ingroup GA05
* @brief Sets Softmute Attack/Recover Rate
* @details Deal with Device Softmute Attack/Recover Rate setup
*
* Attenuation values
*
* | value | description |
* | ----- | ----------- |
* | 0 | Slowest |
* | 1 | Fastest (RSSI mode only) |
* | 2 | Fast |
* | 3 | Slow |
*
* @see KT0915; Monolithic Digital FM/MW/SW/LW Receiver Radio on a Chip(TM); page 22
*
* @param value See the table above
*/
void KT0915::setSoftmuteAttack(uint8_t value)
{
kt09xx_softmute r;
r.raw = getRegister(REG_SOFTMUTE); // gets the current stored value
r.refined.SMUTER = value; // chenges only the parameter
setRegister(REG_SOFTMUTE, r.raw); // stores the new values of the register
}
/**
* @ingroup GA05
* @brief Sets AM Softmute Start Level
* @details Deal with Device Softmute start level setup
* @see KT0915; Monolithic Digital FM/MW/SW/LW Receiver Radio on a Chip(TM); page 22
*
* @param value 000 - Lowest; 111 - Highest
*/
void KT0915::setAmSoftmuteStartLevel(uint8_t value)
{
kt09xx_softmute r;
r.raw = getRegister(REG_SOFTMUTE); // gets the current stored value
r.refined.AM_SMTH = value; // chenges only the parameter
setRegister(REG_SOFTMUTE, r.raw); // stores the new values of the register
}
/**
* @ingroup GA05
* @brief Sets FM Softmute Start Level
* @details Deal with Device Softmute start level setup
* @see KT0915; Monolithic Digital FM/MW/SW/LW Receiver Radio on a Chip(TM); page 22
*
* @param value 000 - Lowest; 111 - Highest
*/
void KT0915::setFmSoftmuteStartLevel(uint8_t value)
{
kt09xx_softmute r;