2
3
4
5
6
7
8
9
10
11
12
13
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
38
39
40
41
42
43
45 this->deviceAddress = i2c_addr;
49
50
51
52
53
54
55
57 this->pin_sdio = pin_sdio;
58 this->pin_sclk = pin_sclk;
62
63
64
67 pinMode(
this->pin_sdio, OUTPUT);
68 pinMode(
this->pin_sclk, OUTPUT);
69 digitalWrite(
this->pin_sdio, HIGH);
70 digitalWrite(
this->pin_sclk, HIGH);
73 digitalWrite(
this->pin_sdio, LOW);
75 digitalWrite(
this->pin_sclk, LOW);
77 digitalWrite(
this->pin_sdio, HIGH);
81
82
83
86 pinMode(pin_sdio, OUTPUT);
87 digitalWrite(
this->pin_sdio, LOW);
90 digitalWrite(
this->pin_sclk, HIGH);
93 digitalWrite(
this->pin_sdio, HIGH);
98
99
100
101
102
103
106 pinMode(pin_sdio, OUTPUT);
107 digitalWrite(
this->pin_sclk, LOW);
108 digitalWrite(
this->pin_sdio, LOW);
109 delayMicroseconds(1);
110 digitalWrite(
this->pin_sclk, HIGH);
111 delayMicroseconds(1);
112 digitalWrite(
this->pin_sclk, LOW);
116
117
118
119
122 pinMode(pin_sdio, OUTPUT);
124 digitalWrite(
this->pin_sclk, LOW);
125 digitalWrite(
this->pin_sdio, HIGH);
126 delayMicroseconds(1);
127 digitalWrite(
this->pin_sclk, HIGH);
128 delayMicroseconds(1);
129 digitalWrite(
this->pin_sclk, LOW);
133
134
135
136
137
141 pinMode(pin_sdio, INPUT);
142 delayMicroseconds(1);
144 digitalWrite(
this->pin_sclk, HIGH);
145 delayMicroseconds(1);
147 ack = digitalRead(
this->pin_sdio);
149 digitalWrite(
this->pin_sclk, LOW);
150 delayMicroseconds(1);
156
157
158
159
162 pinMode(pin_sdio, OUTPUT);
163 delayMicroseconds(1);
165 for (
int i = 0; i < 8; i++) {
167 digitalWrite(
this->pin_sdio, (
bool)(data &
this->deviceAddress) );
169 delayMicroseconds(1);
170 digitalWrite(
this->pin_sclk, HIGH);
171 delayMicroseconds(1);
172 digitalWrite(
this->pin_sclk, LOW);
178
179
180
181
186 pinMode(pin_sdio, INPUT);
187 delayMicroseconds(1);
189 for (
int i = 0; i < 8; i++)
191 digitalWrite(
this->pin_sclk, HIGH);
193 delayMicroseconds(1);
194 if ( digitalRead(
this->pin_sdio) )
196 digitalWrite(
this->pin_sclk, LOW);
197 delayMicroseconds(1);
204
205
206
207
208
211 word16_to_bytes data;
216 this->i2cReceiveAck();
220 this->i2cWriteByte(reg);
221 this->i2cReceiveAck();
223 this->i2cWriteByte(data.refined.highByte);
224 this->i2cReceiveAck();
225 this->i2cWriteByte(data.refined.lowByte);
226 this->i2cReceiveAck();
232
233
234
235
236
239 word16_to_bytes data;
243 this->i2cReceiveAck();
245 reg = (reg << 1) | 1;
247 this->i2cWriteByte(reg);
248 this->i2cReceiveAck();
250 data.refined.highByte =
this->i2cReadByte();
252 data.refined.lowByte =
this->i2cReadByte();
262
263
264
267
268
269
270
271
272
275 uint16_t reg_content;
276 reg_content =
this->readRegister(reg);
277 shadowRegisters[reg] = reg_content;
282
283
284
285
286
287
288
289
290
291
292
295 this->writeRegister(reg, value);
296 shadowRegisters[reg] = value;
297 delayMicroseconds(250);
301
302
303
306 return getRegister(
REG00);
310
311
312
313
316 return getRegister(
REG01);
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
341 tmp.raw = getRegister(
REG0A);
347
348
349
350
351
354 while ( reg0a->refined.STC == 0) {
359 reg03->refined.TUNE = 0;
360 setRegister(
REG03, reg03->raw);
365
366
367
370 reg02->refined.DISABLE = 1;
371 reg02->refined.ENABLE = 0;
372 setRegister(
REG02, reg02->raw);
373 reg02->refined.DISABLE = 0;
374 reg02->refined.ENABLE = 1;
375 setRegister(
REG02, reg02->raw);
379
380
381
382
383
384
390 reg02->refined.DISABLE = 0;
391 reg02->refined.ENABLE = 1;
392 setRegister(
REG02,reg02->raw);
394 setRegister(
REG03, 0x0000);
396 setRegister(
REG04, 0x60D4);
397 setRegister(
REG05, 0x37CF);
401 reg06->refined.CLKSEL =
this->oscillatorType;
402 setRegister(
REG06, reg06->raw);
404 setRegister(
REG07, 0x0101);
405 setRegister(
REG08, 0xAC90);
407 setRegister(
REG10, 0x7B11);
408 setRegister(
REG11, 0x004A);
409 setRegister(
REG12, 0x4000);
410 setRegister(
REG13, 0x3E00);
411 setRegister(
REG14, 0xC29A);
412 setRegister(
REG15, 0x79F8);
413 setRegister(
REG16, 0x4012);
416 setRegister(
REG17, 0x0800);
417 setRegister(
REG18, 0x341C);
418 setRegister(
REG19, 0x0080);
419 setRegister(
REG1A, 0x0000);
420 setRegister(
REG1B, 0x4CA2);
423 setRegister(
REG1C, 0);
424 setRegister(
REG1D, 0x0200);
430
431
432
435 reg02->refined.DISABLE = 1;
436 reg02->refined.ENABLE = 0;
437 setRegister(
REG02, reg02->raw);
442
443
444
445
446
447
448
449
450void BK108X::
setup(
int sda_pin,
int sclk_pin,
int rdsInterruptPin,
int seekInterruptPin, uint8_t oscillator_type)
455 if (rdsInterruptPin >= 0)
457 if (seekInterruptPin >= 0)
460 this->oscillatorType = oscillator_type;
467
468
469
470
471
472
473
474
475
476void BK108X::
setFM(uint16_t minimum_frequency, uint16_t maximum_frequency, uint16_t default_frequency, uint16_t step)
478 this->currentStep = step;
479 this->currentFrequency = default_frequency;
480 this->minimumFrequency = minimum_frequency;
481 this->maximumFrequency = maximum_frequency;
485 setRegister(
REG07, reg07->raw);
488 this->currentFMBand = reg05->refined.BAND = 0;
489 this->currentFMSpace = reg05->refined.SPACE = 2;
490 setRegister(
REG05, reg05->raw);
491 setFrequency(default_frequency);
495
496
497
498
499
500
501
502
503
504void BK108X::
setAM(uint16_t minimum_frequency, uint16_t maximum_frequency, uint16_t default_frequency, uint16_t step, uint16_t am_space)
506 this->currentStep = step;
507 this->currentFrequency = default_frequency;
508 this->minimumFrequency = minimum_frequency;
509 this->maximumFrequency = maximum_frequency;
511 this->currentMode = reg07->refined.MODE =
BK_MODE_AM;
512 setRegister(
REG07, reg07->raw);
516 if (minimum_frequency < 520 )
517 this->currentAMBand = reg05->refined.BAND = 0;
518 else if (minimum_frequency < 1800)
519 this->currentAMBand = reg05->refined.BAND = 1;
521 this->currentAMBand = reg05->refined.BAND = 2;
523 this->currentAMSpace = reg05->refined.SPACE = am_space;
525 setRegister(
REG05, reg05->raw);
526 this->setFrequency(default_frequency);
531
532
533
534
537 reg02->refined.SEEK = 0;
538 setRegister(
REG02,reg02->raw);
540 reg03->refined.TUNE = 1;
541 reg03->refined.CHAN = channel;
543 setRegister(
REG03,reg03->raw);
547 this->currentChannel = channel;
551
552
553
554
555
561 channel = (frequency -
this->fmStartBand[
this->currentFMBand]) /
this->fmSpace[
this->currentFMSpace];
564 channel = (frequency -
this->amStartBand[
this->currentAMBand]) /
this->amSpace[
this->currentAMSpace];
567 this->setChannel(channel);
571
572
573
574
577 this->currentFrequency +=
this->currentStep;
579 if (
this->currentFrequency >
this->maximumFrequency )
580 this->currentFrequency =
this->minimumFrequency;
582 setFrequency(
this->currentFrequency);
586
587
588
589
592 this->currentFrequency -=
this->currentStep;
594 if (
this->currentFrequency <
this->minimumFrequency)
595 this->currentFrequency =
this->maximumFrequency;
597 setFrequency(
this->currentFrequency);
601
602
603
604
607 return this->currentFrequency;
611
612
613
614
617 return this->currentChannel;
621
622
623
624
625
629 return reg0b->refined.READCHAN;
633
634
635
636
637
638
642 return getRealChannel() *
this->amSpace[
this->currentAMSpace] +
this->amStartBand[
this->currentAMBand];
644 return getRealChannel() *
this->fmSpace[
this->currentFMSpace] +
this->fmStartBand[
this->currentFMBand];
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
686 long max_time = millis();
691 reg03->refined.TUNE = 0;
692 setRegister(
REG03, reg03->raw);
697 reg02->refined.SKMODE = seek_mode;
698 reg02->refined.SEEKUP = direction;
699 reg02->refined.SEEK = 1;
700 setRegister(
REG02, reg02->raw);
702 if (showFunc != NULL)
704 this->currentFrequency = getRealFrequency();
708 }
while ( (!reg0a->refined.STC && reg0a->refined.SF_BL) && (millis() - max_time) <
MAX_SEEK_TIME);
710 reg02->refined.SEEK = 0;
711 reg03->refined.TUNE = 0;
712 setRegister(
REG02, reg02->raw);
713 setRegister(
REG03, reg03->raw);
715 this->currentFrequency = getRealFrequency();
719
720
721
722
723
724
725
728 reg03->refined.TUNE = 0;
729 setRegister(
REG03, reg03->raw);
731 reg02->refined.SKMODE = seek_mode;
732 reg02->refined.SEEKUP = direction;
733 reg02->refined.SKAFCRL = 1;
737 reg02->refined.SEEK = 1;
738 setRegister(
REG02, reg02->raw);
740 while (reg0a->refined.STC == 0 )
745 }
while ( reg0a->refined.SF_BL != 0 );
747 reg02->refined.SEEK = 0;
748 setRegister(
REG02, reg02->raw);
751 this->setChannel(
this->getRealChannel());
752 this->currentFrequency = getRealFrequency();
756
757
758
759
760
761
764 reg05->refined.SEEKTH = rssiValue;
765 setRegister(
REG05,reg05->raw);
767 reg06->refined.SKSNR = snrValue;
768 setRegister(
REG06,reg06->raw);
772
773
774
775
776
777
778
779
780
781
782
783
784
785
790 this->currentAMBand = band;
792 this->currentFMBand = band;
794 reg05->refined.BAND = band;
795 setRegister(
REG05,reg05->raw);
799
800
801
802
803
804
805
806
807
808
809
810
811
815 this->currentAMSpace = space;
817 this->currentFMSpace = space;
819 reg05->refined.SPACE = space;
820 setRegister(
REG05, reg05->raw);
824
825
826
827
828
832 return reg0a->refined.RSSI;
836
837
838
839
840
844 return reg09->refined.SNR;
848
849
850
851
852
855 reg02->refined.DSMUTE = !value;
856 setRegister(
REG02,reg02->raw);
860
861
862
863
864
865
866
867
868
869
870
871
872
873
876 reg06->refined.SMUTER = value;
877 setRegister(
REG06,reg06->raw);
881
882
883
884
885
886
887
888
889
890
891
892
895 reg06->refined.SMUTEA = value;
896 setRegister(
REG06, reg06->raw);
900
901
902
903
904
905
906
909 reg14->refined.RSSIMTH = rssi;
910 reg14->refined.SNRMTH = snr;
911 setRegister(
REG14,reg14->raw);
915
916
917
918
919
921 reg14->refined.SKMUTE = value;
922 setRegister(
REG14, reg14->raw);
926
927
928
929
930
932 reg14->refined.AFCMUTE = value;
933 setRegister(
REG14, reg14->raw);
939
940
941
942
943
944
947 reg02->refined.MUTEL = left;
948 reg02->refined.MUTER = right;
949 setRegister(
REG02, reg02->raw);
953
954
955
956
957
964
965
966
967
968
971 reg02->refined.MONO = value;
972 reg02->refined.STEREO = !value;
973 setRegister(
REG02,reg02->raw);
977
978
979
980
981
982
986 return reg0a->refined.STEN;
990
991
992
993
994
997 if ( value > 31)
return;
998 this->currentVolume = value;
1000 reg05->refined.VOLUME = value;
1002 setRegister(
REG05,reg05->raw);
1006
1007
1008
1009
1010
1013 return this->currentVolume;
1017
1018
1019
1020
1023 if (
this->currentVolume < 31)
1025 this->currentVolume++;
1026 setVolume(
this->currentVolume);
1031
1032
1033
1034
1037 if (
this->currentVolume > 0)
1039 this->currentVolume--;
1040 setVolume(
this->currentVolume);
1047
1048
1049
1052
1053
1054
1055
1056
1057
1064
1065
1066
1067
1068
1071 this->rds_mode = rds_mode;
1075
1076
1077
1078
1079
1080
1083 reg04->refined.RDSEN = value;
1084 setRegister(
REG04,reg04->raw);
1090
1091
1092
1093
1094
1095
1096
1097
1101 return reg0a->refined.RDSR;
1105
1106
1107
1108
1109
1110
1117
1118
1119
1120
1121
1128
1129
1130
1131
1132
1139
1140
1141
1142
1143
1150
1151
1152
1153
1154
1155
1163
1164
1165
1166
1167
1168
1169
1177
1178
1179
1180
1181
1188
1189
1190
1191
1192
1193
1194
1201
1202
1203
1204
1205
1206
1213
1214
1215
1216
1223
1224
1225
1226
1233
1234
1235
1236
1237
1245
1246
1247
1250
1251
1252
1253
1254
1255
1261 for (address = 1; address < 127; address++)
1263 Wire.beginTransmission(address);
1264 error = Wire.endTransmission();
1267 addressArray[idx] = address;
1270 else if (error == 4)
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291void BK108X::
convertToChar(uint16_t value,
char *strValue, uint8_t len, uint8_t dot, uint8_t separator,
bool remove_leading_zeros)
1294 for (
int i = (len - 1); i >= 0; i--)
1298 strValue[i] = d + 48;
1300 strValue[len] =
'\0';
1303 for (
int i = len; i >= dot; i--)
1305 strValue[i + 1] = strValue[i];
1307 strValue[dot] = separator;
1310 if (remove_leading_zeros)
1312 if (strValue[0] ==
'0')
1315 if (strValue[1] ==
'0')
uint16_t getChipId()
Returns the Chip Indentifiction.
char rds_buffer2A[65]
RDS Radio Text buffer - Program Information.
char rds_buffer2B[33]
RDS Radio Text buffer - Station Informaation.
uint16_t getDeviceId()
Returns the Device Indentifiction.
uint8_t i2cReceiveAck()
Gets Acknowledge (ACK)
uint8_t i2cReadByte()
Gets a Byte from the slave device.
uint16_t readRegister(uint8_t reg)
Gets an array of values from a BK108X given register.
void i2cBeginTransaction()
Starts the I2C bus transaction
void i2cEndTransaction()
Finish the I2C bus transaction.
void i2cWriteByte(uint8_t data)
Sends a Byte to the slave device.
void writeRegister(uint8_t reg, uint16_t vakue)
Sends an array of values to a BK108X given register.
void i2cNack()
Sends Not Acknowledge (ACK)
void setI2C(uint8_t i2c_addr=I2C_DEVICE_ADDR)
Sets I2C bus address.
void i2cAck()
Sends Acknowledge (ACK)
void i2cInit(int pin_sdio, int pin_sclk)
Sets the MCU pins connected to the I2C bus.
void reset()
Resets the device.
uint16_t getChannel()
Gets the current channel.
void setSpace(uint8_t space=0)
Sets the Space channel for AM or FM.
void seekSoftware(uint8_t seek_mode, uint8_t direction, void(*showFunc)()=NULL)
Seeks a station via Software.
uint16_t getRealChannel()
Gets the current channel stored in register 0x0B.
void waitAndFinishTune()
Wait STC (Seek/Tune Complete) status becomes 0.
void setSoftMuteAttack(uint8_t value)
Sets Softmute Attack/Recover Rate.
void setSeekThreshold(uint8_t rssiValue, uint8_t snrValue)
Sets RSSI and SNR Seek Threshold.
void setAM(uint16_t minimum_frequency, uint16_t maximum_frequency, uint16_t default_frequency, uint16_t step, uint16_t am_space=0)
Sets the receiver to AM mode.
int getSnr()
Gets the current SNR.
void setAudioMute(bool left, bool right)
Sets the Mute true or false.
void setFrequency(uint16_t frequency)
Sets the FM frequency.
void setBand(uint8_t band=1)
Sets the current band for AM or FM
void setFrequencyDown()
Decrements the current frequency.
void setAudioMute(bool value)
Sets the Mute true or false.
void setSoftMute(bool value)
Sets the Softmute true or false.
void setAfcMute(bool value)
Disable or Enable soft mute when AFCRL is high.
bk_reg0a getStatus()
Gets the current status (register 0x0A) content.
bool isStereo()
Checks stereo / mono status.
void setVolumeUp()
Increments the audio volume.
void setup(int sda_pin, int sclk_pin, int rdsInterruptPin=-1, int seekInterruptPin=-1, uint8_t oscillator_type=OSCILLATOR_TYPE_CRYSTAL)
Starts the device.
uint16_t getFrequency()
Gets the current frequency.
void setSoftMuteAttenuation(uint8_t value)
Sets Softmute Attenuation.
void powerDown()
Powers the receiver off.
void setChannel(uint16_t channel)
Sets the channel.
void setFM(uint16_t minimum_frequency, uint16_t maximum_frequency, uint16_t default_frequency, uint16_t step)
Sets the receiver to FM mode.
void setVolume(uint8_t value)
Sets the audio volume level.
uint8_t getVolume()
Gets the current audio volume level.
void setMono(bool value)
Sets the Mono true or false (stereo)
void setFrequencyUp()
Increments the current frequency.
uint16_t getRealFrequency()
Gets the frequency based on READCHAN register (0x0B)
void setVolumeDown()
Decrements the audio volume.
void powerUp()
Powers the receiver on.
int getRssi()
Gets the current Rssi.
void setRegister(uint8_t reg, uint16_t value)
Sets a given value to the device registers.
void setMuteThreshold(uint8_t rssi, uint8_t snr)
Set the Mute Threshold based on RSSI and SNR.
void seekHardware(uint8_t seek_mode, uint8_t direction)
Seeks a station via hardware functionality.
void setSeekMute(bool value)
Disable or Enable soft mute when seeking.
uint16_t getRegister(uint8_t reg)
Gets a givens current register content of the device.
char * getRdsText0A(void)
Gets the station name and other messages.
bool getRdsReady()
Returns true if RDS Ready.
char * getRdsText(void)
Gets the RDS Text when the message is of the Group Type 2 version A.
char * getRdsText2B(void)
Gets the Text processed for the 2B group.
void getNext4Block(char *c)
Process data received from group 2A.
uint8_t getRdsVersionCode(void)
Gets the version code (extracted from the Block B)
char * getRdsTime()
Gets the RDS time and date when the Group type is 4.
uint16_t getRdsGroupType()
Return the group type.
uint8_t getRdsFlagAB(void)
Returns the current Text Flag A/B
uint8_t getRdsProgramType(void)
Returns the Program Type (extracted from the Block B)
void getNext2Block(char *c)
Process data received from group 2B.
char * getRdsText2A(void)
Gets the Text processed for the 2A group.
bool getRdsSync()
Get the Rds Sync.
void setRdsMode(uint8_t rds_mode=0)
Sets the Rds Mode Standard or Verbose.
void setRds(bool value)
Sets the RDS operation.
void getRdsStatus()
Gets the RDS registers information.
void convertToChar(uint16_t value, char *strValue, uint8_t len, uint8_t dot, uint8_t separator, bool remove_leading_zeros=true)
Converts a number to a char array.
int checkI2C(uint8_t *addressArray)
Check the I2C bus address.