Audio System + Drivers (#562)
This commit is contained in:
@@ -2,7 +2,6 @@
|
||||
#include "devices/Power.h"
|
||||
|
||||
#include <tactility/device.h>
|
||||
#include <tactility/drivers/i2c_controller.h>
|
||||
#include <drivers/m5pm1.h>
|
||||
|
||||
#include <Tactility/hal/Configuration.h>
|
||||
@@ -13,87 +12,25 @@ using namespace tt::hal;
|
||||
|
||||
static constexpr auto* TAG = "StickS3";
|
||||
|
||||
static constexpr uint8_t ES8311_I2C_ADDR = 0x18;
|
||||
|
||||
static error_t initSound(::Device* i2c_controller) {
|
||||
// Init data from M5Unified:
|
||||
// https://github.com/m5stack/M5Unified/blob/master/src/M5Unified.cpp#L454
|
||||
static constexpr uint8_t ENABLED_BULK_DATA[] = {
|
||||
0x00, 0x80, // 0x00 RESET/ CSM POWER ON
|
||||
0x01, 0x3F, // 0x01 CLOCK_MANAGER/ use MCLK pin (external), all clocks on
|
||||
0x02, 0x00, // 0x02 CLOCK_MANAGER/ pre_div=1 pre_multi=1 (256x MCLK from ESP32 is correct ratio)
|
||||
0x0D, 0x01, // 0x0D SYSTEM/ Power up analog circuitry
|
||||
0x12, 0x00, // 0x12 SYSTEM/ power-up DAC - NOT default
|
||||
0x13, 0x10, // 0x13 SYSTEM/ Enable output to HP drive - NOT default
|
||||
0x32, 0xBF, // 0x32 DAC/ DAC volume (0xBF == ±0 dB )
|
||||
0x37, 0x08, // 0x37 DAC/ Bypass DAC equalizer - NOT default
|
||||
};
|
||||
|
||||
error_t error = i2c_controller_write_register_array(
|
||||
i2c_controller,
|
||||
ES8311_I2C_ADDR,
|
||||
ENABLED_BULK_DATA,
|
||||
sizeof(ENABLED_BULK_DATA),
|
||||
pdMS_TO_TICKS(1000)
|
||||
);
|
||||
if (error != ERROR_NONE) {
|
||||
LOG_E(TAG, "Failed to enable ES8311: %s", error_to_string(error));
|
||||
return error;
|
||||
}
|
||||
|
||||
// Enable speaker amp via M5PM1 driver
|
||||
// Audio codec register programming is owned by the es8311-module driver (registered as an
|
||||
// AUDIO_CODEC_TYPE device, see m5stack,sticks3.dts). This board still needs to drive the
|
||||
// AW8737 speaker amplifier's enable line via m5pm1 (PM1_G3), which is board wiring glue
|
||||
// rather than codec configuration, so it stays here.
|
||||
static void enableSpeakerAmplifier() {
|
||||
auto* m5pm1 = device_find_by_name("m5pm1");
|
||||
if (m5pm1 != nullptr) {
|
||||
m5pm1_set_speaker_enable(m5pm1, true);
|
||||
} else {
|
||||
LOG_W(TAG, "m5pm1 not found — speaker amp not enabled");
|
||||
if (m5pm1 == nullptr) {
|
||||
LOG_W(TAG, "m5pm1 not found -- speaker amp not enabled");
|
||||
return;
|
||||
}
|
||||
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
static error_t initMicrophone(::Device* i2c_controller) {
|
||||
// Init data from M5Unified:
|
||||
// https://github.com/m5stack/M5Unified/blob/master/src/M5Unified.cpp#L842
|
||||
static constexpr uint8_t ENABLED_BULK_DATA[] = {
|
||||
0x00, 0x80, // 0x00 RESET/ CSM POWER ON
|
||||
0x01, 0x3F, // 0x01 CLOCK_MANAGER/ use MCLK pin (external), all clocks on
|
||||
0x02, 0x00, // 0x02 CLOCK_MANAGER/ pre_div=1 pre_multi=1 (256x MCLK from ESP32 is correct ratio)
|
||||
0x0D, 0x01, // 0x0D SYSTEM/ Power up analog circuitry
|
||||
0x0E, 0x02, // 0x0E SYSTEM/ : Enable analog PGA, enable ADC modulator
|
||||
0x14, 0x10, // ES8311_ADC_REG14 : select Mic1p-Mic1n / PGA GAIN (minimum)
|
||||
0x17, 0xFF, // ES8311_ADC_REG17 : ADC_VOLUME (MAXGAIN) // (0xBF == ± 0 dB )
|
||||
0x1C, 0x6A, // ES8311_ADC_REG1C : ADC Equalizer bypass, cancel DC offset in digital domain
|
||||
};
|
||||
|
||||
error_t error = i2c_controller_write_register_array(
|
||||
i2c_controller,
|
||||
ES8311_I2C_ADDR,
|
||||
ENABLED_BULK_DATA,
|
||||
sizeof(ENABLED_BULK_DATA),
|
||||
pdMS_TO_TICKS(1000)
|
||||
);
|
||||
error_t error = m5pm1_set_speaker_enable(m5pm1, true);
|
||||
if (error != ERROR_NONE) {
|
||||
LOG_E(TAG, "Failed to enable ES8311: %s", error_to_string(error));
|
||||
return error;
|
||||
LOG_E(TAG, "Failed to enable speaker amplifier: %s", error_to_string(error));
|
||||
}
|
||||
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
bool initBoot() {
|
||||
auto* i2c_internal = device_find_by_name("i2c_internal");
|
||||
check(i2c_internal, "i2c_internal not found");
|
||||
|
||||
error_t error = initSound(i2c_internal);
|
||||
if (error != ERROR_NONE) {
|
||||
LOG_E(TAG, "Failed to enable ES8311 speaker");
|
||||
}
|
||||
|
||||
error = initMicrophone(i2c_internal);
|
||||
if (error != ERROR_NONE) {
|
||||
LOG_E(TAG, "Failed to enable ES8311 microphone");
|
||||
}
|
||||
enableSpeakerAmplifier();
|
||||
return driver::pwmbacklight::init(GPIO_NUM_38, 512);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user