1 /*
2  AC101 - a AC101 Codec driver library for ESP-IDF
3  ported to esp-idf by Thomas Hopman from the Arduino driver made by Ivo Pullens
4 
5  AC101 - An AC101 Codec driver library for Arduino
6  Copyright (C) 2019, Ivo Pullens, Emmission
7 
8  Inspired by:
9  https://github.com/donny681/esp-adf/tree/master/components/audio_hal/driver/AC101
10 
11  This program is free software: you can redistribute it and/or modify
12  it under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  This program is distributed in the hope that it will be useful,
17  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  GNU General Public License for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with this program.  If not, see <http://www.gnu.org/licenses/>.
23  */
24 
25 #include <stdio.h>
26 #include <string.h>
27 #include <stdint.h>
28 #include "AC101.h"
29 #include "driver/i2c.h"
30 #include "driver/gpio.h"
31 #include "esp_err.h"
32 #include "esp_log.h"
33 
34 // Constructor.
AC101()35 AC101::AC101()
36 {}
37 
38 // Initialize the I2C interface
InitI2C(void)39 esp_err_t AC101::InitI2C(void)
40 {
41     i2c_port_t i2c_master_port = (i2c_port_t) I2C_MASTER_NUM;
42     i2c_config_t conf;
43     memset(&conf, 0, sizeof(i2c_config_t));
44     conf.mode = I2C_MODE_MASTER;
45     conf.sda_io_num = (gpio_num_t) I2C_MASTER_SDA_IO;
46     conf.sda_pullup_en = GPIO_PULLUP_ENABLE;
47     conf.scl_io_num = (gpio_num_t) I2C_MASTER_SCL_IO;
48     conf.scl_pullup_en = GPIO_PULLUP_ENABLE;
49     conf.master.clk_speed = I2C_MASTER_FREQ_HZ;
50     i2c_param_config(i2c_master_port, &conf);
51     return i2c_driver_install(i2c_master_port, conf.mode,
52                               I2C_MASTER_RX_BUF_DISABLE,
53                               I2C_MASTER_TX_BUF_DISABLE, 0);
54 }
55 
56 // AC101 begin
57 // Initialize codec, using provided I2C pins and bus frequency.
58 // sets up 24 bit 48Khz sample-rate
59 // enables line input, line (headphone) output and turns on the speaker outputs
60 // @return false on success, true on failure.
begin()61 esp_err_t AC101::begin()
62 {
63     if (InitI2C()) return -1;
64     esp_err_t res = WriteReg(CHIP_AUDIO_RS, 0x123);
65     WriteReg(CHIP_AUDIO_RS, 0x123);
66 
67     vTaskDelay(1000 / portTICK_PERIOD_MS);
68     if (ESP_OK != res) {
69         ESP_LOGE(AC101_TAG, "reset failed!");
70         return res;
71     } else {
72         ESP_LOGI(AC101_TAG, "reset succeed");
73     }
74 
75     res |= WriteReg(SPKOUT_CTRL, 0xe880);
76 
77     // Enable the PLL from 512*48KHz MCLK source 24.576Mhz
78     res |= WriteReg(PLL_CTRL1, 0x014f);
79     res |= WriteReg(PLL_CTRL2, 0x8030);                // N = 96 @ 0x8600 mclk 12.288Mhz // N = 3 @ 0x8030 mclk 24.576Mhz
80 
81     // Clocking system
82     res |= WriteReg(SYSCLK_CTRL, 0x8b08);
83     res |= WriteReg(MOD_CLK_ENA, 0x800c);
84     res |= WriteReg(MOD_RST_CTRL, 0x800c);
85 
86     // Set default at I2S, 48KHz, 24bit
87     res |= SetI2sSampleRate(SAMPLE_RATE_48000);
88     res |= SetI2sClock(BCLK_DIV_8, false, LRCK_DIV_64, false);
89     res |= SetI2sMode(MODE_SLAVE);
90     res |= SetI2sWordSize(WORD_SIZE_24_BITS);
91     res |= SetI2sFormat(DATA_FORMAT_I2S);
92 
93     // AIF config
94     res |= WriteReg(I2S1_SDOUT_CTRL, 0xc000);
95     res |= WriteReg(I2S1_SDIN_CTRL, 0xc000);
96     res |= WriteReg(I2S1_MXR_SRC, 0x2200);
97 
98     res |= WriteReg(ADC_SRCBST_CTRL, 0xccc4);
99     res |= WriteReg(ADC_SRC, 0x0408);                // 0x2020 for mic input // 0x0408 for line input
100     res |= WriteReg(ADC_DIG_CTRL, 0x8000);
101     res |= WriteReg(ADC_APC_CTRL, 0xbbc3);
102 
103     // Path Configuration
104     res |= WriteReg(DAC_MXR_SRC, 0xcc00);
105     res |= WriteReg(DAC_DIG_CTRL, 0x8000);
106     res |= WriteReg(OMIXER_SR, 0x0081);
107     res |= WriteReg(OMIXER_DACA_CTRL, 0xf080);
108 
109     res |= SetMode( MODE_DAC );
110 
111     ESP_LOGI(AC101_TAG, "init done");
112     ac101_pa_power(true);
113     return res;
114 }
115 
116 // Get speaker volume.
117 // @return Speaker volume, [63..0] for [0..-43.5] [dB], in increments of 2.
GetVolumeSpeaker()118 uint8_t AC101::GetVolumeSpeaker()
119 {
120     // Times 2, to scale to same range as headphone volume
121     return (ReadReg(SPKOUT_CTRL) & 31) * 2;
122 }
123 
124 // Set speaker volume.
125 // @param volume   Target volume, [63..0] for [0..-43.5] [dB], in increments of 2.
126 // @return false on success, true on failure.
SetVolumeSpeaker(uint8_t volume)127 esp_err_t AC101::SetVolumeSpeaker(uint8_t volume)
128 {
129     // Divide by 2, as it is scaled to same range as headphone volume
130     volume /= 2;
131     if (volume > 31) volume = 31;
132 
133     uint16_t val = ReadReg(SPKOUT_CTRL);
134     val &= ~31;
135     val |= volume;
136     return WriteReg(SPKOUT_CTRL, val);
137 }
138 
139 // Get headphone volume.
140 // @return Headphone volume, [63..0] for [0..-62] [dB]
GetVolumeHeadphone()141 uint8_t AC101::GetVolumeHeadphone()
142 {
143     return (ReadReg(HPOUT_CTRL) >> 4) & 63;
144 }
145 
146 // Set headphone volume
147 // @param volume   Target volume, [63..0] for [0..-62] [dB]
148 // @return false on success, true on failure.
SetVolumeHeadphone(uint8_t volume)149 esp_err_t AC101::SetVolumeHeadphone(uint8_t volume)
150 {
151     if (volume > 63) volume = 63;
152     uint16_t val = ReadReg(HPOUT_CTRL);
153     val &= ~63 << 4;
154     val |= volume << 4;
155     return WriteReg(HPOUT_CTRL, val);
156 }
157 
158 // Configure I2S samplerate.
159 // @param rate   Samplerate.
160 // @return false on success, true on failure.
SetI2sSampleRate(I2sSampleRate_t rate)161 esp_err_t AC101::SetI2sSampleRate(I2sSampleRate_t rate)
162 {
163     return WriteReg(I2S_SR_CTRL, rate);
164 }
165 
166 // Configure I2S mode (master/slave).
167 // @param mode   Mode.
168 // @return false on success, true on failure.
SetI2sMode(I2sMode_t mode)169 esp_err_t AC101::SetI2sMode(I2sMode_t mode)
170 {
171     uint16_t val = ReadReg(I2S1LCK_CTRL);
172     val &= ~0x8000;
173     val |= uint16_t(mode) << 15;
174     return WriteReg(I2S1LCK_CTRL, val);
175 }
176 
177 // Configure I2S word size (8/16/20/24 bits).
178 // @param size   Word size.
179 // @return false on success, true on failure.
SetI2sWordSize(I2sWordSize_t size)180 esp_err_t AC101::SetI2sWordSize(I2sWordSize_t size)
181 {
182     uint16_t val = ReadReg(I2S1LCK_CTRL);
183     val &= ~0x0030;
184     val |= uint16_t(size) << 4;
185     return WriteReg(I2S1LCK_CTRL, val);
186 }
187 
188 // Configure I2S format (I2S/Left/Right/Dsp).
189 // @param format   I2S format.
190 // @return false on success, true on failure.
SetI2sFormat(I2sFormat_t format)191 esp_err_t AC101::SetI2sFormat(I2sFormat_t format)
192 {
193     uint16_t val = ReadReg(I2S1LCK_CTRL);
194     val &= ~0x000C;
195     val |= uint16_t(format) << 2;
196     return WriteReg(I2S1LCK_CTRL, val);
197 }
198 
199 // Configure I2S clock.
200 // @param bitClockDiv   I2S1CLK/BCLK1 ratio.
201 // @param bitClockInv   I2S1 BCLK Polarity.
202 // @param lrClockDiv    BCLK1/LRCK ratio.
203 // @param lrClockInv    I2S1 LRCK Polarity.
204 // @return false on success, true on failure.
SetI2sClock(I2sBitClockDiv_t bitClockDiv,bool bitClockInv,I2sLrClockDiv_t lrClockDiv,bool lrClockInv)205 esp_err_t AC101::SetI2sClock(I2sBitClockDiv_t bitClockDiv, bool bitClockInv, I2sLrClockDiv_t lrClockDiv, bool lrClockInv)
206 {
207     uint16_t val = ReadReg(I2S1LCK_CTRL);
208     val &= ~0x7FC0;
209     val |= uint16_t(bitClockInv ? 1 : 0) << 14;
210     val |= uint16_t(bitClockDiv) << 9;
211     val |= uint16_t(lrClockInv ? 1 : 0) << 13;
212     val |= uint16_t(lrClockDiv) << 6;
213     return WriteReg(I2S1LCK_CTRL, val);
214 }
215 
216 // Configure the mode (Adc/Dac/Adc+Dac/Line)
217 // @param mode    Operating mode.
218 // @return false on success, true on failure.
SetMode(Mode_t mode)219 esp_err_t AC101::SetMode(Mode_t mode)
220 {
221     esp_err_t ret = ESP_OK;
222     if (MODE_LINE == mode) {
223         ret |= WriteReg(ADC_SRC, 0x0408);
224         ret |= WriteReg(ADC_DIG_CTRL, 0x8000);
225         ret |= WriteReg(ADC_APC_CTRL, 0x3bc0);
226     }
227 
228     if ((MODE_ADC == mode) or (MODE_ADC_DAC == mode) or (MODE_LINE == mode)) {
229         ret |= WriteReg(MOD_CLK_ENA,  0x800c);
230         ret |= WriteReg(MOD_RST_CTRL, 0x800c);
231     }
232 
233     if ((MODE_DAC == mode) or (MODE_ADC_DAC == mode) or (MODE_LINE == mode)) {
234         // Enable Headphone output
235         ret |= WriteReg(OMIXER_DACA_CTRL, 0xff80);
236         ret |= WriteReg(HPOUT_CTRL, 0xc3c1);
237         ret |= WriteReg(HPOUT_CTRL, 0xcb00);
238         vTaskDelay(100 / portTICK_PERIOD_MS);
239         ret |= WriteReg(HPOUT_CTRL, 0xfbc0);
240         ret |= SetVolumeHeadphone(30);
241 
242         // Enable Speaker output
243         ret |= WriteReg(SPKOUT_CTRL, 0xeabd);
244         vTaskDelay(10 / portTICK_PERIOD_MS);
245         ret |= SetVolumeSpeaker(30);
246     }
247     return ret;
248 }
249 
250 // AC101 DumpRegister
251 // prints out contents of the AC101 registers in hex
DumpRegisters()252 void AC101::DumpRegisters()
253 {
254     for (size_t i = 0; i < ARRAY_SIZE(regs); ++i) {
255         printf("%02x", regs[i]);
256         printf(" = ");
257         printf("%04x", ReadReg(regs[i]));
258         printf("\n");
259     }
260 }
261 
262 // AC101 PA Power
263 // enables or disables the speaker outputs
ac101_pa_power(bool enable)264 void AC101::ac101_pa_power(bool enable)
265 {
266     gpio_config_t  io_conf;
267     memset(&io_conf, 0, sizeof(io_conf));
268     io_conf.intr_type = (gpio_int_type_t) GPIO_PIN_INTR_DISABLE;
269     io_conf.mode = GPIO_MODE_OUTPUT;
270     io_conf.pin_bit_mask = BIT(GPIO_PA_EN);
271     io_conf.pull_down_en = (gpio_pulldown_t) 0;
272     io_conf.pull_up_en = (gpio_pullup_t) 0;
273     gpio_config(&io_conf);
274     if (enable) {
275         gpio_set_level(GPIO_PA_EN, 1);
276     } else {
277         gpio_set_level(GPIO_PA_EN, 0);
278     }
279 }
280 
281 // printBits
282 // print out any value in binary format used for debugging
printBits(size_t const size,void const * const ptr)283 void AC101::printBits(size_t const size, void const* const ptr)
284 {
285     unsigned char *b = (unsigned char*)ptr;
286     unsigned char byte;
287     int i, j;
288 
289     for (i = size-1; i >= 0; i--) {
290         for (j = 7; j >= 0; j--) {
291             byte = (b[i] >> j) & 1;
292             printf("%u", byte);
293         }
294     }
295     puts("");
296 }
297 // printRead
298 // print out the contents of any ac101 register used for debugging
printRead(uint8_t reg)299 void AC101::printRead(uint8_t reg)
300 {
301     uint8_t buf[2];
302     ReadReg_Full(reg, buf, 2);
303     printBits(sizeof(buf), &buf);
304 }
305 
306 // Write to a register of the AC101
307 // reg: Register Address
308 // val: Value to be written
309 // @return false on success, true on failure.
WriteReg(uint8_t reg,uint16_t val)310 esp_err_t AC101::WriteReg(uint8_t reg, uint16_t val)
311 {
312     esp_err_t ret = ESP_OK;
313     uint8_t buf[2];
314     buf[0] = uint8_t((val >> 8) & 0xff);
315     buf[1] = uint8_t(val & 0xff);
316 
317     i2c_cmd_handle_t cmd = i2c_cmd_link_create();
318     ret |= i2c_master_start(cmd);
319     ret |= i2c_master_write_byte(cmd, (AC101_ADDR << 1) | WRITE_BIT, ACK_CHECK_EN);
320     ret |= i2c_master_write_byte(cmd, reg, ACK_CHECK_EN);
321     ret |= i2c_master_write(cmd, buf, 2, ACK_CHECK_EN);
322     ret |= i2c_master_stop(cmd);
323     ret |= i2c_master_cmd_begin((i2c_port_t) I2C_MASTER_NUM, cmd, 1000 / portTICK_RATE_MS);
324     i2c_cmd_link_delete(cmd);
325     return ret;
326 }
327 
328 // Read a register of the AC101
329 // reg: Register Address to be read
330 // @return false on success, true on failure.
ReadReg(uint8_t reg)331 uint16_t AC101::ReadReg(uint8_t reg)
332 {
333     uint8_t data_rd[2];
334     ReadReg_Full(reg, data_rd, 2);
335     uint16_t val = (data_rd[0]<<8) + data_rd[1];
336     return val;
337 }
338 
339 // AC101 read register
340 // Reads the value of the AC101 register Address
341 // reg: Register Address
342 // data_rd: Pointer to return value
343 // size: size of data to be read
344 // @return false on success, true on failure.
ReadReg_Full(uint8_t reg,uint8_t * data_rd,size_t size)345 esp_err_t AC101::ReadReg_Full(uint8_t reg, uint8_t* data_rd, size_t size)
346 {
347     if (size == 0) {
348         return ESP_OK;
349     }
350     esp_err_t ret = ESP_OK;
351     i2c_cmd_handle_t cmd = i2c_cmd_link_create();
352     ret |= i2c_master_start(cmd);
353     ret |= i2c_master_write_byte(cmd, (AC101_ADDR << 1), ACK_CHECK_EN);
354     ret |= i2c_master_write_byte(cmd, reg, ACK_CHECK_EN);
355     ret |= i2c_master_start(cmd);
356     ret |= i2c_master_write_byte(cmd, (AC101_ADDR << 1) | READ_BIT, ACK_CHECK_EN);
357     if (size > 1) {
358         ret |= i2c_master_read(cmd, data_rd, size - 1, ACK_VAL);
359     }
360     ret |= i2c_master_read_byte(cmd, data_rd + size - 1, NACK_VAL);
361     ret |= i2c_master_stop(cmd);
362     ret |= i2c_master_cmd_begin((i2c_port_t) I2C_MASTER_NUM, cmd, 1000 / portTICK_RATE_MS);
363     i2c_cmd_link_delete(cmd);
364     return ret;
365 }
366