ta5711.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #include "include.h"
  2. #define TRACE_EN 1
  3. #if TRACE_EN
  4. #define TRACE(...) printf(__VA_ARGS__)
  5. #else
  6. #define TRACE(...)
  7. #endif
  8. #if I2S_DEVICE == I2S_DEV_TAS5711 && I2S_EN
  9. /* device registers */
  10. #define TAS5711_CLK_CTRL_REG 0x00
  11. #define TAS5711_DEV_ID_REG 0x01
  12. #define TAS5711_ERR_STATUS_REG 0x02
  13. #define TAS5711_SYS_CTRL_1_REG 0x03
  14. #define TAS5711_SDI_REG 0x04
  15. #define TAS5711_SYS_CTRL_2_REG 0x05
  16. #define TAS5711_SOFT_MUTE_REG 0x06
  17. #define TAS5711_MVOL_REG 0x07
  18. #define TAS5711_OSC_TRIM_REG 0x1b
  19. #if !I2C_SW_EN && !I2C_HW_EN
  20. #error "TAS5711: please enable i2c!\n"
  21. #endif
  22. //TAS5711CLK设置
  23. #if I2S_MCLK_SEL == 0
  24. #define TAS5711_CLK_SEL 0x60 //64fs
  25. #elif I2S_MCLK_SEL == 1
  26. #define TAS5711_CLK_SEL 0x64 //128fs
  27. #elif I2S_MCLK_SEL == 2
  28. #define TAS5711_CLK_SEL 0x6c //256fs
  29. #endif
  30. //TAS5711数据对齐设置
  31. #if I2S_DATA_MODE
  32. #define TAS5711_DAT_SEL 0x03
  33. #else
  34. #define TAS5711_DAT_SEL 0x06 //左对齐模式
  35. #endif
  36. typedef struct {
  37. u8 cmd;
  38. u8 len;
  39. u8 parm;
  40. }tas5711_cmd_t;
  41. static tas5711_cmd_t tas5711_cmd_init[] = {
  42. {TAS5711_CLK_CTRL_REG, 1, TAS5711_CLK_SEL},
  43. {TAS5711_SDI_REG, 1, TAS5711_DAT_SEL},
  44. {TAS5711_SYS_CTRL_2_REG, 1, 0x00},
  45. {TAS5711_SOFT_MUTE_REG, 1, 0x00},
  46. {TAS5711_MVOL_REG, 1, 0x50},
  47. };
  48. AT(.text.bsp.i2s)
  49. static bool tas5711_detect(void)
  50. {
  51. #if I2C_SW_EN
  52. bool ret;
  53. bsp_i2c_start();
  54. bsp_i2c_tx_byte(0x36);
  55. ret = bsp_i2c_rx_ack();
  56. bsp_i2c_stop();
  57. return ret;
  58. #elif I2C_HW_EN
  59. return true;
  60. #else
  61. return false;
  62. #endif
  63. }
  64. AT(.text.bsp.i2s)
  65. static void tas5711_sfr_write(u8 addr, u8 dat)
  66. {
  67. #if I2C_SW_EN
  68. bsp_i2c_start();
  69. bsp_i2c_tx_byte(0x36);
  70. bsp_i2c_rx_ack();
  71. bsp_i2c_tx_byte(addr);
  72. bsp_i2c_rx_ack();
  73. bsp_i2c_tx_byte(dat);
  74. bsp_i2c_rx_ack();
  75. bsp_i2c_stop();
  76. delay_5ms(2);
  77. #elif I2C_HW_EN
  78. bsp_i2c_tx_byte(0x36, addr, dat);
  79. #endif
  80. }
  81. AT(.text.bsp.i2s)
  82. static u8 tas5711_sfr_read(u8 addr)
  83. {
  84. #if I2C_SW_EN
  85. u8 dat;
  86. bsp_i2c_start();
  87. bsp_i2c_tx_byte(0x36);
  88. bsp_i2c_rx_ack();
  89. bsp_i2c_tx_byte(addr);
  90. bsp_i2c_rx_ack();
  91. bsp_i2c_start();
  92. bsp_i2c_tx_byte(0x37);
  93. bsp_i2c_rx_ack();
  94. dat = bsp_i2c_rx_byte();
  95. bsp_i2c_tx_nack();
  96. bsp_i2c_stop();
  97. delay_5ms(2);
  98. return dat;
  99. #elif I2C_HW_EN
  100. u8 buf[1];
  101. bsp_i2c_rx_buf(0x37 << 8 | 0x36, addr, buf, 1);
  102. return buf[0];
  103. #else
  104. return 0;
  105. #endif // I2C_SW_EN
  106. }
  107. AT(.text.bsp.i2s)
  108. void tas5711_init(void)
  109. {
  110. u8 i;
  111. bsp_i2c_init();
  112. //TAS5711 reset
  113. GPIOBDE |= BIT(10);
  114. GPIOBDIR &= ~BIT(10);
  115. GPIOBCLR = BIT(10);
  116. delay_5ms(1);
  117. GPIOBSET = BIT(10);
  118. delay_5ms(4);
  119. if (!tas5711_detect()) {
  120. TRACE("TAS5711 isn't online\n");
  121. return;
  122. }
  123. tas5711_sfr_write(TAS5711_OSC_TRIM_REG, 0x00); //tirm Oscillator
  124. delay_5ms(40);
  125. for (i=0; i<sizeof(tas5711_cmd_init)/sizeof(tas5711_cmd_t); i++) {
  126. tas5711_sfr_write(tas5711_cmd_init[i].cmd, tas5711_cmd_init[i].parm);
  127. delay_us(10);
  128. }
  129. TRACE("[0]:0x%02x\n",tas5711_sfr_read(TAS5711_CLK_CTRL_REG));
  130. TRACE("[1]:0x%02x\n",tas5711_sfr_read(TAS5711_DEV_ID_REG));
  131. TRACE("[2]:0x%02x\n",tas5711_sfr_read(TAS5711_ERR_STATUS_REG));
  132. TRACE("[4]:0x%02x\n",tas5711_sfr_read(TAS5711_SDI_REG));
  133. TRACE("[5]:0x%02x\n",tas5711_sfr_read(TAS5711_SYS_CTRL_2_REG));
  134. TRACE("[6]:0x%02x\n",tas5711_sfr_read(TAS5711_SOFT_MUTE_REG));
  135. TRACE("[7]:0x%02x\n",tas5711_sfr_read(TAS5711_MVOL_REG));
  136. TRACE("[1b]:0x%02x\n",tas5711_sfr_read(TAS5711_OSC_TRIM_REG));
  137. }
  138. #endif