Init
This commit is contained in:
@@ -0,0 +1,89 @@
|
||||
#include "es8311.h"
|
||||
|
||||
#include "drivers/i2s.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
|
||||
#include "driver/i2s_std.h"
|
||||
|
||||
#include "i2c.h"
|
||||
#include "pins.h"
|
||||
|
||||
void es8311_write(uint8_t reg, uint8_t val) {
|
||||
i2c_write_reg(ES8311_I2C_ADDR, reg, val);
|
||||
}
|
||||
|
||||
void es8311_set_dac_volume(uint8_t vol) {
|
||||
es8311_write(ES8311_DAC_VOL_L, vol);
|
||||
es8311_write(ES8311_DAC_VOL_R, vol);
|
||||
}
|
||||
|
||||
void es8311_set_adc_volume(uint8_t vol) { es8311_write(ES8311_ADC_VOL, vol); }
|
||||
|
||||
void es8311_init(void) {
|
||||
es8311_write(ES8311_RESET, ES8311_RESET_CMD);
|
||||
|
||||
vTaskDelay(pdMS_TO_TICKS(20));
|
||||
|
||||
// enable ADC + DAC
|
||||
es8311_write(ES8311_PWR_UP_DOWN, ES8311_PWR_DAC_EN | ES8311_PWR_ADC_EN);
|
||||
|
||||
es8311_write(ES8311_PWR_ANALOG, ES8311_ALL_ON);
|
||||
|
||||
// I2S 16-bit
|
||||
es8311_write(ES8311_SER_FMT, ES8311_FMT_I2S | ES8311_WORD_LEN_16);
|
||||
|
||||
// ADC config
|
||||
es8311_write(ES8311_ADC_CTRL1, 0x44);
|
||||
|
||||
es8311_write(ES8311_ADC_CTRL2, 0x0C);
|
||||
|
||||
// DAC config
|
||||
es8311_write(ES8311_DAC_CTRL1, 0x04);
|
||||
|
||||
es8311_write(ES8311_DAC_CTRL2, 0x0C);
|
||||
|
||||
es8311_set_dac_volume(0xBF);
|
||||
es8311_set_adc_volume(0x88);
|
||||
|
||||
es8311_write(ES8311_SPK_CTRL, 0xFF);
|
||||
|
||||
audio_i2s_init();
|
||||
}
|
||||
|
||||
int audio_write(const int16_t *samples, size_t count) {
|
||||
size_t written;
|
||||
|
||||
esp_err_t r = i2s_channel_write(i2s_tx, samples, count * sizeof(int16_t),
|
||||
&written, portMAX_DELAY);
|
||||
|
||||
if (r != ESP_OK)
|
||||
return -1;
|
||||
|
||||
return written / sizeof(int16_t);
|
||||
}
|
||||
|
||||
int audio_read(int16_t *samples, size_t count) {
|
||||
size_t read;
|
||||
|
||||
esp_err_t r = i2s_channel_read(i2s_rx, samples, count * sizeof(int16_t),
|
||||
&read, portMAX_DELAY);
|
||||
|
||||
if (r != ESP_OK)
|
||||
return -1;
|
||||
|
||||
return read / sizeof(int16_t);
|
||||
}
|
||||
|
||||
void audio_beep(void) {
|
||||
static int16_t buf[4410];
|
||||
|
||||
for (int i = 0; i < 4410; i++) {
|
||||
|
||||
int phase = (i / 20) & 1;
|
||||
|
||||
buf[i] = phase ? 12000 : -12000;
|
||||
}
|
||||
|
||||
audio_write(buf, 4410);
|
||||
}
|
||||
Reference in New Issue
Block a user