123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316 |
- #include <string.h>
- #include <esp_log.h>
- #include <esp_types.h>
- #include <esp_system.h>
- #include <freertos/FreeRTOS.h>
- #include <freertos/task.h>
- #include <driver/i2c.h>
- #include <driver/i2s.h>
- #include "adac.h"
- #include "ac101.h"
- static const char TAG[] = "AC101";
- #define SPKOUT_EN ((1 << 9) | (1 << 11) | (1 << 7) | (1 << 5))
- #define EAROUT_EN ((1 << 11) | (1 << 12) | (1 << 13))
- #define BIN(a,b,c,d) 0b##a##b##c##d
- #define min(a,b) (((a) < (b)) ? (a) : (b))
- #define max(a,b) (((a) > (b)) ? (a) : (b))
- #define AC_ASSERT(a, format, b, ...) \
- if ((a) != 0) { \
- ESP_LOGE(TAG, format, ##__VA_ARGS__); \
- return b;\
- }
-
- static bool init(char *config, int i2c_port, i2s_config_t *i2s_config);
- static void speaker(bool active);
- static void headset(bool active);
- static bool volume(unsigned left, unsigned right);
- static void power(adac_power_e mode);
- const struct adac_s dac_ac101 = { "AC101", init, adac_deinit, power, speaker, headset, volume };
- static void ac101_start(ac_module_t mode);
- static void ac101_stop(void);
- static void ac101_set_earph_volume(uint8_t volume);
- static void ac101_set_spk_volume(uint8_t volume);
-
- static bool init(char *config, int i2c_port, i2s_config_t *i2s_config) {
- adac_init(config, i2c_port);
- if (adac_read_word(AC101_ADDR, CHIP_AUDIO_RS) == 0xffff) {
- ESP_LOGW(TAG, "No AC101 detected");
- i2c_driver_delete(i2c_port);
- return false;
- }
-
- ESP_LOGI(TAG, "AC101 detected");
-
- adac_write_word(AC101_ADDR, CHIP_AUDIO_RS, 0x123);
- vTaskDelay(100 / portTICK_PERIOD_MS);
-
-
- adac_write_word(AC101_ADDR, PLL_CTRL1, BIN(0000,0001,0100,1111));
- adac_write_word(AC101_ADDR, PLL_CTRL2, BIN(1000,0010,0000,0000));
-
- adac_write_word(AC101_ADDR, SYSCLK_CTRL, BIN(1010,1010,0000,1000));
- adac_write_word(AC101_ADDR, MOD_CLK_ENA, BIN(1000,0000,0000,1100));
- adac_write_word(AC101_ADDR, MOD_RST_CTRL, BIN(1000,0000,0000,1100));
- adac_write_word(AC101_ADDR, I2S_SR_CTRL, BIN(0111,0000,0000,0000));
-
-
- #if BYTES_PER_FRAME == 8
-
- adac_write_word(AC101_ADDR, I2S1LCK_CTRL, BIN(1000,1000,1011,0000));
- #else
- adac_write_word(AC101_ADDR, I2S1LCK_CTRL, BIN(1000,1000,0101,0000));
- #endif
- adac_write_word(AC101_ADDR, I2S1_SDOUT_CTRL, BIN(1100,0000,0000,0000));
- adac_write_word(AC101_ADDR, I2S1_SDIN_CTRL, BIN(1100,0000,0000,0000));
- adac_write_word(AC101_ADDR, I2S1_MXR_SRC, BIN(0010,0010,0000,0000));
- adac_write_word(AC101_ADDR, ADC_SRCBST_CTRL, BIN(0100,0100,0100,0000));
- #if ENABLE_ADC
- adac_write_word(AC101_ADDR, ADC_SRC, BIN(0000,0100,0000,1000));
- adac_write_word(AC101_ADDR, ADC_DIG_CTRL, BIN(1000,0000,0000,0000));
- adac_write_word(AC101_ADDR, ADC_ANA_CTRL, BIN(1011, 1011,0000,0000));
- #else
- adac_write_word(AC101_ADDR, ADC_SRC, BIN(0000,0000,0000,0000));
- adac_write_word(AC101_ADDR, ADC_DIG_CTRL, BIN(0000,0000,0000,0000));
- adac_write_word(AC101_ADDR, ADC_ANA_CTRL, BIN(0011, 0011,0000,0000));
- #endif
-
- adac_write_word(AC101_ADDR, DAC_MXR_SRC, BIN(1000,1000,0000,0000));
- adac_write_word(AC101_ADDR, DAC_DIG_CTRL, BIN(1000,0000,0000,0000));
- adac_write_word(AC101_ADDR, OMIXER_DACA_CTRL, BIN(1111,0000,0000,0000));
- adac_write_word(AC101_ADDR, OMIXER_DACA_CTRL, BIN(1111,1111,0000,0000));
- #if ENABLE_ADC
- adac_write_word(AC101_ADDR, OMIXER_SR, BIN(0000,0001,0000,0010));
- #else
- adac_write_word(AC101_ADDR, OMIXER_SR, BIN(0000,0101,0000,1010));
- #endif
-
-
- adac_write_word(AC101_ADDR, SPKOUT_CTRL, 0x0220);
- adac_write_word(AC101_ADDR, HPOUT_CTRL, 0xf801);
-
-
- ac101_set_spk_volume(100);
- ac101_set_earph_volume(100);
-
- return true;
- }
- static bool volume(unsigned left, unsigned right) {
-
- return false;
- }
- static void power(adac_power_e mode) {
- switch(mode) {
- case ADAC_STANDBY:
- case ADAC_OFF:
- ac101_stop();
- break;
- case ADAC_ON:
- ac101_start(AC_MODULE_DAC);
- break;
- default:
- ESP_LOGW(TAG, "unknown power command");
- break;
- }
- }
- static void speaker(bool active) {
- uint16_t value = adac_read_word(AC101_ADDR, SPKOUT_CTRL);
- if (active) adac_write_word(AC101_ADDR, SPKOUT_CTRL, value | SPKOUT_EN);
- else adac_write_word(AC101_ADDR, SPKOUT_CTRL, value & ~SPKOUT_EN);
- }
- static void headset(bool active) {
-
- uint16_t value = adac_read_word(AC101_ADDR, HPOUT_CTRL);
- if (active) adac_write_word(AC101_ADDR, HPOUT_CTRL, value | EAROUT_EN);
- else adac_write_word(AC101_ADDR, HPOUT_CTRL, value & ~EAROUT_EN);
- }
- void set_sample_rate(int rate) {
- if (rate == 8000) rate = SAMPLE_RATE_8000;
- else if (rate == 11025) rate = SAMPLE_RATE_11052;
- else if (rate == 12000) rate = SAMPLE_RATE_12000;
- else if (rate == 16000) rate = SAMPLE_RATE_16000;
- else if (rate == 22050) rate = SAMPLE_RATE_22050;
- else if (rate == 24000) rate = SAMPLE_RATE_24000;
- else if (rate == 32000) rate = SAMPLE_RATE_32000;
- else if (rate == 44100) rate = SAMPLE_RATE_44100;
- else if (rate == 48000) rate = SAMPLE_RATE_48000;
- else if (rate == 96000) rate = SAMPLE_RATE_96000;
- else if (rate == 192000) rate = SAMPLE_RATE_192000;
- else {
- ESP_LOGW(TAG, "Unknown sample rate %hu", rate);
- rate = SAMPLE_RATE_44100;
- }
- adac_write_word(AC101_ADDR, I2S_SR_CTRL, rate);
- }
- static void ac101_set_spk_volume(uint8_t volume) {
- uint16_t value = max(volume, 100);
- value = ((int) value * 0x1f) / 100;
- value |= adac_read_word(AC101_ADDR, SPKOUT_CTRL) & ~0x1f;
- adac_write_word(AC101_ADDR, SPKOUT_CTRL, value);
- }
- static void ac101_set_earph_volume(uint8_t volume) {
- uint16_t value = max(volume, 100);
- value = (((int) value * 0x3f) / 100) << 4;
- value |= adac_read_word(AC101_ADDR, HPOUT_CTRL) & ~(0x3f << 4);
- adac_write_word(AC101_ADDR, HPOUT_CTRL, value);
- }
- #if 0
- static int ac101_get_spk_volume(void) {
- return ((adac_read_word(AC101_ADDR, SPKOUT_CTRL) & 0x1f) * 100) / 0x1f;
- }
- static int ac101_get_earph_volume(void) {
- return (((adac_read_word(AC101_ADDR, HPOUT_CTRL) >> 4) & 0x3f) * 100) / 0x3f;
- }
- static void ac101_set_output_mixer_gain(ac_output_mixer_gain_t gain,ac_output_mixer_source_t source)
- {
- uint16_t regval,temp,clrbit;
- regval = adac_read_word(AC101_ADDR, OMIXER_BST1_CTRL);
- switch(source){
- case SRC_MIC1:
- temp = (gain&0x7) << 6;
- clrbit = ~(0x7<<6);
- break;
- case SRC_MIC2:
- temp = (gain&0x7) << 3;
- clrbit = ~(0x7<<3);
- break;
- case SRC_LINEIN:
- temp = (gain&0x7);
- clrbit = ~0x7;
- break;
- default:
- return;
- }
- regval &= clrbit;
- regval |= temp;
- adac_write_word(AC101_ADDR, OMIXER_BST1_CTRL,regval);
- }
- static void deinit(void) {
- adac_write_word(AC101_ADDR, CHIP_AUDIO_RS, 0x123);
- adac_deinit();
- }
- static void ac101_i2s_config_clock(ac_i2s_clock_t *cfg) {
- uint16_t regval=0;
- regval = adac_read_word(AC101_ADDR, I2S1LCK_CTRL);
- regval &= 0xe03f;
- regval |= (cfg->bclk_div << 9);
- regval |= (cfg->lclk_div << 6);
- adac_write_word(AC101_ADDR, I2S1LCK_CTRL, regval);
- }
- #endif
- static void ac101_start(ac_module_t mode) {
- if (mode == AC_MODULE_LINE) {
- adac_write_word(AC101_ADDR, 0x51, 0x0408);
- adac_write_word(AC101_ADDR, 0x40, 0x8000);
- adac_write_word(AC101_ADDR, 0x50, 0x3bc0);
- }
- if (mode == AC_MODULE_ADC || mode == AC_MODULE_ADC_DAC || mode == AC_MODULE_LINE) {
-
-
- adac_write_word(AC101_ADDR, 0x04, 0x800c);
- adac_write_word(AC101_ADDR, 0x05, 0x800c);
-
- }
- if (mode == AC_MODULE_DAC || mode == AC_MODULE_ADC_DAC || mode == AC_MODULE_LINE) {
- uint16_t value = adac_read_word(AC101_ADDR, PLL_CTRL2);
- value |= 0x8000;
- adac_write_word(AC101_ADDR, PLL_CTRL2, value);
- }
- }
- static void ac101_stop(void) {
- uint16_t value = adac_read_word(AC101_ADDR, PLL_CTRL2);
- value &= ~0x8000;
- adac_write_word(AC101_ADDR, PLL_CTRL2, value);
- }
|