ac101.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. /*
  2. * ESPRESSIF MIT License
  3. *
  4. * Copyright (c) 2018 <ESPRESSIF SYSTEMS (SHANGHAI) PTE LTD>
  5. *
  6. * Permission is hereby granted for use on all ESPRESSIF SYSTEMS products, in which case,
  7. * it is free of charge, to any person obtaining a copy of this software and associated
  8. * documentation files (the "Software"), to deal in the Software without restriction, including
  9. * without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
  10. * and/or sell copies of the Software, and to permit persons to whom the Software is furnished
  11. * to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in all copies or
  14. * substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  18. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  19. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  20. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  21. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. *
  23. */
  24. #include <string.h>
  25. #include <esp_log.h>
  26. #include <esp_types.h>
  27. #include <esp_system.h>
  28. #include <freertos/FreeRTOS.h>
  29. #include <freertos/task.h>
  30. #include <driver/i2c.h>
  31. #include "adac.h"
  32. //#include "audio_hal.h"
  33. #include "ac101.h"
  34. const static char TAG[] = "AC101";
  35. #define AC_ASSERT(a, format, b, ...) \
  36. if ((a) != 0) { \
  37. ESP_LOGE(TAG, format, ##__VA_ARGS__); \
  38. return b;\
  39. }
  40. static bool init(int i2c_port_num, int i2s_num, i2s_config_t *config);
  41. static void deinit(void);
  42. static void speaker(bool active);
  43. static void headset(bool active);
  44. static void volume(unsigned left, unsigned right);
  45. static void power(adac_power_e mode);
  46. struct adac_s dac_a1s = { init, deinit, power, speaker, headset, volume };
  47. static esp_err_t i2c_write_reg(uint8_t reg, uint16_t val);
  48. static uint16_t i2c_read_reg(uint8_t reg);
  49. static esp_err_t ac101_start(ac_module_t mode);
  50. static esp_err_t ac101_stop(void);
  51. static esp_err_t ac101_set_earph_volume(uint8_t volume);
  52. static esp_err_t ac101_set_spk_volume(uint8_t volume);
  53. //static void pa_power(bool enable);
  54. static int i2c_port;
  55. /****************************************************************************************
  56. * init
  57. */
  58. static bool init(int i2c_port_num, int i2s_num, i2s_config_t *i2s_config) {
  59. esp_err_t res;
  60. ESP_LOGI(TAG, "Initializing AC101");
  61. i2c_port = i2c_port_num;
  62. // configure i2c
  63. i2c_config_t i2c_config = {
  64. .mode = I2C_MODE_MASTER,
  65. .sda_io_num = 33,
  66. .sda_pullup_en = GPIO_PULLUP_ENABLE,
  67. .scl_io_num = 32,
  68. .scl_pullup_en = GPIO_PULLUP_ENABLE,
  69. .master.clk_speed = 100000,
  70. };
  71. i2c_param_config(i2c_port, &i2c_config);
  72. i2c_driver_install(i2c_port, I2C_MODE_MASTER, false, false, false);
  73. ESP_LOGI(TAG, "DAC using I2C sda:%u, scl:%u", i2c_config.sda_io_num, i2c_config.scl_io_num);
  74. res = i2c_write_reg(CHIP_AUDIO_RS, 0x123);
  75. //huh?
  76. //vTaskDelay(1000 / portTICK_PERIOD_MS);
  77. if (ESP_OK != res) {
  78. ESP_LOGE(TAG, "reset failed!");
  79. return false;
  80. }
  81. i2c_write_reg(SPKOUT_CTRL, 0xe880);
  82. // Enable the PLL from 256*44.1KHz MCLK source
  83. i2c_write_reg(PLL_CTRL1, 0x014f);
  84. //res |= i2c_write_reg(PLL_CTRL2, 0x83c0);
  85. i2c_write_reg(PLL_CTRL2, 0x8600);
  86. //Clocking system
  87. i2c_write_reg(SYSCLK_CTRL, 0x8b08);
  88. i2c_write_reg(MOD_CLK_ENA, 0x800c);
  89. i2c_write_reg(MOD_RST_CTRL, 0x800c);
  90. i2c_write_reg(I2S_SR_CTRL, 0x7000); //sample rate
  91. //AIF config
  92. i2c_write_reg(I2S1LCK_CTRL, 0x8850); //BCLK/LRCK
  93. i2c_write_reg(I2S1_SDOUT_CTRL, 0xc000); //
  94. i2c_write_reg(I2S1_SDIN_CTRL, 0xc000);
  95. i2c_write_reg(I2S1_MXR_SRC, 0x2200); //
  96. i2c_write_reg(ADC_SRCBST_CTRL, 0xccc4);
  97. i2c_write_reg(ADC_SRC, 0x2020);
  98. i2c_write_reg(ADC_DIG_CTRL, 0x8000);
  99. i2c_write_reg(ADC_APC_CTRL, 0xbbc3);
  100. //Path Configuration
  101. i2c_write_reg(DAC_MXR_SRC, 0xcc00);
  102. i2c_write_reg(DAC_DIG_CTRL, 0x8000);
  103. i2c_write_reg(OMIXER_SR, 0x0081);
  104. i2c_write_reg(OMIXER_DACA_CTRL, 0xf080);//}
  105. //* Enable Speaker output
  106. i2c_write_reg(0x58, 0xeabd);
  107. //ac101_pa_power(true);
  108. uint16_t regval;
  109. // configure I2S
  110. regval = i2c_read_reg(I2S1LCK_CTRL);
  111. regval &= 0xffc3;
  112. regval |= (AC_MODE_SLAVE << 15);
  113. regval |= (BIT_LENGTH_16_BITS << 4);
  114. regval |= (AC_MODE_SLAVE << 2);
  115. res |= i2c_write_reg(I2S1LCK_CTRL, regval);
  116. res |= i2c_write_reg(I2S_SR_CTRL, SAMPLE_RATE_44100);
  117. // configure I2S pins & install driver
  118. i2s_pin_config_t i2s_pin_config = (i2s_pin_config_t) { .bck_io_num = 27, .ws_io_num = 26,
  119. .data_out_num = 35, .data_in_num = 25 //Not used
  120. };
  121. i2s_driver_install(i2s_num, i2s_config, 0, NULL);
  122. i2s_set_pin(i2s_num, &i2s_pin_config);
  123. ESP_LOGI(TAG, "DAC using I2S bck:%u, ws:%u, do:%u", i2s_pin_config.bck_io_num, i2s_pin_config.ws_io_num, i2s_pin_config.data_out_num);
  124. return true;
  125. }
  126. /****************************************************************************************
  127. * init
  128. */
  129. static void deinit(void) {
  130. i2c_driver_delete(i2c_port);
  131. }
  132. /****************************************************************************************
  133. * change volume
  134. */
  135. static void volume(unsigned left, unsigned right) {
  136. // nothing at that point, volume is handled by backend
  137. }
  138. /****************************************************************************************
  139. * power
  140. */
  141. static void power(adac_power_e mode) {
  142. switch(mode) {
  143. case ADAC_STANDBY:
  144. case ADAC_OFF:
  145. ac101_stop();
  146. break;
  147. case ADAC_ON:
  148. ac101_start(AC_MODULE_ADC);
  149. break;
  150. default:
  151. ESP_LOGW(TAG, "unknown power command");
  152. break;
  153. }
  154. }
  155. /****************************************************************************************
  156. * speaker
  157. */
  158. static void speaker(bool active) {
  159. if (active) i2c_write_reg(SPKOUT_CTRL, 0xeabd);
  160. else i2c_write_reg(SPKOUT_CTRL, 0xe880); //disable speaker
  161. }
  162. /****************************************************************************************
  163. * headset
  164. */
  165. static void headset(bool active) {
  166. if (active) {
  167. i2c_write_reg(OMIXER_DACA_CTRL, 0xff80);
  168. i2c_write_reg(HPOUT_CTRL, 0xc3c1);
  169. i2c_write_reg(HPOUT_CTRL, 0xcb00);
  170. // huh?
  171. vTaskDelay(100 / portTICK_PERIOD_MS);
  172. i2c_write_reg(HPOUT_CTRL, 0xfbc0);
  173. } else {
  174. i2c_write_reg(HPOUT_CTRL, 0x01); //disable earphone
  175. }
  176. }
  177. /****************************************************************************************
  178. *
  179. */
  180. static esp_err_t i2c_write_reg(uint8_t reg, uint16_t val)
  181. {
  182. i2c_cmd_handle_t cmd = i2c_cmd_link_create();
  183. esp_err_t ret =0;
  184. uint8_t send_buff[4];
  185. send_buff[0] = (AC101_ADDR << 1);
  186. send_buff[1] = reg;
  187. send_buff[2] = (val>>8) & 0xff;
  188. send_buff[3] = val & 0xff;
  189. ret |= i2c_master_start(cmd);
  190. ret |= i2c_master_write(cmd, send_buff, 4, ACK_CHECK_EN);
  191. ret |= i2c_master_stop(cmd);
  192. ret |= i2c_master_cmd_begin(i2c_port, cmd, 1000 / portTICK_RATE_MS);
  193. i2c_cmd_link_delete(cmd);
  194. return ret;
  195. }
  196. /****************************************************************************************
  197. *
  198. */
  199. static uint16_t i2c_read_reg(uint8_t reg) {
  200. uint8_t data[2] = { 0 };
  201. i2c_cmd_handle_t cmd = i2c_cmd_link_create();
  202. i2c_master_start(cmd);
  203. i2c_master_write_byte(cmd, ( AC101_ADDR << 1 ) | WRITE_BIT, ACK_CHECK_EN);
  204. i2c_master_write_byte(cmd, reg, ACK_CHECK_EN);
  205. i2c_master_start(cmd);
  206. i2c_master_write_byte(cmd, ( AC101_ADDR << 1 ) | READ_BIT, ACK_CHECK_EN); //check or not
  207. i2c_master_read(cmd, data, 2, ACK_VAL);
  208. i2c_master_stop(cmd);
  209. i2c_master_cmd_begin(i2c_port, cmd, 1000 / portTICK_RATE_MS);
  210. i2c_cmd_link_delete(cmd);
  211. return (data[0] << 8) + data[1];;
  212. }
  213. /****************************************************************************************
  214. *
  215. */
  216. void set_codec_clk(ac_adda_fs_i2s1_t rate) {
  217. i2c_write_reg(I2S_SR_CTRL, rate);
  218. }
  219. /****************************************************************************************
  220. *
  221. */
  222. static int ac101_get_spk_volume(void) {
  223. int res;
  224. res = i2c_read_reg(SPKOUT_CTRL);
  225. res &= 0x1f;
  226. return res*2;
  227. }
  228. /****************************************************************************************
  229. *
  230. */
  231. static esp_err_t ac101_set_spk_volume(uint8_t volume) {
  232. uint16_t res;
  233. esp_err_t ret;
  234. volume = volume/2;
  235. res = i2c_read_reg(SPKOUT_CTRL);
  236. res &= (~0x1f);
  237. volume &= 0x1f;
  238. res |= volume;
  239. ret = i2c_write_reg(SPKOUT_CTRL,res);
  240. return ret;
  241. }
  242. /****************************************************************************************
  243. *
  244. */
  245. static int ac101_get_earph_volume(void) {
  246. int res;
  247. res = i2c_read_reg(HPOUT_CTRL);
  248. return (res>>4)&0x3f;
  249. }
  250. /****************************************************************************************
  251. *
  252. */
  253. static esp_err_t ac101_set_earph_volume(uint8_t volume) {
  254. uint16_t res,tmp;
  255. esp_err_t ret;
  256. res = i2c_read_reg(HPOUT_CTRL);
  257. tmp = ~(0x3f<<4);
  258. res &= tmp;
  259. volume &= 0x3f;
  260. res |= (volume << 4);
  261. ret = i2c_write_reg(HPOUT_CTRL,res);
  262. return ret;
  263. }
  264. /****************************************************************************************
  265. *
  266. */
  267. static esp_err_t ac101_set_output_mixer_gain(ac_output_mixer_gain_t gain,ac_output_mixer_source_t source)
  268. {
  269. uint16_t regval,temp,clrbit;
  270. esp_err_t ret;
  271. regval = i2c_read_reg(OMIXER_BST1_CTRL);
  272. switch(source){
  273. case SRC_MIC1:
  274. temp = (gain&0x7) << 6;
  275. clrbit = ~(0x7<<6);
  276. break;
  277. case SRC_MIC2:
  278. temp = (gain&0x7) << 3;
  279. clrbit = ~(0x7<<3);
  280. break;
  281. case SRC_LINEIN:
  282. temp = (gain&0x7);
  283. clrbit = ~0x7;
  284. break;
  285. default:
  286. return -1;
  287. }
  288. regval &= clrbit;
  289. regval |= temp;
  290. ret = i2c_write_reg(OMIXER_BST1_CTRL,regval);
  291. return ret;
  292. }
  293. /****************************************************************************************
  294. *
  295. */
  296. static esp_err_t ac101_start(ac_module_t mode) {
  297. esp_err_t res = 0;
  298. if (mode == AC_MODULE_LINE) {
  299. res |= i2c_write_reg(0x51, 0x0408);
  300. res |= i2c_write_reg(0x40, 0x8000);
  301. res |= i2c_write_reg(0x50, 0x3bc0);
  302. }
  303. if (mode == AC_MODULE_ADC || mode == AC_MODULE_ADC_DAC || mode == AC_MODULE_LINE) {
  304. //I2S1_SDOUT_CTRL
  305. //res |= i2c_write_reg(PLL_CTRL2, 0x8120);
  306. res |= i2c_write_reg(0x04, 0x800c);
  307. res |= i2c_write_reg(0x05, 0x800c);
  308. //res |= i2c_write_reg(0x06, 0x3000);
  309. }
  310. if (mode == AC_MODULE_DAC || mode == AC_MODULE_ADC_DAC || mode == AC_MODULE_LINE) {
  311. //* Enable Headphone output 注意使用耳机时,最后开以下寄存器
  312. res |= i2c_write_reg(OMIXER_DACA_CTRL, 0xff80);
  313. res |= i2c_write_reg(HPOUT_CTRL, 0xc3c1);
  314. res |= i2c_write_reg(HPOUT_CTRL, 0xcb00);
  315. // huh?
  316. vTaskDelay(100 / portTICK_PERIOD_MS);
  317. res |= i2c_write_reg(HPOUT_CTRL, 0xfbc0);
  318. //* Enable Speaker output
  319. res |= i2c_write_reg(SPKOUT_CTRL, 0xeabd);
  320. // huh?
  321. vTaskDelay(10 / portTICK_PERIOD_MS);
  322. ac101_set_earph_volume(255);
  323. ac101_set_spk_volume(255);
  324. }
  325. return res;
  326. }
  327. /****************************************************************************************
  328. *
  329. */
  330. esp_err_t ac101_stop(void) {
  331. esp_err_t res = 0;
  332. res |= i2c_write_reg(HPOUT_CTRL, 0x01); //disable earphone
  333. res |= i2c_write_reg(SPKOUT_CTRL, 0xe880); //disable speaker
  334. return res;
  335. }
  336. /****************************************************************************************
  337. *
  338. */
  339. esp_err_t ac101_deinit(void) {
  340. return i2c_write_reg(CHIP_AUDIO_RS, 0x123); //soft reset
  341. }
  342. /****************************************************************************************
  343. * Don't know when this one is supposed to be called
  344. */
  345. esp_err_t AC101_i2s_config_clock(ac_i2s_clock_t *cfg) {
  346. esp_err_t res = 0;
  347. uint16_t regval=0;
  348. regval = i2c_read_reg(I2S1LCK_CTRL);
  349. regval &= 0xe03f;
  350. regval |= (cfg->bclk_div << 9);
  351. regval |= (cfg->lclk_div << 6);
  352. res = i2c_write_reg(I2S1LCK_CTRL, regval);
  353. return res;
  354. }
  355. /****************************************************************************************
  356. *
  357. */
  358. esp_err_t ac101_get_voice_volume(int* volume) {
  359. *volume = ac101_get_earph_volume();
  360. return 0;
  361. }
  362. /*
  363. void ac101_pa_power(bool enable) {
  364. gpio_config_t io_conf;
  365. memset(&io_conf, 0, sizeof(io_conf));
  366. io_conf.intr_type = GPIO_PIN_INTR_DISABLE;
  367. io_conf.mode = GPIO_MODE_OUTPUT;
  368. io_conf.pin_bit_mask = BIT(GPIO_PA_EN);
  369. io_conf.pull_down_en = 0;
  370. io_conf.pull_up_en = 0;
  371. gpio_config(&io_conf);
  372. if (enable) {
  373. gpio_set_level(GPIO_PA_EN, 1);
  374. } else {
  375. gpio_set_level(GPIO_PA_EN, 0);
  376. }
  377. }
  378. */