bk1080.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /* Copyright 2023 Dual Tachyon
  2. * https://github.com/DualTachyon
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "bsp/dp32g030/gpio.h"
  17. #include "bk1080.h"
  18. #include "driver/gpio.h"
  19. #include "driver/i2c.h"
  20. #include "driver/system.h"
  21. #include "driver/eeprom.h" /* need access to gEeprom */
  22. #include "settings.h" /* " */
  23. #include "misc.h"
  24. #ifndef ARRAY_SIZE
  25. #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
  26. #endif
  27. static const uint16_t BK1080_RegisterTable[] =
  28. {
  29. 0x0008, 0x1080, 0x0201, 0x0000, 0x40C0, 0x0A1F, 0x002E, 0x02FF,
  30. 0x5B11, 0x0000, 0x411E, 0x0000, 0xCE00, 0x0000, 0x0000, 0x1000,
  31. 0x3197, 0x0000, 0x13FF, 0x9852, 0x0000, 0x0000, 0x0008, 0x0000,
  32. 0x51E1, 0xA8BC, 0x2645, 0x00E4, 0x1CD8, 0x3A50, 0xEAE0, 0x3000,
  33. 0x0200, 0x0000,
  34. };
  35. static bool gIsInitBK1080;
  36. uint16_t BK1080_BaseFrequency;
  37. uint16_t BK1080_FrequencyDeviation;
  38. void BK1080_Init0(void)
  39. {
  40. BK1080_Init(0,0,0);
  41. }
  42. void BK1080_Init(uint16_t freq, uint8_t band, uint8_t space)
  43. {
  44. unsigned int i;
  45. if (freq) {
  46. GPIO_ClearBit(&GPIOB->DATA, GPIOB_PIN_BK1080);
  47. if (!gIsInitBK1080) {
  48. for (i = 0; i < ARRAY_SIZE(BK1080_RegisterTable); i++)
  49. BK1080_WriteRegister(i, BK1080_RegisterTable[i]);
  50. SYSTEM_DelayMs(250);
  51. BK1080_WriteRegister(BK1080_REG_25_INTERNAL, 0xA83C);
  52. BK1080_WriteRegister(BK1080_REG_25_INTERNAL, 0xA8BC);
  53. SYSTEM_DelayMs(60);
  54. gIsInitBK1080 = true;
  55. }
  56. else {
  57. BK1080_WriteRegister(BK1080_REG_02_POWER_CONFIGURATION, 0x0201);
  58. }
  59. // 0000 1000 0000 0000 .. 15:res 14:STCIEN=0; 13:DEBPS=0; 12:res; 11:DE=0; 10:AGCD=0; 9:8:res; 7:6:BLNDADJ=0; 5:4:GPIO3=0/Low; 3:2:GPIO2=0; 1:0:GPIO0
  60. uint16_t tmp04 = gEeprom.FM_Preemp ? 0 : 1<<11;
  61. BK1080_WriteRegister(BK1080_REG_04_SYSTEM_CONFIGURATION1, tmp04);
  62. // 0x0A1F - 00001010 00 01 1111 == volume=1111; channel_spacing= (xx)01; BAND=(xx)00; seek_thresh=0
  63. uint16_t tmp05 = 0x0A00 + ((gEeprom.FM_Band & 3) << 6) + ((space & 3) << 4) + 0x00F; // was 0x0A1F
  64. BK1080_WriteRegister(BK1080_REG_05_SYSTEM_CONFIGURATION2, tmp05);
  65. BK1080_SetFrequency(freq, band, space);
  66. }
  67. else {
  68. BK1080_WriteRegister(BK1080_REG_02_POWER_CONFIGURATION, 0x0241);
  69. GPIO_SetBit(&GPIOB->DATA, GPIOB_PIN_BK1080);
  70. }
  71. }
  72. uint16_t BK1080_ReadRegister(BK1080_Register_t Register)
  73. {
  74. uint8_t Value[2];
  75. I2C_Start();
  76. I2C_Write(0x80);
  77. I2C_Write((Register << 1) | I2C_READ);
  78. I2C_ReadBuffer(Value, sizeof(Value));
  79. I2C_Stop();
  80. return (Value[0] << 8) | Value[1];
  81. }
  82. void BK1080_WriteRegister(BK1080_Register_t Register, uint16_t Value)
  83. {
  84. I2C_Start();
  85. I2C_Write(0x80);
  86. I2C_Write((Register << 1) | I2C_WRITE);
  87. Value = ((Value >> 8) & 0xFF) | ((Value & 0xFF) << 8);
  88. I2C_WriteBuffer(&Value, sizeof(Value));
  89. I2C_Stop();
  90. }
  91. void BK1080_Mute(bool Mute)
  92. {
  93. BK1080_WriteRegister(BK1080_REG_02_POWER_CONFIGURATION, Mute ? 0x4201 : 0x0201);
  94. }
  95. void BK1080_SetFrequency(uint16_t frequency, uint8_t band, uint8_t space)
  96. {
  97. uint8_t spacings[] = {20,10,5};
  98. space %= 3;
  99. uint16_t channel = ((frequency - BK1080_GetFreqLoLimit(band)) * 10) / spacings[space];
  100. uint16_t regval = BK1080_ReadRegister(BK1080_REG_04_SYSTEM_CONFIGURATION1);
  101. regval = (regval & ~(1<<11)) | (gEeprom.FM_Preemp ? 0 : 1<<11);
  102. BK1080_WriteRegister(BK1080_REG_04_SYSTEM_CONFIGURATION1, regval);
  103. regval = BK1080_ReadRegister(BK1080_REG_05_SYSTEM_CONFIGURATION2);
  104. regval = (regval & ~(0b11 << 6)) | ((band & 0b11) << 6);
  105. regval = (regval & ~(0b11 << 4)) | ((space & 0b11) << 4);
  106. BK1080_WriteRegister(BK1080_REG_05_SYSTEM_CONFIGURATION2, regval);
  107. BK1080_WriteRegister(BK1080_REG_03_CHANNEL, channel);
  108. SYSTEM_DelayMs(10);
  109. BK1080_WriteRegister(BK1080_REG_03_CHANNEL, channel | 0x8000);
  110. }
  111. void BK1080_GetFrequencyDeviation(uint16_t Frequency)
  112. {
  113. BK1080_BaseFrequency = Frequency;
  114. BK1080_FrequencyDeviation = BK1080_ReadRegister(BK1080_REG_07) / 16;
  115. }
  116. uint16_t BK1080_GetFreqLoLimit(uint8_t band)
  117. {
  118. uint16_t lim[] = {875, 760, 760, 640};
  119. return lim[band % 4];
  120. }
  121. uint16_t BK1080_GetFreqHiLimit(uint8_t band)
  122. {
  123. band %= 4;
  124. uint16_t lim[] = {1080, 1080, 900, 760};
  125. return lim[band % 4];
  126. }