summaryrefslogtreecommitdiff
path: root/src/drivers
diff options
context:
space:
mode:
authorjacqueline <me@jacqueline.id.au>2022-12-06 13:17:56 +1100
committerjacqueline <me@jacqueline.id.au>2022-12-06 13:17:56 +1100
commitf35bb64c2b8dbb72fd15f1880e4d01d263660910 (patch)
tree4696065116c21147da02b6e6470a6215b343081f /src/drivers
parente0b2562cc4e0e5ae73efacddf68b83bd9fbb6acb (diff)
downloadtangara-fw-f35bb64c2b8dbb72fd15f1880e4d01d263660910.tar.gz
basic i2s output element
Diffstat (limited to 'src/drivers')
-rw-r--r--src/drivers/CMakeLists.txt2
-rw-r--r--src/drivers/dac.cpp80
-rw-r--r--src/drivers/include/dac.hpp21
3 files changed, 94 insertions, 9 deletions
diff --git a/src/drivers/CMakeLists.txt b/src/drivers/CMakeLists.txt
index fbecf1c8..4c8d4275 100644
--- a/src/drivers/CMakeLists.txt
+++ b/src/drivers/CMakeLists.txt
@@ -2,5 +2,5 @@ idf_component_register(
SRCS "dac.cpp" "gpio_expander.cpp" "battery.cpp" "storage.cpp" "i2c.cpp"
"spi.cpp" "display.cpp" "display_init.cpp"
INCLUDE_DIRS "include"
- REQUIRES "esp_adc_cal" "fatfs" "result" "lvgl")
+ REQUIRES "esp_adc_cal" "fatfs" "result" "lvgl" "span")
target_compile_options(${COMPONENT_LIB} PRIVATE ${EXTRA_WARNINGS})
diff --git a/src/drivers/dac.cpp b/src/drivers/dac.cpp
index f01d701f..23c67e88 100644
--- a/src/drivers/dac.cpp
+++ b/src/drivers/dac.cpp
@@ -4,11 +4,13 @@
#include "assert.h"
#include "driver/i2c.h"
+#include "driver/i2s.h"
#include "esp_err.h"
#include "esp_log.h"
#include "hal/i2c_types.h"
#include "gpio_expander.hpp"
+#include "hal/i2s_types.h"
#include "i2c.hpp"
namespace drivers {
@@ -16,11 +18,55 @@ namespace drivers {
static const char* kTag = "AUDIODAC";
static const uint8_t kPcm5122Address = 0x4C;
static const uint8_t kPcm5122Timeout = 100 / portTICK_RATE_MS;
+static const i2s_port_t kI2SPort = I2S_NUM_0;
+
+static const AudioDac::SampleRate kDefaultSampleRate =
+ AudioDac::SAMPLE_RATE_44_1;
+static const AudioDac::BitsPerSample kDefaultBps = AudioDac::BPS_16;
auto AudioDac::create(GpioExpander* expander)
-> cpp::result<std::unique_ptr<AudioDac>, Error> {
+ // First, instantiate the instance so it can do all of its power on
+ // configuration.
std::unique_ptr<AudioDac> dac = std::make_unique<AudioDac>(expander);
+ // Whilst we wait for the initial boot, we can work on installing the I2S
+ // driver.
+ i2s_config_t i2s_config = {
+ // static_cast bc esp-adf uses enums incorrectly
+ .mode = static_cast<i2s_mode_t>(I2S_MODE_MASTER | I2S_MODE_TX),
+ .sample_rate = 44100,
+ .bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT,
+ .channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT,
+ .communication_format = I2S_COMM_FORMAT_STAND_I2S,
+ .intr_alloc_flags = ESP_INTR_FLAG_LOWMED,
+ // TODO(jacqueline): tune dma buffer size. this seems very smol.
+ .dma_buf_count = 8,
+ .dma_buf_len = 64,
+ .use_apll = false,
+ .tx_desc_auto_clear = false,
+ .fixed_mclk = 0,
+ .mclk_multiple = I2S_MCLK_MULTIPLE_DEFAULT,
+ .bits_per_chan = I2S_BITS_PER_CHAN_DEFAULT,
+ };
+
+ if (esp_err_t err =
+ i2s_driver_install(kI2SPort, &i2s_config, 0, NULL) != ESP_OK) {
+ ESP_LOGE(kTag, "failed to configure i2s pins %x", err);
+ return cpp::fail(Error::FAILED_TO_INSTALL_I2S);
+ }
+
+ i2s_pin_config_t pin_config = {.mck_io_num = GPIO_NUM_0,
+ .bck_io_num = GPIO_NUM_26,
+ .ws_io_num = GPIO_NUM_27,
+ .data_out_num = GPIO_NUM_5,
+ .data_in_num = I2S_PIN_NO_CHANGE};
+ if (esp_err_t err = i2s_set_pin(kI2SPort, &pin_config) != ESP_OK) {
+ ESP_LOGE(kTag, "failed to configure i2s pins %x", err);
+ return cpp::fail(Error::FAILED_TO_INSTALL_I2S);
+ }
+
+ // Now let's double check that the DAC itself came up whilst we we working.
bool is_booted = dac->WaitForPowerState(
[](bool booted, PowerState state) { return booted; });
if (!is_booted) {
@@ -28,8 +74,9 @@ auto AudioDac::create(GpioExpander* expander)
return cpp::fail(Error::FAILED_TO_BOOT);
}
+ // Write the initial configuration.
dac->WriteRegister(Register::DE_EMPHASIS, 1 << 4);
- dac->WriteVolume(100);
+ dac->WriteVolume(255);
bool is_configured =
dac->WaitForPowerState([](bool booted, PowerState state) {
@@ -43,14 +90,16 @@ auto AudioDac::create(GpioExpander* expander)
return dac;
}
-AudioDac::AudioDac(GpioExpander* gpio) {
- this->gpio_ = gpio;
-};
+AudioDac::AudioDac(GpioExpander* gpio) : gpio_(gpio) {
+ gpio_->set_pin(GpioExpander::AUDIO_POWER_ENABLE, true);
+ gpio_->Write();
+}
-AudioDac::~AudioDac(){
- // TODO: reset stuff like de-emphasis? Reboot the whole dac? Need to think
- // about this.
-};
+AudioDac::~AudioDac() {
+ i2s_driver_uninstall(kI2SPort);
+ gpio_->set_pin(GpioExpander::AUDIO_POWER_ENABLE, false);
+ gpio_->Write();
+}
void AudioDac::WriteVolume(uint8_t volume) {
WriteRegister(Register::DIGITAL_VOLUME_L, volume);
@@ -93,6 +142,21 @@ bool AudioDac::WaitForPowerState(
return has_matched;
}
+auto AudioDac::Reconfigure(BitsPerSample bps, SampleRate rate) -> bool {
+ // TODO(jacqueline): investigate how reliable the auto-clocking of the dac
+ // is. We might need to explicit reconfigure the dac here as well if it's not
+ // good enough.
+ i2s_set_clk(kI2SPort, rate, bps, I2S_CHANNEL_STEREO);
+ return true;
+}
+
+auto AudioDac::WriteData(const cpp::span<std::byte>& data, TickType_t max_wait)
+ -> std::size_t {
+ std::size_t res = 0;
+ i2s_write(kI2SPort, data.data(), data.size(), &res, max_wait);
+ return res;
+}
+
void AudioDac::WriteRegister(Register reg, uint8_t val) {
I2CTransaction transaction;
transaction.start()
diff --git a/src/drivers/include/dac.hpp b/src/drivers/include/dac.hpp
index 6d812b6a..dc03624b 100644
--- a/src/drivers/include/dac.hpp
+++ b/src/drivers/include/dac.hpp
@@ -5,7 +5,10 @@
#include <functional>
#include "esp_err.h"
+#include "freertos/portmacro.h"
+#include "hal/i2s_types.h"
#include "result.hpp"
+#include "span.hpp"
#include "gpio_expander.hpp"
@@ -19,7 +22,9 @@ class AudioDac {
enum Error {
FAILED_TO_BOOT,
FAILED_TO_CONFIGURE,
+ FAILED_TO_INSTALL_I2S,
};
+
static auto create(GpioExpander* expander)
-> cpp::result<std::unique_ptr<AudioDac>, Error>;
@@ -47,6 +52,22 @@ class AudioDac {
/* Returns the current boot-up status and internal state of the DAC */
std::pair<bool, PowerState> ReadPowerState();
+ enum BitsPerSample {
+ BPS_16 = I2S_BITS_PER_SAMPLE_16BIT,
+ BPS_24 = I2S_BITS_PER_SAMPLE_24BIT,
+ BPS_32 = I2S_BITS_PER_SAMPLE_32BIT
+ };
+ enum SampleRate {
+ SAMPLE_RATE_44_1 = 44100,
+ SAMPLE_RATE_48 = 48000,
+ };
+
+ // TODO(jacqueline): worth supporting channels here as well?
+ auto Reconfigure(BitsPerSample bps, SampleRate rate) -> bool;
+
+ auto WriteData(const cpp::span<std::byte>& data, TickType_t max_wait)
+ -> std::size_t;
+
// Not copyable or movable.
AudioDac(const AudioDac&) = delete;
AudioDac& operator=(const AudioDac&) = delete;