TEA5767.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /**
  2. * @file TEA5767.h
  3. * @author Coolshrimp - CoolshrimpModz.com
  4. * @brief Header file for controlling the TEA5767 FM radio chip.
  5. * @version 0.1
  6. * @date 2023-09-29
  7. *
  8. *
  9. * TEA5767 Datasheet: https://www.sparkfun.com/datasheets/Wireless/General/TEA5767.pdf
  10. *
  11. *
  12. * @copyright GPLv3
  13. *
  14. */
  15. #ifndef TEA5767_H
  16. #define TEA5767_H
  17. #include <stdbool.h>
  18. #include <stdint.h>
  19. // Definitions for the Wire communication
  20. #define TEA5767_ADR 0xC0 // I2C address of TEA5767
  21. // #define TEA5767_ADR 0x60 // I2C address of TEA5767
  22. // Radio chip specific definitions including the registers
  23. #define QUARTZ 32768 // Frequency of the quartz crystal in Hz
  24. #define FILTER 225000 // Frequency of the filter in Hz
  25. // Define the registers
  26. #define REG_1 0x00 // Register 1 address
  27. #define REG_1_MUTE 0x80 // Mute: 1 to mute the audio output, 0 to unmute
  28. #define REG_1_SM 0x40 // Search Mode: 1 to activate search mode, 0 for normal mode
  29. #define REG_1_PLL 0x3F // PLL Setting: Sets the PLL value (6 bits)
  30. #define REG_2 0x01 // Register 2 address
  31. #define REG_2_PLL 0xFF // PLL Setting: Sets the PLL value (8 bits)
  32. #define REG_3 0x02 // Register 3 address
  33. #define REG_3_SUD 0x80 // Search Up/Down: 1 to search up, 0 to search down
  34. #define REG_3_SSL 0x60 // Search Stop Level: Sets the level at which the search stops (2 bits)
  35. #define REG_3_HLSI \
  36. 0x10 // High/Low Side Injection: 1 for high side LO injection, 0 for low side LO injection
  37. #define REG_3_MS 0x08 // Mono to Stereo: 1 to force mono, 0 for stereo
  38. #define REG_3_MR 0x04 // Mute Right: 1 to mute the right audio channel and force mono, 0 to unmute
  39. #define REG_3_ML 0x02 // Mute Left: 1 to mute the left audio channel and force mono, 0 to unmute
  40. #define REG_3_SWP1 0x01 // Software programmable port 1: 1 for HIGH, 0 for LOW
  41. #define REG_4 0x03 // Register 4 address
  42. #define REG_4_SWP2 0x80 // Software programmable port 2: 1 for HIGH, 0 for LOW
  43. #define REG_4_STBY 0x40 // Standby: 1 to activate standby mode, 0 to deactivate
  44. #define REG_4_BL 0x20 // Band Limits: 1 for Japanese FM band, 0 for US/Europe FM band
  45. #define REG_4_XTAL 0x10 // Clock frequency: Sets the clock frequency (see Table 16)
  46. #define REG_4_SMUTE 0x08 // Soft Mute: 1 to activate soft mute, 0 to deactivate
  47. #define REG_4_HCC 0x04 // High Cut Control: 1 to activate high cut control, 0 to deactivate
  48. #define REG_4_SNC \
  49. 0x02 // Stereo Noise Cancelling: 1 to activate stereo noise cancelling, 0 to deactivate
  50. #define REG_4_SI \
  51. 0x01 // Search Indicator: 1 for ready flag output on pin SWPORT1, 0 for software programmable port 1
  52. #define REG_5 0x04 // Register 5 address
  53. #define REG_5_PLLREF \
  54. 0x80 // PLL Ref: 1 to enable the 6.5 MHz reference frequency for the PLL, 0 to disable
  55. #define REG_5_DTC 0x40 // De-emphasis Time Constant: 1 for 75 µs, 0 for 50 µs
  56. /// The BANDs TEA5767 can tune to.
  57. typedef enum {
  58. RADIO_BAND_FM = 0x01, ///< FM band 87.5 - 108 MHz (USA, Europe) selected.
  59. RADIO_BAND_FM_JAPAN = 0x02 ///< FM band 76 - 90 MHz (Japan) selected.
  60. } RADIO_BAND;
  61. /// A structure that contains information about the radio features from the chip.
  62. struct RADIO_INFO {
  63. float frequency; // Frequency in MHz
  64. int signalLevel; // Signal level
  65. bool stereo; // Stereo or not
  66. bool muted; // Muted or not
  67. char signalQuality[10]; // Field for signal quality text
  68. };
  69. // Function prototypes
  70. bool tea5767_is_device_ready();
  71. bool tea5767_read_registers(uint8_t* buffer);
  72. bool tea5767_write_registers(uint8_t* buffer);
  73. bool tea5767_init(uint8_t* buffer);
  74. bool tea5767_set_mute(uint8_t* buffer, bool mute);
  75. bool tea5767_set_stereo(uint8_t* buffer, bool stereo);
  76. bool tea5767_seek(uint8_t* buffer, bool seek_up);
  77. bool tea5767_get_frequency(uint8_t* buffer, int* value);
  78. bool tea5767_set_frequency(uint8_t* buffer, int value);
  79. bool tea5767_get_radio_info(uint8_t* buffer, struct RADIO_INFO* info);
  80. // New high-level function prototypes
  81. void tea5767_sleep(uint8_t* buffer);
  82. void tea5767_seekUp();
  83. void tea5767_seekDown();
  84. void tea5767_ToggleMute();
  85. void tea5767_MuteOn();
  86. void tea5767_MuteOff();
  87. void tea5767_SetFreqKHz(int freq_khz);
  88. void tea5767_SetFreqMHz(float freq_mhz);
  89. float tea5767_GetFreq();
  90. #endif // TEA5767_H